-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathharness-encryption.test.ts
More file actions
91 lines (73 loc) · 3.16 KB
/
harness-encryption.test.ts
File metadata and controls
91 lines (73 loc) · 3.16 KB
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
import { expect } from 'chai';
import * as sinon from 'sinon';
describe('TestHarness Encryption/Decryption', () => {
beforeEach(() => {
// We need to require these modules after the stubs are in place
});
afterEach(() => {
sinon.restore();
});
describe('XOR encryption implementation', () => {
it('should implement the correct XOR logic as specified in the issue', () => {
const value = 'hello';
const secret = 'key';
// Manual implementation of the expected XOR logic from the issue
let expectedResult = '';
for (let i = 0; i < value.length; ++i) {
expectedResult += String.fromCharCode(secret[i % secret.length].charCodeAt(0) ^ value.charCodeAt(i));
}
// Test the algorithm directly
expect(expectedResult).to.not.equal(value);
// Test that applying XOR twice returns the original value (decryption)
let decryptedResult = '';
for (let i = 0; i < expectedResult.length; ++i) {
decryptedResult += String.fromCharCode(
secret[i % secret.length].charCodeAt(0) ^ expectedResult.charCodeAt(i),
);
}
expect(decryptedResult).to.equal(value);
});
it('should handle empty strings', () => {
const value = '';
const secret = 'key';
let result = '';
for (let i = 0; i < value.length; ++i) {
result += String.fromCharCode(secret[i % secret.length].charCodeAt(0) ^ value.charCodeAt(i));
}
expect(result).to.equal('');
});
it('should handle different secret lengths', () => {
const value = 'testvalue123';
const shortSecret = 'ab';
const longSecret = 'verylongsecretkey';
// Test with short secret
let result1 = '';
for (let i = 0; i < value.length; ++i) {
result1 += String.fromCharCode(shortSecret[i % shortSecret.length].charCodeAt(0) ^ value.charCodeAt(i));
}
// Decrypt back
let decrypted1 = '';
for (let i = 0; i < result1.length; ++i) {
decrypted1 += String.fromCharCode(
shortSecret[i % shortSecret.length].charCodeAt(0) ^ result1.charCodeAt(i),
);
}
expect(decrypted1).to.equal(value);
// Test with long secret
let result2 = '';
for (let i = 0; i < value.length; ++i) {
result2 += String.fromCharCode(longSecret[i % longSecret.length].charCodeAt(0) ^ value.charCodeAt(i));
}
// Decrypt back
let decrypted2 = '';
for (let i = 0; i < result2.length; ++i) {
decrypted2 += String.fromCharCode(
longSecret[i % longSecret.length].charCodeAt(0) ^ result2.charCodeAt(i),
);
}
expect(decrypted2).to.equal(value);
// Results should be different with different secrets
expect(result1).to.not.equal(result2);
});
});
});