Asper Header  1.0.22
The header injector extension
Loading...
Searching...
No Matches
ciphers.test.ts
Go to the documentation of this file.
1
15import * as assert from 'assert';
16import { Cipher, ALLCiphers } from '../modules/ciphers';
17
18suite('Ciphers Hardening', () => {
19 test('should expose at least 30 ciphers', () => {
20 const c = new Cipher();
21 const list = c.listCiphers();
22 assert.ok(list.length >= 30, `Expected >=30 ciphers, got ${list.length}: ${list.join(', ')}`);
23 });
24
25 test('every flavoured cipher should have CipherName and encode/decode as functions', () => {
26 const instances = Object.values(ALLCiphers).filter(v => typeof v === 'function' && v !== (ALLCiphers as any).Cipher);
27 for (const Cls of instances as any[]) {
28 const inst = new Cls();
29 assert.ok(typeof inst.CipherName === 'string' && inst.CipherName.length > 0, `${Cls.name} missing CipherName`);
30 assert.strictEqual(typeof inst.encode, 'function', `${Cls.name}.encode should be function`);
31 assert.strictEqual(typeof inst.decode, 'function', `${Cls.name}.decode should be function`);
32 }
33 });
34
35 test('Cipher registry should round-trip for keyless ciphers (Caesar, ROT13, Atbash, Base64)', () => {
36 const c = new Cipher();
37 const plain = 'HELLO WORLD';
38 // Caesar (default shift 3) -> encode -> decode should return uppercased plain (BaseCipher sanitizes)
39 for (const name of ['caesar', 'rot13', 'atbash', 'base64']) {
40 const enc = c.encode(plain, undefined, name);
41 assert.strictEqual(typeof enc, 'string', `${name} encode should return string`);
42 assert.ok(enc.length > 0, `${name} encode should not be empty`);
43 const dec = c.decode(enc, undefined, name);
44 assert.strictEqual(typeof dec, 'string', `${name} decode should return string`);
45 // For these ciphers, decode(encode) is either original uppercased or for base64 exact
46 if (name === 'base64') {
47 assert.strictEqual(dec, plain, 'Base64 round-trip should be exact');
48 } else {
49 // sanitized to A-Z, spaces preserved by Caesar/Atbash as-is per implementation, but we check contains HELLO
50 assert.ok(dec.includes('HELLO'), `${name} round-trip should contain HELLO, got ${dec}`);
51 }
52 }
53 });
54
55 test('should throw on unknown cipher and on missing name', () => {
56 const c = new Cipher();
57 assert.throws(() => c.encode('hi', undefined, undefined as any), /Cipher name required/);
58 assert.throws(() => c.encode('hi', undefined, 'nonexistent'), /not found/);
59 assert.throws(() => c.decode('hi', undefined, undefined as any), /Cipher name required/);
60 assert.throws(() => c.decode('hi', undefined, 'nonexistent'), /not found/);
61 });
62
63 test('listCiphers keys should be normalized lower-case without spaces', () => {
64 const c = new Cipher();
65 const list = c.listCiphers();
66 for (const k of list) {
67 assert.strictEqual(k, k.toLowerCase(), `${k} should be lower-case`);
68 assert.ok(!k.includes(' '), `${k} should not contain spaces`);
69 }
70 });
71
72 test('encode/decode should handle empty string without throwing (all ciphers)', () => {
73 const c = new Cipher();
74 const list = c.listCiphers();
75 // Keys that satisfy different cipher families: alphabetic (vigenere), numeric (gronsfeld), generic
76 const candidateKeys: (string | undefined)[] = [undefined, 'KEY', '123', 'TESTKEY', 'A'];
77 for (const name of list) {
78 let encodeOk = false;
79 let lastEncodeError: any = null;
80 for (const k of candidateKeys) {
81 try {
82 c.encode('', k as any, name);
83 encodeOk = true;
84 break;
85 } catch (e) {
86 lastEncodeError = e;
87 // Try next candidate; many ciphers throw on undefined/missing key which is expected for this smoke test
88 }
89 }
90 assert.ok(encodeOk, `${name} encode('') should handle empty string with some valid key, last error: ${lastEncodeError}`);
91
92 let decodeOk = false;
93 let lastDecodeError: any = null;
94 for (const k of candidateKeys) {
95 try {
96 c.decode('', k as any, name);
97 decodeOk = true;
98 break;
99 } catch (e) {
100 lastDecodeError = e;
101 }
102 }
103 assert.ok(decodeOk, `${name} decode('') should handle empty string with some valid key, last error: ${lastDecodeError}`);
104 }
105 });
106});
import *as assert from assert
export const ALLCiphers
Comprehensive object containing all cipher classes and utilities.
Definition ciphers.ts:158
Central cipher management class providing unified access to all cipher implementations.
Definition ciphers.ts:33
ALLCiphers from ciphers
Definition index.ts:28
Cipher
Definition index.ts:28