forked from WICG/webpackage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigned-web-bundle_test.js
More file actions
129 lines (115 loc) · 4.62 KB
/
Copy pathsigned-web-bundle_test.js
File metadata and controls
129 lines (115 loc) · 4.62 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
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
// @ts-check
/** @typedef {import('jasmine')} _ */
import fs from 'fs';
import path from 'path';
import url from 'url';
import {
getRawPublicKey,
NodeCryptoSigningStrategy,
parsePemKey,
SignedWebBundle,
} from '../lib/wbn-sign.js';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const TEST_ED25519_PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIB8nP5PpWU7HiILHSfh5PYzb5GAcIfHZ+bw6tcd/LZXh
-----END PRIVATE KEY-----`;
const TEST_ED25519_PRIVATE_KEY_2 = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIKxzyXkSRaUIc6fpI+TYecPQjo4YJTSFPulQY/0lGjs1
-----END PRIVATE KEY-----`;
const TEST_ECDSA_P256_WEB_BUNDLE_ID =
'amoiebz32b7o24tilu257xne2yf3nkblkploanxzm7ebeglseqpfeaacai';
const TEST_ED25519_WEB_BUNDLE_ID_1 =
'4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic';
const [STRATEGY_KEY_1, STRATEGY_KEY_2] = [
TEST_ED25519_PRIVATE_KEY,
TEST_ED25519_PRIVATE_KEY_2,
].map((key) => new NodeCryptoSigningStrategy(parsePemKey(key)));
const UNSIGNED_BUNDLE_PATH = path.resolve(__dirname, 'testdata/unsigned.wbn');
const UNSIGNED_WEB_BUNDLE_BYTES = Uint8Array.from(
fs.readFileSync(UNSIGNED_BUNDLE_PATH)
);
describe('Signed Web Bundle - ', function () {
it('fromWebBundle() - bundle id got from first key by default', async function () {
const double_signed_bundle = await SignedWebBundle.fromWebBundle(
UNSIGNED_WEB_BUNDLE_BYTES,
[STRATEGY_KEY_1, STRATEGY_KEY_2]
);
expect(double_signed_bundle.getWebBundleId()).toEqual(
TEST_ED25519_WEB_BUNDLE_ID_1
);
});
it('fromWebBundle() - bundle id successfully overridden', async function () {
const double_signed_bundle = await SignedWebBundle.fromWebBundle(
UNSIGNED_WEB_BUNDLE_BYTES,
[STRATEGY_KEY_1, STRATEGY_KEY_2],
{ webBundleId: TEST_ECDSA_P256_WEB_BUNDLE_ID }
);
expect(double_signed_bundle.getWebBundleId()).toEqual(
TEST_ECDSA_P256_WEB_BUNDLE_ID
);
});
it('addSignature() - the same result as signing with two keys', async function () {
// I'm using only ed25519 keys on purpose: Ecdsa signing algorithm is not deterministic and adds a random nonce
// so I could not just simply verify if two integrity blocks are same after different sequence of operations
const bundle_signed_by_first_key_then_other = await (
await SignedWebBundle.fromWebBundle(UNSIGNED_WEB_BUNDLE_BYTES, [
STRATEGY_KEY_1,
])
).addSignature(STRATEGY_KEY_2);
const bundle_signed_by_two_keys_at_once =
await SignedWebBundle.fromWebBundle(UNSIGNED_WEB_BUNDLE_BYTES, [
STRATEGY_KEY_1,
STRATEGY_KEY_2,
]);
const bundle_signed_by_second_key_then_other = await (
await SignedWebBundle.fromWebBundle(UNSIGNED_WEB_BUNDLE_BYTES, [
STRATEGY_KEY_2,
])
).addSignature(STRATEGY_KEY_1);
const bundle_signed_by_two_keys_at_once_reversed_order =
await SignedWebBundle.fromWebBundle(UNSIGNED_WEB_BUNDLE_BYTES, [
STRATEGY_KEY_2,
STRATEGY_KEY_1,
]);
expect(bundle_signed_by_first_key_then_other).toEqual(
bundle_signed_by_two_keys_at_once
);
expect(bundle_signed_by_second_key_then_other).toEqual(
bundle_signed_by_two_keys_at_once_reversed_order
);
expect(bundle_signed_by_first_key_then_other).not.toEqual(
bundle_signed_by_second_key_then_other
);
});
it('removeSignature() - bundle signed with key A and B + removing A = bundle singed with key B', async function () {
const bundle_signed_by_second_key = await SignedWebBundle.fromWebBundle(
UNSIGNED_WEB_BUNDLE_BYTES,
[STRATEGY_KEY_2],
{ webBundleId: TEST_ED25519_WEB_BUNDLE_ID_1 }
);
const bundle_signed_by_two_keys_second_removed = (
await SignedWebBundle.fromWebBundle(UNSIGNED_WEB_BUNDLE_BYTES, [
STRATEGY_KEY_1,
STRATEGY_KEY_2,
])
).removeSignature(getRawPublicKey(await STRATEGY_KEY_1.getPublicKey()));
expect(bundle_signed_by_two_keys_second_removed).toEqual(
bundle_signed_by_second_key
);
});
it('validateSignatures() - correctly validates signatures and returns bundle ID', async function () {
const double_signed_bundle = await SignedWebBundle.fromWebBundle(
UNSIGNED_WEB_BUNDLE_BYTES,
[STRATEGY_KEY_1, STRATEGY_KEY_2]
);
const validations = double_signed_bundle.validateSignatures();
expect(validations.length).toEqual(2);
expect(validations[0].status).toEqual('success');
expect(validations[0].isValid).toBe(true);
expect(validations[0].derivedBundleId).toEqual(
TEST_ED25519_WEB_BUNDLE_ID_1
);
expect(validations[1].status).toEqual('success');
expect(validations[1].isValid).toBe(true);
});
});