Skip to content

Commit a1426e7

Browse files
committed
add more tests around custom sockets
1 parent 27193ac commit a1426e7

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/node/internal/internal_net.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,10 @@ async function startRead(socket: Socket): Promise<void> {
12011201
break;
12021202
}
12031203
}
1204+
} catch (err) {
1205+
// Ignore error, and don't log them.
1206+
// This is mostly triggered for invalid sockets with following errors:
1207+
// - "This ReadableStream belongs to an object that is closing."
12041208
} finally {
12051209
// Disable eslint to match Node.js behavior
12061210
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

src/workerd/api/node/tests/tls-nodejs-tcp-server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ const echoServer = tls.createServer(options, (s) => {
7272
s.pipe(s);
7373
});
7474
echoServer.listen(8888, () => console.info('Listening on port 8888'));
75+
76+
// Taken from test-tls-connect-given-socket.js
77+
const helloServer = tls.createServer(options, (socket) => {
78+
socket.end('Hello');
79+
});
80+
helloServer.listen(8887, () => console.info('Listening on port 8887'));

src/workerd/api/node/tests/tls-nodejs-test.js

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2424
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2525
import tls from 'node:tls';
26-
import { strictEqual, ok, rejects } from 'node:assert';
26+
import { strictEqual, ok, rejects, throws } from 'node:assert';
2727
import { once } from 'node:events';
28+
import net from 'node:net';
2829

2930
// Tests are taken from
3031
// https://github.com/nodejs/node/blob/304743655d5236c2edc39094336ee2667600b684/test/parallel/test-tls-connect-abort-controller.js
@@ -209,3 +210,75 @@ export const tlsConnectNoHost = {
209210
await promise;
210211
},
211212
};
213+
214+
// Tests are taken from
215+
// https://github.com/nodejs/node/blob/755e4603fd1679de72d250514ea5096b272ae8d6/test/parallel/test-tls-connect-given-socket.js
216+
export const tlsConnectGivenSocket = {
217+
async test() {
218+
const promises = [];
219+
let waiting = 2;
220+
function establish(socket, shouldNotCallCallback = false) {
221+
const { promise, resolve } = Promise.withResolvers();
222+
promises.push(promise);
223+
const client = tls.connect(
224+
{
225+
socket: socket,
226+
},
227+
() => {
228+
if (shouldNotCallCallback) {
229+
reject(new Error('should not have called tls.connect() callback'));
230+
return;
231+
}
232+
let data = '';
233+
client
234+
.on('data', (chunk) => {
235+
data += chunk.toString();
236+
})
237+
.on('end', () => {
238+
strictEqual(data, 'Hello');
239+
if (--waiting === 0) {
240+
resolve();
241+
}
242+
});
243+
}
244+
);
245+
246+
if (shouldNotCallCallback) {
247+
queueMicrotask(() => resolve());
248+
}
249+
250+
return client;
251+
}
252+
253+
const port = 8887;
254+
// Immediate death socket
255+
const immediateDeath = net.connect(port);
256+
establish(immediateDeath, true).destroy();
257+
258+
// Outliving
259+
const outlivingTCPPromise = Promise.withResolvers();
260+
const outlivingTCP = net.connect(port, () => {
261+
outlivingTLS.destroy();
262+
next();
263+
outlivingTCPPromise.resolve();
264+
});
265+
promises.push(outlivingTCPPromise.promise);
266+
const outlivingTLS = establish(outlivingTCP, true);
267+
268+
function next() {
269+
// Already connected socket
270+
const { promise, resolve } = Promise.withResolvers();
271+
const connected = net.connect(port, () => {
272+
establish(connected);
273+
resolve();
274+
});
275+
promises.push(promise);
276+
277+
// Connecting socket
278+
const connecting = net.connect(port);
279+
establish(connecting);
280+
}
281+
282+
await Promise.all(promises);
283+
},
284+
};

0 commit comments

Comments
 (0)