|
1 | 1 | import test from 'ava'; |
2 | 2 | import { constantTimeEqual, fromBase64Url, hmacSha256, randomBytes, randomInt } from '../../dist/Common.js'; |
3 | | -test('fromBase64Url converts base64url to Uint8Array', (t) => { |
| 3 | +test('Common: fromBase64Url converts base64url to Uint8Array', (t) => { |
4 | 4 | const base64url = 'SGVsbG8gV29ybGQ'; |
5 | 5 | const result = fromBase64Url(base64url); |
6 | 6 | t.true(result instanceof Uint8Array); |
7 | 7 | t.is(result.length, 11); |
8 | 8 | t.is(String.fromCharCode(...result), 'Hello World'); |
9 | 9 | }); |
10 | | -test('fromBase64Url handles padding correctly', (t) => { |
| 10 | +test('Common: fromBase64Url handles padding correctly', (t) => { |
11 | 11 | const base64url = 'dGVzdA'; |
12 | 12 | const result = fromBase64Url(base64url); |
13 | 13 | t.is(String.fromCharCode(...result), 'test'); |
14 | 14 | }); |
15 | | -test('randomBytes generates correct length', (t) => { |
| 15 | +test('Common: randomBytes generates correct length', (t) => { |
16 | 16 | const bytes = randomBytes(32); |
17 | 17 | t.true(bytes instanceof Uint8Array); |
18 | 18 | t.is(bytes.length, 32); |
19 | 19 | }); |
20 | | -test('randomBytes generates different values', (t) => { |
| 20 | +test('Common: randomBytes generates different values', (t) => { |
21 | 21 | const bytes1 = randomBytes(16); |
22 | 22 | const bytes2 = randomBytes(16); |
23 | 23 | t.false(constantTimeEqual(bytes1, bytes2)); |
24 | 24 | }); |
25 | | -test('randomInt generates number in range', (t) => { |
| 25 | +test('Common: randomInt generates number in range', (t) => { |
26 | 26 | const min = 100; |
27 | 27 | const max = 200; |
28 | 28 | for (let i = 0; i < 100; i++) { |
29 | 29 | const num = randomInt(min, max); |
30 | 30 | t.true(num >= min && num < max); |
31 | 31 | } |
32 | 32 | }); |
33 | | -test('randomInt throws on invalid range', (t) => { |
| 33 | +test('Common: randomInt throws on invalid range', (t) => { |
34 | 34 | t.throws(() => randomInt(100, 100), { message: 'max must be > min' }); |
35 | 35 | t.throws(() => randomInt(200, 100), { message: 'max must be > min' }); |
36 | 36 | }); |
37 | | -test('constantTimeEqual returns true for equal arrays', (t) => { |
| 37 | +test('Common: constantTimeEqual returns true for equal arrays', (t) => { |
38 | 38 | const a = new Uint8Array([1, 2, 3, 4, 5]); |
39 | 39 | const b = new Uint8Array([1, 2, 3, 4, 5]); |
40 | 40 | t.true(constantTimeEqual(a, b)); |
41 | 41 | }); |
42 | | -test('constantTimeEqual returns false for different arrays', (t) => { |
| 42 | +test('Common: constantTimeEqual returns false for different arrays', (t) => { |
43 | 43 | const a = new Uint8Array([1, 2, 3, 4, 5]); |
44 | 44 | const b = new Uint8Array([1, 2, 3, 4, 6]); |
45 | 45 | t.false(constantTimeEqual(a, b)); |
46 | 46 | }); |
47 | | -test('constantTimeEqual returns false for different lengths', (t) => { |
| 47 | +test('Common: constantTimeEqual returns false for different lengths', (t) => { |
48 | 48 | const a = new Uint8Array([1, 2, 3]); |
49 | 49 | const b = new Uint8Array([1, 2, 3, 4]); |
50 | 50 | t.false(constantTimeEqual(a, b)); |
51 | 51 | }); |
52 | | -test('hmacSha256 generates correct signature', async (t) => { |
| 52 | +test('Common: hmacSha256 generates correct signature', async (t) => { |
53 | 53 | const key = new Uint8Array(32); |
54 | 54 | const data = 'test message'; |
55 | 55 | const signature = await hmacSha256(key, data); |
56 | 56 | t.true(signature instanceof Uint8Array); |
57 | 57 | t.is(signature.length, 32); |
58 | 58 | }); |
59 | | -test('hmacSha256 produces consistent signatures', async (t) => { |
| 59 | +test('Common: hmacSha256 produces consistent signatures', async (t) => { |
60 | 60 | const key = new Uint8Array([1, 2, 3, 4]); |
61 | 61 | const data = 'consistent test'; |
62 | 62 | const sig1 = await hmacSha256(key, data); |
63 | 63 | const sig2 = await hmacSha256(key, data); |
64 | 64 | t.true(constantTimeEqual(sig1, sig2)); |
65 | 65 | }); |
66 | | -test('hmacSha256 produces different signatures for different keys', async (t) => { |
| 66 | +test('Common: hmacSha256 produces different signatures for different keys', async (t) => { |
67 | 67 | const key1 = new Uint8Array([1, 2, 3, 4]); |
68 | 68 | const key2 = new Uint8Array([5, 6, 7, 8]); |
69 | 69 | const data = 'test'; |
|
0 commit comments