1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.mina.filter.firewall;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29
30 import org.junit.Test;
31 import org.junit.Ignore;
32
33
34
35
36
37
38 public class SubnetIPv4Test {
39 @Test
40 public void test24() throws UnknownHostException {
41 InetAddress a = InetAddress.getByName("127.2.3.0");
42 InetAddress b = InetAddress.getByName("127.2.3.4");
43 InetAddress c = InetAddress.getByName("127.2.3.255");
44 InetAddress d = InetAddress.getByName("127.2.4.4");
45
46 Subnet mask = new Subnet(a, 24);
47
48 assertTrue(mask.inSubnet(a));
49 assertTrue(mask.inSubnet(b));
50 assertTrue(mask.inSubnet(c));
51 assertFalse(mask.inSubnet(d));
52 }
53
54 @Test
55 public void test16() throws UnknownHostException {
56 InetAddress a = InetAddress.getByName("127.2.0.0");
57 InetAddress b = InetAddress.getByName("127.2.3.4");
58 InetAddress c = InetAddress.getByName("127.2.129.255");
59 InetAddress d = InetAddress.getByName("127.3.4.4");
60
61 Subnet mask = new Subnet(a, 16);
62
63 assertTrue(mask.inSubnet(a));
64 assertTrue(mask.inSubnet(b));
65 assertTrue(mask.inSubnet(c));
66 assertFalse(mask.inSubnet(d));
67 }
68
69 @Test
70 public void testSingleIp() throws UnknownHostException {
71 InetAddress a = InetAddress.getByName("127.2.3.4");
72 InetAddress b = InetAddress.getByName("127.2.3.3");
73 InetAddress c = InetAddress.getByName("127.2.3.255");
74 InetAddress d = InetAddress.getByName("127.2.3.0");
75
76 Subnet mask = new Subnet(a, 32);
77
78 assertTrue(mask.inSubnet(a));
79 assertFalse(mask.inSubnet(b));
80 assertFalse(mask.inSubnet(c));
81 assertFalse(mask.inSubnet(d));
82 }
83
84 @Test
85 public void testToString() throws UnknownHostException {
86 InetAddress a = InetAddress.getByName("127.2.3.0");
87 Subnet mask = new Subnet(a, 24);
88
89 assertEquals("127.2.3.0/24", mask.toString());
90 }
91
92 @Test
93 public void testToStringLiteral() throws UnknownHostException {
94 InetAddress a = InetAddress.getByName("127.0.0.1");
95 Subnet mask = new Subnet(a, 32);
96
97 assertEquals("127.0.0.1/32", mask.toString());
98 }
99
100 @Test
101 public void testEquals() throws UnknownHostException {
102 Subnet a = new Subnet(InetAddress.getByName("127.2.3.4"), 32);
103 Subnet b = new Subnet(InetAddress.getByName("127.2.3.4"), 32);
104 Subnet c = new Subnet(InetAddress.getByName("127.2.3.5"), 32);
105 Subnet d = new Subnet(InetAddress.getByName("127.2.3.5"), 24);
106
107 assertTrue(a.equals(b));
108 assertFalse(a.equals(c));
109 assertFalse(a.equals(d));
110 assertFalse(a.equals(null));
111 }
112 }