11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (common.isWindows)
51cb0ef41Sopenharmony_ci  common.skip('Does not support binding fd on Windows');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst dgram = require('dgram');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { kStateSymbol } = require('internal/dgram');
101cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
111cb0ef41Sopenharmony_ciconst { TCP, constants } = internalBinding('tcp_wrap');
121cb0ef41Sopenharmony_ciconst TYPE = 'udp4';
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// Throw when the fd is occupied according to https://github.com/libuv/libuv/pull/1851.
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  const socket = dgram.createSocket(TYPE);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  socket.bind(common.mustCall(() => {
191cb0ef41Sopenharmony_ci    const anotherSocket = dgram.createSocket(TYPE);
201cb0ef41Sopenharmony_ci    const { handle } = socket[kStateSymbol];
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci    assert.throws(() => {
231cb0ef41Sopenharmony_ci      anotherSocket.bind({
241cb0ef41Sopenharmony_ci        fd: handle.fd,
251cb0ef41Sopenharmony_ci      });
261cb0ef41Sopenharmony_ci    }, {
271cb0ef41Sopenharmony_ci      code: 'EEXIST',
281cb0ef41Sopenharmony_ci      name: 'Error',
291cb0ef41Sopenharmony_ci      message: /^open EEXIST$/
301cb0ef41Sopenharmony_ci    });
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    socket.close();
331cb0ef41Sopenharmony_ci  }));
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// Throw when the type of fd is not "UDP".
371cb0ef41Sopenharmony_ci{
381cb0ef41Sopenharmony_ci  const handle = new TCP(constants.SOCKET);
391cb0ef41Sopenharmony_ci  handle.listen();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const fd = handle.fd;
421cb0ef41Sopenharmony_ci  assert.notStrictEqual(fd, -1);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  const socket = new dgram.createSocket(TYPE);
451cb0ef41Sopenharmony_ci  assert.throws(() => {
461cb0ef41Sopenharmony_ci    socket.bind({
471cb0ef41Sopenharmony_ci      fd,
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci  }, {
501cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_FD_TYPE',
511cb0ef41Sopenharmony_ci    name: 'TypeError',
521cb0ef41Sopenharmony_ci    message: /^Unsupported fd type: TCP$/
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  handle.close();
561cb0ef41Sopenharmony_ci}
57