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 java.net.InetAddress;
24  import java.net.UnknownHostException;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * TODO Add documentation
30   * 
31   * @author The Apache MINA Project (dev@mina.apache.org)
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 }