1
1
package com .stealthcopter .networktools ;
2
2
3
+ import org .junit .Ignore ;
3
4
import org .junit .Test ;
4
5
6
+ import java .net .InetAddress ;
7
+ import java .net .UnknownHostException ;
8
+ import java .util .List ;
9
+
5
10
import static org .junit .Assert .assertFalse ;
11
+ import static org .junit .Assert .assertNotNull ;
6
12
import static org .junit .Assert .assertTrue ;
7
13
8
14
/**
@@ -13,7 +19,7 @@ public class IPToolsTest {
13
19
14
20
15
21
String [] getInvalidIpAddresses (){
16
- return new String []{"beepbeep" , "nope" , "hello" };
22
+ return new String []{null , "beepbeep" , "nope" , "hello" };
17
23
}
18
24
19
25
String [] getIPv4Addresses (){
@@ -24,8 +30,12 @@ String[] getIPv6Addresses(){
24
30
return new String []{"2001:0db8:85a3:0000:0000:8a2e:0370:7334" };
25
31
}
26
32
33
+ String [] getIPv6AddressesHexCompresed (){
34
+ return new String []{"2001:db8:85a3::8a2e:370:7334" , "2001::8a2e:370:7334" , "2001:" , "2001::2001" };
35
+ }
36
+
27
37
@ Test
28
- public void testIsIPv4Address () throws Exception {
38
+ public void testIsIPv4Address () {
29
39
30
40
for (String address : getIPv4Addresses ()) {
31
41
assertTrue (IPTools .isIPv4Address (address ));
@@ -41,7 +51,7 @@ public void testIsIPv4Address() throws Exception {
41
51
}
42
52
43
53
@ Test
44
- public void testIsIPv6Address () throws Exception {
54
+ public void testIsIPv6Address () {
45
55
for (String address : getIPv4Addresses ()) {
46
56
assertFalse (IPTools .isIPv6Address (address ));
47
57
}
@@ -54,4 +64,63 @@ public void testIsIPv6Address() throws Exception {
54
64
assertFalse (IPTools .isIPv6Address (address ));
55
65
}
56
66
}
67
+
68
+ @ Test
69
+ public void testIsIPv6AddressesStandard () {
70
+ for (String address : getIPv4Addresses ()) {
71
+ assertFalse (IPTools .isIPv6StdAddress (address ));
72
+ }
73
+
74
+ for (String address : getIPv6Addresses ()) {
75
+ assertTrue (IPTools .isIPv6StdAddress (address ));
76
+ }
77
+
78
+ for (String address : getInvalidIpAddresses ()) {
79
+ assertFalse (IPTools .isIPv6StdAddress (address ));
80
+ }
81
+ }
82
+
83
+ @ Test
84
+ @ Ignore // Not working yet, possibly not correct pattern for compression
85
+ public void testIPv6HexCompressedAddress () {
86
+ for (String address : getIPv6AddressesHexCompresed ()) {
87
+ assertTrue (IPTools .isIPv6HexCompressedAddress (address ));
88
+ }
89
+ }
90
+
91
+ @ Test
92
+ public void testGetLocalAddressReturnsLocalIP (){
93
+ InetAddress test = IPTools .getLocalIPv4Address ();
94
+
95
+ assertNotNull (test );
96
+
97
+ assertTrue (IPTools .isIpAddressLocalhost (test ));
98
+ assertTrue (IPTools .isIpAddressLocalNetwork (test ));
99
+ }
100
+
101
+
102
+ @ Test
103
+ public void testGetAllLocalAddressReturnsLocalIP (){
104
+ List <InetAddress > test = IPTools .getLocalIPv4Addresses ();
105
+
106
+ for (InetAddress address : test ){
107
+ System .out .println (address );
108
+ assertNotNull (address );
109
+
110
+ assertTrue (IPTools .isIpAddressLocalhost (address ));
111
+ assertTrue (IPTools .isIpAddressLocalNetwork (address ));
112
+ }
113
+ }
114
+
115
+ @ Test
116
+ public void testLocalAddresses () throws UnknownHostException {
117
+ assertTrue (IPTools .isIpAddressLocalhost (InetAddress .getByName ("127.0.0.1" )));
118
+ assertFalse (IPTools .isIpAddressLocalhost (InetAddress .getByName ("8.8.8.8" )));
119
+ }
120
+
121
+ @ Test
122
+ public void testLocalAddressesNetwork () throws UnknownHostException {
123
+ assertFalse (IPTools .isIpAddressLocalNetwork (InetAddress .getByName ("8.8.8.8" )));
124
+ }
125
+
57
126
}
0 commit comments