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 assert = require('assert');
81cb0ef41Sopenharmony_ciconst dgram = require('dgram');
91cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
101cb0ef41Sopenharmony_ciconst { UDP } = internalBinding('udp_wrap');
111cb0ef41Sopenharmony_ciconst { TCP, constants } = internalBinding('tcp_wrap');
121cb0ef41Sopenharmony_ciconst _createSocketHandle = dgram._createSocketHandle;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// Return a negative number if the "existing fd" is invalid.
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  const err = _createSocketHandle(common.localhostIPv4, 0, 'udp4', 42);
171cb0ef41Sopenharmony_ci  assert(err < 0);
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// Return a negative number if the type of fd is not "UDP".
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  // Create a handle with fd.
231cb0ef41Sopenharmony_ci  const rawHandle = new UDP();
241cb0ef41Sopenharmony_ci  const err = rawHandle.bind(common.localhostIPv4, 0, 0);
251cb0ef41Sopenharmony_ci  assert(err >= 0, String(err));
261cb0ef41Sopenharmony_ci  assert.notStrictEqual(rawHandle.fd, -1);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const handle = _createSocketHandle(null, 0, 'udp4', rawHandle.fd);
291cb0ef41Sopenharmony_ci  assert(handle instanceof UDP);
301cb0ef41Sopenharmony_ci  assert.strictEqual(typeof handle.fd, 'number');
311cb0ef41Sopenharmony_ci  assert(handle.fd > 0);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// Create a bound handle.
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci  const rawHandle = new TCP(constants.SOCKET);
371cb0ef41Sopenharmony_ci  const err = rawHandle.listen();
381cb0ef41Sopenharmony_ci  assert(err >= 0, String(err));
391cb0ef41Sopenharmony_ci  assert.notStrictEqual(rawHandle.fd, -1);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const handle = _createSocketHandle(null, 0, 'udp4', rawHandle.fd);
421cb0ef41Sopenharmony_ci  assert(handle < 0);
431cb0ef41Sopenharmony_ci  rawHandle.close();
441cb0ef41Sopenharmony_ci}
45