From 28779e0b3585969b2b4c612577751fd874afefb5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 27 Jul 2014 02:05:25 -0400 Subject: [PATCH] Fix tests by returning promises --- js/helpers.js | 19 ++++++++----- js/test.js | 77 +++++++++++++++++++++++---------------------------- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/js/helpers.js b/js/helpers.js index 58879c10..a576d9fd 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -570,11 +570,11 @@ window.textsecure.nacl = function() { var onLoadCallbacks = []; var naclLoaded = 0; self.registerOnLoadFunction = function(func) { - if (naclLoaded || !self.USE_NACL) { - func(); - return; - } - onLoadCallbacks[onLoadCallbacks.length] = func; + return new Promise(function(resolve, reject) { + if (naclLoaded || !self.USE_NACL) + return resolve(func()); + onLoadCallbacks[onLoadCallbacks.length] = [ func, resolve, reject ]; + }); } var naclMessageNextId = 0; @@ -582,8 +582,13 @@ window.textsecure.nacl = function() { window.moduleDidLoad = function() { common.hideModule(); naclLoaded = 1; - for (var i = 0; i < onLoadCallbacks.length; i++) - onLoadCallbacks[i](); + for (var i = 0; i < onLoadCallbacks.length; i++) { + try { + onLoadCallbacks[i][1](onLoadCallbacks[i][0]()); + } catch (e) { + onLoadCallbacks[i][2](e); + } + } onLoadCallbacks = []; } diff --git a/js/test.js b/js/test.js index e5dba698..652f5ee0 100644 --- a/js/test.js +++ b/js/test.js @@ -35,10 +35,9 @@ describe("Cryptographic primitives", function() { var counter = hexToArrayBuffer('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172a'); var ciphertext = hexToArrayBuffer('874d6191b620e3261bef6864990db6ce'); - window.crypto.subtle.encrypt({name: "AES-CTR", counter: counter}, key, plaintext).then(function(result) { - assert.strictEqual(getString(result), getString(ciphertext)); - done(); - }); + return window.crypto.subtle.encrypt({name: "AES-CTR", counter: counter}, key, plaintext).then(function(result) { + assert.strictEqual(getString(result) +"", getString(ciphertext)); + }).then(done).catch(done); }); }); @@ -48,10 +47,9 @@ describe("Cryptographic primitives", function() { var counter = hexToArrayBuffer('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172a'); var ciphertext = hexToArrayBuffer('874d6191b620e3261bef6864990db6ce'); - window.crypto.subtle.decrypt({name: "AES-CTR", counter: counter}, key, ciphertext).then(function(result) { + return window.crypto.subtle.decrypt({name: "AES-CTR", counter: counter}, key, ciphertext).then(function(result) { assert.strictEqual(getString(result), getString(plaintext)); - done(); - }); + }).then(done).catch(done); }); }); @@ -61,10 +59,9 @@ describe("Cryptographic primitives", function() { var iv = hexToArrayBuffer('000102030405060708090a0b0c0d0e0f'); var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'); var ciphertext = hexToArrayBuffer('f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644'); - window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, key, ciphertext).then(function(result) { + return window.crypto.subtle.decrypt({name: "AES-CBC", iv: iv}, key, ciphertext).then(function(result) { assert.strictEqual(getString(result), getString(plaintext)); - done(); - }); + }).then(done).catch(done); }); }); @@ -73,10 +70,9 @@ describe("Cryptographic primitives", function() { var key = hexToArrayBuffer('6f35628d65813435534b5d67fbdb54cb33403d04e843103e6399f806cb5df95febbdd61236f33245'); var input = hexToArrayBuffer('752cff52e4b90768558e5369e75d97c69643509a5e5904e0a386cbe4d0970ef73f918f675945a9aefe26daea27587e8dc909dd56fd0468805f834039b345f855cfe19c44b55af241fff3ffcd8045cd5c288e6c4e284c3720570b58e4d47b8feeedc52fd1401f698a209fccfa3b4c0d9a797b046a2759f82a54c41ccd7b5f592b'); var mac = getString(hexToArrayBuffer('05d1243e6465ed9620c9aec1c351a186')); - window.crypto.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input).then(function(result) { + return window.crypto.subtle.sign({name: "HMAC", hash: "SHA-256"}, key, input).then(function(result) { assert.strictEqual(getString(result).substring(0, mac.length), mac); - done(); - }); + }).then(done).catch(done); }); }); @@ -95,13 +91,12 @@ describe("Cryptographic primitives", function() { for (var i = 0; i < 10; i++) info[i] = 240 + i; - textsecure.crypto.testing_only.HKDF(IKM.buffer, salt.buffer, info.buffer).then(function(OKM){ + return textsecure.crypto.testing_only.HKDF(IKM.buffer, salt.buffer, info.buffer).then(function(OKM){ var T1 = hexToArrayBuffer("3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf"); var T2 = hexToArrayBuffer("34007208d5b887185865"); assert.equal(getString(OKM[0]), getString(T1)); assert.equal(getString(OKM[1]).substring(0, 10), getString(T2)); - done(); - }); + }).then(done).catch(done); }); }); }); @@ -125,13 +120,12 @@ describe('Unencrypted PushMessageProto "decrypt"', function() { message: text_message.encode() }; - textsecure.crypto.handleIncomingPushMessageProto(server_message). + return textsecure.crypto.handleIncomingPushMessageProto(server_message). then(function(message) { assert.equal(message.body, text_message.body); assert.equal(message.attachments.length, text_message.attachments.length); assert.equal(text_message.attachments.length, 0); - done(); - }); + }).then(done).catch(done); }); }); @@ -148,7 +142,7 @@ describe("Curve25519", function() { describe("Simple Curve25519 test vectors", function() { it('works', function(done) { - textsecure.registerOnLoadFunction(function() { + return textsecure.registerOnLoadFunction(function() { // These are just some random curve25519 test vectors I found online (with a version byte prepended to pubkeys) var alice_priv = hexToArrayBuffer("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"); var alice_pub = hexToArrayBuffer("058520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a"); @@ -156,14 +150,14 @@ describe("Curve25519", function() { var bob_pub = hexToArrayBuffer("05de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f"); var shared_sec = hexToArrayBuffer("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"); - textsecure.crypto.testing_only.privToPub(alice_priv, true).then(function(aliceKeyPair) { + return textsecure.crypto.testing_only.privToPub(alice_priv, true).then(function(aliceKeyPair) { var target = new Uint8Array(alice_priv.slice(0)); target[0] &= 248; target[31] &= 127; target[31] |= 64; assert.equal(getString(aliceKeyPair.privKey), getString(target)); - textsecure.crypto.testing_only.privToPub(bob_priv, true).then(function(bobKeyPair) { + return textsecure.crypto.testing_only.privToPub(bob_priv, true).then(function(bobKeyPair) { var target = new Uint8Array(bob_priv.slice(0)); target[0] &= 248; target[31] &= 127; @@ -172,23 +166,22 @@ describe("Curve25519", function() { assert.equal(getString(aliceKeyPair.pubKey), getString(alice_pub)); assert.equal(getString(bobKeyPair.pubKey), getString(bob_pub)); - textsecure.crypto.testing_only.ECDHE(bobKeyPair.pubKey, aliceKeyPair.privKey).then(function(ss) { + return textsecure.crypto.testing_only.ECDHE(bobKeyPair.pubKey, aliceKeyPair.privKey).then(function(ss) { assert.equal(getString(ss), getString(shared_sec)); - textsecure.crypto.testing_only.ECDHE(aliceKeyPair.pubKey, bobKeyPair.privKey).then(function(ss) { + return textsecure.crypto.testing_only.ECDHE(aliceKeyPair.pubKey, bobKeyPair.privKey).then(function(ss) { assert.equal(getString(ss), getString(shared_sec)); - done(); }); }); }); }); - }); + }).then(done).catch(done); }); }); describe("Simple Ed25519 tests", function() { it('works', function(done) { - textsecure.registerOnLoadFunction(function() { + return textsecure.registerOnLoadFunction(function() { // Some self-generated test vectors var priv = hexToArrayBuffer("48a8892cc4e49124b7b57d94fa15becfce071830d6449004685e387c62409973"); var pub = hexToArrayBuffer("0555f1bfede27b6a03e0dd389478ffb01462e5c52dbbac32cf870f00af1ed9af3a"); @@ -196,19 +189,17 @@ describe("Curve25519", function() { var sig = hexToArrayBuffer("2bc06c745acb8bae10fbc607ee306084d0c28e2b3bb819133392473431291fd0"+ "dfa9c7f11479996cf520730d2901267387e08d85bbf2af941590e3035a545285"); - textsecure.crypto.testing_only.privToPub(priv, false).then(function(pubCalc) { + return textsecure.crypto.testing_only.privToPub(priv, false).then(function(pubCalc) { //if (getString(pub) != getString(pubCalc)) // return false; - textsecure.crypto.testing_only.Ed25519Sign(priv, msg).then(function(sigCalc) { + return textsecure.crypto.testing_only.Ed25519Sign(priv, msg).then(function(sigCalc) { assert.equal(getString(sig), getString(sigCalc)); - textsecure.crypto.testing_only.Ed25519Verify(pub, msg, sig).then(function() { - done(); - }); + return textsecure.crypto.testing_only.Ed25519Verify(pub, msg, sig); }); }); - }); + }).then(done).catch(done); }); }); @@ -217,15 +208,15 @@ describe("Curve25519", function() { before(function() { localStorage.clear(); }); after(function() { localStorage.clear(); }); it ('works', function(done) { - textsecure.registerOnLoadFunction(function() { - textsecure.crypto.generateKeys().then(function() { + return textsecure.registerOnLoadFunction(function() { + return textsecure.crypto.generateKeys().then(function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey")); assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey0")); for (var i = 0; i < 100; i++) { assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i)); } var origIdentityKey = getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey); - textsecure.crypto.generateKeys().then(function() { + return textsecure.crypto.generateKeys().then(function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey")); assert.equal(getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey), origIdentityKey); @@ -236,7 +227,7 @@ describe("Curve25519", function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i)); } - textsecure.crypto.generateKeys().then(function() { + return textsecure.crypto.generateKeys().then(function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey")); assert.equal(getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey), origIdentityKey); @@ -247,12 +238,10 @@ describe("Curve25519", function() { for (var i = 0; i < 300; i++) { assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i)); } - - done(); }); }); }); - }); + }).then(done).catch(done); }); }); }); @@ -392,8 +381,12 @@ describe("Axolotl", function() { describe("test vectors", function() { _.each(axolotlTestVectors, function(t, i) { - it(t.name, function() { - assert(runAxolotlTest(t.vectors)); + it(t.name, function(done) { + return textsecure.registerOnLoadFunction(function() { + return runAxolotlTest(t.vectors).then(function(res) { + assert(res); + }); + }).then(done).catch(done); }); }); });