Normalize whitespace in storage tests

// FREEBIE
This commit is contained in:
lilia 2017-06-14 10:40:21 -07:00 committed by Scott Nonnenberg
parent 0e0f14723e
commit 27692b4dfc

View file

@ -1,357 +1,353 @@
/*
* vim: ts=4:sw=4:expandtab
*/
'use strict'; 'use strict';
describe("SignalProtocolStore", function() { describe("SignalProtocolStore", function() {
before(function(done) { before(function(done) {
storage.put('registrationId', 1337); storage.put('registrationId', 1337);
storage.put('identityKey', identityKey); storage.put('identityKey', identityKey);
storage.fetch().then(done, done); storage.fetch().then(done, done);
});
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),
};
describe('getLocalRegistrationId', function() {
it('retrieves my registration id', function(done) {
store.getLocalRegistrationId().then(function(reg) {
assert.strictEqual(reg, 1337);
}).then(done, done);
}); });
var store = textsecure.storage.protocol; });
var identifier = '+5558675309'; describe('getIdentityKeyPair', function() {
var identityKey = { it('retrieves my identity key', function(done) {
pubKey: libsignal.crypto.getRandomBytes(33), store.getIdentityKeyPair().then(function(key) {
privKey: libsignal.crypto.getRandomBytes(32), assertEqualArrayBuffers(key.pubKey, identityKey.pubKey);
}; assertEqualArrayBuffers(key.privKey, identityKey.privKey);
var testKey = { }).then(done,done);
pubKey: libsignal.crypto.getRandomBytes(33),
privKey: libsignal.crypto.getRandomBytes(32),
};
describe('getLocalRegistrationId', function() {
it('retrieves my registration id', function(done) {
store.getLocalRegistrationId().then(function(reg) {
assert.strictEqual(reg, 1337);
}).then(done, done);
});
}); });
describe('getIdentityKeyPair', function() { });
it('retrieves my identity key', function(done) { describe('saveIdentity', function() {
store.getIdentityKeyPair().then(function(key) { var IdentityKeyRecord = Backbone.Model.extend({
assertEqualArrayBuffers(key.pubKey, identityKey.pubKey); database: Whisper.Database,
assertEqualArrayBuffers(key.privKey, identityKey.privKey);
}).then(done,done);
});
});
describe('saveIdentity', function() {
var IdentityKeyRecord = Backbone.Model.extend({
database: Whisper.Database,
storeName: 'identityKeys' storeName: 'identityKeys'
}); });
var record = new IdentityKeyRecord({id: identifier}); var record = new IdentityKeyRecord({id: identifier});
it('stores identity keys', function(done) { it('stores identity keys', function(done) {
store.saveIdentity(identifier, testKey.pubKey).then(function() {
return store.loadIdentityKey(identifier).then(function(key) {
assertEqualArrayBuffers(key, testKey.pubKey);
});
}).then(done,done);
});
it('allows key changes', function(done) {
var newIdentity = libsignal.crypto.getRandomBytes(33);
store.saveIdentity(identifier, testKey.pubKey).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
done();
});
}).catch(done);
});
describe('When there is no existing key (first use)', function() {
before(function(done) {
store.removeIdentityKey(identifier).then(function() {
store.saveIdentity(identifier, testKey.pubKey).then(function() { store.saveIdentity(identifier, testKey.pubKey).then(function() {
return store.loadIdentityKey(identifier).then(function(key) { record.fetch().then(function() { done(); });
assertEqualArrayBuffers(key, testKey.pubKey); });
}); });
}).then(done,done);
}); });
it('allows key changes', function(done) { it('marks the key firstUse', function() {
var newIdentity = libsignal.crypto.getRandomBytes(33); assert(record.get('firstUse'));
store.saveIdentity(identifier, testKey.pubKey).then(function() { });
store.saveIdentity(identifier, newIdentity).then(function() { it('sets the timestamp', function() {
done(); assert(record.get('timestamp'));
});
});
describe('When there is a different existing key (non first use)', function() {
var newIdentity = libsignal.crypto.getRandomBytes(33);
var oldTimestamp = Date.now();
before(function(done) {
record.save({
publicKey : testKey.pubKey,
firstUse : true,
timestamp : oldTimestamp,
nonblockingApproval : false,
}).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
record.fetch().then(function() { done(); });
});
});
});
it('marks the key not firstUse', function() {
assert(!record.get('firstUse'));
});
it('updates the timestamp', function() {
assert.notEqual(record.get('timestamp'), oldTimestamp);
});
});
describe('When the key has not changed', function() {
var oldTimestamp = Date.now();
before(function(done) {
record.save({
publicKey : testKey.pubKey,
timestamp : oldTimestamp,
nonblockingApproval : false,
}).then(function() { done(); });
});
describe('If it is marked firstUse', function() {
before(function(done) {
record.save({ firstUse: true }).then(function() { done(); });
});
it('nothing changes', function(done) {
store.saveIdentity(identifier, testKey.pubKey, true).then(function() {
record.fetch().then(function() {
assert(!record.get('nonblockingApproval'));
assert.strictEqual(record.get('timestamp'), oldTimestamp);
done();
});
});
});
});
describe('If it is not marked firstUse', function() {
before(function(done) {
record.save({ firstUse: false }).then(function() { done(); });
});
describe('If nonblocking approval is required', function() {
it('updates non-blocking approval', function(done) {
store.saveIdentity(identifier, testKey.pubKey, true).then(function() {
record.fetch().then(function() {
assert(record.get('nonblockingApproval'));
assert.strictEqual(record.get('timestamp'), oldTimestamp);
assert.strictEqual(record.get('firstUse'), false);
done();
}); });
});
});
});
});
});
});
describe('isTrustedIdentity', function() {
describe('When invalid direction is given', function(done) {
it('should fail', function(done) {
store.isTrustedIdentity(identifier, testKey.pubKey).then(function() {
done(new Error('isTrustedIdentity should have failed'));
}).catch(function(e) {
done();
});
});
});
describe('When direction is RECEIVING', function() {
it('always returns true', function(done) {
var newIdentity = libsignal.crypto.getRandomBytes(33);
store.saveIdentity(identifier, testKey.pubKey).then(function() {
store.isTrustedIdentity(identifier, newIdentity, store.Direction.RECEIVING).then(function(trusted) {
if (trusted) {
done();
} else {
done(new Error('isTrusted returned false when receiving'));
}
}).catch(done); }).catch(done);
});
}); });
});
describe('When direction is SENDING', function() {
describe('When there is no existing key (first use)', function() { describe('When there is no existing key (first use)', function() {
before(function(done) { before(function(done) {
store.removeIdentityKey(identifier).then(function() { store.removeIdentityKey(identifier).then(function() {
store.saveIdentity(identifier, testKey.pubKey).then(function() {
record.fetch().then(function() { done(); });
});
});
});
it('marks the key firstUse', function() {
assert(record.get('firstUse'));
});
it('sets the timestamp', function() {
assert(record.get('timestamp'));
});
});
describe('When there is a different existing key (non first use)', function() {
var newIdentity = libsignal.crypto.getRandomBytes(33);
var oldTimestamp = Date.now();
before(function(done) {
record.save({
publicKey : testKey.pubKey,
firstUse : true,
timestamp : oldTimestamp,
nonblockingApproval : false,
}).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
record.fetch().then(function() { done(); });
});
});
});
it('marks the key not firstUse', function() {
assert(!record.get('firstUse'));
});
it('updates the timestamp', function() {
assert.notEqual(record.get('timestamp'), oldTimestamp);
});
});
describe('When the key has not changed', function() {
var oldTimestamp = Date.now();
before(function(done) {
record.save({
publicKey : testKey.pubKey,
timestamp : oldTimestamp,
nonblockingApproval : false,
}).then(function() { done(); });
});
describe('If it is marked firstUse', function() {
before(function(done) {
record.save({ firstUse: true }).then(function() { done(); });
});
it('nothing changes', function(done) {
store.saveIdentity(identifier, testKey.pubKey, true).then(function() {
record.fetch().then(function() {
assert(!record.get('nonblockingApproval'));
assert.strictEqual(record.get('timestamp'), oldTimestamp);
done();
});
});
});
});
describe('If it is not marked firstUse', function() {
before(function(done) {
record.save({ firstUse: false }).then(function() { done(); });
});
describe('If nonblocking approval is required', function() {
it('updates non-blocking approval', function(done) {
store.saveIdentity(identifier, testKey.pubKey, true).then(function() {
record.fetch().then(function() {
assert(record.get('nonblockingApproval'));
assert.strictEqual(record.get('timestamp'), oldTimestamp);
assert.strictEqual(record.get('firstUse'), false);
done();
});
});
});
});
});
});
});
describe('isTrustedIdentity', function() {
describe('When invalid direction is given', function(done) {
it('should fail', function(done) {
store.isTrustedIdentity(identifier, testKey.pubKey).then(function() {
done(new Error('isTrustedIdentity should have failed'));
}).catch(function(e) {
done(); done();
}); });
}); });
}); it('returns true', function(done) {
describe('When direction is RECEIVING', function() { var newIdentity = libsignal.crypto.getRandomBytes(33);
it('always returns true', function(done) { store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) {
var newIdentity = libsignal.crypto.getRandomBytes(33); if (trusted) {
store.saveIdentity(identifier, testKey.pubKey).then(function() { done();
store.isTrustedIdentity(identifier, newIdentity, store.Direction.RECEIVING).then(function(trusted) { } else {
if (trusted) { done(new Error('isTrusted returned false on first use'));
done(); }
} else { }).catch(done);
done(new Error('isTrusted returned false when receiving'));
}
}).catch(done);
});
}); });
}); });
describe('When direction is SENDING', function() { describe('When there is an existing key', function() {
describe('When there is no existing key (first use)', function() { before(function(done) {
before(function(done) { store.saveIdentity(identifier, testKey.pubKey).then(function() {
store.removeIdentityKey(identifier).then(function() { done();
done();
});
}); });
it('returns true', function(done) { });
describe('When the existing key is different', function() {
it('returns false', function(done) {
var newIdentity = libsignal.crypto.getRandomBytes(33); var newIdentity = libsignal.crypto.getRandomBytes(33);
store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) { store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) {
if (trusted) { if (trusted) {
done(); done(new Error('isTrusted returned true on untrusted key'));
} else { } else {
done(new Error('isTrusted returned false on first use')); done();
} }
}).catch(done); }).catch(done);
}); });
}); });
describe('When there is an existing key', function() { describe('When the existing key matches the new key', function() {
var newIdentity = libsignal.crypto.getRandomBytes(33);
before(function(done) { before(function(done) {
store.saveIdentity(identifier, testKey.pubKey).then(function() { store.saveIdentity(identifier, newIdentity).then(function() {
done(); done();
}); });
}); });
describe('When the existing key is different', function() { it('returns false if blocking approval is required', function(done) {
it('returns false', function(done) { storage.put('safety-numbers-approval', true);
var newIdentity = libsignal.crypto.getRandomBytes(33); store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) {
store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) { if (trusted) {
if (trusted) { done(new Error('isTrusted returned true on untrusted key'));
done(new Error('isTrusted returned true on untrusted key')); } else {
} else {
done();
}
}).catch(done);
});
});
describe('When the existing key matches the new key', function() {
var newIdentity = libsignal.crypto.getRandomBytes(33);
before(function(done) {
store.saveIdentity(identifier, newIdentity).then(function() {
done(); done();
}); }
}); }).catch(done);
it('returns false if blocking approval is required', function(done) { });
storage.put('safety-numbers-approval', true); it('returns false if keys match but nonblocking approval is required', function(done) {
storage.put('safety-numbers-approval', false);
store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) {
if (trusted) {
done(new Error('isTrusted returned true on untrusted key'));
} else {
done();
}
}).catch(done);
});
it('returns true if neither blocking nor nonblocking approval is required', function(done) {
storage.put('safety-numbers-approval', false);
store.saveIdentity(identifier, newIdentity, true).then(function() {
store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) { store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) {
if (trusted) { if (trusted) {
done(new Error('isTrusted returned true on untrusted key')); done();
} else { } else {
done(); done(new Error('isTrusted returned false on an approved key'));
} }
}).catch(done); }).catch(done);
}); });
it('returns false if keys match but nonblocking approval is required', function(done) {
storage.put('safety-numbers-approval', false);
store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) {
if (trusted) {
done(new Error('isTrusted returned true on untrusted key'));
} else {
done();
}
}).catch(done);
});
it('returns true if neither blocking nor nonblocking approval is required', function(done) {
storage.put('safety-numbers-approval', false);
store.saveIdentity(identifier, newIdentity, true).then(function() {
store.isTrustedIdentity(identifier, newIdentity, store.Direction.SENDING).then(function(trusted) {
if (trusted) {
done();
} else {
done(new Error('isTrusted returned false on an approved key'));
}
}).catch(done);
});
});
});
}); });
}); });
});
}); });
describe('storePreKey', function() { });
it('stores prekeys', function(done) { describe('storePreKey', function() {
store.storePreKey(1, testKey).then(function() { it('stores prekeys', function(done) {
return store.loadPreKey(1).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);
}); });
}).then(done,done);
}); });
describe('removePreKey', function() { });
before(function(done) { describe('removePreKey', function() {
store.storePreKey(2, testKey).then(done); before(function(done) {
}); store.storePreKey(2, testKey).then(done);
it('deletes prekeys', function(done) {
store.removePreKey(2, testKey).then(function() {
return store.loadPreKey(2).then(function(key) {
assert.isUndefined(key);
});
}).then(done,done);
});
}); });
describe('storeSignedPreKey', function() { it('deletes prekeys', function(done) {
it('stores signed prekeys', function(done) { store.removePreKey(2, testKey).then(function() {
store.storeSignedPreKey(3, testKey).then(function() { return store.loadPreKey(2).then(function(key) {
return store.loadSignedPreKey(3).then(function(key) { assert.isUndefined(key);
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
assertEqualArrayBuffers(key.privKey, testKey.privKey);
});
}).then(done,done);
}); });
}).then(done,done);
}); });
describe('removeSignedPreKey', function() { });
before(function(done) { describe('storeSignedPreKey', function() {
store.storeSignedPreKey(4, testKey).then(done); it('stores signed prekeys', function(done) {
}); store.storeSignedPreKey(3, testKey).then(function() {
it('deletes signed prekeys', function(done) { return store.loadSignedPreKey(3).then(function(key) {
store.removeSignedPreKey(4, testKey).then(function() { assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
return store.loadSignedPreKey(4).then(function(key) { assertEqualArrayBuffers(key.privKey, testKey.privKey);
assert.isUndefined(key);
});
}).then(done,done);
}); });
}).then(done,done);
}); });
describe('storeSession', function() { });
it('stores sessions', function(done) { describe('removeSignedPreKey', function() {
var testRecord = "an opaque string"; before(function(done) {
store.storeSession(identifier + '.1', testRecord).then(function() { store.storeSignedPreKey(4, testKey).then(done);
return store.loadSession(identifier + '.1').then(function(record) {
assert.deepEqual(record, testRecord);
});
}).then(done,done);
});
}); });
describe('removeAllSessions', function() { it('deletes signed prekeys', function(done) {
it('removes all sessions for a number', function(done) { store.removeSignedPreKey(4, testKey).then(function() {
var testRecord = "an opaque string"; return store.loadSignedPreKey(4).then(function(key) {
var devices = [1, 2, 3].map(function(deviceId) { assert.isUndefined(key);
return [identifier, deviceId].join('.');
});
var promise = Promise.resolve();
devices.forEach(function(encodedNumber) {
promise = promise.then(function() {
return store.storeSession(encodedNumber, testRecord + encodedNumber);
});
});
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);
}); });
}).then(done,done);
}); });
describe('clearSessionStore', function() { });
it ('clears the session store', function(done) { describe('storeSession', function() {
var testRecord = "an opaque string"; it('stores sessions', 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.loadSession(identifier + '.1').then(function(record) {
assert.isUndefined(record); assert.deepEqual(record, testRecord);
}); });
}); }).then(done,done);
}).then(done,done); });
});
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('.');
});
var promise = Promise.resolve();
devices.forEach(function(encodedNumber) {
promise = promise.then(function() {
return store.storeSession(encodedNumber, testRecord + encodedNumber);
});
});
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);
});
});
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);
});
});
}).then(done,done);
});
}); });
describe('getDeviceIds', function() { });
it('returns deviceIds for a number', function(done) { describe('getDeviceIds', function() {
var testRecord = "an opaque string"; it('returns deviceIds for a number', function(done) {
var devices = [1, 2, 3].map(function(deviceId) { var testRecord = "an opaque string";
return [identifier, deviceId].join('.'); var devices = [1, 2, 3].map(function(deviceId) {
}); return [identifier, deviceId].join('.');
var promise = Promise.resolve(); });
devices.forEach(function(encodedNumber) { var promise = Promise.resolve();
promise = promise.then(function() { devices.forEach(function(encodedNumber) {
return store.storeSession(encodedNumber, testRecord + encodedNumber); promise = promise.then(function() {
}); return store.storeSession(encodedNumber, testRecord + encodedNumber);
});
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) { promise.then(function() {
assert.sameMembers(deviceIds,[]); return store.getDeviceIds(identifier).then(function(deviceIds) {
}).then(done,done); 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);
});
});
}); });