-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTestNetworkUtils.java
More file actions
188 lines (167 loc) · 8.26 KB
/
Copy pathTestNetworkUtils.java
File metadata and controls
188 lines (167 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import monero.common.NetworkUtils;
import org.junit.jupiter.api.Test;
/**
* Tests generic network/transport addressing utilities.
*/
public class TestNetworkUtils {
@Test
public void testParseUriPrependsHttpScheme() {
assertEquals("http://example.com", NetworkUtils.parseUri("example.com").toString());
assertEquals("http://localhost:8080", NetworkUtils.parseUri("localhost:8080").toString());
assertEquals("http://127.0.0.1:18081", NetworkUtils.parseUri("127.0.0.1:18081").toString());
}
@Test
public void testParseUriPreservesExistingScheme() {
assertEquals("https://example.com", NetworkUtils.parseUri("https://example.com").toString());
assertEquals("http://[::1]:18081", NetworkUtils.parseUri("http://[::1]:18081").toString());
assertEquals("socks5://127.0.0.1:9050", NetworkUtils.parseUri("socks5://127.0.0.1:9050").toString());
}
@Test
public void testParseUriSupportsIpv6WithoutScheme() {
assertEquals("[::1]", NetworkUtils.parseUri("[::1]:18081").getHost());
assertEquals(18081, NetworkUtils.parseUri("[::1]:18081").getPort());
assertEquals("[2607:3c40:1900:33e0::1]", NetworkUtils.parseUri("2607:3c40:1900:33e0::1").getHost());
assertEquals("[2607:3c40:1900:33e0::1]", NetworkUtils.parseUri("[2607:3c40:1900:33e0::1]:18089").getHost());
assertEquals(18089, NetworkUtils.parseUri("[2607:3c40:1900:33e0::1]:18089").getPort());
}
@Test
public void testParseUriBracketsBareIpv6Literal() {
assertEquals("http://[fe80::1]", NetworkUtils.parseUri("fe80::1").toString());
assertEquals("http://[::ffff:192.168.1.1]", NetworkUtils.parseUri("::ffff:192.168.1.1").toString());
}
@Test
public void testParseUriThrowsOnInvalid() {
assertThrows(IllegalArgumentException.class, () -> NetworkUtils.parseUri("http://[localhost]:9050"));
}
@Test
public void testParseHostAndPortParsesBracketedIpv6() {
NetworkUtils.HostAndPort hostAndPort = NetworkUtils.parseHostAndPort("[2607:3c40:1900:33e0::1]:18089", -1);
assertEquals("2607:3c40:1900:33e0::1", hostAndPort.getHost());
assertEquals(18089, hostAndPort.getPort());
assertTrue(hostAndPort.hasPort());
}
@Test
public void testParseHostAndPortAppliesDefaultPort() {
NetworkUtils.HostAndPort hostAndPort = NetworkUtils.parseHostAndPort("feder8.me", 18081);
assertEquals("feder8.me", hostAndPort.getHost());
assertEquals(18081, hostAndPort.getPort());
}
@Test
public void testParseHostAndPortRejectsBracketedNonIpv6() {
assertThrows(IllegalArgumentException.class, () -> NetworkUtils.parseHostAndPort("[localhost]:9050", -1));
}
@Test
public void testParseHostAndPortRejectsUnbracketedIpv6WithPort() {
assertThrows(IllegalArgumentException.class, () -> NetworkUtils.parseHostAndPort("2a0b:f4c2:2::63:18081", -1, true));
}
@Test
public void testParseHostAndPortRejectsMissingPortWhenRequired() {
assertThrows(IllegalArgumentException.class, () -> NetworkUtils.parseHostAndPort("127.0.0.1", -1, true));
}
@Test
public void testParseHostAndPortRejectsPortOutOfRange() {
assertThrows(IllegalArgumentException.class, () -> NetworkUtils.parseHostAndPort("[::1]:65536", -1));
}
@Test
public void testParseHostAndPortRejectsNullAndEmpty() {
assertThrows(IllegalArgumentException.class, () -> NetworkUtils.parseHostAndPort(null, -1));
assertThrows(IllegalArgumentException.class, () -> NetworkUtils.parseHostAndPort(" ", -1));
}
@Test
public void testFormatHostAndPort() {
assertEquals("127.0.0.1:9050", NetworkUtils.formatHostAndPort("127.0.0.1", 9050));
assertEquals("[::1]:9050", NetworkUtils.formatHostAndPort("::1", 9050));
assertEquals("[::1]:9050", NetworkUtils.formatHostAndPort("[::1]", 9050));
assertEquals("feder8.me:18089", NetworkUtils.formatHostAndPort("feder8.me", 18089));
}
@Test
public void testIsIpv6Literal() {
assertTrue(NetworkUtils.isIpv6Literal("::1"));
assertTrue(NetworkUtils.isIpv6Literal("::"));
assertTrue(NetworkUtils.isIpv6Literal("[::1]"));
assertTrue(NetworkUtils.isIpv6Literal("2607:3c40:1900:33e0::1"));
assertTrue(NetworkUtils.isIpv6Literal("1:2:3:4:5:6:7:8"));
assertTrue(NetworkUtils.isIpv6Literal("::ffff:192.168.1.1"));
assertTrue(NetworkUtils.isIpv6Literal("fe80::1%eth0"));
assertFalse(NetworkUtils.isIpv6Literal(null));
assertFalse(NetworkUtils.isIpv6Literal("localhost"));
assertFalse(NetworkUtils.isIpv6Literal("127.0.0.1"));
assertFalse(NetworkUtils.isIpv6Literal("1:2:3:4:5:6:7:8:9"));
assertFalse(NetworkUtils.isIpv6Literal("1::2::3"));
assertFalse(NetworkUtils.isIpv6Literal("2a0b:f4c2:2::63:18081"));
}
@Test
public void testIsLocalHost() {
assertTrue(NetworkUtils.isLocalHost("localhost"));
assertTrue(NetworkUtils.isLocalHost("127.0.0.1:18081"));
assertTrue(NetworkUtils.isLocalHost("http://127.0.0.1:18081"));
assertTrue(NetworkUtils.isLocalHost("[::1]:18081"));
assertTrue(NetworkUtils.isLocalHost("http://[::1]:18081"));
assertFalse(NetworkUtils.isLocalHost("example.com"));
assertFalse(NetworkUtils.isLocalHost("http://192.168.1.1:18081"));
assertFalse(NetworkUtils.isLocalHost("[fe80::1]:18081"));
}
@Test
public void testIsLoopbackUrl() {
assertTrue(NetworkUtils.isLoopbackUrl("localhost"));
assertTrue(NetworkUtils.isLoopbackUrl("LOCALHOST"));
assertTrue(NetworkUtils.isLoopbackUrl("127.0.0.1"));
assertTrue(NetworkUtils.isLoopbackUrl("127.0.0.1:18081"));
assertTrue(NetworkUtils.isLoopbackUrl("http://127.0.0.1:18081"));
assertTrue(NetworkUtils.isLoopbackUrl("127.5.6.7")); // entire 127.0.0.0/8 range is loopback
assertTrue(NetworkUtils.isLoopbackUrl("::1"));
assertTrue(NetworkUtils.isLoopbackUrl("[::1]:18081"));
assertTrue(NetworkUtils.isLoopbackUrl("http://[::1]:18081"));
assertFalse(NetworkUtils.isLoopbackUrl(null));
assertFalse(NetworkUtils.isLoopbackUrl(""));
assertFalse(NetworkUtils.isLoopbackUrl("example.com"));
assertFalse(NetworkUtils.isLoopbackUrl("http://192.168.1.1:18081"));
assertFalse(NetworkUtils.isLoopbackUrl("8.8.8.8"));
assertFalse(NetworkUtils.isLoopbackUrl("[fe80::1]:18081"));
assertFalse(NetworkUtils.isLoopbackUrl("[2607:3c40:1900:33e0::1]:18089"));
}
@Test
public void testIsPrivateIp() {
// loopback / localhost
assertTrue(NetworkUtils.isPrivateIp("127.0.0.1"));
assertTrue(NetworkUtils.isPrivateIp("localhost"));
// RFC 1918 site-local
assertTrue(NetworkUtils.isPrivateIp("192.168.1.1"));
assertTrue(NetworkUtils.isPrivateIp("10.0.0.1:18081"));
assertTrue(NetworkUtils.isPrivateIp("http://172.16.0.1:18081"));
// IPv6 link-local and unique local (fc00::/7)
assertTrue(NetworkUtils.isPrivateIp("fe80::1"));
assertTrue(NetworkUtils.isPrivateIp("[fe80::1]:18081"));
assertTrue(NetworkUtils.isPrivateIp("http://[fe80::1]:18081"));
assertTrue(NetworkUtils.isPrivateIp("fd12:3456:789a::1"));
assertTrue(NetworkUtils.isPrivateIp("fc00::1"));
assertTrue(NetworkUtils.isPrivateIp("http://[fdaa:bbbb:cccc::1]:18089"));
// public addresses and hostnames are not private
assertFalse(NetworkUtils.isPrivateIp(""));
assertFalse(NetworkUtils.isPrivateIp(null));
assertFalse(NetworkUtils.isPrivateIp("8.8.8.8"));
assertFalse(NetworkUtils.isPrivateIp("example.com"));
assertFalse(NetworkUtils.isPrivateIp("[2607:3c40:1900:33e0::1]:18089"));
}
@Test
public void testIsIpv6Uri() {
assertTrue(NetworkUtils.isIpv6Uri("http://[2607:3c40:1900:33e0::1]:18089"));
assertTrue(NetworkUtils.isIpv6Uri("[::1]:18081"));
assertFalse(NetworkUtils.isIpv6Uri("http://127.0.0.1:18081"));
assertFalse(NetworkUtils.isIpv6Uri("http://example.com:18081"));
}
@Test
public void testIsIpv4Literal() {
assertTrue(NetworkUtils.isIpv4Literal("127.0.0.1"));
assertTrue(NetworkUtils.isIpv4Literal("255.255.255.255"));
assertFalse(NetworkUtils.isIpv4Literal(null));
assertFalse(NetworkUtils.isIpv4Literal("256.0.0.1"));
assertFalse(NetworkUtils.isIpv4Literal("192.168.1"));
assertFalse(NetworkUtils.isIpv4Literal("01.2.3.4")); // no ambiguous leading zeros
assertFalse(NetworkUtils.isIpv4Literal("::1"));
}
}