signal-desktop/test/storage_test.js

198 lines
7.9 KiB
JavaScript
Raw Normal View History

/*
* vim: ts=4:sw=4:expandtab
*/
'use strict';
2016-04-21 23:45:21 +00:00
describe("SignalProtocolStore", function() {
2015-05-14 22:44:43 +00:00
before(function(done) {
storage.put('registrationId', 1337);
storage.put('identityKey', identityKey);
storage.fetch().then(done, done);
});
2016-04-22 20:39:05 +00:00
var store = textsecure.storage.protocol;
var identifier = '+5558675309';
var identityKey = {
pubKey: libsignal.crypto.getRandomBytes(33),
privKey: libsignal.crypto.getRandomBytes(32),
};
var testKey = {
pubKey: libsignal.crypto.getRandomBytes(33),
privKey: libsignal.crypto.getRandomBytes(32),
};
2016-05-04 07:30:42 +00:00
describe('getLocalRegistrationId', function() {
it('retrieves my registration id', function(done) {
store.getLocalRegistrationId().then(function(reg) {
assert.strictEqual(reg, 1337);
}).then(done, done);
2015-07-22 19:48:08 +00:00
});
});
2016-05-04 07:30:42 +00:00
describe('getIdentityKeyPair', function() {
it('retrieves my identity key', function(done) {
store.getIdentityKeyPair().then(function(key) {
assertEqualArrayBuffers(key.pubKey, identityKey.pubKey);
assertEqualArrayBuffers(key.privKey, identityKey.privKey);
}).then(done,done);
2016-05-04 07:09:44 +00:00
});
});
describe('saveIdentity', function() {
2016-05-04 07:30:42 +00:00
it('stores identity keys', function(done) {
store.saveIdentity(identifier, testKey.pubKey).then(function() {
2016-05-04 07:30:42 +00:00
return store.loadIdentityKey(identifier).then(function(key) {
assertEqualArrayBuffers(key, testKey.pubKey);
});
}).then(done,done);
});
it('rejects on key change', function(done) {
var newIdentity = libsignal.crypto.getRandomBytes(33);
store.saveIdentity(identifier, testKey.pubKey).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
2016-05-04 07:09:44 +00:00
done(new Error('Allowed to overwrite identity key'));
2016-05-04 07:30:42 +00:00
}).catch(function(e) {
assert(e instanceof Error);
2016-05-04 07:09:44 +00:00
done();
2016-05-04 07:30:42 +00:00
});
});
2016-05-04 07:09:44 +00:00
});
});
2016-05-04 07:30:42 +00:00
describe('isTrustedIdentity', function() {
it('returns true if a key is trusted', function(done) {
store.saveIdentity(identifier, testKey.pubKey).then(function() {
2016-05-04 07:30:42 +00:00
store.isTrustedIdentity(identifier, testKey.pubKey).then(function(trusted) {
if (trusted) {
done();
} else {
done(new Error('Allowed to overwrite identity key'));
}
}).catch(done);
});
});
2016-05-04 07:30:42 +00:00
it('returns false if a key is untrusted', function(done) {
var newIdentity = libsignal.crypto.getRandomBytes(33);
store.saveIdentity(identifier, testKey.pubKey).then(function() {
2016-05-04 07:30:42 +00:00
store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
if (trusted) {
done(new Error('Allowed to overwrite identity key'));
} else {
done();
}
}).catch(done);
});
2016-05-04 07:30:42 +00:00
});
});
2016-05-04 07:30:42 +00:00
describe('storePreKey', function() {
it('stores prekeys', function(done) {
store.storePreKey(1, testKey).then(function() {
return store.loadPreKey(1).then(function(key) {
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
assertEqualArrayBuffers(key.privKey, testKey.privKey);
});
}).then(done,done);
});
});
describe('removePreKey', function() {
2016-05-04 07:31:32 +00:00
before(function(done) {
store.storePreKey(2, testKey).then(done);
});
2016-05-04 07:30:42 +00:00
it('deletes prekeys', function(done) {
store.removePreKey(2, testKey).then(function() {
return store.loadPreKey(2).then(function(key) {
assert.isUndefined(key);
});
}).then(done,done);
});
});
2016-05-04 07:30:42 +00:00
describe('storeSignedPreKey', function() {
it('stores signed prekeys', function(done) {
store.storeSignedPreKey(3, testKey).then(function() {
return store.loadSignedPreKey(3).then(function(key) {
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
assertEqualArrayBuffers(key.privKey, testKey.privKey);
});
}).then(done,done);
});
});
2016-05-04 07:30:42 +00:00
describe('removeSignedPreKey', function() {
2016-05-04 07:31:32 +00:00
before(function(done) {
store.storeSignedPreKey(4, testKey).then(done);
});
2016-05-04 07:30:42 +00:00
it('deletes signed prekeys', function(done) {
store.removeSignedPreKey(4, testKey).then(function() {
return store.loadSignedPreKey(4).then(function(key) {
assert.isUndefined(key);
});
}).then(done,done);
});
});
2016-05-04 07:30:42 +00:00
describe('storeSession', function() {
it('stores sessions', function(done) {
var testRecord = "an opaque string";
store.storeSession(identifier + '.1', testRecord).then(function() {
return store.loadSession(identifier + '.1').then(function(record) {
assert.deepEqual(record, testRecord);
});
}).then(done,done);
});
2016-05-04 07:30:42 +00:00
});
describe('removeAllSessions', function() {
it('removes all sessions for a number', function(done) {
var testRecord = "an opaque string";
var devices = [1, 2, 3].map(function(deviceId) {
return [identifier, deviceId].join('.');
});
2016-05-04 07:30:42 +00:00
var promise = Promise.resolve();
devices.forEach(function(encodedNumber) {
promise = promise.then(function() {
return store.storeSession(encodedNumber, testRecord + encodedNumber);
});
});
2016-05-04 07:30:42 +00:00
promise.then(function() {
return store.removeAllSessions(identifier).then(function(record) {
return Promise.all(devices.map(store.loadSession.bind(store))).then(function(records) {
for (var i in records) {
assert.isUndefined(records[i]);
};
});
});
}).then(done,done);
});
});
2016-05-04 07:30:42 +00:00
describe('clearSessionStore', function() {
it ('clears the session store', function(done) {
var testRecord = "an opaque string";
store.storeSession(identifier + '.1', testRecord).then(function() {
return store.clearSessionStore().then(function() {
return store.loadSession(identifier + '.1').then(function(record) {
assert.isUndefined(record);
});
});
2016-05-04 07:30:42 +00:00
}).then(done,done);
});
2016-05-04 07:30:42 +00:00
});
describe('getDeviceIds', function() {
it('returns deviceIds for a number', function(done) {
var testRecord = "an opaque string";
var devices = [1, 2, 3].map(function(deviceId) {
return [identifier, deviceId].join('.');
});
2016-05-04 07:30:42 +00:00
var promise = Promise.resolve();
devices.forEach(function(encodedNumber) {
promise = promise.then(function() {
return store.storeSession(encodedNumber, testRecord + encodedNumber);
});
});
2016-05-04 07:30:42 +00:00
promise.then(function() {
return store.getDeviceIds(identifier).then(function(deviceIds) {
assert.sameMembers(deviceIds, [1, 2, 3]);
});
}).then(done,done);
});
it('returns empty array for a number with no device ids', function(done) {
return store.getDeviceIds('foo').then(function(deviceIds) {
assert.sameMembers(deviceIds,[]);
}).then(done,done);
});
});
});