Projekt

Obecné

Profil

Stáhnout (2.95 KB) Statistiky
| Větev: | Revize:
1
var Buffer = require('safe-buffer').Buffer
2
var CipherBase = require('./')
3

    
4
var test = require('tape')
5
var inherits = require('inherits')
6

    
7
test('basic version', function (t) {
8
  function Cipher () {
9
    CipherBase.call(this)
10
  }
11
  inherits(Cipher, CipherBase)
12
  Cipher.prototype._update = function (input) {
13
    t.ok(Buffer.isBuffer(input))
14
    return input
15
  }
16
  Cipher.prototype._final = function () {
17
    // noop
18
  }
19
  var cipher = new Cipher()
20
  var utf8 = 'abc123abcd'
21
  var update = cipher.update(utf8, 'utf8', 'base64') + cipher.final('base64')
22
  var string = (Buffer.from(update, 'base64')).toString()
23
  t.equals(utf8, string)
24
  t.end()
25
})
26
test('hash mode', function (t) {
27
  function Cipher () {
28
    CipherBase.call(this, 'finalName')
29
    this._cache = []
30
  }
31
  inherits(Cipher, CipherBase)
32
  Cipher.prototype._update = function (input) {
33
    t.ok(Buffer.isBuffer(input))
34
    this._cache.push(input)
35
  }
36
  Cipher.prototype._final = function () {
37
    return Buffer.concat(this._cache)
38
  }
39
  var cipher = new Cipher()
40
  var utf8 = 'abc123abcd'
41
  var update = cipher.update(utf8, 'utf8').finalName('base64')
42
  var string = (Buffer.from(update, 'base64')).toString()
43

    
44
  t.equals(utf8, string)
45
  t.end()
46
})
47
test('hash mode as stream', function (t) {
48
  function Cipher () {
49
    CipherBase.call(this, 'finalName')
50
    this._cache = []
51
  }
52
  inherits(Cipher, CipherBase)
53
  Cipher.prototype._update = function (input) {
54
    t.ok(Buffer.isBuffer(input))
55
    this._cache.push(input)
56
  }
57
  Cipher.prototype._final = function () {
58
    return Buffer.concat(this._cache)
59
  }
60
  var cipher = new Cipher()
61
  cipher.on('error', function (e) {
62
    t.notOk(e)
63
  })
64
  var utf8 = 'abc123abcd'
65
  cipher.end(utf8, 'utf8')
66
  var update = cipher.read().toString('base64')
67
  var string = (Buffer.from(update, 'base64')).toString()
68

    
69
  t.equals(utf8, string)
70
  t.end()
71
})
72

    
73
test('encodings', function (t) {
74
  inherits(Cipher, CipherBase)
75
  function Cipher () {
76
    CipherBase.call(this)
77
  }
78
  Cipher.prototype._update = function (input) {
79
    return input
80
  }
81
  Cipher.prototype._final = function () {
82
    // noop
83
  }
84
  t.test('mix and match encoding', function (t) {
85
    t.plan(2)
86

    
87
    var cipher = new Cipher()
88
    cipher.update('foo', 'utf8', 'utf8')
89
    t.throws(function () {
90
      cipher.update('foo', 'utf8', 'base64')
91
    })
92
    cipher = new Cipher()
93
    cipher.update('foo', 'utf8', 'base64')
94
    t.doesNotThrow(function () {
95
      cipher.update('foo', 'utf8')
96
      cipher.final('base64')
97
    })
98
  })
99
  t.test('handle long uft8 plaintexts', function (t) {
100
    t.plan(1)
101
    var txt = 'ふっかつ あきる すぶり はやい つける まゆげ たんさん みんぞく ねほりはほり せまい たいまつばな ひはん'
102

    
103
    var cipher = new Cipher()
104
    var decipher = new Cipher()
105
    var enc = decipher.update(cipher.update(txt, 'utf8', 'base64'), 'base64', 'utf8')
106
    enc += decipher.update(cipher.final('base64'), 'base64', 'utf8')
107
    enc += decipher.final('utf8')
108

    
109
    t.equals(txt, enc)
110
  })
111
})
(7-7/7)