node/test/parallel/test-dgram-createSocket-type.js
Claudio Rodriguez e9b6fbbf17 dgram: prefer strict equality, type validation
- Enforces strict comparisons in dgram - bindState should
always be strictly equal to one of the defined constant states,
and newHandle type is a string.

- Check that the argument `type` in createSocket is not null
when it is of type 'object', before using its `type` property.

- Adds a test to check dgram.createSocket is properly
validating its `type` argument.

PR-URL: https://github.com/nodejs/node/pull/8011
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
2016-09-08 22:21:31 +01:00

37 lines
679 B
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const dgram = require('dgram');
const invalidTypes = [
'test',
['udp4'],
new String('udp4'),
1,
{},
true,
false,
null,
undefined
];
const validTypes = [
'udp4',
'udp6',
{ type: 'udp4' },
{ type: 'udp6' }
];
// Error must be thrown with invalid types
invalidTypes.forEach((invalidType) => {
assert.throws(() => {
dgram.createSocket(invalidType);
}, /Bad socket type specified/);
});
// Error must not be thrown with valid types
validTypes.forEach((validType) => {
assert.doesNotThrow(() => {
const socket = dgram.createSocket(validType);
socket.close();
});
});