summaryrefslogtreecommitdiff
path: root/test/parallel/test-policy-parse-integrity.js
diff options
context:
space:
mode:
authorDaiki Ihara <sasurau4@gmail.com>2019-03-23 09:27:53 +0900
committerRich Trott <rtrott@gmail.com>2019-04-29 15:48:39 -0700
commita565853dddcf03cd33cc826cda5452fe75144055 (patch)
treeb9b29cc1ae5d7b25ec51f70286de040ed99992c5 /test/parallel/test-policy-parse-integrity.js
parent723d5c058fa180684df13bd2a83bbf3ca6201957 (diff)
downloadandroid-node-v8-a565853dddcf03cd33cc826cda5452fe75144055.tar.gz
android-node-v8-a565853dddcf03cd33cc826cda5452fe75144055.tar.bz2
android-node-v8-a565853dddcf03cd33cc826cda5452fe75144055.zip
test: add test of policy about parse error
PR-URL: https://github.com/nodejs/node/pull/26873 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/parallel/test-policy-parse-integrity.js')
-rw-r--r--test/parallel/test-policy-parse-integrity.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/test/parallel/test-policy-parse-integrity.js b/test/parallel/test-policy-parse-integrity.js
new file mode 100644
index 0000000000..7779f767fb
--- /dev/null
+++ b/test/parallel/test-policy-parse-integrity.js
@@ -0,0 +1,86 @@
+'use strict';
+
+const common = require('../common');
+if (!common.hasCrypto) common.skip('missing crypto');
+
+const tmpdir = require('../common/tmpdir');
+const assert = require('assert');
+const { spawnSync } = require('child_process');
+const crypto = require('crypto');
+const fs = require('fs');
+const path = require('path');
+const { pathToFileURL } = require('url');
+
+tmpdir.refresh();
+
+function hash(algo, body) {
+ const h = crypto.createHash(algo);
+ h.update(body);
+ return h.digest('base64');
+}
+
+const policyFilepath = path.join(tmpdir.path, 'policy');
+
+const parentFilepath = path.join(tmpdir.path, 'parent.js');
+const parentBody = "require('./dep.js')";
+
+const depFilepath = path.join(tmpdir.path, 'dep.js');
+const depURL = pathToFileURL(depFilepath);
+const depBody = '';
+
+fs.writeFileSync(parentFilepath, parentBody);
+fs.writeFileSync(depFilepath, depBody);
+
+const tmpdirURL = pathToFileURL(tmpdir.path);
+if (!tmpdirURL.pathname.endsWith('/')) {
+ tmpdirURL.pathname += '/';
+}
+
+function test({ shouldFail, integrity }) {
+ const resources = {
+ [depURL]: {
+ body: depBody,
+ integrity
+ }
+ };
+ const manifest = {
+ resources: {},
+ };
+ for (const [url, { body, integrity }] of Object.entries(resources)) {
+ manifest.resources[url] = {
+ integrity,
+ };
+ fs.writeFileSync(new URL(url, tmpdirURL.href), body);
+ }
+ fs.writeFileSync(policyFilepath, JSON.stringify(manifest, null, 2));
+ const { status } = spawnSync(process.execPath, [
+ '--experimental-policy',
+ policyFilepath,
+ depFilepath
+ ]);
+ if (shouldFail) {
+ assert.notStrictEqual(status, 0);
+ } else {
+ assert.strictEqual(status, 0);
+ }
+}
+
+test({
+ shouldFail: false,
+ integrity: `sha256-${hash('sha256', depBody)}`,
+});
+test({
+ shouldFail: true,
+ integrity: `1sha256-${hash('sha256', depBody)}`,
+});
+test({
+ shouldFail: true,
+ integrity: 'hoge',
+});
+test({
+ shouldFail: true,
+ integrity: `sha256-${hash('sha256', depBody)}sha256-${hash(
+ 'sha256',
+ depBody
+ )}`,
+});