forked from richardschneider/secure-string
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecure-string.spec.js
81 lines (72 loc) · 2.29 KB
/
secure-string.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const expect = chai.expect
chai.use(require('dirty-chai'))
const SecureString = require('../src/secure-string')
describe('SecureString', () => {
it('created with new', () => {
const password = new SecureString()
expect(password).to.be.instanceOf(SecureString)
})
it('plain text is a buffer', () => {
const password = new SecureString()
password.value(plainText => {
expect(plainText).to.exist();
expect(plainText).to.be.instanceOf(Buffer)
expect(plainText.toString()).to.equal('')
})
})
it('plain text is a UTF-8 encoded buffer', () => {
const password = new SecureString()
password.appendCodePoint(0x41)
password.appendCodePoint(0x24B62)
password.appendCodePoint(0x42)
password.value(plainText => {
expect(plainText).to.exist();
expect(plainText).to.be.instanceOf(Buffer)
const expected = Buffer.from('41f0a4ada242', 'hex')
expect(plainText).to.deep.equal(expected)
})
})
it('clears plain text after making the call', () => {
const password = new SecureString()
password.appendCodePoint(0x41)
let plain
password.value(plainText => {
plain = plainText
expect(plainText.toString()).to.equal('A')
})
expect(plain.toString()).to.not.equal('A')
})
it('can append non-ASCII characters', () => {
const password = new SecureString()
password.appendCodePoint(0x41)
password.appendCodePoint(0x24B62)
password.appendCodePoint(0x42)
password.value(plainText => {
expect(plainText.toString()).to.equal('A\u{24b62}B')
})
})
it('can accept long strings', () => {
const password = new SecureString()
const n = 1024
for (let i = 0; i < n; ++i) {
password.appendCodePoint(0x41)
}
password.value(plainText => {
expect(plainText.toString()).to.equal(Array(n+1).join('A'))
})
})
it('can be cleared', () => {
const password = new SecureString()
password.appendCodePoint(0x41)
password.value(plainText => {
expect(plainText.toString()).to.equal('A')
})
password.clear()
password.value(plainText => {
expect(plainText.toString()).to.equal('')
})
})
})