source: node_modules/sha.js/test/hash.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1var tape = require('tape')
2var Hash = require('../hash')
3var hex = '0A1B2C3D4E5F6G7H'
4
5function equal (t, a, b) {
6 t.equal(a.length, b.length)
7 t.equal(a.toString('hex'), b.toString('hex'))
8}
9
10var hexBuf = Buffer.from('0A1B2C3D4E5F6G7H', 'utf8')
11var count16 = {
12 strings: ['0A1B2C3D4E5F6G7H'],
13 buffers: [
14 hexBuf,
15 Buffer.from('80000000000000000000000000000080', 'hex')
16 ]
17}
18
19var empty = {
20 strings: [''],
21 buffers: [
22 Buffer.from('80000000000000000000000000000000', 'hex')
23 ]
24}
25
26var multi = {
27 strings: ['abcd', 'efhijk', 'lmnopq'],
28 buffers: [
29 Buffer.from('abcdefhijklmnopq', 'ascii'),
30 Buffer.from('80000000000000000000000000000080', 'hex')
31 ]
32}
33
34var long = {
35 strings: [hex + hex],
36 buffers: [
37 hexBuf,
38 hexBuf,
39 Buffer.from('80000000000000000000000000000100', 'hex')
40 ]
41}
42
43function 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
72makeTest('Hash#update 1 in 1', count16)
73makeTest('empty Hash#update', empty)
74makeTest('Hash#update 1 in 3', multi)
75makeTest('Hash#update 2 in 1', long)
Note: See TracBrowser for help on using the repository browser.