summaryrefslogtreecommitdiffstats
path: root/core/tests
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2015-03-10 01:32:40 +0900
committerLorenzo Colitti <lorenzo@google.com>2015-03-20 16:32:34 +0900
commite5ef28f50974320659e2ecfe3910718a9e60f317 (patch)
tree10f92e1baa7fd048f3dbe26db47de76d66470927 /core/tests
parent3a96f767c3a368969a58e319ad36df5cb39b15f2 (diff)
downloadframeworks_base-e5ef28f50974320659e2ecfe3910718a9e60f317.zip
frameworks_base-e5ef28f50974320659e2ecfe3910718a9e60f317.tar.gz
frameworks_base-e5ef28f50974320659e2ecfe3910718a9e60f317.tar.bz2
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
Diffstat (limited to 'core/tests')
-rw-r--r--core/tests/coretests/src/android/net/NetworkUtilsTest.java70
1 files changed, 70 insertions, 0 deletions
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"));
+ }
+}