More descriptive tests

// FREEBIE
pull/749/head
lilia 9 years ago
parent f173104c82
commit 348ee0b3e7

@ -20,154 +20,178 @@ describe("SignalProtocolStore", function() {
pubKey: textsecure.crypto.getRandomBytes(33), pubKey: textsecure.crypto.getRandomBytes(33),
privKey: textsecure.crypto.getRandomBytes(32), privKey: textsecure.crypto.getRandomBytes(32),
}; };
it('retrieves my registration id', function(done) { describe('getLocalRegistrationId', function() {
store.getLocalRegistrationId().then(function(reg) { it('retrieves my registration id', function(done) {
assert.strictEqual(reg, 1337); store.getLocalRegistrationId().then(function(reg) {
}).then(done, done); assert.strictEqual(reg, 1337);
}); }).then(done, done);
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);
});
it('stores identity keys', function(done) {
store.putIdentityKey(identifier, testKey.pubKey).then(function() {
return store.loadIdentityKey(identifier).then(function(key) {
assertEqualArrayBuffers(key, testKey.pubKey);
});
}).then(done,done);
});
it('rejects on key change', function(done) {
var newIdentity = textsecure.crypto.getRandomBytes(33);
store.putIdentityKey(identifier, testKey.pubKey).then(function() {
store.putIdentityKey(identifier, newIdentity).then(function() {
done(new Error('Allowed to overwrite identity key'));
}).catch(function(e) {
assert(e instanceof Error);
done();
});
}); });
}); });
it('returns true if a key is trusted', function(done) { describe('getIdentityKeyPair', function() {
store.putIdentityKey(identifier, testKey.pubKey).then(function() { it('retrieves my identity key', function(done) {
store.isTrustedIdentity(identifier, testKey.pubKey).then(function(trusted) { store.getIdentityKeyPair().then(function(key) {
if (trusted) { assertEqualArrayBuffers(key.pubKey, identityKey.pubKey);
done(); assertEqualArrayBuffers(key.privKey, identityKey.privKey);
} else { }).then(done,done);
done(new Error('Allowed to overwrite identity key'));
}
}).catch(done);
}); });
}); });
it('returns false if a key is untrusted', function(done) { describe('putIdentityKey', function() {
var newIdentity = textsecure.crypto.getRandomBytes(33); it('stores identity keys', function(done) {
store.putIdentityKey(identifier, testKey.pubKey).then(function() { store.putIdentityKey(identifier, testKey.pubKey).then(function() {
store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) { return store.loadIdentityKey(identifier).then(function(key) {
if (trusted) { assertEqualArrayBuffers(key, testKey.pubKey);
});
}).then(done,done);
});
it('rejects on key change', function(done) {
var newIdentity = textsecure.crypto.getRandomBytes(33);
store.putIdentityKey(identifier, testKey.pubKey).then(function() {
store.putIdentityKey(identifier, newIdentity).then(function() {
done(new Error('Allowed to overwrite identity key')); done(new Error('Allowed to overwrite identity key'));
} else { }).catch(function(e) {
assert(e instanceof Error);
done(); done();
} });
}).catch(done); });
}); });
}); });
it('stores prekeys', function(done) { describe('isTrustedIdentity', function() {
store.storePreKey(1, testKey).then(function() { it('returns true if a key is trusted', function(done) {
return store.loadPreKey(1).then(function(key) { store.putIdentityKey(identifier, testKey.pubKey).then(function() {
assertEqualArrayBuffers(key.pubKey, testKey.pubKey); store.isTrustedIdentity(identifier, testKey.pubKey).then(function(trusted) {
assertEqualArrayBuffers(key.privKey, testKey.privKey); if (trusted) {
done();
} else {
done(new Error('Allowed to overwrite identity key'));
}
}).catch(done);
}); });
}).then(done,done);
});
it('deletes prekeys', function(done) {
before(function(done) {
store.storePreKey(2, testKey).then(done);
}); });
store.removePreKey(2, testKey).then(function() { it('returns false if a key is untrusted', function(done) {
return store.loadPreKey(2).then(function(key) { var newIdentity = textsecure.crypto.getRandomBytes(33);
assert.isUndefined(key); store.putIdentityKey(identifier, testKey.pubKey).then(function() {
store.isTrustedIdentity(identifier, newIdentity).then(function(trusted) {
if (trusted) {
done(new Error('Allowed to overwrite identity key'));
} else {
done();
}
}).catch(done);
}); });
}).then(done,done); });
}); });
it('stores signed prekeys', function(done) { describe('storePreKey', function() {
store.storeSignedPreKey(3, testKey).then(function() { it('stores prekeys', function(done) {
return store.loadSignedPreKey(3).then(function(key) { store.storePreKey(1, testKey).then(function() {
assertEqualArrayBuffers(key.pubKey, testKey.pubKey); return store.loadPreKey(1).then(function(key) {
assertEqualArrayBuffers(key.privKey, testKey.privKey); assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
assertEqualArrayBuffers(key.privKey, testKey.privKey);
});
}).then(done,done);
});
});
describe('removePreKey', function() {
it('deletes prekeys', function(done) {
before(function(done) {
store.storePreKey(2, testKey).then(done);
}); });
}).then(done,done); store.removePreKey(2, testKey).then(function() {
return store.loadPreKey(2).then(function(key) {
assert.isUndefined(key);
});
}).then(done,done);
});
}); });
it('deletes signed prekeys', function(done) { describe('storeSignedPreKey', function() {
before(function(done) { it('stores signed prekeys', function(done) {
store.storeSignedPreKey(4, testKey).then(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);
}); });
store.removeSignedPreKey(4, testKey).then(function() {
return store.loadSignedPreKey(4).then(function(key) {
assert.isUndefined(key);
});
}).then(done,done);
}); });
it('stores sessions', function(done) { describe('removeSignedPreKey', function() {
var testRecord = "an opaque string"; it('deletes signed prekeys', function(done) {
store.storeSession(identifier + '.1', testRecord).then(function() { before(function(done) {
return store.loadSession(identifier + '.1').then(function(record) { store.storeSignedPreKey(4, testKey).then(done);
assert.deepEqual(record, testRecord);
}); });
}).then(done,done); store.removeSignedPreKey(4, testKey).then(function() {
return store.loadSignedPreKey(4).then(function(key) {
assert.isUndefined(key);
});
}).then(done,done);
});
}); });
it('removes all sessions for a number', function(done) { describe('storeSession', function() {
var testRecord = "an opaque string"; it('stores sessions', function(done) {
var devices = [1, 2, 3].map(function(deviceId) { var testRecord = "an opaque string";
return [identifier, deviceId].join('.'); store.storeSession(identifier + '.1', testRecord).then(function() {
return store.loadSession(identifier + '.1').then(function(record) {
assert.deepEqual(record, testRecord);
});
}).then(done,done);
}); });
var promise = Promise.resolve(); });
devices.forEach(function(encodedNumber) { describe('removeAllSessions', function() {
promise = promise.then(function() { it('removes all sessions for a number', function(done) {
return store.storeSession(encodedNumber, testRecord + encodedNumber); var testRecord = "an opaque string";
var devices = [1, 2, 3].map(function(deviceId) {
return [identifier, deviceId].join('.');
}); });
}); var promise = Promise.resolve();
promise.then(function() { devices.forEach(function(encodedNumber) {
return store.removeAllSessions(identifier).then(function(record) { promise = promise.then(function() {
return Promise.all(devices.map(store.loadSession.bind(store))).then(function(records) { return store.storeSession(encodedNumber, testRecord + encodedNumber);
for (var i in records) {
assert.isUndefined(records[i]);
};
}); });
}); });
}).then(done,done); 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);
});
}); });
it ('clears the session store', function(done) { describe('clearSessionStore', function() {
var testRecord = "an opaque string"; it ('clears the session store', function(done) {
store.storeSession(identifier + '.1', testRecord).then(function() { var testRecord = "an opaque string";
return store.clearSessionStore().then(function() { store.storeSession(identifier + '.1', testRecord).then(function() {
return store.loadSession(identifier + '.1').then(function(record) { return store.clearSessionStore().then(function() {
assert.isUndefined(record); return store.loadSession(identifier + '.1').then(function(record) {
assert.isUndefined(record);
});
}); });
}); }).then(done,done);
}).then(done,done);
});
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('.');
}); });
var promise = Promise.resolve(); });
devices.forEach(function(encodedNumber) { describe('getDeviceIds', function() {
promise = promise.then(function() { it('returns deviceIds for a number', function(done) {
return store.storeSession(encodedNumber, testRecord + encodedNumber); var testRecord = "an opaque string";
var devices = [1, 2, 3].map(function(deviceId) {
return [identifier, deviceId].join('.');
}); });
}); var promise = Promise.resolve();
promise.then(function() { devices.forEach(function(encodedNumber) {
return store.getDeviceIds(identifier).then(function(deviceIds) { promise = promise.then(function() {
assert.sameMembers(deviceIds, [1, 2, 3]); return store.storeSession(encodedNumber, testRecord + encodedNumber);
});
}); });
}).then(done,done); promise.then(function() {
}); return store.getDeviceIds(identifier).then(function(deviceIds) {
it('returns empty array for a number with no device ids', function(done) { assert.sameMembers(deviceIds, [1, 2, 3]);
return store.getDeviceIds('foo').then(function(deviceIds) { });
assert.sameMembers(deviceIds,[]); }).then(done,done);
}).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);
});
}); });
}); });

Loading…
Cancel
Save