test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var test = require('tape')
  2. var CipherBase = require('./')
  3. var inherits = require('inherits')
  4. test('basic version', function (t) {
  5. inherits(Cipher, CipherBase)
  6. function Cipher () {
  7. CipherBase.call(this)
  8. }
  9. Cipher.prototype._update = function (input) {
  10. t.ok(Buffer.isBuffer(input))
  11. return input
  12. }
  13. Cipher.prototype._final = function () {
  14. // noop
  15. }
  16. var cipher = new Cipher()
  17. var utf8 = 'abc123abcd'
  18. var update = cipher.update(utf8, 'utf8', 'base64') + cipher.final('base64')
  19. var string = (new Buffer(update, 'base64')).toString()
  20. t.equals(utf8, string)
  21. t.end()
  22. })
  23. test('hash mode', function (t) {
  24. inherits(Cipher, CipherBase)
  25. function Cipher () {
  26. CipherBase.call(this, 'finalName')
  27. this._cache = []
  28. }
  29. Cipher.prototype._update = function (input) {
  30. t.ok(Buffer.isBuffer(input))
  31. this._cache.push(input)
  32. }
  33. Cipher.prototype._final = function () {
  34. return Buffer.concat(this._cache)
  35. }
  36. var cipher = new Cipher()
  37. var utf8 = 'abc123abcd'
  38. var update = cipher.update(utf8, 'utf8').finalName('base64')
  39. var string = (new Buffer(update, 'base64')).toString()
  40. t.equals(utf8, string)
  41. t.end()
  42. })
  43. test('hash mode as stream', function (t) {
  44. inherits(Cipher, CipherBase)
  45. function Cipher () {
  46. CipherBase.call(this, 'finalName')
  47. this._cache = []
  48. }
  49. Cipher.prototype._update = function (input) {
  50. t.ok(Buffer.isBuffer(input))
  51. this._cache.push(input)
  52. }
  53. Cipher.prototype._final = function () {
  54. return Buffer.concat(this._cache)
  55. }
  56. var cipher = new Cipher()
  57. cipher.on('error', function (e) {
  58. t.notOk(e)
  59. })
  60. var utf8 = 'abc123abcd'
  61. cipher.end(utf8, 'utf8')
  62. var update = cipher.read().toString('base64')
  63. var string = (new Buffer(update, 'base64')).toString()
  64. t.equals(utf8, string)
  65. t.end()
  66. })
  67. test('encodings', function (t) {
  68. inherits(Cipher, CipherBase)
  69. function Cipher () {
  70. CipherBase.call(this)
  71. }
  72. Cipher.prototype._update = function (input) {
  73. return input
  74. }
  75. Cipher.prototype._final = function () {
  76. // noop
  77. }
  78. t.test('mix and match encoding', function (t) {
  79. t.plan(2)
  80. var cipher = new Cipher()
  81. cipher.update('foo', 'utf8', 'utf8')
  82. t.throws(function () {
  83. cipher.update('foo', 'utf8', 'base64')
  84. })
  85. cipher = new Cipher()
  86. cipher.update('foo', 'utf8', 'base64')
  87. t.doesNotThrow(function () {
  88. cipher.update('foo', 'utf8')
  89. cipher.final('base64')
  90. })
  91. })
  92. t.test('handle long uft8 plaintexts', function (t) {
  93. t.plan(1)
  94. var txt = 'ふっかつ あきる すぶり はやい つける まゆげ たんさん みんぞく ねほりはほり せまい たいまつばな ひはん'
  95. var cipher = new Cipher()
  96. var decipher = new Cipher()
  97. var enc = decipher.update(cipher.update(txt, 'utf8', 'base64'), 'base64', 'utf8')
  98. enc += decipher.update(cipher.final('base64'), 'base64', 'utf8')
  99. enc += decipher.final('utf8')
  100. t.equals(txt, enc)
  101. })
  102. })