1 | var tape = require('tape')
|
---|
2 | var Hash = require('../hash')
|
---|
3 | var hex = '0A1B2C3D4E5F6G7H'
|
---|
4 |
|
---|
5 | function equal (t, a, b) {
|
---|
6 | t.equal(a.length, b.length)
|
---|
7 | t.equal(a.toString('hex'), b.toString('hex'))
|
---|
8 | }
|
---|
9 |
|
---|
10 | var hexBuf = Buffer.from('0A1B2C3D4E5F6G7H', 'utf8')
|
---|
11 | var count16 = {
|
---|
12 | strings: ['0A1B2C3D4E5F6G7H'],
|
---|
13 | buffers: [
|
---|
14 | hexBuf,
|
---|
15 | Buffer.from('80000000000000000000000000000080', 'hex')
|
---|
16 | ]
|
---|
17 | }
|
---|
18 |
|
---|
19 | var empty = {
|
---|
20 | strings: [''],
|
---|
21 | buffers: [
|
---|
22 | Buffer.from('80000000000000000000000000000000', 'hex')
|
---|
23 | ]
|
---|
24 | }
|
---|
25 |
|
---|
26 | var multi = {
|
---|
27 | strings: ['abcd', 'efhijk', 'lmnopq'],
|
---|
28 | buffers: [
|
---|
29 | Buffer.from('abcdefhijklmnopq', 'ascii'),
|
---|
30 | Buffer.from('80000000000000000000000000000080', 'hex')
|
---|
31 | ]
|
---|
32 | }
|
---|
33 |
|
---|
34 | var long = {
|
---|
35 | strings: [hex + hex],
|
---|
36 | buffers: [
|
---|
37 | hexBuf,
|
---|
38 | hexBuf,
|
---|
39 | Buffer.from('80000000000000000000000000000100', 'hex')
|
---|
40 | ]
|
---|
41 | }
|
---|
42 |
|
---|
43 | function makeTest (name, data) {
|
---|
44 | tape(name, function (t) {
|
---|
45 | var h = new Hash(16, 8)
|
---|
46 | var hash = Buffer.alloc(20)
|
---|
47 | var n = 2
|
---|
48 | var expected = data.buffers.slice()
|
---|
49 | // t.plan(expected.length + 1)
|
---|
50 |
|
---|
51 | h._update = function (block) {
|
---|
52 | var e = expected.shift()
|
---|
53 | equal(t, block, e)
|
---|
54 |
|
---|
55 | if (n < 0) {
|
---|
56 | throw new Error('expecting only 2 calls to _update')
|
---|
57 | }
|
---|
58 | }
|
---|
59 | h._hash = function () {
|
---|
60 | return hash
|
---|
61 | }
|
---|
62 |
|
---|
63 | data.strings.forEach(function (string) {
|
---|
64 | h.update(string, 'ascii')
|
---|
65 | })
|
---|
66 |
|
---|
67 | equal(t, h.digest(), hash)
|
---|
68 | t.end()
|
---|
69 | })
|
---|
70 | }
|
---|
71 |
|
---|
72 | makeTest('Hash#update 1 in 1', count16)
|
---|
73 | makeTest('empty Hash#update', empty)
|
---|
74 | makeTest('Hash#update 1 in 3', multi)
|
---|
75 | makeTest('Hash#update 2 in 1', long)
|
---|