Files
node/test/parallel/test-dlopen-binary.js
Livia Medeiros 04824484d6 test: restrict os.tmpdir() in favour of common/tmpdir
Signed-off-by: LiviaMedeiros <livia@cirno.name>
PR-URL: https://github.com/nodejs/node/pull/67567
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2026-11-20 05:26:57 +00:00

53 lines
2.1 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { internalBinding } = require('internal/test/binding');
// dlopenBinary() loads a native addon from bytes already in memory, for an
// addon that the dynamic loader cannot open by path (e.g. one served by a
// virtual file system). It is internal: process.dlopen() keeps its documented
// (module, filename[, flags]) signature and never takes the bytes.
const { dlopenBinary } = internalBinding('process_methods');
assert.strictEqual(typeof dlopenBinary, 'function');
assert.strictEqual(process.dlopen.length, 0);
const addonPath = path.join(
__dirname, '..', 'addons', 'hello-world', 'build', 'Release', 'binding.node');
if (!fs.existsSync(addonPath)) common.skip('the hello-world addon is not built');
const bytes = fs.readFileSync(addonPath);
const flags = (os.constants.dlopen?.RTLD_LAZY) || 0;
// A path that does not exist on disk, as a VFS-resident addon would be.
const fakePath = tmpdir.resolve('nonexistent-vfs-dir', 'binding.node');
assert.ok(!fs.existsSync(fakePath));
const m = { exports: {} };
dlopenBinary(m, fakePath, flags, bytes);
assert.strictEqual(typeof m.exports.hello, 'function');
assert.strictEqual(m.exports.hello(), 'world');
// The flags argument may be left undefined when passing bytes.
const m2 = { exports: {} };
dlopenBinary(m2, fakePath, undefined, bytes);
assert.strictEqual(m2.exports.hello(), 'world');
// Without the bytes, that nonexistent path cannot be loaded.
assert.throws(() => dlopenBinary({ exports: {} }, fakePath, flags),
{ code: 'ERR_DLOPEN_FAILED' });
// A non-buffer binary is rejected.
assert.throws(
() => dlopenBinary({ exports: {} }, fakePath, flags, 'nope'),
{ code: 'ERR_INVALID_ARG_TYPE' });
// process.dlopen() ignores a fourth argument rather than loading from it: the
// public signature is unchanged, so this still fails on the missing path.
assert.throws(
() => process.dlopen({ exports: {} }, fakePath, flags, bytes),
{ code: 'ERR_DLOPEN_FAILED' });