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