Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test-dgram-address.js #11271

Merged
merged 1 commit into from
Feb 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 55 additions & 42 deletions test/parallel/test-dgram-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,58 @@ const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');

// IPv4 Test
const socket_ipv4 = dgram.createSocket('udp4');
const family_ipv4 = 'IPv4';

socket_ipv4.on('listening', function() {
const address_ipv4 = socket_ipv4.address();
assert.strictEqual(address_ipv4.address, common.localhostIPv4);
assert.strictEqual(typeof address_ipv4.port, 'number');
assert.ok(isFinite(address_ipv4.port));
assert.ok(address_ipv4.port > 0);
assert.strictEqual(address_ipv4.family, family_ipv4);
socket_ipv4.close();
});

socket_ipv4.on('error', function(e) {
console.log('Error on udp4 socket. ' + e.toString());
socket_ipv4.close();
});

socket_ipv4.bind(0, common.localhostIPv4);

// IPv6 Test
const localhost_ipv6 = '::1';
const socket_ipv6 = dgram.createSocket('udp6');
const family_ipv6 = 'IPv6';

socket_ipv6.on('listening', function() {
const address_ipv6 = socket_ipv6.address();
assert.strictEqual(address_ipv6.address, localhost_ipv6);
assert.strictEqual(typeof address_ipv6.port, 'number');
assert.ok(isFinite(address_ipv6.port));
assert.ok(address_ipv6.port > 0);
assert.strictEqual(address_ipv6.family, family_ipv6);
socket_ipv6.close();
});

socket_ipv6.on('error', function(e) {
console.log('Error on udp6 socket. ' + e.toString());
socket_ipv6.close();
});

socket_ipv6.bind(0, localhost_ipv6);
{
// IPv4 Test
const socket = dgram.createSocket('udp4');

socket.on('listening', common.mustCall(() => {
const address = socket.address();

assert.strictEqual(address.address, common.localhostIPv4);
assert.strictEqual(typeof address.port, 'number');
assert.ok(isFinite(address.port));
assert.ok(address.port > 0);
assert.strictEqual(address.family, 'IPv4');
socket.close();
}));

socket.on('error', (err) => {
socket.close();
common.fail(`Unexpected error on udp4 socket. ${err.toString()}`);
});

socket.bind(0, common.localhostIPv4);
}

{
// IPv6 Test
const socket = dgram.createSocket('udp6');
const localhost = '::1';

socket.on('listening', common.mustCall(() => {
const address = socket.address();

assert.strictEqual(address.address, localhost);
assert.strictEqual(typeof address.port, 'number');
assert.ok(isFinite(address.port));
assert.ok(address.port > 0);
assert.strictEqual(address.family, 'IPv6');
socket.close();
}));

socket.on('error', (err) => {
socket.close();
common.fail(`Unexpected error on udp6 socket. ${err.toString()}`);
});

socket.bind(0, localhost);
}

{
// Verify that address() throws if the socket is not bound.
const socket = dgram.createSocket('udp4');

assert.throws(() => {
socket.address();
}, /^Error: getsockname EINVAL$/);
}