1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const dgram = require('dgram'); 26 27const buf = Buffer.from('test'); 28const host = '127.0.0.1'; 29const sock = dgram.createSocket('udp4'); 30 31function checkArgs(connected) { 32 // First argument should be a buffer. 33 assert.throws( 34 () => { sock.send(); }, 35 { 36 code: 'ERR_INVALID_ARG_TYPE', 37 name: 'TypeError', 38 message: 'The "buffer" argument must be of type string or an instance ' + 39 'of Buffer, TypedArray, or DataView. Received undefined' 40 } 41 ); 42 43 // send(buf, offset, length, port, host) 44 if (connected) { 45 assert.throws( 46 () => { sock.send(buf, 1, 1, -1, host); }, 47 { 48 code: 'ERR_SOCKET_DGRAM_IS_CONNECTED', 49 name: 'Error', 50 message: 'Already connected' 51 } 52 ); 53 54 assert.throws( 55 () => { sock.send(buf, 1, 1, 0, host); }, 56 { 57 code: 'ERR_SOCKET_DGRAM_IS_CONNECTED', 58 name: 'Error', 59 message: 'Already connected' 60 } 61 ); 62 63 assert.throws( 64 () => { sock.send(buf, 1, 1, 65536, host); }, 65 { 66 code: 'ERR_SOCKET_DGRAM_IS_CONNECTED', 67 name: 'Error', 68 message: 'Already connected' 69 } 70 ); 71 72 assert.throws( 73 () => { sock.send(buf, 1234, '127.0.0.1', common.mustNotCall()); }, 74 { 75 code: 'ERR_SOCKET_DGRAM_IS_CONNECTED', 76 name: 'Error', 77 message: 'Already connected' 78 } 79 ); 80 } else { 81 assert.throws(() => { sock.send(buf, 1, 1, -1, host); }, RangeError); 82 assert.throws(() => { sock.send(buf, 1, 1, 0, host); }, RangeError); 83 assert.throws(() => { sock.send(buf, 1, 1, 65536, host); }, RangeError); 84 } 85 86 // send(buf, port, host) 87 assert.throws( 88 () => { sock.send(23, 12345, host); }, 89 { 90 code: 'ERR_INVALID_ARG_TYPE', 91 name: 'TypeError', 92 message: 'The "buffer" argument must be of type string or an instance ' + 93 'of Buffer, TypedArray, or DataView. Received type number (23)' 94 } 95 ); 96 97 // send([buf1, ..], port, host) 98 assert.throws( 99 () => { sock.send([buf, 23], 12345, host); }, 100 { 101 code: 'ERR_INVALID_ARG_TYPE', 102 name: 'TypeError', 103 message: 'The "buffer list arguments" argument must be of type string ' + 104 'or an instance of Buffer, TypedArray, or DataView. ' + 105 'Received an instance of Array' 106 } 107 ); 108} 109 110checkArgs(); 111sock.connect(12345, common.mustCall(() => { 112 checkArgs(true); 113 sock.close(); 114})); 115