View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * TODO Add documentation
35   * 
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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 }