@@ -12,13 +12,45 @@ describe('utils', () => {
12
12
expect ( utils . isIPv4MappedIPv6 ( '::ffff:7f00:800' ) ) . toBe ( true ) ;
13
13
expect ( utils . isIPv4MappedIPv6 ( '::ffff:255.255.255.255' ) ) . toBe ( true ) ;
14
14
} ) ;
15
- test ( 'to and from IPv4 mapped IPv6 addresses' , ( ) => {
16
- expect ( utils . toIPv4MappedIPv6 ( '127.0.0.1' ) ) . toBe ( '::ffff:127.0.0.1' ) ;
17
- expect ( utils . toIPv4MappedIPv6 ( '0.0.0.0' ) ) . toBe ( '::ffff:0.0.0.0' ) ;
18
- expect ( utils . toIPv4MappedIPv6 ( '255.255.255.255' ) ) . toBe (
15
+ test ( 'detect IPv4 mapped IPv6 Dec addresses' , ( ) => {
16
+ expect ( utils . isIPv4MappedIPv6Dec ( '::ffff:127.0.0.1' ) ) . toBe ( true ) ;
17
+ expect ( utils . isIPv4MappedIPv6Dec ( '::ffff:7f00:1' ) ) . toBe ( false ) ;
18
+ expect ( utils . isIPv4MappedIPv6Dec ( '::' ) ) . toBe ( false ) ;
19
+ expect ( utils . isIPv4MappedIPv6Dec ( '::1' ) ) . toBe ( false ) ;
20
+ expect ( utils . isIPv4MappedIPv6Dec ( '127.0.0.1' ) ) . toBe ( false ) ;
21
+ expect ( utils . isIPv4MappedIPv6Dec ( '::ffff:4a7d:2b63' ) ) . toBe ( false ) ;
22
+ expect ( utils . isIPv4MappedIPv6Dec ( '::ffff:7f00:800' ) ) . toBe ( false ) ;
23
+ expect ( utils . isIPv4MappedIPv6Dec ( '::ffff:255.255.255.255' ) ) . toBe ( true ) ;
24
+ } ) ;
25
+ test ( 'detect IPv4 mapped IPv6 Hex addresses' , ( ) => {
26
+ expect ( utils . isIPv4MappedIPv6Hex ( '::ffff:127.0.0.1' ) ) . toBe ( false ) ;
27
+ expect ( utils . isIPv4MappedIPv6Hex ( '::ffff:7f00:1' ) ) . toBe ( true ) ;
28
+ expect ( utils . isIPv4MappedIPv6Hex ( '::' ) ) . toBe ( false ) ;
29
+ expect ( utils . isIPv4MappedIPv6Hex ( '::1' ) ) . toBe ( false ) ;
30
+ expect ( utils . isIPv4MappedIPv6Hex ( '127.0.0.1' ) ) . toBe ( false ) ;
31
+ expect ( utils . isIPv4MappedIPv6Hex ( '::ffff:4a7d:2b63' ) ) . toBe ( true ) ;
32
+ expect ( utils . isIPv4MappedIPv6Hex ( '::ffff:7f00:800' ) ) . toBe ( true ) ;
33
+ expect ( utils . isIPv4MappedIPv6Hex ( '::ffff:255.255.255.255' ) ) . toBe ( false ) ;
34
+ } ) ;
35
+ test ( 'to IPv4 mapped IPv6 addresses Dec' , ( ) => {
36
+ expect ( utils . toIPv4MappedIPv6Dec ( '127.0.0.1' ) ) . toBe ( '::ffff:127.0.0.1' ) ;
37
+ expect ( utils . toIPv4MappedIPv6Dec ( '0.0.0.0' ) ) . toBe ( '::ffff:0.0.0.0' ) ;
38
+ expect ( utils . toIPv4MappedIPv6Dec ( '255.255.255.255' ) ) . toBe (
19
39
'::ffff:255.255.255.255' ,
20
40
) ;
21
- expect ( utils . toIPv4MappedIPv6 ( '74.125.43.99' ) ) . toBe ( '::ffff:74.125.43.99' ) ;
41
+ expect ( utils . toIPv4MappedIPv6Dec ( '74.125.43.99' ) ) . toBe (
42
+ '::ffff:74.125.43.99' ,
43
+ ) ;
44
+ } ) ;
45
+ test ( 'to IPv4 mapped IPv6 addresses Hex' , ( ) => {
46
+ expect ( utils . toIPv4MappedIPv6Hex ( '127.0.0.1' ) ) . toBe ( '::ffff:7f00:1' ) ;
47
+ expect ( utils . toIPv4MappedIPv6Hex ( '0.0.0.0' ) ) . toBe ( '::ffff:0:0' ) ;
48
+ expect ( utils . toIPv4MappedIPv6Hex ( '255.255.255.255' ) ) . toBe (
49
+ '::ffff:ffff:ffff' ,
50
+ ) ;
51
+ expect ( utils . toIPv4MappedIPv6Hex ( '74.125.43.99' ) ) . toBe ( '::ffff:4a7d:2b63' ) ;
52
+ } ) ;
53
+ test ( 'from IPv4 mapped IPv6 addresses' , ( ) => {
22
54
expect ( utils . fromIPv4MappedIPv6 ( '::ffff:7f00:1' ) ) . toBe ( '127.0.0.1' ) ;
23
55
expect ( utils . fromIPv4MappedIPv6 ( '::ffff:127.0.0.1' ) ) . toBe ( '127.0.0.1' ) ;
24
56
expect ( utils . fromIPv4MappedIPv6 ( '::ffff:0.0.0.0' ) ) . toBe ( '0.0.0.0' ) ;
@@ -35,9 +67,33 @@ describe('utils', () => {
35
67
expect ( utils . fromIPv4MappedIPv6 ( '::ffff:0:0' ) ) . toBe ( '0.0.0.0' ) ;
36
68
// Converting from ::ffff:7f00:1 to ::ffff:127.0.0.1
37
69
expect (
38
- utils . toIPv4MappedIPv6 ( utils . fromIPv4MappedIPv6 ( '::ffff:7f00:1' ) ) ,
70
+ utils . toIPv4MappedIPv6Dec ( utils . fromIPv4MappedIPv6 ( '::ffff:7f00:1' ) ) ,
39
71
) . toBe ( '::ffff:127.0.0.1' ) ;
40
72
} ) ;
73
+ test ( 'to canonical IP address' , ( ) => {
74
+ // IPv4 -> IPv4
75
+ expect ( utils . toCanonicalIp ( '127.0.0.1' ) ) . toBe ( '127.0.0.1' ) ;
76
+ expect ( utils . toCanonicalIp ( '0.0.0.0' ) ) . toBe ( '0.0.0.0' ) ;
77
+ expect ( utils . toCanonicalIp ( '255.255.255.255' ) ) . toBe ( '255.255.255.255' ) ;
78
+ expect ( utils . toCanonicalIp ( '74.125.43.99' ) ) . toBe ( '74.125.43.99' ) ;
79
+ // IPv4 mapped hex -> IPv4
80
+ expect ( utils . toCanonicalIp ( '::ffff:7f00:1' ) ) . toBe ( '127.0.0.1' ) ;
81
+ expect ( utils . toCanonicalIp ( '::ffff:0:0' ) ) . toBe ( '0.0.0.0' ) ;
82
+ expect ( utils . toCanonicalIp ( '::ffff:ffff:ffff' ) ) . toBe ( '255.255.255.255' ) ;
83
+ expect ( utils . toCanonicalIp ( '::ffff:4a7d:2b63' ) ) . toBe ( '74.125.43.99' ) ;
84
+ // IPv4 mapped dec -> IPv4
85
+ expect ( utils . toCanonicalIp ( '::ffff:127.0.0.1' ) ) . toBe ( '127.0.0.1' ) ;
86
+ expect ( utils . toCanonicalIp ( '::ffff:0.0.0.0' ) ) . toBe ( '0.0.0.0' ) ;
87
+ expect ( utils . toCanonicalIp ( '::ffff:255.255.255.255' ) ) . toBe (
88
+ '255.255.255.255' ,
89
+ ) ;
90
+ expect ( utils . toCanonicalIp ( '::ffff:74.125.43.99' ) ) . toBe ( '74.125.43.99' ) ;
91
+ // IPv6 -> IPv6
92
+ expect ( utils . toCanonicalIp ( '::1234:7f00:1' ) ) . toBe ( '::1234:7f00:1' ) ;
93
+ expect ( utils . toCanonicalIp ( '::1234:0:0' ) ) . toBe ( '::1234:0:0' ) ;
94
+ expect ( utils . toCanonicalIp ( '::1234:ffff:ffff' ) ) . toBe ( '::1234:ffff:ffff' ) ;
95
+ expect ( utils . toCanonicalIp ( '::1234:4a7d:2b63' ) ) . toBe ( '::1234:4a7d:2b63' ) ;
96
+ } ) ;
41
97
test ( 'resolves zero IP to local IP' , ( ) => {
42
98
expect ( utils . resolvesZeroIP ( '0.0.0.0' as Host ) ) . toBe ( '127.0.0.1' ) ;
43
99
expect ( utils . resolvesZeroIP ( '::' as Host ) ) . toBe ( '::1' ) ;
0 commit comments