Skip to content

Commit 0d74198

Browse files
zecksontargos
authored andcommitted
test: cover import of a *.node file with a policy manifest
Cover import of a *.node file with a policy manifest. Add invalid integrity test case. PR-URL: #27903 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 9220a68 commit 0d74198

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

test/node-api/test_policy/binding.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <node_api.h>
2+
#include "../../js-native-api/common.h"
3+
#include <string.h>
4+
5+
static napi_value Method(napi_env env, napi_callback_info info) {
6+
napi_value world;
7+
const char* str = "world";
8+
size_t str_len = strlen(str);
9+
NAPI_CALL(env, napi_create_string_utf8(env, str, str_len, &world));
10+
return world;
11+
}
12+
13+
NAPI_MODULE_INIT() {
14+
napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("hello", Method);
15+
NAPI_CALL(env, napi_define_properties(env, exports, 1, &desc));
16+
return exports;
17+
}

test/node-api/test_policy/binding.gyp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "binding",
5+
"sources": [ "binding.c" ]
6+
}
7+
]
8+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
const common = require('../../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
const assert = require('assert');
7+
const tmpdir = require('../../common/tmpdir');
8+
const { spawnSync } = require('child_process');
9+
const crypto = require('crypto');
10+
const fs = require('fs');
11+
const path = require('path');
12+
const { pathToFileURL } = require('url');
13+
14+
tmpdir.refresh();
15+
16+
function hash(algo, body) {
17+
const h = crypto.createHash(algo);
18+
h.update(body);
19+
return h.digest('base64');
20+
}
21+
22+
const policyFilepath = path.join(tmpdir.path, 'policy');
23+
24+
const depFilepath = require.resolve(`./build/${common.buildType}/binding.node`);
25+
const depURL = pathToFileURL(depFilepath);
26+
27+
const tmpdirURL = pathToFileURL(tmpdir.path);
28+
if (!tmpdirURL.pathname.endsWith('/')) {
29+
tmpdirURL.pathname += '/';
30+
}
31+
32+
const depBody = fs.readFileSync(depURL);
33+
function writePolicy(...resources) {
34+
const manifest = { resources: {} };
35+
for (const { url, integrity } of resources) {
36+
manifest.resources[url] = { integrity };
37+
}
38+
fs.writeFileSync(policyFilepath, JSON.stringify(manifest, null, 2));
39+
}
40+
41+
42+
function test(shouldFail, resources) {
43+
writePolicy(...resources);
44+
const { status, stdout, stderr } = spawnSync(process.execPath, [
45+
'--experimental-policy',
46+
policyFilepath,
47+
depFilepath
48+
]);
49+
50+
console.log(stdout.toString(), stderr.toString());
51+
if (shouldFail) {
52+
assert.notStrictEqual(status, 0);
53+
} else {
54+
assert.strictEqual(status, 0);
55+
}
56+
}
57+
58+
test(false, [{
59+
url: depURL,
60+
integrity: `sha256-${hash('sha256', depBody)}`
61+
}]);
62+
test(true, [{
63+
url: depURL,
64+
integrity: `sha256akjsalkjdlaskjdk-${hash('sha256', depBody)}`
65+
}]);

0 commit comments

Comments
 (0)