{"size": 2362, "ext": "js", "max_stars_repo_path": "public/signal-protocol/test/PreKeyStore_test.js", "max_stars_repo_name": "andeluvian/React-Chat", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "public/signal-protocol/test/PreKeyStore_test.js", "max_issues_repo_name": "andeluvian/React-Chat", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "public/signal-protocol/test/PreKeyStore_test.js", "max_forks_repo_name": "andeluvian/React-Chat", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 38.0967741935, "max_line_length": 76, "alphanum_fraction": 0.6350550381}
var Crypto = require('../src/crypto.js');
var SignalProtocolAddress = require('../src/SignalProtocolAddress.js');
var assert = require('chai').assert;
var test_util = require('./test_helpers.js');
var hexToArrayBuffer = test_util.hexToArrayBuffer;
var assertEqualArrayBuffers = test_util.assertEqualArrayBuffers;

function testPreKeyStore(store) {
  var number = '+5558675309';
  var testKey;
  describe('PreKeyStore', function() {
    before(function(done) {
      Crypto.crypto.createKeyPair().then(function(keyPair) {
        testKey = keyPair;
      }).then(done,done);
    });
    describe('storePreKey', function() {
      it('stores prekeys', function(done) {
        var address = new SignalProtocolAddress(number, 1);
        store.storePreKey(address.toString(), testKey).then(function() {
          return store.loadPreKey(address.toString()).then(function(key) {
            assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
            assertEqualArrayBuffers(key.privKey, testKey.privKey);
          });
        }).then(done,done);
      });
    });
    describe('loadPreKey', function() {
      it('returns prekeys that exist', function(done) {
        var address = new SignalProtocolAddress(number, 1);
        store.storePreKey(address.toString(), testKey).then(function() {
          return store.loadPreKey(address.toString()).then(function(key) {
            assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
            assertEqualArrayBuffers(key.privKey, testKey.privKey);
          });
        }).then(done,done);
      });
      it('returns undefined for prekeys that do not exist', function(done) {
        var address = new SignalProtocolAddress(number, 2);
        store.loadPreKey(2).then(function(key) {
          assert.isUndefined(key);
        }).then(done,done);
      });
    });
    describe('removePreKey', function() {
      it('deletes prekeys', function(done) {
        var address = new SignalProtocolAddress(number, 2);
        before(function(done) {
          store.storePreKey(address.toString(), testKey).then(done);
        });
        store.removePreKey(address.toString()).then(function() {
          return store.loadPreKey(address.toString()).then(function(key) {
            assert.isUndefined(key);
          });
        }).then(done,done);
      });
    });
  });
}

module.exports = testPreKeyStore;
