Merge "Add test for stopServiceResolution"
diff --git a/Cronet/TEST_MAPPING b/Cronet/TEST_MAPPING
index b1f3088..9bc5c00 100644
--- a/Cronet/TEST_MAPPING
+++ b/Cronet/TEST_MAPPING
@@ -1,7 +1,7 @@
{
"presubmit": [
{
- "name": "CtsCronetTestCases"
+ "name": "CtsNetHttpTestCases"
}
]
}
diff --git a/Cronet/tests/cts/Android.bp b/Cronet/tests/cts/Android.bp
index d10c68c..51e1e2a 100644
--- a/Cronet/tests/cts/Android.bp
+++ b/Cronet/tests/cts/Android.bp
@@ -18,8 +18,16 @@
default_applicable_licenses: ["Android-Apache-2.0"],
}
+// cronet_test_java_defaults can be used to specify a java_defaults target that
+// either enables or disables Cronet tests. This is used to disable Cronet
+// tests on tm-mainline-prod where the required APIs are not present.
+cronet_test_java_defaults = "CronetTestJavaDefaultsEnabled"
+// This is a placeholder comment to avoid merge conflicts
+// as cronet_test_java_defaults may have different values
+// depending on the branch
+
android_test {
- name: "CtsCronetTestCases",
+ name: "CtsNetHttpTestCases",
compile_multilib: "both", // Include both the 32 and 64 bit versions
defaults: ["cts_defaults"],
sdk_version: "test_current",
diff --git a/Cronet/tests/cts/AndroidTest.xml b/Cronet/tests/cts/AndroidTest.xml
index d2422f1..e0421fd 100644
--- a/Cronet/tests/cts/AndroidTest.xml
+++ b/Cronet/tests/cts/AndroidTest.xml
@@ -23,14 +23,14 @@
<option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
<option name="cleanup-apks" value="true" />
- <option name="test-file-name" value="CtsCronetTestCases.apk" />
+ <option name="test-file-name" value="CtsNetHttpTestCases.apk" />
</target_preparer>
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="android.net.http.cts" />
<option name="runtime-hint" value="10s" />
</test>
- <!-- Only run CtsCronetTestcasess in MTS if the Tethering Mainline module is installed. -->
+ <!-- Only run CtsNetHttpTestCases in MTS if the Tethering Mainline module is installed. -->
<object type="module_controller"
class="com.android.tradefed.testtype.suite.module.MainlineTestModuleController">
<option name="mainline-module-package-name" value="com.google.android.tethering" />
diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java
index 63702f2..f90b3a4 100644
--- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java
+++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java
@@ -1802,7 +1802,7 @@
TestNetworkAgent mobile, TestNetworkAgent wifi, TestNetworkAgent dun) throws Exception {
final NetworkCallback dunNetworkCallback = setupDunUpstreamTest(automatic, inOrder);
- // Pretend cellular connected and expect the upstream to be set.
+ // Pretend cellular connected and expect the upstream to be not set.
mobile.fakeConnect();
mCm.makeDefaultNetwork(mobile, BROADCAST_FIRST);
mLooper.dispatchAll();
diff --git a/service-t/src/com/android/server/NsdService.java b/service-t/src/com/android/server/NsdService.java
index c5eeff3..ce105ce 100644
--- a/service-t/src/com/android/server/NsdService.java
+++ b/service-t/src/com/android/server/NsdService.java
@@ -60,6 +60,7 @@
import com.android.net.module.util.DeviceConfigUtils;
import com.android.net.module.util.PermissionUtils;
import com.android.server.connectivity.mdns.ExecutorProvider;
+import com.android.server.connectivity.mdns.MdnsAdvertiser;
import com.android.server.connectivity.mdns.MdnsDiscoveryManager;
import com.android.server.connectivity.mdns.MdnsMultinetworkSocketClient;
import com.android.server.connectivity.mdns.MdnsSearchOptions;
@@ -74,6 +75,11 @@
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -89,10 +95,22 @@
public class NsdService extends INsdManager.Stub {
private static final String TAG = "NsdService";
private static final String MDNS_TAG = "mDnsConnector";
+ /**
+ * Enable discovery using the Java DiscoveryManager, instead of the legacy mdnsresponder
+ * implementation.
+ */
private static final String MDNS_DISCOVERY_MANAGER_VERSION = "mdns_discovery_manager_version";
private static final String LOCAL_DOMAIN_NAME = "local";
+ // Max label length as per RFC 1034/1035
+ private static final int MAX_LABEL_LENGTH = 63;
- private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
+ /**
+ * Enable advertising using the Java MdnsAdvertiser, instead of the legacy mdnsresponder
+ * implementation.
+ */
+ private static final String MDNS_ADVERTISER_VERSION = "mdns_advertiser_version";
+
+ public static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
private static final long CLEANUP_DELAY_MS = 10000;
private static final int IFACE_IDX_ANY = 0;
@@ -106,6 +124,8 @@
private final MdnsDiscoveryManager mMdnsDiscoveryManager;
@Nullable
private final MdnsSocketProvider mMdnsSocketProvider;
+ @Nullable
+ private final MdnsAdvertiser mAdvertiser;
// WARNING : Accessing these values in any thread is not safe, it must only be changed in the
// state machine thread. If change this outside state machine, it will need to introduce
// synchronization.
@@ -345,7 +365,7 @@
mLegacyClientCount -= 1;
}
}
- if (mMdnsDiscoveryManager != null) {
+ if (mMdnsDiscoveryManager != null || mAdvertiser != null) {
maybeStopMonitoringSocketsIfNoActiveRequest();
}
maybeScheduleStop();
@@ -453,6 +473,7 @@
clientInfo.mClientRequests.delete(clientId);
mIdToClientInfoMap.remove(globalId);
maybeScheduleStop();
+ maybeStopMonitoringSocketsIfNoActiveRequest();
}
private void storeListenerMap(int clientId, int transactionId, MdnsListener listener,
@@ -491,8 +512,31 @@
final Matcher matcher = serviceTypePattern.matcher(serviceType);
if (!matcher.matches()) return null;
return matcher.group(1) == null
- ? serviceType + ".local"
- : matcher.group(1) + "_sub." + matcher.group(2) + ".local";
+ ? serviceType
+ : matcher.group(1) + "_sub." + matcher.group(2);
+ }
+
+ /**
+ * Truncate a service name to up to 63 UTF-8 bytes.
+ *
+ * See RFC6763 4.1.1: service instance names are UTF-8 and up to 63 bytes. Truncating
+ * names used in registerService follows historical behavior (see mdnsresponder
+ * handle_regservice_request).
+ */
+ @NonNull
+ private String truncateServiceName(@NonNull String originalName) {
+ // UTF-8 is at most 4 bytes per character; return early in the common case where
+ // the name can't possibly be over the limit given its string length.
+ if (originalName.length() <= MAX_LABEL_LENGTH / 4) return originalName;
+
+ final Charset utf8 = StandardCharsets.UTF_8;
+ final CharsetEncoder encoder = utf8.newEncoder();
+ final ByteBuffer out = ByteBuffer.allocate(MAX_LABEL_LENGTH);
+ // encode will write as many characters as possible to the out buffer, and just
+ // return an overflow code if there were too many characters (no need to check the
+ // return code here, this method truncates the name on purpose).
+ encoder.encode(CharBuffer.wrap(originalName), out, true /* endOfInput */);
+ return new String(out.array(), 0, out.position(), utf8);
}
@Override
@@ -530,14 +574,16 @@
break;
}
+ final String listenServiceType = serviceType + ".local";
maybeStartMonitoringSockets();
final MdnsListener listener =
- new DiscoveryListener(clientId, id, info, serviceType);
+ new DiscoveryListener(clientId, id, info, listenServiceType);
final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
.setNetwork(info.getNetwork())
.setIsPassiveMode(true)
.build();
- mMdnsDiscoveryManager.registerListener(serviceType, listener, options);
+ mMdnsDiscoveryManager.registerListener(
+ listenServiceType, listener, options);
storeListenerMap(clientId, id, listener, clientInfo);
clientInfo.onDiscoverServicesStarted(clientId, info);
} else {
@@ -615,16 +661,36 @@
break;
}
- maybeStartDaemon();
id = getUniqueId();
- if (registerService(id, args.serviceInfo)) {
- if (DBG) Log.d(TAG, "Register " + clientId + " " + id);
+ if (mAdvertiser != null) {
+ final NsdServiceInfo serviceInfo = args.serviceInfo;
+ final String serviceType = serviceInfo.getServiceType();
+ final String registerServiceType = constructServiceType(serviceType);
+ if (registerServiceType == null) {
+ Log.e(TAG, "Invalid service type: " + serviceType);
+ clientInfo.onRegisterServiceFailed(clientId,
+ NsdManager.FAILURE_INTERNAL_ERROR);
+ break;
+ }
+ serviceInfo.setServiceType(registerServiceType);
+ serviceInfo.setServiceName(truncateServiceName(
+ serviceInfo.getServiceName()));
+
+ maybeStartMonitoringSockets();
+ mAdvertiser.addService(id, serviceInfo);
storeRequestMap(clientId, id, clientInfo, msg.what);
- // Return success after mDns reports success
} else {
- unregisterService(id);
- clientInfo.onRegisterServiceFailed(
- clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ maybeStartDaemon();
+ if (registerService(id, args.serviceInfo)) {
+ if (DBG) Log.d(TAG, "Register " + clientId + " " + id);
+ storeRequestMap(clientId, id, clientInfo, msg.what);
+ // Return success after mDns reports success
+ } else {
+ unregisterService(id);
+ clientInfo.onRegisterServiceFailed(
+ clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ }
+
}
break;
case NsdManager.UNREGISTER_SERVICE:
@@ -640,11 +706,17 @@
}
id = clientInfo.mClientIds.get(clientId);
removeRequestMap(clientId, id, clientInfo);
- if (unregisterService(id)) {
+
+ if (mAdvertiser != null) {
+ mAdvertiser.removeService(id);
clientInfo.onUnregisterServiceSucceeded(clientId);
} else {
- clientInfo.onUnregisterServiceFailed(
- clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ if (unregisterService(id)) {
+ clientInfo.onUnregisterServiceSucceeded(clientId);
+ } else {
+ clientInfo.onUnregisterServiceFailed(
+ clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ }
}
break;
case NsdManager.RESOLVE_SERVICE: {
@@ -668,15 +740,17 @@
NsdManager.FAILURE_INTERNAL_ERROR);
break;
}
+ final String resolveServiceType = serviceType + ".local";
maybeStartMonitoringSockets();
final MdnsListener listener =
- new ResolutionListener(clientId, id, info, serviceType);
+ new ResolutionListener(clientId, id, info, resolveServiceType);
final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
.setNetwork(info.getNetwork())
.setIsPassiveMode(true)
.build();
- mMdnsDiscoveryManager.registerListener(serviceType, listener, options);
+ mMdnsDiscoveryManager.registerListener(
+ resolveServiceType, listener, options);
storeListenerMap(clientId, id, listener, clientInfo);
} else {
if (clientInfo.mResolvedService != null) {
@@ -1035,18 +1109,32 @@
mNsdStateMachine.start();
mMDnsManager = ctx.getSystemService(MDnsManager.class);
mMDnsEventCallback = new MDnsEventCallback(mNsdStateMachine);
- if (deps.isMdnsDiscoveryManagerEnabled(ctx)) {
+
+ final boolean discoveryManagerEnabled = deps.isMdnsDiscoveryManagerEnabled(ctx);
+ final boolean advertiserEnabled = deps.isMdnsAdvertiserEnabled(ctx);
+ if (discoveryManagerEnabled || advertiserEnabled) {
mMdnsSocketProvider = deps.makeMdnsSocketProvider(ctx, handler.getLooper());
+ } else {
+ mMdnsSocketProvider = null;
+ }
+
+ if (discoveryManagerEnabled) {
mMdnsSocketClient =
new MdnsMultinetworkSocketClient(handler.getLooper(), mMdnsSocketProvider);
mMdnsDiscoveryManager =
deps.makeMdnsDiscoveryManager(new ExecutorProvider(), mMdnsSocketClient);
handler.post(() -> mMdnsSocketClient.setCallback(mMdnsDiscoveryManager));
} else {
- mMdnsSocketProvider = null;
mMdnsSocketClient = null;
mMdnsDiscoveryManager = null;
}
+
+ if (advertiserEnabled) {
+ mAdvertiser = deps.makeMdnsAdvertiser(handler.getLooper(), mMdnsSocketProvider,
+ new AdvertiserCallback());
+ } else {
+ mAdvertiser = null;
+ }
}
/**
@@ -1055,10 +1143,10 @@
@VisibleForTesting
public static class Dependencies {
/**
- * Check whether or not MdnsDiscoveryManager feature is enabled.
+ * Check whether the MdnsDiscoveryManager feature is enabled.
*
* @param context The global context information about an app environment.
- * @return true if MdnsDiscoveryManager feature is enabled.
+ * @return true if the MdnsDiscoveryManager feature is enabled.
*/
public boolean isMdnsDiscoveryManagerEnabled(Context context) {
return DeviceConfigUtils.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY,
@@ -1066,6 +1154,17 @@
}
/**
+ * Check whether the MdnsAdvertiser feature is enabled.
+ *
+ * @param context The global context information about an app environment.
+ * @return true if the MdnsAdvertiser feature is enabled.
+ */
+ public boolean isMdnsAdvertiserEnabled(Context context) {
+ return DeviceConfigUtils.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY,
+ MDNS_ADVERTISER_VERSION, false /* defaultEnabled */);
+ }
+
+ /**
* @see MdnsDiscoveryManager
*/
public MdnsDiscoveryManager makeMdnsDiscoveryManager(
@@ -1074,6 +1173,15 @@
}
/**
+ * @see MdnsAdvertiser
+ */
+ public MdnsAdvertiser makeMdnsAdvertiser(
+ @NonNull Looper looper, @NonNull MdnsSocketProvider socketProvider,
+ @NonNull MdnsAdvertiser.AdvertiserCallback cb) {
+ return new MdnsAdvertiser(looper, socketProvider, cb);
+ }
+
+ /**
* @see MdnsSocketProvider
*/
public MdnsSocketProvider makeMdnsSocketProvider(Context context, Looper looper) {
@@ -1131,6 +1239,49 @@
}
}
+ private class AdvertiserCallback implements MdnsAdvertiser.AdvertiserCallback {
+ @Override
+ public void onRegisterServiceSucceeded(int serviceId, NsdServiceInfo registeredInfo) {
+ final ClientInfo clientInfo = getClientInfoOrLog(serviceId);
+ if (clientInfo == null) return;
+
+ final int clientId = getClientIdOrLog(clientInfo, serviceId);
+ if (clientId < 0) return;
+
+ // onRegisterServiceSucceeded only has the service name in its info. This aligns with
+ // historical behavior.
+ final NsdServiceInfo cbInfo = new NsdServiceInfo(registeredInfo.getServiceName(), null);
+ clientInfo.onRegisterServiceSucceeded(clientId, cbInfo);
+ }
+
+ @Override
+ public void onRegisterServiceFailed(int serviceId, int errorCode) {
+ final ClientInfo clientInfo = getClientInfoOrLog(serviceId);
+ if (clientInfo == null) return;
+
+ final int clientId = getClientIdOrLog(clientInfo, serviceId);
+ if (clientId < 0) return;
+
+ clientInfo.onRegisterServiceFailed(clientId, errorCode);
+ }
+
+ private ClientInfo getClientInfoOrLog(int serviceId) {
+ final ClientInfo clientInfo = mIdToClientInfoMap.get(serviceId);
+ if (clientInfo == null) {
+ Log.e(TAG, String.format("Callback for service %d has no client", serviceId));
+ }
+ return clientInfo;
+ }
+
+ private int getClientIdOrLog(@NonNull ClientInfo info, int serviceId) {
+ final int clientId = info.getClientId(serviceId);
+ if (clientId < 0) {
+ Log.e(TAG, String.format("Client ID not found for service %d", serviceId));
+ }
+ return clientId;
+ }
+ }
+
@Override
public INsdServiceConnector connect(INsdManagerCallback cb) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET, "NsdService");
@@ -1400,7 +1551,11 @@
stopResolveService(globalId);
break;
case NsdManager.REGISTER_SERVICE:
- unregisterService(globalId);
+ if (mAdvertiser != null) {
+ mAdvertiser.removeService(globalId);
+ } else {
+ unregisterService(globalId);
+ }
break;
default:
break;
diff --git a/tests/unit/java/com/android/server/NsdServiceTest.java b/tests/unit/java/com/android/server/NsdServiceTest.java
index 56279cf..a8f8121 100644
--- a/tests/unit/java/com/android/server/NsdServiceTest.java
+++ b/tests/unit/java/com/android/server/NsdServiceTest.java
@@ -16,6 +16,7 @@
package com.android.server;
+import static android.net.InetAddresses.parseNumericAddress;
import static android.net.nsd.NsdManager.FAILURE_INTERNAL_ERROR;
import static android.net.nsd.NsdManager.FAILURE_OPERATION_NOT_RUNNING;
@@ -48,7 +49,6 @@
import android.content.ContentResolver;
import android.content.Context;
import android.net.INetd;
-import android.net.InetAddresses;
import android.net.Network;
import android.net.mdns.aidl.DiscoveryInfo;
import android.net.mdns.aidl.GetAddressInfo;
@@ -76,6 +76,7 @@
import androidx.test.filters.SmallTest;
import com.android.server.NsdService.Dependencies;
+import com.android.server.connectivity.mdns.MdnsAdvertiser;
import com.android.server.connectivity.mdns.MdnsDiscoveryManager;
import com.android.server.connectivity.mdns.MdnsServiceBrowserListener;
import com.android.server.connectivity.mdns.MdnsServiceInfo;
@@ -98,6 +99,7 @@
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
+import java.util.Objects;
import java.util.Queue;
// TODOs:
@@ -131,6 +133,7 @@
@Mock MDnsManager mMockMDnsM;
@Mock Dependencies mDeps;
@Mock MdnsDiscoveryManager mDiscoveryManager;
+ @Mock MdnsAdvertiser mAdvertiser;
@Mock MdnsSocketProvider mSocketProvider;
HandlerThread mThread;
TestHandler mHandler;
@@ -401,7 +404,7 @@
final NsdServiceInfo resolvedService = resInfoCaptor.getValue();
assertEquals(SERVICE_NAME, resolvedService.getServiceName());
assertEquals("." + SERVICE_TYPE, resolvedService.getServiceType());
- assertEquals(InetAddresses.parseNumericAddress(serviceAddress), resolvedService.getHost());
+ assertEquals(parseNumericAddress(serviceAddress), resolvedService.getHost());
assertEquals(servicePort, resolvedService.getPort());
assertNull(resolvedService.getNetwork());
assertEquals(interfaceIdx, resolvedService.getInterfaceIndex());
@@ -673,6 +676,16 @@
verify(mDeps).makeMdnsSocketProvider(any(), any());
}
+ private void makeServiceWithMdnsAdvertiserEnabled() {
+ doReturn(true).when(mDeps).isMdnsAdvertiserEnabled(any(Context.class));
+ doReturn(mAdvertiser).when(mDeps).makeMdnsAdvertiser(any(), any(), any());
+ doReturn(mSocketProvider).when(mDeps).makeMdnsSocketProvider(any(), any());
+
+ mService = makeService();
+ verify(mDeps).makeMdnsAdvertiser(any(), any(), any());
+ verify(mDeps).makeMdnsSocketProvider(any(), any());
+ }
+
@Test
public void testMdnsDiscoveryManagerFeature() {
// Create NsdService w/o feature enabled.
@@ -824,7 +837,7 @@
assertTrue(info.getAttributes().containsKey("key"));
assertEquals(1, info.getAttributes().size());
assertArrayEquals(new byte[]{(byte) 0xFF, (byte) 0xFE}, info.getAttributes().get("key"));
- assertEquals(InetAddresses.parseNumericAddress(IPV4_ADDRESS), info.getHost());
+ assertEquals(parseNumericAddress(IPV4_ADDRESS), info.getHost());
assertEquals(network, info.getNetwork());
// Verify the listener has been unregistered.
@@ -833,6 +846,102 @@
verify(mSocketProvider, timeout(CLEANUP_DELAY_MS + TIMEOUT_MS)).stopMonitoringSockets();
}
+ @Test
+ public void testAdvertiseWithMdnsAdvertiser() {
+ makeServiceWithMdnsAdvertiserEnabled();
+
+ final NsdManager client = connectClient(mService);
+ final RegistrationListener regListener = mock(RegistrationListener.class);
+ // final String serviceTypeWithLocalDomain = SERVICE_TYPE + ".local";
+ final ArgumentCaptor<MdnsAdvertiser.AdvertiserCallback> cbCaptor =
+ ArgumentCaptor.forClass(MdnsAdvertiser.AdvertiserCallback.class);
+ verify(mDeps).makeMdnsAdvertiser(any(), any(), cbCaptor.capture());
+
+ final NsdServiceInfo regInfo = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
+ regInfo.setHost(parseNumericAddress("192.0.2.123"));
+ regInfo.setPort(12345);
+ regInfo.setAttribute("testattr", "testvalue");
+ regInfo.setNetwork(new Network(999));
+
+ client.registerService(regInfo, NsdManager.PROTOCOL_DNS_SD, Runnable::run, regListener);
+ waitForIdle();
+ verify(mSocketProvider).startMonitoringSockets();
+ final ArgumentCaptor<Integer> idCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mAdvertiser).addService(idCaptor.capture(), argThat(info ->
+ matches(info, regInfo)));
+
+ // Verify onServiceRegistered callback
+ final MdnsAdvertiser.AdvertiserCallback cb = cbCaptor.getValue();
+ cb.onRegisterServiceSucceeded(idCaptor.getValue(), regInfo);
+
+ verify(regListener, timeout(TIMEOUT_MS)).onServiceRegistered(argThat(info -> matches(info,
+ new NsdServiceInfo(regInfo.getServiceName(), null))));
+
+ client.unregisterService(regListener);
+ waitForIdle();
+ verify(mAdvertiser).removeService(idCaptor.getValue());
+ verify(regListener, timeout(TIMEOUT_MS)).onServiceUnregistered(
+ argThat(info -> matches(info, regInfo)));
+ verify(mSocketProvider, timeout(TIMEOUT_MS)).stopMonitoringSockets();
+ }
+
+ @Test
+ public void testAdvertiseWithMdnsAdvertiser_FailedWithInvalidServiceType() {
+ makeServiceWithMdnsAdvertiserEnabled();
+
+ final NsdManager client = connectClient(mService);
+ final RegistrationListener regListener = mock(RegistrationListener.class);
+ // final String serviceTypeWithLocalDomain = SERVICE_TYPE + ".local";
+ final ArgumentCaptor<MdnsAdvertiser.AdvertiserCallback> cbCaptor =
+ ArgumentCaptor.forClass(MdnsAdvertiser.AdvertiserCallback.class);
+ verify(mDeps).makeMdnsAdvertiser(any(), any(), cbCaptor.capture());
+
+ final NsdServiceInfo regInfo = new NsdServiceInfo(SERVICE_NAME, "invalid_type");
+ regInfo.setHost(parseNumericAddress("192.0.2.123"));
+ regInfo.setPort(12345);
+ regInfo.setAttribute("testattr", "testvalue");
+ regInfo.setNetwork(new Network(999));
+
+ client.registerService(regInfo, NsdManager.PROTOCOL_DNS_SD, Runnable::run, regListener);
+ waitForIdle();
+ verify(mAdvertiser, never()).addService(anyInt(), any());
+
+ verify(regListener, timeout(TIMEOUT_MS)).onRegistrationFailed(
+ argThat(info -> matches(info, regInfo)), eq(FAILURE_INTERNAL_ERROR));
+ }
+
+ @Test
+ public void testAdvertiseWithMdnsAdvertiser_LongServiceName() {
+ makeServiceWithMdnsAdvertiserEnabled();
+
+ final NsdManager client = connectClient(mService);
+ final RegistrationListener regListener = mock(RegistrationListener.class);
+ // final String serviceTypeWithLocalDomain = SERVICE_TYPE + ".local";
+ final ArgumentCaptor<MdnsAdvertiser.AdvertiserCallback> cbCaptor =
+ ArgumentCaptor.forClass(MdnsAdvertiser.AdvertiserCallback.class);
+ verify(mDeps).makeMdnsAdvertiser(any(), any(), cbCaptor.capture());
+
+ final NsdServiceInfo regInfo = new NsdServiceInfo("a".repeat(70), SERVICE_TYPE);
+ regInfo.setHost(parseNumericAddress("192.0.2.123"));
+ regInfo.setPort(12345);
+ regInfo.setAttribute("testattr", "testvalue");
+ regInfo.setNetwork(new Network(999));
+
+ client.registerService(regInfo, NsdManager.PROTOCOL_DNS_SD, Runnable::run, regListener);
+ waitForIdle();
+ final ArgumentCaptor<Integer> idCaptor = ArgumentCaptor.forClass(Integer.class);
+ // Service name is truncated to 63 characters
+ verify(mAdvertiser).addService(idCaptor.capture(),
+ argThat(info -> info.getServiceName().equals("a".repeat(63))));
+
+ // Verify onServiceRegistered callback
+ final MdnsAdvertiser.AdvertiserCallback cb = cbCaptor.getValue();
+ cb.onRegisterServiceSucceeded(idCaptor.getValue(), regInfo);
+
+ verify(regListener, timeout(TIMEOUT_MS)).onServiceRegistered(
+ argThat(info -> matches(info, new NsdServiceInfo(regInfo.getServiceName(), null))));
+ }
+
private void waitForIdle() {
HandlerUtils.waitForIdle(mHandler, TIMEOUT_MS);
}
@@ -877,6 +986,19 @@
verify(mMockMDnsM, timeout(cleanupDelayMs + TIMEOUT_MS)).stopDaemon();
}
+ /**
+ * Return true if two service info are the same.
+ *
+ * Useful for argument matchers as {@link NsdServiceInfo} does not implement equals.
+ */
+ private boolean matches(NsdServiceInfo a, NsdServiceInfo b) {
+ return Objects.equals(a.getServiceName(), b.getServiceName())
+ && Objects.equals(a.getServiceType(), b.getServiceType())
+ && Objects.equals(a.getHost(), b.getHost())
+ && Objects.equals(a.getNetwork(), b.getNetwork())
+ && Objects.equals(a.getAttributes(), b.getAttributes());
+ }
+
public static class TestHandler extends Handler {
public Message lastMessage;
diff --git a/tests/unit/java/com/android/server/VpnManagerServiceTest.java b/tests/unit/java/com/android/server/VpnManagerServiceTest.java
index c8a93a6..deb56ef 100644
--- a/tests/unit/java/com/android/server/VpnManagerServiceTest.java
+++ b/tests/unit/java/com/android/server/VpnManagerServiceTest.java
@@ -131,6 +131,11 @@
Vpn vpn, VpnProfile profile) {
return mLockdownVpnTracker;
}
+
+ @Override
+ public @UserIdInt int getMainUserId() {
+ return UserHandle.USER_SYSTEM;
+ }
}
@Before
diff --git a/tools/gn2bp/Android.bp.swp b/tools/gn2bp/Android.bp.swp
index ebf1a9b..4d4a0b2 100644
--- a/tools/gn2bp/Android.bp.swp
+++ b/tools/gn2bp/Android.bp.swp
@@ -14,6 +14,13 @@
//
// This file is automatically generated by gen_android_bp. Do not edit.
+// GN: PACKAGE
+package {
+ default_applicable_licenses: [
+ "external_cronet_license",
+ ],
+}
+
// GN: //components/cronet/android:cronet_api_java
java_library {
name: "cronet_aml_api_java",
@@ -10645,3 +10652,55 @@
],
}
+// GN: LICENSE
+license {
+ name: "external_cronet_license",
+ license_kinds: [
+ "SPDX-license-identifier-AFL-2.0",
+ "SPDX-license-identifier-Apache-2.0",
+ "SPDX-license-identifier-BSD",
+ "SPDX-license-identifier-BSL-1.0",
+ "SPDX-license-identifier-GPL",
+ "SPDX-license-identifier-GPL-2.0",
+ "SPDX-license-identifier-GPL-3.0",
+ "SPDX-license-identifier-ICU",
+ "SPDX-license-identifier-ISC",
+ "SPDX-license-identifier-LGPL",
+ "SPDX-license-identifier-LGPL-2.1",
+ "SPDX-license-identifier-MIT",
+ "SPDX-license-identifier-MPL",
+ "SPDX-license-identifier-MPL-2.0",
+ "SPDX-license-identifier-NCSA",
+ "SPDX-license-identifier-OpenSSL",
+ "SPDX-license-identifier-Unicode-DFS",
+ "legacy_unencumbered",
+ ],
+ license_text: [
+ "LICENSE",
+ "base/third_party/double_conversion/LICENSE",
+ "base/third_party/dynamic_annotations/LICENSE",
+ "base/third_party/icu/LICENSE",
+ "base/third_party/nspr/LICENSE",
+ "base/third_party/superfasthash/LICENSE",
+ "base/third_party/symbolize/LICENSE",
+ "base/third_party/valgrind/LICENSE",
+ "base/third_party/xdg_user_dirs/LICENSE",
+ "net/third_party/quiche/src/LICENSE",
+ "net/third_party/uri_template/LICENSE",
+ "third_party/abseil-cpp/LICENSE",
+ "third_party/ashmem/LICENSE",
+ "third_party/boringssl/src/LICENSE",
+ "third_party/boringssl/src/third_party/fiat/LICENSE",
+ "third_party/boringssl/src/third_party/googletest/LICENSE",
+ "third_party/boringssl/src/third_party/wycheproof_testvectors/LICENSE",
+ "third_party/brotli/LICENSE",
+ "third_party/icu/LICENSE",
+ "third_party/icu/scripts/LICENSE",
+ "third_party/libevent/LICENSE",
+ "third_party/metrics_proto/LICENSE",
+ "third_party/modp_b64/LICENSE",
+ "third_party/protobuf/LICENSE",
+ "third_party/protobuf/third_party/utf8_range/LICENSE",
+ ],
+}
+
diff --git a/tools/gn2bp/gen_android_bp b/tools/gn2bp/gen_android_bp
index e10c415..7829694 100755
--- a/tools/gn2bp/gen_android_bp
+++ b/tools/gn2bp/gen_android_bp
@@ -391,6 +391,9 @@
self.processor_class = None
self.sdk_version = None
self.javacflags = set()
+ self.license_kinds = set()
+ self.license_text = set()
+ self.default_applicable_licenses = set()
def to_string(self, output):
if self.comment:
@@ -446,6 +449,9 @@
self._output_field(output, 'processor_class')
self._output_field(output, 'sdk_version')
self._output_field(output, 'javacflags')
+ self._output_field(output, 'license_kinds')
+ self._output_field(output, 'license_text')
+ self._output_field(output, 'default_applicable_licenses')
if self.rtti:
self._output_field(output, 'rtti')
@@ -1647,6 +1653,59 @@
return blueprint
+def create_license_module(blueprint):
+ module = Module("license", "external_cronet_license", "LICENSE")
+ module.license_kinds.update({
+ 'SPDX-license-identifier-LGPL-2.1',
+ 'SPDX-license-identifier-GPL-2.0',
+ 'SPDX-license-identifier-MPL',
+ 'SPDX-license-identifier-ISC',
+ 'SPDX-license-identifier-GPL',
+ 'SPDX-license-identifier-AFL-2.0',
+ 'SPDX-license-identifier-MPL-2.0',
+ 'SPDX-license-identifier-BSD',
+ 'SPDX-license-identifier-Apache-2.0',
+ 'SPDX-license-identifier-BSL-1.0',
+ 'SPDX-license-identifier-LGPL',
+ 'SPDX-license-identifier-GPL-3.0',
+ 'SPDX-license-identifier-Unicode-DFS',
+ 'SPDX-license-identifier-NCSA',
+ 'SPDX-license-identifier-OpenSSL',
+ 'SPDX-license-identifier-MIT',
+ "SPDX-license-identifier-ICU",
+ 'legacy_unencumbered', # public domain
+ })
+ module.license_text.update({
+ "LICENSE",
+ "net/third_party/uri_template/LICENSE",
+ "net/third_party/quiche/src/LICENSE",
+ "base/third_party/symbolize/LICENSE",
+ "base/third_party/superfasthash/LICENSE",
+ "base/third_party/xdg_user_dirs/LICENSE",
+ "base/third_party/double_conversion/LICENSE",
+ "base/third_party/nspr/LICENSE",
+ "base/third_party/dynamic_annotations/LICENSE",
+ "base/third_party/icu/LICENSE",
+ "base/third_party/valgrind/LICENSE",
+ "third_party/brotli/LICENSE",
+ "third_party/protobuf/LICENSE",
+ "third_party/protobuf/third_party/utf8_range/LICENSE",
+ "third_party/metrics_proto/LICENSE",
+ "third_party/boringssl/src/LICENSE",
+ "third_party/boringssl/src/third_party/googletest/LICENSE",
+ "third_party/boringssl/src/third_party/wycheproof_testvectors/LICENSE",
+ "third_party/boringssl/src/third_party/fiat/LICENSE",
+ "third_party/libevent/LICENSE",
+ "third_party/ashmem/LICENSE",
+ "third_party/icu/LICENSE",
+ "third_party/icu/scripts/LICENSE",
+ "third_party/abseil-cpp/LICENSE",
+ "third_party/modp_b64/LICENSE",
+ })
+ default_license = Module("package", "", "PACKAGE")
+ default_license.default_applicable_licenses.add(module.name)
+ blueprint.add_module(module)
+ blueprint.add_module(default_license)
def main():
parser = argparse.ArgumentParser(
@@ -1698,7 +1757,7 @@
# Add any proto groups to the blueprint.
for l_name, t_names in proto_groups.items():
create_proto_group_modules(blueprint, gn, l_name, t_names)
-
+ create_license_module(blueprint)
output = [
"""// Copyright (C) 2022 The Android Open Source Project
//