-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.test.js
More file actions
321 lines (289 loc) Β· 10.3 KB
/
cli.test.js
File metadata and controls
321 lines (289 loc) Β· 10.3 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import test from 'node:test';
import assert from 'node:assert';
import {execa} from 'execa';
// Helper function to test help output
async function expectHelp(args = []) {
let code;
try {
const {exitCode} = await execa('./cli.js', args);
code = exitCode;
} catch (error) {
code = error.exitCode;
}
assert.strictEqual(code, 2);
}
test('Help output for missing required flags', async t => {
await t.test('No flags shows help', async () => {
await expectHelp();
});
await t.test('Missing "expires" flag shows help', async () => {
await expectHelp(['--contact=itsec@acme.org']);
});
await t.test('Missing "contact" flag shows help', async () => {
await expectHelp(['--expires=7']);
});
await t.test('Empty "expires" value shows help', async () => {
await expectHelp(['--contact=itsec@acme.org', '--expires=']);
});
await t.test('Empty "contact" value shows help', async () => {
// Simulate empty contact array by not providing contact
await expectHelp(['--expires=7', '--contact=']);
});
});
test('Basic functionality', async t => {
await t.test('Minimal flags are handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=6',
]);
assert.match(stdout, /Contact:\smailto:itsec@acme.org\n/v);
assert.match(stdout, /Expires:\s(.+)/v);
});
await t.test('All flags are handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=6',
'--lang=en',
'--canonical=https://acme.org/.well-known/security.txt',
'--encryption=https://acme.org/key.asc',
'--ack=https://acme.org/security/acknowledgments.txt',
'--policy=https://acme.org/security/policy.txt',
'--hiring=https://acme.org/jobs',
'--csaf=https://acme.org/csaf/provider-metadata.json',
]);
assert.match(stdout, /Contact:\smailto:itsec@acme.org\n/v);
assert.match(stdout, /Expires:\s(.+)\n/v);
assert.match(stdout, /Preferred-Languages:\sen\n/v);
assert.match(
stdout,
/Canonical:\shttps:\/\/acme\.org\/\.well-known\/security\.txt\n/v,
);
assert.match(stdout, /Encryption:\shttps:\/\/acme\.org\/key\.asc\n/v);
assert.match(
stdout,
/Acknowledgments:\shttps:\/\/acme\.org\/security\/acknowledgments\.txt\n/v,
);
assert.match(stdout, /Policy:\shttps:\/\/acme\.org\/security\/policy\.txt\n/v);
assert.match(stdout, /Hiring:\shttps:\/\/acme\.org\/jobs\n/v);
assert.match(stdout, /CSAF:\shttps:\/\/acme\.org\/csaf\/provider-metadata\.json/v);
});
});
test('Contact handling', async t => {
await t.test('Email address without `mailto:` protocol gets prefixed automatically', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=6',
]);
assert.match(stdout, /Contact: mailto:itsec@acme.org\n/v);
});
await t.test('Email address with explicit `mailto:` protocol remains intact', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=mailto:itsec@acme.org',
'--expires=6',
]);
assert.match(stdout, /Contact: mailto:itsec@acme.org\n/v);
});
await t.test('URL contact point is handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=https://acme.org/contact',
'--expires=6',
]);
assert.match(stdout, /Contact: https:\/\/acme\.org\/contact\n/v);
});
await t.test('More than one contact point is handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--contact=https://acme.org/contact',
'--expires=6',
]);
assert.match(stdout, /Contact:\smailto:itsec@acme.org\n/v);
assert.match(stdout, /Contact:\shttps:\/\/acme\.org\/contact\n/v);
assert.match(stdout, /Expires:\s(.+)/v);
});
await t.test(
'Email address with `@` symbol but already having the `mailto:` protocol is not double-prefixed',
async () => {
const {stdout} = await execa('./cli.js', [
'--contact=mailto:security@example.com',
'--expires=7',
]);
assert.match(stdout, /Contact: mailto:security@example\.com\n/v);
assert.doesNotMatch(stdout, /(?:mailto:){2}/v);
},
);
await t.test('Contact point without `@` symbol is handled as URL', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=https://acme.org/contact',
'--expires=7',
]);
assert.match(stdout, /Contact: https:\/\/acme\.org\/contact\n/v);
});
});
test('Language handling', async t => {
await t.test('Multiple languages are handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=6',
'--lang=en',
'--lang=fi',
]);
assert.match(stdout, /Contact:\smailto:itsec@acme.org\n/v);
assert.match(stdout, /Expires:\s(.+)\n/v);
assert.match(stdout, /Preferred-Languages:\sen,\sfi/v);
});
await t.test('Single language is handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=7',
'--lang=de',
]);
assert.match(stdout, /Preferred-Languages: de/v);
});
await t.test('No languages does not output `Preferred-Languages` field', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=6',
]);
assert.doesNotMatch(stdout, /Preferred-Languages:/v);
});
await t.test('Empty language does not output `Preferred-Languages` field', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=7',
'--lang=',
]);
assert.doesNotMatch(stdout, /Preferred-Languages:/v);
});
});
test('Expires handling', async t => {
await t.test('Numeric value is handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=6',
]);
assert.match(stdout, /Expires:\s(.+)/v);
});
await t.test('ISO date string is handled correctly', async () => {
const date = '2080-08-01T15:00:00Z';
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
`--expires=${date}`,
]);
assert.match(stdout, /Expires:\s(.+)/v);
});
await t.test('Past date still generates valid output', async () => {
const pastDate = '2020-01-01T00:00:00Z';
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
`--expires=${pastDate}`,
]);
assert.match(stdout, /Expires: (.+)/v);
});
await t.test('Zero expires value is handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=test@example.com',
'--expires=0',
]);
assert.match(stdout, /Contact: mailto:test@example\.com\n/v);
assert.match(stdout, /Expires: (.+)/v);
});
await t.test('Negative expires value is handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=-5',
]);
assert.match(stdout, /Contact: mailto:itsec@acme\.org\n/v);
assert.match(stdout, /Expires: (.+)/v);
});
await t.test('Large numeric expires value is handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=999999',
]);
assert.match(stdout, /Contact: mailto:itsec@acme\.org/v);
assert.match(stdout, /Expires: (.+)/v);
});
await t.test('Unparseable expires date shows help', async () => {
await expectHelp(['--contact=itsec@acme.org', '--expires=FAIL']);
});
await t.test('Malformed ISO date string shows help', async () => {
await expectHelp([
'--contact=itsec@acme.org',
'--expires=2024-13-45T25:99:99Z',
]);
});
});
test('Flag formats and multiple values', async t => {
await t.test('Short flags work the same as long flags', async () => {
const {stdout} = await execa('./cli.js', [
'-c',
'itsec@acme.org',
'-e',
'6',
'-l',
'en',
'-p',
'https://acme.org/policy.txt',
'-s',
'https://acme.org/csaf/provider-metadata.json',
]);
assert.match(stdout, /Contact: mailto:itsec@acme.org\n/v);
assert.match(stdout, /Expires: (.+)\n/v);
assert.match(stdout, /Preferred-Languages: en\n/v);
assert.match(stdout, /Policy: https:\/\/acme\.org\/policy\.txt\n/v);
assert.match(stdout, /CSAF: https:\/\/acme\.org\/csaf\/provider-metadata\.json/v);
});
await t.test('Multiple flags of the same type are handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'-c',
'itsec@acme.org',
'-c',
'https://acme.org/contact',
'-e',
'6',
'-p',
'https://acme.org/policy1.txt',
'-p',
'https://acme.org/policy2.txt',
'-s',
'https://acme.org/csaf/provider-metadata-1.json',
'-s',
'https://acme.org/csaf/provider-metadata-2.json',
]);
assert.match(stdout, /Contact: mailto:itsec@acme.org\n/v);
assert.match(stdout, /Contact: https:\/\/acme\.org\/contact\n/v);
assert.match(stdout, /Policy: https:\/\/acme\.org\/policy1\.txt\n/v);
assert.match(stdout, /Policy: https:\/\/acme\.org\/policy2\.txt\n/v);
assert.match(stdout, /CSAF: https:\/\/acme\.org\/csaf\/provider-metadata-1\.json\n/v);
assert.match(stdout, /CSAF: https:\/\/acme\.org\/csaf\/provider-metadata-2\.json/v);
});
await t.test('Multiple CSAF URLs are handled correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=6',
'--csaf=https://acme.org/csaf/provider-metadata-1.json',
'--csaf=https://acme.org/csaf/provider-metadata-2.json',
]);
assert.match(stdout, /Contact:\smailto:itsec@acme.org\n/v);
assert.match(stdout, /Expires:\s(.+)\n/v);
assert.match(stdout, /CSAF:\shttps:\/\/acme\.org\/csaf\/provider-metadata-1\.json\n/v);
assert.match(stdout, /CSAF:\shttps:\/\/acme\.org\/csaf\/provider-metadata-2\.json/v);
});
await t.test('All flag types with single values are processed correctly', async () => {
const {stdout} = await execa('./cli.js', [
'--contact=itsec@acme.org',
'--expires=7',
'--canonical=https://acme.org/.well-known/security.txt',
'--encryption=https://acme.org/pgp.asc',
'--ack=https://acme.org/thanks',
'--policy=https://acme.org/policy',
'--hiring=https://acme.org/jobs',
]);
assert.match(stdout, /Contact: mailto:itsec@acme\.org/v);
assert.match(stdout, /Canonical: https:\/\/acme\.org\/\.well-known\/security\.txt/v);
assert.match(stdout, /Encryption: https:\/\/acme\.org\/pgp\.asc/v);
assert.match(stdout, /Acknowledgments: https:\/\/acme\.org\/thanks/v);
assert.match(stdout, /Policy: https:\/\/acme\.org\/policy/v);
assert.match(stdout, /Hiring: https:\/\/acme\.org\/jobs/v);
});
});