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
32
33
34
35
36
37 public class SubnetIPv4Test {
38 @Test
39 public void test24() throws UnknownHostException {
40 InetAddress a = InetAddress.getByName("127.2.3.0");
41 InetAddress b = InetAddress.getByName("127.2.3.4");
42 InetAddress c = InetAddress.getByName("127.2.3.255");
43 InetAddress d = InetAddress.getByName("127.2.4.4");
44
45 Subnet mask = new Subnet(a, 24);
46
47 assertTrue(mask.inSubnet(a));
48 assertTrue(mask.inSubnet(b));
49 assertTrue(mask.inSubnet(c));
50 assertFalse(mask.inSubnet(d));
51 }
52
53 @Test
54 public void test16() throws UnknownHostException {
55 InetAddress a = InetAddress.getByName("127.2.0.0");
56 InetAddress b = InetAddress.getByName("127.2.3.4");
57 InetAddress c = InetAddress.getByName("127.2.129.255");
58 InetAddress d = InetAddress.getByName("127.3.4.4");
59
60 Subnet mask = new Subnet(a, 16);
61
62 assertTrue(mask.inSubnet(a));
63 assertTrue(mask.inSubnet(b));
64 assertTrue(mask.inSubnet(c));
65 assertFalse(mask.inSubnet(d));
66 }
67
68 @Test
69 public void testSingleIp() throws UnknownHostException {
70 InetAddress a = InetAddress.getByName("127.2.3.4");
71 InetAddress b = InetAddress.getByName("127.2.3.3");
72 InetAddress c = InetAddress.getByName("127.2.3.255");
73 InetAddress d = InetAddress.getByName("127.2.3.0");
74
75 Subnet mask = new Subnet(a, 32);
76
77 assertTrue(mask.inSubnet(a));
78 assertFalse(mask.inSubnet(b));
79 assertFalse(mask.inSubnet(c));
80 assertFalse(mask.inSubnet(d));
81 }
82
83 @Test
84 public void testToString() throws UnknownHostException {
85 InetAddress a = InetAddress.getByName("127.2.3.0");
86 Subnet mask = new Subnet(a, 24);
87
88 assertEquals("127.2.3.0/24", mask.toString());
89 }
90
91 @Test
92 public void testToStringLiteral() throws UnknownHostException {
93 InetAddress a = InetAddress.getByName(null);
94 Subnet mask = new Subnet(a, 32);
95
96 assertEquals("127.0.0.1/32", mask.toString());
97 }
98
99 @Test
100 public void testEquals() throws UnknownHostException {
101 Subnet a = new Subnet(InetAddress.getByName("127.2.3.4"), 32);
102 Subnet b = new Subnet(InetAddress.getByName("127.2.3.4"), 32);
103 Subnet c = new Subnet(InetAddress.getByName("127.2.3.5"), 32);
104 Subnet d = new Subnet(InetAddress.getByName("127.2.3.5"), 24);
105
106 assertTrue(a.equals(b));
107 assertFalse(a.equals(c));
108 assertFalse(a.equals(d));
109 assertFalse(a.equals(null));
110 }
111 }