-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAES.spec.ts
147 lines (132 loc) · 4.63 KB
/
AES.spec.ts
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
import { AESClient } from "../src/AESClient";
describe("AES key initialization: init()", () => {
it("Should generate a key", async () => {
expect.assertions(1);
const AES = new AESClient();
await expect(AES.init()).resolves.not.toThrow();
});
});
describe("AES key export: exportKey()", () => {
it("Should export key", async () => {
expect.assertions(2);
const AES = new AESClient();
await expect(AES.init()).resolves.not.toThrow();
await expect(AES.exportKey()).resolves.not.toThrow();
});
it("Should not export key", async () => {
expect.assertions(1);
const AES = new AESClient();
await expect(AES.exportKey()).rejects.toEqual(
"Error while exporting: No key found."
);
});
it("Should export key of 32 Byets", async () => {
expect.assertions(3);
const AES = new AESClient();
await expect(AES.init()).resolves.not.toThrow();
const exportedKey = await AES.exportKey();
expect(exportedKey).toBeTruthy();
expect(exportedKey.byteLength === 32).toBe(true);
});
});
describe("AES key import: importBufferKey()", () => {
it("Should import key", async () => {
expect.assertions(3);
const AES = new AESClient();
await expect(AES.init()).resolves.not.toThrow();
const exportedKey = await AES.exportKey();
expect(exportedKey).toBeTruthy();
await expect(AES.importBufferKey(exportedKey)).resolves.not.toThrow();
});
it("Should not import key", async () => {
expect.assertions(3);
const AES = new AESClient();
await expect(AES.init()).resolves.not.toThrow();
const exportedKey = await AES.exportKey();
expect(exportedKey).toBeTruthy();
await expect(
AES.importBufferKey(new Uint8Array([0x01, 0x00, 0x01]))
).rejects.toThrow();
});
});
describe("AES Encrytion: encrypt()", () => {
it("should encrypt", async () => {
const message = "Secret Message";
const AESClient1 = new AESClient();
await expect(AESClient1.init()).resolves.not.toThrow();
expect(AESClient1.encrypt(Buffer.from(message))).resolves.not.toThrow();
});
it("should encrypt and message should be encrypted", async () => {
const message = "Secret Message";
const AESClient1 = new AESClient();
await expect(AESClient1.init()).resolves.not.toThrow();
const { data: encryptedMessage } = await AESClient1.encrypt(
Buffer.from(message)
);
expect(AESClient1.arrayBufferToString(encryptedMessage)).not.toBe(message);
});
it("should encrypt and return length 12 of init ventor", async () => {
const message = "Secret Message";
const AESClient1 = new AESClient();
await expect(AESClient1.init()).resolves.not.toThrow();
const { iv } = await AESClient1.encrypt(Buffer.from(message));
expect(iv.length).toBe(12);
});
it("should not encrypt", async () => {
const message = "Secret Message";
const AESClient1 = new AESClient();
expect(AESClient1.encrypt(Buffer.from(message))).rejects.toEqual(
"Error while encrypting: No key found."
);
});
});
describe("AES Decryption: decrypt()", () => {
it("should not decrypt", async () => {
const message = "Secret Message";
const AESClient1 = new AESClient();
expect(
AESClient1.decrypt(Buffer.from(message), new Uint8Array(12))
).rejects.toEqual("Error while decrypting: No key found.");
});
});
describe("AES Encryption Encryption intergration", () => {
it("should encrypt and decrypt a message", async () => {
const message = "Secret Message";
const AESClient1 = new AESClient();
await expect(AESClient1.init()).resolves.not.toThrow();
const exportedKey = await AESClient1.exportKey();
expect(exportedKey).toBeTruthy();
const { data: encryptedMessage, iv } = await AESClient1.encrypt(
Buffer.from(message)
);
expect(encryptedMessage).toBeTruthy();
const AESClient2 = new AESClient();
await expect(
AESClient2.importBufferKey(exportedKey)
).resolves.not.toThrow();
const decryptedData = await AESClient2.decrypt(encryptedMessage, iv);
expect(decryptedData).toBeTruthy();
expect(AESClient2.arrayBufferToString(decryptedData)).toBe(
"Secret Message"
);
});
it("should encrypt, but not decrypt a message", async () => {
const message = "Secret Message";
const AESClient1 = new AESClient();
await expect(AESClient1.init()).resolves.not.toThrow();
const exportedKey = await AESClient1.exportKey();
expect(exportedKey).toBeTruthy();
const { data: encryptedMessage, iv } = await AESClient1.encrypt(
Buffer.from(message)
);
expect(iv).toBeTruthy();
expect(encryptedMessage).toBeTruthy();
const AESClient2 = new AESClient();
await expect(
AESClient2.importBufferKey(exportedKey)
).resolves.not.toThrow();
expect(
AESClient2.decrypt(encryptedMessage, new Uint8Array(12)) // passing wrong init vector
).rejects.toThrow();
});
});