forked from guacsec/trustify-da-javascript-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.test.js
More file actions
374 lines (319 loc) · 18.2 KB
/
Copy pathjavascript.test.js
File metadata and controls
374 lines (319 loc) · 18.2 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { fail } from 'assert';
import fs from 'fs'
import { expect } from 'chai'
import esmock from 'esmock';
import { useFakeTimers } from "sinon";
import { availableProviders, match } from '../../src/provider.js';
import Manifest from '../../src/providers/manifest.js';
import { compareSboms } from '../utils/sbom_utils.js';
let clock
async function mockProvider(providerName, listingOutput, version) {
const mockInvokeCommand = (_cmd, args) => {
if (args.includes('--version')) {return version ? version : '0.0.0-mock';}
return listingOutput;
};
return esmock(`../../src/providers/javascript_${providerName}.js`, {
'../../src/providers/base_javascript.js': await esmock('../../src/providers/base_javascript.js', {
'../../src/tools.js': {
invokeCommand: mockInvokeCommand
}
})
});
}
async function createMockProvider(providerName, listingOutput) {
switch (providerName) {
case 'npm': {
const Javascript_npm = await mockProvider(providerName, listingOutput);
return new Javascript_npm();
}
case 'pnpm': {
const Javascript_pnpm = await mockProvider(providerName, listingOutput);
return new Javascript_pnpm();
}
case 'yarn-classic': {
const Javascript_yarn = await mockProvider('yarn', listingOutput, '1.22.22');
return new Javascript_yarn();
}
case 'yarn-berry': {
const Javascript_yarn = await mockProvider('yarn', listingOutput, '4.9.1');
return new Javascript_yarn();
}
case 'bun': {
const Javascript_bun = await mockProvider(providerName, listingOutput);
return new Javascript_bun();
}
default: { fail('Not implemented'); }
}
}
suite('testing the javascript-npm data provider', async () => {
[
{ name: 'npm/with_lock_file', validation: true },
{ name: 'npm/without_lock_file', validation: false },
{ name: 'npm/workspace_member_with_lock/packages/module-a', validation: true },
{ name: 'npm/workspace_member_without_lock/packages/module-a', validation: false },
{ name: 'pnpm/with_lock_file', validation: true },
{ name: 'pnpm/without_lock_file', validation: false },
{ name: 'yarn-classic/with_lock_file', validation: true },
{ name: 'yarn-classic/without_lock_file', validation: false },
{ name: 'yarn-berry/with_lock_file', validation: true },
{ name: 'yarn-berry/without_lock_file', validation: false },
{ name: 'bun/with_lock_file', validation: true },
{ name: 'bun/without_lock_file', validation: false },
{ name: 'bun/workspace_member_with_lock/packages/module-a', validation: true },
{ name: 'bun/workspace_member_without_lock/packages/module-a', validation: false }
].forEach(testCase => {
test(`verify isSupported returns ${testCase.expected} for ${testCase.name}`, () => {
let manifest = `test/providers/provider_manifests/${testCase.name}/package.json`;
try {
const provider = match(manifest, availableProviders);
expect(provider).not.to.be.null;
expect(testCase.validation).to.be.true;
} catch (e) {
expect(testCase.validation).to.be.false;
}
})
});
['npm', 'pnpm', 'yarn-classic', 'yarn-berry'].flatMap(providerName => [
"package_json_deps_without_exhortignore_object",
"package_json_deps_with_exhortignore_object",
"package_json_deps_with_mixed_dep_types"
].map(testCase => ({ providerName, testCase }))).forEach(({ providerName, testCase }) => {
let scenario = testCase.replace('package_json_deps_', '').replaceAll('_', ' ')
test(`verify package.json data provided for ${providerName} - stack analysis - ${scenario}`, async () => {
// load the expected graph for the scenario
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/stack_expected_sbom.json`,).toString();
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_stack.json`,).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/package.json`;
let providedDataForStack = provider.provideStack(manifestPath);
compareSboms(providedDataForStack.content, expectedSbom);
}).timeout(30000);
test(`verify package.json data provided for ${providerName} - component analysis - ${scenario}`, async () => {
// load the expected list for the scenario
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/js-common/${testCase}/component_expected_sbom.json`,).toString().trim()
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_component.json`,).toString()
// verify returned data matches expectation
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/package.json`;
let providedDataForComponent = provider.provideComponent(manifestPath);
compareSboms(providedDataForComponent.content, expectedSbom);
}).timeout(15000)
});
[
{ providerName: 'pnpm', testCase: 'workspace_member' },
].forEach(({ providerName, testCase }) => {
test(`verify workspace member data for ${providerName} - stack analysis`, async () => {
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/stack_expected_sbom.json`).toString();
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_stack.json`).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/packages/member-a/package.json`;
let providedDataForStack = provider.provideStack(manifestPath);
compareSboms(providedDataForStack.content, expectedSbom);
}).timeout(30000);
test(`verify workspace member data for ${providerName} - component analysis`, async () => {
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/component_expected_sbom.json`).toString();
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_component.json`).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/packages/member-a/package.json`;
let providedDataForComponent = provider.provideComponent(manifestPath);
compareSboms(providedDataForComponent.content, expectedSbom);
}).timeout(15000);
});
[
{ providerName: 'yarn-berry', testCase: 'workspace_member' },
].forEach(({ providerName, testCase }) => {
test(`verify workspace member data for ${providerName} - stack analysis`, async () => {
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/stack_expected_sbom.json`).toString();
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_stack.json`).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/packages/member-a/package.json`;
let providedDataForStack = provider.provideStack(manifestPath);
compareSboms(providedDataForStack.content, expectedSbom);
}).timeout(30000);
test(`verify workspace member data for ${providerName} - component analysis`, async () => {
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/component_expected_sbom.json`).toString();
let listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_component.json`).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/packages/member-a/package.json`;
let providedDataForComponent = provider.provideComponent(manifestPath);
compareSboms(providedDataForComponent.content, expectedSbom);
}).timeout(15000);
});
[
{ providerName: 'npm', testCase: 'workspace_member' },
].forEach(({ providerName, testCase }) => {
/// Verifies that stack analysis resolves transitive dependencies for a workspace member.
test(`verify workspace member data provided for ${providerName} - stack analysis`, async () => {
// Given a workspace member manifest and mock listing from the workspace root
const listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_stack.json`).toString();
const expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/stack_expected_sbom.json`).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/packages/member-a/package.json`;
// When running stack analysis on the workspace member
const result = provider.provideStack(manifestPath);
// Then the SBOM should contain the member's transitive dependencies
compareSboms(result.content, expectedSbom);
}).timeout(30000);
/// Verifies that component analysis resolves direct dependencies for a workspace member.
test(`verify workspace member data provided for ${providerName} - component analysis`, async () => {
// Given a workspace member manifest and mock listing from the workspace root
const listing = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/listing_component.json`).toString();
const expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/component_expected_sbom.json`).toString();
const provider = await createMockProvider(providerName, listing);
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/packages/member-a/package.json`;
// When running component analysis on the workspace member
const result = provider.provideComponent(manifestPath);
// Then the SBOM should contain only the member's direct dependencies
compareSboms(result.content, expectedSbom);
}).timeout(15000);
});
['bun'].flatMap(providerName => [
{ testCase: "package_json_deps_without_exhortignore_object", manifest: "package.json" },
{ testCase: "package_json_deps_with_exhortignore_object", manifest: "package.json" },
{ testCase: "package_json_deps_with_mixed_dep_types", manifest: "package.json" },
{ testCase: "workspace_member", manifest: "packages/member-a/package.json" },
].map(tc => ({ providerName, ...tc }))).forEach(({ providerName, testCase, manifest }) => {
let scenario = testCase.replace('package_json_deps_', '').replaceAll('_', ' ')
test(`verify package.json data provided for ${providerName} - stack analysis - ${scenario}`, async () => {
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/stack_expected_sbom.json`).toString();
const provider = await createMockProvider(providerName, '');
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/${manifest}`;
let providedDataForStack = provider.provideStack(manifestPath);
compareSboms(providedDataForStack.content, expectedSbom);
}).timeout(30000);
test(`verify package.json data provided for ${providerName} - component analysis - ${scenario}`, async () => {
let expectedSbom = fs.readFileSync(`test/providers/tst_manifests/${providerName}/${testCase}/component_expected_sbom.json`).toString().trim();
const provider = await createMockProvider(providerName, '');
const manifestPath = `test/providers/tst_manifests/${providerName}/${testCase}/${manifest}`;
let providedDataForComponent = provider.provideComponent(manifestPath);
compareSboms(providedDataForComponent.content, expectedSbom);
}).timeout(15000)
});
test('loads a valid manifest with ignored dependencies', () => {
const testCase = 'package_json_deps_with_exhortignore_object';
const manifestPath = `test/providers/tst_manifests/npm/${testCase}/package.json`;
const m = new Manifest(manifestPath);
expect(m.name).to.be.equals('backend');
expect(m.version).to.be.equals('1.0.0');
expect(m.manifestPath).to.be.equals(manifestPath);
expect(m.dependencies).to.have.all.members([
"@hapi/joi",
"backend",
"bcryptjs",
"dotenv",
"express",
"jsonwebtoken",
"mongoose",
"nodemon",
"axios",
"jsdom"]);
const ignoredNames = m.ignored.map(dep => dep.name);
expect(ignoredNames).to.have.all.members(['jsonwebtoken']);
});
test('loads a valid manifest without ignored dependencies', () => {
const testCase = 'package_json_deps_without_exhortignore_object';
const manifestPath = `test/providers/tst_manifests/npm/${testCase}/package.json`;
const m = new Manifest(manifestPath);
expect(m.name).to.be.equals('backend');
expect(m.version).to.be.equals('1.0.0');
expect(m.manifestPath).to.be.equals(manifestPath);
expect(m.dependencies).to.have.all.members([
"@hapi/joi",
"backend",
"bcryptjs",
"dotenv",
"express",
"jsdom",
"jsonwebtoken",
"mongoose",
"nodemon",
"axios"]);
expect(m.ignored).to.be.empty;
});
test('loads a manifest with mixed dependency types (peer, optional, bundled)', () => {
const testCase = 'package_json_deps_with_mixed_dep_types';
const manifestPath = `test/providers/tst_manifests/npm/${testCase}/package.json`;
const m = new Manifest(manifestPath);
expect(m.name).to.be.equals('mixed-deps-test');
expect(m.version).to.be.equals('1.0.0');
expect(m.dependencies).to.have.all.members([
'express', 'axios', 'minimist', 'lodash']);
expect(m.dependencies).to.not.include('jest');
expect(m.dependencies).to.not.include('eslint');
expect(m.peerDependencies).to.deep.equal({ minimist: '1.2.0' });
expect(m.optionalDependencies).to.deep.equal({ lodash: '4.17.19' });
expect(m.ignored).to.be.empty;
});
test('fails when the manifest does not exist', () => {
const testCase = 'wrong_folder';
const manifestPath = `test/providers/tst_manifests/npm/${testCase}/package.json`;
expect(() => new Manifest(manifestPath)).to.throw(Error);
});
test('verify match with opts.TRUSTIFY_DA_WORKSPACE_DIR finds npm provider when lock is at workspace root', () => {
const manifest = 'test/providers/provider_manifests/npm/with_lock_file/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/npm/with_lock_file' }
const provider = match(manifest, availableProviders, opts)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify match with opts.TRUSTIFY_DA_WORKSPACE_DIR finds pnpm provider when lock is at workspace root', () => {
const manifest = 'test/providers/provider_manifests/pnpm/with_lock_file/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/pnpm/with_lock_file' }
const provider = match(manifest, availableProviders, opts)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify workspace member walks up and finds lock file at workspace root', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_with_lock/packages/module-a/package.json'
const provider = match(manifest, availableProviders)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify workspace member throws when workspace root has no lock file', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_without_lock/packages/module-a/package.json'
expect(() => match(manifest, availableProviders))
.to.throw('package.json requires a lock file')
})
test('verify match with opts.TRUSTIFY_DA_WORKSPACE_DIR overrides walk-up for workspace member', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_with_lock/packages/module-a/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/npm/workspace_member_with_lock' }
const provider = match(manifest, availableProviders, opts)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify pnpm workspace member stops at pnpm-workspace.yaml boundary', () => {
const manifest = 'test/providers/provider_manifests/pnpm/workspace_member_without_lock/packages/module-a/package.json'
expect(() => match(manifest, availableProviders))
.to.throw('package.json requires a lock file')
})
test('verify match with wrong TRUSTIFY_DA_WORKSPACE_DIR fails even when walk-up would succeed', () => {
const manifest = 'test/providers/provider_manifests/npm/workspace_member_with_lock/packages/module-a/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/npm/workspace_member_without_lock' }
expect(() => match(manifest, availableProviders, opts))
.to.throw('package.json requires a lock file')
})
test('verify match with opts.TRUSTIFY_DA_WORKSPACE_DIR finds bun provider when lock is at workspace root', () => {
const manifest = 'test/providers/provider_manifests/bun/with_lock_file/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/bun/with_lock_file' }
const provider = match(manifest, availableProviders, opts)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify bun workspace member walks up and finds lock file at workspace root', () => {
const manifest = 'test/providers/provider_manifests/bun/workspace_member_with_lock/packages/module-a/package.json'
const provider = match(manifest, availableProviders)
expect(provider).to.not.be.null
expect(provider.isSupported('package.json')).to.be.true
})
test('verify bun workspace member throws when workspace root has no lock file', () => {
const manifest = 'test/providers/provider_manifests/bun/workspace_member_without_lock/packages/module-a/package.json'
expect(() => match(manifest, availableProviders))
.to.throw('package.json requires a lock file')
})
test('verify match with wrong TRUSTIFY_DA_WORKSPACE_DIR fails for bun even when walk-up would succeed', () => {
const manifest = 'test/providers/provider_manifests/bun/workspace_member_with_lock/packages/module-a/package.json'
const opts = { TRUSTIFY_DA_WORKSPACE_DIR: 'test/providers/provider_manifests/bun/workspace_member_without_lock' }
expect(() => match(manifest, availableProviders, opts))
.to.throw('package.json requires a lock file')
})
}).beforeAll(() => clock = useFakeTimers(new Date('2023-08-07T00:00:00.000Z'))).afterAll(() => clock.restore());