• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Flags: --expose-internals
3const common = require('../common');
4const assert = require('assert');
5const net = require('net');
6const { normalizedArgsSymbol } = require('internal/net');
7const { getSystemErrorName } = require('util');
8
9function validateNormalizedArgs(input, output) {
10  const args = net._normalizeArgs(input);
11
12  assert.deepStrictEqual(args, output);
13  assert.strictEqual(args[normalizedArgsSymbol], true);
14}
15
16// Test creation of normalized arguments.
17const res = [{}, null];
18res[normalizedArgsSymbol] = true;
19validateNormalizedArgs([], res);
20res[0].port = 1234;
21validateNormalizedArgs([{ port: 1234 }], res);
22res[1] = assert.fail;
23validateNormalizedArgs([{ port: 1234 }, assert.fail], res);
24
25// Connecting to the server should fail with a standard array.
26{
27  const server = net.createServer(common.mustNotCall('should not connect'));
28
29  server.listen(common.mustCall(() => {
30    const possibleErrors = ['ECONNREFUSED', 'EADDRNOTAVAIL'];
31    const port = server.address().port;
32    const socket = new net.Socket();
33
34    socket.on('error', common.mustCall((err) => {
35      assert(possibleErrors.includes(err.code));
36      assert(possibleErrors.includes(getSystemErrorName(err.errno)));
37      assert.strictEqual(err.syscall, 'connect');
38      server.close();
39    }));
40
41    socket.connect([{ port }, assert.fail]);
42  }));
43}
44
45// Connecting to the server should succeed with a normalized array.
46{
47  const server = net.createServer(common.mustCall((connection) => {
48    connection.end();
49    server.close();
50  }));
51
52  server.listen(common.mustCall(() => {
53    const port = server.address().port;
54    const socket = new net.Socket();
55    const args = net._normalizeArgs([{ port }, common.mustCall()]);
56
57    socket.connect(args);
58  }));
59}
60