1// Flags: --expose-internals
2'use strict';
3const common = require('../common');
4if (common.isWindows)
5  common.skip('Does not support binding fd on Windows');
6
7const dgram = require('dgram');
8const assert = require('assert');
9const { kStateSymbol } = require('internal/dgram');
10const { internalBinding } = require('internal/test/binding');
11const { TCP, constants } = internalBinding('tcp_wrap');
12const TYPE = 'udp4';
13
14// Throw when the fd is occupied according to https://github.com/libuv/libuv/pull/1851.
15{
16  const socket = dgram.createSocket(TYPE);
17
18  socket.bind(common.mustCall(() => {
19    const anotherSocket = dgram.createSocket(TYPE);
20    const { handle } = socket[kStateSymbol];
21
22    assert.throws(() => {
23      anotherSocket.bind({
24        fd: handle.fd,
25      });
26    }, {
27      code: 'EEXIST',
28      name: 'Error',
29      message: /^open EEXIST$/
30    });
31
32    socket.close();
33  }));
34}
35
36// Throw when the type of fd is not "UDP".
37{
38  const handle = new TCP(constants.SOCKET);
39  handle.listen();
40
41  const fd = handle.fd;
42  assert.notStrictEqual(fd, -1);
43
44  const socket = new dgram.createSocket(TYPE);
45  assert.throws(() => {
46    socket.bind({
47      fd,
48    });
49  }, {
50    code: 'ERR_INVALID_FD_TYPE',
51    name: 'TypeError',
52    message: /^Unsupported fd type: TCP$/
53  });
54
55  handle.close();
56}
57