1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const dgram = require('dgram');
5
6const buf = Buffer.from('test');
7
8const defaultCases = ['', null, undefined];
9
10const onMessage = common.mustSucceed((bytes) => {
11  assert.strictEqual(bytes, buf.length);
12}, defaultCases.length + 1);
13
14const client = dgram.createSocket('udp4').bind(0, () => {
15  const port = client.address().port;
16
17  // Check valid addresses
18  defaultCases.forEach((address) => {
19    client.send(buf, port, address, onMessage);
20  });
21
22  // Valid address: not provided
23  client.send(buf, port, onMessage);
24
25  // Check invalid addresses
26  [
27    [],
28    0,
29    1,
30    true,
31    false,
32    0n,
33    1n,
34    {},
35    Symbol(),
36  ].forEach((invalidInput) => {
37    const expectedError = {
38      code: 'ERR_INVALID_ARG_TYPE',
39      name: 'TypeError',
40      message: 'The "address" argument must be of type string.' +
41               `${common.invalidArgTypeHelper(invalidInput)}`
42    };
43    assert.throws(() => client.send(buf, port, invalidInput), expectedError);
44  });
45});
46
47client.unref();
48