Skip to content

Commit ba4466c

Browse files
committed
More tests
1 parent 0c2a0e8 commit ba4466c

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

library/src/test/java/com/stealthcopter/networktools/IPToolsTest.java

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.stealthcopter.networktools;
22

3+
import org.junit.Ignore;
34
import org.junit.Test;
45

6+
import java.net.InetAddress;
7+
import java.net.UnknownHostException;
8+
import java.util.List;
9+
510
import static org.junit.Assert.assertFalse;
11+
import static org.junit.Assert.assertNotNull;
612
import static org.junit.Assert.assertTrue;
713

814
/**
@@ -13,7 +19,7 @@ public class IPToolsTest {
1319

1420

1521
String[] getInvalidIpAddresses(){
16-
return new String[]{"beepbeep", "nope", "hello"};
22+
return new String[]{null, "beepbeep", "nope", "hello"};
1723
}
1824

1925
String[] getIPv4Addresses(){
@@ -24,8 +30,12 @@ String[] getIPv6Addresses(){
2430
return new String[]{"2001:0db8:85a3:0000:0000:8a2e:0370:7334"};
2531
}
2632

33+
String[] getIPv6AddressesHexCompresed(){
34+
return new String[]{"2001:db8:85a3::8a2e:370:7334", "2001::8a2e:370:7334", "2001:", "2001::2001"};
35+
}
36+
2737
@Test
28-
public void testIsIPv4Address() throws Exception {
38+
public void testIsIPv4Address() {
2939

3040
for (String address: getIPv4Addresses()) {
3141
assertTrue(IPTools.isIPv4Address(address));
@@ -41,7 +51,7 @@ public void testIsIPv4Address() throws Exception {
4151
}
4252

4353
@Test
44-
public void testIsIPv6Address() throws Exception {
54+
public void testIsIPv6Address() {
4555
for (String address: getIPv4Addresses()) {
4656
assertFalse(IPTools.isIPv6Address(address));
4757
}
@@ -54,4 +64,63 @@ public void testIsIPv6Address() throws Exception {
5464
assertFalse(IPTools.isIPv6Address(address));
5565
}
5666
}
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+
57126
}

library/src/test/java/com/stealthcopter/networktools/MACToolsTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.stealthcopter.networktools;
22

33
import org.junit.Test;
4+
import org.mockito.internal.matchers.Null;
45

6+
import static org.junit.Assert.assertEquals;
57
import static org.junit.Assert.assertFalse;
68
import static org.junit.Assert.assertTrue;
79

@@ -33,4 +35,26 @@ public void testInvalidMACAddresses() {
3335
}
3436
}
3537

38+
@Test
39+
public void testMACGetBytes() {
40+
byte[] bytes = MACTools.getMacBytes("01:02:03:04:05:06");
41+
42+
assertEquals(bytes[0], 0x01);
43+
assertEquals(bytes[1], 0x02);
44+
assertEquals(bytes[2], 0x03);
45+
assertEquals(bytes[3], 0x04);
46+
assertEquals(bytes[4], 0x05);
47+
assertEquals(bytes[5], 0x06);
48+
}
49+
50+
51+
@Test(expected = IllegalArgumentException.class)
52+
public void testMACGetBytesThrowsNull() {
53+
MACTools.getMacBytes(null);
54+
}
55+
56+
@Test(expected = IllegalArgumentException.class)
57+
public void testMACGetBytesThrowInvalidMac() {
58+
MACTools.getMacBytes("00:00:00");
59+
}
3660
}

0 commit comments

Comments
 (0)