• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25const net = require('net');
26
27assert.strictEqual(net.isIP('127.0.0.1'), 4);
28assert.strictEqual(net.isIP('x127.0.0.1'), 0);
29assert.strictEqual(net.isIP('example.com'), 0);
30assert.strictEqual(net.isIP('0000:0000:0000:0000:0000:0000:0000:0000'), 6);
31assert.strictEqual(net.isIP('0000:0000:0000:0000:0000:0000:0000:0000::0000'),
32                   0);
33assert.strictEqual(net.isIP('1050:0:0:0:5:600:300c:326b'), 6);
34assert.strictEqual(net.isIP('2001:252:0:1::2008:6'), 6);
35assert.strictEqual(net.isIP('2001:dead:beef:1::2008:6'), 6);
36assert.strictEqual(net.isIP('2001::'), 6);
37assert.strictEqual(net.isIP('2001:dead::'), 6);
38assert.strictEqual(net.isIP('2001:dead:beef::'), 6);
39assert.strictEqual(net.isIP('2001:dead:beef:1::'), 6);
40assert.strictEqual(net.isIP('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 6);
41assert.strictEqual(net.isIP(':2001:252:0:1::2008:6:'), 0);
42assert.strictEqual(net.isIP(':2001:252:0:1::2008:6'), 0);
43assert.strictEqual(net.isIP('2001:252:0:1::2008:6:'), 0);
44assert.strictEqual(net.isIP('2001:252::1::2008:6'), 0);
45assert.strictEqual(net.isIP('::2001:252:1:2008:6'), 6);
46assert.strictEqual(net.isIP('::2001:252:1:1.1.1.1'), 6);
47assert.strictEqual(net.isIP('::2001:252:1:255.255.255.255'), 6);
48assert.strictEqual(net.isIP('::2001:252:1:255.255.255.255.76'), 0);
49assert.strictEqual(net.isIP('fe80::2008%eth0'), 6);
50assert.strictEqual(net.isIP('fe80::2008%eth0.0'), 6);
51assert.strictEqual(net.isIP('fe80::2008%eth0@1'), 0);
52assert.strictEqual(net.isIP('::anything'), 0);
53assert.strictEqual(net.isIP('::1'), 6);
54assert.strictEqual(net.isIP('::'), 6);
55assert.strictEqual(net.isIP('0000:0000:0000:0000:0000:0000:12345:0000'), 0);
56assert.strictEqual(net.isIP('0'), 0);
57assert.strictEqual(net.isIP(), 0);
58assert.strictEqual(net.isIP(''), 0);
59assert.strictEqual(net.isIP(null), 0);
60assert.strictEqual(net.isIP(123), 0);
61assert.strictEqual(net.isIP(true), 0);
62assert.strictEqual(net.isIP({}), 0);
63assert.strictEqual(net.isIP({ toString: () => '::2001:252:1:255.255.255.255' }),
64                   6);
65assert.strictEqual(net.isIP({ toString: () => '127.0.0.1' }), 4);
66assert.strictEqual(net.isIP({ toString: () => 'bla' }), 0);
67
68assert.strictEqual(net.isIPv4('127.0.0.1'), true);
69assert.strictEqual(net.isIPv4('example.com'), false);
70assert.strictEqual(net.isIPv4('2001:252:0:1::2008:6'), false);
71assert.strictEqual(net.isIPv4(), false);
72assert.strictEqual(net.isIPv4(''), false);
73assert.strictEqual(net.isIPv4(null), false);
74assert.strictEqual(net.isIPv4(123), false);
75assert.strictEqual(net.isIPv4(true), false);
76assert.strictEqual(net.isIPv4({}), false);
77assert.strictEqual(net.isIPv4({
78  toString: () => '::2001:252:1:255.255.255.255'
79}), false);
80assert.strictEqual(net.isIPv4({ toString: () => '127.0.0.1' }), true);
81assert.strictEqual(net.isIPv4({ toString: () => 'bla' }), false);
82
83assert.strictEqual(net.isIPv6('127.0.0.1'), false);
84assert.strictEqual(net.isIPv6('example.com'), false);
85assert.strictEqual(net.isIPv6('2001:252:0:1::2008:6'), true);
86assert.strictEqual(net.isIPv6(), false);
87assert.strictEqual(net.isIPv6(''), false);
88assert.strictEqual(net.isIPv6(null), false);
89assert.strictEqual(net.isIPv6(123), false);
90assert.strictEqual(net.isIPv6(true), false);
91assert.strictEqual(net.isIPv6({}), false);
92assert.strictEqual(net.isIPv6({
93  toString: () => '::2001:252:1:255.255.255.255'
94}), true);
95assert.strictEqual(net.isIPv6({ toString: () => '127.0.0.1' }), false);
96assert.strictEqual(net.isIPv6({ toString: () => 'bla' }), false);
97