Skip to content

Commit 8958af4

Browse files
watildetargos
authored andcommitted
test: add onerror test cases to policy
Increase test coverage of lib/internal/policy/manifest.js PR-URL: #35797 Refs: https://coverage.nodejs.org/coverage-642f2064c06793b7/lib/internal/policy/manifest.js.html#L60 Refs: https://coverage.nodejs.org/coverage-642f2064c06793b7/lib/internal/policy/manifest.js.html#L146 Reviewed-By: Ben Coe <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent d291e3a commit 8958af4

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

test/parallel/test-policy-parse-integrity.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ const packageFilepath = path.join(tmpdirPath, 'package.json');
4444
const packageURL = pathToFileURL(packageFilepath);
4545
const packageBody = '{"main": "dep.js"}';
4646

47-
function test({ shouldFail, integrity }) {
47+
function test({ shouldFail, integrity, manifest = {} }) {
48+
manifest.resources = {};
4849
const resources = {
4950
[packageURL]: {
5051
body: packageBody,
@@ -55,9 +56,6 @@ function test({ shouldFail, integrity }) {
5556
integrity
5657
}
5758
};
58-
const manifest = {
59-
resources: {},
60-
};
6159
for (const [url, { body, integrity }] of Object.entries(resources)) {
6260
manifest.resources[url] = {
6361
integrity,
@@ -96,3 +94,17 @@ test({
9694
depBody
9795
)}`,
9896
});
97+
test({
98+
shouldFail: true,
99+
integrity: `sha256-${hash('sha256', 'file:///')}`,
100+
manifest: {
101+
onerror: 'exit'
102+
}
103+
});
104+
test({
105+
shouldFail: false,
106+
integrity: `sha256-${hash('sha256', 'file:///')}`,
107+
manifest: {
108+
onerror: 'log'
109+
}
110+
});

test/parallel/test-policy-scopes-integrity.js

+33
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,36 @@ const assert = require('assert');
280280
}
281281
}
282282
// #endregion
283+
// #startonerror
284+
{
285+
const manifest = new Manifest({
286+
scopes: {
287+
'file:///': {
288+
integrity: true
289+
}
290+
},
291+
onerror: 'throw'
292+
});
293+
assert.throws(
294+
() => {
295+
manifest.assertIntegrity('http://example.com');
296+
},
297+
/ERR_MANIFEST_ASSERT_INTEGRITY/
298+
);
299+
}
300+
{
301+
assert.throws(
302+
() => {
303+
new Manifest({
304+
scopes: {
305+
'file:///': {
306+
integrity: true
307+
}
308+
},
309+
onerror: 'unknown'
310+
});
311+
},
312+
/ERR_MANIFEST_UNKNOWN_ONERROR/
313+
);
314+
}
315+
// #endonerror

0 commit comments

Comments
 (0)