-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
83 lines (64 loc) · 2 KB
/
test.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
82
83
const test = require('tape')
const { encrypt, decrypt } = require('./index.js')
const secret = 'mysecretpassword'
const data = 'sensitive data to encrypt'
test('Encrypts and decrypts data', t => {
t.plan(1)
const encrypted = encrypt(data, secret)
const decrypted = decrypt(encrypted, secret)
t.equal(decrypted, data)
})
test('Encryption generates a different value each time', t => {
t.plan(1)
const encrypted1 = encrypt(data, secret)
const encrypted2 = encrypt(data, secret)
t.notEqual(encrypted1, encrypted2)
})
test('Encrypted data is a hex string', t => {
t.plan(1)
t.match(encrypt(data, secret), /^[0-9a-f]+$/)
})
test('Decrypts data encrypted with Dart', t => {
t.plan(1)
const encrypted = '0c8f8023a48d7684dc6f662a4e33d85fbcc9b5ab168e2d9c3bd4ce4e72a6eb8ea75d833e1ce7f0a0f5f24c234f6aaec9359c48577fcc8e82'
t.equal(decrypt(encrypted, 'password'), 'Hello World!')
})
test('Throws an error if secret is not a string', t => {
t.plan(4)
t.throws(
() => encrypt(data),
{
code: 'ERR_INVALID_ARG_TYPE',
}
)
t.throws(
() => encrypt(data, null),
{
code: 'ERR_INVALID_ARG_TYPE',
}
)
t.throws(
() => encrypt(data, 0),
{
code: 'ERR_INVALID_ARG_TYPE',
}
)
t.throws(
() => encrypt(data, {}),
{
code: 'ERR_INVALID_ARG_TYPE',
}
)
})
test('Throws an error if encryption value is not a string', t => {
t.plan(3)
t.throws(() => encrypt({}, secret), /Encryption error: data must be a string/)
t.throws(() => encrypt(undefined, secret), /Encryption error: data must be a string/)
t.throws(() => encrypt(null, secret), /Encryption error: data must be a string/)
})
test('Throws an error if decryption value is not a string', t => {
t.plan(3)
t.throws(() => decrypt({}, secret), /Encryption error: cipher text must be a string/)
t.throws(() => decrypt(undefined, secret), /Encryption error: cipher text must be a string/)
t.throws(() => decrypt(null, secret), /Encryption error: cipher text must be a string/)
})