001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.net; 019 020 import org.apache.commons.net.util.SubnetUtils; 021 import org.apache.commons.net.util.SubnetUtils.SubnetInfo; 022 023 import junit.framework.TestCase; 024 025 public class SubnetUtilsTest extends TestCase { 026 027 public void testParseSimpleNetmask() { 028 final String address = "192.168.0.1"; 029 final String masks[] = new String[] { "255.0.0.0", "255.255.0.0", "255.255.255.0", "255.255.255.248"}; 030 final String bcastAddresses[] = new String[] { "192.255.255.255", "192.168.255.255", "192.168.0.255", "192.168.0.7"}; 031 final String lowAddresses[] = new String[] { "192.0.0.1", "192.168.0.1", "192.168.0.1", "192.168.0.1" }; 032 final String highAddresses[] = new String[] { "192.255.255.254", "192.168.255.254", "192.168.0.254", "192.168.0.6" }; 033 final String networkAddresses[] = new String[] { "192.0.0.0", "192.168.0.0", "192.168.0.0", "192.168.0.0" }; 034 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" }; 035 final int usableAddresses[] = new int[] { 16777214, 65534, 254, 6 }; 036 037 for (int i = 0; i < masks.length; ++i) { 038 SubnetUtils utils = new SubnetUtils(address, masks[i]); 039 SubnetInfo info = utils.getInfo(); 040 assertEquals(bcastAddresses[i], info.getBroadcastAddress()); 041 assertEquals(cidrSignatures[i], info.getCidrSignature()); 042 assertEquals(lowAddresses[i], info.getLowAddress()); 043 assertEquals(highAddresses[i], info.getHighAddress()); 044 assertEquals(networkAddresses[i], info.getNetworkAddress()); 045 assertEquals(usableAddresses[i], info.getAddressCount()); 046 } 047 } 048 049 public void testAddresses() { 050 SubnetUtils utils = new SubnetUtils("192.168.0.1/29"); 051 SubnetInfo info = utils.getInfo(); 052 assertTrue(info.isInRange("192.168.0.1")); 053 // We dont count the broadcast address as usable 054 assertFalse(info.isInRange("192.168.0.7")); 055 assertFalse(info.isInRange("192.168.0.8")); 056 assertFalse(info.isInRange("10.10.2.1")); 057 assertFalse(info.isInRange("192.168.1.1")); 058 assertFalse(info.isInRange("192.168.0.255")); 059 } 060 }