View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net;
19  
20  import org.apache.commons.net.util.SubnetUtils;
21  import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
22  
23  import junit.framework.TestCase;
24  
25  public class SubnetUtilsTest extends TestCase {
26      
27      public void testParseSimpleNetmask() {
28          final String address = "192.168.0.1";
29          final String masks[] = new String[] { "255.0.0.0", "255.255.0.0", "255.255.255.0", "255.255.255.248"};
30          final String bcastAddresses[] = new String[] { "192.255.255.255", "192.168.255.255", "192.168.0.255", "192.168.0.7"};
31          final String lowAddresses[] = new String[] { "192.0.0.1", "192.168.0.1", "192.168.0.1", "192.168.0.1" };
32          final String highAddresses[] = new String[] { "192.255.255.254", "192.168.255.254", "192.168.0.254", "192.168.0.6" };
33          final String networkAddresses[] = new String[] { "192.0.0.0", "192.168.0.0", "192.168.0.0", "192.168.0.0" };
34          final String cidrSignatures[] = new String[] { "192.168.0.1/8", "192.168.0.1/16", "192.168.0.1/24", "192.168.0.1/29" };
35          final int usableAddresses[] = new int[] { 16777214, 65534, 254, 6 };
36          
37          for (int i = 0; i < masks.length; ++i) {
38              SubnetUtils utils = new SubnetUtils(address, masks[i]);
39              SubnetInfo info = utils.getInfo();
40              assertEquals(bcastAddresses[i], info.getBroadcastAddress());
41              assertEquals(cidrSignatures[i], info.getCidrSignature());
42              assertEquals(lowAddresses[i], info.getLowAddress());
43              assertEquals(highAddresses[i], info.getHighAddress());
44              assertEquals(networkAddresses[i], info.getNetworkAddress());
45              assertEquals(usableAddresses[i], info.getAddressCount());
46          }
47      }
48      
49      public void testAddresses() {
50          SubnetUtils utils = new SubnetUtils("192.168.0.1/29");
51          SubnetInfo info = utils.getInfo();
52          assertTrue(info.isInRange("192.168.0.1"));
53          // We dont count the broadcast address as usable
54          assertFalse(info.isInRange("192.168.0.7"));
55          assertFalse(info.isInRange("192.168.0.8"));
56          assertFalse(info.isInRange("10.10.2.1"));
57          assertFalse(info.isInRange("192.168.1.1"));
58          assertFalse(info.isInRange("192.168.0.255"));
59      }
60  }