Skip to content

Commit

Permalink
Update identity store tests
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
liliakai authored and scottnonnenberg committed Aug 4, 2017
1 parent 27692b4 commit 5108014
Showing 1 changed file with 179 additions and 6 deletions.
185 changes: 179 additions & 6 deletions test/storage_test.js
Expand Up @@ -31,11 +31,14 @@ describe("SignalProtocolStore", function() {
}).then(done,done);
});
});



var IdentityKeyRecord = Backbone.Model.extend({
database: Whisper.Database,
storeName: 'identityKeys'
});
describe('saveIdentity', function() {
var IdentityKeyRecord = Backbone.Model.extend({
database: Whisper.Database,
storeName: 'identityKeys'
});
var record = new IdentityKeyRecord({id: identifier});

it('stores identity keys', function(done) {
Expand Down Expand Up @@ -68,6 +71,9 @@ describe("SignalProtocolStore", function() {
it('sets the timestamp', function() {
assert(record.get('timestamp'));
});
it('sets the verified status to DEFAULT', function() {
assert.strictEqual(record.get('verified'), store.VerifiedStatus.DEFAULT);
});
});
describe('When there is a different existing key (non first use)', function() {
var newIdentity = libsignal.crypto.getRandomBytes(33);
Expand All @@ -78,6 +84,7 @@ describe("SignalProtocolStore", function() {
firstUse : true,
timestamp : oldTimestamp,
nonblockingApproval : false,
verified : store.VerifiedStatus.DEFAULT
}).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
record.fetch().then(function() { done(); });
Expand All @@ -90,6 +97,61 @@ describe("SignalProtocolStore", function() {
it('updates the timestamp', function() {
assert.notEqual(record.get('timestamp'), oldTimestamp);
});

describe('The previous verified status was DEFAULT', function() {
before(function(done) {
record.save({
publicKey : testKey.pubKey,
firstUse : true,
timestamp : oldTimestamp,
nonblockingApproval : false,
verified : store.VerifiedStatus.DEFAULT
}).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
record.fetch().then(function() { done(); });
});
});
});
it('sets the new key to unverified', function() {
assert.strictEqual(record.get('verified'), store.VerifiedStatus.DEFAULT);
});
});
describe('The previous verified status was VERIFIED', function() {
before(function(done) {
record.save({
publicKey : testKey.pubKey,
firstUse : true,
timestamp : oldTimestamp,
nonblockingApproval : false,
verified : store.VerifiedStatus.VERIFIED
}).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
record.fetch().then(function() { done(); });
});
});
});
it('sets the new key to unverified', function() {
assert.strictEqual(record.get('verified'), store.VerifiedStatus.UNVERIFIED);
});
});
describe('The previous verified status was UNVERIFIED', function() {
before(function(done) {
record.save({
publicKey : testKey.pubKey,
firstUse : true,
timestamp : oldTimestamp,
nonblockingApproval : false,
verified : store.VerifiedStatus.UNVERIFIED
}).then(function() {
store.saveIdentity(identifier, newIdentity).then(function() {
record.fetch().then(function() { done(); });
});
});
});
it('sets the new key to unverified', function() {
assert.strictEqual(record.get('verified'), store.VerifiedStatus.UNVERIFIED);
});
});
});
describe('When the key has not changed', function() {
var oldTimestamp = Date.now();
Expand All @@ -98,6 +160,7 @@ describe("SignalProtocolStore", function() {
publicKey : testKey.pubKey,
timestamp : oldTimestamp,
nonblockingApproval : false,
verified : store.VerifiedStatus.DEFAULT
}).then(function() { done(); });
});
describe('If it is marked firstUse', function() {
Expand All @@ -119,10 +182,10 @@ describe("SignalProtocolStore", function() {
record.save({ firstUse: false }).then(function() { done(); });
});
describe('If nonblocking approval is required', function() {
it('updates non-blocking approval', function(done) {
it('sets 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('nonblockingApproval'), true);
assert.strictEqual(record.get('timestamp'), oldTimestamp);
assert.strictEqual(record.get('firstUse'), false);
done();
Expand All @@ -133,6 +196,116 @@ describe("SignalProtocolStore", function() {
});
});
});
describe('saveIdentityWithAttributes', function() {
var now = Date.now();
var record = new IdentityKeyRecord({id: identifier});
var validAttributes = {
publicKey : testKey.pubKey,
firstUse : true,
timestamp : now,
verified : store.VerifiedStatus.VERIFIED,
nonblockingApproval : false
};
before(function(done) {
store.removeIdentityKey(identifier).then(function() { done(); });
});
describe('with valid attributes', function() {
before(function(done) {
store.saveIdentityWithAttributes(identifier, validAttributes).then(function() {
return new Promise(function(resolve) {
record.fetch().then(resolve);
});
}).then(done, done);
});

it('publicKey is saved', function() {
assertEqualArrayBuffers(record.get('publicKey'), testKey.pubKey);
});
it('firstUse is saved', function() {
assert.strictEqual(record.get('firstUse'), true);
});
it('timestamp is saved', function() {
assert.strictEqual(record.get('timestamp'), now);
});
it('verified is saved', function() {
assert.strictEqual(record.get('verified'), store.VerifiedStatus.VERIFIED);
});
it('nonblockingApproval is saved', function() {
assert.strictEqual(record.get('nonblockingApproval'), false);
});
});
describe('with invalid attributes', function() {
var attributes;
beforeEach(function() {
attributes = _.clone(validAttributes);
});

function testInvalidAttributes(done) {
store.saveIdentityWithAttributes(identifier, attributes).then(function() {
done(new Error("saveIdentityWithAttributes should have failed"));
}, function() {
done(); // good. we expect to fail with invalid attributes.
});
}

it('rejects an invalid publicKey', function(done) {
attributes.publicKey = 'a string';
testInvalidAttributes(done);
});
it('rejects invalid firstUse', function(done) {
attributes.firstUse = 0;
testInvalidAttributes(done);
});
it('rejects invalid timestamp', function(done) {
attributes.timestamp = NaN;
testInvalidAttributes(done);
});
it('rejects invalid verified', function(done) {
attributes.verified = null;
testInvalidAttributes(done);
});
it('rejects invalid nonblockingApproval', function(done) {
attributes.nonblockingApproval = 0;
testInvalidAttributes(done);
});
});
});
describe('setApproval', function() {
var record = new IdentityKeyRecord({id: identifier});
function fetchRecord() {
return new Promise(function(resolve) {
record.fetch().then(resolve);
});
}
it ('sets nonblockingApproval', function(done) {
store.setApproval(identifier, true).then(fetchRecord).then(function() {
assert.strictEqual(record.get('nonblockingApproval'), true);
}).then(done, done);
});
});
describe('setVerified', function() {
var record = new IdentityKeyRecord({id: identifier});
function fetchRecord() {
return new Promise(function(resolve) {
record.fetch().then(resolve);
});
}
it ('updates the verified status', function(done) {
store.setVerified(identifier, store.VerifiedStatus.UNVERIFIED).then(fetchRecord).then(function() {
assert.strictEqual(record.get('verified'), store.VerifiedStatus.UNVERIFIED);
}).then(done, done);
});
});
describe('getVerified', function() {
before(function(done) {
store.setVerified(identifier, store.VerifiedStatus.VERIFIED).then(done, done);
});
it ('resolves to the verified status', function(done) {
store.getVerified(identifier).then(function(result) {
assert.strictEqual(result, store.VerifiedStatus.VERIFIED);
}).then(done, done);
});
});
describe('isTrustedIdentity', function() {
describe('When invalid direction is given', function(done) {
it('should fail', function(done) {
Expand Down

0 comments on commit 5108014

Please sign in to comment.