signal-desktop/libtextsecure/test/storage_test.js

189 lines
5.5 KiB
JavaScript
Raw Normal View History

2018-07-21 21:51:20 +00:00
describe('SignalProtocolStore', () => {
before(() => {
2018-05-02 16:51:22 +00:00
localStorage.clear();
});
2018-07-21 21:51:20 +00:00
const store = textsecure.storage.protocol;
const identifier = '+5558675309';
const another_identifier = '+5555590210';
const identityKey = {
2018-05-02 16:51:22 +00:00
pubKey: libsignal.crypto.getRandomBytes(33),
privKey: libsignal.crypto.getRandomBytes(32),
};
2018-07-21 21:51:20 +00:00
const testKey = {
2018-05-02 16:51:22 +00:00
pubKey: libsignal.crypto.getRandomBytes(33),
privKey: libsignal.crypto.getRandomBytes(32),
};
2018-07-21 21:51:20 +00:00
it('retrieves my registration id', done => {
2018-05-02 16:51:22 +00:00
store.put('registrationId', 1337);
store
.getLocalRegistrationId()
2018-07-21 21:51:20 +00:00
.then(reg => {
2018-05-02 16:51:22 +00:00
assert.strictEqual(reg, 1337);
})
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('retrieves my identity key', done => {
2018-05-02 16:51:22 +00:00
store.put('identityKey', identityKey);
store
.getIdentityKeyPair()
2018-07-21 21:51:20 +00:00
.then(key => {
2018-05-02 16:51:22 +00:00
assertEqualArrayBuffers(key.pubKey, identityKey.pubKey);
assertEqualArrayBuffers(key.privKey, identityKey.privKey);
})
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('stores identity keys', done => {
2018-05-02 16:51:22 +00:00
store
.saveIdentity(identifier, testKey.pubKey)
2018-07-21 21:51:20 +00:00
.then(() =>
store.loadIdentityKey(identifier).then(key => {
2018-05-02 16:51:22 +00:00
assertEqualArrayBuffers(key, testKey.pubKey);
2018-07-21 21:51:20 +00:00
})
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('returns whether a key is trusted', done => {
const newIdentity = libsignal.crypto.getRandomBytes(33);
store.saveIdentity(identifier, testKey.pubKey).then(() => {
2018-05-02 16:51:22 +00:00
store
.isTrustedIdentity(identifier, newIdentity)
2018-07-21 21:51:20 +00:00
.then(trusted => {
2018-05-02 16:51:22 +00:00
if (trusted) {
done(new Error('Allowed to overwrite identity key'));
} else {
done();
}
})
.catch(done);
});
2018-05-02 16:51:22 +00:00
});
2018-07-21 21:51:20 +00:00
it('returns whether a key is untrusted', done => {
const newIdentity = libsignal.crypto.getRandomBytes(33);
store.saveIdentity(identifier, testKey.pubKey).then(() => {
2018-05-02 16:51:22 +00:00
store
.isTrustedIdentity(identifier, testKey.pubKey)
2018-07-21 21:51:20 +00:00
.then(trusted => {
2018-05-02 16:51:22 +00:00
if (trusted) {
done();
} else {
done(new Error('Allowed to overwrite identity key'));
}
})
.catch(done);
});
2018-05-02 16:51:22 +00:00
});
2018-07-21 21:51:20 +00:00
it('stores prekeys', done => {
2018-05-02 16:51:22 +00:00
store
.storePreKey(1, testKey)
2018-07-21 21:51:20 +00:00
.then(() =>
store.loadPreKey(1).then(key => {
2018-05-02 16:51:22 +00:00
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
assertEqualArrayBuffers(key.privKey, testKey.privKey);
2018-07-21 21:51:20 +00:00
})
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('deletes prekeys', done => {
before(done => {
2018-05-02 16:51:22 +00:00
store.storePreKey(2, testKey).then(done);
2016-05-04 07:09:44 +00:00
});
2018-05-02 16:51:22 +00:00
store
.removePreKey(2, testKey)
2018-07-21 21:51:20 +00:00
.then(() =>
store.loadPreKey(2).then(key => {
2018-05-02 16:51:22 +00:00
assert.isUndefined(key);
2018-07-21 21:51:20 +00:00
})
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('stores signed prekeys', done => {
2018-05-02 16:51:22 +00:00
store
.storeSignedPreKey(3, testKey)
2018-07-21 21:51:20 +00:00
.then(() =>
store.loadSignedPreKey(3).then(key => {
2018-05-02 16:51:22 +00:00
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
assertEqualArrayBuffers(key.privKey, testKey.privKey);
2018-07-21 21:51:20 +00:00
})
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('deletes signed prekeys', done => {
before(done => {
2018-05-02 16:51:22 +00:00
store.storeSignedPreKey(4, testKey).then(done);
});
2018-05-02 16:51:22 +00:00
store
.removeSignedPreKey(4, testKey)
2018-07-21 21:51:20 +00:00
.then(() =>
store.loadSignedPreKey(4).then(key => {
2018-05-02 16:51:22 +00:00
assert.isUndefined(key);
2018-07-21 21:51:20 +00:00
})
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('stores sessions', done => {
const testRecord = 'an opaque string';
const devices = [1, 2, 3].map(deviceId => [identifier, deviceId].join('.'));
let promise = Promise.resolve();
devices.forEach(encodedNumber => {
promise = promise.then(() =>
store.storeSession(encodedNumber, testRecord + encodedNumber)
);
2015-04-21 20:30:22 +00:00
});
2018-05-02 16:51:22 +00:00
promise
2018-07-21 21:51:20 +00:00
.then(() =>
Promise.all(devices.map(store.loadSession.bind(store))).then(
records => {
for (const i in records) {
2018-05-02 16:51:22 +00:00
assert.strictEqual(records[i], testRecord + devices[i]);
}
}
2018-07-21 21:51:20 +00:00
)
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('removes all sessions for a number', done => {
const testRecord = 'an opaque string';
const devices = [1, 2, 3].map(deviceId => [identifier, deviceId].join('.'));
let promise = Promise.resolve();
devices.forEach(encodedNumber => {
promise = promise.then(() =>
store.storeSession(encodedNumber, testRecord + encodedNumber)
);
2018-05-02 16:51:22 +00:00
});
promise
2018-07-21 21:51:20 +00:00
.then(() =>
store.removeAllSessions(identifier).then(record =>
Promise.all(devices.map(store.loadSession.bind(store))).then(
records => {
for (const i in records) {
2018-05-02 16:51:22 +00:00
assert.isUndefined(records[i]);
}
}
2018-07-21 21:51:20 +00:00
)
)
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('returns deviceIds for a number', done => {
const testRecord = 'an opaque string';
const devices = [1, 2, 3].map(deviceId => [identifier, deviceId].join('.'));
let promise = Promise.resolve();
devices.forEach(encodedNumber => {
promise = promise.then(() =>
store.storeSession(encodedNumber, testRecord + encodedNumber)
);
});
2018-05-02 16:51:22 +00:00
promise
2018-07-21 21:51:20 +00:00
.then(() =>
store.getDeviceIds(identifier).then(deviceIds => {
2018-05-02 16:51:22 +00:00
assert.sameMembers(deviceIds, [1, 2, 3]);
2018-07-21 21:51:20 +00:00
})
)
2018-05-02 16:51:22 +00:00
.then(done, done);
});
2018-07-21 21:51:20 +00:00
it('returns empty array for a number with no device ids', () =>
store.getDeviceIds('foo').then(deviceIds => {
assert.sameMembers(deviceIds, []);
2018-07-21 21:51:20 +00:00
}));
});