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: move the corresponding IPv4 and IPv6 tests #28124

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 32 additions & 66 deletions test/parallel/test-net-isip.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,36 @@ require('../common');
const assert = require('assert');
const net = require('net');

assert.strictEqual(net.isIP('127.0.0.1'), 4);
assert.strictEqual(net.isIP('x127.0.0.1'), 0);
assert.strictEqual(net.isIP('example.com'), 0);
assert.strictEqual(net.isIP('0000:0000:0000:0000:0000:0000:0000:0000'), 6);
assert.strictEqual(net.isIP('0000:0000:0000:0000:0000:0000:0000:0000::0000'),
0);
assert.strictEqual(net.isIP('1050:0:0:0:5:600:300c:326b'), 6);
assert.strictEqual(net.isIP('2001:252:0:1::2008:6'), 6);
assert.strictEqual(net.isIP('2001:dead:beef:1::2008:6'), 6);
assert.strictEqual(net.isIP('2001::'), 6);
assert.strictEqual(net.isIP('2001:dead::'), 6);
assert.strictEqual(net.isIP('2001:dead:beef::'), 6);
assert.strictEqual(net.isIP('2001:dead:beef:1::'), 6);
assert.strictEqual(net.isIP('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 6);
assert.strictEqual(net.isIP(':2001:252:0:1::2008:6:'), 0);
assert.strictEqual(net.isIP(':2001:252:0:1::2008:6'), 0);
assert.strictEqual(net.isIP('2001:252:0:1::2008:6:'), 0);
assert.strictEqual(net.isIP('2001:252::1::2008:6'), 0);
assert.strictEqual(net.isIP('::2001:252:1:2008:6'), 6);
assert.strictEqual(net.isIP('::2001:252:1:1.1.1.1'), 6);
assert.strictEqual(net.isIP('::2001:252:1:255.255.255.255'), 6);
assert.strictEqual(net.isIP('::2001:252:1:255.255.255.255.76'), 0);
assert.strictEqual(net.isIP('::anything'), 0);
assert.strictEqual(net.isIP('::1'), 6);
assert.strictEqual(net.isIP('::'), 6);
assert.strictEqual(net.isIP('0000:0000:0000:0000:0000:0000:12345:0000'), 0);
assert.strictEqual(net.isIP('0'), 0);
assert.strictEqual(net.isIP(), 0);
assert.strictEqual(net.isIP(''), 0);
assert.strictEqual(net.isIP(null), 0);
assert.strictEqual(net.isIP(123), 0);
assert.strictEqual(net.isIP(true), 0);
assert.strictEqual(net.isIP({}), 0);
assert.strictEqual(net.isIP({ toString: () => '::2001:252:1:255.255.255.255' }),
6);
assert.strictEqual(net.isIP({ toString: () => '127.0.0.1' }), 4);
assert.strictEqual(net.isIP({ toString: () => 'bla' }), 0);
const ip = [
['127.0.0.1', 4],
['8.8.8.8', 4],
['x127.0.0.1', 0],
['example.com', 0],
['0000:0000:0000:0000:0000:0000:0000:0000', 6],
['0000:0000:0000:0000:0000:0000:0000:0000::0000', 0],
['1050:0:0:0:5:600:300c:326b', 6],
['2001:252:0:1::2008:6', 6],
['2001::', 6],
['2001:dead::', 6],
['2001:dead:beef::', 6],
['2001:dead:beef:1::', 6],
['ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 6],
[':2001:252:0:1::2008:6:', 0],
[':2001:252:0:1::2008:6', 0],
['2001:252:0:1::2008:6:', 0],
['2001:252::1::2008:6', 0],
['::2001:252:1:2008:6', 6],
['::2001:252:1:1.1.1.1', 6],
['::2001:252:1:255.255.255.255', 6],
['::2001:252:1:255.255.255.255.76', 0],
['::anything', 0],
['::1', 6],
['::', 6],
['0000:0000:0000:0000:0000:0000:12345:0000', 0],
['0', 0]
];

assert.strictEqual(net.isIPv4('127.0.0.1'), true);
assert.strictEqual(net.isIPv4('example.com'), false);
assert.strictEqual(net.isIPv4('2001:252:0:1::2008:6'), false);
assert.strictEqual(net.isIPv4(), false);
assert.strictEqual(net.isIPv4(''), false);
assert.strictEqual(net.isIPv4(null), false);
assert.strictEqual(net.isIPv4(123), false);
assert.strictEqual(net.isIPv4(true), false);
assert.strictEqual(net.isIPv4({}), false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cases are not tested in the other file. Is there a specific reason for removing them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About this, I think isIPxx since it only supports string input, testing non-string input is an unnecessary test, don't get useful feedback in run the test case.

I'm not sure if it should do this kind of testing in node, imo, It is up to the user to make a simple assertion before using it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not certain I can follow. We verify that the API works as expected and to have a full coverage.

assert.strictEqual(net.isIPv4({
toString: () => '::2001:252:1:255.255.255.255'
}), false);
assert.strictEqual(net.isIPv4({ toString: () => '127.0.0.1' }), true);
assert.strictEqual(net.isIPv4({ toString: () => 'bla' }), false);

assert.strictEqual(net.isIPv6('127.0.0.1'), false);
assert.strictEqual(net.isIPv6('example.com'), false);
assert.strictEqual(net.isIPv6('2001:252:0:1::2008:6'), true);
assert.strictEqual(net.isIPv6(), false);
assert.strictEqual(net.isIPv6(''), false);
assert.strictEqual(net.isIPv6(null), false);
assert.strictEqual(net.isIPv6(123), false);
assert.strictEqual(net.isIPv6(true), false);
assert.strictEqual(net.isIPv6({}), false);
assert.strictEqual(net.isIPv6({
toString: () => '::2001:252:1:255.255.255.255'
}), true);
assert.strictEqual(net.isIPv6({ toString: () => '127.0.0.1' }), false);
assert.strictEqual(net.isIPv6({ toString: () => 'bla' }), false);
ip.forEach(([ip, version]) => {
assert.strictEqual(net.isIP(ip), version);
assert.strictEqual(net.isIP({ toString: () => ip }), version);
});
2 changes: 2 additions & 0 deletions test/parallel/test-net-isipv4.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ const v4not = [

v4.forEach((ip) => {
assert.strictEqual(net.isIPv4(ip), true);
assert.strictEqual(net.isIPv4({ toString: () => ip }), true);
});

v4not.forEach((ip) => {
assert.strictEqual(net.isIPv4(ip), false);
assert.strictEqual(net.isIPv4({ toString: () => ip }), false);
});
2 changes: 2 additions & 0 deletions test/parallel/test-net-isipv6.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ const v6not = [

v6.forEach((ip) => {
assert.strictEqual(net.isIPv6(ip), true);
assert.strictEqual(net.isIPv6({ toString: () => ip }), true);
});

v6not.forEach((ip) => {
assert.strictEqual(net.isIPv6(ip), false);
assert.strictEqual(net.isIPv6({ toString: () => ip }), false);
});