2017-01-12 22:10:26 -08:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const dgram = require('dgram');
|
|
|
|
|
|
|
|
const buf = Buffer.from('test');
|
|
|
|
|
2021-06-29 05:20:00 +04:30
|
|
|
const defaultCases = ['', null, undefined];
|
|
|
|
|
2020-09-06 22:27:07 +02:00
|
|
|
const onMessage = common.mustSucceed((bytes) => {
|
2017-01-12 22:10:26 -08:00
|
|
|
assert.strictEqual(bytes, buf.length);
|
2021-06-29 05:20:00 +04:30
|
|
|
}, defaultCases.length + 1);
|
2017-01-12 22:10:26 -08:00
|
|
|
|
2017-05-12 21:54:01 +00:00
|
|
|
const client = dgram.createSocket('udp4').bind(0, () => {
|
|
|
|
const port = client.address().port;
|
2017-01-12 22:10:26 -08:00
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
// Check valid addresses
|
2021-06-29 05:20:00 +04:30
|
|
|
defaultCases.forEach((address) => {
|
2018-03-19 13:33:46 +01:00
|
|
|
client.send(buf, port, address, onMessage);
|
|
|
|
});
|
2017-01-12 22:10:26 -08:00
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
// Valid address: not provided
|
2017-05-12 21:54:01 +00:00
|
|
|
client.send(buf, port, onMessage);
|
2017-01-12 22:10:26 -08:00
|
|
|
|
2018-03-19 13:33:46 +01:00
|
|
|
// Check invalid addresses
|
2021-06-29 05:20:00 +04:30
|
|
|
[
|
|
|
|
[],
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
0n,
|
|
|
|
1n,
|
|
|
|
{},
|
|
|
|
Symbol(),
|
|
|
|
].forEach((invalidInput) => {
|
2018-03-19 13:33:46 +01:00
|
|
|
const expectedError = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-03-16 12:09:14 +01:00
|
|
|
name: 'TypeError',
|
2021-06-29 05:20:00 +04:30
|
|
|
message: 'The "address" argument must be of type string.' +
|
2019-09-23 08:17:25 +02:00
|
|
|
`${common.invalidArgTypeHelper(invalidInput)}`
|
2018-03-19 13:33:46 +01:00
|
|
|
};
|
|
|
|
assert.throws(() => client.send(buf, port, invalidInput), expectedError);
|
|
|
|
});
|
2017-05-12 21:54:01 +00:00
|
|
|
});
|
2017-01-12 22:10:26 -08:00
|
|
|
|
|
|
|
client.unref();
|