-
Notifications
You must be signed in to change notification settings - Fork 542
/
validation.test.ts
266 lines (239 loc) · 6.63 KB
/
validation.test.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { assert, is, size, string } from 'superstruct';
import { getSnapManifest } from '../test-utils';
import {
assertIsSnapManifest,
base64,
Base64Opts,
Bip32EntropyStruct,
Bip32PathStruct,
createSnapManifest,
isSnapManifest,
} from './validation';
describe('base64', () => {
it.each([
['abcd', undefined],
['abcd', { paddingRequired: true }],
['ab', undefined],
['ab==', undefined],
['ab==', { paddingRequired: true }],
['abc', undefined],
['abc=', undefined],
['abc=', { paddingRequired: true }],
[
'abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVXYZ01234567890+/',
undefined,
],
[
'abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVXYZ01234567890-_',
{ characterSet: 'base64url' },
] as const,
] as [string, Base64Opts | undefined][])(
'validates valid base64',
(value, opts) => {
const struct = base64(string(), opts);
expect(is(value, struct)).toBe(true);
},
);
it.each([
['ab', { paddingRequired: true }],
['abc', { paddingRequired: true }],
['a', undefined],
['aaaaa', undefined],
[String.raw`\\\\`, undefined],
['ab=', undefined],
['ab=', { paddingRequired: true }],
['abc==', undefined],
['abc==', { paddingRequired: true }],
[',.', undefined],
[',.', { characterSet: 'base64url' }] as const,
[
'abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVXYZ01234567890-_',
undefined,
],
[
'abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVXYZ01234567890+/',
{ characterSet: 'base64url' },
],
] as [string, Base64Opts | undefined][])(
"doesn't validate invalid bas64",
(value, opts) => {
const struct = base64(string(), opts);
expect(is(value, struct)).toBe(false);
},
);
it('respects string() constraints', () => {
const struct = base64(size(string(), 4, 4));
expect(is('abcd', struct)).toBe(true);
expect(is('abcdabcd', struct)).toBe(false);
});
});
describe('Bip32PathStruct', () => {
it.each(['m/0/1/2', "m/0'/1/2", "m/1'/2'/3'/4/5/6", "m/0/1'/2"])(
'validates correctly',
(path) => {
expect(is(path.split('/'), Bip32PathStruct)).toBe(true);
},
);
it('requires an array', () => {
expect(is(['m', "0'", '123'], Bip32PathStruct)).toBe(true);
expect(is("m/0'/123", Bip32PathStruct)).toBe(false);
expect(is(42, Bip32PathStruct)).toBe(false);
});
it('requires an non-empty array', () => {
expect(() => assert([], Bip32PathStruct)).toThrow('non-empty');
});
it('requires "m" as first argument', () => {
expect(() => assert(['a'], Bip32PathStruct)).toThrow(
'Path must start with "m"',
);
expect(() => assert(['a', "0'", '123'], Bip32PathStruct)).toThrow(
'Path must start with "m"',
);
});
it('requires length >= 3', () => {
expect(() => assert(['m', "0'", '123'], Bip32PathStruct)).not.toThrow();
expect(() =>
assert(['m', "0'", '123', '456'], Bip32PathStruct),
).not.toThrow();
expect(() => assert(['m', "0'"], Bip32PathStruct)).toThrow(
'length of at least three',
);
expect(() => assert(['m'], Bip32PathStruct)).toThrow(
'length of at least three',
);
});
it.each(["m/0'/123/asd", 'm/0"/123', 'm/1/2/3/_', "m/1'/2'/3'/-1"])(
'requires numbers or hardened numbers',
(path) => {
expect(() => assert(path.split('/'), Bip32PathStruct)).toThrow(
'Path must be a valid BIP-32 derivation path array.',
);
},
);
it('throws for forbidden paths', () => {
expect(() => assert(['m', "1399742832'", '0'], Bip32PathStruct)).toThrow(
'The purpose "1399742832\'" is not allowed for entropy derivation.',
);
});
});
describe('Bip32EntropyStruct', () => {
it('works with ed25519', () => {
expect(
is(
{ path: "m/0'/1'/2'".split('/'), curve: 'ed25519' },
Bip32EntropyStruct,
),
).toBe(true);
});
it('ed25519 requires hardened paths', () => {
expect(() =>
assert(
{ path: "m/0'/1'/2'/3".split('/'), curve: 'ed25519' },
Bip32EntropyStruct,
),
).toThrow('Ed25519 does not support unhardened paths.');
});
it('works with secp256k1', () => {
expect(
is(
{ path: 'm/0/1/2'.split('/'), curve: 'secp256k1' },
Bip32EntropyStruct,
),
).toBe(true);
});
it.each([1, '', 'asd', {}, null, undefined])(
'requires valid curve',
(curve) => {
expect(
is({ path: "m/0'/1'/2'".split('/'), curve }, Bip32EntropyStruct),
).toBe(false);
},
);
it.each([42, "m/0'/123/asd".split('/')])('requires valid path', (path) => {
expect(is({ path, curve: 'secp256k1' }, Bip32EntropyStruct)).toBe(false);
});
it.each([undefined, null, {}, { asd: 123 }])(
'requires valid structure',
(value) => {
expect(is(value, Bip32EntropyStruct)).toBe(false);
},
);
});
describe('isSnapManifest', () => {
it('returns true for a valid snap manifest', () => {
expect(isSnapManifest(getSnapManifest())).toBe(true);
});
it.each([
true,
false,
null,
undefined,
0,
1,
'',
'foo',
[],
{},
{ name: 'foo' },
{ version: '1.0.0' },
getSnapManifest({ version: 'foo bar' }),
])('returns false for an invalid snap manifest', (value) => {
expect(isSnapManifest(value)).toBe(false);
});
});
describe('assertIsSnapManifest', () => {
it('does not throw for a valid snap manifest', () => {
expect(() => assertIsSnapManifest(getSnapManifest())).not.toThrow();
});
it.each([
true,
false,
null,
undefined,
0,
1,
'',
'foo',
[],
{},
{ name: 'foo' },
{ version: '1.0.0' },
getSnapManifest({ version: 'foo bar' }),
])('throws for an invalid snap manifest', (value) => {
expect(() => assertIsSnapManifest(value)).toThrow(
'"snap.manifest.json" is invalid:',
);
});
});
describe('createSnapManifest', () => {
it('does not throw for a valid snap manifest', () => {
expect(() => createSnapManifest(getSnapManifest())).not.toThrow();
});
it('coerces source paths', () => {
expect(
createSnapManifest(
getSnapManifest({ filePath: './bundle.js', iconPath: './icon.svg' }),
),
).toStrictEqual(
getSnapManifest({ filePath: 'bundle.js', iconPath: 'icon.svg' }),
);
});
it.each([
true,
false,
null,
undefined,
0,
1,
'',
'foo',
[],
{},
{ name: 'foo' },
{ version: '1.0.0' },
getSnapManifest({ version: 'foo bar' }),
])('throws for an invalid snap manifest', (value) => {
// eslint-disable-next-line jest/require-to-throw-message
expect(() => createSnapManifest(value)).toThrow();
});
});