-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.test.js
56 lines (49 loc) · 2.37 KB
/
cipher.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
const C = require('./cipher.min.js');
describe("Basic tests", () => {
let ktc = null;
beforeAll(() => {
ktc = new C("secret");
})
test('encrypted message is JHQSU XFXBQ', () => {
expect(ktc.encrypt("cryptology"))
.toBe("JHQSU XFXBQ ")
})
test('decrypted message is CRYPT OLOGY', () => {
expect(ktc.decrypt("JHQSU XFXBQ "))
.toBe("CRYPT OLOGY ")
})
})
describe("Expanded tests", () => {
let ktc = null
let key = ""
let encryptedMessage = ""
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()_-+=[{}]\\|;:',<.>/?" + '"'
const props = {
alpha: alphabet,
toUpper: false,
stripCharacters: false,
addSpaces: false,
stripSpaces: false
}
const expectedEncryption = "hBFJNRVZdjnrx159@^)=]:.\"iCGKOSWaekouy26`#&_[\\'> sDHLPTXbflpvz37~$*-{|,/ tAEIMQUYcgmqw048!%(+};"
// The decrypted alphabet contains the character `v` in place of the character `*`
// This is because the shape of the alphabet "grid" is such that it requires two blank spaces
// when decrypting, these blank spaces always resolve to the first found blank character. In the case
// of this test suite it maps to the character `v`.
// This is a known limitation of the basic cipher and will be fixed in an upcoming version.
const decrypted_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&v()_-+=[{}]\\|;:',<.>/?" + '"'
beforeAll(() => {
key = "this"
ktc = new C(key, props)
encryptedMessage = ktc.encrypt(alphabet)
})
test('Props are set to expected value', () => expect(ktc.props).toBe(props))
test('Key equals expected value', () => expect(ktc.key).toBe(key))
test('Cipher is set to an array', () => expect(ktc.cipher).toEqual(["h","i","s","t"]))
test('Cipher width is correct', () => expect(ktc.width).toBe(4))
test('Cipher height is correct', () => expect(ktc.height).toBe(24))
test('Alpha length is correct', () => expect(ktc.alpha.length).toBe(94))
test('Distinct works: AABBCC == ABC', () => expect(ktc._distinct("AABBCC")).toBe("ABC"))
test('Encryption works', () => expect(encryptedMessage).toBe(expectedEncryption))
test('Decrypted alphabet returns expected string', () => expect(ktc.decrypt(expectedEncryption)).toBe(decrypted_alphabet))
})