diff options
author | Remi NGUYEN VAN <reminv@google.com> | 2021-03-18 04:53:40 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-03-18 04:53:40 +0000 |
commit | e9966a525f5b1293ddfb41ed0e6817c24d46ccc8 (patch) | |
tree | 795072a675fa5acffcff0b3a14fbf460d1600099 /common | |
parent | 34ee25f55ecc4c4f77c9371db25c7d078e856bf5 (diff) | |
parent | 7606dec9507e190938e3ba1148800c2d3168faa4 (diff) | |
download | net-e9966a525f5b1293ddfb41ed0e6817c24d46ccc8.tar.gz |
Merge "Move trimV4AddrZeros to libs/net" am: 54fcb1500e am: 3bb6f4c4eb am: 7606dec950
Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/net/+/1637461
Change-Id: I05d5a2ed5d9f6a850770493c45eedf6ef18c1fd5
Diffstat (limited to 'common')
-rw-r--r-- | common/framework/com/android/net/module/util/Inet4AddressUtils.java | 26 | ||||
-rw-r--r-- | common/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java | 13 |
2 files changed, 39 insertions, 0 deletions
diff --git a/common/framework/com/android/net/module/util/Inet4AddressUtils.java b/common/framework/com/android/net/module/util/Inet4AddressUtils.java index a1d34a0f..87f43d51 100644 --- a/common/framework/com/android/net/module/util/Inet4AddressUtils.java +++ b/common/framework/com/android/net/module/util/Inet4AddressUtils.java @@ -163,4 +163,30 @@ public class Inet4AddressUtils { throws IllegalArgumentException { return intToInet4AddressHTH(prefixLengthToV4NetmaskIntHTH(prefixLength)); } + + /** + * Trim leading zeros from IPv4 address strings + * Non-v4 addresses and host names remain unchanged. + * For example, 192.168.000.010 -> 192.168.0.10 + * @param addr a string representing an ip address + * @return a string properly trimmed + */ + public static String trimAddressZeros(String addr) { + if (addr == null) return null; + String[] octets = addr.split("\\."); + if (octets.length != 4) return addr; + StringBuilder builder = new StringBuilder(16); + String result = null; + for (int i = 0; i < 4; i++) { + try { + if (octets[i].length() > 3) return addr; + builder.append(Integer.parseInt(octets[i])); + } catch (NumberFormatException e) { + return addr; + } + if (i < 3) builder.append('.'); + } + result = builder.toString(); + return result; + } } diff --git a/common/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java b/common/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java index 5d5ed911..702bdaf8 100644 --- a/common/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java +++ b/common/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java @@ -26,9 +26,11 @@ import static com.android.net.module.util.Inet4AddressUtils.intToInet4AddressHTL import static com.android.net.module.util.Inet4AddressUtils.netmaskToPrefixLength; import static com.android.net.module.util.Inet4AddressUtils.prefixLengthToV4NetmaskIntHTH; import static com.android.net.module.util.Inet4AddressUtils.prefixLengthToV4NetmaskIntHTL; +import static com.android.net.module.util.Inet4AddressUtils.trimAddressZeros; import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import android.net.InetAddresses; @@ -204,6 +206,17 @@ public class Inet4AddressUtilsTest { getPrefixMaskAsInet4Address(-1); } + @Test + public void testTrimAddressZeros() { + assertNull(trimAddressZeros(null)); + assertEquals("$invalid&", trimAddressZeros("$invalid&")); + assertEquals("example.com", trimAddressZeros("example.com")); + assertEquals("a.b.c.d", trimAddressZeros("a.b.c.d")); + + assertEquals("192.0.2.2", trimAddressZeros("192.000.02.2")); + assertEquals("192.0.2.2", trimAddressZeros("192.0.2.2")); + } + private Inet4Address ipv4Address(String addr) { return (Inet4Address) InetAddresses.parseNumericAddress(addr); } |