From e5ef28f50974320659e2ecfe3910718a9e60f317 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Tue, 10 Mar 2015 01:32:40 +0900 Subject: Add two utility methods for IPv4 netmasks. 1. Add a validating method to convert a netmask to a prefix length. 2. Add a function to get the implicit netmask of an IPv4 address. 3. Add a unit test. Bug: 19704592 Change-Id: Icb9f58d3903ea01df9e3720383c9bd5db6dd8f26 --- .../src/android/net/NetworkUtilsTest.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 core/tests/coretests/src/android/net/NetworkUtilsTest.java (limited to 'core/tests') diff --git a/core/tests/coretests/src/android/net/NetworkUtilsTest.java b/core/tests/coretests/src/android/net/NetworkUtilsTest.java new file mode 100644 index 0000000..8d51c3b --- /dev/null +++ b/core/tests/coretests/src/android/net/NetworkUtilsTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net; + +import android.net.NetworkUtils; +import android.test.suitebuilder.annotation.SmallTest; + +import java.net.Inet4Address; +import java.net.InetAddress; + +import junit.framework.TestCase; + +public class NetworkUtilsTest extends TestCase { + + private InetAddress Address(String addr) { + return InetAddress.parseNumericAddress(addr); + } + + private Inet4Address IPv4Address(String addr) { + return (Inet4Address) Address(addr); + } + + @SmallTest + public void testGetImplicitNetmask() { + assertEquals(8, NetworkUtils.getImplicitNetmask(IPv4Address("4.2.2.2"))); + assertEquals(8, NetworkUtils.getImplicitNetmask(IPv4Address("10.5.6.7"))); + assertEquals(16, NetworkUtils.getImplicitNetmask(IPv4Address("173.194.72.105"))); + assertEquals(16, NetworkUtils.getImplicitNetmask(IPv4Address("172.23.68.145"))); + assertEquals(24, NetworkUtils.getImplicitNetmask(IPv4Address("192.0.2.1"))); + assertEquals(24, NetworkUtils.getImplicitNetmask(IPv4Address("192.168.5.1"))); + assertEquals(32, NetworkUtils.getImplicitNetmask(IPv4Address("224.0.0.1"))); + assertEquals(32, NetworkUtils.getImplicitNetmask(IPv4Address("255.6.7.8"))); + } + + private void assertInvalidNetworkMask(Inet4Address addr) { + try { + NetworkUtils.netmaskToPrefixLength(addr); + fail("Invalid netmask " + addr.getHostAddress() + " did not cause exception"); + } catch (IllegalArgumentException expected) { + } + } + + @SmallTest + public void testNetmaskToPrefixLength() { + assertEquals(0, NetworkUtils.netmaskToPrefixLength(IPv4Address("0.0.0.0"))); + assertEquals(9, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.128.0.0"))); + assertEquals(17, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.128.0"))); + assertEquals(23, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.254.0"))); + assertEquals(31, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.255.254"))); + assertEquals(32, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.255.255"))); + + assertInvalidNetworkMask(IPv4Address("0.0.0.1")); + assertInvalidNetworkMask(IPv4Address("255.255.255.253")); + assertInvalidNetworkMask(IPv4Address("255.255.0.255")); + } +} -- cgit v1.1