test.js 3.0 KB

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