[automerge] Refactors common scope id setup code into utility function 2p: 9989ddf895 am: 9e64637af2
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/libs/net/+/22904993
Change-Id: I04bffc126eab1fee4f18d59ec653afc8ea2f061c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/common/framework/com/android/net/module/util/InetAddressUtils.java b/common/framework/com/android/net/module/util/InetAddressUtils.java
index 31d0729..40fc59f 100644
--- a/common/framework/com/android/net/module/util/InetAddressUtils.java
+++ b/common/framework/com/android/net/module/util/InetAddressUtils.java
@@ -16,7 +16,10 @@
package com.android.net.module.util;
+import android.annotation.NonNull;
import android.os.Parcel;
+import android.util.Log;
+
import java.net.Inet6Address;
import java.net.InetAddress;
@@ -28,6 +31,7 @@
*/
public class InetAddressUtils {
+ private static final String TAG = InetAddressUtils.class.getSimpleName();
private static final int INET6_ADDR_LENGTH = 16;
/**
@@ -71,5 +75,23 @@
}
}
+ /**
+ * Create a Inet6Address with scope id if it is a link local address. Otherwise, returns the
+ * original address.
+ */
+ public static Inet6Address withScopeId(@NonNull final Inet6Address addr, int scopeid) {
+ if (!addr.isLinkLocalAddress()) {
+ return addr;
+ }
+ try {
+ return Inet6Address.getByAddress(null /* host */, addr.getAddress(),
+ scopeid);
+ } catch (UnknownHostException impossible) {
+ Log.wtf(TAG, "Cannot construct scoped Inet6Address with Inet6Address.getAddress("
+ + addr.getHostAddress() + "): ", impossible);
+ return null;
+ }
+ }
+
private InetAddressUtils() {}
}
diff --git a/common/tests/unit/src/com/android/net/module/util/InetAddressUtilsTest.java b/common/tests/unit/src/com/android/net/module/util/InetAddressUtilsTest.java
index 2736c53..bb2b933 100644
--- a/common/tests/unit/src/com/android/net/module/util/InetAddressUtilsTest.java
+++ b/common/tests/unit/src/com/android/net/module/util/InetAddressUtilsTest.java
@@ -18,6 +18,10 @@
import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.net.InetAddresses;
import android.os.Parcel;
import androidx.test.filters.SmallTest;
@@ -67,4 +71,25 @@
assertEquals(ipv6, out);
assertEquals(42, out.getScopeId());
}
+
+ @Test
+ public void testWithScopeId() {
+ final int scopeId = 999;
+
+ final String globalAddrStr = "2401:fa00:49c:484:dc41:e6ff:fefd:f180";
+ final Inet6Address globalAddr = (Inet6Address) InetAddresses
+ .parseNumericAddress(globalAddrStr);
+ final Inet6Address updatedGlobalAddr = InetAddressUtils.withScopeId(globalAddr, scopeId);
+ assertFalse(updatedGlobalAddr.isLinkLocalAddress());
+ assertEquals(globalAddrStr, updatedGlobalAddr.getHostAddress());
+ assertEquals(0, updatedGlobalAddr.getScopeId());
+
+ final String localAddrStr = "fe80::4735:9628:d038:2087";
+ final Inet6Address localAddr = (Inet6Address) InetAddresses
+ .parseNumericAddress(localAddrStr);
+ final Inet6Address updatedLocalAddr = InetAddressUtils.withScopeId(localAddr, scopeId);
+ assertTrue(updatedLocalAddr.isLinkLocalAddress());
+ assertEquals(localAddrStr + "%" + scopeId, updatedLocalAddr.getHostAddress());
+ assertEquals(scopeId, updatedLocalAddr.getScopeId());
+ }
}