[Pkg-javascript-commits] [node-sha.js] 96/237: refactor, to use buffers in tests

Bastien Roucariès rouca at moszumanska.debian.org
Fri May 5 09:03:39 UTC 2017


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

rouca pushed a commit to branch master
in repository node-sha.js.

commit 8e7119b5c079f51e5c24bfa2d5c3e25536e8b677
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Wed Jan 15 13:08:07 2014 +0700

    refactor, to use buffers in tests
---
 test/enc.js        | 34 ++++++++++++++++++++++++++--------
 test/fakebuffer.js | 31 -------------------------------
 test/hash.js       | 27 ++++++++++++++++-----------
 test/test.js       |  2 +-
 test/vectors.js    |  7 ++++---
 5 files changed, 47 insertions(+), 54 deletions(-)

diff --git a/test/enc.js b/test/enc.js
index d191590..00f39b0 100644
--- a/test/enc.js
+++ b/test/enc.js
@@ -1,7 +1,11 @@
-var Sha1 = require('../sha1')
+
+var Sha1 = require('../').sha1
 var tape = require('tape')
 var toString = require('../util').toString
-var toBuffer = require('bops/typedarray/from')
+var Buffer = require('native-buffer-browserify').Buffer
+var toBuffer = function (b, e) {
+  return new Buffer(b, e)
+}
 
 var EMPTY = new Uint8Array([
     218,  57, 163, 238,  94, 107,  75,  13,
@@ -9,31 +13,45 @@ var EMPTY = new Uint8Array([
     175, 216,   7,   9
   ])
 
+function equalBuffers(a, b, t) {
+
+  if(a.length !== b.length) throw new Error('different lengths')
+  for(var i = 0; i < a.length; i++)
+    if(a[i] !== b[i])
+      t.fail('buffers differ at index:' + i + '(' + a[i] + ' != ' + b[i] + ')')
+}
+
+
 tape('all the output encodings work', function (t) {
   var hex = new Sha1().update('', 'ascii').digest('hex')
   var buffer = new Sha1().update('', 'ascii').digest()
   var base64 = new Sha1().update('', 'ascii').digest('base64')
+
   t.equal(base64, '2jmj7l5rSw0yVb/vlWAYkK/YBwk=')
   t.equal(hex, 'da39a3ee5e6b4b0d3255bfef95601890afd80709')
-  t.deepEqual(buffer, EMPTY)
+
+  t.notEqual(typeof buffer, 'string')
+  console.error('output', typeof buffer)
+  equalBuffers(buffer, EMPTY, t)
+
   t.end()
 })
 
 tape('base64', function (t) {
-  t.deepEqual(toBuffer(toString(EMPTY, 'base64'), 'base64'), EMPTY)
+  equalBuffers(toBuffer(toString(EMPTY, 'base64'), 'base64'), EMPTY, t)
   t.end()
 })
 
-tape('all input encodings work', function (t) {
+tape('all input encodings work2', function (t) {
   var hex = new Sha1().update(toString(EMPTY, 'hex'), 'hex').digest('hex')
   var base64 = new Sha1().update(toString(EMPTY, 'base64'), 'base64').digest('hex')
   console.log('Binary:', JSON.stringify(toString(EMPTY, 'binary')))
   var binary = new Sha1().update(toString(EMPTY, 'binary'), 'binary').digest('hex')
   var buffer = new Sha1().update(EMPTY).digest('hex')
   console.log(hex, base64, buffer)
-  t.equal(hex, base64)
-  t.equal(hex, buffer)
-  t.equal(hex, binary)
+  equalBuffers(hex, base64, t)
+  equalBuffers(hex, buffer, t)
+  equalBuffers(hex, binary, t)
   t.end()
 })
 
diff --git a/test/fakebuffer.js b/test/fakebuffer.js
deleted file mode 100644
index afa5ed6..0000000
--- a/test/fakebuffer.js
+++ /dev/null
@@ -1,31 +0,0 @@
-
-var RealBuffer = require('buffer').Buffer
-var FakeBuffer = require('../fakebuffer')
-
-var length = 12
-var rb = new RealBuffer(length)
-var fb = new FakeBuffer(length)
-
-for(var i = 0; i < length; i += 4) {
-  var r = (Math.random()*0x80000000)|0
-  fb.writeUInt32BE(r, i)
-  rb.writeUInt32BE(r, i)
-}
-
-console.log(fb)
-console.log(rb)
-
-var equal = require('assert').equal
-
-for(var i = 0; i < length; i += 4) {
-  equal(rb.readUInt32BE(i), fb.readUInt32BE(i))
-  equal(rb.readInt32BE(i), fb.readInt32BE(i))
-  equal(rb.readUInt32LE(i), fb.readUInt32LE(i))
-  equal(rb.readInt32LE(i), fb.readInt32LE(i))
-}
-
-for(var i = 0; i < length; i ++) {
-  equal(rb[i], fb[i])
-}
-
-
diff --git a/test/hash.js b/test/hash.js
index 974da42..ab37784 100644
--- a/test/hash.js
+++ b/test/hash.js
@@ -1,11 +1,11 @@
-
-var Hash = require('../hash')
 var hexpp = require('../hexpp').defaults({bigendian: false})
 var u = require('../util')
 var tape = require('tape')
+var Buffer = require('native-buffer-browserify').Buffer
+var Hash = require('../hash')(Buffer)
 
 var hex = '0A1B2C3D4E5F6G7H', hexbuf
-function equal(t, a,b) {
+function equal(t, a, b) {
   t.equal(a.length, b.length)
   for(var i = 0; i < a.length; i++)
     t.equal(a[i], b[i])
@@ -14,11 +14,11 @@ function equal(t, a,b) {
 var count16 = {
       strings: ['0A1B2C3D4E5F6G7H'],
       buffers: [
-        hexbuf = new Uint8Array([
+        hexbuf = new Buffer([
           48, 65, 49, 66,   50, 67, 51, 68,
           52, 69, 53, 70,   54, 71, 55, 72
         ]),
-        new Uint8Array([
+        new Buffer([
          128,  0,  0,  0,    0,  0,  0,  0,
            0,  0,  0,  0,    0,  0,  0, 128
         ])
@@ -27,7 +27,7 @@ var count16 = {
 var empty = {
       strings: [''],
       buffers: [
-        new Uint8Array([
+        new Buffer([
          128,  0,  0,  0,    0,  0,  0,  0,
            0,  0,  0,  0,    0,  0,  0,  0
         ])
@@ -39,7 +39,7 @@ var multi = {
       strings: ['abcd', 'efhijk', 'lmnopq'],
       buffers: [
         toBuffer('abcdefhijklmnopq', 'ascii'),
-        new Uint8Array([
+        new Buffer([
          128,  0,  0,  0,    0,  0,  0,  0,
            0,  0,  0,  0,    0,  0,  0,  128
         ])
@@ -51,7 +51,7 @@ var long = {
       buffers: [
         hexbuf,
         hexbuf,
-        new Uint8Array([
+        new Buffer([
          128,  0,  0,  0,    0,  0,  0,  0,
            0,  0,  0,  0,    0,  0,  1,  0
         ])
@@ -60,7 +60,7 @@ var long = {
 
 
 function toBuffer (string, enc) {
-  var a = new Uint8Array(string.length)
+  var a = new Buffer(string.length)
   u.write(a, string, enc, 0, 0, string.length, true)
   return a
 }
@@ -69,13 +69,18 @@ function makeTest(name, data) {
   tape(name, function (t) {
 
     var h = new Hash(16, 8)
-    var hash = new Uint8Array(20)
+    var hash = new Buffer(20)
     var n = 2
     var expected = data.buffers.slice()
     //t.plan(expected.length + 1)
     h._update = function (block) {
       var e = expected.shift()
-      equal(t, block, e.buffer)
+      console.error('---block---')
+      console.error(hexpp(block), block.length)
+      console.error('---e---')
+      console.error(hexpp(e), block.length)
+      console.log(block)
+      equal(t, block, e)
       if(n < 0)
         throw new Error('expecting only 2 calls to _update')
 
diff --git a/test/test.js b/test/test.js
index 49cffae..4f3b1dd 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,7 +1,7 @@
 
 var crypto = require('crypto')
 var tape   = require('tape')
-var Sha1   = require('../sha1')
+var Sha1   = require('../').sha1
 var Uint32toHex = Sha1.Uint32toHex
 var u = require('../util')
 
diff --git a/test/vectors.js b/test/vectors.js
index c991a7b..afa7117 100644
--- a/test/vectors.js
+++ b/test/vectors.js
@@ -1,7 +1,8 @@
 
 var vectors = require('./nist-vectors.json')
 var tape = require('tape')
-var from = require('bops/typedarray/from')
+//var from = require('bops/typedarray/from')
+var Buffer = require('buffer').Buffer
 var hexpp = require('../hexpp')
 
 var createHash = require('../')
@@ -14,10 +15,10 @@ function makeTest(alg, i, verbose) {
       console.log(v)
       console.log('VECTOR', i)
       console.log('INPUT', v.input)
-      console.log(hexpp(from(v.input, 'base64')))
+      console.log(hexpp(new Buffer(v.input, 'base64')))
       console.log(new Buffer(v.input, 'base64').toString('hex'))
     }
-    var buf = from(v.input, 'base64')
+    var buf = new Buffer(v.input, 'base64')
 //    console.log(createHash(alg).update(buf))
     t.equal(createHash(alg).update(buf).digest('hex'), v[alg])
     t.end()

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



More information about the Pkg-javascript-commits mailing list