From f15fadbb4d941269e538005feac4f5f7f31d0666 Mon Sep 17 00:00:00 2001 From: lilia Date: Fri, 16 Jun 2017 20:56:06 -0700 Subject: [PATCH] Test processVerifiedMessage --- test/storage_test.js | 122 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/test/storage_test.js b/test/storage_test.js index 07ddd8906..52d630dba 100644 --- a/test/storage_test.js +++ b/test/storage_test.js @@ -338,6 +338,128 @@ describe("SignalProtocolStore", function() { }); }); }); + describe('processVerifiedMessage', function() { + var record; + var newIdentity = libsignal.crypto.getRandomBytes(33); + function fetchRecord() { + return new Promise(function(resolve, reject) { + record.fetch().then(resolve, reject); + }); + } + describe('when the new verified status is DEFAULT', function() { + describe('when there is no existing record', function() { + before(function() { + record = new IdentityKeyRecord({ id: identifier }); + return new Promise(function(resolve, reject) { + record.destroy().then(resolve, reject); + }); + }); + + it ('does nothing', function() { + return store.processVerifiedMessage( + identifier, store.VerifiedStatus.DEFAULT, newIdentity + ).then(fetchRecord).then(function() { + // fetchRecord resolved so there is a record. + // Bad. + throw new Error("processVerifiedMessage should not save new records"); + }, function() { + return; + }); + }); + }); + describe('when the record exists and is not DEFAULT and the key matches', function() { + before(function() { + record = new IdentityKeyRecord({ + id : identifier, + publicKey : testKey.pubKey, + firstUse : true, + timestamp : Date.now(), + verified : store.VerifiedStatus.VERIFIED, + nonblockingApproval : false + }); + return new Promise(function(resolve, reject) { + record.save().then(resolve, reject); + }); + }); + it ('updates the verified status', function() { + return store.processVerifiedMessage( + identifier, store.VerifiedStatus.DEFAULT, testKey.pubKey + ).then(fetchRecord).then(function() { + assert.strictEqual(record.get('verified'), store.VerifiedStatus.DEFAULT); + assertEqualArrayBuffers(record.get('publicKey'), testKey.pubKey); + }); + }); + }); + }); + describe('when the new verified status is VERIFIED', function() { + describe('when there is no existing record', function() { + before(function() { + record = new IdentityKeyRecord({ id: identifier }); + return new Promise(function(resolve, reject) { + record.destroy().then(resolve, reject); + }); + }); + + it ('saves the new identity and marks it verified', function() { + return store.processVerifiedMessage( + identifier, store.VerifiedStatus.VERIFIED, newIdentity + ).then(fetchRecord).then(function() { + assert.strictEqual(record.get('verified'), store.VerifiedStatus.VERIFIED); + assertEqualArrayBuffers(record.get('publicKey'), newIdentity); + }); + }); + }); + describe('when the record exists record', function() { + describe('when the existing key is different', function() { + before(function() { + record = new IdentityKeyRecord({ + id : identifier, + publicKey : testKey.pubKey, + firstUse : true, + timestamp : Date.now(), + verified : store.VerifiedStatus.VERIFIED, + nonblockingApproval : false + }); + return new Promise(function(resolve, reject) { + record.save().then(resolve, reject); + }); + }); + it ('saves the new identity and marks it verified', function() { + return store.processVerifiedMessage( + identifier, store.VerifiedStatus.VERIFIED, newIdentity + ).then(fetchRecord).then(function() { + assert.strictEqual(record.get('verified'), store.VerifiedStatus.VERIFIED); + assertEqualArrayBuffers(record.get('publicKey'), newIdentity); + }); + }); + }); + describe('when the existing key is the same and not verified', function() { + before(function() { + record = new IdentityKeyRecord({ + id : identifier, + publicKey : testKey.pubKey, + firstUse : true, + timestamp : Date.now(), + verified : store.VerifiedStatus.UNVERIFIED, + nonblockingApproval : false + }); + return new Promise(function(resolve, reject) { + record.save().then(resolve, reject); + }); + }); + + it ('saves the identity and marks it verified', function() { + return store.processVerifiedMessage( + identifier, store.VerifiedStatus.VERIFIED, testKey.pubKey + ).then(fetchRecord).then(function() { + assert.strictEqual(record.get('verified'), store.VerifiedStatus.VERIFIED); + assertEqualArrayBuffers(record.get('publicKey'), testKey.pubKey); + }); + }); + }); + }); + }); + }); describe('getVerified', function() { before(function(done) { store.setVerified(identifier, store.VerifiedStatus.VERIFIED).then(done, done);