You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.5 KiB

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