From 3bb7f3602b855442cc933d0c4cbba33dfa182353 Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Mon, 26 Oct 2020 01:15:18 +0900 Subject: [PATCH] test: add onerror test cases to policy Increase test coverage of lib/internal/policy/manifest.js PR-URL: https://github.com/nodejs/node/pull/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 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- test/parallel/test-policy-parse-integrity.js | 20 ++++++++--- test/parallel/test-policy-scopes-integrity.js | 33 +++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-policy-parse-integrity.js b/test/parallel/test-policy-parse-integrity.js index 9241f1e7843b64..d042ccee0a49af 100644 --- a/test/parallel/test-policy-parse-integrity.js +++ b/test/parallel/test-policy-parse-integrity.js @@ -44,7 +44,8 @@ const packageFilepath = path.join(tmpdirPath, 'package.json'); const packageURL = pathToFileURL(packageFilepath); const packageBody = '{"main": "dep.js"}'; -function test({ shouldFail, integrity }) { +function test({ shouldFail, integrity, manifest = {} }) { + manifest.resources = {}; const resources = { [packageURL]: { body: packageBody, @@ -55,9 +56,6 @@ function test({ shouldFail, integrity }) { integrity } }; - const manifest = { - resources: {}, - }; for (const [url, { body, integrity }] of Object.entries(resources)) { manifest.resources[url] = { integrity, @@ -96,3 +94,17 @@ test({ depBody )}`, }); +test({ + shouldFail: true, + integrity: `sha256-${hash('sha256', 'file:///')}`, + manifest: { + onerror: 'exit' + } +}); +test({ + shouldFail: false, + integrity: `sha256-${hash('sha256', 'file:///')}`, + manifest: { + onerror: 'log' + } +}); diff --git a/test/parallel/test-policy-scopes-integrity.js b/test/parallel/test-policy-scopes-integrity.js index b506c92c41f625..b61fc3199cd4e0 100644 --- a/test/parallel/test-policy-scopes-integrity.js +++ b/test/parallel/test-policy-scopes-integrity.js @@ -280,3 +280,36 @@ const assert = require('assert'); } } // #endregion +// #startonerror +{ + const manifest = new Manifest({ + scopes: { + 'file:///': { + integrity: true + } + }, + onerror: 'throw' + }); + assert.throws( + () => { + manifest.assertIntegrity('http://example.com'); + }, + /ERR_MANIFEST_ASSERT_INTEGRITY/ + ); +} +{ + assert.throws( + () => { + new Manifest({ + scopes: { + 'file:///': { + integrity: true + } + }, + onerror: 'unknown' + }); + }, + /ERR_MANIFEST_UNKNOWN_ONERROR/ + ); +} +// #endonerror