-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
215 lines (155 loc) · 6.37 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import crypto from "crypto";
import {
decrypt,
encrypt,
encryptIterations,
} from "./index";
test("encryption manifest", async () => {
let password, text, encrypted, tokens;
password = "Check under the couch cushion.";
text = "I found my purpose.";
encrypted = await encrypt(password, text);
tokens = encrypted.split(":");
expect(encrypted).toBeTruthy();
expect(tokens[0]).toBe("1"); // version 1
expect(tokens[1].split(",").length).toBe(3); // 3 parameters
expect(tokens[2]).toBeTruthy(); // with payload
});
test("readme example", async () => {
let password, text, encrypted, decrypted;
password = "Check under the couch cushion.";
text = "I found my purpose.";
encrypted = await encrypt(password, text);
decrypted = await decrypt(password, encrypted); // I found my purpose.
expect(decrypted).toBe(text);
});
test("encrypt a simple string", async () => {
let password, text, encrypted, decrypted;
password = "Le cœur déçu mais l'âme plutôt naïve";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
encrypted = await encrypt(password, text);
decrypted = await decrypt(password, encrypted);
expect(decrypted).toBe(text);
});
test("encrypt an XML document", async () => {
let password, text, encrypted, decrypted;
password = "Pchnąć w tę łódź jeża lub ośm skrzyń fig";
text = `
<?xml version="1.0" encoding="UTF-8"?>
<one>
<two attr="2"/>
<three>
<![CDATA[
All the statistical manipulation is performed. Example. '"&<> and submission
]]>
</three>
</one>`;
encrypted = await encrypt(password, text);
decrypted = await decrypt(password, encrypted);
expect(decrypted).toBe(text);
});
test("encrypt a long string", async () => {
let password, text, encrypted, decrypted;
password = "Pchnąć w tę łódź jeża lub ośm skrzyń fig";
text = crypto.randomBytes(1_000_000).toString();
encrypted = await encrypt(password, text);
decrypted = await decrypt(password, encrypted);
expect(decrypted).toBe(text);
});
test("decrypt with the wrong password", async () => {
let password, text, encrypted;
password = "Le cœur déçu mais l'âme plutôt naïve";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
encrypted = await encrypt(password, text);
await expect(() => decrypt("nope", encrypted)).rejects.toThrow();
});
test("decrypt with tampered encrypted text", async () => {
let password, text, encrypted, tampered;
password = "Le cœur déçu mais l'âme plutôt naïve";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
encrypted = await encrypt(password, text);
tampered = [
encrypted.slice(0, encrypted.length - 10),
"0",
encrypted.slice(encrypted.length - 11)
].join();
await expect(() => decrypt(password, tampered)).rejects.toThrow();
});
test("uniqueness", async () => {
let password, text, encrypted1, encrypted2, payload1, payload2;
password = "Le cœur déçu mais l'âme plutôt naïve";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
encrypted1 = await encrypt(password, text);
encrypted2 = await encrypt(password, text);
payload1 = encrypted1.slice(encrypted1.lastIndexOf(":"));
payload2 = encrypted2.slice(encrypted2.lastIndexOf(":"));
expect(payload1).not.toBe(payload2);
});
test("decrypt with an unsupported version", async () => {
let password, text, encrypted;
password = "Le cœur déçu mais l'âme plutôt naïve";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
encrypted = await encrypt(password, text);
encrypted = [
"99",
encrypted.slice(encrypted.indexOf(":")),
].join();
await expect(() => decrypt(password, encrypted)).rejects.toThrow();
});
test("encrypt with fewer iterations", async () => {
let password, text, encrypted, decrypted;
password = "Le cœur déçu mais l'âme plutôt naïve";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
encrypted = await encryptIterations(password, 1, text);
decrypted = await decrypt(password, encrypted);
expect(decrypted).toBe(text);
});
test("encrypt with more iterations", async () => {
let password, text, encrypted, decrypted;
password = "Le cœur déçu mais l'âme plutôt naïve";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
encrypted = await encryptIterations(password, 100_000, text);
decrypted = await decrypt(password, encrypted);
expect(decrypted).toBe(text);
});
test("encrypt with invalid passwords", async () => {
let password1, password2, text;
password1 = null;
password2 = "";
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
await expect(() => encrypt(password1, text)).rejects.toThrow(TypeError);
await expect(() => encrypt(password2, text)).rejects.toThrow(TypeError);
});
test("encrypt with invalid iterations", async () => {
let password, iterations1, iterations2, text;
password = "Le cœur déçu mais l'âme plutôt naïve";
iterations1 = null;
iterations2 = 0;
text = "Portez ce vieux whisky au juge blond qui fume sur son île intérieure";
await expect(() => encryptIterations(password, iterations1, text)).rejects.toThrow(TypeError);
await expect(() => encryptIterations(password, iterations2, text)).rejects.toThrow(TypeError);
});
test("encrypt with invalid texts", async () => {
let password, text1, text2;
password = "Le cœur déçu mais l'âme plutôt naïve";
text1 = null;
text2 = "";
await expect(() => encrypt(password, text1)).rejects.toThrow(TypeError);
await expect(() => encrypt(password, text2)).rejects.toThrow(TypeError);
});
test("decrypt with invalid passwords", async () => {
let password1, password2, encrypted;
password1 = null;
password2 = "";
encrypted = "1234567890";
await expect(() => decrypt(password1, encrypted)).rejects.toThrow(TypeError);
await expect(() => encrypt(password2, encrypted)).rejects.toThrow(TypeError);
});
test("decrypt with invalid encrypted text", async () => {
let password, encrypted1, encrypted2;
password = "Le cœur déçu mais l'âme plutôt naïve";
encrypted1 = null;
encrypted2 = "";
await expect(() => decrypt(password, encrypted1)).rejects.toThrow(TypeError);
await expect(() => encrypt(password, encrypted2)).rejects.toThrow(TypeError);
});