Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cover import of a *.node file with a policy manifest #27903

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions test/node-api/test_policy/binding.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <node_api.h>
#include "../../js-native-api/common.h"
#include <string.h>

static napi_value Method(napi_env env, napi_callback_info info) {
napi_value world;
const char* str = "world";
size_t str_len = strlen(str);
NAPI_CALL(env, napi_create_string_utf8(env, str, str_len, &world));
return world;
}

NAPI_MODULE_INIT() {
napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("hello", Method);
NAPI_CALL(env, napi_define_properties(env, exports, 1, &desc));
return exports;
}
8 changes: 8 additions & 0 deletions test/node-api/test_policy/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.c" ]
}
]
}
61 changes: 61 additions & 0 deletions test/node-api/test_policy/test_policy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';
const common = require('../../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const tmpdir = require('../../common/tmpdir');
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 depFilepath = require.resolve(`./build/${common.buildType}/binding.node`);
const depURL = pathToFileURL(depFilepath);

const tmpdirURL = pathToFileURL(tmpdir.path);
if (!tmpdirURL.pathname.endsWith('/')) {
tmpdirURL.pathname += '/';
}

const depBody = fs.readFileSync(depURL);
function writePolicy(...resources) {
const manifest = { resources: {} };
for (const { url, integrity } of resources) {
manifest.resources[url] = { integrity };
}
fs.writeFileSync(policyFilepath, JSON.stringify(manifest, null, 2));
}


function test(shouldFail, resources) {
writePolicy(...resources);
const { status, stdout, stderr } = spawnSync(process.execPath, [
'--experimental-policy',
policyFilepath,
depFilepath
]);

console.log(stdout.toString(), stderr.toString());
if (shouldFail) {
assert.strictNotEqual(status, 0);
} else {
assert.strictEqual(status, 0);
}
}

test(false, [{
url: depURL,
integrity: `sha256-${hash('sha256', depBody)}`
}]);
zeckson marked this conversation as resolved.
Show resolved Hide resolved