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   * @version $Rev$, $Date$
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 }