1 package com.atlassian.ip;
2
3 import org.junit.Test;
4
5 import java.net.InetAddress;
6
7 import static org.junit.Assert.assertArrayEquals;
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.assertFalse;
10 import static org.junit.Assert.assertTrue;
11
12 public class SubnetTest
13 {
14 @Test
15 public void testInvalidHostNamePattern() throws Exception
16 {
17 assertFalse(Subnet.isValidPattern("ssdf"));
18 }
19
20 @Test
21 public void testInvalidPostCIDRPattern() throws Exception
22 {
23 assertFalse(Subnet.isValidPattern("192.168.1.0/"));
24 }
25
26 @Test
27 public void testInvalidPreCIDRPattern() throws Exception
28 {
29 assertFalse(Subnet.isValidPattern("/24"));
30 }
31
32 @Test
33 public void testInvalidWildcardPattern() throws Exception
34 {
35 assertFalse(Subnet.isValidPattern("*.168.1.0"));
36 }
37
38 @Test
39 public void testValidPattern() throws Exception
40 {
41 assertTrue(Subnet.isValidPattern("0.0.0.0"));
42 }
43
44 @Test
45 public void testIPv4PatternParsing() throws Exception
46 {
47 Subnet subnet = Subnet.forPattern("100.100.100.100");
48 assertEquals(32, subnet.getMask());
49 assertArrayEquals(new byte[]{100, 100, 100, 100}, subnet.getAddress());
50 }
51
52 @Test
53 public void testIPv6PatternParsing() throws Exception
54 {
55 Subnet subnet = Subnet.forPattern("::1");
56 assertEquals(128, subnet.getMask());
57 assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, subnet.getAddress());
58 }
59
60 @Test
61 public void testIPv4WildcardPatternParsing() throws Exception
62 {
63 Subnet subnet = Subnet.forPattern("100.100.*.*");
64 assertEquals(16, subnet.getMask());
65 assertArrayEquals(new byte[]{100, 100, 0, 0}, subnet.getAddress());
66 }
67
68 @Test
69 public void testIPV4WildcardSubnetZeroPatternParsing() throws Exception
70 {
71 Subnet subnet = Subnet.forPattern("*.*.*.*");
72 assertEquals(0, subnet.getMask());
73 }
74
75 @Test
76 public void testIPv4CIDRPatternParsing() throws Exception
77 {
78 Subnet subnet = Subnet.forPattern("100.100.100.100/16");
79 assertEquals(16, subnet.getMask());
80 assertArrayEquals(new byte[]{100, 100, 100, 100}, subnet.getAddress());
81 }
82
83 @Test
84 public void testIPv4CIDRSubnetZeroPatternParsing() throws Exception
85 {
86 Subnet subnet = Subnet.forPattern("100.100.100.100/0");
87 assertEquals(0, subnet.getMask());
88 }
89
90 @Test
91 public void testIPv6CIDRPatternParsing() throws Exception
92 {
93 Subnet subnet = Subnet.forPattern("::1/64");
94 assertEquals(64, subnet.getMask());
95 assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, subnet.getAddress());
96 }
97
98 @Test
99 public void testIPv4AddressParsing() throws Exception
100 {
101 Subnet subnet = Subnet.forAddress(InetAddress.getByName("1.0.0.1"));
102 assertEquals(32, subnet.getMask());
103 assertArrayEquals(new byte[]{1, 0, 0, 1}, subnet.getAddress());
104 }
105
106 @Test
107 public void testIPv6AddressParsing() throws Exception
108 {
109 Subnet subnet = Subnet.forAddress(InetAddress.getByName("1::1"));
110 assertEquals(128, subnet.getMask());
111 assertArrayEquals(new byte[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, subnet.getAddress());
112 }
113 }