generated from BenEdridge/node-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
149 lines (118 loc) · 5.55 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
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
const { OneTimePad } = require('../index.js');
const assert = require('assert');
const fs = require('fs');
(function run() {
generatedTestsSmallSize();
randomTests1();
edgeCases();
test2();
test3();
text_test();
textFileEncryption();
pngFileEncryption();
}());
function generatedTestsSmallSize() {
for (let i = 0; i <= 2000; i++) {
// Randomly sized message and pad
const size = Math.floor(Math.random() * 2000);
let plainTextBuffer = new Uint8Array(size);
// Populate message buffer with random bytes
for (let j = 0; j < size; j++) {
let randomByte = Math.floor(1 + Math.random() * 255);
assert.ok(randomByte <= 255 && randomByte > 0, `FAILED: \nRandom Byte was: ${randomByte}`);
plainTextBuffer[j] = randomByte;
}
const pad = OneTimePad.generatePad(plainTextBuffer);
const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
const decryptedData = OneTimePad.decrypt(pad, encryptedData);
assert.deepStrictEqual(
plainTextBuffer, decryptedData,
`Plain Text Buffer: ${Buffer.from(plainTextBuffer).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
console.log(`Test ${i} passed with pad of length: ${size} encrypted length: ${encryptedData.length} and decryptedData length: ${decryptedData.length}`);
}
}
function randomTests1() {
for (let i = 0; i <= 10; i++) {
// Randomly sized message and pad
const size = Math.floor(Math.random() * 100000);
let plainTextBuffer = new Uint8Array(size);
// Populate message buffer with random bytes
for (let j = 0; j < size; j++) {
let randomByte = Math.floor(1 + Math.random() * 255);
assert.ok(randomByte <= 255 && randomByte > 0, `FAILED: \nRandom Byte was: ${randomByte}`);
plainTextBuffer[j] = randomByte;
}
const pad = OneTimePad.generatePad(plainTextBuffer);
const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
const decryptedData = OneTimePad.decrypt(pad, encryptedData);
assert.deepStrictEqual(
plainTextBuffer, decryptedData,
`Plain Text Buffer: ${Buffer.from(plainTextBuffer).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
console.log(`Test ${i} passed with pad of length: ${size} encrypted length: ${encryptedData.length} and decryptedData length: ${decryptedData.length}`);
}
}
function edgeCases() {
let plainTextBuffer = Uint8Array.from([0, 255, 0, 255, 1, 0, 0, 255, 255, 1, 1, 1, 255, 255, 255]);
const pad = OneTimePad.generatePad(plainTextBuffer);
const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
const decryptedData = OneTimePad.decrypt(pad, encryptedData);
assert.deepStrictEqual(
plainTextBuffer, decryptedData,
`Plain Text Buffer: ${Buffer.from(plainTextBuffer).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
}
function test2() {
let plainTextBuffer = Uint8Array.from([0]);
const pad = OneTimePad.generatePad(plainTextBuffer);
const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
const decryptedData = OneTimePad.decrypt(pad, encryptedData);
assert.deepStrictEqual(
plainTextBuffer, decryptedData,
`Plain Text Buffer: ${Buffer.from(plainTextBuffer).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
}
function test3() {
let plainTextBuffer = Uint8Array.from([255]);
const pad = OneTimePad.generatePad(plainTextBuffer);
const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
const decryptedData = OneTimePad.decrypt(pad, encryptedData);
assert.deepStrictEqual(
plainTextBuffer, decryptedData,
`Plain Text Buffer: ${Buffer.from(plainTextBuffer).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
}
function text_test() {
const plainText = 'Hello World!';
const pad = OneTimePad.generatePad(Buffer.from(plainText, 'utf8'));
const encryptedData = OneTimePad.encrypt(pad, Buffer.from(plainText, 'utf8'));
const decryptedData = OneTimePad.decrypt(pad, encryptedData)
assert.deepStrictEqual(
Uint8Array.from(Buffer.from(plainText, 'utf8')), decryptedData,
`Plain Text Buffer: ${Buffer.from(plainText).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
}
function textFileEncryption() {
const plainTextBuffer = new Uint8Array(fs.readFileSync('./tests/file.txt'));
const pad = OneTimePad.generatePad(plainTextBuffer);
const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
const decryptedData = OneTimePad.decrypt(pad, encryptedData)
assert.ok(plainTextBuffer.length > 0, 'Test file should have loaded!')
assert.deepStrictEqual(
plainTextBuffer, decryptedData,
`Plain Text Buffer: ${Buffer.from(plainTextBuffer).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
}
function pngFileEncryption() {
const pngBuffer = new Uint8Array(fs.readFileSync('./tests/test_image.png'));
const pad = OneTimePad.generatePad(pngBuffer);
fs.writeFileSync('./tests/scratch/encrypted.png', OneTimePad.encrypt(pad, pngBuffer));
const encryptedPng = new Uint8Array(fs.readFileSync('./tests/scratch/encrypted.png'));
const decryptedData = OneTimePad.decrypt(pad, encryptedPng);
fs.unlinkSync('./tests/scratch/encrypted.png');
assert.deepStrictEqual(
pngBuffer, decryptedData,
`Plain Text Buffer: ${Buffer.from(pngBuffer).toString('hex')} does not match Decrypted Data: ${Buffer.from(decryptedData).toString('hex')}`
);
}