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 assert = require('assert'); 8const dgram = require('dgram'); 9const { internalBinding } = require('internal/test/binding'); 10const { UDP } = internalBinding('udp_wrap'); 11const { TCP, constants } = internalBinding('tcp_wrap'); 12const _createSocketHandle = dgram._createSocketHandle; 13 14// Return a negative number if the "existing fd" is invalid. 15{ 16 const err = _createSocketHandle(common.localhostIPv4, 0, 'udp4', 42); 17 assert(err < 0); 18} 19 20// Return a negative number if the type of fd is not "UDP". 21{ 22 // Create a handle with fd. 23 const rawHandle = new UDP(); 24 const err = rawHandle.bind(common.localhostIPv4, 0, 0); 25 assert(err >= 0, String(err)); 26 assert.notStrictEqual(rawHandle.fd, -1); 27 28 const handle = _createSocketHandle(null, 0, 'udp4', rawHandle.fd); 29 assert(handle instanceof UDP); 30 assert.strictEqual(typeof handle.fd, 'number'); 31 assert(handle.fd > 0); 32} 33 34// Create a bound handle. 35{ 36 const rawHandle = new TCP(constants.SOCKET); 37 const err = rawHandle.listen(); 38 assert(err >= 0, String(err)); 39 assert.notStrictEqual(rawHandle.fd, -1); 40 41 const handle = _createSocketHandle(null, 0, 'udp4', rawHandle.fd); 42 assert(handle < 0); 43 rawHandle.close(); 44} 45