Skip to content

Commit a38219f

Browse files
addaleaxMylesBorins
authored andcommitted
test: add regression test for C++-created Buffer transfer
Add a test for a regression that occurs when transferring some `Buffer` objects that were created from C++ to a parent thread. Fixes: #34126 PR-URL: #34140 Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent 9c98af7 commit a38219f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 { Worker } = require('worker_threads');
8+
const fixturesPath = require.resolve('../common/fixtures');
9+
10+
// Test that transferring the result of e.g. crypto.sign() from Worker to parent
11+
// thread does not crash
12+
13+
const w = new Worker(`
14+
const { parentPort } = require('worker_threads');
15+
const crypto = require('crypto');
16+
const assert = require('assert');
17+
const fixtures = require(${JSON.stringify(fixturesPath)});
18+
19+
const keyPem = fixtures.readKey('rsa_private.pem');
20+
21+
const buf = crypto.sign('sha256', Buffer.from('hello'), keyPem);
22+
assert.notStrictEqual(buf.byteLength, 0);
23+
parentPort.postMessage(buf, [buf.buffer]);
24+
assert.strictEqual(buf.byteLength, 0);
25+
`, { eval: true });
26+
27+
w.on('message', common.mustCall((buf) => {
28+
assert.notStrictEqual(buf.byteLength, 0);
29+
}));
30+
w.on('exit', common.mustCall());

0 commit comments

Comments
 (0)