[Pkg-javascript-commits] [node-evp-bytestokey] 07/29: Update tests

Bastien Roucariès rouca at moszumanska.debian.org
Fri Sep 8 09:56:49 UTC 2017


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

rouca pushed a commit to branch master
in repository node-evp-bytestokey.

commit 74c892f489be35096efb701de43ad93598589c87
Author: Kirill Fomichev <fanatid at ya.ru>
Date:   Fri Aug 26 12:30:21 2016 +0300

    Update tests
---
 README.md     |  2 +-
 binding.gyp   | 37 +++++++++++++++++++++++++++++++++++++
 package.json  | 11 +++++++----
 test.js       | 19 -------------------
 test/index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 test/main.cc  | 37 +++++++++++++++++++++++++++++++++++++
 6 files changed, 131 insertions(+), 24 deletions(-)

diff --git a/README.md b/README.md
index 7b6c73b..7c8d252 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ The super secure [key derivation algorithm from openssl][1]
 * `data` - `Buffer`, data which is used to derive the keying data.
 * `count` - `number`, count is the iteration count to use.
 * `keyLen` - `number`, key length in bytes.
-* `ivLen` - `number`, iv length in bytes
+* `ivLen` - `number`, iv length in bytes.
 
 *Return*: `{ key: Buffer, iv: Buffer }`
 
diff --git a/binding.gyp b/binding.gyp
new file mode 100644
index 0000000..a6ec9ed
--- /dev/null
+++ b/binding.gyp
@@ -0,0 +1,37 @@
+{
+  "targets": [{
+    "target_name": "OpenSSL_EVP_BytesToKey",
+    "sources": [
+      "./test/main.cc",
+    ],
+    "cflags": [
+      "-Wall",
+      "-Wno-maybe-uninitialized",
+      "-Wno-uninitialized",
+      "-Wno-unused-function",
+      "-Wextra"
+    ],
+    "cflags_cc+": [
+      "-std=c++0x"
+    ],
+    "include_dirs": [
+      "/usr/local/include",
+      "<!(node -e \"require('nan')\")"
+    ],
+    "conditions": [
+      [
+        "OS=='mac'", {
+          "libraries": [
+            "-L/usr/local/lib"
+          ],
+          "xcode_settings": {
+            "MACOSX_DEPLOYMENT_TARGET": "10.7",
+            "OTHER_CPLUSPLUSFLAGS": [
+              "-stdlib=libc++"
+            ]
+          }
+        }
+      ]
+    ]
+  }]
+}
diff --git a/package.json b/package.json
index fb01f96..fd8ffba 100644
--- a/package.json
+++ b/package.json
@@ -24,15 +24,18 @@
     "url": "https://github.com/crypto-browserify/EVP_BytesToKey.git"
   },
   "scripts": {
-    "coverage": "nyc node test/*.js",
+    "coverage": "nyc tape test/*.js",
     "lint": "standard",
     "test": "npm run lint && npm run unit",
-    "unit": "node test/*.js"
+    "test:prepare": "node-gyp rebuild",
+    "unit": "tape test/*.js"
   },
   "devDependencies": {
-    "create-hash": "^1.1.2",
+    "bindings": "^1.2.1",
+    "nan": "^2.4.0",
     "nyc": "^8.1.0",
     "standard": "^8.0.0",
     "tape": "^4.6.0"
-  }
+  },
+  "gypfile": true
 }
diff --git a/test.js b/test.js
deleted file mode 100644
index a638fa0..0000000
--- a/test.js
+++ /dev/null
@@ -1,19 +0,0 @@
-var test = require('tape')
-var evp = require('./')
-var crypto = require('crypto')
-
-function runTest (password) {
-  test('password: ' + password, function (t) {
-    t.plan(1)
-    var keys = evp(password, false, 256, 16)
-    var nodeCipher = crypto.createCipher('aes-256-ctr', password)
-    var ourCipher = crypto.createCipheriv('aes-256-ctr', keys.key, keys.iv)
-    var nodeOut = nodeCipher.update('foooooo')
-    var ourOut = ourCipher.update('foooooo')
-    t.equals(nodeOut.toString('hex'), ourOut.toString('hex'))
-  })
-}
-runTest('password')
-runTest('ふっかつ あきる すぶり はやい つける まゆげ たんさん みんぞく ねほりはほり せまい たいまつばな ひはん')
-runTest('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞')
-runTest('💩')
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..138c74f
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,49 @@
+/* eslint-disable camelcase */
+try {
+  require('bindings')('OpenSSL_EVP_BytesToKey')
+} catch (err) {
+  console.error('Run "npm run test:prepare" first')
+  process.exit(1)
+}
+
+var OpenSSL_EVP_BytesToKey = require('bindings')('OpenSSL_EVP_BytesToKey')
+var crypto = require('crypto')
+var test = require('tape')
+var EVP_BytesToKey = require('../')
+
+function createHashMD5 () { return crypto.createHash('md5') }
+var keyLen = 32
+var ivLen = 16
+
+var counts = [1, 2, 100]
+for (var i = 0; i < counts.length; ++i) {
+  var count = counts[i]
+
+  test('count = ' + count, function (t) {
+    var salt = crypto.randomBytes(8)
+    var data = crypto.randomBytes(10 + Math.round(Math.random() * 100))
+
+    t.test('salt is null', function (t) {
+      var result = EVP_BytesToKey(createHashMD5, null, data, count, keyLen, ivLen)
+      var expected = OpenSSL_EVP_BytesToKey.md5_key32_iv16(null, data, count)
+      t.same(result, expected)
+      t.end()
+    })
+
+    t.test('salt is not null', function (t) {
+      var result = EVP_BytesToKey(createHashMD5, salt, data, count, keyLen, ivLen)
+      var expected = OpenSSL_EVP_BytesToKey.md5_key32_iv16(salt, data, count)
+      t.same(result, expected)
+      t.end()
+    })
+
+    t.end()
+  })
+}
+
+test('salt buffer length is 7', function (t) {
+  t.throws(function () {
+    EVP_BytesToKey(createHashMD5, new Buffer(7))
+  }, /^RangeError: salt should be Buffer with 8 byte length$/)
+  t.end()
+})
diff --git a/test/main.cc b/test/main.cc
new file mode 100644
index 0000000..9a7ba03
--- /dev/null
+++ b/test/main.cc
@@ -0,0 +1,37 @@
+#include <node.h>
+#include <nan.h>
+#include <openssl/evp.h>
+
+NAN_METHOD(md5_key32_iv16) {
+  Nan::HandleScope scope;
+
+  const EVP_CIPHER* cipher = EVP_get_cipherbyname("aes-256-cbc");
+
+  const unsigned char* salt = NULL;
+  if (!info[0]->IsNull()) {
+    salt = (const unsigned char*) node::Buffer::Data(info[0].As<v8::Object>());
+  }
+
+  v8::Local<v8::Object> data_buffer = info[1].As<v8::Object>();
+  const unsigned char* data = (const unsigned char*) node::Buffer::Data(data_buffer);
+
+  const int count = info[3].As<v8::Integer>()->Value();
+  unsigned char key[32] = {0};
+  unsigned char iv[16] = {0};
+
+  int key_len = EVP_BytesToKey(cipher, EVP_md5(), salt, data, node::Buffer::Length(data_buffer), count, key, iv);
+  if (key_len != 32) {
+    return Nan::ThrowRangeError("invalid key length returned");
+  }
+
+  v8::Local<v8::Object> obj = Nan::New<v8::Object>();
+  obj->Set(Nan::New<v8::String>("key").ToLocalChecked(), Nan::CopyBuffer((const char*) key, 32).ToLocalChecked());
+  obj->Set(Nan::New<v8::String>("iv").ToLocalChecked(), Nan::CopyBuffer((const char*) iv, 16).ToLocalChecked());
+  info.GetReturnValue().Set(obj);
+}
+
+NAN_MODULE_INIT(Init) {
+  Nan::Export(target, "md5_key32_iv16", md5_key32_iv16);
+}
+
+NODE_MODULE(OpenSSL_EVP_BytesToKey, Init)

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



More information about the Pkg-javascript-commits mailing list