From f451ddb91045f9ec6365aea771ece043e8408ca0 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Wed, 21 May 2025 15:18:36 -0700 Subject: [PATCH] more and break more --- .../parallel/test-http2-large-write-close.js | 2 +- .../parallel/test-http2-pipe-named-pipe.js | 1 - .../parallel/test-http2-premature-close.js | 89 +++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 test/js/node/test/parallel/test-http2-premature-close.js diff --git a/test/js/node/test/parallel/test-http2-large-write-close.js b/test/js/node/test/parallel/test-http2-large-write-close.js index 3761ebe305..ff12ffcfed 100644 --- a/test/js/node/test/parallel/test-http2-large-write-close.js +++ b/test/js/node/test/parallel/test-http2-large-write-close.js @@ -28,7 +28,7 @@ server.on('stream', common.mustCall((stream) => { })); server.listen(0, common.mustCall(() => { - const client = http2.connect(`https://localhost:${server.address().port}`, + const client = http2.connect(`https://127.0.0.1:${server.address().port}`, { rejectUnauthorized: false }); const req = client.request({ ':path': '/' }); diff --git a/test/js/node/test/parallel/test-http2-pipe-named-pipe.js b/test/js/node/test/parallel/test-http2-pipe-named-pipe.js index eb9b1b568c..027893b9f8 100644 --- a/test/js/node/test/parallel/test-http2-pipe-named-pipe.js +++ b/test/js/node/test/parallel/test-http2-pipe-named-pipe.js @@ -31,7 +31,6 @@ server.on('stream', common.mustCall((stream) => { fs.readFileSync(loc).length); })); })); - server.listen(common.PIPE, common.mustCall(() => { const client = http2.connect('http://localhost', { createConnection(url) { diff --git a/test/js/node/test/parallel/test-http2-premature-close.js b/test/js/node/test/parallel/test-http2-premature-close.js new file mode 100644 index 0000000000..d2582de3ab --- /dev/null +++ b/test/js/node/test/parallel/test-http2-premature-close.js @@ -0,0 +1,89 @@ +// Flags: --expose-internals +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) common.skip('missing crypto'); + +const h2 = require('http2'); +const net = require('net'); + +async function requestAndClose(server) { + const client = new net.Socket(); + + const address = server.address(); + if (!common.hasIPv6 && address.family === 'IPv6') { + // Necessary to pass CI running inside containers. + client.connect(address.port); + } else { + client.connect(address); + } + + client.on('connect', common.mustCall(function() { + // Send HTTP/2 Preface + client.write(Buffer.from('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n', 'utf8')); + + // Send a SETTINGS frame (empty payload) + client.write(Buffer.from([0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00])); + + const streamId = 1; + // Send a valid HEADERS frame + const headersFrame = Buffer.concat([ + Buffer.from([ + 0x00, 0x00, 0x0e, // Length: 14 bytes + 0x01, // Type: HEADERS + 0x04, // Flags: END_HEADERS + (streamId >> 24) & 0xFF, // Stream ID: high byte + (streamId >> 16) & 0xFF, + (streamId >> 8) & 0xFF, + streamId & 0xFF, // Stream ID: low byte + ]), + Buffer.from([ + 0x82, // Indexed Header Field Representation (Predefined ":method: GET") + 0x84, // Indexed Header Field Representation (Predefined ":path: /") + 0x86, // Indexed Header Field Representation (Predefined ":scheme: http") + 0x41, 0x09, // ":authority: localhost" Length: 9 bytes + 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, + ]), + ]); + client.write(headersFrame); + + // Send a valid DATA frame + const dataFrame = Buffer.concat([ + Buffer.from([ + 0x00, 0x00, 0x05, // Length: 5 bytes + 0x00, // Type: DATA + 0x00, // Flags: No flags + (streamId >> 24) & 0xFF, // Stream ID: high byte + (streamId >> 16) & 0xFF, + (streamId >> 8) & 0xFF, + streamId & 0xFF, // Stream ID: low byte + ]), + Buffer.from('Hello', 'utf8'), // Data payload + ]); + client.write(dataFrame); + + // Does not wait for server to reply. Shutdown the socket + client.end(); + })); +} + +const server = h2.createServer(); + +server.on('error', common.mustNotCall()); + +server.on( + 'session', + common.mustCall((session) => { + session.on('close', common.mustCall(() => { + server.close(); + })); + }), +); + +server.listen( + 0, + "127.0.0.1", + common.mustCall(async () => { + await requestAndClose(server); + }), +);