summaryrefslogtreecommitdiff
path: root/test/parallel/test-dgram-create-socket-handle-fd.js
blob: 93175859c196ea179e3cd9b7a98a949393c01fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Flags: --expose-internals
'use strict';
const common = require('../common');
if (common.isWindows)
  common.skip('Does not support binding fd on Windows');

const assert = require('assert');
const dgram = require('dgram');
const { internalBinding } = require('internal/test/binding');
const { UDP } = internalBinding('udp_wrap');
const { TCP, constants } = internalBinding('tcp_wrap');
const _createSocketHandle = dgram._createSocketHandle;

// Return a negative number if the "existing fd" is invalid.
{
  const err = _createSocketHandle(common.localhostIPv4, 0, 'udp4', 42);
  assert(err < 0);
}

// Return a negative number if the type of fd is not "UDP".
{
  // Create a handle with fd.
  const rawHandle = new UDP();
  const err = rawHandle.bind(common.localhostIPv4, 0, 0);
  assert(err >= 0, String(err));
  assert.notStrictEqual(rawHandle.fd, -1);

  const handle = _createSocketHandle(null, 0, 'udp4', rawHandle.fd);
  assert(handle instanceof UDP);
  assert.strictEqual(typeof handle.fd, 'number');
  assert(handle.fd > 0);
}

// Create a bound handle.
{
  const rawHandle = new TCP(constants.SOCKET);
  const err = rawHandle.listen();
  assert(err >= 0, String(err));
  assert.notStrictEqual(rawHandle.fd, -1);

  const handle = _createSocketHandle(null, 0, 'udp4', rawHandle.fd);
  assert(handle < 0);
  rawHandle.close();
}