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
81    const longArray = [1, 2, 3, 4, 5, 6, 7, 8];
82    for (const input of ['hello',
83                         Buffer.from('hello'),
84                         Buffer.from('hello world').subarray(0, 5),
85                         Buffer.from('hello world').subarray(4, 9),
86                         Buffer.from('hello world').subarray(6),
87                         new Uint8Array([1, 2, 3, 4, 5]),
88                         new Uint8Array(longArray).subarray(0, 5),
89                         new Uint8Array(longArray).subarray(2, 7),
90                         new Uint8Array(longArray).subarray(3),
91                         new DataView(new ArrayBuffer(5), 0),
92                         new DataView(new ArrayBuffer(6), 1),
93                         new DataView(new ArrayBuffer(7), 1, 5)]) {
94      assert.throws(
95        () => { sock.send(input, 6, 0); },
96        {
97          code: 'ERR_BUFFER_OUT_OF_BOUNDS',
98          name: 'RangeError',
99          message: '"offset" is outside of buffer bounds',
100        }
101      );
102
103      assert.throws(
104        () => { sock.send(input, 0, 6); },
105        {
106          code: 'ERR_BUFFER_OUT_OF_BOUNDS',
107          name: 'RangeError',
108          message: '"length" is outside of buffer bounds',
109        }
110      );
111
112      assert.throws(
113        () => { sock.send(input, 3, 4); },
114        {
115          code: 'ERR_BUFFER_OUT_OF_BOUNDS',
116          name: 'RangeError',
117          message: '"length" is outside of buffer bounds',
118        }
119      );
120    }
121  } else {
122    assert.throws(() => { sock.send(buf, 1, 1, -1, host); }, RangeError);
123    assert.throws(() => { sock.send(buf, 1, 1, 0, host); }, RangeError);
124    assert.throws(() => { sock.send(buf, 1, 1, 65536, host); }, RangeError);
125  }
126
127  // send(buf, port, host)
128  assert.throws(
129    () => { sock.send(23, 12345, host); },
130    {
131      code: 'ERR_INVALID_ARG_TYPE',
132      name: 'TypeError',
133      message: 'The "buffer" argument must be of type string or an instance ' +
134      'of Buffer, TypedArray, or DataView. Received type number (23)'
135    }
136  );
137
138  // send([buf1, ..], port, host)
139  assert.throws(
140    () => { sock.send([buf, 23], 12345, host); },
141    {
142      code: 'ERR_INVALID_ARG_TYPE',
143      name: 'TypeError',
144      message: 'The "buffer list arguments" argument must be of type string ' +
145      'or an instance of Buffer, TypedArray, or DataView. ' +
146      'Received an instance of Array'
147    }
148  );
149}
150
151checkArgs();
152sock.connect(12345, common.mustCall(() => {
153  checkArgs(true);
154  sock.close();
155}));
156