001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020
021package org.apache.mina.filter.firewall;
022
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertFalse;
025import static org.junit.Assert.assertTrue;
026
027import java.net.InetAddress;
028import java.net.UnknownHostException;
029
030import org.junit.Test;
031
032/**
033 * TODO Add documentation
034 * 
035 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
036 */
037public class SubnetIPv4Test {
038    @Test
039    public void test24() throws UnknownHostException {
040        InetAddress a = InetAddress.getByName("127.2.3.0");
041        InetAddress b = InetAddress.getByName("127.2.3.4");
042        InetAddress c = InetAddress.getByName("127.2.3.255");
043        InetAddress d = InetAddress.getByName("127.2.4.4");
044
045        Subnet mask = new Subnet(a, 24);
046
047        assertTrue(mask.inSubnet(a));
048        assertTrue(mask.inSubnet(b));
049        assertTrue(mask.inSubnet(c));
050        assertFalse(mask.inSubnet(d));
051    }
052
053    @Test
054    public void test16() throws UnknownHostException {
055        InetAddress a = InetAddress.getByName("127.2.0.0");
056        InetAddress b = InetAddress.getByName("127.2.3.4");
057        InetAddress c = InetAddress.getByName("127.2.129.255");
058        InetAddress d = InetAddress.getByName("127.3.4.4");
059
060        Subnet mask = new Subnet(a, 16);
061
062        assertTrue(mask.inSubnet(a));
063        assertTrue(mask.inSubnet(b));
064        assertTrue(mask.inSubnet(c));
065        assertFalse(mask.inSubnet(d));
066    }
067
068    @Test
069    public void testSingleIp() throws UnknownHostException {
070        InetAddress a = InetAddress.getByName("127.2.3.4");
071        InetAddress b = InetAddress.getByName("127.2.3.3");
072        InetAddress c = InetAddress.getByName("127.2.3.255");
073        InetAddress d = InetAddress.getByName("127.2.3.0");
074
075        Subnet mask = new Subnet(a, 32);
076
077        assertTrue(mask.inSubnet(a));
078        assertFalse(mask.inSubnet(b));
079        assertFalse(mask.inSubnet(c));
080        assertFalse(mask.inSubnet(d));
081    }
082
083    @Test
084    public void testToString() throws UnknownHostException {
085        InetAddress a = InetAddress.getByName("127.2.3.0");
086        Subnet mask = new Subnet(a, 24);
087
088        assertEquals("127.2.3.0/24", mask.toString());
089    }
090
091    @Test
092    public void testToStringLiteral() throws UnknownHostException {
093        InetAddress a = InetAddress.getByName("127.0.0.1");
094        Subnet mask = new Subnet(a, 32);
095
096        assertEquals("127.0.0.1/32", mask.toString());
097    }
098
099    @Test
100    public void testEquals() throws UnknownHostException {
101        Subnet a = new Subnet(InetAddress.getByName("127.2.3.4"), 32);
102        Subnet b = new Subnet(InetAddress.getByName("127.2.3.4"), 32);
103        Subnet c = new Subnet(InetAddress.getByName("127.2.3.5"), 32);
104        Subnet d = new Subnet(InetAddress.getByName("127.2.3.5"), 24);
105
106        assertTrue(a.equals(b));
107        assertFalse(a.equals(c));
108        assertFalse(a.equals(d));
109        assertFalse(a.equals(null));
110    }
111}