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