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