[Pkg-javascript-commits] [node-browserify-aes] 33/43: tests: move TEST_CASES for >Node10 to json

Bastien Roucariès rouca at moszumanska.debian.org
Thu Sep 7 14:42:00 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-browserify-aes.

commit dfdccbb28f6b59fe1149aa5edd646c8dcba6ffce
Author: Daniel Cousens <github at dcousens.com>
Date:   Fri Jun 16 13:39:18 2017 +1000

    tests: move TEST_CASES for >Node10 to json
---
 test/extra.json |  59 +++++++++++++
 test/index.js   | 269 ++++++++++++++++++++++----------------------------------
 2 files changed, 166 insertions(+), 162 deletions(-)

diff --git a/test/extra.json b/test/extra.json
new file mode 100644
index 0000000..a2bc7dd
--- /dev/null
+++ b/test/extra.json
@@ -0,0 +1,59 @@
+[
+  {
+    "algo": "aes-128-gcm",
+    "key": "6970787039613669314d623455536234",
+    "iv": "583673497131313748307652",
+    "plain": "Hello World!",
+    "ct": "4BE13896F64DFA2C2D0F2C76",
+    "tag": "272B422F62EB545EAA15B5FF84092447",
+    "tampered": false
+  },
+  {
+    "algo": "aes-128-gcm",
+    "key": "6970787039613669314d623455536234",
+    "iv": "583673497131313748307652",
+    "plain": "Hello World!",
+    "ct": "4BE13896F64DFA2C2D0F2C76",
+    "aad": "000000FF",
+    "tag": "BA2479F66275665A88CB7B15F43EB005",
+    "tampered": false
+  },
+  {
+    "algo": "aes-128-gcm",
+    "key": "6970787039613669314d623455536234",
+    "iv": "583673497131313748307652",
+    "plain": "Hello World!",
+    "ct": "4BE13596F64DFA2C2D0FAC76",
+    "tag": "272B422F62EB545EAA15B5FF84092447",
+    "tampered": true
+  },
+  {
+    "algo": "aes-256-gcm",
+    "key": "337a54767a7233703637564336316a6d56353472495975313534357834546c59",
+    "iv": "36306950306836764a6f4561",
+    "plain": "Hello node.js world!",
+    "ct": "58E62CFE7B1D274111A82267EBB93866E72B6C2A",
+    "tag": "9BB44F663BADABACAE9720881FB1EC7A",
+    "tampered": false
+  },
+  {
+    "algo": "aes-256-gcm",
+    "key": "337a54767a7233703637564336316a6d56353472495975313534357834546c59",
+    "iv": "36306950306836764a6f4561",
+    "plain": "Hello node.js world!",
+    "ct": "58E62CFF7B1D274011A82267EBB93866E72B6C2B",
+    "tag": "9BB44F663BADABACAE9720881FB1EC7A",
+    "tampered": true
+  },
+  {
+    "algo": "aes-192-gcm",
+    "key": "1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9",
+    "iv": "0e1791e9db3bd21a9122c416",
+    "plain": "Hello node.js world!",
+    "password": "very bad password",
+    "aad": "63616c76696e",
+    "ct": "DDA53A4059AA17B88756984995F7BBA3C636CC44",
+    "tag": "D2A35E5C611E5E3D2258360241C5B045",
+    "tampered": false
+  }
+]
diff --git a/test/index.js b/test/index.js
index eaaf33a..97c82a4 100644
--- a/test/index.js
+++ b/test/index.js
@@ -1,6 +1,7 @@
 var Buffer = require('safe-buffer').Buffer
 var test = require('tape')
 var fixtures = require('./fixtures.json')
+var fixtures2 = require('./extra.json')
 var _crypto = require('crypto')
 var crypto = require('../browser.js')
 var modes = require('../modes')
@@ -247,175 +248,119 @@ fixtures.forEach(function (fixture, i) {
   })
 })
 
-test('node tests', function (t) {
-  var TEST_CASES = [
-    { algo: 'aes-128-gcm',
-      key: '6970787039613669314d623455536234',
-      iv: '583673497131313748307652',
-      plain: 'Hello World!',
-      ct: '4BE13896F64DFA2C2D0F2C76',
-      tag: '272B422F62EB545EAA15B5FF84092447',
-      tampered: false },
-    { algo: 'aes-128-gcm',
-      key: '6970787039613669314d623455536234',
-      iv: '583673497131313748307652',
-      plain: 'Hello World!',
-      ct: '4BE13896F64DFA2C2D0F2C76',
-      aad: '000000FF',
-      tag: 'BA2479F66275665A88CB7B15F43EB005',
-      tampered: false },
-    { algo: 'aes-128-gcm',
-      key: '6970787039613669314d623455536234',
-      iv: '583673497131313748307652',
-      plain: 'Hello World!',
-      ct: '4BE13596F64DFA2C2D0FAC76',
-      tag: '272B422F62EB545EAA15B5FF84092447',
-      tampered: true },
-    { algo: 'aes-256-gcm',
-      key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
-      iv: '36306950306836764a6f4561',
-      plain: 'Hello node.js world!',
-      ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A',
-      tag: '9BB44F663BADABACAE9720881FB1EC7A',
-      tampered: false },
-    { algo: 'aes-256-gcm',
-      key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
-      iv: '36306950306836764a6f4561',
-      plain: 'Hello node.js world!',
-      ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B',
-      tag: '9BB44F663BADABACAE9720881FB1EC7A',
-      tampered: true },
-    { algo: 'aes-192-gcm',
-      key: '1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9',
-      iv: '0e1791e9db3bd21a9122c416',
-      plain: 'Hello node.js world!',
-      password: 'very bad password',
-      aad: '63616c76696e',
-      ct: 'DDA53A4059AA17B88756984995F7BBA3C636CC44',
-      tag: 'D2A35E5C611E5E3D2258360241C5B045',
-      tampered: false }
-  ]
-
-  var ciphers = Object.keys(modes)
-  function testIt (i) {
-    t.test('test case ' + i, function (t) {
-      var test = TEST_CASES[i]
-
-      if (ciphers.indexOf(test.algo) === -1) {
-        console.log('skipping unsupported ' + test.algo + ' test')
-        return
-      }
-
-      (function () {
-        var encrypt = crypto.createCipheriv(test.algo,
-          Buffer.from(test.key, 'hex'), Buffer.from(test.iv, 'hex'))
-        if (test.aad) encrypt.setAAD(Buffer.from(test.aad, 'hex'))
+var ciphers = Object.keys(modes)
 
-        var hex = encrypt.update(test.plain, 'ascii', 'hex')
-        hex += encrypt.final('hex')
-        var authTag = encrypt.getAuthTag()
+fixtures2.forEach((f, i) => {
+  test('test case ' + i, function (t) {
+    if (ciphers.indexOf(f.algo) === -1) {
+      console.log('skipping unsupported ' + f.algo + ' test')
+      return
+    }
 
-        // only test basic encryption run if output is marked as tampered.
-        if (!test.tampered) {
-          t.equal(hex.toUpperCase(), test.ct)
-          t.equal(authTag.toString('hex').toUpperCase(), test.tag)
-        }
-      })()
-
-      ;(function () {
-        var decrypt = crypto.createDecipheriv(test.algo,
-          Buffer.from(test.key, 'hex'), Buffer.from(test.iv, 'hex'))
-        decrypt.setAuthTag(Buffer.from(test.tag, 'hex'))
-        if (test.aad) decrypt.setAAD(Buffer.from(test.aad, 'hex'))
-        var msg = decrypt.update(test.ct, 'hex', 'ascii')
-        if (!test.tampered) {
-          msg += decrypt.final('ascii')
-          t.equal(msg, test.plain)
-        } else {
-          // assert that final throws if input data could not be verified!
-          t.throws(function () { decrypt.final('ascii') }, / auth/)
-        }
-      })()
-
-      ;(function () {
-        if (!test.password) return
-        var encrypt = crypto.createCipher(test.algo, test.password)
-        if (test.aad) encrypt.setAAD(Buffer.from(test.aad, 'hex'))
-        var hex = encrypt.update(test.plain, 'ascii', 'hex')
-        hex += encrypt.final('hex')
-        var authTag = encrypt.getAuthTag()
-        // only test basic encryption run if output is marked as tampered.
-        if (!test.tampered) {
-          t.equal(hex.toUpperCase(), test.ct)
-          t.equal(authTag.toString('hex').toUpperCase(), test.tag)
-        }
-      })()
-
-      ;(function () {
-        if (!test.password) return
-        var decrypt = crypto.createDecipher(test.algo, test.password)
-        decrypt.setAuthTag(Buffer.from(test.tag, 'hex'))
-        if (test.aad) decrypt.setAAD(Buffer.from(test.aad, 'hex'))
-        var msg = decrypt.update(test.ct, 'hex', 'ascii')
-        if (!test.tampered) {
-          msg += decrypt.final('ascii')
-          t.equal(msg, test.plain)
-        } else {
-          // assert that final throws if input data could not be verified!
-          t.throws(function () { decrypt.final('ascii') }, / auth/)
-        }
-      })()
+    (function () {
+      var encrypt = crypto.createCipheriv(f.algo,
+        Buffer.from(f.key, 'hex'), Buffer.from(f.iv, 'hex'))
+      if (f.aad) encrypt.setAAD(Buffer.from(f.aad, 'hex'))
 
-      // after normal operation, test some incorrect ways of calling the API:
-      // it's most certainly enough to run these tests with one algorithm only.
+      var hex = encrypt.update(f.plain, 'ascii', 'hex')
+      hex += encrypt.final('hex')
+      var authTag = encrypt.getAuthTag()
 
-      if (i > 0) {
-        t.end()
-        return
+      // only test basic encryption run if output is marked as tampered.
+      if (!f.tampered) {
+        t.equal(hex.toUpperCase(), f.ct)
+        t.equal(authTag.toString('hex').toUpperCase(), f.tag)
+      }
+    })()
+
+    ;(function () {
+      var decrypt = crypto.createDecipheriv(f.algo,
+        Buffer.from(f.key, 'hex'), Buffer.from(f.iv, 'hex'))
+      decrypt.setAuthTag(Buffer.from(f.tag, 'hex'))
+      if (f.aad) decrypt.setAAD(Buffer.from(f.aad, 'hex'))
+      var msg = decrypt.update(f.ct, 'hex', 'ascii')
+      if (!f.tampered) {
+        msg += decrypt.final('ascii')
+        t.equal(msg, f.plain)
+      } else {
+        // assert that final throws if input data could not be verified!
+        t.throws(function () { decrypt.final('ascii') }, / auth/)
+      }
+    })()
+
+    ;(function () {
+      if (!f.password) return
+      var encrypt = crypto.createCipher(f.algo, f.password)
+      if (f.aad) encrypt.setAAD(Buffer.from(f.aad, 'hex'))
+      var hex = encrypt.update(f.plain, 'ascii', 'hex')
+      hex += encrypt.final('hex')
+      var authTag = encrypt.getAuthTag()
+      // only test basic encryption run if output is marked as tampered.
+      if (!f.tampered) {
+        t.equal(hex.toUpperCase(), f.ct)
+        t.equal(authTag.toString('hex').toUpperCase(), f.tag)
       }
+    })()
+
+    ;(function () {
+      if (!f.password) return
+      var decrypt = crypto.createDecipher(f.algo, f.password)
+      decrypt.setAuthTag(Buffer.from(f.tag, 'hex'))
+      if (f.aad) decrypt.setAAD(Buffer.from(f.aad, 'hex'))
+      var msg = decrypt.update(f.ct, 'hex', 'ascii')
+      if (!f.tampered) {
+        msg += decrypt.final('ascii')
+        t.equal(msg, f.plain)
+      } else {
+        // assert that final throws if input data could not be verified!
+        t.throws(function () { decrypt.final('ascii') }, / auth/)
+      }
+    })()
 
-      (function () {
-        // non-authenticating mode:
-        var encrypt = crypto.createCipheriv('aes-128-cbc',
-          'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC')
-        encrypt.update('blah', 'ascii')
-        encrypt.final()
-        t.throws(function () { encrypt.getAuthTag() })
-        t.throws(function () {
-          encrypt.setAAD(Buffer.from('123', 'ascii'))
-        })
-      })()
-
-      ;(function () {
-        // trying to get tag before inputting all data:
-        var encrypt = crypto.createCipheriv(test.algo,
-          Buffer.from(test.key, 'hex'), Buffer.from(test.iv, 'hex'))
-        encrypt.update('blah', 'ascii')
-        t.throws(function () { encrypt.getAuthTag() }, / state/)
-      })()
-
-      ;(function () {
-        // trying to set tag on encryption object:
-        var encrypt = crypto.createCipheriv(test.algo,
-          Buffer.from(test.key, 'hex'), Buffer.from(test.iv, 'hex'))
-        t.throws(function () {
-          encrypt.setAuthTag(Buffer.from(test.tag, 'hex'))
-        }, / state/)
-      })()
-
-      ;(function () {
-        // trying to read tag from decryption object:
-        var decrypt = crypto.createDecipheriv(test.algo,
-          Buffer.from(test.key, 'hex'), Buffer.from(test.iv, 'hex'))
-        t.throws(function () { decrypt.getAuthTag() }, / state/)
-      })()
+    // after normal operation, test some incorrect ways of calling the API:
+    // it's most certainly enough to run these tests with one algorithm only.
+    if (i !== 0) {
       t.end()
-    })
-  }
-
-  for (var i in TEST_CASES) {
-    testIt(i)
-  }
+      return
+    }
+
+    (function () {
+      // non-authenticating mode:
+      var encrypt = crypto.createCipheriv('aes-128-cbc',
+        'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC')
+      encrypt.update('blah', 'ascii')
+      encrypt.final()
+      t.throws(function () { encrypt.getAuthTag() })
+      t.throws(function () {
+        encrypt.setAAD(Buffer.from('123', 'ascii'))
+      })
+    })()
+
+    ;(function () {
+      // trying to get tag before inputting all data:
+      var encrypt = crypto.createCipheriv(f.algo,
+        Buffer.from(f.key, 'hex'), Buffer.from(f.iv, 'hex'))
+      encrypt.update('blah', 'ascii')
+      t.throws(function () { encrypt.getAuthTag() }, / state/)
+    })()
+
+    ;(function () {
+      // trying to set tag on encryption object:
+      var encrypt = crypto.createCipheriv(f.algo,
+        Buffer.from(f.key, 'hex'), Buffer.from(f.iv, 'hex'))
+      t.throws(function () {
+        encrypt.setAuthTag(Buffer.from(f.tag, 'hex'))
+      }, / state/)
+    })()
+
+    ;(function () {
+      // trying to read tag from decryption object:
+      var decrypt = crypto.createDecipheriv(f.algo,
+        Buffer.from(f.key, 'hex'), Buffer.from(f.iv, 'hex'))
+      t.throws(function () { decrypt.getAuthTag() }, / state/)
+    })()
+    t.end()
+  })
 })
 
 test('autopadding false decipher', function (t) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browserify-aes.git



More information about the Pkg-javascript-commits mailing list