2017-05-03 15:35:00 +02:00
|
|
|
var tape = require('tape')
|
|
|
|
var Hash = require('../hash')
|
|
|
|
var hex = '0A1B2C3D4E5F6G7H'
|
|
|
|
|
|
|
|
function equal (t, a, b) {
|
|
|
|
t.equal(a.length, b.length)
|
2017-12-10 21:51:33 +01:00
|
|
|
t.equal(a.toString('hex'), b.toString('hex'))
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var hexBuf = Buffer.from('0A1B2C3D4E5F6G7H', 'utf8')
|
2017-05-03 15:35:00 +02:00
|
|
|
var count16 = {
|
|
|
|
strings: ['0A1B2C3D4E5F6G7H'],
|
|
|
|
buffers: [
|
|
|
|
hexBuf,
|
2017-12-10 21:51:33 +01:00
|
|
|
Buffer.from('80000000000000000000000000000080', 'hex')
|
2017-05-03 15:35:00 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
var empty = {
|
|
|
|
strings: [''],
|
|
|
|
buffers: [
|
2017-12-10 21:51:33 +01:00
|
|
|
Buffer.from('80000000000000000000000000000000', 'hex')
|
2017-05-03 15:35:00 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
var multi = {
|
2017-12-10 21:51:33 +01:00
|
|
|
strings: ['abcd', 'efhijk', 'lmnopq'],
|
|
|
|
buffers: [
|
|
|
|
Buffer.from('abcdefhijklmnopq', 'ascii'),
|
|
|
|
Buffer.from('80000000000000000000000000000080', 'hex')
|
|
|
|
]
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
var long = {
|
|
|
|
strings: [hex + hex],
|
|
|
|
buffers: [
|
|
|
|
hexBuf,
|
|
|
|
hexBuf,
|
2017-12-10 21:51:33 +01:00
|
|
|
Buffer.from('80000000000000000000000000000100', 'hex')
|
2017-05-03 15:35:00 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeTest (name, data) {
|
|
|
|
tape(name, function (t) {
|
|
|
|
var h = new Hash(16, 8)
|
2017-12-10 21:51:33 +01:00
|
|
|
var hash = Buffer.alloc(20)
|
2017-05-03 15:35:00 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
throw new Error('expecting only 2 calls to _update')
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
}
|
|
|
|
h._hash = function () {
|
2017-05-03 15:35:00 +02:00
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
data.strings.forEach(function (string) {
|
|
|
|
h.update(string, 'ascii')
|
|
|
|
})
|
|
|
|
|
|
|
|
equal(t, h.digest(), hash)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
makeTest('Hash#update 1 in 1', count16)
|
|
|
|
makeTest('empty Hash#update', empty)
|
|
|
|
makeTest('Hash#update 1 in 3', multi)
|
|
|
|
makeTest('Hash#update 2 in 1', long)
|