summaryrefslogtreecommitdiff
path: root/test/parallel/test-dgram-create-socket-handle.js
blob: 57480f7d2cfaa6a69c44e8a44977f44e36da9a5b (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
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const UDP = process.binding('udp_wrap').UDP;
const _createSocketHandle = dgram._createSocketHandle;

// Throws if an "existing fd" is passed in.
common.expectsError(() => {
  _createSocketHandle(common.localhostIPv4, 0, 'udp4', 42);
}, {
  code: 'ERR_ASSERTION',
  message: /^false == true$/
});

{
  // Create a handle that is not bound.
  const handle = _createSocketHandle(null, null, 'udp4');

  assert(handle instanceof UDP);
  assert.strictEqual(typeof handle.fd, 'number');
  assert(handle.fd < 0);
}

{
  // Create a bound handle.
  const handle = _createSocketHandle(common.localhostIPv4, 0, 'udp4');

  assert(handle instanceof UDP);
  assert.strictEqual(typeof handle.fd, 'number');

  if (!common.isWindows)
    assert(handle.fd > 0);
}

{
  // Return an error if binding fails.
  const err = _createSocketHandle('localhost', 0, 'udp4');

  assert.strictEqual(typeof err, 'number');
  assert(err < 0);
}