Merge "Update BPF EXT maps with new ignore_on fields."
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..68e3cf1 100644
--- a/Cronet/tests/cts/Android.bp
+++ b/Cronet/tests/cts/Android.bp
@@ -18,10 +18,36 @@
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
+
+java_defaults {
+ name: "CronetTestJavaDefaultsEnabled",
+ enabled: true,
+}
+
+java_defaults {
+ name: "CronetTestJavaDefaultsDisabled",
+ enabled: false,
+}
+
+java_defaults {
+ name: "CronetTestJavaDefaults",
+ defaults: [cronet_test_java_defaults],
+}
+
android_test {
- name: "CtsCronetTestCases",
+ name: "CtsNetHttpTestCases",
compile_multilib: "both", // Include both the 32 and 64 bit versions
- defaults: ["cts_defaults"],
+ defaults: [
+ "CronetTestJavaDefaults",
+ "cts_defaults",
+ ],
sdk_version: "test_current",
srcs: [
"src/**/*.java",
@@ -39,7 +65,7 @@
"android.test.base",
"android.test.mock",
"androidx.annotation_annotation",
- "framework-cronet",
+ "framework-tethering",
"org.apache.http.legacy",
],
diff --git a/Cronet/tests/cts/AndroidManifest.xml b/Cronet/tests/cts/AndroidManifest.xml
index 5a92dea..eaa24aa 100644
--- a/Cronet/tests/cts/AndroidManifest.xml
+++ b/Cronet/tests/cts/AndroidManifest.xml
@@ -25,7 +25,6 @@
<application android:networkSecurityConfig="@xml/network_security_config">
<uses-library android:name="android.test.runner"/>
- <uses-library android:name="framework-cronet"/>
</application>
<instrumentation
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/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java b/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java
index 2898f54..598be0e 100644
--- a/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java
+++ b/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java
@@ -22,7 +22,12 @@
import android.content.Context;
import android.net.ConnectivityManager;
+import android.net.http.HttpEngine;
+import android.net.http.UrlRequest;
+import android.net.http.UrlRequest.Status;
+import android.net.http.UrlResponseInfo;
import android.net.http.cts.util.CronetCtsTestServer;
+import android.net.http.cts.util.TestStatusListener;
import android.net.http.cts.util.TestUrlRequestCallback;
import android.net.http.cts.util.TestUrlRequestCallback.ResponseStep;
@@ -30,9 +35,6 @@
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
-import org.chromium.net.CronetEngine;
-import org.chromium.net.UrlRequest;
-import org.chromium.net.UrlResponseInfo;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -42,7 +44,8 @@
public class CronetUrlRequestTest {
private static final String TAG = CronetUrlRequestTest.class.getSimpleName();
- @NonNull private CronetEngine mCronetEngine;
+ @NonNull private HttpEngine mHttpEngine;
+ @NonNull private TestUrlRequestCallback mCallback;
@NonNull private ConnectivityManager mCm;
@NonNull private CronetCtsTestServer mTestServer;
@@ -50,18 +53,19 @@
public void setUp() throws Exception {
Context context = InstrumentationRegistry.getInstrumentation().getContext();
mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- CronetEngine.Builder builder = new CronetEngine.Builder(context);
- builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024)
- .enableHttp2(true)
- // .enableBrotli(true)
- .enableQuic(true);
- mCronetEngine = builder.build();
+ HttpEngine.Builder builder = new HttpEngine.Builder(context);
+ builder.setEnableHttpCache(HttpEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024)
+ .setEnableHttp2(true)
+ // .setEnableBrotli(true)
+ .setEnableQuic(true);
+ mHttpEngine = builder.build();
+ mCallback = new TestUrlRequestCallback();
mTestServer = new CronetCtsTestServer(context);
}
@After
public void tearDown() throws Exception {
- mCronetEngine.shutdown();
+ mHttpEngine.shutdown();
mTestServer.shutdown();
}
@@ -73,21 +77,33 @@
assertNotNull("This test requires a working Internet connection", mCm.getActiveNetwork());
}
+ private UrlRequest buildUrlRequest(String url) {
+ return mHttpEngine.newUrlRequestBuilder(url, mCallback, mCallback.getExecutor()).build();
+ }
+
@Test
public void testUrlRequestGet_CompletesSuccessfully() throws Exception {
assertHasTestableNetworks();
String url = mTestServer.getSuccessUrl();
- TestUrlRequestCallback callback = new TestUrlRequestCallback();
- UrlRequest.Builder builder =
- mCronetEngine.newUrlRequestBuilder(url, callback, callback.getExecutor());
- builder.build().start();
+ UrlRequest request = buildUrlRequest(url);
+ request.start();
- callback.expectCallback(ResponseStep.ON_SUCCEEDED);
+ mCallback.expectCallback(ResponseStep.ON_SUCCEEDED);
- UrlResponseInfo info = callback.mResponseInfo;
+ UrlResponseInfo info = mCallback.mResponseInfo;
assertEquals(
"Unexpected http status code from " + url + ".", 200, info.getHttpStatusCode());
assertGreaterThan(
"Received byte from " + url + " is 0.", (int) info.getReceivedByteCount(), 0);
}
+
+ @Test
+ public void testUrlRequestStatus_InvalidBeforeRequestStarts() throws Exception {
+ UrlRequest request = buildUrlRequest(mTestServer.getSuccessUrl());
+ // Calling before request is started should give Status.INVALID,
+ // since the native adapter is not created.
+ TestStatusListener statusListener = new TestStatusListener();
+ request.getStatus(statusListener);
+ statusListener.expectStatus(Status.INVALID);
+ }
}
diff --git a/Cronet/tests/cts/src/android/net/http/cts/util/TestStatusListener.kt b/Cronet/tests/cts/src/android/net/http/cts/util/TestStatusListener.kt
new file mode 100644
index 0000000..e526c7d
--- /dev/null
+++ b/Cronet/tests/cts/src/android/net/http/cts/util/TestStatusListener.kt
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2022 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.http.cts.util
+
+import android.net.http.UrlRequest.StatusListener
+import java.util.concurrent.CompletableFuture
+import java.util.concurrent.TimeUnit
+import org.junit.Assert.assertSame
+
+private const val TIMEOUT_MS = 12000L
+
+/** Test status listener for requests */
+class TestStatusListener : StatusListener() {
+ private val statusFuture = CompletableFuture<Int>()
+
+ override fun onStatus(status: Int) {
+ statusFuture.complete(status)
+ }
+
+ /** Fails if the expected status is not the returned status */
+ fun expectStatus(expected: Int) {
+ assertSame(expected, statusFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS))
+ }
+}
diff --git a/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java b/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java
index c7143f5..0b9e90f 100644
--- a/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java
+++ b/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java
@@ -24,15 +24,14 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import android.net.http.CallbackException;
+import android.net.http.HttpException;
+import android.net.http.InlineExecutionProhibitedException;
+import android.net.http.UrlRequest;
+import android.net.http.UrlResponseInfo;
import android.os.ConditionVariable;
import android.os.StrictMode;
-import org.chromium.net.CallbackException;
-import org.chromium.net.CronetException;
-import org.chromium.net.InlineExecutionProhibitedException;
-import org.chromium.net.UrlRequest;
-import org.chromium.net.UrlResponseInfo;
-
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
@@ -50,7 +49,7 @@
public ArrayList<UrlResponseInfo> mRedirectResponseInfoList = new ArrayList<>();
public ArrayList<String> mRedirectUrlList = new ArrayList<>();
public UrlResponseInfo mResponseInfo;
- public CronetException mError;
+ public HttpException mError;
public ResponseStep mResponseStep = ResponseStep.NOTHING;
@@ -89,7 +88,7 @@
// Signaled on each step when mAutoAdvance is false.
private final ConditionVariable mStepBlock = new ConditionVariable();
- // Executor Service for Cronet callbacks.
+ // Executor Service for Http callbacks.
private final ExecutorService mExecutorService;
private Thread mExecutorThread;
@@ -349,7 +348,7 @@
}
@Override
- public void onFailed(UrlRequest request, UrlResponseInfo info, CronetException error) {
+ public void onFailed(UrlRequest request, UrlResponseInfo info, HttpException error) {
// If the failure is because of prohibited direct execution, the test shouldn't fail
// since the request already did.
if (error.getCause() instanceof InlineExecutionProhibitedException) {
diff --git a/Tethering/AndroidManifest.xml b/Tethering/AndroidManifest.xml
index b832e16..23467e7 100644
--- a/Tethering/AndroidManifest.xml
+++ b/Tethering/AndroidManifest.xml
@@ -43,7 +43,9 @@
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
+ <!-- Sending non-protected broadcast from system uid is not allowed. -->
<protected-broadcast android:name="com.android.server.connectivity.tethering.DISABLE_TETHERING" />
+ <protected-broadcast android:name="com.android.server.connectivity.KeepaliveTracker.TCP_POLLING_ALARM" />
<application
android:process="com.android.networkstack.process"
diff --git a/Tethering/apex/Android.bp b/Tethering/apex/Android.bp
index 8840394..b26911c 100644
--- a/Tethering/apex/Android.bp
+++ b/Tethering/apex/Android.bp
@@ -50,11 +50,40 @@
// as the above target may have different "enabled" values
// depending on the branch
+// cronet_in_tethering_apex_defaults can be used to specify an apex_defaults target that either
+// enables or disables inclusion of Cronet in the Tethering apex. This is used to disable Cronet
+// on tm-mainline-prod. Note: in order for Cronet APIs to work Cronet must also be enabled
+// by the cronet_java_*_defaults in common/TetheringLib/Android.bp.
+cronet_in_tethering_apex_defaults = "CronetInTetheringApexDefaultsEnabled"
+// This is a placeholder comment to avoid merge conflicts
+// as cronet_apex_defaults may have different values
+// depending on the branch
+
+apex_defaults {
+ name: "CronetInTetheringApexDefaults",
+ defaults: [cronet_in_tethering_apex_defaults],
+}
+
+apex_defaults {
+ name: "CronetInTetheringApexDefaultsEnabled",
+ jni_libs: ["cronet_aml_components_cronet_android_cronet"],
+ arch: {
+ riscv64: {
+ // TODO: remove this when there is a riscv64 libcronet
+ exclude_jni_libs: ["cronet_aml_components_cronet_android_cronet"],
+ },
+ },
+}
+
+apex_defaults {
+ name: "CronetInTetheringApexDefaultsDisabled",
+}
+
apex {
name: "com.android.tethering",
defaults: [
"ConnectivityApexDefaults",
- "CronetApexDefaults",
+ "CronetInTetheringApexDefaults",
"r-launched-apex-module",
],
compile_multilib: "both",
@@ -183,6 +212,7 @@
"android.app.usage",
"android.nearby",
"android.net",
+ "android.net.http",
"android.net.netstats",
"android.net.util",
],
@@ -197,6 +227,8 @@
"android.nearby.aidl",
"android.net.apf",
"android.net.connectivity",
+ "android.net.http.apihelpers",
+ "android.net.http.internal",
"android.net.netstats.provider",
"android.net.nsd",
"android.net.wear",
diff --git a/Tethering/common/TetheringLib/Android.bp b/Tethering/common/TetheringLib/Android.bp
index 481557b..cbdf0c0 100644
--- a/Tethering/common/TetheringLib/Android.bp
+++ b/Tethering/common/TetheringLib/Android.bp
@@ -17,9 +17,30 @@
default_applicable_licenses: ["Android-Apache-2.0"],
}
+// Both cronet_java_defaults and cronet_java_prejarjar_defaults can be used to
+// specify a java_defaults target that either enables or disables Cronet. This
+// is used to disable Cronet on tm-mainline-prod.
+// Note: they must either both be enabled or disabled.
+cronet_java_defaults = "CronetJavaDefaultsEnabled"
+cronet_java_prejarjar_defaults = "CronetJavaPrejarjarDefaultsEnabled"
+// This is a placeholder comment to avoid merge conflicts
+// as cronet_defaults may have different values
+// depending on the branch
+
+// cronet_java_defaults_enabled_srcs is used to specify the srcs of CronetJavaDefaultsEnabled
+// This is required until the external/cronet is auto-merged to tm-mainline-prod and
+// :cronet_aml_api_sources is available
+cronet_java_defaults_enabled_srcs = [":cronet_aml_api_sources"]
+// This is a placeholder comment to avoid merge conflicts
+// as cronet_defaults may have different values
+// depending on the branch
+
java_sdk_library {
name: "framework-tethering",
- defaults: ["framework-module-defaults"],
+ defaults: [
+ "CronetJavaDefaults",
+ "framework-tethering-defaults",
+ ],
impl_library_visibility: [
"//packages/modules/Connectivity/Tethering:__subpackages__",
"//packages/modules/Connectivity/framework",
@@ -45,24 +66,103 @@
"//packages/modules/NetworkStack/tests:__subpackages__",
"//packages/modules/Wifi/service/tests/wifitests",
],
-
- srcs: [":framework-tethering-srcs"],
- libs: ["framework-connectivity.stubs.module_lib"],
stub_only_libs: ["framework-connectivity.stubs.module_lib"],
+
+ jarjar_rules: ":framework-tethering-jarjar-rules",
+ installable: true,
+
+ hostdex: true, // for hiddenapi check
+ permitted_packages: ["android.net"],
+}
+
+java_defaults {
+ name: "CronetJavaDefaults",
+ defaults: [cronet_java_defaults],
+}
+
+java_defaults {
+ name: "CronetJavaDefaultsEnabled",
+ srcs: cronet_java_defaults_enabled_srcs,
+ libs: [
+ "androidx.annotation_annotation",
+ ],
+ impl_only_static_libs: [
+ "cronet_aml_java",
+ ],
+ // STOPSHIP(b/265674359): fix all Cronet lint warnings and re-enable lint
+ // directly in framework-tethering
+ lint: {
+ enabled: false,
+ },
+ api_lint: {
+ enabled: false,
+ },
+ api_dir: "cronet_enabled/api",
+}
+
+java_defaults {
+ name: "CronetJavaDefaultsDisabled",
+ lint: { strict_updatability_linting: true },
+}
+
+java_defaults {
+ name: "CronetJavaPrejarjarDefaults",
+ defaults: [cronet_java_prejarjar_defaults],
+}
+
+java_defaults {
+ name: "CronetJavaPrejarjarDefaultsDisabled",
+}
+
+java_defaults {
+ name: "CronetJavaPrejarjarDefaultsEnabled",
+ static_libs: [
+ "cronet_aml_api_java",
+ "cronet_aml_java"
+ ],
+}
+
+java_library {
+ name: "framework-tethering-pre-jarjar",
+ defaults: [
+ "framework-tethering-defaults",
+ "CronetJavaPrejarjarDefaults",
+ ],
+}
+
+java_genrule {
+ name: "framework-tethering-jarjar-rules",
+ tool_files: [
+ ":framework-tethering-pre-jarjar{.jar}",
+ ":framework-tethering.stubs.module_lib{.jar}",
+ "jarjar-excludes.txt",
+ ],
+ tools: [
+ "jarjar-rules-generator",
+ ],
+ out: ["framework_tethering_jarjar_rules.txt"],
+ cmd: "$(location jarjar-rules-generator) " +
+ "$(location :framework-tethering-pre-jarjar{.jar}) " +
+ "--apistubs $(location :framework-tethering.stubs.module_lib{.jar}) " +
+ "--prefix android.net.http.internal " +
+ "--excludes $(location jarjar-excludes.txt) " +
+ "--output $(out)",
+}
+
+java_defaults {
+ name: "framework-tethering-defaults",
+ defaults: ["framework-module-defaults"],
+ srcs: [
+ ":framework-tethering-srcs"
+ ],
+ libs: ["framework-connectivity.stubs.module_lib"],
aidl: {
include_dirs: [
"packages/modules/Connectivity/framework/aidl-export",
],
},
-
- jarjar_rules: "jarjar-rules.txt",
- installable: true,
-
- hostdex: true, // for hiddenapi check
apex_available: ["com.android.tethering"],
- permitted_packages: ["android.net"],
min_sdk_version: "30",
- lint: { strict_updatability_linting: true },
}
filegroup {
diff --git a/Tethering/common/TetheringLib/cronet_enabled/api/current.txt b/Tethering/common/TetheringLib/cronet_enabled/api/current.txt
new file mode 100644
index 0000000..c0157b7
--- /dev/null
+++ b/Tethering/common/TetheringLib/cronet_enabled/api/current.txt
@@ -0,0 +1,194 @@
+// Signature format: 2.0
+package android.net.http {
+
+ public abstract class CallbackException extends android.net.http.HttpException {
+ ctor protected CallbackException(String, Throwable);
+ }
+
+ public class ConnectionMigrationOptions {
+ method @Nullable public Boolean getEnableDefaultNetworkMigration();
+ method @Nullable public Boolean getEnablePathDegradationMigration();
+ }
+
+ public static class ConnectionMigrationOptions.Builder {
+ ctor public ConnectionMigrationOptions.Builder();
+ method public android.net.http.ConnectionMigrationOptions build();
+ method public android.net.http.ConnectionMigrationOptions.Builder setEnableDefaultNetworkMigration(boolean);
+ method public android.net.http.ConnectionMigrationOptions.Builder setEnablePathDegradationMigration(boolean);
+ }
+
+ public final class DnsOptions {
+ method @Nullable public Boolean getPersistHostCache();
+ method @Nullable public java.time.Duration getPersistHostCachePeriod();
+ }
+
+ public static final class DnsOptions.Builder {
+ ctor public DnsOptions.Builder();
+ method public android.net.http.DnsOptions build();
+ method public android.net.http.DnsOptions.Builder setPersistHostCache(boolean);
+ method public android.net.http.DnsOptions.Builder setPersistHostCachePeriod(java.time.Duration);
+ }
+
+ public abstract class HttpEngine {
+ method public abstract java.net.URLStreamHandlerFactory createURLStreamHandlerFactory();
+ method public abstract String getVersionString();
+ method public abstract android.net.http.UrlRequest.Builder newUrlRequestBuilder(String, android.net.http.UrlRequest.Callback, java.util.concurrent.Executor);
+ method public abstract java.net.URLConnection openConnection(java.net.URL) throws java.io.IOException;
+ method public abstract void shutdown();
+ }
+
+ public static class HttpEngine.Builder {
+ ctor public HttpEngine.Builder(android.content.Context);
+ method public android.net.http.HttpEngine.Builder addPublicKeyPins(String, java.util.Set<byte[]>, boolean, java.time.Instant);
+ method public android.net.http.HttpEngine.Builder addQuicHint(String, int, int);
+ method public android.net.http.HttpEngine build();
+ method public String getDefaultUserAgent();
+ method public android.net.http.HttpEngine.Builder setConnectionMigrationOptions(android.net.http.ConnectionMigrationOptions);
+ method public android.net.http.HttpEngine.Builder setDnsOptions(android.net.http.DnsOptions);
+ method public android.net.http.HttpEngine.Builder setEnableBrotli(boolean);
+ method public android.net.http.HttpEngine.Builder setEnableHttp2(boolean);
+ method public android.net.http.HttpEngine.Builder setEnableHttpCache(int, long);
+ method public android.net.http.HttpEngine.Builder setEnablePublicKeyPinningBypassForLocalTrustAnchors(boolean);
+ method public android.net.http.HttpEngine.Builder setEnableQuic(boolean);
+ method public android.net.http.HttpEngine.Builder setQuicOptions(android.net.http.QuicOptions);
+ method public android.net.http.HttpEngine.Builder setStoragePath(String);
+ method public android.net.http.HttpEngine.Builder setUserAgent(String);
+ field public static final int HTTP_CACHE_DISABLED = 0; // 0x0
+ field public static final int HTTP_CACHE_DISK = 3; // 0x3
+ field public static final int HTTP_CACHE_DISK_NO_HTTP = 2; // 0x2
+ field public static final int HTTP_CACHE_IN_MEMORY = 1; // 0x1
+ }
+
+ public class HttpException extends java.io.IOException {
+ ctor public HttpException(String, Throwable);
+ }
+
+ public final class InlineExecutionProhibitedException extends java.util.concurrent.RejectedExecutionException {
+ ctor public InlineExecutionProhibitedException();
+ }
+
+ public abstract class NetworkException extends android.net.http.HttpException {
+ ctor public NetworkException(String, Throwable);
+ method public abstract int getErrorCode();
+ method public abstract boolean isImmediatelyRetryable();
+ field public static final int ERROR_ADDRESS_UNREACHABLE = 9; // 0x9
+ field public static final int ERROR_CONNECTION_CLOSED = 5; // 0x5
+ field public static final int ERROR_CONNECTION_REFUSED = 7; // 0x7
+ field public static final int ERROR_CONNECTION_RESET = 8; // 0x8
+ field public static final int ERROR_CONNECTION_TIMED_OUT = 6; // 0x6
+ field public static final int ERROR_HOSTNAME_NOT_RESOLVED = 1; // 0x1
+ field public static final int ERROR_INTERNET_DISCONNECTED = 2; // 0x2
+ field public static final int ERROR_NETWORK_CHANGED = 3; // 0x3
+ field public static final int ERROR_OTHER = 11; // 0xb
+ field public static final int ERROR_QUIC_PROTOCOL_FAILED = 10; // 0xa
+ field public static final int ERROR_TIMED_OUT = 4; // 0x4
+ }
+
+ public abstract class QuicException extends android.net.http.NetworkException {
+ ctor protected QuicException(String, Throwable);
+ }
+
+ public class QuicOptions {
+ method @Nullable public String getHandshakeUserAgent();
+ method @Nullable public Integer getInMemoryServerConfigsCacheSize();
+ method public java.util.Set<java.lang.String> getQuicHostAllowlist();
+ }
+
+ public static class QuicOptions.Builder {
+ ctor public QuicOptions.Builder();
+ method public android.net.http.QuicOptions.Builder addAllowedQuicHost(String);
+ method public android.net.http.QuicOptions build();
+ method public android.net.http.QuicOptions.Builder setHandshakeUserAgent(String);
+ method public android.net.http.QuicOptions.Builder setInMemoryServerConfigsCacheSize(int);
+ }
+
+ public abstract class UploadDataProvider implements java.io.Closeable {
+ ctor public UploadDataProvider();
+ method public void close() throws java.io.IOException;
+ method public abstract long getLength() throws java.io.IOException;
+ method public abstract void read(android.net.http.UploadDataSink, java.nio.ByteBuffer) throws java.io.IOException;
+ method public abstract void rewind(android.net.http.UploadDataSink) throws java.io.IOException;
+ }
+
+ public abstract class UploadDataSink {
+ ctor public UploadDataSink();
+ method public abstract void onReadError(Exception);
+ method public abstract void onReadSucceeded(boolean);
+ method public abstract void onRewindError(Exception);
+ method public abstract void onRewindSucceeded();
+ }
+
+ public abstract class UrlRequest {
+ method public abstract void cancel();
+ method public abstract void followRedirect();
+ method public abstract void getStatus(android.net.http.UrlRequest.StatusListener);
+ method public abstract boolean isDone();
+ method public abstract void read(java.nio.ByteBuffer);
+ method public abstract void start();
+ }
+
+ public abstract static class UrlRequest.Builder {
+ method public abstract android.net.http.UrlRequest.Builder addHeader(String, String);
+ method public abstract android.net.http.UrlRequest.Builder allowDirectExecutor();
+ method public abstract android.net.http.UrlRequest build();
+ method public abstract android.net.http.UrlRequest.Builder disableCache();
+ method public abstract android.net.http.UrlRequest.Builder setHttpMethod(String);
+ method public abstract android.net.http.UrlRequest.Builder setPriority(int);
+ method public abstract android.net.http.UrlRequest.Builder setUploadDataProvider(android.net.http.UploadDataProvider, java.util.concurrent.Executor);
+ field public static final int REQUEST_PRIORITY_HIGHEST = 4; // 0x4
+ field public static final int REQUEST_PRIORITY_IDLE = 0; // 0x0
+ field public static final int REQUEST_PRIORITY_LOW = 2; // 0x2
+ field public static final int REQUEST_PRIORITY_LOWEST = 1; // 0x1
+ field public static final int REQUEST_PRIORITY_MEDIUM = 3; // 0x3
+ }
+
+ public abstract static class UrlRequest.Callback {
+ ctor public UrlRequest.Callback();
+ method public void onCanceled(android.net.http.UrlRequest, android.net.http.UrlResponseInfo);
+ method public abstract void onFailed(android.net.http.UrlRequest, android.net.http.UrlResponseInfo, android.net.http.HttpException);
+ method public abstract void onReadCompleted(android.net.http.UrlRequest, android.net.http.UrlResponseInfo, java.nio.ByteBuffer) throws java.lang.Exception;
+ method public abstract void onRedirectReceived(android.net.http.UrlRequest, android.net.http.UrlResponseInfo, String) throws java.lang.Exception;
+ method public abstract void onResponseStarted(android.net.http.UrlRequest, android.net.http.UrlResponseInfo) throws java.lang.Exception;
+ method public abstract void onSucceeded(android.net.http.UrlRequest, android.net.http.UrlResponseInfo);
+ }
+
+ public static class UrlRequest.Status {
+ field public static final int CONNECTING = 10; // 0xa
+ field public static final int DOWNLOADING_PAC_FILE = 5; // 0x5
+ field public static final int ESTABLISHING_PROXY_TUNNEL = 8; // 0x8
+ field public static final int IDLE = 0; // 0x0
+ field public static final int INVALID = -1; // 0xffffffff
+ field public static final int READING_RESPONSE = 14; // 0xe
+ field public static final int RESOLVING_HOST = 9; // 0x9
+ field public static final int RESOLVING_HOST_IN_PAC_FILE = 7; // 0x7
+ field public static final int RESOLVING_PROXY_FOR_URL = 6; // 0x6
+ field public static final int SENDING_REQUEST = 12; // 0xc
+ field public static final int SSL_HANDSHAKE = 11; // 0xb
+ field public static final int WAITING_FOR_AVAILABLE_SOCKET = 2; // 0x2
+ field public static final int WAITING_FOR_CACHE = 4; // 0x4
+ field public static final int WAITING_FOR_DELEGATE = 3; // 0x3
+ field public static final int WAITING_FOR_RESPONSE = 13; // 0xd
+ field public static final int WAITING_FOR_STALLED_SOCKET_POOL = 1; // 0x1
+ }
+
+ public abstract static class UrlRequest.StatusListener {
+ ctor public UrlRequest.StatusListener();
+ method public abstract void onStatus(int);
+ }
+
+ public abstract class UrlResponseInfo {
+ ctor public UrlResponseInfo();
+ method public abstract java.util.Map<java.lang.String,java.util.List<java.lang.String>> getAllHeaders();
+ method public abstract java.util.List<java.util.Map.Entry<java.lang.String,java.lang.String>> getAllHeadersAsList();
+ method public abstract int getHttpStatusCode();
+ method public abstract String getHttpStatusText();
+ method public abstract String getNegotiatedProtocol();
+ method public abstract String getProxyServer();
+ method public abstract long getReceivedByteCount();
+ method public abstract String getUrl();
+ method public abstract java.util.List<java.lang.String> getUrlChain();
+ method public abstract boolean wasCached();
+ }
+
+}
+
diff --git a/Tethering/common/TetheringLib/cronet_enabled/api/module-lib-current.txt b/Tethering/common/TetheringLib/cronet_enabled/api/module-lib-current.txt
new file mode 100644
index 0000000..460c216
--- /dev/null
+++ b/Tethering/common/TetheringLib/cronet_enabled/api/module-lib-current.txt
@@ -0,0 +1,50 @@
+// Signature format: 2.0
+package android.net {
+
+ public final class TetheringConstants {
+ field public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
+ field public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
+ field public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
+ field public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
+ field public static final String EXTRA_SET_ALARM = "extraSetAlarm";
+ }
+
+ public class TetheringManager {
+ ctor public TetheringManager(@NonNull android.content.Context, @NonNull java.util.function.Supplier<android.os.IBinder>);
+ method public int getLastTetherError(@NonNull String);
+ method @NonNull public String[] getTetherableBluetoothRegexs();
+ method @NonNull public String[] getTetherableIfaces();
+ method @NonNull public String[] getTetherableUsbRegexs();
+ method @NonNull public String[] getTetherableWifiRegexs();
+ method @NonNull public String[] getTetheredIfaces();
+ method @NonNull public String[] getTetheringErroredIfaces();
+ method public boolean isTetheringSupported();
+ method public boolean isTetheringSupported(@NonNull String);
+ method public void requestLatestTetheringEntitlementResult(int, @NonNull android.os.ResultReceiver, boolean);
+ method @Deprecated public int setUsbTethering(boolean);
+ method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void startTethering(int, @NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.StartTetheringCallback);
+ method @Deprecated public int tether(@NonNull String);
+ method @Deprecated public int untether(@NonNull String);
+ }
+
+ public static interface TetheringManager.TetheredInterfaceCallback {
+ method public void onAvailable(@NonNull String);
+ method public void onUnavailable();
+ }
+
+ public static interface TetheringManager.TetheredInterfaceRequest {
+ method public void release();
+ }
+
+ public static interface TetheringManager.TetheringEventCallback {
+ method @Deprecated public default void onTetherableInterfaceRegexpsChanged(@NonNull android.net.TetheringManager.TetheringInterfaceRegexps);
+ }
+
+ @Deprecated public static class TetheringManager.TetheringInterfaceRegexps {
+ method @Deprecated @NonNull public java.util.List<java.lang.String> getTetherableBluetoothRegexs();
+ method @Deprecated @NonNull public java.util.List<java.lang.String> getTetherableUsbRegexs();
+ method @Deprecated @NonNull public java.util.List<java.lang.String> getTetherableWifiRegexs();
+ }
+
+}
+
diff --git a/Tethering/common/TetheringLib/cronet_enabled/api/module-lib-removed.txt b/Tethering/common/TetheringLib/cronet_enabled/api/module-lib-removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/Tethering/common/TetheringLib/cronet_enabled/api/module-lib-removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/Tethering/common/TetheringLib/cronet_enabled/api/removed.txt b/Tethering/common/TetheringLib/cronet_enabled/api/removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/Tethering/common/TetheringLib/cronet_enabled/api/removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/Tethering/common/TetheringLib/cronet_enabled/api/system-current.txt b/Tethering/common/TetheringLib/cronet_enabled/api/system-current.txt
new file mode 100644
index 0000000..844ff64
--- /dev/null
+++ b/Tethering/common/TetheringLib/cronet_enabled/api/system-current.txt
@@ -0,0 +1,117 @@
+// Signature format: 2.0
+package android.net {
+
+ public final class TetheredClient implements android.os.Parcelable {
+ ctor public TetheredClient(@NonNull android.net.MacAddress, @NonNull java.util.Collection<android.net.TetheredClient.AddressInfo>, int);
+ method public int describeContents();
+ method @NonNull public java.util.List<android.net.TetheredClient.AddressInfo> getAddresses();
+ method @NonNull public android.net.MacAddress getMacAddress();
+ method public int getTetheringType();
+ method public void writeToParcel(@NonNull android.os.Parcel, int);
+ field @NonNull public static final android.os.Parcelable.Creator<android.net.TetheredClient> CREATOR;
+ }
+
+ public static final class TetheredClient.AddressInfo implements android.os.Parcelable {
+ method public int describeContents();
+ method @NonNull public android.net.LinkAddress getAddress();
+ method @Nullable public String getHostname();
+ method public void writeToParcel(@NonNull android.os.Parcel, int);
+ field @NonNull public static final android.os.Parcelable.Creator<android.net.TetheredClient.AddressInfo> CREATOR;
+ }
+
+ public final class TetheringInterface implements android.os.Parcelable {
+ ctor public TetheringInterface(int, @NonNull String);
+ method public int describeContents();
+ method @NonNull public String getInterface();
+ method public int getType();
+ method public void writeToParcel(@NonNull android.os.Parcel, int);
+ field @NonNull public static final android.os.Parcelable.Creator<android.net.TetheringInterface> CREATOR;
+ }
+
+ public class TetheringManager {
+ method @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public void registerTetheringEventCallback(@NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.TetheringEventCallback);
+ method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void requestLatestTetheringEntitlementResult(int, boolean, @NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.OnTetheringEntitlementResultListener);
+ method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void startTethering(@NonNull android.net.TetheringManager.TetheringRequest, @NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.StartTetheringCallback);
+ method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void stopAllTethering();
+ method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void stopTethering(int);
+ method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.ACCESS_NETWORK_STATE}) public void unregisterTetheringEventCallback(@NonNull android.net.TetheringManager.TetheringEventCallback);
+ field @Deprecated public static final String ACTION_TETHER_STATE_CHANGED = "android.net.conn.TETHER_STATE_CHANGED";
+ field public static final int CONNECTIVITY_SCOPE_GLOBAL = 1; // 0x1
+ field public static final int CONNECTIVITY_SCOPE_LOCAL = 2; // 0x2
+ field public static final String EXTRA_ACTIVE_LOCAL_ONLY = "android.net.extra.ACTIVE_LOCAL_ONLY";
+ field public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
+ field public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
+ field public static final String EXTRA_ERRORED_TETHER = "erroredArray";
+ field public static final int TETHERING_BLUETOOTH = 2; // 0x2
+ field public static final int TETHERING_ETHERNET = 5; // 0x5
+ field public static final int TETHERING_INVALID = -1; // 0xffffffff
+ field public static final int TETHERING_NCM = 4; // 0x4
+ field public static final int TETHERING_USB = 1; // 0x1
+ field public static final int TETHERING_WIFI = 0; // 0x0
+ field public static final int TETHERING_WIFI_P2P = 3; // 0x3
+ field public static final int TETHER_ERROR_DHCPSERVER_ERROR = 12; // 0xc
+ field public static final int TETHER_ERROR_DISABLE_FORWARDING_ERROR = 9; // 0x9
+ field public static final int TETHER_ERROR_ENABLE_FORWARDING_ERROR = 8; // 0x8
+ field public static final int TETHER_ERROR_ENTITLEMENT_UNKNOWN = 13; // 0xd
+ field public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; // 0xa
+ field public static final int TETHER_ERROR_INTERNAL_ERROR = 5; // 0x5
+ field public static final int TETHER_ERROR_NO_ACCESS_TETHERING_PERMISSION = 15; // 0xf
+ field public static final int TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION = 14; // 0xe
+ field public static final int TETHER_ERROR_NO_ERROR = 0; // 0x0
+ field public static final int TETHER_ERROR_PROVISIONING_FAILED = 11; // 0xb
+ field public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2; // 0x2
+ field public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6; // 0x6
+ field public static final int TETHER_ERROR_UNAVAIL_IFACE = 4; // 0x4
+ field public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; // 0x1
+ field public static final int TETHER_ERROR_UNKNOWN_TYPE = 16; // 0x10
+ field public static final int TETHER_ERROR_UNSUPPORTED = 3; // 0x3
+ field public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7; // 0x7
+ field public static final int TETHER_HARDWARE_OFFLOAD_FAILED = 2; // 0x2
+ field public static final int TETHER_HARDWARE_OFFLOAD_STARTED = 1; // 0x1
+ field public static final int TETHER_HARDWARE_OFFLOAD_STOPPED = 0; // 0x0
+ }
+
+ public static interface TetheringManager.OnTetheringEntitlementResultListener {
+ method public void onTetheringEntitlementResult(int);
+ }
+
+ public static interface TetheringManager.StartTetheringCallback {
+ method public default void onTetheringFailed(int);
+ method public default void onTetheringStarted();
+ }
+
+ public static interface TetheringManager.TetheringEventCallback {
+ method public default void onClientsChanged(@NonNull java.util.Collection<android.net.TetheredClient>);
+ method public default void onError(@NonNull String, int);
+ method public default void onError(@NonNull android.net.TetheringInterface, int);
+ method public default void onLocalOnlyInterfacesChanged(@NonNull java.util.List<java.lang.String>);
+ method public default void onLocalOnlyInterfacesChanged(@NonNull java.util.Set<android.net.TetheringInterface>);
+ method public default void onOffloadStatusChanged(int);
+ method public default void onTetherableInterfacesChanged(@NonNull java.util.List<java.lang.String>);
+ method public default void onTetherableInterfacesChanged(@NonNull java.util.Set<android.net.TetheringInterface>);
+ method public default void onTetheredInterfacesChanged(@NonNull java.util.List<java.lang.String>);
+ method public default void onTetheredInterfacesChanged(@NonNull java.util.Set<android.net.TetheringInterface>);
+ method public default void onTetheringSupported(boolean);
+ method public default void onUpstreamChanged(@Nullable android.net.Network);
+ }
+
+ public static class TetheringManager.TetheringRequest {
+ method @Nullable public android.net.LinkAddress getClientStaticIpv4Address();
+ method public int getConnectivityScope();
+ method @Nullable public android.net.LinkAddress getLocalIpv4Address();
+ method public boolean getShouldShowEntitlementUi();
+ method public int getTetheringType();
+ method public boolean isExemptFromEntitlementCheck();
+ }
+
+ public static class TetheringManager.TetheringRequest.Builder {
+ ctor public TetheringManager.TetheringRequest.Builder(int);
+ method @NonNull public android.net.TetheringManager.TetheringRequest build();
+ method @NonNull @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public android.net.TetheringManager.TetheringRequest.Builder setConnectivityScope(int);
+ method @NonNull @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public android.net.TetheringManager.TetheringRequest.Builder setExemptFromEntitlementCheck(boolean);
+ method @NonNull @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public android.net.TetheringManager.TetheringRequest.Builder setShouldShowEntitlementUi(boolean);
+ method @NonNull @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public android.net.TetheringManager.TetheringRequest.Builder setStaticIpv4Addresses(@NonNull android.net.LinkAddress, @NonNull android.net.LinkAddress);
+ }
+
+}
+
diff --git a/Tethering/common/TetheringLib/cronet_enabled/api/system-removed.txt b/Tethering/common/TetheringLib/cronet_enabled/api/system-removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/Tethering/common/TetheringLib/cronet_enabled/api/system-removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/Tethering/common/TetheringLib/jarjar-excludes.txt b/Tethering/common/TetheringLib/jarjar-excludes.txt
new file mode 100644
index 0000000..50bc186
--- /dev/null
+++ b/Tethering/common/TetheringLib/jarjar-excludes.txt
@@ -0,0 +1,2 @@
+# Don't touch anything that's already under android.net
+android\.net\..+
\ No newline at end of file
diff --git a/Tethering/common/TetheringLib/jarjar-rules.txt b/Tethering/common/TetheringLib/jarjar-rules.txt
deleted file mode 100644
index e459fad..0000000
--- a/Tethering/common/TetheringLib/jarjar-rules.txt
+++ /dev/null
@@ -1 +0,0 @@
-# jarjar rules for the bootclasspath tethering framework library here
\ No newline at end of file
diff --git a/Tethering/src/com/android/networkstack/tethering/Tethering.java b/Tethering/src/com/android/networkstack/tethering/Tethering.java
index b3ec805..e48019c 100644
--- a/Tethering/src/com/android/networkstack/tethering/Tethering.java
+++ b/Tethering/src/com/android/networkstack/tethering/Tethering.java
@@ -1203,6 +1203,9 @@
}
private void handleConnectivityAction(Intent intent) {
+ // CONNECTIVITY_ACTION is not handled since U+ device.
+ if (SdkLevel.isAtLeastU()) return;
+
final NetworkInfo networkInfo =
(NetworkInfo) intent.getParcelableExtra(EXTRA_NETWORK_INFO);
if (networkInfo == null
@@ -2034,6 +2037,11 @@
// broadcasts that result in being passed a
// TetherMainSM.CMD_UPSTREAM_CHANGED.
handleNewUpstreamNetworkState(null);
+
+ if (SdkLevel.isAtLeastU()) {
+ // Need to try DUN immediately if Wi-Fi goes down.
+ chooseUpstreamType(true);
+ }
break;
default:
mLog.e("Unknown arg1 value: " + arg1);
@@ -2407,6 +2415,9 @@
/** Unregister tethering event callback */
void unregisterTetheringEventCallback(ITetheringEventCallback callback) {
+ if (callback == null) {
+ throw new NullPointerException();
+ }
mHandler.post(() -> {
mTetheringEventCallbacks.unregister(callback);
});
diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java
index 38f1e9c..da81bda 100644
--- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java
+++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java
@@ -666,7 +666,7 @@
// Calling System.gc() or System.runFinalization() doesn't guarantee GCs or finalizers
// are executed synchronously. The finalizer is called after GC on a separate thread.
- final int attempts = 100;
+ final int attempts = 600;
final long waitIntervalMs = 50;
for (int i = 0; i < attempts; i++) {
forceGc();
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 a8d886b..f90b3a4 100644
--- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java
+++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java
@@ -82,6 +82,8 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
@@ -179,6 +181,7 @@
import android.util.ArraySet;
import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
@@ -1337,6 +1340,131 @@
verifyDisableTryCellWhenTetheringStop(inOrder);
}
+ private void verifyWifiUpstreamAndUnregisterDunCallback(@NonNull final InOrder inOrder,
+ @NonNull final TestNetworkAgent wifi,
+ @NonNull final NetworkCallback currentDunCallack) throws Exception {
+ assertNotNull(currentDunCallack);
+
+ inOrder.verify(mUpstreamNetworkMonitor).setTryCell(false);
+ inOrder.verify(mCm).unregisterNetworkCallback(eq(currentDunCallack));
+ inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(wifi.networkId);
+ inOrder.verify(mUpstreamNetworkMonitor, never()).setCurrentUpstream(any());
+ }
+
+ @Nullable
+ private NetworkCallback verifyDunUpstream(@NonNull final InOrder inOrder,
+ @NonNull final TestNetworkAgent dun, final boolean needToRequestNetwork)
+ throws Exception {
+ inOrder.verify(mUpstreamNetworkMonitor).setTryCell(true);
+ ArgumentCaptor<NetworkCallback> captor = ArgumentCaptor.forClass(NetworkCallback.class);
+ NetworkCallback dunNetworkCallback = null;
+ if (needToRequestNetwork) {
+ inOrder.verify(mCm).requestNetwork(any(), eq(0), eq(TYPE_MOBILE_DUN), any(),
+ captor.capture());
+ dunNetworkCallback = captor.getValue();
+ }
+ inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(null);
+ final Runnable doDispatchAll = () -> mLooper.dispatchAll();
+ dun.fakeConnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(dun.networkId);
+
+ if (needToRequestNetwork) {
+ assertNotNull(dunNetworkCallback);
+ } else {
+ assertNull(dunNetworkCallback);
+ }
+
+ return dunNetworkCallback;
+ }
+
+ // Overall test coverage:
+ // - verifyChooseDunUpstreamByAutomaticMode: common, test#1, test#2
+ // - testChooseDunUpstreamByAutomaticMode_defaultNetworkWifi: test#3, test#4
+ // - testChooseDunUpstreamByAutomaticMode_loseDefaultNetworkWifi: test#5
+ // - testChooseDunUpstreamByAutomaticMode_defaultNetworkCell: test#5, test#7
+ // - testChooseDunUpstreamByAutomaticMode_loseAndRegainDun: test#8
+ // - testChooseDunUpstreamByAutomaticMode_switchDefaultFromWifiToCell: test#9, test#10
+ //
+ // Overall test cases:
+ // +-------+-------+-------+-------+-------+
+ // | Test | WiFi | Cellu | Dun | Expec |
+ // | Case | | alr | | ted |
+ // | # | | | | Upstr |
+ // | | | | | eam |
+ // +-------+-------+-------+-------+-------+
+ // | - | | | | - | --+
+ // +-------+-------+-------+-------+-------+ |
+ // | - | | V | | - | |
+ // +-------+-------+-------+-------+-------+ |
+ // | - | | V | O | Dun | +-- chooseDunUpstreamTestCommon
+ // +-------+-------+-------+-------+-------+ |
+ // | - | V | O | O | WiFi | |
+ // +-------+-------+-------+-------+-------+ |
+ // | - | V | O | | WiFi | --+
+ // +-------+-------+-------+-------+-------+
+ // | | O | V | | - |
+ // | 1 +-------+-------+-------+-------+
+ // | | O | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | | O | V | | - |
+ // | 2 +-------+-------+-------+-------+
+ // | | O | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | 3 | V | O | | WiFi |
+ // +-------+-------+-------+-------+-------+
+ // | 4 | V | | | WiFi |
+ // +-------+-------+-------+-------+-------+
+ // | 5 | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | 6 | | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | 7 | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | | | | | - |
+ // | 8 +-------+-------+-------+-------+
+ // | | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | | V | | O | WiFi |
+ // | 9 +-------+-------+-------+-------+
+ // | | V | | | WiFi |
+ // +-------+-------+-------+-------+-------+
+ // | | O | V | | - |
+ // | 10 +-------+-------+-------+-------+
+ // | | O | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ //
+ // Annotation:
+ // 1. "V" means that the given network is connected and it is default network.
+ // 2. "O" means that the given network is connected and it is not default network.
+ //
+
+ // Test case:
+ // +-------+-------+-------+-------+-------+
+ // | Test | WiFi | Cellu | Dun | Expec |
+ // | Case | | alr | | ted |
+ // | # | | | | Upstr |
+ // | | | | | eam |
+ // +-------+-------+-------+-------+-------+
+ // | - | | | | - | --+
+ // +-------+-------+-------+-------+-------+ |
+ // | - | | V | | - | |
+ // +-------+-------+-------+-------+-------+ |
+ // | - | | V | O | Dun | +-- chooseDunUpstreamTestCommon
+ // +-------+-------+-------+-------+-------+ |
+ // | - | V | O | O | WiFi | |
+ // +-------+-------+-------+-------+-------+ |
+ // | - | V | O | | WiFi | --+
+ // +-------+-------+-------+-------+-------+
+ // | | O | V | | - |
+ // | 1 +-------+-------+-------+-------+
+ // | | O | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | | O | V | | - |
+ // | 2 +-------+-------+-------+-------+
+ // | | O | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ //
private void verifyChooseDunUpstreamByAutomaticMode(boolean configAutomatic) throws Exception {
// Enable automatic upstream selection.
TestNetworkAgent mobile = new TestNetworkAgent(mCm, buildMobileDualStackUpstreamState());
@@ -1345,20 +1473,14 @@
InOrder inOrder = inOrder(mCm, mUpstreamNetworkMonitor);
chooseDunUpstreamTestCommon(configAutomatic, inOrder, mobile, wifi, dun);
- // When default network switch to mobile and wifi is connected (may have low signal),
+ // [1] When default network switch to mobile and wifi is connected (may have low signal),
// automatic mode would request dun again and choose it as upstream.
mCm.makeDefaultNetwork(mobile, CALLBACKS_FIRST);
mLooper.dispatchAll();
- inOrder.verify(mUpstreamNetworkMonitor).setTryCell(true);
- ArgumentCaptor<NetworkCallback> captor = ArgumentCaptor.forClass(NetworkCallback.class);
- inOrder.verify(mCm).requestNetwork(any(), eq(0), eq(TYPE_MOBILE_DUN), any(), any());
- inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(null);
- final Runnable doDispatchAll = () -> mLooper.dispatchAll();
- dun.fakeConnect(CALLBACKS_FIRST, doDispatchAll);
- mLooper.dispatchAll();
- inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(dun.networkId);
+ verifyDunUpstream(inOrder, dun, true /* needToRequestNetwork */);
- // Lose and regain upstream again.
+ // [2] Lose and regain upstream again.
+ final Runnable doDispatchAll = () -> mLooper.dispatchAll();
dun.fakeDisconnect(CALLBACKS_FIRST, doDispatchAll);
mLooper.dispatchAll();
inOrder.verify(mUpstreamNetworkMonitor).setTryCell(true);
@@ -1376,12 +1498,245 @@
verifyChooseDunUpstreamByAutomaticMode(true /* configAutomatic */);
}
+ // testChooseDunUpstreamByAutomaticMode_* doesn't verify configAutomatic:false because no
+ // matter |configAutomatic| set to true or false, the result always be automatic mode. We
+ // just need one test to make sure this behavior. Don't need to test this configuration
+ // in all tests.
@Test
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
public void testChooseDunUpstreamByAutomaticModeWithConfigDisabled() throws Exception {
verifyChooseDunUpstreamByAutomaticMode(false /* configAutomatic */);
}
+ // Test case:
+ // +-------+-------+-------+-------+-------+
+ // | Test | WiFi | Cellu | Dun | Expec |
+ // | Case | | alr | | ted |
+ // | # | | | | Upstr |
+ // | | | | | eam |
+ // +-------+-------+-------+-------+-------+
+ // | - | O | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | 3 | V | O | | WiFi |
+ // +-------+-------+-------+-------+-------+
+ // | 4 | V | | | WiFi |
+ // +-------+-------+-------+-------+-------+
+ //
+ // See verifyChooseDunUpstreamByAutomaticMode for the annotation.
+ //
+ @Test
+ public void testChooseDunUpstreamByAutomaticMode_defaultNetworkWifi() throws Exception {
+ TestNetworkAgent mobile = new TestNetworkAgent(mCm, buildMobileDualStackUpstreamState());
+ TestNetworkAgent wifi = new TestNetworkAgent(mCm, buildWifiUpstreamState());
+ TestNetworkAgent dun = new TestNetworkAgent(mCm, buildDunUpstreamState());
+ final InOrder inOrder = inOrder(mCm, mUpstreamNetworkMonitor);
+ final NetworkCallback dunNetworkCallback1 = setupDunUpstreamTest(
+ true /* configAutomatic */, inOrder);
+
+ // When wifi connected, unregister dun request and choose wifi as upstream.
+ wifi.fakeConnect();
+ mCm.makeDefaultNetwork(wifi, CALLBACKS_FIRST);
+ mLooper.dispatchAll();
+ verifyWifiUpstreamAndUnregisterDunCallback(inOrder, wifi, dunNetworkCallback1);
+
+ // When default network switch to mobile and wifi is connected (may have low signal),
+ // automatic mode would request dun again and choose it as upstream.
+ mCm.makeDefaultNetwork(mobile, CALLBACKS_FIRST);
+ mLooper.dispatchAll();
+ final NetworkCallback dunNetworkCallback2 =
+ verifyDunUpstream(inOrder, dun, true /* needToRequestNetwork */);
+
+ // [3] When default network switch to wifi and mobile is still connected,
+ // unregister dun request and choose wifi as upstream.
+ mCm.makeDefaultNetwork(wifi, CALLBACKS_FIRST);
+ mLooper.dispatchAll();
+ verifyWifiUpstreamAndUnregisterDunCallback(inOrder, wifi, dunNetworkCallback2);
+
+ // [4] When mobile is disconnected, keep wifi as upstream.
+ final Runnable doDispatchAll = () -> mLooper.dispatchAll();
+ mobile.fakeDisconnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor, never()).setCurrentUpstream(any());
+
+ verifyDisableTryCellWhenTetheringStop(inOrder);
+ }
+
+ // Test case:
+ // +-------+-------+-------+-------+-------+
+ // | Test | WiFi | Cellu | Dun | Expec |
+ // | Case | | alr | | ted |
+ // | # | | | | Upstr |
+ // | | | | | eam |
+ // +-------+-------+-------+-------+-------+
+ // | - | V | | | WiFi |
+ // +-------+-------+-------+-------+-------+
+ // | 5 | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ //
+ // See verifyChooseDunUpstreamByAutomaticMode for the annotation.
+ //
+ @Test
+ public void testChooseDunUpstreamByAutomaticMode_loseDefaultNetworkWifi() throws Exception {
+ TestNetworkAgent wifi = new TestNetworkAgent(mCm, buildWifiUpstreamState());
+ TestNetworkAgent dun = new TestNetworkAgent(mCm, buildDunUpstreamState());
+ final InOrder inOrder = inOrder(mCm, mUpstreamNetworkMonitor);
+ final NetworkCallback dunNetworkCallback = setupDunUpstreamTest(
+ true /* configAutomatic */, inOrder);
+
+ // When wifi connected, unregister dun request and choose wifi as upstream.
+ wifi.fakeConnect();
+ mCm.makeDefaultNetwork(wifi, CALLBACKS_FIRST);
+ mLooper.dispatchAll();
+ verifyWifiUpstreamAndUnregisterDunCallback(inOrder, wifi, dunNetworkCallback);
+
+ // [5] When wifi is disconnected, automatic mode would request dun again and choose it
+ // as upstream.
+ final Runnable doDispatchAll = () -> mLooper.dispatchAll();
+ mCm.makeDefaultNetwork(null, CALLBACKS_FIRST, doDispatchAll);
+ wifi.fakeDisconnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ verifyDunUpstream(inOrder, dun, true /* needToRequestNetwork */);
+
+ verifyDisableTryCellWhenTetheringStop(inOrder);
+ }
+
+ // Test case:
+ // +-------+-------+-------+-------+-------+
+ // | Test | WiFi | Cellu | Dun | Expec |
+ // | Case | | alr | | ted |
+ // | # | | | | Upstr |
+ // | | | | | eam |
+ // +-------+-------+-------+-------+-------+
+ // | - | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | 6 | | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | 7 | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ //
+ // See verifyChooseDunUpstreamByAutomaticMode for the annotation.
+ //
+ @Test
+ public void testChooseDunUpstreamByAutomaticMode_defaultNetworkCell() throws Exception {
+ TestNetworkAgent mobile = new TestNetworkAgent(mCm, buildMobileDualStackUpstreamState());
+ TestNetworkAgent dun = new TestNetworkAgent(mCm, buildDunUpstreamState());
+ final InOrder inOrder = inOrder(mCm, mUpstreamNetworkMonitor);
+ setupDunUpstreamTest(true /* configAutomatic */, inOrder);
+
+ // Pretend dun connected and expect choose dun as upstream.
+ final Runnable doDispatchAll = () -> mLooper.dispatchAll();
+ dun.fakeConnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(dun.networkId);
+
+ // [6] When mobile is connected and default network switch to mobile, keep dun as upstream.
+ mobile.fakeConnect();
+ mCm.makeDefaultNetwork(mobile, CALLBACKS_FIRST);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor, never()).setCurrentUpstream(any());
+
+ // [7] When mobile is disconnected, keep dun as upstream.
+ mCm.makeDefaultNetwork(null, CALLBACKS_FIRST, doDispatchAll);
+ mobile.fakeDisconnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor, never()).setCurrentUpstream(any());
+
+ verifyDisableTryCellWhenTetheringStop(inOrder);
+ }
+
+ // Test case:
+ // +-------+-------+-------+-------+-------+
+ // | Test | WiFi | Cellu | Dun | Expec |
+ // | Case | | alr | | ted |
+ // | # | | | | Upstr |
+ // | | | | | eam |
+ // +-------+-------+-------+-------+-------+
+ // | - | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | | | | | - |
+ // | 8 +-------+-------+-------+-------+
+ // | | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ //
+ // See verifyChooseDunUpstreamByAutomaticMode for the annotation.
+ //
+ @Test
+ public void testChooseDunUpstreamByAutomaticMode_loseAndRegainDun() throws Exception {
+ TestNetworkAgent dun = new TestNetworkAgent(mCm, buildDunUpstreamState());
+ final InOrder inOrder = inOrder(mCm, mUpstreamNetworkMonitor);
+ setupDunUpstreamTest(true /* configAutomatic */, inOrder);
+
+ // Pretend dun connected and expect choose dun as upstream.
+ final Runnable doDispatchAll = () -> mLooper.dispatchAll();
+ dun.fakeConnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(dun.networkId);
+
+ // [8] Lose and regain upstream again.
+ dun.fakeDisconnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ verifyDunUpstream(inOrder, dun, false /* needToRequestNetwork */);
+
+ verifyDisableTryCellWhenTetheringStop(inOrder);
+ }
+
+ // Test case:
+ // +-------+-------+-------+-------+-------+
+ // | Test | WiFi | Cellu | Dun | Expec |
+ // | Case | | alr | | ted |
+ // | # | | | | Upstr |
+ // | | | | | eam |
+ // +-------+-------+-------+-------+-------+
+ // | - | | | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ // | | V | | O | WiFi |
+ // | 9 +-------+-------+-------+-------+
+ // | | V | | | WiFi |
+ // +-------+-------+-------+-------+-------+
+ // | | O | V | | - |
+ // | 10 +-------+-------+-------+-------+
+ // | | O | V | O | Dun |
+ // +-------+-------+-------+-------+-------+
+ //
+ // See verifyChooseDunUpstreamByAutomaticMode for the annotation.
+ //
+ @Test
+ public void testChooseDunUpstreamByAutomaticMode_switchDefaultFromWifiToCell()
+ throws Exception {
+ TestNetworkAgent mobile = new TestNetworkAgent(mCm, buildMobileDualStackUpstreamState());
+ TestNetworkAgent wifi = new TestNetworkAgent(mCm, buildWifiUpstreamState());
+ TestNetworkAgent dun = new TestNetworkAgent(mCm, buildDunUpstreamState());
+ final InOrder inOrder = inOrder(mCm, mUpstreamNetworkMonitor);
+ final NetworkCallback dunNetworkCallback = setupDunUpstreamTest(
+ true /* configAutomatic */, inOrder);
+
+ // Pretend dun connected and expect choose dun as upstream.
+ final Runnable doDispatchAll = () -> mLooper.dispatchAll();
+ dun.fakeConnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(dun.networkId);
+
+ // [9] When wifi is connected and default network switch to wifi, unregister dun request
+ // and choose wifi as upstream. When dun is disconnected, keep wifi as upstream.
+ wifi.fakeConnect();
+ mCm.makeDefaultNetwork(wifi, CALLBACKS_FIRST);
+ mLooper.dispatchAll();
+ verifyWifiUpstreamAndUnregisterDunCallback(inOrder, wifi, dunNetworkCallback);
+ dun.fakeDisconnect(CALLBACKS_FIRST, doDispatchAll);
+ mLooper.dispatchAll();
+ inOrder.verify(mUpstreamNetworkMonitor, never()).setCurrentUpstream(any());
+
+ // [10] When mobile and mobile are connected and default network switch to mobile
+ // (may have low signal), automatic mode would request dun again and choose it as
+ // upstream.
+ mobile.fakeConnect();
+ mCm.makeDefaultNetwork(mobile, CALLBACKS_FIRST);
+ mLooper.dispatchAll();
+ verifyDunUpstream(inOrder, dun, true /* needToRequestNetwork */);
+
+ verifyDisableTryCellWhenTetheringStop(inOrder);
+ }
+
@Test
@IgnoreAfter(Build.VERSION_CODES.TIRAMISU)
public void testChooseDunUpstreamByLegacyMode() throws Exception {
@@ -1425,8 +1780,7 @@
verifyDisableTryCellWhenTetheringStop(inOrder);
}
- private void chooseDunUpstreamTestCommon(final boolean automatic, InOrder inOrder,
- TestNetworkAgent mobile, TestNetworkAgent wifi, TestNetworkAgent dun) throws Exception {
+ private NetworkCallback setupDunUpstreamTest(final boolean automatic, InOrder inOrder) {
when(mResources.getBoolean(R.bool.config_tether_upstream_automatic)).thenReturn(automatic);
when(mTelephonyManager.isTetheringApnRequired()).thenReturn(true);
sendConfigurationChanged();
@@ -1439,10 +1793,16 @@
inOrder.verify(mUpstreamNetworkMonitor).setTryCell(true);
ArgumentCaptor<NetworkCallback> captor = ArgumentCaptor.forClass(NetworkCallback.class);
inOrder.verify(mCm).requestNetwork(any(), eq(0), eq(TYPE_MOBILE_DUN), any(),
- captor.capture());
- final NetworkCallback dunNetworkCallback1 = captor.getValue();
+ captor.capture() /* DUN network callback */);
- // Pretend cellular connected and expect the upstream to be set.
+ return captor.getValue();
+ }
+
+ private void chooseDunUpstreamTestCommon(final boolean automatic, InOrder inOrder,
+ TestNetworkAgent mobile, TestNetworkAgent wifi, TestNetworkAgent dun) throws Exception {
+ final NetworkCallback dunNetworkCallback = setupDunUpstreamTest(automatic, inOrder);
+
+ // Pretend cellular connected and expect the upstream to be not set.
mobile.fakeConnect();
mCm.makeDefaultNetwork(mobile, BROADCAST_FIRST);
mLooper.dispatchAll();
@@ -1458,9 +1818,7 @@
wifi.fakeConnect();
mCm.makeDefaultNetwork(wifi, CALLBACKS_FIRST);
mLooper.dispatchAll();
- inOrder.verify(mUpstreamNetworkMonitor).setTryCell(false);
- inOrder.verify(mCm).unregisterNetworkCallback(eq(dunNetworkCallback1));
- inOrder.verify(mUpstreamNetworkMonitor).setCurrentUpstream(wifi.networkId);
+ verifyWifiUpstreamAndUnregisterDunCallback(inOrder, wifi, dunNetworkCallback);
dun.fakeDisconnect(BROADCAST_FIRST, doDispatchAll);
mLooper.dispatchAll();
inOrder.verify(mUpstreamNetworkMonitor, never()).setCurrentUpstream(any());
@@ -1767,7 +2125,7 @@
new ArrayList<Network>(Arrays.asList(networks));
for (Network upstream : expectedUpstreams) {
// throws OOB if no expectations
- assertEquals(mActualUpstreams.remove(0), upstream);
+ assertEquals(upstream, mActualUpstreams.remove(0));
}
assertNoUpstreamChangeCallback();
}
@@ -1782,14 +2140,14 @@
for (TetheringConfigurationParcel config : expectedTetherConfig) {
// throws OOB if no expectations
final TetheringConfigurationParcel actualConfig = mTetheringConfigs.remove(0);
- assertTetherConfigParcelEqual(actualConfig, config);
+ assertTetherConfigParcelEqual(config, actualConfig);
}
assertNoConfigChangeCallback();
}
public void expectOffloadStatusChanged(final int expectedStatus) {
assertOffloadStatusChangedCallback();
- assertEquals(mOffloadStatus.remove(0), new Integer(expectedStatus));
+ assertEquals(Integer.valueOf(expectedStatus), mOffloadStatus.remove(0));
}
public TetherStatesParcel pollTetherStatesChanged() {
@@ -1880,12 +2238,12 @@
private void assertTetherConfigParcelEqual(@NonNull TetheringConfigurationParcel actual,
@NonNull TetheringConfigurationParcel expect) {
- assertArrayEquals(actual.tetherableUsbRegexs, expect.tetherableUsbRegexs);
- assertArrayEquals(actual.tetherableWifiRegexs, expect.tetherableWifiRegexs);
- assertArrayEquals(actual.tetherableBluetoothRegexs, expect.tetherableBluetoothRegexs);
- assertArrayEquals(actual.legacyDhcpRanges, expect.legacyDhcpRanges);
- assertArrayEquals(actual.provisioningApp, expect.provisioningApp);
- assertEquals(actual.provisioningAppNoUi, expect.provisioningAppNoUi);
+ assertArrayEquals(expect.tetherableUsbRegexs, actual.tetherableUsbRegexs);
+ assertArrayEquals(expect.tetherableWifiRegexs, actual.tetherableWifiRegexs);
+ assertArrayEquals(expect.tetherableBluetoothRegexs, actual.tetherableBluetoothRegexs);
+ assertArrayEquals(expect.legacyDhcpRanges, actual.legacyDhcpRanges);
+ assertArrayEquals(expect.provisioningApp, actual.provisioningApp);
+ assertEquals(expect.provisioningAppNoUi, actual.provisioningAppNoUi);
}
}
diff --git a/framework-t/api/current.txt b/framework-t/api/current.txt
index eb77288..5532853 100644
--- a/framework-t/api/current.txt
+++ b/framework-t/api/current.txt
@@ -192,15 +192,20 @@
method @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public void discoverServices(@NonNull String, int, @NonNull android.net.NetworkRequest, @NonNull java.util.concurrent.Executor, @NonNull android.net.nsd.NsdManager.DiscoveryListener);
method public void registerService(android.net.nsd.NsdServiceInfo, int, android.net.nsd.NsdManager.RegistrationListener);
method public void registerService(@NonNull android.net.nsd.NsdServiceInfo, int, @NonNull java.util.concurrent.Executor, @NonNull android.net.nsd.NsdManager.RegistrationListener);
- method public void resolveService(android.net.nsd.NsdServiceInfo, android.net.nsd.NsdManager.ResolveListener);
- method public void resolveService(@NonNull android.net.nsd.NsdServiceInfo, @NonNull java.util.concurrent.Executor, @NonNull android.net.nsd.NsdManager.ResolveListener);
+ method public void registerServiceInfoCallback(@NonNull android.net.nsd.NsdServiceInfo, @NonNull java.util.concurrent.Executor, @NonNull android.net.nsd.NsdManager.ServiceInfoCallback);
+ method @Deprecated public void resolveService(android.net.nsd.NsdServiceInfo, android.net.nsd.NsdManager.ResolveListener);
+ method @Deprecated public void resolveService(@NonNull android.net.nsd.NsdServiceInfo, @NonNull java.util.concurrent.Executor, @NonNull android.net.nsd.NsdManager.ResolveListener);
method public void stopServiceDiscovery(android.net.nsd.NsdManager.DiscoveryListener);
+ method public void stopServiceResolution(@NonNull android.net.nsd.NsdManager.ResolveListener);
method public void unregisterService(android.net.nsd.NsdManager.RegistrationListener);
+ method public void unregisterServiceInfoCallback(@NonNull android.net.nsd.NsdManager.ServiceInfoCallback);
field public static final String ACTION_NSD_STATE_CHANGED = "android.net.nsd.STATE_CHANGED";
field public static final String EXTRA_NSD_STATE = "nsd_state";
field public static final int FAILURE_ALREADY_ACTIVE = 3; // 0x3
+ field public static final int FAILURE_BAD_PARAMETERS = 6; // 0x6
field public static final int FAILURE_INTERNAL_ERROR = 0; // 0x0
field public static final int FAILURE_MAX_LIMIT = 4; // 0x4
+ field public static final int FAILURE_OPERATION_NOT_RUNNING = 5; // 0x5
field public static final int NSD_STATE_DISABLED = 1; // 0x1
field public static final int NSD_STATE_ENABLED = 2; // 0x2
field public static final int PROTOCOL_DNS_SD = 1; // 0x1
@@ -224,21 +229,32 @@
public static interface NsdManager.ResolveListener {
method public void onResolveFailed(android.net.nsd.NsdServiceInfo, int);
+ method public default void onResolveStopped(@NonNull android.net.nsd.NsdServiceInfo);
method public void onServiceResolved(android.net.nsd.NsdServiceInfo);
+ method public default void onStopResolutionFailed(@NonNull android.net.nsd.NsdServiceInfo, int);
+ }
+
+ public static interface NsdManager.ServiceInfoCallback {
+ method public void onServiceInfoCallbackRegistrationFailed(int);
+ method public void onServiceInfoCallbackUnregistered();
+ method public void onServiceLost();
+ method public void onServiceUpdated(@NonNull android.net.nsd.NsdServiceInfo);
}
public final class NsdServiceInfo implements android.os.Parcelable {
ctor public NsdServiceInfo();
method public int describeContents();
method public java.util.Map<java.lang.String,byte[]> getAttributes();
- method public java.net.InetAddress getHost();
+ method @Deprecated public java.net.InetAddress getHost();
+ method @NonNull public java.util.List<java.net.InetAddress> getHostAddresses();
method @Nullable public android.net.Network getNetwork();
method public int getPort();
method public String getServiceName();
method public String getServiceType();
method public void removeAttribute(String);
method public void setAttribute(String, String);
- method public void setHost(java.net.InetAddress);
+ method @Deprecated public void setHost(java.net.InetAddress);
+ method public void setHostAddresses(@NonNull java.util.List<java.net.InetAddress>);
method public void setNetwork(@Nullable android.net.Network);
method public void setPort(int);
method public void setServiceName(String);
diff --git a/framework-t/api/system-current.txt b/framework-t/api/system-current.txt
index c2d245c..87b0a64 100644
--- a/framework-t/api/system-current.txt
+++ b/framework-t/api/system-current.txt
@@ -260,6 +260,7 @@
public class IpSecManager {
method @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) public void applyTunnelModeTransform(@NonNull android.net.IpSecManager.IpSecTunnelInterface, int, @NonNull android.net.IpSecTransform) throws java.io.IOException;
method @NonNull @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) public android.net.IpSecManager.IpSecTunnelInterface createIpSecTunnelInterface(@NonNull java.net.InetAddress, @NonNull java.net.InetAddress, @NonNull android.net.Network) throws java.io.IOException, android.net.IpSecManager.ResourceUnavailableException;
+ method @RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS) public void startTunnelModeTransformMigration(@NonNull android.net.IpSecTransform, @NonNull java.net.InetAddress, @NonNull java.net.InetAddress);
}
public static final class IpSecManager.IpSecTunnelInterface implements java.lang.AutoCloseable {
diff --git a/framework-t/src/android/net/IpSecManager.java b/framework-t/src/android/net/IpSecManager.java
index 1c83e09..3afa6ef 100644
--- a/framework-t/src/android/net/IpSecManager.java
+++ b/framework-t/src/android/net/IpSecManager.java
@@ -273,7 +273,7 @@
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
- mCloseGuard.open("open");
+ mCloseGuard.open("close");
}
/** @hide */
@@ -610,7 +610,7 @@
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
- mCloseGuard.open("constructor");
+ mCloseGuard.open("close");
}
/** Get the encapsulation socket's file descriptor. */
@@ -823,16 +823,18 @@
* Update the underlying network for this IpSecTunnelInterface.
*
* <p>This new underlying network will be used for all transforms applied AFTER this call is
- * complete. Before new {@link IpSecTransform}(s) with matching addresses are applied to
- * this tunnel interface, traffic will still use the old SA, and be routed on the old
+ * complete. Before {@link IpSecTransform}(s) with matching addresses are applied to this
+ * tunnel interface, traffic will still use the old transform, and be routed on the old
* underlying network.
*
* <p>To migrate IPsec tunnel mode traffic, a caller should:
*
* <ol>
* <li>Update the IpSecTunnelInterface’s underlying network.
- * <li>Apply {@link IpSecTransform}(s) with matching addresses to this
- * IpSecTunnelInterface.
+ * <li>Apply the new {@link IpSecTransform}(s) to this IpSecTunnelInterface. These can be
+ * new {@link IpSecTransform}(s) with matching addresses, or {@link IpSecTransform}(s)
+ * that have started migration (see {@link
+ * IpSecManager#startTunnelModeTransformMigration}).
* </ol>
*
* @param underlyingNetwork the new {@link Network} that will carry traffic for this tunnel.
@@ -841,7 +843,6 @@
* method will throw an {@link IllegalArgumentException}. If the IpSecTunnelInterface is
* later added to this network, all outbound traffic will be blackholed.
*/
- // TODO: b/169171001 Update the documentation when transform migration is supported.
// The purpose of making updating network and applying transforms separate is to leave open
// the possibility to support lossless migration procedures. To do that, Android platform
// will need to support multiple inbound tunnel mode transforms, just like it can support
@@ -890,7 +891,7 @@
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
- mCloseGuard.open("constructor");
+ mCloseGuard.open("close");
}
/**
@@ -1033,9 +1034,10 @@
* @param newDestinationAddress the new destination address
* @hide
*/
+ @SystemApi
@RequiresFeature(FEATURE_IPSEC_TUNNEL_MIGRATION)
@RequiresPermission(android.Manifest.permission.MANAGE_IPSEC_TUNNELS)
- public void startMigration(
+ public void startTunnelModeTransformMigration(
@NonNull IpSecTransform transform,
@NonNull InetAddress newSourceAddress,
@NonNull InetAddress newDestinationAddress) {
diff --git a/framework-t/src/android/net/IpSecTransform.java b/framework-t/src/android/net/IpSecTransform.java
index 68ae5de..c236b6c 100644
--- a/framework-t/src/android/net/IpSecTransform.java
+++ b/framework-t/src/android/net/IpSecTransform.java
@@ -126,7 +126,7 @@
checkResultStatus(status);
mResourceId = result.resourceId;
Log.d(TAG, "Added Transform with Id " + mResourceId);
- mCloseGuard.open("build");
+ mCloseGuard.open("close");
} catch (ServiceSpecificException e) {
throw IpSecManager.rethrowUncheckedExceptionFromServiceSpecificException(e);
}
diff --git a/framework-t/src/android/net/NetworkTemplate.java b/framework-t/src/android/net/NetworkTemplate.java
index b2da371..f633a8f 100644
--- a/framework-t/src/android/net/NetworkTemplate.java
+++ b/framework-t/src/android/net/NetworkTemplate.java
@@ -88,18 +88,6 @@
public static final int MATCH_WIFI = 4;
/** Match rule to match ethernet networks. */
public static final int MATCH_ETHERNET = 5;
- /**
- * Match rule to match all cellular networks.
- *
- * @hide
- */
- public static final int MATCH_MOBILE_WILDCARD = 6;
- /**
- * Match rule to match all wifi networks.
- *
- * @hide
- */
- public static final int MATCH_WIFI_WILDCARD = 7;
/** Match rule to match bluetooth networks. */
public static final int MATCH_BLUETOOTH = 8;
/**
@@ -177,8 +165,6 @@
case MATCH_MOBILE:
case MATCH_WIFI:
case MATCH_ETHERNET:
- case MATCH_MOBILE_WILDCARD:
- case MATCH_WIFI_WILDCARD:
case MATCH_BLUETOOTH:
case MATCH_PROXY:
case MATCH_CARRIER:
@@ -258,7 +244,6 @@
}
private final int mMatchRule;
- private final String mSubscriberId;
/**
* Ugh, templates are designed to target a single subscriber, but we might
@@ -285,9 +270,8 @@
private static void checkValidMatchSubscriberIds(int matchRule, String[] matchSubscriberIds) {
switch (matchRule) {
- case MATCH_MOBILE:
case MATCH_CARRIER:
- // MOBILE and CARRIER templates must always specify a subscriber ID.
+ // CARRIER templates must always specify a valid subscriber ID.
if (matchSubscriberIds.length == 0) {
throw new IllegalArgumentException("checkValidMatchSubscriberIds with empty"
+ " list of ids for rule" + getMatchRuleName(matchRule));
@@ -313,21 +297,20 @@
// to metered networks. It is now possible to match mobile with any meteredness, but
// in order to preserve backward compatibility of @UnsupportedAppUsage methods, this
//constructor passes METERED_YES for these types.
- this(matchRule, subscriberId, new String[] { subscriberId },
+ this(matchRule, new String[] { subscriberId },
wifiNetworkKey != null ? new String[] { wifiNetworkKey } : new String[0],
- (matchRule == MATCH_MOBILE || matchRule == MATCH_MOBILE_WILDCARD
- || matchRule == MATCH_CARRIER) ? METERED_YES : METERED_ALL,
- ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL);
+ (matchRule == MATCH_MOBILE || matchRule == MATCH_CARRIER)
+ ? METERED_YES : METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
+ NETWORK_TYPE_ALL, OEM_MANAGED_ALL);
}
/** @hide */
- public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
- String[] matchWifiNetworkKeys, int metered, int roaming,
- int defaultNetwork, int ratType, int oemManaged) {
+ public NetworkTemplate(int matchRule, String[] matchSubscriberIds,
+ String[] matchWifiNetworkKeys, int metered, int roaming, int defaultNetwork,
+ int ratType, int oemManaged) {
Objects.requireNonNull(matchWifiNetworkKeys);
Objects.requireNonNull(matchSubscriberIds);
mMatchRule = matchRule;
- mSubscriberId = subscriberId;
mMatchSubscriberIds = matchSubscriberIds;
mMatchWifiNetworkKeys = matchWifiNetworkKeys;
mMetered = metered;
@@ -344,7 +327,6 @@
private NetworkTemplate(Parcel in) {
mMatchRule = in.readInt();
- mSubscriberId = in.readString();
mMatchSubscriberIds = in.createStringArray();
mMatchWifiNetworkKeys = in.createStringArray();
mMetered = in.readInt();
@@ -357,7 +339,6 @@
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mMatchRule);
- dest.writeString(mSubscriberId);
dest.writeStringArray(mMatchSubscriberIds);
dest.writeStringArray(mMatchWifiNetworkKeys);
dest.writeInt(mMetered);
@@ -376,10 +357,6 @@
public String toString() {
final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
- if (mSubscriberId != null) {
- builder.append(", subscriberId=").append(
- NetworkIdentityUtils.scrubSubscriberId(mSubscriberId));
- }
if (mMatchSubscriberIds != null) {
builder.append(", matchSubscriberIds=").append(
Arrays.toString(NetworkIdentityUtils.scrubSubscriberIds(mMatchSubscriberIds)));
@@ -406,7 +383,7 @@
@Override
public int hashCode() {
- return Objects.hash(mMatchRule, mSubscriberId, Arrays.hashCode(mMatchWifiNetworkKeys),
+ return Objects.hash(mMatchRule, Arrays.hashCode(mMatchWifiNetworkKeys),
mMetered, mRoaming, mDefaultNetwork, mRatType, mOemManaged);
}
@@ -415,7 +392,6 @@
if (obj instanceof NetworkTemplate) {
final NetworkTemplate other = (NetworkTemplate) obj;
return mMatchRule == other.mMatchRule
- && Objects.equals(mSubscriberId, other.mSubscriberId)
&& mMetered == other.mMetered
&& mRoaming == other.mRoaming
&& mDefaultNetwork == other.mDefaultNetwork
@@ -426,42 +402,25 @@
return false;
}
- /** @hide */
- public boolean isMatchRuleMobile() {
- switch (mMatchRule) {
- case MATCH_MOBILE:
- case MATCH_MOBILE_WILDCARD:
- return true;
- default:
- return false;
- }
- }
-
/**
* Get match rule of the template. See {@code MATCH_*}.
*/
- @UnsupportedAppUsage
public int getMatchRule() {
- // Wildcard rules are not exposed. For external callers, convert wildcard rules to
- // exposed rules before returning.
- switch (mMatchRule) {
- case MATCH_MOBILE_WILDCARD:
- return MATCH_MOBILE;
- case MATCH_WIFI_WILDCARD:
- return MATCH_WIFI;
- default:
- return mMatchRule;
- }
+ return mMatchRule;
}
/**
* Get subscriber Id of the template.
+ *
+ * @deprecated User should use {@link #getSubscriberIds} instead.
* @hide
*/
+ @Deprecated
@Nullable
- @UnsupportedAppUsage
+ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.TIRAMISU,
+ publicAlternatives = "Caller should use {@code getSubscriberIds} instead.")
public String getSubscriberId() {
- return mSubscriberId;
+ return CollectionUtils.isEmpty(mMatchSubscriberIds) ? null : mMatchSubscriberIds[0];
}
/**
@@ -548,10 +507,6 @@
return matchesWifi(ident);
case MATCH_ETHERNET:
return matchesEthernet(ident);
- case MATCH_MOBILE_WILDCARD:
- return matchesMobileWildcard(ident);
- case MATCH_WIFI_WILDCARD:
- return matchesWifiWildcard(ident);
case MATCH_BLUETOOTH:
return matchesBluetooth(ident);
case MATCH_PROXY:
@@ -632,9 +587,9 @@
// TODO: consider matching against WiMAX subscriber identity
return true;
} else {
- return ident.mType == TYPE_MOBILE && !CollectionUtils.isEmpty(mMatchSubscriberIds)
- && CollectionUtils.contains(mMatchSubscriberIds, ident.mSubscriberId)
- && matchesCollapsedRatType(ident);
+ return (CollectionUtils.isEmpty(mMatchSubscriberIds)
+ || CollectionUtils.contains(mMatchSubscriberIds, ident.mSubscriberId))
+ && (ident.mType == TYPE_MOBILE && matchesCollapsedRatType(ident));
}
}
@@ -646,6 +601,8 @@
case TYPE_WIFI:
return matchesSubscriberId(ident.mSubscriberId)
&& matchesWifiNetworkKey(ident.mWifiNetworkKey);
+ case TYPE_WIFI_P2P:
+ return CollectionUtils.isEmpty(mMatchWifiNetworkKeys);
default:
return false;
}
@@ -681,24 +638,6 @@
|| CollectionUtils.contains(mMatchWifiNetworkKeys, ident.mWifiNetworkKey)));
}
- private boolean matchesMobileWildcard(NetworkIdentity ident) {
- if (ident.mType == TYPE_WIMAX) {
- return true;
- } else {
- return ident.mType == TYPE_MOBILE && matchesCollapsedRatType(ident);
- }
- }
-
- private boolean matchesWifiWildcard(NetworkIdentity ident) {
- switch (ident.mType) {
- case TYPE_WIFI:
- case TYPE_WIFI_P2P:
- return true;
- default:
- return false;
- }
- }
-
/**
* Check if matches Bluetooth network template.
*/
@@ -724,10 +663,6 @@
return "WIFI";
case MATCH_ETHERNET:
return "ETHERNET";
- case MATCH_MOBILE_WILDCARD:
- return "MOBILE_WILDCARD";
- case MATCH_WIFI_WILDCARD:
- return "WIFI_WILDCARD";
case MATCH_BLUETOOTH:
return "BLUETOOTH";
case MATCH_PROXY:
@@ -775,20 +710,20 @@
// information. For instances:
// The TYPE_WIFI with subscriberId means that it is a merged carrier wifi network.
// The TYPE_CARRIER means that the network associate to specific carrier network.
- if (template.mSubscriberId == null) return template;
- if (CollectionUtils.contains(merged, template.mSubscriberId)) {
+ if (CollectionUtils.isEmpty(template.mMatchSubscriberIds)) return template;
+
+ if (CollectionUtils.contains(merged, template.mMatchSubscriberIds[0])) {
// Requested template subscriber is part of the merge group; return
// a template that matches all merged subscribers.
final String[] matchWifiNetworkKeys = template.mMatchWifiNetworkKeys;
// TODO: Use NetworkTemplate.Builder to build a template after NetworkTemplate
// could handle incompatible subscriberIds. See b/217805241.
- return new NetworkTemplate(template.mMatchRule, merged[0], merged,
+ return new NetworkTemplate(template.mMatchRule, merged,
CollectionUtils.isEmpty(matchWifiNetworkKeys)
? new String[0] : new String[] { matchWifiNetworkKeys[0] },
- (template.mMatchRule == MATCH_MOBILE
- || template.mMatchRule == MATCH_MOBILE_WILDCARD
- || template.mMatchRule == MATCH_CARRIER) ? METERED_YES : METERED_ALL,
+ (template.mMatchRule == MATCH_MOBILE || template.mMatchRule == MATCH_CARRIER)
+ ? METERED_YES : METERED_ALL,
ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL);
}
@@ -956,10 +891,7 @@
* @param matchRule the target match rule to be checked.
*/
private static void assertRequestableMatchRule(final int matchRule) {
- if (!isKnownMatchRule(matchRule)
- || matchRule == MATCH_PROXY
- || matchRule == MATCH_MOBILE_WILDCARD
- || matchRule == MATCH_WIFI_WILDCARD) {
+ if (!isKnownMatchRule(matchRule) || matchRule == MATCH_PROXY) {
throw new IllegalArgumentException("Invalid match rule: "
+ getMatchRuleName(matchRule));
}
@@ -980,20 +912,6 @@
}
/**
- * For backward compatibility, deduce match rule to a wildcard match rule
- * if the Subscriber Ids are empty.
- */
- private int getWildcardDeducedMatchRule() {
- if (mMatchRule == MATCH_MOBILE && mMatchSubscriberIds.isEmpty()) {
- return MATCH_MOBILE_WILDCARD;
- } else if (mMatchRule == MATCH_WIFI && mMatchSubscriberIds.isEmpty()
- && mMatchWifiNetworkKeys.isEmpty()) {
- return MATCH_WIFI_WILDCARD;
- }
- return mMatchRule;
- }
-
- /**
* Builds the instance of the NetworkTemplate.
*
* @return the built instance of NetworkTemplate.
@@ -1001,8 +919,7 @@
@NonNull
public NetworkTemplate build() {
assertRequestableParameters();
- return new NetworkTemplate(getWildcardDeducedMatchRule(),
- mMatchSubscriberIds.isEmpty() ? null : mMatchSubscriberIds.iterator().next(),
+ return new NetworkTemplate(mMatchRule,
mMatchSubscriberIds.toArray(new String[0]),
mMatchWifiNetworkKeys.toArray(new String[0]), mMetered, mRoaming,
mDefaultNetwork, mRatType, mOemManaged);
diff --git a/framework-t/src/android/net/nsd/INsdManagerCallback.aidl b/framework-t/src/android/net/nsd/INsdManagerCallback.aidl
index 1a262ec..d89bfa9 100644
--- a/framework-t/src/android/net/nsd/INsdManagerCallback.aidl
+++ b/framework-t/src/android/net/nsd/INsdManagerCallback.aidl
@@ -36,4 +36,10 @@
void onUnregisterServiceSucceeded(int listenerKey);
void onResolveServiceFailed(int listenerKey, int error);
void onResolveServiceSucceeded(int listenerKey, in NsdServiceInfo info);
+ void onStopResolutionFailed(int listenerKey, int error);
+ void onStopResolutionSucceeded(int listenerKey);
+ void onServiceInfoCallbackRegistrationFailed(int listenerKey, int error);
+ void onServiceUpdated(int listenerKey, in NsdServiceInfo info);
+ void onServiceUpdatedLost(int listenerKey);
+ void onServiceInfoCallbackUnregistered(int listenerKey);
}
diff --git a/framework-t/src/android/net/nsd/INsdServiceConnector.aidl b/framework-t/src/android/net/nsd/INsdServiceConnector.aidl
index b06ae55..5533154 100644
--- a/framework-t/src/android/net/nsd/INsdServiceConnector.aidl
+++ b/framework-t/src/android/net/nsd/INsdServiceConnector.aidl
@@ -32,4 +32,7 @@
void stopDiscovery(int listenerKey);
void resolveService(int listenerKey, in NsdServiceInfo serviceInfo);
void startDaemon();
+ void stopResolution(int listenerKey);
+ void registerServiceInfoCallback(int listenerKey, in NsdServiceInfo serviceInfo);
+ void unregisterServiceInfoCallback(int listenerKey);
}
\ No newline at end of file
diff --git a/framework-t/src/android/net/nsd/NsdManager.java b/framework-t/src/android/net/nsd/NsdManager.java
index fb3b1d6..122e3a0 100644
--- a/framework-t/src/android/net/nsd/NsdManager.java
+++ b/framework-t/src/android/net/nsd/NsdManager.java
@@ -16,6 +16,7 @@
package android.net.nsd;
+import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
@@ -44,6 +45,8 @@
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
import java.util.concurrent.Executor;
@@ -230,7 +233,6 @@
/** @hide */
public static final int DAEMON_CLEANUP = 18;
-
/** @hide */
public static final int DAEMON_STARTUP = 19;
@@ -242,6 +244,30 @@
/** @hide */
public static final int UNREGISTER_CLIENT = 22;
+ /** @hide */
+ public static final int MDNS_DISCOVERY_MANAGER_EVENT = 23;
+
+ /** @hide */
+ public static final int STOP_RESOLUTION = 24;
+ /** @hide */
+ public static final int STOP_RESOLUTION_FAILED = 25;
+ /** @hide */
+ public static final int STOP_RESOLUTION_SUCCEEDED = 26;
+
+ /** @hide */
+ public static final int REGISTER_SERVICE_CALLBACK = 27;
+ /** @hide */
+ public static final int REGISTER_SERVICE_CALLBACK_FAILED = 28;
+ /** @hide */
+ public static final int SERVICE_UPDATED = 29;
+ /** @hide */
+ public static final int SERVICE_UPDATED_LOST = 30;
+
+ /** @hide */
+ public static final int UNREGISTER_SERVICE_CALLBACK = 31;
+ /** @hide */
+ public static final int UNREGISTER_SERVICE_CALLBACK_SUCCEEDED = 32;
+
/** Dns based service discovery protocol */
public static final int PROTOCOL_DNS_SD = 0x0001;
@@ -267,6 +293,15 @@
EVENT_NAMES.put(DAEMON_CLEANUP, "DAEMON_CLEANUP");
EVENT_NAMES.put(DAEMON_STARTUP, "DAEMON_STARTUP");
EVENT_NAMES.put(MDNS_SERVICE_EVENT, "MDNS_SERVICE_EVENT");
+ EVENT_NAMES.put(STOP_RESOLUTION, "STOP_RESOLUTION");
+ EVENT_NAMES.put(STOP_RESOLUTION_FAILED, "STOP_RESOLUTION_FAILED");
+ EVENT_NAMES.put(STOP_RESOLUTION_SUCCEEDED, "STOP_RESOLUTION_SUCCEEDED");
+ EVENT_NAMES.put(REGISTER_SERVICE_CALLBACK, "REGISTER_SERVICE_CALLBACK");
+ EVENT_NAMES.put(REGISTER_SERVICE_CALLBACK_FAILED, "REGISTER_SERVICE_CALLBACK_FAILED");
+ EVENT_NAMES.put(SERVICE_UPDATED, "SERVICE_UPDATED");
+ EVENT_NAMES.put(UNREGISTER_SERVICE_CALLBACK, "UNREGISTER_SERVICE_CALLBACK");
+ EVENT_NAMES.put(UNREGISTER_SERVICE_CALLBACK_SUCCEEDED,
+ "UNREGISTER_SERVICE_CALLBACK_SUCCEEDED");
}
/** @hide */
@@ -592,6 +627,36 @@
public void onResolveServiceSucceeded(int listenerKey, NsdServiceInfo info) {
sendInfo(RESOLVE_SERVICE_SUCCEEDED, listenerKey, info);
}
+
+ @Override
+ public void onStopResolutionFailed(int listenerKey, int error) {
+ sendError(STOP_RESOLUTION_FAILED, listenerKey, error);
+ }
+
+ @Override
+ public void onStopResolutionSucceeded(int listenerKey) {
+ sendNoArg(STOP_RESOLUTION_SUCCEEDED, listenerKey);
+ }
+
+ @Override
+ public void onServiceInfoCallbackRegistrationFailed(int listenerKey, int error) {
+ sendError(REGISTER_SERVICE_CALLBACK_FAILED, listenerKey, error);
+ }
+
+ @Override
+ public void onServiceUpdated(int listenerKey, NsdServiceInfo info) {
+ sendInfo(SERVICE_UPDATED, listenerKey, info);
+ }
+
+ @Override
+ public void onServiceUpdatedLost(int listenerKey) {
+ sendNoArg(SERVICE_UPDATED_LOST, listenerKey);
+ }
+
+ @Override
+ public void onServiceInfoCallbackUnregistered(int listenerKey) {
+ sendNoArg(UNREGISTER_SERVICE_CALLBACK_SUCCEEDED, listenerKey);
+ }
}
/**
@@ -615,6 +680,37 @@
*/
public static final int FAILURE_MAX_LIMIT = 4;
+ /**
+ * Indicates that the stop operation failed because it is not running.
+ * This failure is passed with {@link ResolveListener#onStopResolutionFailed}.
+ */
+ public static final int FAILURE_OPERATION_NOT_RUNNING = 5;
+
+ /**
+ * Indicates that the service has failed to resolve because of bad parameters.
+ *
+ * This failure is passed with
+ * {@link ServiceInfoCallback#onServiceInfoCallbackRegistrationFailed}.
+ */
+ public static final int FAILURE_BAD_PARAMETERS = 6;
+
+ /** @hide */
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef(value = {
+ FAILURE_OPERATION_NOT_RUNNING,
+ })
+ public @interface StopOperationFailureCode {
+ }
+
+ /** @hide */
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef(value = {
+ FAILURE_ALREADY_ACTIVE,
+ FAILURE_BAD_PARAMETERS,
+ })
+ public @interface ResolutionFailureCode {
+ }
+
/** Interface for callback invocation for service discovery */
public interface DiscoveryListener {
@@ -643,12 +739,97 @@
public void onServiceUnregistered(NsdServiceInfo serviceInfo);
}
- /** Interface for callback invocation for service resolution */
+ /**
+ * Callback for use with {@link NsdManager#resolveService} to resolve the service info and use
+ * with {@link NsdManager#stopServiceResolution} to stop resolution.
+ */
public interface ResolveListener {
- public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode);
+ /**
+ * Called on the internal thread or with an executor passed to
+ * {@link NsdManager#resolveService} to report the resolution was failed with an error.
+ *
+ * A resolution operation would call either onServiceResolved or onResolveFailed once based
+ * on the result.
+ */
+ void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode);
- public void onServiceResolved(NsdServiceInfo serviceInfo);
+ /**
+ * Called on the internal thread or with an executor passed to
+ * {@link NsdManager#resolveService} to report the resolved service info.
+ *
+ * A resolution operation would call either onServiceResolved or onResolveFailed once based
+ * on the result.
+ */
+ void onServiceResolved(NsdServiceInfo serviceInfo);
+
+ /**
+ * Called on the internal thread or with an executor passed to
+ * {@link NsdManager#resolveService} to report the resolution was stopped.
+ *
+ * A stop resolution operation would call either onResolveStopped or onStopResolutionFailed
+ * once based on the result.
+ */
+ default void onResolveStopped(@NonNull NsdServiceInfo serviceInfo) { }
+
+ /**
+ * Called once on the internal thread or with an executor passed to
+ * {@link NsdManager#resolveService} to report that stopping resolution failed with an
+ * error.
+ *
+ * A stop resolution operation would call either onResolveStopped or onStopResolutionFailed
+ * once based on the result.
+ */
+ default void onStopResolutionFailed(@NonNull NsdServiceInfo serviceInfo,
+ @StopOperationFailureCode int errorCode) { }
+ }
+
+ /**
+ * Callback to listen to service info updates.
+ *
+ * For use with {@link NsdManager#registerServiceInfoCallback} to register, and with
+ * {@link NsdManager#unregisterServiceInfoCallback} to stop listening.
+ */
+ public interface ServiceInfoCallback {
+
+ /**
+ * Reports that registering the callback failed with an error.
+ *
+ * Called on the executor passed to {@link NsdManager#registerServiceInfoCallback}.
+ *
+ * onServiceInfoCallbackRegistrationFailed will be called exactly once when the callback
+ * could not be registered. No other callback will be sent in that case.
+ */
+ void onServiceInfoCallbackRegistrationFailed(@ResolutionFailureCode int errorCode);
+
+ /**
+ * Reports updated service info.
+ *
+ * Called on the executor passed to {@link NsdManager#registerServiceInfoCallback}. Any
+ * service updates will be notified via this callback until
+ * {@link NsdManager#unregisterServiceInfoCallback} is called. This will only be called once
+ * the service is found, so may never be called if the service is never present.
+ */
+ void onServiceUpdated(@NonNull NsdServiceInfo serviceInfo);
+
+ /**
+ * Reports when the service that this callback listens to becomes unavailable.
+ *
+ * Called on the executor passed to {@link NsdManager#registerServiceInfoCallback}. The
+ * service may become available again, in which case {@link #onServiceUpdated} will be
+ * called.
+ */
+ void onServiceLost();
+
+ /**
+ * Reports that service info updates have stopped.
+ *
+ * Called on the executor passed to {@link NsdManager#registerServiceInfoCallback}.
+ *
+ * A callback unregistration operation will call onServiceInfoCallbackUnregistered
+ * once. After this, the callback may be reused.
+ */
+ void onServiceInfoCallbackUnregistered();
}
@VisibleForTesting
@@ -741,6 +922,33 @@
executor.execute(() -> ((ResolveListener) listener).onServiceResolved(
(NsdServiceInfo) obj));
break;
+ case STOP_RESOLUTION_FAILED:
+ removeListener(key);
+ executor.execute(() -> ((ResolveListener) listener).onStopResolutionFailed(
+ ns, errorCode));
+ break;
+ case STOP_RESOLUTION_SUCCEEDED:
+ removeListener(key);
+ executor.execute(() -> ((ResolveListener) listener).onResolveStopped(
+ ns));
+ break;
+ case REGISTER_SERVICE_CALLBACK_FAILED:
+ removeListener(key);
+ executor.execute(() -> ((ServiceInfoCallback) listener)
+ .onServiceInfoCallbackRegistrationFailed(errorCode));
+ break;
+ case SERVICE_UPDATED:
+ executor.execute(() -> ((ServiceInfoCallback) listener)
+ .onServiceUpdated((NsdServiceInfo) obj));
+ break;
+ case SERVICE_UPDATED_LOST:
+ executor.execute(() -> ((ServiceInfoCallback) listener).onServiceLost());
+ break;
+ case UNREGISTER_SERVICE_CALLBACK_SUCCEEDED:
+ removeListener(key);
+ executor.execute(() -> ((ServiceInfoCallback) listener)
+ .onServiceInfoCallbackUnregistered());
+ break;
default:
Log.d(TAG, "Ignored " + message);
break;
@@ -1052,7 +1260,14 @@
* @param serviceInfo service to be resolved
* @param listener to receive callback upon success or failure. Cannot be null.
* Cannot be in use for an active service resolution.
+ *
+ * @deprecated the returned ServiceInfo may get stale at any time after resolution, including
+ * immediately after the callback is called, and may not contain some service information that
+ * could be delivered later, like additional host addresses. Prefer using
+ * {@link #registerServiceInfoCallback}, which will keep the application up-to-date with the
+ * state of the service.
*/
+ @Deprecated
public void resolveService(NsdServiceInfo serviceInfo, ResolveListener listener) {
resolveService(serviceInfo, Runnable::run, listener);
}
@@ -1064,7 +1279,14 @@
* @param serviceInfo service to be resolved
* @param executor Executor to run listener callbacks with
* @param listener to receive callback upon success or failure.
+ *
+ * @deprecated the returned ServiceInfo may get stale at any time after resolution, including
+ * immediately after the callback is called, and may not contain some service information that
+ * could be delivered later, like additional host addresses. Prefer using
+ * {@link #registerServiceInfoCallback}, which will keep the application up-to-date with the
+ * state of the service.
*/
+ @Deprecated
public void resolveService(@NonNull NsdServiceInfo serviceInfo,
@NonNull Executor executor, @NonNull ResolveListener listener) {
checkServiceInfo(serviceInfo);
@@ -1076,6 +1298,85 @@
}
}
+ /**
+ * Stop service resolution initiated with {@link #resolveService}.
+ *
+ * A successful stop is notified with a call to {@link ResolveListener#onResolveStopped}.
+ *
+ * <p> Upon failure to stop service resolution for example if resolution is done or the
+ * requester stops resolution repeatedly, the application is notified
+ * {@link ResolveListener#onStopResolutionFailed} with {@link #FAILURE_OPERATION_NOT_RUNNING}
+ *
+ * @param listener This should be a listener object that was passed to {@link #resolveService}.
+ * It identifies the resolution that should be stopped and notifies of a
+ * successful or unsuccessful stop. Throws {@code IllegalArgumentException} if
+ * the listener was not passed to resolveService before.
+ */
+ public void stopServiceResolution(@NonNull ResolveListener listener) {
+ int id = getListenerKey(listener);
+ try {
+ mService.stopResolution(id);
+ } catch (RemoteException e) {
+ e.rethrowFromSystemServer();
+ }
+ }
+
+ /**
+ * Register a callback to listen for updates to a service.
+ *
+ * An application can listen to a service to continuously monitor availability of given service.
+ * The callback methods will be called on the passed executor. And service updates are sent with
+ * continuous calls to {@link ServiceInfoCallback#onServiceUpdated}.
+ *
+ * This is different from {@link #resolveService} which provides one shot service information.
+ *
+ * <p> An application can listen to a service once a time. It needs to cancel the registration
+ * before registering other callbacks. Upon failure to register a callback for example if
+ * it's a duplicated registration, the application is notified through
+ * {@link ServiceInfoCallback#onServiceInfoCallbackRegistrationFailed} with
+ * {@link #FAILURE_BAD_PARAMETERS} or {@link #FAILURE_ALREADY_ACTIVE}.
+ *
+ * @param serviceInfo the service to receive updates for
+ * @param executor Executor to run callbacks with
+ * @param listener to receive callback upon service update
+ */
+ public void registerServiceInfoCallback(@NonNull NsdServiceInfo serviceInfo,
+ @NonNull Executor executor, @NonNull ServiceInfoCallback listener) {
+ checkServiceInfo(serviceInfo);
+ int key = putListener(listener, executor, serviceInfo);
+ try {
+ mService.registerServiceInfoCallback(key, serviceInfo);
+ } catch (RemoteException e) {
+ e.rethrowFromSystemServer();
+ }
+ }
+
+ /**
+ * Unregister a callback registered with {@link #registerServiceInfoCallback}.
+ *
+ * A successful unregistration is notified with a call to
+ * {@link ServiceInfoCallback#onServiceInfoCallbackUnregistered}. The same callback can only be
+ * reused after this is called.
+ *
+ * <p>If the callback is not already registered, this will throw with
+ * {@link IllegalArgumentException}.
+ *
+ * @param listener This should be a listener object that was passed to
+ * {@link #registerServiceInfoCallback}. It identifies the registration that
+ * should be unregistered and notifies of a successful or unsuccessful stop.
+ * Throws {@code IllegalArgumentException} if the listener was not passed to
+ * {@link #registerServiceInfoCallback} before.
+ */
+ public void unregisterServiceInfoCallback(@NonNull ServiceInfoCallback listener) {
+ // Will throw IllegalArgumentException if the listener is not known
+ int id = getListenerKey(listener);
+ try {
+ mService.unregisterServiceInfoCallback(id);
+ } catch (RemoteException e) {
+ e.rethrowFromSystemServer();
+ }
+ }
+
private static void checkListener(Object listener) {
Objects.requireNonNull(listener, "listener cannot be null");
}
diff --git a/framework-t/src/android/net/nsd/NsdServiceInfo.java b/framework-t/src/android/net/nsd/NsdServiceInfo.java
index 6438a60..caeecdd 100644
--- a/framework-t/src/android/net/nsd/NsdServiceInfo.java
+++ b/framework-t/src/android/net/nsd/NsdServiceInfo.java
@@ -26,10 +26,14 @@
import android.util.ArrayMap;
import android.util.Log;
+import com.android.net.module.util.InetAddressUtils;
+
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.Collections;
+import java.util.List;
import java.util.Map;
/**
@@ -46,7 +50,7 @@
private final ArrayMap<String, byte[]> mTxtRecord = new ArrayMap<>();
- private InetAddress mHost;
+ private final List<InetAddress> mHostAddresses = new ArrayList<>();
private int mPort;
@@ -84,17 +88,32 @@
mServiceType = s;
}
- /** Get the host address. The host address is valid for a resolved service. */
+ /**
+ * Get the host address. The host address is valid for a resolved service.
+ *
+ * @deprecated Use {@link #getHostAddresses()} to get the entire list of addresses for the host.
+ */
+ @Deprecated
public InetAddress getHost() {
- return mHost;
+ return mHostAddresses.size() == 0 ? null : mHostAddresses.get(0);
}
- /** Set the host address */
+ /**
+ * Set the host address
+ *
+ * @deprecated Use {@link #setHostAddresses(List)} to set multiple addresses for the host.
+ */
+ @Deprecated
public void setHost(InetAddress s) {
- mHost = s;
+ setHostAddresses(Collections.singletonList(s));
}
- /** Get port number. The port number is valid for a resolved service. */
+ /**
+ * Get port number. The port number is valid for a resolved service.
+ *
+ * The port is valid for all addresses.
+ * @see #getHostAddresses()
+ */
public int getPort() {
return mPort;
}
@@ -105,6 +124,24 @@
}
/**
+ * Get the host addresses.
+ *
+ * All host addresses are valid for the resolved service.
+ * All addresses share the same port
+ * @see #getPort()
+ */
+ @NonNull
+ public List<InetAddress> getHostAddresses() {
+ return new ArrayList<>(mHostAddresses);
+ }
+
+ /** Set the host addresses */
+ public void setHostAddresses(@NonNull List<InetAddress> addresses) {
+ mHostAddresses.clear();
+ mHostAddresses.addAll(addresses);
+ }
+
+ /**
* Unpack txt information from a base-64 encoded byte array.
*
* @param txtRecordsRawBytes The raw base64 encoded byte array.
@@ -359,7 +396,7 @@
StringBuilder sb = new StringBuilder();
sb.append("name: ").append(mServiceName)
.append(", type: ").append(mServiceType)
- .append(", host: ").append(mHost)
+ .append(", hostAddresses: ").append(TextUtils.join(", ", mHostAddresses))
.append(", port: ").append(mPort)
.append(", network: ").append(mNetwork);
@@ -377,12 +414,6 @@
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mServiceName);
dest.writeString(mServiceType);
- if (mHost != null) {
- dest.writeInt(1);
- dest.writeByteArray(mHost.getAddress());
- } else {
- dest.writeInt(0);
- }
dest.writeInt(mPort);
// TXT record key/value pairs.
@@ -401,6 +432,10 @@
dest.writeParcelable(mNetwork, 0);
dest.writeInt(mInterfaceIndex);
+ dest.writeInt(mHostAddresses.size());
+ for (InetAddress address : mHostAddresses) {
+ InetAddressUtils.parcelInetAddress(dest, address, flags);
+ }
}
/** Implement the Parcelable interface */
@@ -410,13 +445,6 @@
NsdServiceInfo info = new NsdServiceInfo();
info.mServiceName = in.readString();
info.mServiceType = in.readString();
-
- if (in.readInt() == 1) {
- try {
- info.mHost = InetAddress.getByAddress(in.createByteArray());
- } catch (java.net.UnknownHostException e) {}
- }
-
info.mPort = in.readInt();
// TXT record key/value pairs.
@@ -432,6 +460,10 @@
}
info.mNetwork = in.readParcelable(null, Network.class);
info.mInterfaceIndex = in.readInt();
+ int size = in.readInt();
+ for (int i = 0; i < size; i++) {
+ info.mHostAddresses.add(InetAddressUtils.unparcelInetAddress(in));
+ }
return info;
}
diff --git a/framework/api/module-lib-current.txt b/framework/api/module-lib-current.txt
index 7669e0e..f623b05 100644
--- a/framework/api/module-lib-current.txt
+++ b/framework/api/module-lib-current.txt
@@ -63,6 +63,7 @@
field public static final int FIREWALL_RULE_DENY = 2; // 0x2
field public static final int PROFILE_NETWORK_PREFERENCE_DEFAULT = 0; // 0x0
field public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE = 1; // 0x1
+ field public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING = 3; // 0x3
field public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK = 2; // 0x2
}
diff --git a/framework/api/system-current.txt b/framework/api/system-current.txt
index dd3404c..0b03983 100644
--- a/framework/api/system-current.txt
+++ b/framework/api/system-current.txt
@@ -470,7 +470,9 @@
}
public abstract class SocketKeepalive implements java.lang.AutoCloseable {
+ method public final void start(@IntRange(from=0xa, to=0xe10) int, int);
field public static final int ERROR_NO_SUCH_SLOT = -33; // 0xffffffdf
+ field public static final int FLAG_AUTOMATIC_ON_OFF = 1; // 0x1
field public static final int SUCCESS = 0; // 0x0
}
diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java
index 60bc68c..40defd4 100644
--- a/framework/src/android/net/ConnectivityManager.java
+++ b/framework/src/android/net/ConnectivityManager.java
@@ -1232,16 +1232,19 @@
}
/**
- * Preference for {@link ProfileNetworkPreference#setPreference(int)}.
+ * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}.
* See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)}
- * Specify that the traffic for this user should by follow the default rules.
+ * Specify that the traffic for this user should by follow the default rules:
+ * applications in the profile designated by the UserHandle behave like any
+ * other application and use the system default network as their default
+ * network. Compare other PROFILE_NETWORK_PREFERENCE_* settings.
* @hide
*/
@SystemApi(client = MODULE_LIBRARIES)
public static final int PROFILE_NETWORK_PREFERENCE_DEFAULT = 0;
/**
- * Preference for {@link ProfileNetworkPreference#setPreference(int)}.
+ * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}.
* See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)}
* Specify that the traffic for this user should by default go on a network with
* {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}, and on the system default network
@@ -1252,16 +1255,38 @@
public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE = 1;
/**
- * Preference for {@link ProfileNetworkPreference#setPreference(int)}.
+ * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}.
* See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)}
* Specify that the traffic for this user should by default go on a network with
* {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE} and if no such network is available
- * should not go on the system default network
+ * should not have a default network at all (that is, network accesses that
+ * do not specify a network explicitly terminate with an error), even if there
+ * is a system default network available to apps outside this preference.
+ * The apps can still use a non-enterprise network if they request it explicitly
+ * provided that specific network doesn't require any specific permission they
+ * do not hold.
* @hide
*/
@SystemApi(client = MODULE_LIBRARIES)
public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK = 2;
+ /**
+ * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}.
+ * See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)}
+ * Specify that the traffic for this user should by default go on a network with
+ * {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}.
+ * If there is no such network, the apps will have no default
+ * network at all, even if there are available non-enterprise networks on the
+ * device (that is, network accesses that do not specify a network explicitly
+ * terminate with an error). Additionally, the designated apps should be
+ * blocked from using any non-enterprise network even if they specify it
+ * explicitly, unless they hold specific privilege overriding this (see
+ * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS}).
+ * @hide
+ */
+ @SystemApi(client = MODULE_LIBRARIES)
+ public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING = 3;
+
/** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {
diff --git a/framework/src/android/net/IConnectivityManager.aidl b/framework/src/android/net/IConnectivityManager.aidl
index 7b6e769..7db231e 100644
--- a/framework/src/android/net/IConnectivityManager.aidl
+++ b/framework/src/android/net/IConnectivityManager.aidl
@@ -188,7 +188,7 @@
void startNattKeepaliveWithFd(in Network network, in ParcelFileDescriptor pfd, int resourceId,
int intervalSeconds, in ISocketKeepaliveCallback cb, String srcAddr,
- String dstAddr);
+ String dstAddr, boolean automaticOnOffKeepalives);
void startTcpKeepalive(in Network network, in ParcelFileDescriptor pfd, int intervalSeconds,
in ISocketKeepaliveCallback cb);
diff --git a/framework/src/android/net/NattSocketKeepalive.java b/framework/src/android/net/NattSocketKeepalive.java
index 56cc923..4d45e70 100644
--- a/framework/src/android/net/NattSocketKeepalive.java
+++ b/framework/src/android/net/NattSocketKeepalive.java
@@ -47,13 +47,39 @@
mResourceId = resourceId;
}
+ /**
+ * Request that keepalive be started with the given {@code intervalSec}.
+ *
+ * When a VPN is running with the network for this keepalive as its underlying network, the
+ * system can monitor the TCP connections on that VPN to determine whether this keepalive is
+ * necessary. To enable this behavior, pass {@link SocketKeepalive#FLAG_AUTOMATIC_ON_OFF} into
+ * the flags. When this is enabled, the system will disable sending keepalive packets when
+ * there are no TCP connections over the VPN(s) running over this network to save battery, and
+ * restart sending them as soon as any TCP connection is opened over one of the VPN networks.
+ * When no VPN is running on top of this network, this flag has no effect, i.e. the keepalives
+ * are always sent with the specified interval.
+ *
+ * Also {@see SocketKeepalive}.
+ *
+ * @param intervalSec The target interval in seconds between keepalive packet transmissions.
+ * The interval should be between 10 seconds and 3600 seconds. Otherwise,
+ * the supplied {@link Callback} will see a call to
+ * {@link Callback#onError(int)} with {@link #ERROR_INVALID_INTERVAL}.
+ * @param flags Flags to enable/disable available options on this keepalive.
+ * @hide
+ */
@Override
- protected void startImpl(int intervalSec) {
+ protected void startImpl(int intervalSec, int flags) {
+ if (0 != (flags & ~FLAG_AUTOMATIC_ON_OFF)) {
+ throw new IllegalArgumentException("Illegal flag value for "
+ + this.getClass().getSimpleName() + " : " + flags);
+ }
+ final boolean automaticOnOffKeepalives = 0 != (flags & FLAG_AUTOMATIC_ON_OFF);
mExecutor.execute(() -> {
try {
mService.startNattKeepaliveWithFd(mNetwork, mPfd, mResourceId,
- intervalSec, mCallback,
- mSource.getHostAddress(), mDestination.getHostAddress());
+ intervalSec, mCallback, mSource.getHostAddress(),
+ mDestination.getHostAddress(), automaticOnOffKeepalives);
} catch (RemoteException e) {
Log.e(TAG, "Error starting socket keepalive: ", e);
throw e.rethrowFromSystemServer();
diff --git a/framework/src/android/net/NetworkAgent.java b/framework/src/android/net/NetworkAgent.java
index 1486619..732bd87 100644
--- a/framework/src/android/net/NetworkAgent.java
+++ b/framework/src/android/net/NetworkAgent.java
@@ -483,6 +483,20 @@
*/
public static final int EVENT_UNREGISTER_AFTER_REPLACEMENT = BASE + 29;
+ /**
+ * Sent by AutomaticOnOffKeepaliveTracker periodically (when relevant) to trigger monitor
+ * automatic keepalive request.
+ *
+ * NATT keepalives have an automatic mode where the system only sends keepalive packets when
+ * TCP sockets are open over a VPN. The system will check periodically for presence of
+ * such open sockets, and this message is what triggers the re-evaluation.
+ *
+ * arg1 = hardware slot number of the keepalive
+ * obj = {@link Network} that the keepalive is started on.
+ * @hide
+ */
+ public static final int CMD_MONITOR_AUTOMATIC_KEEPALIVE = BASE + 30;
+
private static NetworkInfo getLegacyNetworkInfo(final NetworkAgentConfig config) {
final NetworkInfo ni = new NetworkInfo(config.legacyType, config.legacySubType,
config.legacyTypeName, config.legacySubTypeName);
diff --git a/framework/src/android/net/SocketKeepalive.java b/framework/src/android/net/SocketKeepalive.java
index 57cf5e3..90e5e9b 100644
--- a/framework/src/android/net/SocketKeepalive.java
+++ b/framework/src/android/net/SocketKeepalive.java
@@ -16,6 +16,8 @@
package android.net;
+import static android.annotation.SystemApi.Client.PRIVILEGED_APPS;
+
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
@@ -174,6 +176,27 @@
public @interface KeepaliveEvent {}
/**
+ * Whether the system automatically toggles keepalive when no TCP connection is open on the VPN.
+ *
+ * If this flag is present, the system will monitor the VPN(s) running on top of the specified
+ * network for open TCP connections. When no such connections are open, it will turn off the
+ * keepalives to conserve battery power. When there is at least one such connection it will
+ * turn on the keepalives to make sure functionality is preserved.
+ *
+ * This only works with {@link NattSocketKeepalive}.
+ * @hide
+ */
+ @SystemApi
+ public static final int FLAG_AUTOMATIC_ON_OFF = 1 << 0;
+
+ /** @hide */
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef(prefix = { "FLAG_"}, flag = true, value = {
+ FLAG_AUTOMATIC_ON_OFF
+ })
+ public @interface StartFlags {}
+
+ /**
* The minimum interval in seconds between keepalive packet transmissions.
*
* @hide
@@ -294,13 +317,15 @@
}
/**
- * Request that keepalive be started with the given {@code intervalSec}. See
- * {@link SocketKeepalive}. If the remote binder dies, or the binder call throws an exception
- * when invoking start or stop of the {@link SocketKeepalive}, a {@link RemoteException} will be
- * thrown into the {@code executor}. This is typically not important to catch because the remote
- * party is the system, so if it is not in shape to communicate through binder the system is
- * probably going down anyway. If the caller cares regardless, it can use a custom
- * {@link Executor} to catch the {@link RemoteException}.
+ * Request that keepalive be started with the given {@code intervalSec}.
+ *
+ * See {@link SocketKeepalive}. If the remote binder dies, or the binder call throws an
+ * exception when invoking start or stop of the {@link SocketKeepalive}, a
+ * {@link RuntimeException} caused by a {@link RemoteException} will be thrown into the
+ * {@link Executor}. This is typically not important to catch because the remote party is
+ * the system, so if it is not in shape to communicate through binder the system is going
+ * down anyway. If the caller still cares, it can use a custom {@link Executor} to catch the
+ * {@link RuntimeException}.
*
* @param intervalSec The target interval in seconds between keepalive packet transmissions.
* The interval should be between 10 seconds and 3600 seconds, otherwise
@@ -308,11 +333,35 @@
*/
public final void start(@IntRange(from = MIN_INTERVAL_SEC, to = MAX_INTERVAL_SEC)
int intervalSec) {
- startImpl(intervalSec);
+ startImpl(intervalSec, 0 /* flags */);
+ }
+
+ /**
+ * Request that keepalive be started with the given {@code intervalSec}.
+ *
+ * See {@link SocketKeepalive}. If the remote binder dies, or the binder call throws an
+ * exception when invoking start or stop of the {@link SocketKeepalive}, a
+ * {@link RuntimeException} caused by a {@link RemoteException} will be thrown into the
+ * {@link Executor}. This is typically not important to catch because the remote party is
+ * the system, so if it is not in shape to communicate through binder the system is going
+ * down anyway. If the caller still cares, it can use a custom {@link Executor} to catch the
+ * {@link RuntimeException}.
+ *
+ * @param intervalSec The target interval in seconds between keepalive packet transmissions.
+ * The interval should be between 10 seconds and 3600 seconds. Otherwise,
+ * the supplied {@link Callback} will see a call to
+ * {@link Callback#onError(int)} with {@link #ERROR_INVALID_INTERVAL}.
+ * @param flags Flags to enable/disable available options on this keepalive.
+ * @hide
+ */
+ @SystemApi(client = PRIVILEGED_APPS)
+ public final void start(@IntRange(from = MIN_INTERVAL_SEC, to = MAX_INTERVAL_SEC)
+ int intervalSec, @StartFlags int flags) {
+ startImpl(intervalSec, flags);
}
/** @hide */
- protected abstract void startImpl(int intervalSec);
+ protected abstract void startImpl(int intervalSec, @StartFlags int flags);
/**
* Requests that keepalive be stopped. The application must wait for {@link Callback#onStopped}
diff --git a/framework/src/android/net/TcpSocketKeepalive.java b/framework/src/android/net/TcpSocketKeepalive.java
index 7131784..51d805e 100644
--- a/framework/src/android/net/TcpSocketKeepalive.java
+++ b/framework/src/android/net/TcpSocketKeepalive.java
@@ -50,7 +50,11 @@
* acknowledgement.
*/
@Override
- protected void startImpl(int intervalSec) {
+ protected void startImpl(int intervalSec, int flags) {
+ if (0 != flags) {
+ throw new IllegalArgumentException("Illegal flag value for "
+ + this.getClass().getSimpleName() + " : " + flags);
+ }
mExecutor.execute(() -> {
try {
mService.startTcpKeepalive(mNetwork, mPfd, intervalSec, mCallback);
diff --git a/service-t/Android.bp b/service-t/Android.bp
index d876166..5bf2973 100644
--- a/service-t/Android.bp
+++ b/service-t/Android.bp
@@ -52,6 +52,7 @@
"framework-connectivity-t-pre-jarjar",
// TODO: use framework-tethering-pre-jarjar when it is separated from framework-tethering
"framework-tethering.impl",
+ "framework-wifi",
"service-connectivity-pre-jarjar",
"service-nearby-pre-jarjar",
"ServiceConnectivityResources",
diff --git a/service-t/src/com/android/server/NsdService.java b/service-t/src/com/android/server/NsdService.java
index 1226eea..b361720 100644
--- a/service-t/src/com/android/server/NsdService.java
+++ b/service-t/src/com/android/server/NsdService.java
@@ -17,12 +17,17 @@
package com.android.server;
import static android.net.ConnectivityManager.NETID_UNSET;
+import static android.net.nsd.NsdManager.MDNS_DISCOVERY_MANAGER_EVENT;
import static android.net.nsd.NsdManager.MDNS_SERVICE_EVENT;
+import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.INetd;
+import android.net.InetAddresses;
import android.net.LinkProperties;
import android.net.Network;
import android.net.mdns.aidl.DiscoveryInfo;
@@ -39,9 +44,11 @@
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
+import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.UserHandle;
+import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
@@ -50,7 +57,17 @@
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
+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;
+import com.android.server.connectivity.mdns.MdnsServiceBrowserListener;
+import com.android.server.connectivity.mdns.MdnsServiceInfo;
+import com.android.server.connectivity.mdns.MdnsSocketClientBase;
+import com.android.server.connectivity.mdns.MdnsSocketProvider;
import java.io.FileDescriptor;
import java.io.PrintWriter;
@@ -58,7 +75,17 @@
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;
+import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* Network Service Discovery Service handles remote service discovery operation requests by
@@ -69,8 +96,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;
@@ -78,10 +119,19 @@
private final NsdStateMachine mNsdStateMachine;
private final MDnsManager mMDnsManager;
private final MDnsEventCallback mMDnsEventCallback;
- // WARNING : Accessing this value in any thread is not safe, it must only be changed in the
+ @Nullable
+ private final MdnsMultinetworkSocketClient mMdnsSocketClient;
+ @Nullable
+ 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.
private boolean mIsDaemonStarted = false;
+ private boolean mIsMonitoringSocketsStarted = false;
/**
* Clients receiving asynchronous messages
@@ -98,6 +148,110 @@
// The count of the connected legacy clients.
private int mLegacyClientCount = 0;
+ private static class MdnsListener implements MdnsServiceBrowserListener {
+ protected final int mClientId;
+ protected final int mTransactionId;
+ @NonNull
+ protected final NsdServiceInfo mReqServiceInfo;
+ @NonNull
+ protected final String mListenedServiceType;
+
+ MdnsListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo,
+ @NonNull String listenedServiceType) {
+ mClientId = clientId;
+ mTransactionId = transactionId;
+ mReqServiceInfo = reqServiceInfo;
+ mListenedServiceType = listenedServiceType;
+ }
+
+ @NonNull
+ public String getListenedServiceType() {
+ return mListenedServiceType;
+ }
+
+ @Override
+ public void onServiceFound(@NonNull MdnsServiceInfo serviceInfo) { }
+
+ @Override
+ public void onServiceUpdated(@NonNull MdnsServiceInfo serviceInfo) { }
+
+ @Override
+ public void onServiceRemoved(@NonNull MdnsServiceInfo serviceInfo) { }
+
+ @Override
+ public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo) { }
+
+ @Override
+ public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) { }
+
+ @Override
+ public void onSearchStoppedWithError(int error) { }
+
+ @Override
+ public void onSearchFailedToStart() { }
+
+ @Override
+ public void onDiscoveryQuerySent(@NonNull List<String> subtypes, int transactionId) { }
+
+ @Override
+ public void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode) { }
+ }
+
+ private class DiscoveryListener extends MdnsListener {
+
+ DiscoveryListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo,
+ @NonNull String listenServiceType) {
+ super(clientId, transactionId, reqServiceInfo, listenServiceType);
+ }
+
+ @Override
+ public void onServiceNameDiscovered(@NonNull MdnsServiceInfo serviceInfo) {
+ mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId,
+ NsdManager.SERVICE_FOUND,
+ new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo));
+ }
+
+ @Override
+ public void onServiceNameRemoved(@NonNull MdnsServiceInfo serviceInfo) {
+ mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId,
+ NsdManager.SERVICE_LOST,
+ new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo));
+ }
+ }
+
+ private class ResolutionListener extends MdnsListener {
+
+ ResolutionListener(int clientId, int transactionId, @NonNull NsdServiceInfo reqServiceInfo,
+ @NonNull String listenServiceType) {
+ super(clientId, transactionId, reqServiceInfo, listenServiceType);
+ }
+
+ @Override
+ public void onServiceFound(MdnsServiceInfo serviceInfo) {
+ mNsdStateMachine.sendMessage(MDNS_DISCOVERY_MANAGER_EVENT, mTransactionId,
+ NsdManager.RESOLVE_SERVICE_SUCCEEDED,
+ new MdnsEvent(mClientId, mReqServiceInfo.getServiceType(), serviceInfo));
+ }
+ }
+
+ /**
+ * Data class of mdns service callback information.
+ */
+ private static class MdnsEvent {
+ final int mClientId;
+ @NonNull
+ final String mRequestedServiceType;
+ @NonNull
+ final MdnsServiceInfo mMdnsServiceInfo;
+
+ MdnsEvent(int clientId, @NonNull String requestedServiceType,
+ @NonNull MdnsServiceInfo mdnsServiceInfo) {
+ mClientId = clientId;
+ mRequestedServiceType = requestedServiceType;
+ mMdnsServiceInfo = mdnsServiceInfo;
+ }
+ }
+
private class NsdStateMachine extends StateMachine {
private final DefaultState mDefaultState = new DefaultState();
@@ -148,6 +302,31 @@
this.removeMessages(NsdManager.DAEMON_CLEANUP);
}
+ private void maybeStartMonitoringSockets() {
+ if (mIsMonitoringSocketsStarted) {
+ if (DBG) Log.d(TAG, "Socket monitoring is already started.");
+ return;
+ }
+
+ mMdnsSocketProvider.startMonitoringSockets();
+ mIsMonitoringSocketsStarted = true;
+ }
+
+ private void maybeStopMonitoringSockets() {
+ if (!mIsMonitoringSocketsStarted) {
+ if (DBG) Log.d(TAG, "Socket monitoring has not been started.");
+ return;
+ }
+ mMdnsSocketProvider.stopMonitoringSockets();
+ mIsMonitoringSocketsStarted = false;
+ }
+
+ private void maybeStopMonitoringSocketsIfNoActiveRequest() {
+ if (!isAnyRequestActive()) {
+ maybeStopMonitoringSockets();
+ }
+ }
+
NsdStateMachine(String name, Handler handler) {
super(name, handler);
addState(mDefaultState);
@@ -179,11 +358,17 @@
final NsdServiceConnector connector = (NsdServiceConnector) msg.obj;
cInfo = mClients.remove(connector);
if (cInfo != null) {
+ if (mMdnsDiscoveryManager != null) {
+ cInfo.unregisterAllListeners();
+ }
cInfo.expungeAllRequests();
if (cInfo.isLegacy()) {
mLegacyClientCount -= 1;
}
}
+ if (mMdnsDiscoveryManager != null || mAdvertiser != null) {
+ maybeStopMonitoringSocketsIfNoActiveRequest();
+ }
maybeScheduleStop();
break;
case NsdManager.DISCOVER_SERVICES:
@@ -221,6 +406,20 @@
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
break;
+ case NsdManager.STOP_RESOLUTION:
+ cInfo = getClientInfoForReply(msg);
+ if (cInfo != null) {
+ cInfo.onStopResolutionFailed(
+ clientId, NsdManager.FAILURE_OPERATION_NOT_RUNNING);
+ }
+ break;
+ case NsdManager.REGISTER_SERVICE_CALLBACK:
+ cInfo = getClientInfoForReply(msg);
+ if (cInfo != null) {
+ cInfo.onServiceInfoCallbackRegistrationFailed(
+ clientId, NsdManager.FAILURE_BAD_PARAMETERS);
+ }
+ break;
case NsdManager.DAEMON_CLEANUP:
maybeStopDaemon();
break;
@@ -282,6 +481,75 @@
clientInfo.mClientRequests.delete(clientId);
mIdToClientInfoMap.remove(globalId);
maybeScheduleStop();
+ maybeStopMonitoringSocketsIfNoActiveRequest();
+ }
+
+ private void storeListenerMap(int clientId, int transactionId, MdnsListener listener,
+ ClientInfo clientInfo) {
+ clientInfo.mClientIds.put(clientId, transactionId);
+ clientInfo.mListeners.put(clientId, listener);
+ mIdToClientInfoMap.put(transactionId, clientInfo);
+ }
+
+ private void removeListenerMap(int clientId, int transactionId, ClientInfo clientInfo) {
+ clientInfo.mClientIds.delete(clientId);
+ clientInfo.mListeners.delete(clientId);
+ mIdToClientInfoMap.remove(transactionId);
+ maybeStopMonitoringSocketsIfNoActiveRequest();
+ }
+
+ private void clearRegisteredServiceInfo(ClientInfo clientInfo) {
+ clientInfo.mRegisteredService = null;
+ clientInfo.mClientIdForServiceUpdates = 0;
+ }
+
+ /**
+ * Check the given service type is valid and construct it to a service type
+ * which can use for discovery / resolution service.
+ *
+ * <p> The valid service type should be 2 labels, or 3 labels if the query is for a
+ * subtype (see RFC6763 7.1). Each label is up to 63 characters and must start with an
+ * underscore; they are alphanumerical characters or dashes or underscore, except the
+ * last one that is just alphanumerical. The last label must be _tcp or _udp.
+ *
+ * @param serviceType the request service type for discovery / resolution service
+ * @return constructed service type or null if the given service type is invalid.
+ */
+ @Nullable
+ private String constructServiceType(String serviceType) {
+ if (TextUtils.isEmpty(serviceType)) return null;
+
+ final Pattern serviceTypePattern = Pattern.compile(
+ "^(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\.)?"
+ + "(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\._(?:tcp|udp))$");
+ final Matcher matcher = serviceTypePattern.matcher(serviceType);
+ if (!matcher.matches()) return null;
+ return matcher.group(1) == null
+ ? 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
@@ -291,7 +559,7 @@
final int clientId = msg.arg2;
final ListenerArgs args;
switch (msg.what) {
- case NsdManager.DISCOVER_SERVICES:
+ case NsdManager.DISCOVER_SERVICES: {
if (DBG) Log.d(TAG, "Discover services");
args = (ListenerArgs) msg.obj;
clientInfo = mClients.get(args.connector);
@@ -309,21 +577,45 @@
break;
}
- maybeStartDaemon();
+ final NsdServiceInfo info = args.serviceInfo;
id = getUniqueId();
- if (discoverServices(id, args.serviceInfo)) {
- if (DBG) {
- Log.d(TAG, "Discover " + msg.arg2 + " " + id
- + args.serviceInfo.getServiceType());
+ if (mMdnsDiscoveryManager != null) {
+ final String serviceType = constructServiceType(info.getServiceType());
+ if (serviceType == null) {
+ clientInfo.onDiscoverServicesFailed(clientId,
+ NsdManager.FAILURE_INTERNAL_ERROR);
+ break;
}
- storeRequestMap(clientId, id, clientInfo, msg.what);
- clientInfo.onDiscoverServicesStarted(clientId, args.serviceInfo);
+
+ final String listenServiceType = serviceType + ".local";
+ maybeStartMonitoringSockets();
+ final MdnsListener listener =
+ new DiscoveryListener(clientId, id, info, listenServiceType);
+ final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
+ .setNetwork(info.getNetwork())
+ .setIsPassiveMode(true)
+ .build();
+ mMdnsDiscoveryManager.registerListener(
+ listenServiceType, listener, options);
+ storeListenerMap(clientId, id, listener, clientInfo);
+ clientInfo.onDiscoverServicesStarted(clientId, info);
} else {
- stopServiceDiscovery(id);
- clientInfo.onDiscoverServicesFailed(clientId,
- NsdManager.FAILURE_INTERNAL_ERROR);
+ maybeStartDaemon();
+ if (discoverServices(id, info)) {
+ if (DBG) {
+ Log.d(TAG, "Discover " + msg.arg2 + " " + id
+ + info.getServiceType());
+ }
+ storeRequestMap(clientId, id, clientInfo, msg.what);
+ clientInfo.onDiscoverServicesStarted(clientId, info);
+ } else {
+ stopServiceDiscovery(id);
+ clientInfo.onDiscoverServicesFailed(clientId,
+ NsdManager.FAILURE_INTERNAL_ERROR);
+ }
}
break;
+ }
case NsdManager.STOP_DISCOVERY:
if (DBG) Log.d(TAG, "Stop service discovery");
args = (ListenerArgs) msg.obj;
@@ -343,12 +635,25 @@
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
break;
}
- removeRequestMap(clientId, id, clientInfo);
- if (stopServiceDiscovery(id)) {
+ if (mMdnsDiscoveryManager != null) {
+ final MdnsListener listener = clientInfo.mListeners.get(clientId);
+ if (listener == null) {
+ clientInfo.onStopDiscoveryFailed(
+ clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ break;
+ }
+ mMdnsDiscoveryManager.unregisterListener(
+ listener.getListenedServiceType(), listener);
+ removeListenerMap(clientId, id, clientInfo);
clientInfo.onStopDiscoverySucceeded(clientId);
} else {
- clientInfo.onStopDiscoveryFailed(
- clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ removeRequestMap(clientId, id, clientInfo);
+ if (stopServiceDiscovery(id)) {
+ clientInfo.onStopDiscoverySucceeded(clientId);
+ } else {
+ clientInfo.onStopDiscoveryFailed(
+ clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ }
}
break;
case NsdManager.REGISTER_SERVICE:
@@ -369,16 +674,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:
@@ -394,14 +719,20 @@
}
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:
+ case NsdManager.RESOLVE_SERVICE: {
if (DBG) Log.d(TAG, "Resolve service");
args = (ListenerArgs) msg.obj;
clientInfo = mClients.get(args.connector);
@@ -413,8 +744,82 @@
break;
}
- if (clientInfo.mResolvedService != null) {
- clientInfo.onResolveServiceFailed(
+ final NsdServiceInfo info = args.serviceInfo;
+ id = getUniqueId();
+ if (mMdnsDiscoveryManager != null) {
+ final String serviceType = constructServiceType(info.getServiceType());
+ if (serviceType == null) {
+ clientInfo.onResolveServiceFailed(clientId,
+ NsdManager.FAILURE_INTERNAL_ERROR);
+ break;
+ }
+ final String resolveServiceType = serviceType + ".local";
+
+ maybeStartMonitoringSockets();
+ final MdnsListener listener =
+ new ResolutionListener(clientId, id, info, resolveServiceType);
+ final MdnsSearchOptions options = MdnsSearchOptions.newBuilder()
+ .setNetwork(info.getNetwork())
+ .setIsPassiveMode(true)
+ .build();
+ mMdnsDiscoveryManager.registerListener(
+ resolveServiceType, listener, options);
+ storeListenerMap(clientId, id, listener, clientInfo);
+ } else {
+ if (clientInfo.mResolvedService != null) {
+ clientInfo.onResolveServiceFailed(
+ clientId, NsdManager.FAILURE_ALREADY_ACTIVE);
+ break;
+ }
+
+ maybeStartDaemon();
+ if (resolveService(id, args.serviceInfo)) {
+ clientInfo.mResolvedService = new NsdServiceInfo();
+ storeRequestMap(clientId, id, clientInfo, msg.what);
+ } else {
+ clientInfo.onResolveServiceFailed(
+ clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ }
+ }
+ break;
+ }
+ case NsdManager.STOP_RESOLUTION:
+ if (DBG) Log.d(TAG, "Stop service resolution");
+ args = (ListenerArgs) msg.obj;
+ clientInfo = mClients.get(args.connector);
+ // If the binder death notification for a INsdManagerCallback was received
+ // before any calls are received by NsdService, the clientInfo would be
+ // cleared and cause NPE. Add a null check here to prevent this corner case.
+ if (clientInfo == null) {
+ Log.e(TAG, "Unknown connector in stop resolution");
+ break;
+ }
+
+ id = clientInfo.mClientIds.get(clientId);
+ removeRequestMap(clientId, id, clientInfo);
+ if (stopResolveService(id)) {
+ clientInfo.onStopResolutionSucceeded(clientId);
+ } else {
+ clientInfo.onStopResolutionFailed(
+ clientId, NsdManager.FAILURE_OPERATION_NOT_RUNNING);
+ }
+ clientInfo.mResolvedService = null;
+ // TODO: Implement the stop resolution with MdnsDiscoveryManager.
+ break;
+ case NsdManager.REGISTER_SERVICE_CALLBACK:
+ if (DBG) Log.d(TAG, "Register a service callback");
+ args = (ListenerArgs) msg.obj;
+ clientInfo = mClients.get(args.connector);
+ // If the binder death notification for a INsdManagerCallback was received
+ // before any calls are received by NsdService, the clientInfo would be
+ // cleared and cause NPE. Add a null check here to prevent this corner case.
+ if (clientInfo == null) {
+ Log.e(TAG, "Unknown connector in callback registration");
+ break;
+ }
+
+ if (clientInfo.mRegisteredService != null) {
+ clientInfo.onServiceInfoCallbackRegistrationFailed(
clientId, NsdManager.FAILURE_ALREADY_ACTIVE);
break;
}
@@ -422,24 +827,64 @@
maybeStartDaemon();
id = getUniqueId();
if (resolveService(id, args.serviceInfo)) {
- clientInfo.mResolvedService = new NsdServiceInfo();
+ clientInfo.mRegisteredService = new NsdServiceInfo();
+ clientInfo.mClientIdForServiceUpdates = clientId;
storeRequestMap(clientId, id, clientInfo, msg.what);
} else {
- clientInfo.onResolveServiceFailed(
- clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ clientInfo.onServiceInfoCallbackRegistrationFailed(
+ clientId, NsdManager.FAILURE_BAD_PARAMETERS);
}
break;
+ case NsdManager.UNREGISTER_SERVICE_CALLBACK:
+ if (DBG) Log.d(TAG, "Unregister a service callback");
+ args = (ListenerArgs) msg.obj;
+ clientInfo = mClients.get(args.connector);
+ // If the binder death notification for a INsdManagerCallback was received
+ // before any calls are received by NsdService, the clientInfo would be
+ // cleared and cause NPE. Add a null check here to prevent this corner case.
+ if (clientInfo == null) {
+ Log.e(TAG, "Unknown connector in callback unregistration");
+ break;
+ }
+
+ id = clientInfo.mClientIds.get(clientId);
+ removeRequestMap(clientId, id, clientInfo);
+ if (stopResolveService(id)) {
+ clientInfo.onServiceInfoCallbackUnregistered(clientId);
+ } else {
+ Log.e(TAG, "Failed to unregister service info callback");
+ }
+ clearRegisteredServiceInfo(clientInfo);
+ break;
case MDNS_SERVICE_EVENT:
if (!handleMDnsServiceEvent(msg.arg1, msg.arg2, msg.obj)) {
return NOT_HANDLED;
}
break;
+ case MDNS_DISCOVERY_MANAGER_EVENT:
+ if (!handleMdnsDiscoveryManagerEvent(msg.arg1, msg.arg2, msg.obj)) {
+ return NOT_HANDLED;
+ }
+ break;
default:
return NOT_HANDLED;
}
return HANDLED;
}
+ private void notifyResolveFailedResult(boolean isListenedToUpdates, int clientId,
+ ClientInfo clientInfo, int error) {
+ if (isListenedToUpdates) {
+ clientInfo.onServiceInfoCallbackRegistrationFailed(clientId, error);
+ clearRegisteredServiceInfo(clientInfo);
+ } else {
+ // The resolve API always returned FAILURE_INTERNAL_ERROR on error; keep it
+ // for backwards compatibility.
+ clientInfo.onResolveServiceFailed(clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ clientInfo.mResolvedService = null;
+ }
+ }
+
private boolean handleMDnsServiceEvent(int code, int id, Object obj) {
NsdServiceInfo servInfo;
ClientInfo clientInfo = mIdToClientInfoMap.get(id);
@@ -490,6 +935,8 @@
// found services on the same interface index and their network at the time
setServiceNetworkForCallback(servInfo, lostNetId, info.interfaceIdx);
clientInfo.onServiceLost(clientId, servInfo);
+ // TODO: also support registered service lost when not discovering
+ clientInfo.maybeNotifyRegisteredServiceLost(servInfo);
break;
}
case IMDnsEventListener.SERVICE_DISCOVERY_FAILED:
@@ -526,10 +973,15 @@
String rest = fullName.substring(index);
String type = rest.replace(".local.", "");
- clientInfo.mResolvedService.setServiceName(name);
- clientInfo.mResolvedService.setServiceType(type);
- clientInfo.mResolvedService.setPort(info.port);
- clientInfo.mResolvedService.setTxtRecords(info.txtRecord);
+ final boolean isListenedToUpdates =
+ clientId == clientInfo.mClientIdForServiceUpdates;
+ final NsdServiceInfo serviceInfo = isListenedToUpdates
+ ? clientInfo.mRegisteredService : clientInfo.mResolvedService;
+
+ serviceInfo.setServiceName(name);
+ serviceInfo.setServiceType(type);
+ serviceInfo.setPort(info.port);
+ serviceInfo.setTxtRecords(info.txtRecord);
// Network will be added after SERVICE_GET_ADDR_SUCCESS
stopResolveService(id);
@@ -539,9 +991,8 @@
if (getAddrInfo(id2, info.hostname, info.interfaceIdx)) {
storeRequestMap(clientId, id2, clientInfo, NsdManager.RESOLVE_SERVICE);
} else {
- clientInfo.onResolveServiceFailed(
- clientId, NsdManager.FAILURE_INTERNAL_ERROR);
- clientInfo.mResolvedService = null;
+ notifyResolveFailedResult(isListenedToUpdates, clientId, clientInfo,
+ NsdManager.FAILURE_BAD_PARAMETERS);
}
break;
}
@@ -549,17 +1000,17 @@
/* NNN resolveId errorCode */
stopResolveService(id);
removeRequestMap(clientId, id, clientInfo);
- clientInfo.mResolvedService = null;
- clientInfo.onResolveServiceFailed(
- clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ notifyResolveFailedResult(
+ clientId == clientInfo.mClientIdForServiceUpdates,
+ clientId, clientInfo, NsdManager.FAILURE_BAD_PARAMETERS);
break;
case IMDnsEventListener.SERVICE_GET_ADDR_FAILED:
/* NNN resolveId errorCode */
stopGetAddrInfo(id);
removeRequestMap(clientId, id, clientInfo);
- clientInfo.mResolvedService = null;
- clientInfo.onResolveServiceFailed(
- clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ notifyResolveFailedResult(
+ clientId == clientInfo.mClientIdForServiceUpdates,
+ clientId, clientInfo, NsdManager.FAILURE_BAD_PARAMETERS);
break;
case IMDnsEventListener.SERVICE_GET_ADDR_SUCCESS: {
/* NNN resolveId hostname ttl addr interfaceIdx netId */
@@ -576,19 +1027,117 @@
// If the resolved service is on an interface without a network, consider it
// as a failure: it would not be usable by apps as they would need
// privileged permissions.
- if (netId != NETID_UNSET && serviceHost != null) {
- clientInfo.mResolvedService.setHost(serviceHost);
- setServiceNetworkForCallback(clientInfo.mResolvedService,
- netId, info.interfaceIdx);
- clientInfo.onResolveServiceSucceeded(
- clientId, clientInfo.mResolvedService);
+ if (clientId == clientInfo.mClientIdForServiceUpdates) {
+ if (netId != NETID_UNSET && serviceHost != null) {
+ setServiceNetworkForCallback(clientInfo.mRegisteredService,
+ netId, info.interfaceIdx);
+ final List<InetAddress> addresses =
+ clientInfo.mRegisteredService.getHostAddresses();
+ addresses.add(serviceHost);
+ clientInfo.mRegisteredService.setHostAddresses(addresses);
+ clientInfo.onServiceUpdated(
+ clientId, clientInfo.mRegisteredService);
+ } else {
+ stopGetAddrInfo(id);
+ removeRequestMap(clientId, id, clientInfo);
+ clearRegisteredServiceInfo(clientInfo);
+ clientInfo.onServiceInfoCallbackRegistrationFailed(
+ clientId, NsdManager.FAILURE_BAD_PARAMETERS);
+ }
} else {
+ if (netId != NETID_UNSET && serviceHost != null) {
+ clientInfo.mResolvedService.setHost(serviceHost);
+ setServiceNetworkForCallback(clientInfo.mResolvedService,
+ netId, info.interfaceIdx);
+ clientInfo.onResolveServiceSucceeded(
+ clientId, clientInfo.mResolvedService);
+ } else {
+ clientInfo.onResolveServiceFailed(
+ clientId, NsdManager.FAILURE_INTERNAL_ERROR);
+ }
+ stopGetAddrInfo(id);
+ removeRequestMap(clientId, id, clientInfo);
+ clientInfo.mResolvedService = null;
+ }
+ break;
+ }
+ default:
+ return false;
+ }
+ return true;
+ }
+
+ private NsdServiceInfo buildNsdServiceInfoFromMdnsEvent(final MdnsEvent event) {
+ final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo;
+ final String serviceType = event.mRequestedServiceType;
+ final String serviceName = serviceInfo.getServiceInstanceName();
+ final NsdServiceInfo servInfo = new NsdServiceInfo(serviceName, serviceType);
+ final Network network = serviceInfo.getNetwork();
+ setServiceNetworkForCallback(
+ servInfo,
+ network == null ? NETID_UNSET : network.netId,
+ serviceInfo.getInterfaceIndex());
+ return servInfo;
+ }
+
+ private boolean handleMdnsDiscoveryManagerEvent(
+ int transactionId, int code, Object obj) {
+ final ClientInfo clientInfo = mIdToClientInfoMap.get(transactionId);
+ if (clientInfo == null) {
+ Log.e(TAG, String.format(
+ "id %d for %d has no client mapping", transactionId, code));
+ return false;
+ }
+
+ final MdnsEvent event = (MdnsEvent) obj;
+ final int clientId = event.mClientId;
+ final NsdServiceInfo info = buildNsdServiceInfoFromMdnsEvent(event);
+ if (DBG) {
+ Log.d(TAG, String.format("MdnsDiscoveryManager event code=%s transactionId=%d",
+ NsdManager.nameOf(code), transactionId));
+ }
+ switch (code) {
+ case NsdManager.SERVICE_FOUND:
+ clientInfo.onServiceFound(clientId, info);
+ break;
+ case NsdManager.SERVICE_LOST:
+ clientInfo.onServiceLost(clientId, info);
+ break;
+ case NsdManager.RESOLVE_SERVICE_SUCCEEDED: {
+ final MdnsServiceInfo serviceInfo = event.mMdnsServiceInfo;
+ // Add '.' in front of the service type that aligns with historical behavior
+ info.setServiceType("." + event.mRequestedServiceType);
+ info.setPort(serviceInfo.getPort());
+
+ Map<String, String> attrs = serviceInfo.getAttributes();
+ for (Map.Entry<String, String> kv : attrs.entrySet()) {
+ final String key = kv.getKey();
+ try {
+ info.setAttribute(key, serviceInfo.getAttributeAsBytes(key));
+ } catch (IllegalArgumentException e) {
+ Log.e(TAG, "Invalid attribute", e);
+ }
+ }
+ try {
+ if (serviceInfo.getIpv4Address() != null) {
+ info.setHost(InetAddresses.parseNumericAddress(
+ serviceInfo.getIpv4Address()));
+ } else {
+ info.setHost(InetAddresses.parseNumericAddress(
+ serviceInfo.getIpv6Address()));
+ }
+ clientInfo.onResolveServiceSucceeded(clientId, info);
+ } catch (IllegalArgumentException e) {
+ Log.wtf(TAG, "Invalid address in RESOLVE_SERVICE_SUCCEEDED", e);
clientInfo.onResolveServiceFailed(
clientId, NsdManager.FAILURE_INTERNAL_ERROR);
}
- stopGetAddrInfo(id);
- removeRequestMap(clientId, id, clientInfo);
- clientInfo.mResolvedService = null;
+
+ // Unregister the listener immediately like IMDnsEventListener design
+ final MdnsListener listener = clientInfo.mListeners.get(clientId);
+ mMdnsDiscoveryManager.unregisterListener(
+ listener.getListenedServiceType(), listener);
+ removeListenerMap(clientId, transactionId, clientInfo);
break;
}
default:
@@ -650,12 +1199,95 @@
@VisibleForTesting
NsdService(Context ctx, Handler handler, long cleanupDelayMs) {
+ this(ctx, handler, cleanupDelayMs, new Dependencies());
+ }
+
+ @VisibleForTesting
+ NsdService(Context ctx, Handler handler, long cleanupDelayMs, Dependencies deps) {
mCleanupDelayMs = cleanupDelayMs;
mContext = ctx;
mNsdStateMachine = new NsdStateMachine(TAG, handler);
mNsdStateMachine.start();
mMDnsManager = ctx.getSystemService(MDnsManager.class);
mMDnsEventCallback = new MDnsEventCallback(mNsdStateMachine);
+
+ 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 {
+ mMdnsSocketClient = null;
+ mMdnsDiscoveryManager = null;
+ }
+
+ if (advertiserEnabled) {
+ mAdvertiser = deps.makeMdnsAdvertiser(handler.getLooper(), mMdnsSocketProvider,
+ new AdvertiserCallback());
+ } else {
+ mAdvertiser = null;
+ }
+ }
+
+ /**
+ * Dependencies of NsdService, for injection in tests.
+ */
+ @VisibleForTesting
+ public static class Dependencies {
+ /**
+ * Check whether the MdnsDiscoveryManager feature is enabled.
+ *
+ * @param context The global context information about an app environment.
+ * @return true if the MdnsDiscoveryManager feature is enabled.
+ */
+ public boolean isMdnsDiscoveryManagerEnabled(Context context) {
+ return DeviceConfigUtils.isFeatureEnabled(context, NAMESPACE_CONNECTIVITY,
+ MDNS_DISCOVERY_MANAGER_VERSION, false /* defaultEnabled */);
+ }
+
+ /**
+ * 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(
+ ExecutorProvider executorProvider, MdnsSocketClientBase socketClient) {
+ return new MdnsDiscoveryManager(executorProvider, socketClient);
+ }
+
+ /**
+ * @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) {
+ return new MdnsSocketProvider(context, looper);
+ }
}
public static NsdService create(Context context) {
@@ -708,6 +1340,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");
@@ -763,6 +1438,26 @@
}
@Override
+ public void stopResolution(int listenerKey) {
+ mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
+ NsdManager.STOP_RESOLUTION, 0, listenerKey, new ListenerArgs(this, null)));
+ }
+
+ @Override
+ public void registerServiceInfoCallback(int listenerKey, NsdServiceInfo serviceInfo) {
+ mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
+ NsdManager.REGISTER_SERVICE_CALLBACK, 0, listenerKey,
+ new ListenerArgs(this, serviceInfo)));
+ }
+
+ @Override
+ public void unregisterServiceInfoCallback(int listenerKey) {
+ mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
+ NsdManager.UNREGISTER_SERVICE_CALLBACK, 0, listenerKey,
+ new ListenerArgs(this, null)));
+ }
+
+ @Override
public void startDaemon() {
mNsdStateMachine.sendMessage(mNsdStateMachine.obtainMessage(
NsdManager.DAEMON_STARTUP, new ListenerArgs(this, null)));
@@ -917,9 +1612,17 @@
/* A map from client id to the type of the request we had received */
private final SparseIntArray mClientRequests = new SparseIntArray();
+ /* A map from client id to the MdnsListener */
+ private final SparseArray<MdnsListener> mListeners = new SparseArray<>();
+
// The target SDK of this client < Build.VERSION_CODES.S
private boolean mIsLegacy = false;
+ /*** The service that is registered to listen to its updates */
+ private NsdServiceInfo mRegisteredService;
+ /*** The client id that listen to updates */
+ private int mClientIdForServiceUpdates;
+
private ClientInfo(INsdManagerCallback cb) {
mCb = cb;
if (DBG) Log.d(TAG, "New client");
@@ -968,7 +1671,11 @@
stopResolveService(globalId);
break;
case NsdManager.REGISTER_SERVICE:
- unregisterService(globalId);
+ if (mAdvertiser != null) {
+ mAdvertiser.removeService(globalId);
+ } else {
+ unregisterService(globalId);
+ }
break;
default:
break;
@@ -978,6 +1685,15 @@
mClientRequests.clear();
}
+ void unregisterAllListeners() {
+ for (int i = 0; i < mListeners.size(); i++) {
+ final MdnsListener listener = mListeners.valueAt(i);
+ mMdnsDiscoveryManager.unregisterListener(
+ listener.getListenedServiceType(), listener);
+ }
+ mListeners.clear();
+ }
+
// mClientIds is a sparse array of listener id -> mDnsClient id. For a given mDnsClient id,
// return the corresponding listener id. mDnsClient id is also called a global id.
private int getClientId(final int globalId) {
@@ -988,6 +1704,18 @@
return mClientIds.keyAt(idx);
}
+ private void maybeNotifyRegisteredServiceLost(@NonNull NsdServiceInfo info) {
+ if (mRegisteredService == null) return;
+ if (!Objects.equals(mRegisteredService.getServiceName(), info.getServiceName())) return;
+ // Resolved services have a leading dot appended at the beginning of their type, but in
+ // discovered info it's at the end
+ if (!Objects.equals(
+ mRegisteredService.getServiceType() + ".", "." + info.getServiceType())) {
+ return;
+ }
+ onServiceUpdatedLost(mClientIdForServiceUpdates);
+ }
+
void onDiscoverServicesStarted(int listenerKey, NsdServiceInfo info) {
try {
mCb.onDiscoverServicesStarted(listenerKey, info);
@@ -1083,5 +1811,53 @@
Log.e(TAG, "Error calling onResolveServiceSucceeded", e);
}
}
+
+ void onStopResolutionFailed(int listenerKey, int error) {
+ try {
+ mCb.onStopResolutionFailed(listenerKey, error);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error calling onStopResolutionFailed", e);
+ }
+ }
+
+ void onStopResolutionSucceeded(int listenerKey) {
+ try {
+ mCb.onStopResolutionSucceeded(listenerKey);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error calling onStopResolutionSucceeded", e);
+ }
+ }
+
+ void onServiceInfoCallbackRegistrationFailed(int listenerKey, int error) {
+ try {
+ mCb.onServiceInfoCallbackRegistrationFailed(listenerKey, error);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error calling onServiceInfoCallbackRegistrationFailed", e);
+ }
+ }
+
+ void onServiceUpdated(int listenerKey, NsdServiceInfo info) {
+ try {
+ mCb.onServiceUpdated(listenerKey, info);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error calling onServiceUpdated", e);
+ }
+ }
+
+ void onServiceUpdatedLost(int listenerKey) {
+ try {
+ mCb.onServiceUpdatedLost(listenerKey);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error calling onServiceUpdatedLost", e);
+ }
+ }
+
+ void onServiceInfoCallbackUnregistered(int listenerKey) {
+ try {
+ mCb.onServiceInfoCallbackUnregistered(listenerKey);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error calling onServiceInfoCallbackUnregistered", e);
+ }
+ }
}
}
diff --git a/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java b/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
index 0605abe..51683de 100644
--- a/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
+++ b/service-t/src/com/android/server/ethernet/EthernetNetworkFactory.java
@@ -491,10 +491,19 @@
mDeps.makeIpClient(mContext, name, mIpClientCallback);
mIpClientCallback.awaitIpClientStart();
+ if (mIpConfig.getProxySettings() == ProxySettings.STATIC
+ || mIpConfig.getProxySettings() == ProxySettings.PAC) {
+ mIpClient.setHttpProxy(mIpConfig.getHttpProxy());
+ }
+
if (sTcpBufferSizes == null) {
sTcpBufferSizes = mDeps.getTcpBufferSizesFromResource(mContext);
}
- provisionIpClient(mIpClient, mIpConfig, sTcpBufferSizes);
+ if (!TextUtils.isEmpty(sTcpBufferSizes)) {
+ mIpClient.setTcpBufferSizes(sTcpBufferSizes);
+ }
+
+ mIpClient.startProvisioning(createProvisioningConfiguration(mIpConfig));
}
void onIpLayerStarted(@NonNull final LinkProperties linkProperties) {
@@ -635,20 +644,6 @@
mRequestIds.clear();
}
- private static void provisionIpClient(@NonNull final IpClientManager ipClient,
- @NonNull final IpConfiguration config, @NonNull final String tcpBufferSizes) {
- if (config.getProxySettings() == ProxySettings.STATIC ||
- config.getProxySettings() == ProxySettings.PAC) {
- ipClient.setHttpProxy(config.getHttpProxy());
- }
-
- if (!TextUtils.isEmpty(tcpBufferSizes)) {
- ipClient.setTcpBufferSizes(tcpBufferSizes);
- }
-
- ipClient.startProvisioning(createProvisioningConfiguration(config));
- }
-
private static ProvisioningConfiguration createProvisioningConfiguration(
@NonNull final IpConfiguration config) {
if (config.getIpAssignment() == IpAssignment.STATIC) {
diff --git a/service/mdns/com/android/server/connectivity/mdns/ConnectivityMonitor.java b/service-t/src/com/android/server/mdns/ConnectivityMonitor.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/ConnectivityMonitor.java
rename to service-t/src/com/android/server/mdns/ConnectivityMonitor.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/ConnectivityMonitorWithConnectivityManager.java b/service-t/src/com/android/server/mdns/ConnectivityMonitorWithConnectivityManager.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/ConnectivityMonitorWithConnectivityManager.java
rename to service-t/src/com/android/server/mdns/ConnectivityMonitorWithConnectivityManager.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/EnqueueMdnsQueryCallable.java b/service-t/src/com/android/server/mdns/EnqueueMdnsQueryCallable.java
similarity index 67%
rename from service/mdns/com/android/server/connectivity/mdns/EnqueueMdnsQueryCallable.java
rename to service-t/src/com/android/server/mdns/EnqueueMdnsQueryCallable.java
index f7871f3..fdd1478 100644
--- a/service/mdns/com/android/server/connectivity/mdns/EnqueueMdnsQueryCallable.java
+++ b/service-t/src/com/android/server/mdns/EnqueueMdnsQueryCallable.java
@@ -18,7 +18,9 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
+import android.net.Network;
import android.text.TextUtils;
+import android.util.Log;
import android.util.Pair;
import com.android.server.connectivity.mdns.util.MdnsLogger;
@@ -58,26 +60,29 @@
}
}
- private final WeakReference<MdnsSocketClient> weakRequestSender;
+ private final WeakReference<MdnsSocketClientBase> weakRequestSender;
private final MdnsPacketWriter packetWriter;
private final String[] serviceTypeLabels;
private final List<String> subtypes;
private final boolean expectUnicastResponse;
private final int transactionId;
+ private final Network network;
EnqueueMdnsQueryCallable(
- @NonNull MdnsSocketClient requestSender,
+ @NonNull MdnsSocketClientBase requestSender,
@NonNull MdnsPacketWriter packetWriter,
@NonNull String serviceType,
@NonNull Collection<String> subtypes,
boolean expectUnicastResponse,
- int transactionId) {
+ int transactionId,
+ @Nullable Network network) {
weakRequestSender = new WeakReference<>(requestSender);
this.packetWriter = packetWriter;
serviceTypeLabels = TextUtils.split(serviceType, "\\.");
this.subtypes = new ArrayList<>(subtypes);
this.expectUnicastResponse = expectUnicastResponse;
this.transactionId = transactionId;
+ this.network = network;
}
// Incompatible return type for override of Callable#call().
@@ -86,7 +91,7 @@
@Nullable
public Pair<Integer, List<String>> call() {
try {
- MdnsSocketClient requestSender = weakRequestSender.get();
+ MdnsSocketClientBase requestSender = weakRequestSender.get();
if (requestSender == null) {
return null;
}
@@ -127,15 +132,24 @@
MdnsConstants.QCLASS_INTERNET
| (expectUnicastResponse ? MdnsConstants.QCLASS_UNICAST : 0));
- InetAddress mdnsAddress = MdnsConstants.getMdnsIPv4Address();
- if (requestSender.isOnIPv6OnlyNetwork()) {
- mdnsAddress = MdnsConstants.getMdnsIPv6Address();
- }
+ if (requestSender instanceof MdnsMultinetworkSocketClient) {
+ sendPacketToIpv4AndIpv6(requestSender, MdnsConstants.MDNS_PORT, network);
+ for (Integer emulatorPort : castShellEmulatorMdnsPorts) {
+ sendPacketToIpv4AndIpv6(requestSender, emulatorPort, network);
+ }
+ } else if (requestSender instanceof MdnsSocketClient) {
+ final MdnsSocketClient client = (MdnsSocketClient) requestSender;
+ InetAddress mdnsAddress = MdnsConstants.getMdnsIPv4Address();
+ if (client.isOnIPv6OnlyNetwork()) {
+ mdnsAddress = MdnsConstants.getMdnsIPv6Address();
+ }
- sendPacketTo(requestSender,
- new InetSocketAddress(mdnsAddress, MdnsConstants.MDNS_PORT));
- for (Integer emulatorPort : castShellEmulatorMdnsPorts) {
- sendPacketTo(requestSender, new InetSocketAddress(mdnsAddress, emulatorPort));
+ sendPacketTo(client, new InetSocketAddress(mdnsAddress, MdnsConstants.MDNS_PORT));
+ for (Integer emulatorPort : castShellEmulatorMdnsPorts) {
+ sendPacketTo(client, new InetSocketAddress(mdnsAddress, emulatorPort));
+ }
+ } else {
+ throw new IOException("Unknown socket client type: " + requestSender.getClass());
}
return Pair.create(transactionId, subtypes);
} catch (IOException e) {
@@ -145,7 +159,7 @@
}
}
- private void sendPacketTo(MdnsSocketClient requestSender, InetSocketAddress address)
+ private void sendPacketTo(MdnsSocketClientBase requestSender, InetSocketAddress address)
throws IOException {
DatagramPacket packet = packetWriter.getPacket(address);
if (expectUnicastResponse) {
@@ -154,4 +168,31 @@
requestSender.sendMulticastPacket(packet);
}
}
+
+ private void sendPacketFromNetwork(MdnsSocketClientBase requestSender,
+ InetSocketAddress address, Network network)
+ throws IOException {
+ DatagramPacket packet = packetWriter.getPacket(address);
+ if (expectUnicastResponse) {
+ requestSender.sendUnicastPacket(packet, network);
+ } else {
+ requestSender.sendMulticastPacket(packet, network);
+ }
+ }
+
+ private void sendPacketToIpv4AndIpv6(MdnsSocketClientBase requestSender, int port,
+ Network network) {
+ try {
+ sendPacketFromNetwork(requestSender,
+ new InetSocketAddress(MdnsConstants.getMdnsIPv4Address(), port), network);
+ } catch (IOException e) {
+ Log.i(TAG, "Can't send packet to IPv4", e);
+ }
+ try {
+ sendPacketFromNetwork(requestSender,
+ new InetSocketAddress(MdnsConstants.getMdnsIPv6Address(), port), network);
+ } catch (IOException e) {
+ Log.i(TAG, "Can't send packet to IPv6", e);
+ }
+ }
}
\ No newline at end of file
diff --git a/service/mdns/com/android/server/connectivity/mdns/ExecutorProvider.java b/service-t/src/com/android/server/mdns/ExecutorProvider.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/ExecutorProvider.java
rename to service-t/src/com/android/server/mdns/ExecutorProvider.java
diff --git a/service-t/src/com/android/server/mdns/MdnsAdvertiser.java b/service-t/src/com/android/server/mdns/MdnsAdvertiser.java
new file mode 100644
index 0000000..977478a
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MdnsAdvertiser.java
@@ -0,0 +1,474 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.net.LinkAddress;
+import android.net.Network;
+import android.net.nsd.NsdManager;
+import android.net.nsd.NsdServiceInfo;
+import android.os.Looper;
+import android.util.ArrayMap;
+import android.util.Log;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import java.util.List;
+import java.util.Map;
+import java.util.function.BiPredicate;
+import java.util.function.Consumer;
+
+/**
+ * MdnsAdvertiser manages advertising services per {@link com.android.server.NsdService} requests.
+ *
+ * All methods except the constructor must be called on the looper thread.
+ */
+public class MdnsAdvertiser {
+ private static final String TAG = MdnsAdvertiser.class.getSimpleName();
+ static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
+
+ private final Looper mLooper;
+ private final AdvertiserCallback mCb;
+
+ // Max-sized buffers to be used as temporary buffer to read/build packets. May be used by
+ // multiple components, but only for self-contained operations in the looper thread, so not
+ // concurrently.
+ // TODO: set according to MTU. 1300 should fit for ethernet MTU 1500 with some overhead.
+ private final byte[] mPacketCreationBuffer = new byte[1300];
+
+ private final MdnsSocketProvider mSocketProvider;
+ private final ArrayMap<Network, InterfaceAdvertiserRequest> mAdvertiserRequests =
+ new ArrayMap<>();
+ private final ArrayMap<MdnsInterfaceSocket, MdnsInterfaceAdvertiser> mAllAdvertisers =
+ new ArrayMap<>();
+ private final SparseArray<Registration> mRegistrations = new SparseArray<>();
+ private final Dependencies mDeps;
+
+ /**
+ * Dependencies for {@link MdnsAdvertiser}, useful for testing.
+ */
+ @VisibleForTesting
+ public static class Dependencies {
+ /**
+ * @see MdnsInterfaceAdvertiser
+ */
+ public MdnsInterfaceAdvertiser makeAdvertiser(@NonNull MdnsInterfaceSocket socket,
+ @NonNull List<LinkAddress> initialAddresses,
+ @NonNull Looper looper, @NonNull byte[] packetCreationBuffer,
+ @NonNull MdnsInterfaceAdvertiser.Callback cb) {
+ // Note NetworkInterface is final and not mockable
+ final String logTag = socket.getInterface().getName();
+ return new MdnsInterfaceAdvertiser(logTag, socket, initialAddresses, looper,
+ packetCreationBuffer, cb);
+ }
+ }
+
+ private final MdnsInterfaceAdvertiser.Callback mInterfaceAdvertiserCb =
+ new MdnsInterfaceAdvertiser.Callback() {
+ @Override
+ public void onRegisterServiceSucceeded(
+ @NonNull MdnsInterfaceAdvertiser advertiser, int serviceId) {
+ // Wait for all current interfaces to be done probing before notifying of success.
+ if (any(mAllAdvertisers, (k, a) -> a.isProbing(serviceId))) return;
+ // The service may still be unregistered/renamed if a conflict is found on a later added
+ // interface, or if a conflicting announcement/reply is detected (RFC6762 9.)
+
+ final Registration registration = mRegistrations.get(serviceId);
+ if (registration == null) {
+ Log.wtf(TAG, "Register succeeded for unknown registration");
+ return;
+ }
+ if (!registration.mNotifiedRegistrationSuccess) {
+ mCb.onRegisterServiceSucceeded(serviceId, registration.getServiceInfo());
+ registration.mNotifiedRegistrationSuccess = true;
+ }
+ }
+
+ @Override
+ public void onServiceConflict(@NonNull MdnsInterfaceAdvertiser advertiser, int serviceId) {
+ if (DBG) {
+ Log.v(TAG, "Found conflict, restarted probing for service " + serviceId);
+ }
+
+ final Registration registration = mRegistrations.get(serviceId);
+ if (registration == null) return;
+ if (registration.mNotifiedRegistrationSuccess) {
+ // TODO: consider notifying clients that the service is no longer registered with
+ // the old name (back to probing). The legacy implementation did not send any
+ // callback though; it only sent onServiceRegistered after re-probing finishes
+ // (with the old, conflicting, actually not used name as argument... The new
+ // implementation will send callbacks with the new name).
+ registration.mNotifiedRegistrationSuccess = false;
+
+ // The service was done probing, just reset it to probing state (RFC6762 9.)
+ forAllAdvertisers(a -> a.restartProbingForConflict(serviceId));
+ return;
+ }
+
+ // Conflict was found during probing; rename once to find a name that has no conflict
+ registration.updateForConflict(
+ registration.makeNewServiceInfoForConflict(1 /* renameCount */),
+ 1 /* renameCount */);
+
+ // Keep renaming if the new name conflicts in local registrations
+ updateRegistrationUntilNoConflict((net, adv) -> adv.hasRegistration(registration),
+ registration);
+
+ // Update advertisers to use the new name
+ forAllAdvertisers(a -> a.renameServiceForConflict(
+ serviceId, registration.getServiceInfo()));
+ }
+
+ @Override
+ public void onDestroyed(@NonNull MdnsInterfaceSocket socket) {
+ for (int i = mAdvertiserRequests.size() - 1; i >= 0; i--) {
+ if (mAdvertiserRequests.valueAt(i).onAdvertiserDestroyed(socket)) {
+ mAdvertiserRequests.removeAt(i);
+ }
+ }
+ mAllAdvertisers.remove(socket);
+ }
+ };
+
+ private boolean hasAnyConflict(
+ @NonNull BiPredicate<Network, InterfaceAdvertiserRequest> applicableAdvertiserFilter,
+ @NonNull NsdServiceInfo newInfo) {
+ return any(mAdvertiserRequests, (network, adv) ->
+ applicableAdvertiserFilter.test(network, adv) && adv.hasConflict(newInfo));
+ }
+
+ private void updateRegistrationUntilNoConflict(
+ @NonNull BiPredicate<Network, InterfaceAdvertiserRequest> applicableAdvertiserFilter,
+ @NonNull Registration registration) {
+ int renameCount = 0;
+ NsdServiceInfo newInfo = registration.getServiceInfo();
+ while (hasAnyConflict(applicableAdvertiserFilter, newInfo)) {
+ renameCount++;
+ newInfo = registration.makeNewServiceInfoForConflict(renameCount);
+ }
+ registration.updateForConflict(newInfo, renameCount);
+ }
+
+ /**
+ * A request for a {@link MdnsInterfaceAdvertiser}.
+ *
+ * This class tracks services to be advertised on all sockets provided via a registered
+ * {@link MdnsSocketProvider.SocketCallback}.
+ */
+ private class InterfaceAdvertiserRequest implements MdnsSocketProvider.SocketCallback {
+ /** Registrations to add to newer MdnsInterfaceAdvertisers when sockets are created. */
+ @NonNull
+ private final SparseArray<Registration> mPendingRegistrations = new SparseArray<>();
+ @NonNull
+ private final ArrayMap<MdnsInterfaceSocket, MdnsInterfaceAdvertiser> mAdvertisers =
+ new ArrayMap<>();
+
+ InterfaceAdvertiserRequest(@Nullable Network requestedNetwork) {
+ mSocketProvider.requestSocket(requestedNetwork, this);
+ }
+
+ /**
+ * Called when an advertiser was destroyed, after all services were unregistered and it sent
+ * exit announcements, or the interface is gone.
+ *
+ * @return true if this {@link InterfaceAdvertiserRequest} should now be deleted.
+ */
+ boolean onAdvertiserDestroyed(@NonNull MdnsInterfaceSocket socket) {
+ mAdvertisers.remove(socket);
+ if (mAdvertisers.size() == 0 && mPendingRegistrations.size() == 0) {
+ // No advertiser is using sockets from this request anymore (in particular for exit
+ // announcements), and there is no registration so newer sockets will not be
+ // necessary, so the request can be unregistered.
+ mSocketProvider.unrequestSocket(this);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Return whether this {@link InterfaceAdvertiserRequest} has the given registration.
+ */
+ boolean hasRegistration(@NonNull Registration registration) {
+ return mPendingRegistrations.indexOfValue(registration) >= 0;
+ }
+
+ /**
+ * Return whether using the proposed new {@link NsdServiceInfo} to add a registration would
+ * cause a conflict in this {@link InterfaceAdvertiserRequest}.
+ */
+ boolean hasConflict(@NonNull NsdServiceInfo newInfo) {
+ return getConflictingService(newInfo) >= 0;
+ }
+
+ /**
+ * Get the ID of a conflicting service, or -1 if none.
+ */
+ int getConflictingService(@NonNull NsdServiceInfo info) {
+ for (int i = 0; i < mPendingRegistrations.size(); i++) {
+ final NsdServiceInfo other = mPendingRegistrations.valueAt(i).getServiceInfo();
+ if (info.getServiceName().equals(other.getServiceName())
+ && info.getServiceType().equals(other.getServiceType())) {
+ return mPendingRegistrations.keyAt(i);
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Add a service.
+ *
+ * Conflicts must be checked via {@link #getConflictingService} before attempting to add.
+ */
+ void addService(int id, Registration registration) {
+ mPendingRegistrations.put(id, registration);
+ for (int i = 0; i < mAdvertisers.size(); i++) {
+ try {
+ mAdvertisers.valueAt(i).addService(id, registration.getServiceInfo());
+ } catch (NameConflictException e) {
+ Log.wtf(TAG, "Name conflict adding services that should have unique names", e);
+ }
+ }
+ }
+
+ void removeService(int id) {
+ mPendingRegistrations.remove(id);
+ for (int i = 0; i < mAdvertisers.size(); i++) {
+ mAdvertisers.valueAt(i).removeService(id);
+ }
+ }
+
+ @Override
+ public void onSocketCreated(@NonNull Network network,
+ @NonNull MdnsInterfaceSocket socket,
+ @NonNull List<LinkAddress> addresses) {
+ MdnsInterfaceAdvertiser advertiser = mAllAdvertisers.get(socket);
+ if (advertiser == null) {
+ advertiser = mDeps.makeAdvertiser(socket, addresses, mLooper, mPacketCreationBuffer,
+ mInterfaceAdvertiserCb);
+ mAllAdvertisers.put(socket, advertiser);
+ advertiser.start();
+ }
+ mAdvertisers.put(socket, advertiser);
+ for (int i = 0; i < mPendingRegistrations.size(); i++) {
+ try {
+ advertiser.addService(mPendingRegistrations.keyAt(i),
+ mPendingRegistrations.valueAt(i).getServiceInfo());
+ } catch (NameConflictException e) {
+ Log.wtf(TAG, "Name conflict adding services that should have unique names", e);
+ }
+ }
+ }
+
+ @Override
+ public void onInterfaceDestroyed(@NonNull Network network,
+ @NonNull MdnsInterfaceSocket socket) {
+ final MdnsInterfaceAdvertiser advertiser = mAdvertisers.get(socket);
+ if (advertiser != null) advertiser.destroyNow();
+ }
+
+ @Override
+ public void onAddressesChanged(@NonNull Network network,
+ @NonNull MdnsInterfaceSocket socket, @NonNull List<LinkAddress> addresses) {
+ final MdnsInterfaceAdvertiser advertiser = mAdvertisers.get(socket);
+ if (advertiser != null) advertiser.updateAddresses(addresses);
+ }
+ }
+
+ private static class Registration {
+ @NonNull
+ final String mOriginalName;
+ boolean mNotifiedRegistrationSuccess;
+ private int mConflictCount;
+ @NonNull
+ private NsdServiceInfo mServiceInfo;
+
+ private Registration(@NonNull NsdServiceInfo serviceInfo) {
+ this.mOriginalName = serviceInfo.getServiceName();
+ this.mServiceInfo = serviceInfo;
+ }
+
+ /**
+ * Update the registration to use a different service name, after a conflict was found.
+ *
+ * @param newInfo New service info to use.
+ * @param renameCount How many renames were done before reaching the current name.
+ */
+ private void updateForConflict(@NonNull NsdServiceInfo newInfo, int renameCount) {
+ mConflictCount += renameCount;
+ mServiceInfo = newInfo;
+ }
+
+ /**
+ * Make a new service name for the registration, after a conflict was found.
+ *
+ * If a name conflict was found during probing or because different advertising requests
+ * used the same name, the registration is attempted again with a new name (here using
+ * a number suffix, (1), (2) etc). Registration success is notified once probing succeeds
+ * with a new name. This matches legacy behavior based on mdnsresponder, and appendix D of
+ * RFC6763.
+ *
+ * @param renameCount How much to increase the number suffix for this conflict.
+ */
+ @NonNull
+ public NsdServiceInfo makeNewServiceInfoForConflict(int renameCount) {
+ // In case of conflict choose a different service name. After the first conflict use
+ // "Name (2)", then "Name (3)" etc.
+ // TODO: use a hidden method in NsdServiceInfo once MdnsAdvertiser is moved to service-t
+ final NsdServiceInfo newInfo = new NsdServiceInfo();
+ newInfo.setServiceName(mOriginalName + " (" + (mConflictCount + renameCount + 1) + ")");
+ newInfo.setServiceType(mServiceInfo.getServiceType());
+ for (Map.Entry<String, byte[]> attr : mServiceInfo.getAttributes().entrySet()) {
+ newInfo.setAttribute(attr.getKey(),
+ attr.getValue() == null ? null : new String(attr.getValue()));
+ }
+ newInfo.setHost(mServiceInfo.getHost());
+ newInfo.setPort(mServiceInfo.getPort());
+ newInfo.setNetwork(mServiceInfo.getNetwork());
+ // interfaceIndex is not set when registering
+ return newInfo;
+ }
+
+ @NonNull
+ public NsdServiceInfo getServiceInfo() {
+ return mServiceInfo;
+ }
+ }
+
+ /**
+ * Callbacks for advertising services.
+ *
+ * Every method is called on the MdnsAdvertiser looper thread.
+ */
+ public interface AdvertiserCallback {
+ /**
+ * Called when a service was successfully registered, after probing.
+ *
+ * @param serviceId ID of the service provided when registering.
+ * @param registeredInfo Registered info, which may be different from the requested info,
+ * after probing and possibly choosing alternative service names.
+ */
+ void onRegisterServiceSucceeded(int serviceId, NsdServiceInfo registeredInfo);
+
+ /**
+ * Called when service registration failed.
+ *
+ * @param serviceId ID of the service provided when registering.
+ * @param errorCode One of {@code NsdManager.FAILURE_*}
+ */
+ void onRegisterServiceFailed(int serviceId, int errorCode);
+
+ // Unregistration is notified immediately as success in NsdService so no callback is needed
+ // here.
+ }
+
+ public MdnsAdvertiser(@NonNull Looper looper, @NonNull MdnsSocketProvider socketProvider,
+ @NonNull AdvertiserCallback cb) {
+ this(looper, socketProvider, cb, new Dependencies());
+ }
+
+ @VisibleForTesting
+ MdnsAdvertiser(@NonNull Looper looper, @NonNull MdnsSocketProvider socketProvider,
+ @NonNull AdvertiserCallback cb, @NonNull Dependencies deps) {
+ mLooper = looper;
+ mCb = cb;
+ mSocketProvider = socketProvider;
+ mDeps = deps;
+ }
+
+ private void checkThread() {
+ if (Thread.currentThread() != mLooper.getThread()) {
+ throw new IllegalStateException("This must be called on the looper thread");
+ }
+ }
+
+ /**
+ * Add a service to advertise.
+ * @param id A unique ID for the service.
+ * @param service The service info to advertise.
+ */
+ public void addService(int id, NsdServiceInfo service) {
+ checkThread();
+ if (mRegistrations.get(id) != null) {
+ Log.e(TAG, "Adding duplicate registration for " + service);
+ // TODO (b/264986328): add a more specific error code
+ mCb.onRegisterServiceFailed(id, NsdManager.FAILURE_INTERNAL_ERROR);
+ return;
+ }
+
+ if (DBG) {
+ Log.i(TAG, "Adding service " + service + " with ID " + id);
+ }
+
+ final Network network = service.getNetwork();
+ final Registration registration = new Registration(service);
+ final BiPredicate<Network, InterfaceAdvertiserRequest> checkConflictFilter;
+ if (network == null) {
+ // If registering on all networks, no advertiser must have conflicts
+ checkConflictFilter = (net, adv) -> true;
+ } else {
+ // If registering on one network, the matching network advertiser and the one for all
+ // networks must not have conflicts
+ checkConflictFilter = (net, adv) -> net == null || network.equals(net);
+ }
+
+ updateRegistrationUntilNoConflict(checkConflictFilter, registration);
+
+ InterfaceAdvertiserRequest advertiser = mAdvertiserRequests.get(network);
+ if (advertiser == null) {
+ advertiser = new InterfaceAdvertiserRequest(network);
+ mAdvertiserRequests.put(network, advertiser);
+ }
+ advertiser.addService(id, registration);
+ mRegistrations.put(id, registration);
+ }
+
+ /**
+ * Remove a previously added service.
+ * @param id ID used when registering.
+ */
+ public void removeService(int id) {
+ checkThread();
+ if (!mRegistrations.contains(id)) return;
+ if (DBG) {
+ Log.i(TAG, "Removing service with ID " + id);
+ }
+ for (int i = mAdvertiserRequests.size() - 1; i >= 0; i--) {
+ final InterfaceAdvertiserRequest advertiser = mAdvertiserRequests.valueAt(i);
+ advertiser.removeService(id);
+ }
+ mRegistrations.remove(id);
+ }
+
+ private static <K, V> boolean any(@NonNull ArrayMap<K, V> map,
+ @NonNull BiPredicate<K, V> predicate) {
+ for (int i = 0; i < map.size(); i++) {
+ if (predicate.test(map.keyAt(i), map.valueAt(i))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void forAllAdvertisers(@NonNull Consumer<MdnsInterfaceAdvertiser> consumer) {
+ any(mAllAdvertisers, (socket, advertiser) -> {
+ consumer.accept(advertiser);
+ return false;
+ });
+ }
+}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsAnnouncer.java b/service-t/src/com/android/server/mdns/MdnsAnnouncer.java
similarity index 61%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsAnnouncer.java
rename to service-t/src/com/android/server/mdns/MdnsAnnouncer.java
index 91e08a8..27fc945 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsAnnouncer.java
+++ b/service-t/src/com/android/server/mdns/MdnsAnnouncer.java
@@ -22,52 +22,58 @@
import com.android.internal.annotations.VisibleForTesting;
-import java.net.SocketAddress;
import java.util.Collections;
import java.util.List;
-import java.util.function.Supplier;
/**
* Sends mDns announcements when a service registration changes and at regular intervals.
*
* This allows maintaining other hosts' caches up-to-date. See RFC6762 8.3.
*/
-public class MdnsAnnouncer extends MdnsPacketRepeater<MdnsAnnouncer.AnnouncementInfo> {
+public class MdnsAnnouncer extends MdnsPacketRepeater<MdnsAnnouncer.BaseAnnouncementInfo> {
private static final long ANNOUNCEMENT_INITIAL_DELAY_MS = 1000L;
@VisibleForTesting
static final int ANNOUNCEMENT_COUNT = 8;
+ // Matches delay and GoodbyeCount used by the legacy implementation
+ private static final long EXIT_DELAY_MS = 2000L;
+ private static final int EXIT_COUNT = 3;
+
@NonNull
private final String mLogTag;
- static class AnnouncementInfo implements MdnsPacketRepeater.Request {
+ /** Base class for announcement requests to send with {@link MdnsAnnouncer}. */
+ public abstract static class BaseAnnouncementInfo implements MdnsPacketRepeater.Request {
+ private final int mServiceId;
@NonNull
private final MdnsPacket mPacket;
- @NonNull
- private final Supplier<Iterable<SocketAddress>> mDestinationsSupplier;
- AnnouncementInfo(List<MdnsRecord> announcedRecords, List<MdnsRecord> additionalRecords,
- Supplier<Iterable<SocketAddress>> destinationsSupplier) {
- // Records to announce (as answers)
- // Records to place in the "Additional records", with NSEC negative responses
- // to mark records that have been verified unique
+ protected BaseAnnouncementInfo(int serviceId, @NonNull List<MdnsRecord> announcedRecords,
+ @NonNull List<MdnsRecord> additionalRecords) {
+ mServiceId = serviceId;
final int flags = 0x8400; // Response, authoritative (rfc6762 18.4)
mPacket = new MdnsPacket(flags,
Collections.emptyList() /* questions */,
announcedRecords,
Collections.emptyList() /* authorityRecords */,
additionalRecords);
- mDestinationsSupplier = destinationsSupplier;
+ }
+
+ public int getServiceId() {
+ return mServiceId;
}
@Override
public MdnsPacket getPacket(int index) {
return mPacket;
}
+ }
- @Override
- public Iterable<SocketAddress> getDestinations(int index) {
- return mDestinationsSupplier.get();
+ /** Announcement request to send with {@link MdnsAnnouncer}. */
+ public static class AnnouncementInfo extends BaseAnnouncementInfo {
+ AnnouncementInfo(int serviceId, List<MdnsRecord> announcedRecords,
+ List<MdnsRecord> additionalRecords) {
+ super(serviceId, announcedRecords, additionalRecords);
}
@Override
@@ -82,9 +88,26 @@
}
}
+ /** Service exit announcement request to send with {@link MdnsAnnouncer}. */
+ public static class ExitAnnouncementInfo extends BaseAnnouncementInfo {
+ ExitAnnouncementInfo(int serviceId, List<MdnsRecord> announcedRecords) {
+ super(serviceId, announcedRecords, Collections.emptyList() /* additionalRecords */);
+ }
+
+ @Override
+ public long getDelayMs(int nextIndex) {
+ return EXIT_DELAY_MS;
+ }
+
+ @Override
+ public int getNumSends() {
+ return EXIT_COUNT;
+ }
+ }
+
public MdnsAnnouncer(@NonNull String interfaceTag, @NonNull Looper looper,
@NonNull MdnsReplySender replySender,
- @Nullable PacketRepeaterCallback<AnnouncementInfo> cb) {
+ @Nullable PacketRepeaterCallback<BaseAnnouncementInfo> cb) {
super(looper, replySender, cb);
mLogTag = MdnsAnnouncer.class.getSimpleName() + "/" + interfaceTag;
}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsAnyRecord.java b/service-t/src/com/android/server/mdns/MdnsAnyRecord.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsAnyRecord.java
rename to service-t/src/com/android/server/mdns/MdnsAnyRecord.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsConfigs.java b/service-t/src/com/android/server/mdns/MdnsConfigs.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsConfigs.java
rename to service-t/src/com/android/server/mdns/MdnsConfigs.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsConstants.java b/service-t/src/com/android/server/mdns/MdnsConstants.java
similarity index 97%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsConstants.java
rename to service-t/src/com/android/server/mdns/MdnsConstants.java
index 396be5f..f0e1717 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsConstants.java
+++ b/service-t/src/com/android/server/mdns/MdnsConstants.java
@@ -37,6 +37,7 @@
public static final int FLAGS_QUERY = 0x0000;
public static final int FLAGS_RESPONSE_MASK = 0xF80F;
public static final int FLAGS_RESPONSE = 0x8000;
+ public static final int FLAG_TRUNCATED = 0x0200;
public static final int QCLASS_INTERNET = 0x0001;
public static final int QCLASS_UNICAST = 0x8000;
public static final String SUBTYPE_LABEL = "_sub";
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsDiscoveryManager.java b/service-t/src/com/android/server/mdns/MdnsDiscoveryManager.java
similarity index 89%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsDiscoveryManager.java
rename to service-t/src/com/android/server/mdns/MdnsDiscoveryManager.java
index 0f3c23a..cc6b98b 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsDiscoveryManager.java
+++ b/service-t/src/com/android/server/mdns/MdnsDiscoveryManager.java
@@ -34,18 +34,18 @@
* This class keeps tracking the set of registered {@link MdnsServiceBrowserListener} instances, and
* notify them when a mDNS service instance is found, updated, or removed?
*/
-public class MdnsDiscoveryManager implements MdnsSocketClient.Callback {
+public class MdnsDiscoveryManager implements MdnsSocketClientBase.Callback {
private static final String TAG = MdnsDiscoveryManager.class.getSimpleName();
public static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
private static final MdnsLogger LOGGER = new MdnsLogger("MdnsDiscoveryManager");
private final ExecutorProvider executorProvider;
- private final MdnsSocketClient socketClient;
+ private final MdnsSocketClientBase socketClient;
private final Map<String, MdnsServiceTypeClient> serviceTypeClients = new ArrayMap<>();
- public MdnsDiscoveryManager(
- @NonNull ExecutorProvider executorProvider, @NonNull MdnsSocketClient socketClient) {
+ public MdnsDiscoveryManager(@NonNull ExecutorProvider executorProvider,
+ @NonNull MdnsSocketClientBase socketClient) {
this.executorProvider = executorProvider;
this.socketClient = socketClient;
}
@@ -76,12 +76,16 @@
return;
}
}
+ // Request the network for discovery.
+ socketClient.notifyNetworkRequested(listener, searchOptions.getNetwork());
+
// All listeners of the same service types shares the same MdnsServiceTypeClient.
MdnsServiceTypeClient serviceTypeClient = serviceTypeClients.get(serviceType);
if (serviceTypeClient == null) {
serviceTypeClient = createServiceTypeClient(serviceType);
serviceTypeClients.put(serviceType, serviceTypeClient);
}
+ // TODO(b/264634275): Wait for a socket to be created before sending packets.
serviceTypeClient.startSendAndReceive(listener, searchOptions);
}
@@ -96,20 +100,22 @@
public synchronized void unregisterListener(
@NonNull String serviceType, @NonNull MdnsServiceBrowserListener listener) {
LOGGER.log("Unregistering listener for service type: %s", serviceType);
+ if (DBG) Log.d(TAG, "Unregistering listener for serviceType:" + serviceType);
MdnsServiceTypeClient serviceTypeClient = serviceTypeClients.get(serviceType);
if (serviceTypeClient == null) {
return;
}
if (serviceTypeClient.stopSendAndReceive(listener)) {
// No listener is registered for the service type anymore, remove it from the list of
- // the
- // service type clients.
+ // the service type clients.
serviceTypeClients.remove(serviceType);
if (serviceTypeClients.isEmpty()) {
// No discovery request. Stops the socket client.
socketClient.stopDiscovery();
}
}
+ // Unrequested the network.
+ socketClient.notifyNetworkUnrequested(listener);
}
@Override
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsInetAddressRecord.java b/service-t/src/com/android/server/mdns/MdnsInetAddressRecord.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsInetAddressRecord.java
rename to service-t/src/com/android/server/mdns/MdnsInetAddressRecord.java
diff --git a/service-t/src/com/android/server/mdns/MdnsInterfaceAdvertiser.java b/service-t/src/com/android/server/mdns/MdnsInterfaceAdvertiser.java
new file mode 100644
index 0000000..c616e01
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MdnsInterfaceAdvertiser.java
@@ -0,0 +1,344 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.net.LinkAddress;
+import android.net.nsd.NsdServiceInfo;
+import android.os.Handler;
+import android.os.Looper;
+import android.util.Log;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.net.module.util.HexDump;
+import com.android.server.connectivity.mdns.MdnsAnnouncer.BaseAnnouncementInfo;
+import com.android.server.connectivity.mdns.MdnsPacketRepeater.PacketRepeaterCallback;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.List;
+
+/**
+ * A class that handles advertising services on a {@link MdnsInterfaceSocket} tied to an interface.
+ */
+public class MdnsInterfaceAdvertiser implements MulticastPacketReader.PacketHandler {
+ private static final boolean DBG = MdnsAdvertiser.DBG;
+ @VisibleForTesting
+ public static final long EXIT_ANNOUNCEMENT_DELAY_MS = 100L;
+ @NonNull
+ private final String mTag;
+ @NonNull
+ private final ProbingCallback mProbingCallback = new ProbingCallback();
+ @NonNull
+ private final AnnouncingCallback mAnnouncingCallback = new AnnouncingCallback();
+ @NonNull
+ private final MdnsRecordRepository mRecordRepository;
+ @NonNull
+ private final Callback mCb;
+ // Callbacks are on the same looper thread, but posted to the next handler loop
+ @NonNull
+ private final Handler mCbHandler;
+ @NonNull
+ private final MdnsInterfaceSocket mSocket;
+ @NonNull
+ private final MdnsAnnouncer mAnnouncer;
+ @NonNull
+ private final MdnsProber mProber;
+ @NonNull
+ private final MdnsReplySender mReplySender;
+
+ /**
+ * Callbacks called by {@link MdnsInterfaceAdvertiser} to report status updates.
+ */
+ interface Callback {
+ /**
+ * Called by the advertiser after it successfully registered a service, after probing.
+ */
+ void onRegisterServiceSucceeded(@NonNull MdnsInterfaceAdvertiser advertiser, int serviceId);
+
+ /**
+ * Called by the advertiser when a conflict was found, during or after probing.
+ *
+ * If a conflict is found during probing, the {@link #renameServiceForConflict} must be
+ * called to restart probing and attempt registration with a different name.
+ */
+ void onServiceConflict(@NonNull MdnsInterfaceAdvertiser advertiser, int serviceId);
+
+ /**
+ * Called by the advertiser when it destroyed itself.
+ *
+ * This can happen after a call to {@link #destroyNow()}, or after all services were
+ * unregistered and the advertiser finished sending exit announcements.
+ */
+ void onDestroyed(@NonNull MdnsInterfaceSocket socket);
+ }
+
+ /**
+ * Callbacks from {@link MdnsProber}.
+ */
+ private class ProbingCallback implements
+ PacketRepeaterCallback<MdnsProber.ProbingInfo> {
+ @Override
+ public void onFinished(MdnsProber.ProbingInfo info) {
+ final MdnsAnnouncer.AnnouncementInfo announcementInfo;
+ if (DBG) {
+ Log.v(mTag, "Probing finished for service " + info.getServiceId());
+ }
+ mCbHandler.post(() -> mCb.onRegisterServiceSucceeded(
+ MdnsInterfaceAdvertiser.this, info.getServiceId()));
+ try {
+ announcementInfo = mRecordRepository.onProbingSucceeded(info);
+ } catch (IOException e) {
+ Log.e(mTag, "Error building announcements", e);
+ return;
+ }
+
+ mAnnouncer.startSending(info.getServiceId(), announcementInfo,
+ 0L /* initialDelayMs */);
+ }
+ }
+
+ /**
+ * Callbacks from {@link MdnsAnnouncer}.
+ */
+ private class AnnouncingCallback implements PacketRepeaterCallback<BaseAnnouncementInfo> {
+ @Override
+ public void onSent(int index, @NonNull BaseAnnouncementInfo info) {
+ mRecordRepository.onAdvertisementSent(info.getServiceId());
+ }
+
+ @Override
+ public void onFinished(@NonNull BaseAnnouncementInfo info) {
+ if (info instanceof MdnsAnnouncer.ExitAnnouncementInfo) {
+ mRecordRepository.removeService(info.getServiceId());
+
+ if (mRecordRepository.getServicesCount() == 0) {
+ destroyNow();
+ }
+ }
+ }
+ }
+
+ /**
+ * Dependencies for {@link MdnsInterfaceAdvertiser}, useful for testing.
+ */
+ @VisibleForTesting
+ public static class Dependencies {
+ /** @see MdnsRecordRepository */
+ @NonNull
+ public MdnsRecordRepository makeRecordRepository(@NonNull Looper looper) {
+ return new MdnsRecordRepository(looper);
+ }
+
+ /** @see MdnsReplySender */
+ @NonNull
+ public MdnsReplySender makeReplySender(@NonNull String interfaceTag, @NonNull Looper looper,
+ @NonNull MdnsInterfaceSocket socket, @NonNull byte[] packetCreationBuffer) {
+ return new MdnsReplySender(interfaceTag, looper, socket, packetCreationBuffer);
+ }
+
+ /** @see MdnsAnnouncer */
+ public MdnsAnnouncer makeMdnsAnnouncer(@NonNull String interfaceTag, @NonNull Looper looper,
+ @NonNull MdnsReplySender replySender,
+ @Nullable PacketRepeaterCallback<MdnsAnnouncer.BaseAnnouncementInfo> cb) {
+ return new MdnsAnnouncer(interfaceTag, looper, replySender, cb);
+ }
+
+ /** @see MdnsProber */
+ public MdnsProber makeMdnsProber(@NonNull String interfaceTag, @NonNull Looper looper,
+ @NonNull MdnsReplySender replySender,
+ @NonNull PacketRepeaterCallback<MdnsProber.ProbingInfo> cb) {
+ return new MdnsProber(interfaceTag, looper, replySender, cb);
+ }
+ }
+
+ public MdnsInterfaceAdvertiser(@NonNull String logTag,
+ @NonNull MdnsInterfaceSocket socket, @NonNull List<LinkAddress> initialAddresses,
+ @NonNull Looper looper, @NonNull byte[] packetCreationBuffer, @NonNull Callback cb) {
+ this(logTag, socket, initialAddresses, looper, packetCreationBuffer, cb,
+ new Dependencies());
+ }
+
+ public MdnsInterfaceAdvertiser(@NonNull String logTag,
+ @NonNull MdnsInterfaceSocket socket, @NonNull List<LinkAddress> initialAddresses,
+ @NonNull Looper looper, @NonNull byte[] packetCreationBuffer, @NonNull Callback cb,
+ @NonNull Dependencies deps) {
+ mTag = MdnsInterfaceAdvertiser.class.getSimpleName() + "/" + logTag;
+ mRecordRepository = deps.makeRecordRepository(looper);
+ mRecordRepository.updateAddresses(initialAddresses);
+ mSocket = socket;
+ mCb = cb;
+ mCbHandler = new Handler(looper);
+ mReplySender = deps.makeReplySender(logTag, looper, socket, packetCreationBuffer);
+ mAnnouncer = deps.makeMdnsAnnouncer(logTag, looper, mReplySender,
+ mAnnouncingCallback);
+ mProber = deps.makeMdnsProber(logTag, looper, mReplySender, mProbingCallback);
+ }
+
+ /**
+ * Start the advertiser.
+ *
+ * The advertiser will stop itself when all services are removed and exit announcements sent,
+ * notifying via {@link Callback#onDestroyed}. This can also be triggered manually via
+ * {@link #destroyNow()}.
+ */
+ public void start() {
+ mSocket.addPacketHandler(this);
+ }
+
+ /**
+ * Start advertising a service.
+ *
+ * @throws NameConflictException There is already a service being advertised with that name.
+ */
+ public void addService(int id, NsdServiceInfo service) throws NameConflictException {
+ final int replacedExitingService = mRecordRepository.addService(id, service);
+ // Cancel announcements for the existing service. This only happens for exiting services
+ // (so cancelling exiting announcements), as per RecordRepository.addService.
+ if (replacedExitingService >= 0) {
+ if (DBG) {
+ Log.d(mTag, "Service " + replacedExitingService
+ + " getting re-added, cancelling exit announcements");
+ }
+ mAnnouncer.stop(replacedExitingService);
+ }
+ mProber.startProbing(mRecordRepository.setServiceProbing(id));
+ }
+
+ /**
+ * Stop advertising a service.
+ *
+ * This will trigger exit announcements for the service.
+ */
+ public void removeService(int id) {
+ if (!mRecordRepository.hasActiveService(id)) return;
+ mProber.stop(id);
+ mAnnouncer.stop(id);
+ final MdnsAnnouncer.ExitAnnouncementInfo exitInfo = mRecordRepository.exitService(id);
+ if (exitInfo != null) {
+ // This effectively schedules destroyNow(), as it is to be called when the exit
+ // announcement finishes if there is no service left.
+ // A non-zero exit announcement delay follows legacy mdnsresponder behavior, and is
+ // also useful to ensure that when a host receives the exit announcement, the service
+ // has been unregistered on all interfaces; so an announcement sent from interface A
+ // that was already in-flight while unregistering won't be received after the exit on
+ // interface B.
+ mAnnouncer.startSending(id, exitInfo, EXIT_ANNOUNCEMENT_DELAY_MS);
+ } else {
+ // No exit announcement necessary: remove the service immediately.
+ mRecordRepository.removeService(id);
+ if (mRecordRepository.getServicesCount() == 0) {
+ destroyNow();
+ }
+ }
+ }
+
+ /**
+ * Update interface addresses used to advertise.
+ *
+ * This causes new address records to be announced.
+ */
+ public void updateAddresses(@NonNull List<LinkAddress> newAddresses) {
+ mRecordRepository.updateAddresses(newAddresses);
+ // TODO: restart advertising, but figure out what exit messages need to be sent for the
+ // previous addresses
+ }
+
+ /**
+ * Destroy the advertiser immediately, not sending any exit announcement.
+ *
+ * <p>Useful when the underlying network went away. This will trigger an onDestroyed callback.
+ */
+ public void destroyNow() {
+ for (int serviceId : mRecordRepository.clearServices()) {
+ mProber.stop(serviceId);
+ mAnnouncer.stop(serviceId);
+ }
+ mReplySender.cancelAll();
+ mSocket.removePacketHandler(this);
+ mCbHandler.post(() -> mCb.onDestroyed(mSocket));
+ }
+
+ /**
+ * Reset a service to the probing state due to a conflict found on the network.
+ */
+ public void restartProbingForConflict(int serviceId) {
+ final MdnsProber.ProbingInfo probingInfo = mRecordRepository.setServiceProbing(serviceId);
+ if (probingInfo == null) return;
+
+ mProber.restartForConflict(probingInfo);
+ }
+
+ /**
+ * Rename a service following a conflict found on the network, and restart probing.
+ *
+ * If the service was not registered on this {@link MdnsInterfaceAdvertiser}, this is a no-op.
+ */
+ public void renameServiceForConflict(int serviceId, NsdServiceInfo newInfo) {
+ final MdnsProber.ProbingInfo probingInfo = mRecordRepository.renameServiceForConflict(
+ serviceId, newInfo);
+ if (probingInfo == null) return;
+
+ mProber.restartForConflict(probingInfo);
+ }
+
+ /**
+ * Indicates whether probing is in progress for the given service on this interface.
+ *
+ * Also returns false if the specified service is not registered.
+ */
+ public boolean isProbing(int serviceId) {
+ return mRecordRepository.isProbing(serviceId);
+ }
+
+ @Override
+ public void handlePacket(byte[] recvbuf, int length, InetSocketAddress src) {
+ final MdnsPacket packet;
+ try {
+ packet = MdnsPacket.parse(new MdnsPacketReader(recvbuf, length));
+ } catch (MdnsPacket.ParseException e) {
+ Log.e(mTag, "Error parsing mDNS packet", e);
+ if (DBG) {
+ Log.v(
+ mTag, "Packet: " + HexDump.toHexString(recvbuf, 0, length));
+ }
+ return;
+ }
+
+ if (DBG) {
+ Log.v(mTag,
+ "Parsed packet with " + packet.questions.size() + " questions, "
+ + packet.answers.size() + " answers, "
+ + packet.authorityRecords.size() + " authority, "
+ + packet.additionalRecords.size() + " additional from " + src);
+ }
+
+ for (int conflictServiceId : mRecordRepository.getConflictingServices(packet)) {
+ mCbHandler.post(() -> mCb.onServiceConflict(this, conflictServiceId));
+ }
+
+ // Even in case of conflict, add replies for other services. But in general conflicts would
+ // happen when the incoming packet has answer records (not a question), so there will be no
+ // answer. One exception is simultaneous probe tiebreaking (rfc6762 8.2), in which case the
+ // conflicting service is still probing and won't reply either.
+ final MdnsRecordRepository.ReplyInfo answers = mRecordRepository.getReply(packet, src);
+
+ if (answers == null) return;
+ mReplySender.queueReply(answers);
+ }
+}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsInterfaceSocket.java b/service-t/src/com/android/server/mdns/MdnsInterfaceSocket.java
similarity index 66%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsInterfaceSocket.java
rename to service-t/src/com/android/server/mdns/MdnsInterfaceSocket.java
index 6090415..119c7a8 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsInterfaceSocket.java
+++ b/service-t/src/com/android/server/mdns/MdnsInterfaceSocket.java
@@ -22,10 +22,15 @@
import android.annotation.NonNull;
import android.net.LinkAddress;
import android.net.util.SocketUtils;
+import android.os.Handler;
+import android.os.Looper;
import android.os.ParcelFileDescriptor;
import android.system.ErrnoException;
+import android.system.Os;
+import android.system.OsConstants;
import android.util.Log;
+import java.io.FileDescriptor;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
@@ -41,15 +46,19 @@
* otherwise.
*
* @see MulticastSocket for javadoc of each public method.
+ * @see MulticastSocket for javadoc of each public method.
*/
public class MdnsInterfaceSocket {
private static final String TAG = MdnsInterfaceSocket.class.getSimpleName();
@NonNull private final MulticastSocket mMulticastSocket;
@NonNull private final NetworkInterface mNetworkInterface;
+ @NonNull private final MulticastPacketReader mPacketReader;
+ @NonNull private final ParcelFileDescriptor mFileDescriptor;
private boolean mJoinedIpv4 = false;
private boolean mJoinedIpv6 = false;
- public MdnsInterfaceSocket(@NonNull NetworkInterface networkInterface, int port)
+ public MdnsInterfaceSocket(@NonNull NetworkInterface networkInterface, int port,
+ @NonNull Looper looper, @NonNull byte[] packetReadBuffer)
throws IOException {
mNetworkInterface = networkInterface;
mMulticastSocket = new MulticastSocket(port);
@@ -58,11 +67,19 @@
mMulticastSocket.setNetworkInterface(networkInterface);
// Bind socket to the interface for receiving from that interface only.
- try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(mMulticastSocket)) {
- SocketUtils.bindSocketToInterface(pfd.getFileDescriptor(), mNetworkInterface.getName());
+ mFileDescriptor = ParcelFileDescriptor.fromDatagramSocket(mMulticastSocket);
+ try {
+ final FileDescriptor fd = mFileDescriptor.getFileDescriptor();
+ final int flags = Os.fcntlInt(fd, OsConstants.F_GETFL, 0);
+ Os.fcntlInt(fd, OsConstants.F_SETFL, flags | OsConstants.SOCK_NONBLOCK);
+ SocketUtils.bindSocketToInterface(fd, mNetworkInterface.getName());
} catch (ErrnoException e) {
throw new IOException("Error setting socket options", e);
}
+
+ mPacketReader = new MulticastPacketReader(networkInterface.getName(), mFileDescriptor,
+ new Handler(looper), packetReadBuffer);
+ mPacketReader.start();
}
/**
@@ -74,23 +91,14 @@
mMulticastSocket.send(packet);
}
- /**
- * Receives a datagram packet from this socket.
- *
- * <p>This method could be used on any thread.
- */
- public void receive(@NonNull DatagramPacket packet) throws IOException {
- mMulticastSocket.receive(packet);
- }
-
- private boolean hasIpv4Address(List<LinkAddress> addresses) {
+ private static boolean hasIpv4Address(@NonNull List<LinkAddress> addresses) {
for (LinkAddress address : addresses) {
if (address.isIpv4()) return true;
}
return false;
}
- private boolean hasIpv6Address(List<LinkAddress> addresses) {
+ private static boolean hasIpv6Address(@NonNull List<LinkAddress> addresses) {
for (LinkAddress address : addresses) {
if (address.isIpv6()) return true;
}
@@ -103,7 +111,7 @@
maybeJoinIpv6(addresses);
}
- private boolean joinGroup(InetSocketAddress multicastAddress) {
+ private boolean joinGroup(@NonNull InetSocketAddress multicastAddress) {
try {
mMulticastSocket.joinGroup(multicastAddress, mNetworkInterface);
return true;
@@ -114,7 +122,7 @@
}
}
- private void maybeJoinIpv4(List<LinkAddress> addresses) {
+ private void maybeJoinIpv4(@NonNull List<LinkAddress> addresses) {
final boolean hasAddr = hasIpv4Address(addresses);
if (!mJoinedIpv4 && hasAddr) {
mJoinedIpv4 = joinGroup(MULTICAST_IPV4_ADDRESS);
@@ -124,7 +132,7 @@
}
}
- private void maybeJoinIpv6(List<LinkAddress> addresses) {
+ private void maybeJoinIpv6(@NonNull List<LinkAddress> addresses) {
final boolean hasAddr = hasIpv6Address(addresses);
if (!mJoinedIpv6 && hasAddr) {
mJoinedIpv6 = joinGroup(MULTICAST_IPV6_ADDRESS);
@@ -134,33 +142,40 @@
}
}
- /*** Destroy this socket by leaving all joined multicast groups and closing this socket. */
+ /*** Destroy the socket */
public void destroy() {
- if (mJoinedIpv4) {
- try {
- mMulticastSocket.leaveGroup(MULTICAST_IPV4_ADDRESS, mNetworkInterface);
- } catch (IOException e) {
- Log.e(TAG, "Error leaving IPv4 group for " + mNetworkInterface, e);
- }
- }
- if (mJoinedIpv6) {
- try {
- mMulticastSocket.leaveGroup(MULTICAST_IPV6_ADDRESS, mNetworkInterface);
- } catch (IOException e) {
- Log.e(TAG, "Error leaving IPv4 group for " + mNetworkInterface, e);
- }
+ mPacketReader.stop();
+ try {
+ mFileDescriptor.close();
+ } catch (IOException e) {
+ Log.e(TAG, "Close file descriptor failed.");
}
mMulticastSocket.close();
}
/**
- * Returns the index of the network interface that this socket is bound to. If the interface
- * cannot be determined, returns -1.
+ * Add a handler to receive callbacks when reads the packet from socket. If the handler is
+ * already set, this is a no-op.
+ */
+ public void addPacketHandler(@NonNull MulticastPacketReader.PacketHandler handler) {
+ mPacketReader.addPacketHandler(handler);
+ }
+
+ /**
+ * Remove a handler added via {@link #addPacketHandler}. If the handler is not present, this is
+ * a no-op.
+ */
+ public void removePacketHandler(@NonNull MulticastPacketReader.PacketHandler handler) {
+ mPacketReader.removePacketHandler(handler);
+ }
+
+ /**
+ * Returns the network interface that this socket is bound to.
*
* <p>This method could be used on any thread.
*/
- public int getInterfaceIndex() {
- return mNetworkInterface.getIndex();
+ public NetworkInterface getInterface() {
+ return mNetworkInterface;
}
/*** Returns whether this socket has joined IPv4 group */
diff --git a/service-t/src/com/android/server/mdns/MdnsMultinetworkSocketClient.java b/service-t/src/com/android/server/mdns/MdnsMultinetworkSocketClient.java
new file mode 100644
index 0000000..d959065
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MdnsMultinetworkSocketClient.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import static com.android.server.connectivity.mdns.MdnsSocketProvider.ensureRunningOnHandlerThread;
+import static com.android.server.connectivity.mdns.MdnsSocketProvider.isNetworkMatched;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.net.LinkAddress;
+import android.net.Network;
+import android.os.Handler;
+import android.os.Looper;
+import android.util.ArrayMap;
+import android.util.Log;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The {@link MdnsMultinetworkSocketClient} manages the multinetwork socket for mDns
+ *
+ * * <p>This class is not thread safe.
+ */
+public class MdnsMultinetworkSocketClient implements MdnsSocketClientBase {
+ private static final String TAG = MdnsMultinetworkSocketClient.class.getSimpleName();
+ private static final boolean DBG = MdnsDiscoveryManager.DBG;
+
+ @NonNull private final Handler mHandler;
+ @NonNull private final MdnsSocketProvider mSocketProvider;
+ @NonNull private final MdnsResponseDecoder mResponseDecoder;
+
+ private final Map<MdnsServiceBrowserListener, InterfaceSocketCallback> mRequestedNetworks =
+ new ArrayMap<>();
+ private final ArrayMap<MdnsInterfaceSocket, Network> mActiveNetworkSockets = new ArrayMap<>();
+ private final ArrayMap<MdnsInterfaceSocket, ReadPacketHandler> mSocketPacketHandlers =
+ new ArrayMap<>();
+ private MdnsSocketClientBase.Callback mCallback = null;
+ private int mReceivedPacketNumber = 0;
+
+ public MdnsMultinetworkSocketClient(@NonNull Looper looper,
+ @NonNull MdnsSocketProvider provider) {
+ mHandler = new Handler(looper);
+ mSocketProvider = provider;
+ mResponseDecoder = new MdnsResponseDecoder(
+ new MdnsResponseDecoder.Clock(), null /* serviceType */);
+ }
+
+ private class InterfaceSocketCallback implements MdnsSocketProvider.SocketCallback {
+ @Override
+ public void onSocketCreated(@NonNull Network network,
+ @NonNull MdnsInterfaceSocket socket, @NonNull List<LinkAddress> addresses) {
+ // The socket may be already created by other request before, try to get the stored
+ // ReadPacketHandler.
+ ReadPacketHandler handler = mSocketPacketHandlers.get(socket);
+ if (handler == null) {
+ // First request to create this socket. Initial a ReadPacketHandler for this socket.
+ handler = new ReadPacketHandler(network, socket.getInterface().getIndex());
+ mSocketPacketHandlers.put(socket, handler);
+ }
+ socket.addPacketHandler(handler);
+ mActiveNetworkSockets.put(socket, network);
+ }
+
+ @Override
+ public void onInterfaceDestroyed(@NonNull Network network,
+ @NonNull MdnsInterfaceSocket socket) {
+ mSocketPacketHandlers.remove(socket);
+ mActiveNetworkSockets.remove(socket);
+ }
+ }
+
+ private class ReadPacketHandler implements MulticastPacketReader.PacketHandler {
+ private final Network mNetwork;
+ private final int mInterfaceIndex;
+
+ ReadPacketHandler(@NonNull Network network, int interfaceIndex) {
+ mNetwork = network;
+ mInterfaceIndex = interfaceIndex;
+ }
+
+ @Override
+ public void handlePacket(byte[] recvbuf, int length, InetSocketAddress src) {
+ processResponsePacket(recvbuf, length, mInterfaceIndex, mNetwork);
+ }
+ }
+
+ /*** Set callback for receiving mDns response */
+ @Override
+ public void setCallback(@Nullable MdnsSocketClientBase.Callback callback) {
+ ensureRunningOnHandlerThread(mHandler);
+ mCallback = callback;
+ }
+
+ /***
+ * Notify that the given network is requested for mdns discovery / resolution
+ *
+ * @param listener the listener for discovery.
+ * @param network the target network for discovery. Null means discovery on all possible
+ * interfaces.
+ */
+ @Override
+ public void notifyNetworkRequested(@NonNull MdnsServiceBrowserListener listener,
+ @Nullable Network network) {
+ ensureRunningOnHandlerThread(mHandler);
+ InterfaceSocketCallback callback = mRequestedNetworks.get(listener);
+ if (callback != null) {
+ throw new IllegalArgumentException("Can not register duplicated listener");
+ }
+
+ if (DBG) Log.d(TAG, "notifyNetworkRequested: network=" + network);
+ callback = new InterfaceSocketCallback();
+ mRequestedNetworks.put(listener, callback);
+ mSocketProvider.requestSocket(network, callback);
+ }
+
+ /*** Notify that the network is unrequested */
+ @Override
+ public void notifyNetworkUnrequested(@NonNull MdnsServiceBrowserListener listener) {
+ ensureRunningOnHandlerThread(mHandler);
+ final InterfaceSocketCallback callback = mRequestedNetworks.remove(listener);
+ if (callback == null) {
+ Log.e(TAG, "Can not be unrequested with unknown listener=" + listener);
+ return;
+ }
+ mSocketProvider.unrequestSocket(callback);
+ }
+
+ private void sendMdnsPacket(@NonNull DatagramPacket packet, @Nullable Network targetNetwork) {
+ final boolean isIpv6 = ((InetSocketAddress) packet.getSocketAddress()).getAddress()
+ instanceof Inet6Address;
+ final boolean isIpv4 = ((InetSocketAddress) packet.getSocketAddress()).getAddress()
+ instanceof Inet4Address;
+ for (int i = 0; i < mActiveNetworkSockets.size(); i++) {
+ final MdnsInterfaceSocket socket = mActiveNetworkSockets.keyAt(i);
+ final Network network = mActiveNetworkSockets.valueAt(i);
+ // Check ip capability and network before sending packet
+ if (((isIpv6 && socket.hasJoinedIpv6()) || (isIpv4 && socket.hasJoinedIpv4()))
+ && isNetworkMatched(targetNetwork, network)) {
+ try {
+ socket.send(packet);
+ } catch (IOException e) {
+ Log.e(TAG, "Failed to send a mDNS packet.", e);
+ }
+ }
+ }
+ }
+
+ private void processResponsePacket(byte[] recvbuf, int length, int interfaceIndex,
+ @NonNull Network network) {
+ int packetNumber = ++mReceivedPacketNumber;
+
+ final List<MdnsResponse> responses = new ArrayList<>();
+ final int errorCode = mResponseDecoder.decode(
+ recvbuf, length, responses, interfaceIndex, network);
+ if (errorCode == MdnsResponseDecoder.SUCCESS) {
+ for (MdnsResponse response : responses) {
+ if (mCallback != null) {
+ mCallback.onResponseReceived(response);
+ }
+ }
+ } else if (errorCode != MdnsResponseErrorCode.ERROR_NOT_RESPONSE_MESSAGE) {
+ if (mCallback != null) {
+ mCallback.onFailedToParseMdnsResponse(packetNumber, errorCode);
+ }
+ }
+ }
+
+ /** Sends a mDNS request packet that asks for multicast response. */
+ @Override
+ public void sendMulticastPacket(@NonNull DatagramPacket packet) {
+ sendMulticastPacket(packet, null /* network */);
+ }
+
+ /**
+ * Sends a mDNS request packet via given network that asks for multicast response. Null network
+ * means sending packet via all networks.
+ */
+ @Override
+ public void sendMulticastPacket(@NonNull DatagramPacket packet, @Nullable Network network) {
+ mHandler.post(() -> sendMdnsPacket(packet, network));
+ }
+
+ /** Sends a mDNS request packet that asks for unicast response. */
+ @Override
+ public void sendUnicastPacket(@NonNull DatagramPacket packet) {
+ sendUnicastPacket(packet, null /* network */);
+ }
+
+ /**
+ * Sends a mDNS request packet via given network that asks for unicast response. Null network
+ * means sending packet via all networks.
+ */
+ @Override
+ public void sendUnicastPacket(@NonNull DatagramPacket packet, @Nullable Network network) {
+ // TODO: Separate unicast packet.
+ mHandler.post(() -> sendMdnsPacket(packet, network));
+ }
+}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsNsecRecord.java b/service-t/src/com/android/server/mdns/MdnsNsecRecord.java
similarity index 86%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsNsecRecord.java
rename to service-t/src/com/android/server/mdns/MdnsNsecRecord.java
index 06fdd5e..6ec2f99 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsNsecRecord.java
+++ b/service-t/src/com/android/server/mdns/MdnsNsecRecord.java
@@ -17,12 +17,14 @@
package com.android.server.connectivity.mdns;
import android.net.DnsResolver;
+import android.text.TextUtils;
import com.android.net.module.util.CollectionUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Objects;
/**
* A mDNS "NSEC" record, used in particular for negative responses (RFC6762 6.1).
@@ -140,4 +142,30 @@
writer.writeUInt8(bytesInBlock);
writer.writeBytes(bytes);
}
+
+ @Override
+ public String toString() {
+ return "NSEC: NextDomain: " + TextUtils.join(".", mNextDomain)
+ + " Types " + Arrays.toString(mTypes);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(),
+ Arrays.hashCode(mNextDomain), Arrays.hashCode(mTypes));
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) {
+ return true;
+ }
+ if (!(other instanceof MdnsNsecRecord)) {
+ return false;
+ }
+
+ return super.equals(other)
+ && Arrays.equals(mNextDomain, ((MdnsNsecRecord) other).mNextDomain)
+ && Arrays.equals(mTypes, ((MdnsNsecRecord) other).mTypes);
+ }
}
diff --git a/service-t/src/com/android/server/mdns/MdnsPacket.java b/service-t/src/com/android/server/mdns/MdnsPacket.java
new file mode 100644
index 0000000..27002b9
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MdnsPacket.java
@@ -0,0 +1,231 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.util.Log;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A class holding data that can be included in a mDNS packet.
+ */
+public class MdnsPacket {
+ private static final String TAG = MdnsPacket.class.getSimpleName();
+
+ public final int flags;
+ @NonNull
+ public final List<MdnsRecord> questions;
+ @NonNull
+ public final List<MdnsRecord> answers;
+ @NonNull
+ public final List<MdnsRecord> authorityRecords;
+ @NonNull
+ public final List<MdnsRecord> additionalRecords;
+
+ MdnsPacket(int flags,
+ @NonNull List<MdnsRecord> questions,
+ @NonNull List<MdnsRecord> answers,
+ @NonNull List<MdnsRecord> authorityRecords,
+ @NonNull List<MdnsRecord> additionalRecords) {
+ this.flags = flags;
+ this.questions = Collections.unmodifiableList(questions);
+ this.answers = Collections.unmodifiableList(answers);
+ this.authorityRecords = Collections.unmodifiableList(authorityRecords);
+ this.additionalRecords = Collections.unmodifiableList(additionalRecords);
+ }
+
+ /**
+ * Exception thrown on parse errors.
+ */
+ public static class ParseException extends IOException {
+ public final int code;
+
+ public ParseException(int code, @NonNull String message, @Nullable Throwable cause) {
+ super(message, cause);
+ this.code = code;
+ }
+ }
+
+ /**
+ * Parse the packet in the provided {@link MdnsPacketReader}.
+ */
+ @NonNull
+ public static MdnsPacket parse(@NonNull MdnsPacketReader reader) throws ParseException {
+ final int flags;
+ try {
+ reader.readUInt16(); // transaction ID (not used)
+ flags = reader.readUInt16();
+ } catch (EOFException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_END_OF_FILE,
+ "Reached the end of the mDNS response unexpectedly.", e);
+ }
+ return parseRecordsSection(reader, flags);
+ }
+
+ /**
+ * Parse the records section of a mDNS packet in the provided {@link MdnsPacketReader}.
+ *
+ * The records section starts with the questions count, just after the packet flags.
+ */
+ public static MdnsPacket parseRecordsSection(@NonNull MdnsPacketReader reader, int flags)
+ throws ParseException {
+ try {
+ final int numQuestions = reader.readUInt16();
+ final int numAnswers = reader.readUInt16();
+ final int numAuthority = reader.readUInt16();
+ final int numAdditional = reader.readUInt16();
+
+ final ArrayList<MdnsRecord> questions = parseRecords(reader, numQuestions, true);
+ final ArrayList<MdnsRecord> answers = parseRecords(reader, numAnswers, false);
+ final ArrayList<MdnsRecord> authority = parseRecords(reader, numAuthority, false);
+ final ArrayList<MdnsRecord> additional = parseRecords(reader, numAdditional, false);
+
+ return new MdnsPacket(flags, questions, answers, authority, additional);
+ } catch (EOFException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_END_OF_FILE,
+ "Reached the end of the mDNS response unexpectedly.", e);
+ }
+ }
+
+ private static ArrayList<MdnsRecord> parseRecords(@NonNull MdnsPacketReader reader, int count,
+ boolean isQuestion)
+ throws ParseException {
+ final ArrayList<MdnsRecord> records = new ArrayList<>(count);
+ for (int i = 0; i < count; ++i) {
+ final MdnsRecord record = parseRecord(reader, isQuestion);
+ if (record != null) {
+ records.add(record);
+ }
+ }
+ return records;
+ }
+
+ @Nullable
+ private static MdnsRecord parseRecord(@NonNull MdnsPacketReader reader, boolean isQuestion)
+ throws ParseException {
+ String[] name;
+ try {
+ name = reader.readLabels();
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_RECORD_NAME,
+ "Failed to read labels from mDNS response.", e);
+ }
+
+ final int type;
+ try {
+ type = reader.readUInt16();
+ } catch (EOFException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_END_OF_FILE,
+ "Reached the end of the mDNS response unexpectedly.", e);
+ }
+
+ switch (type) {
+ case MdnsRecord.TYPE_A: {
+ try {
+ return new MdnsInetAddressRecord(name, MdnsRecord.TYPE_A, reader, isQuestion);
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_A_RDATA,
+ "Failed to read A record from mDNS response.", e);
+ }
+ }
+
+ case MdnsRecord.TYPE_AAAA: {
+ try {
+ return new MdnsInetAddressRecord(name,
+ MdnsRecord.TYPE_AAAA, reader, isQuestion);
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_AAAA_RDATA,
+ "Failed to read AAAA record from mDNS response.", e);
+ }
+ }
+
+ case MdnsRecord.TYPE_PTR: {
+ try {
+ return new MdnsPointerRecord(name, reader, isQuestion);
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_PTR_RDATA,
+ "Failed to read PTR record from mDNS response.", e);
+ }
+ }
+
+ case MdnsRecord.TYPE_SRV: {
+ try {
+ return new MdnsServiceRecord(name, reader, isQuestion);
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_SRV_RDATA,
+ "Failed to read SRV record from mDNS response.", e);
+ }
+ }
+
+ case MdnsRecord.TYPE_TXT: {
+ try {
+ return new MdnsTextRecord(name, reader, isQuestion);
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_TXT_RDATA,
+ "Failed to read TXT record from mDNS response.", e);
+ }
+ }
+
+ case MdnsRecord.TYPE_NSEC: {
+ try {
+ return new MdnsNsecRecord(name, reader, isQuestion);
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_NSEC_RDATA,
+ "Failed to read NSEC record from mDNS response.", e);
+ }
+ }
+
+ case MdnsRecord.TYPE_ANY: {
+ try {
+ return new MdnsAnyRecord(name, reader);
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_READING_ANY_RDATA,
+ "Failed to read TYPE_ANY record from mDNS response.", e);
+ }
+ }
+
+ default: {
+ try {
+ if (MdnsAdvertiser.DBG) {
+ Log.i(TAG, "Skipping parsing of record of unhandled type " + type);
+ }
+ skipMdnsRecord(reader, isQuestion);
+ return null;
+ } catch (IOException e) {
+ throw new ParseException(MdnsResponseErrorCode.ERROR_SKIPPING_UNKNOWN_RECORD,
+ "Failed to skip mDNS record.", e);
+ }
+ }
+ }
+ }
+
+ private static void skipMdnsRecord(@NonNull MdnsPacketReader reader, boolean isQuestion)
+ throws IOException {
+ reader.skip(2); // Skip the class
+ if (isQuestion) return;
+ // Skip TTL and data
+ reader.skip(4);
+ int dataLength = reader.readUInt16();
+ reader.skip(dataLength);
+ }
+}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsPacketReader.java b/service-t/src/com/android/server/mdns/MdnsPacketReader.java
similarity index 97%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsPacketReader.java
rename to service-t/src/com/android/server/mdns/MdnsPacketReader.java
index 856a2cd..aa38844 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsPacketReader.java
+++ b/service-t/src/com/android/server/mdns/MdnsPacketReader.java
@@ -38,8 +38,13 @@
/** Constructs a reader for the given packet. */
public MdnsPacketReader(DatagramPacket packet) {
- buf = packet.getData();
- count = packet.getLength();
+ this(packet.getData(), packet.getLength());
+ }
+
+ /** Constructs a reader for the given packet. */
+ public MdnsPacketReader(byte[] buffer, int length) {
+ buf = buffer;
+ count = length;
pos = 0;
limit = -1;
labelDictionary = new SparseArray<>(16);
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsPacketRepeater.java b/service-t/src/com/android/server/mdns/MdnsPacketRepeater.java
similarity index 88%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsPacketRepeater.java
rename to service-t/src/com/android/server/mdns/MdnsPacketRepeater.java
index 015dbd8..4c385da 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsPacketRepeater.java
+++ b/service-t/src/com/android/server/mdns/MdnsPacketRepeater.java
@@ -16,6 +16,9 @@
package com.android.server.connectivity.mdns;
+import static com.android.server.connectivity.mdns.MdnsRecordRepository.IPV4_ADDR;
+import static com.android.server.connectivity.mdns.MdnsRecordRepository.IPV6_ADDR;
+
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Handler;
@@ -24,7 +27,7 @@
import android.util.Log;
import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
/**
* A class used to send several packets at given time intervals.
@@ -32,6 +35,10 @@
*/
public abstract class MdnsPacketRepeater<T extends MdnsPacketRepeater.Request> {
private static final boolean DBG = MdnsAdvertiser.DBG;
+ private static final InetSocketAddress[] ALL_ADDRS = new InetSocketAddress[] {
+ IPV4_ADDR, IPV6_ADDR
+ };
+
@NonNull
private final MdnsReplySender mReplySender;
@NonNull
@@ -70,12 +77,6 @@
MdnsPacket getPacket(int index);
/**
- * Get a set of destinations for the packet for one iteration.
- */
- @NonNull
- Iterable<SocketAddress> getDestinations(int index);
-
- /**
* Get the delay in milliseconds until the next packet transmission.
*/
long getDelayMs(int nextIndex);
@@ -110,12 +111,13 @@
}
final MdnsPacket packet = request.getPacket(index);
- final Iterable<SocketAddress> destinations = request.getDestinations(index);
if (DBG) {
- Log.v(getTag(), "Sending packets to " + destinations + " for iteration "
- + index + " out of " + request.getNumSends());
+ Log.v(getTag(), "Sending packets for iteration " + index + " out of "
+ + request.getNumSends() + " for ID " + msg.what);
}
- for (SocketAddress destination : destinations) {
+ // Send to both v4 and v6 addresses; the reply sender will take care of ignoring the
+ // send when the socket has not joined the relevant group.
+ for (InetSocketAddress destination : ALL_ADDRS) {
try {
mReplySender.sendNow(packet, destination);
} catch (IOException e) {
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsPacketWriter.java b/service-t/src/com/android/server/mdns/MdnsPacketWriter.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsPacketWriter.java
rename to service-t/src/com/android/server/mdns/MdnsPacketWriter.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsPointerRecord.java b/service-t/src/com/android/server/mdns/MdnsPointerRecord.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsPointerRecord.java
rename to service-t/src/com/android/server/mdns/MdnsPointerRecord.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsProber.java b/service-t/src/com/android/server/mdns/MdnsProber.java
similarity index 84%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsProber.java
rename to service-t/src/com/android/server/mdns/MdnsProber.java
index db7049e..669b323 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsProber.java
+++ b/service-t/src/com/android/server/mdns/MdnsProber.java
@@ -22,12 +22,10 @@
import com.android.internal.annotations.VisibleForTesting;
import com.android.net.module.util.CollectionUtils;
-import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-import java.util.function.Supplier;
/**
* Sends mDns probe requests to verify service records are unique on the network.
@@ -35,37 +33,32 @@
* TODO: implement receiving replies and handling conflicts.
*/
public class MdnsProber extends MdnsPacketRepeater<MdnsProber.ProbingInfo> {
+ private static final long CONFLICT_RETRY_DELAY_MS = 5_000L;
@NonNull
private final String mLogTag;
public MdnsProber(@NonNull String interfaceTag, @NonNull Looper looper,
@NonNull MdnsReplySender replySender,
@NonNull PacketRepeaterCallback<ProbingInfo> cb) {
- // 3 packets as per https://datatracker.ietf.org/doc/html/rfc6762#section-8.1
super(looper, replySender, cb);
mLogTag = MdnsProber.class.getSimpleName() + "/" + interfaceTag;
}
- static class ProbingInfo implements Request {
+ /** Probing request to send with {@link MdnsProber}. */
+ public static class ProbingInfo implements Request {
private final int mServiceId;
@NonNull
private final MdnsPacket mPacket;
- @NonNull
- private final Supplier<Iterable<SocketAddress>> mDestinationsSupplier;
/**
* Create a new ProbingInfo
* @param serviceId Service to probe for.
* @param probeRecords Records to be probed for uniqueness.
- * @param destinationsSupplier Supplier for the probe destinations. Will be called on the
- * probe handler thread for each probe.
*/
- ProbingInfo(int serviceId, @NonNull List<MdnsRecord> probeRecords,
- @NonNull Supplier<Iterable<SocketAddress>> destinationsSupplier) {
+ ProbingInfo(int serviceId, @NonNull List<MdnsRecord> probeRecords) {
mServiceId = serviceId;
mPacket = makePacket(probeRecords);
- mDestinationsSupplier = destinationsSupplier;
}
public int getServiceId() {
@@ -78,12 +71,6 @@
return mPacket;
}
- @NonNull
- @Override
- public Iterable<SocketAddress> getDestinations(int index) {
- return mDestinationsSupplier.get();
- }
-
@Override
public long getDelayMs(int nextIndex) {
// As per https://datatracker.ietf.org/doc/html/rfc6762#section-8.1
@@ -153,4 +140,18 @@
private void startProbing(@NonNull ProbingInfo info, long delay) {
startSending(info.getServiceId(), info, delay);
}
+
+ /**
+ * Restart probing with new service info as a conflict was found.
+ */
+ public void restartForConflict(@NonNull ProbingInfo newInfo) {
+ stop(newInfo.getServiceId());
+
+ /* RFC 6762 8.1: "If fifteen conflicts occur within any ten-second period, then the host
+ MUST wait at least five seconds before each successive additional probe attempt. [...]
+ For very simple devices, a valid way to comply with this requirement is to always wait
+ five seconds after any failed probe attempt before trying again. */
+ // TODO: count 15 conflicts in 10s instead of waiting for 5s every time
+ startProbing(newInfo, CONFLICT_RETRY_DELAY_MS);
+ }
}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsRecord.java b/service-t/src/com/android/server/mdns/MdnsRecord.java
similarity index 99%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsRecord.java
rename to service-t/src/com/android/server/mdns/MdnsRecord.java
index 00871ea..bcee9d1 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsRecord.java
+++ b/service-t/src/com/android/server/mdns/MdnsRecord.java
@@ -45,6 +45,7 @@
private static final int FLAG_CACHE_FLUSH = 0x8000;
public static final long RECEIPT_TIME_NOT_SENT = 0L;
+ public static final int CLASS_ANY = 0x00ff;
/** Status indicating that the record is current. */
public static final int STATUS_OK = 0;
@@ -317,4 +318,4 @@
return (recordType * 31) + Arrays.hashCode(recordName);
}
}
-}
\ No newline at end of file
+}
diff --git a/service-t/src/com/android/server/mdns/MdnsRecordRepository.java b/service-t/src/com/android/server/mdns/MdnsRecordRepository.java
new file mode 100644
index 0000000..e975ab4
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MdnsRecordRepository.java
@@ -0,0 +1,885 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.TargetApi;
+import android.net.LinkAddress;
+import android.net.nsd.NsdServiceInfo;
+import android.os.Build;
+import android.os.Looper;
+import android.os.SystemClock;
+import android.util.ArraySet;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.net.module.util.CollectionUtils;
+import com.android.net.module.util.HexDump;
+
+import java.io.IOException;
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.NetworkInterface;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A repository of records advertised through {@link MdnsInterfaceAdvertiser}.
+ *
+ * Must be used on a consistent looper thread.
+ */
+@TargetApi(Build.VERSION_CODES.TIRAMISU) // Allow calling T+ APIs; this is only loaded on T+
+public class MdnsRecordRepository {
+ // RFC6762 p.15
+ private static final long MIN_MULTICAST_REPLY_INTERVAL_MS = 1_000L;
+
+ // TTLs as per RFC6762 10.
+ // TTL for records with a host name as the resource record's name (e.g., A, AAAA, HINFO) or a
+ // host name contained within the resource record's rdata (e.g., SRV, reverse mapping PTR
+ // record)
+ private static final long NAME_RECORDS_TTL_MILLIS = TimeUnit.SECONDS.toMillis(120);
+ // TTL for other records
+ private static final long NON_NAME_RECORDS_TTL_MILLIS = TimeUnit.MINUTES.toMillis(75);
+
+ // Top-level domain for link-local queries, as per RFC6762 3.
+ private static final String LOCAL_TLD = "local";
+
+ // Service type for service enumeration (RFC6763 9.)
+ private static final String[] DNS_SD_SERVICE_TYPE =
+ new String[] { "_services", "_dns-sd", "_udp", LOCAL_TLD };
+
+ public static final InetSocketAddress IPV6_ADDR = new InetSocketAddress(
+ MdnsConstants.getMdnsIPv6Address(), MdnsConstants.MDNS_PORT);
+ public static final InetSocketAddress IPV4_ADDR = new InetSocketAddress(
+ MdnsConstants.getMdnsIPv4Address(), MdnsConstants.MDNS_PORT);
+
+ @NonNull
+ private final Random mDelayGenerator = new Random();
+ // Map of service unique ID -> records for service
+ @NonNull
+ private final SparseArray<ServiceRegistration> mServices = new SparseArray<>();
+ @NonNull
+ private final List<RecordInfo<?>> mGeneralRecords = new ArrayList<>();
+ @NonNull
+ private final Looper mLooper;
+ @NonNull
+ private String[] mDeviceHostname;
+
+ public MdnsRecordRepository(@NonNull Looper looper) {
+ this(looper, new Dependencies());
+ }
+
+ @VisibleForTesting
+ public MdnsRecordRepository(@NonNull Looper looper, @NonNull Dependencies deps) {
+ mDeviceHostname = deps.getHostname();
+ mLooper = looper;
+ }
+
+ /**
+ * Dependencies to use with {@link MdnsRecordRepository}, useful for testing.
+ */
+ @VisibleForTesting
+ public static class Dependencies {
+ /**
+ * Get a unique hostname to be used by the device.
+ */
+ @NonNull
+ public String[] getHostname() {
+ // Generate a very-probably-unique hostname. This allows minimizing possible conflicts
+ // to the point that probing for it is no longer necessary (as per RFC6762 8.1 last
+ // paragraph), and does not leak more information than what could already be obtained by
+ // looking at the mDNS packets source address.
+ // This differs from historical behavior that just used "Android.local" for many
+ // devices, creating a lot of conflicts.
+ // Having a different hostname per interface is an acceptable option as per RFC6762 14.
+ // This hostname will change every time the interface is reconnected, so this does not
+ // allow tracking the device.
+ // TODO: consider deriving a hostname from other sources, such as the IPv6 addresses
+ // (reusing the same privacy-protecting mechanics).
+ return new String[] {
+ "Android_" + UUID.randomUUID().toString().replace("-", ""), LOCAL_TLD };
+ }
+
+ /**
+ * @see NetworkInterface#getInetAddresses().
+ */
+ @NonNull
+ public Enumeration<InetAddress> getInterfaceInetAddresses(@NonNull NetworkInterface iface) {
+ return iface.getInetAddresses();
+ }
+ }
+
+ private static class RecordInfo<T extends MdnsRecord> {
+ public final T record;
+ public final NsdServiceInfo serviceInfo;
+
+ /**
+ * Whether the name of this record is expected to be fully owned by the service or may be
+ * advertised by other hosts as well (shared).
+ */
+ public final boolean isSharedName;
+
+ /**
+ * Whether probing is still in progress for the record.
+ */
+ public boolean isProbing;
+
+ /**
+ * Last time (as per SystemClock.elapsedRealtime) when advertised via multicast, 0 if never
+ */
+ public long lastAdvertisedTimeMs;
+
+ /**
+ * Last time (as per SystemClock.elapsedRealtime) when sent via unicast or multicast,
+ * 0 if never
+ */
+ public long lastSentTimeMs;
+
+ RecordInfo(NsdServiceInfo serviceInfo, T record, boolean sharedName,
+ boolean probing) {
+ this.serviceInfo = serviceInfo;
+ this.record = record;
+ this.isSharedName = sharedName;
+ this.isProbing = probing;
+ }
+ }
+
+ private static class ServiceRegistration {
+ @NonNull
+ public final List<RecordInfo<?>> allRecords;
+ @NonNull
+ public final RecordInfo<MdnsPointerRecord> ptrRecord;
+ @NonNull
+ public final RecordInfo<MdnsServiceRecord> srvRecord;
+ @NonNull
+ public final RecordInfo<MdnsTextRecord> txtRecord;
+ @NonNull
+ public final NsdServiceInfo serviceInfo;
+
+ /**
+ * Whether the service is sending exit announcements and will be destroyed soon.
+ */
+ public boolean exiting = false;
+
+ /**
+ * Create a ServiceRegistration for dns-sd service registration (RFC6763).
+ *
+ * @param deviceHostname Hostname of the device (for the interface used)
+ * @param serviceInfo Service to advertise
+ */
+ ServiceRegistration(@NonNull String[] deviceHostname, @NonNull NsdServiceInfo serviceInfo) {
+ this.serviceInfo = serviceInfo;
+
+ final String[] serviceType = splitServiceType(serviceInfo);
+ final String[] serviceName = splitFullyQualifiedName(serviceInfo, serviceType);
+
+ // Service PTR record
+ ptrRecord = new RecordInfo<>(
+ serviceInfo,
+ new MdnsPointerRecord(
+ serviceType,
+ 0L /* receiptTimeMillis */,
+ false /* cacheFlush */,
+ NON_NAME_RECORDS_TTL_MILLIS,
+ serviceName),
+ true /* sharedName */, true /* probing */);
+
+ srvRecord = new RecordInfo<>(
+ serviceInfo,
+ new MdnsServiceRecord(serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ NAME_RECORDS_TTL_MILLIS, 0 /* servicePriority */, 0 /* serviceWeight */,
+ serviceInfo.getPort(),
+ deviceHostname),
+ false /* sharedName */, true /* probing */);
+
+ txtRecord = new RecordInfo<>(
+ serviceInfo,
+ new MdnsTextRecord(serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */, // Service name is verified unique after probing
+ NON_NAME_RECORDS_TTL_MILLIS,
+ attrsToTextEntries(serviceInfo.getAttributes())),
+ false /* sharedName */, true /* probing */);
+
+ final ArrayList<RecordInfo<?>> allRecords = new ArrayList<>(4);
+ allRecords.add(ptrRecord);
+ allRecords.add(srvRecord);
+ allRecords.add(txtRecord);
+ // Service type enumeration record (RFC6763 9.)
+ allRecords.add(new RecordInfo<>(
+ serviceInfo,
+ new MdnsPointerRecord(
+ DNS_SD_SERVICE_TYPE,
+ 0L /* receiptTimeMillis */,
+ false /* cacheFlush */,
+ NON_NAME_RECORDS_TTL_MILLIS,
+ serviceType),
+ true /* sharedName */, true /* probing */));
+
+ this.allRecords = Collections.unmodifiableList(allRecords);
+ }
+
+ void setProbing(boolean probing) {
+ for (RecordInfo<?> info : allRecords) {
+ info.isProbing = probing;
+ }
+ }
+ }
+
+ /**
+ * Inform the repository of the latest interface addresses.
+ */
+ public void updateAddresses(@NonNull List<LinkAddress> newAddresses) {
+ mGeneralRecords.clear();
+ for (LinkAddress addr : newAddresses) {
+ final String[] revDnsAddr = getReverseDnsAddress(addr.getAddress());
+ mGeneralRecords.add(new RecordInfo<>(
+ null /* serviceInfo */,
+ new MdnsPointerRecord(
+ revDnsAddr,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ NAME_RECORDS_TTL_MILLIS,
+ mDeviceHostname),
+ false /* sharedName */, false /* probing */));
+
+ mGeneralRecords.add(new RecordInfo<>(
+ null /* serviceInfo */,
+ new MdnsInetAddressRecord(
+ mDeviceHostname,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ NAME_RECORDS_TTL_MILLIS,
+ addr.getAddress()),
+ false /* sharedName */, false /* probing */));
+ }
+ }
+
+ /**
+ * Add a service to the repository.
+ *
+ * This may remove/replace any existing service that used the name added but is exiting.
+ * @param serviceId A unique service ID.
+ * @param serviceInfo Service info to add.
+ * @return If the added service replaced another with a matching name (which was exiting), the
+ * ID of the replaced service.
+ * @throws NameConflictException There is already a (non-exiting) service using the name.
+ */
+ public int addService(int serviceId, NsdServiceInfo serviceInfo) throws NameConflictException {
+ if (mServices.contains(serviceId)) {
+ throw new IllegalArgumentException(
+ "Service ID must not be reused across registrations: " + serviceId);
+ }
+
+ final int existing = getServiceByName(serviceInfo.getServiceName());
+ // It's OK to re-add a service that is exiting
+ if (existing >= 0 && !mServices.get(existing).exiting) {
+ throw new NameConflictException(existing);
+ }
+
+ final ServiceRegistration registration = new ServiceRegistration(
+ mDeviceHostname, serviceInfo);
+ mServices.put(serviceId, registration);
+
+ // Remove existing exiting service
+ mServices.remove(existing);
+ return existing;
+ }
+
+ /**
+ * @return The ID of the service identified by its name, or -1 if none.
+ */
+ private int getServiceByName(@NonNull String serviceName) {
+ for (int i = 0; i < mServices.size(); i++) {
+ final ServiceRegistration registration = mServices.valueAt(i);
+ if (serviceName.equals(registration.serviceInfo.getServiceName())) {
+ return mServices.keyAt(i);
+ }
+ }
+ return -1;
+ }
+
+ private MdnsProber.ProbingInfo makeProbingInfo(int serviceId,
+ @NonNull MdnsServiceRecord srvRecord) {
+ final List<MdnsRecord> probingRecords = new ArrayList<>();
+ // Probe with cacheFlush cleared; it is set when announcing, as it was verified unique:
+ // RFC6762 10.2
+ probingRecords.add(new MdnsServiceRecord(srvRecord.getName(),
+ 0L /* receiptTimeMillis */,
+ false /* cacheFlush */,
+ srvRecord.getTtl(),
+ srvRecord.getServicePriority(), srvRecord.getServiceWeight(),
+ srvRecord.getServicePort(),
+ srvRecord.getServiceHost()));
+
+ return new MdnsProber.ProbingInfo(serviceId, probingRecords);
+ }
+
+ private static List<MdnsServiceInfo.TextEntry> attrsToTextEntries(Map<String, byte[]> attrs) {
+ final List<MdnsServiceInfo.TextEntry> out = new ArrayList<>(attrs.size());
+ for (Map.Entry<String, byte[]> attr : attrs.entrySet()) {
+ out.add(new MdnsServiceInfo.TextEntry(attr.getKey(), attr.getValue()));
+ }
+ return out;
+ }
+
+ /**
+ * Mark a service in the repository as exiting.
+ * @param id ID of the service, used at registration time.
+ * @return The exit announcement to indicate the service was removed, or null if not necessary.
+ */
+ @Nullable
+ public MdnsAnnouncer.ExitAnnouncementInfo exitService(int id) {
+ final ServiceRegistration registration = mServices.get(id);
+ if (registration == null) return null;
+ if (registration.exiting) return null;
+
+ // Send exit (TTL 0) for the PTR record, if the record was sent (in particular don't send
+ // if still probing)
+ if (registration.ptrRecord.lastSentTimeMs == 0L) {
+ return null;
+ }
+
+ registration.exiting = true;
+ final MdnsPointerRecord expiredRecord = new MdnsPointerRecord(
+ registration.ptrRecord.record.getName(),
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 0L /* ttlMillis */,
+ registration.ptrRecord.record.getPointer());
+
+ // Exit should be skipped if the record is still advertised by another service, but that
+ // would be a conflict (2 service registrations with the same service name), so it would
+ // not have been allowed by the repository.
+ return new MdnsAnnouncer.ExitAnnouncementInfo(id, Collections.singletonList(expiredRecord));
+ }
+
+ public void removeService(int id) {
+ mServices.remove(id);
+ }
+
+ /**
+ * @return The number of services currently held in the repository, including exiting services.
+ */
+ public int getServicesCount() {
+ return mServices.size();
+ }
+
+ /**
+ * Remove all services from the repository
+ * @return IDs of the removed services
+ */
+ @NonNull
+ public int[] clearServices() {
+ final int[] ret = new int[mServices.size()];
+ for (int i = 0; i < mServices.size(); i++) {
+ ret[i] = mServices.keyAt(i);
+ }
+ mServices.clear();
+ return ret;
+ }
+
+ /**
+ * Info about a reply to be sent.
+ */
+ public static class ReplyInfo {
+ @NonNull
+ public final List<MdnsRecord> answers;
+ @NonNull
+ public final List<MdnsRecord> additionalAnswers;
+ public final long sendDelayMs;
+ @NonNull
+ public final InetSocketAddress destination;
+
+ public ReplyInfo(
+ @NonNull List<MdnsRecord> answers,
+ @NonNull List<MdnsRecord> additionalAnswers,
+ long sendDelayMs,
+ @NonNull InetSocketAddress destination) {
+ this.answers = answers;
+ this.additionalAnswers = additionalAnswers;
+ this.sendDelayMs = sendDelayMs;
+ this.destination = destination;
+ }
+
+ @Override
+ public String toString() {
+ return "{ReplyInfo to " + destination + ", answers: " + answers.size()
+ + ", additionalAnswers: " + additionalAnswers.size()
+ + ", sendDelayMs " + sendDelayMs + "}";
+ }
+ }
+
+ /**
+ * Get the reply to send to an incoming packet.
+ *
+ * @param packet The incoming packet.
+ * @param src The source address of the incoming packet.
+ */
+ @Nullable
+ public ReplyInfo getReply(MdnsPacket packet, InetSocketAddress src) {
+ final long now = SystemClock.elapsedRealtime();
+ final boolean replyUnicast = (packet.flags & MdnsConstants.QCLASS_UNICAST) != 0;
+ final ArrayList<MdnsRecord> additionalAnswerRecords = new ArrayList<>();
+ final ArrayList<RecordInfo<?>> answerInfo = new ArrayList<>();
+ for (MdnsRecord question : packet.questions) {
+ // Add answers from general records
+ addReplyFromService(question, mGeneralRecords, null /* servicePtrRecord */,
+ null /* serviceSrvRecord */, null /* serviceTxtRecord */, replyUnicast, now,
+ answerInfo, additionalAnswerRecords);
+
+ // Add answers from each service
+ for (int i = 0; i < mServices.size(); i++) {
+ final ServiceRegistration registration = mServices.valueAt(i);
+ if (registration.exiting) continue;
+ addReplyFromService(question, registration.allRecords, registration.ptrRecord,
+ registration.srvRecord, registration.txtRecord, replyUnicast, now,
+ answerInfo, additionalAnswerRecords);
+ }
+ }
+
+ if (answerInfo.size() == 0 && additionalAnswerRecords.size() == 0) {
+ return null;
+ }
+
+ // Determine the send delay
+ final long delayMs;
+ if ((packet.flags & MdnsConstants.FLAG_TRUNCATED) != 0) {
+ // RFC 6762 6.: 400-500ms delay if TC bit is set
+ delayMs = 400L + mDelayGenerator.nextInt(100);
+ } else if (packet.questions.size() > 1
+ || CollectionUtils.any(answerInfo, a -> a.isSharedName)) {
+ // 20-120ms if there may be responses from other hosts (not a fully owned
+ // name) (RFC 6762 6.), or if there are multiple questions (6.3).
+ // TODO: this should be 0 if this is a probe query ("can be distinguished from a
+ // normal query by the fact that a probe query contains a proposed record in the
+ // Authority Section that answers the question" in 6.), and the reply is for a fully
+ // owned record.
+ delayMs = 20L + mDelayGenerator.nextInt(100);
+ } else {
+ delayMs = 0L;
+ }
+
+ // Determine the send destination
+ final InetSocketAddress dest;
+ if (replyUnicast) {
+ dest = src;
+ } else if (src.getAddress() instanceof Inet4Address) {
+ dest = IPV4_ADDR;
+ } else {
+ dest = IPV6_ADDR;
+ }
+
+ // Build the list of answer records from their RecordInfo
+ final ArrayList<MdnsRecord> answerRecords = new ArrayList<>(answerInfo.size());
+ for (RecordInfo<?> info : answerInfo) {
+ // TODO: consider actual packet send delay after response aggregation
+ info.lastSentTimeMs = now + delayMs;
+ if (!replyUnicast) {
+ info.lastAdvertisedTimeMs = info.lastSentTimeMs;
+ }
+ answerRecords.add(info.record);
+ }
+
+ return new ReplyInfo(answerRecords, additionalAnswerRecords, delayMs, dest);
+ }
+
+ /**
+ * Add answers and additional answers for a question, from a ServiceRegistration.
+ */
+ private void addReplyFromService(@NonNull MdnsRecord question,
+ @NonNull List<RecordInfo<?>> serviceRecords,
+ @Nullable RecordInfo<MdnsPointerRecord> servicePtrRecord,
+ @Nullable RecordInfo<MdnsServiceRecord> serviceSrvRecord,
+ @Nullable RecordInfo<MdnsTextRecord> serviceTxtRecord,
+ boolean replyUnicast, long now, @NonNull List<RecordInfo<?>> answerInfo,
+ @NonNull List<MdnsRecord> additionalAnswerRecords) {
+ boolean hasDnsSdPtrRecordAnswer = false;
+ boolean hasDnsSdSrvRecordAnswer = false;
+ boolean hasFullyOwnedNameMatch = false;
+ boolean hasKnownAnswer = false;
+
+ final int answersStartIndex = answerInfo.size();
+ for (RecordInfo<?> info : serviceRecords) {
+ if (info.isProbing) continue;
+
+ /* RFC6762 6.: the record name must match the question name, the record rrtype
+ must match the question qtype unless the qtype is "ANY" (255) or the rrtype is
+ "CNAME" (5), and the record rrclass must match the question qclass unless the
+ qclass is "ANY" (255) */
+ if (!Arrays.equals(info.record.getName(), question.getName())) continue;
+ hasFullyOwnedNameMatch |= !info.isSharedName;
+
+ // The repository does not store CNAME records
+ if (question.getType() != MdnsRecord.TYPE_ANY
+ && question.getType() != info.record.getType()) {
+ continue;
+ }
+ if (question.getRecordClass() != MdnsRecord.CLASS_ANY
+ && question.getRecordClass() != info.record.getRecordClass()) {
+ continue;
+ }
+
+ hasKnownAnswer = true;
+ hasDnsSdPtrRecordAnswer |= (info == servicePtrRecord);
+ hasDnsSdSrvRecordAnswer |= (info == serviceSrvRecord);
+
+ // TODO: responses to probe queries should bypass this check and only ensure the
+ // reply is sent 250ms after the last sent time (RFC 6762 p.15)
+ if (!replyUnicast && info.lastAdvertisedTimeMs > 0L
+ && now - info.lastAdvertisedTimeMs < MIN_MULTICAST_REPLY_INTERVAL_MS) {
+ continue;
+ }
+
+ // TODO: Don't reply if in known answers of the querier (7.1) if TTL is > half
+
+ answerInfo.add(info);
+ }
+
+ // RFC6762 6.1:
+ // "Any time a responder receives a query for a name for which it has verified exclusive
+ // ownership, for a type for which that name has no records, the responder MUST [...]
+ // respond asserting the nonexistence of that record"
+ if (hasFullyOwnedNameMatch && !hasKnownAnswer) {
+ additionalAnswerRecords.add(new MdnsNsecRecord(
+ question.getName(),
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ // TODO: RFC6762 6.1: "In general, the TTL given for an NSEC record SHOULD
+ // be the same as the TTL that the record would have had, had it existed."
+ NAME_RECORDS_TTL_MILLIS,
+ question.getName(),
+ new int[] { question.getType() }));
+ }
+
+ // No more records to add if no answer
+ if (answerInfo.size() == answersStartIndex) return;
+
+ final List<RecordInfo<?>> additionalAnswerInfo = new ArrayList<>();
+ // RFC6763 12.1: if including PTR record, include the SRV and TXT records it names
+ if (hasDnsSdPtrRecordAnswer) {
+ if (serviceTxtRecord != null) {
+ additionalAnswerInfo.add(serviceTxtRecord);
+ }
+ if (serviceSrvRecord != null) {
+ additionalAnswerInfo.add(serviceSrvRecord);
+ }
+ }
+
+ // RFC6763 12.1&.2: if including PTR or SRV record, include the address records it names
+ if (hasDnsSdPtrRecordAnswer || hasDnsSdSrvRecordAnswer) {
+ for (RecordInfo<?> record : mGeneralRecords) {
+ if (record.record instanceof MdnsInetAddressRecord) {
+ additionalAnswerInfo.add(record);
+ }
+ }
+ }
+
+ for (RecordInfo<?> info : additionalAnswerInfo) {
+ additionalAnswerRecords.add(info.record);
+ }
+
+ // RFC6762 6.1: negative responses
+ addNsecRecordsForUniqueNames(additionalAnswerRecords,
+ answerInfo.listIterator(answersStartIndex),
+ additionalAnswerInfo.listIterator());
+ }
+
+ /**
+ * Add NSEC records indicating that the response records are unique.
+ *
+ * Following RFC6762 6.1:
+ * "On receipt of a question for a particular name, rrtype, and rrclass, for which a responder
+ * does have one or more unique answers, the responder MAY also include an NSEC record in the
+ * Additional Record Section indicating the nonexistence of other rrtypes for that name and
+ * rrclass."
+ * @param destinationList List to add the NSEC records to.
+ * @param answerRecords Lists of answered records based on which to add NSEC records (typically
+ * answer and additionalAnswer sections)
+ */
+ @SafeVarargs
+ private static void addNsecRecordsForUniqueNames(
+ List<MdnsRecord> destinationList,
+ Iterator<RecordInfo<?>>... answerRecords) {
+ // Group unique records by name. Use a TreeMap with comparator as arrays don't implement
+ // equals / hashCode.
+ final Map<String[], List<MdnsRecord>> nsecByName = new TreeMap<>(Arrays::compare);
+ // But keep the list of names in added order, otherwise records would be sorted in
+ // alphabetical order instead of the order of the original records, which would look like
+ // inconsistent behavior depending on service name.
+ final List<String[]> namesInAddedOrder = new ArrayList<>();
+ for (Iterator<RecordInfo<?>> answers : answerRecords) {
+ addNonSharedRecordsToMap(answers, nsecByName, namesInAddedOrder);
+ }
+
+ for (String[] nsecName : namesInAddedOrder) {
+ final List<MdnsRecord> entryRecords = nsecByName.get(nsecName);
+ long minTtl = Long.MAX_VALUE;
+ final Set<Integer> types = new ArraySet<>(entryRecords.size());
+ for (MdnsRecord record : entryRecords) {
+ if (minTtl > record.getTtl()) minTtl = record.getTtl();
+ types.add(record.getType());
+ }
+
+ destinationList.add(new MdnsNsecRecord(
+ nsecName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ minTtl,
+ nsecName,
+ CollectionUtils.toIntArray(types)));
+ }
+ }
+
+ /**
+ * Add non-shared records to a map listing them by record name, and to a list of names that
+ * remembers the adding order.
+ *
+ * In the destination map records are grouped by name; so the map has one key per record name,
+ * and the values are the lists of different records that share the same name.
+ * @param records Records to scan.
+ * @param dest Map to add the records to.
+ * @param namesInAddedOrder List of names to add the names in order, keeping the first
+ * occurrence of each name.
+ */
+ private static void addNonSharedRecordsToMap(
+ Iterator<RecordInfo<?>> records,
+ Map<String[], List<MdnsRecord>> dest,
+ List<String[]> namesInAddedOrder) {
+ while (records.hasNext()) {
+ final RecordInfo<?> record = records.next();
+ if (record.isSharedName) continue;
+ final List<MdnsRecord> recordsForName = dest.computeIfAbsent(record.record.name,
+ key -> {
+ namesInAddedOrder.add(key);
+ return new ArrayList<>();
+ });
+ recordsForName.add(record.record);
+ }
+ }
+
+ /**
+ * Called to indicate that probing succeeded for a service.
+ * @param probeSuccessInfo The successful probing info.
+ * @return The {@link MdnsAnnouncer.AnnouncementInfo} to send, now that probing has succeeded.
+ */
+ public MdnsAnnouncer.AnnouncementInfo onProbingSucceeded(
+ MdnsProber.ProbingInfo probeSuccessInfo)
+ throws IOException {
+
+ final ServiceRegistration registration = mServices.get(probeSuccessInfo.getServiceId());
+ if (registration == null) throw new IOException(
+ "Service is not registered: " + probeSuccessInfo.getServiceId());
+ registration.setProbing(false);
+
+ final ArrayList<MdnsRecord> answers = new ArrayList<>();
+ final ArrayList<MdnsRecord> additionalAnswers = new ArrayList<>();
+
+ // Interface address records in general records
+ for (RecordInfo<?> record : mGeneralRecords) {
+ answers.add(record.record);
+ }
+
+ // All service records
+ for (RecordInfo<?> info : registration.allRecords) {
+ answers.add(info.record);
+ }
+
+ addNsecRecordsForUniqueNames(additionalAnswers,
+ mGeneralRecords.iterator(), registration.allRecords.iterator());
+
+ return new MdnsAnnouncer.AnnouncementInfo(probeSuccessInfo.getServiceId(),
+ answers, additionalAnswers);
+ }
+
+ /**
+ * Get the service IDs of services conflicting with a received packet.
+ */
+ public Set<Integer> getConflictingServices(MdnsPacket packet) {
+ // Avoid allocating a new set for each incoming packet: use an empty set by default.
+ Set<Integer> conflicting = Collections.emptySet();
+ for (MdnsRecord record : packet.answers) {
+ for (int i = 0; i < mServices.size(); i++) {
+ final ServiceRegistration registration = mServices.valueAt(i);
+ if (registration.exiting) continue;
+
+ // Only look for conflicts in service name, as a different service name can be used
+ // if there is a conflict, but there is nothing actionable if any other conflict
+ // happens. In fact probing is only done for the service name in the SRV record.
+ // This means only SRV and TXT records need to be checked.
+ final RecordInfo<MdnsServiceRecord> srvRecord = registration.srvRecord;
+ if (!Arrays.equals(record.getName(), srvRecord.record.getName())) continue;
+
+ // As per RFC6762 9., it's fine if the "conflict" is an identical record with same
+ // data.
+ if (record instanceof MdnsServiceRecord) {
+ final MdnsServiceRecord local = srvRecord.record;
+ final MdnsServiceRecord other = (MdnsServiceRecord) record;
+ // Note "equals" does not consider TTL or receipt time, as intended here
+ if (Objects.equals(local, other)) {
+ continue;
+ }
+ }
+
+ if (record instanceof MdnsTextRecord) {
+ final MdnsTextRecord local = registration.txtRecord.record;
+ final MdnsTextRecord other = (MdnsTextRecord) record;
+ if (Objects.equals(local, other)) {
+ continue;
+ }
+ }
+
+ if (conflicting.size() == 0) {
+ // Conflict was found: use a mutable set
+ conflicting = new ArraySet<>();
+ }
+ final int serviceId = mServices.keyAt(i);
+ conflicting.add(serviceId);
+ }
+ }
+
+ return conflicting;
+ }
+
+ /**
+ * (Re)set a service to the probing state.
+ * @return The {@link MdnsProber.ProbingInfo} to send for probing.
+ */
+ @Nullable
+ public MdnsProber.ProbingInfo setServiceProbing(int serviceId) {
+ final ServiceRegistration registration = mServices.get(serviceId);
+ if (registration == null) return null;
+
+ registration.setProbing(true);
+ return makeProbingInfo(serviceId, registration.srvRecord.record);
+ }
+
+ /**
+ * Indicates whether a given service is in probing state.
+ */
+ public boolean isProbing(int serviceId) {
+ final ServiceRegistration registration = mServices.get(serviceId);
+ if (registration == null) return false;
+
+ return registration.srvRecord.isProbing;
+ }
+
+ /**
+ * Return whether the repository has an active (non-exiting) service for the given ID.
+ */
+ public boolean hasActiveService(int serviceId) {
+ final ServiceRegistration registration = mServices.get(serviceId);
+ if (registration == null) return false;
+
+ return !registration.exiting;
+ }
+
+ /**
+ * Rename a service to the newly provided info, following a conflict.
+ *
+ * If the specified service does not exist, this returns null.
+ */
+ @Nullable
+ public MdnsProber.ProbingInfo renameServiceForConflict(int serviceId, NsdServiceInfo newInfo) {
+ if (!mServices.contains(serviceId)) return null;
+
+ final ServiceRegistration newService = new ServiceRegistration(
+ mDeviceHostname, newInfo);
+ mServices.put(serviceId, newService);
+ return makeProbingInfo(serviceId, newService.srvRecord.record);
+ }
+
+ /**
+ * Called when {@link MdnsAdvertiser} sent an advertisement for the given service.
+ */
+ public void onAdvertisementSent(int serviceId) {
+ final ServiceRegistration registration = mServices.get(serviceId);
+ if (registration == null) return;
+
+ final long now = SystemClock.elapsedRealtime();
+ for (RecordInfo<?> record : registration.allRecords) {
+ record.lastSentTimeMs = now;
+ record.lastAdvertisedTimeMs = now;
+ }
+ }
+
+ /**
+ * Compute:
+ * 2001:db8::1 --> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.0.1.0.0.2.ip6.arpa
+ *
+ * Or:
+ * 192.0.2.123 --> 123.2.0.192.in-addr.arpa
+ */
+ @VisibleForTesting
+ public static String[] getReverseDnsAddress(@NonNull InetAddress addr) {
+ // xxx.xxx.xxx.xxx.in-addr.arpa (up to 28 characters)
+ // or 32 hex characters separated by dots + .ip6.arpa
+ final byte[] addrBytes = addr.getAddress();
+ final List<String> out = new ArrayList<>();
+ if (addr instanceof Inet4Address) {
+ for (int i = addrBytes.length - 1; i >= 0; i--) {
+ out.add(String.valueOf(Byte.toUnsignedInt(addrBytes[i])));
+ }
+ out.add("in-addr");
+ } else {
+ final String hexAddr = HexDump.toHexString(addrBytes);
+
+ for (int i = hexAddr.length() - 1; i >= 0; i--) {
+ out.add(String.valueOf(hexAddr.charAt(i)));
+ }
+ out.add("ip6");
+ }
+ out.add("arpa");
+
+ return out.toArray(new String[0]);
+ }
+
+ private static String[] splitFullyQualifiedName(
+ @NonNull NsdServiceInfo info, @NonNull String[] serviceType) {
+ final String[] split = new String[serviceType.length + 1];
+ split[0] = info.getServiceName();
+ System.arraycopy(serviceType, 0, split, 1, serviceType.length);
+
+ return split;
+ }
+
+ private static String[] splitServiceType(@NonNull NsdServiceInfo info) {
+ // String.split(pattern, 0) removes trailing empty strings, which would appear when
+ // splitting "domain.name." (with a dot a the end), so this is what is needed here.
+ final String[] split = info.getServiceType().split("\\.", 0);
+ final String[] type = new String[split.length + 1];
+ System.arraycopy(split, 0, type, 0, split.length);
+ type[split.length] = LOCAL_TLD;
+
+ return type;
+ }
+}
diff --git a/service-t/src/com/android/server/mdns/MdnsReplySender.java b/service-t/src/com/android/server/mdns/MdnsReplySender.java
new file mode 100644
index 0000000..f1389ca
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MdnsReplySender.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import static com.android.server.connectivity.mdns.MdnsSocketProvider.ensureRunningOnHandlerThread;
+
+import android.annotation.NonNull;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.util.Log;
+
+import com.android.server.connectivity.mdns.MdnsRecordRepository.ReplyInfo;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetSocketAddress;
+import java.net.MulticastSocket;
+import java.util.Collections;
+
+/**
+ * A class that handles sending mDNS replies to a {@link MulticastSocket}, possibly queueing them
+ * to be sent after some delay.
+ *
+ * TODO: implement sending after a delay, combining queued replies and duplicate answer suppression
+ */
+public class MdnsReplySender {
+ private static final boolean DBG = MdnsAdvertiser.DBG;
+ private static final int MSG_SEND = 1;
+
+ private final String mLogTag;
+ @NonNull
+ private final MdnsInterfaceSocket mSocket;
+ @NonNull
+ private final Handler mHandler;
+ @NonNull
+ private final byte[] mPacketCreationBuffer;
+
+ public MdnsReplySender(@NonNull String interfaceTag, @NonNull Looper looper,
+ @NonNull MdnsInterfaceSocket socket, @NonNull byte[] packetCreationBuffer) {
+ mHandler = new SendHandler(looper);
+ mLogTag = MdnsReplySender.class.getSimpleName() + "/" + interfaceTag;
+ mSocket = socket;
+ mPacketCreationBuffer = packetCreationBuffer;
+ }
+
+ /**
+ * Queue a reply to be sent when its send delay expires.
+ */
+ public void queueReply(@NonNull ReplyInfo reply) {
+ ensureRunningOnHandlerThread(mHandler);
+ // TODO: implement response aggregation (RFC 6762 6.4)
+ mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SEND, reply), reply.sendDelayMs);
+
+ if (DBG) {
+ Log.v(mLogTag, "Scheduling " + reply);
+ }
+ }
+
+ /**
+ * Send a packet immediately.
+ *
+ * Must be called on the looper thread used by the {@link MdnsReplySender}.
+ */
+ public void sendNow(@NonNull MdnsPacket packet, @NonNull InetSocketAddress destination)
+ throws IOException {
+ ensureRunningOnHandlerThread(mHandler);
+ if (!((destination.getAddress() instanceof Inet6Address && mSocket.hasJoinedIpv6())
+ || (destination.getAddress() instanceof Inet4Address && mSocket.hasJoinedIpv4()))) {
+ // Skip sending if the socket has not joined the v4/v6 group (there was no address)
+ return;
+ }
+
+ // TODO: support packets over size (send in multiple packets with TC bit set)
+ final MdnsPacketWriter writer = new MdnsPacketWriter(mPacketCreationBuffer);
+
+ writer.writeUInt16(0); // Transaction ID (advertisement: 0)
+ writer.writeUInt16(packet.flags); // Response, authoritative (rfc6762 18.4)
+ writer.writeUInt16(packet.questions.size()); // questions count
+ writer.writeUInt16(packet.answers.size()); // answers count
+ writer.writeUInt16(packet.authorityRecords.size()); // authority entries count
+ writer.writeUInt16(packet.additionalRecords.size()); // additional records count
+
+ for (MdnsRecord record : packet.questions) {
+ // Questions do not have TTL or data
+ record.writeHeaderFields(writer);
+ }
+ for (MdnsRecord record : packet.answers) {
+ record.write(writer, 0L);
+ }
+ for (MdnsRecord record : packet.authorityRecords) {
+ record.write(writer, 0L);
+ }
+ for (MdnsRecord record : packet.additionalRecords) {
+ record.write(writer, 0L);
+ }
+
+ final int len = writer.getWritePosition();
+ final byte[] outBuffer = new byte[len];
+ System.arraycopy(mPacketCreationBuffer, 0, outBuffer, 0, len);
+
+ mSocket.send(new DatagramPacket(outBuffer, 0, len, destination));
+ }
+
+ /**
+ * Cancel all pending sends.
+ */
+ public void cancelAll() {
+ ensureRunningOnHandlerThread(mHandler);
+ mHandler.removeMessages(MSG_SEND);
+ }
+
+ private class SendHandler extends Handler {
+ SendHandler(@NonNull Looper looper) {
+ super(looper);
+ }
+
+ @Override
+ public void handleMessage(@NonNull Message msg) {
+ final ReplyInfo replyInfo = (ReplyInfo) msg.obj;
+ if (DBG) Log.v(mLogTag, "Sending " + replyInfo);
+
+ final int flags = 0x8400; // Response, authoritative (rfc6762 18.4)
+ final MdnsPacket packet = new MdnsPacket(flags,
+ Collections.emptyList() /* questions */,
+ replyInfo.answers,
+ Collections.emptyList() /* authorityRecords */,
+ replyInfo.additionalAnswers);
+
+ try {
+ sendNow(packet, replyInfo.destination);
+ } catch (IOException e) {
+ Log.e(mLogTag, "Error sending MDNS response", e);
+ }
+ }
+ }
+}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsResponse.java b/service-t/src/com/android/server/mdns/MdnsResponse.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsResponse.java
rename to service-t/src/com/android/server/mdns/MdnsResponse.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsResponseDecoder.java b/service-t/src/com/android/server/mdns/MdnsResponseDecoder.java
similarity index 66%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
rename to service-t/src/com/android/server/mdns/MdnsResponseDecoder.java
index 7cf84f6..82da2e4 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
+++ b/service-t/src/com/android/server/mdns/MdnsResponseDecoder.java
@@ -24,11 +24,9 @@
import com.android.server.connectivity.mdns.util.MdnsLogger;
import java.io.EOFException;
-import java.io.IOException;
import java.net.DatagramPacket;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.LinkedList;
import java.util.List;
/** A class that decodes mDNS responses from UDP packets. */
@@ -48,12 +46,6 @@
this.serviceType = serviceType;
}
- private static void skipMdnsRecord(MdnsPacketReader reader) throws IOException {
- reader.skip(2 + 4); // skip the class and TTL
- int dataLength = reader.readUInt16();
- reader.skip(dataLength);
- }
-
private static MdnsResponse findResponseWithPointer(
List<MdnsResponse> responses, String[] pointer) {
if (responses != null) {
@@ -101,9 +93,26 @@
*/
public int decode(@NonNull DatagramPacket packet, @NonNull List<MdnsResponse> responses,
int interfaceIndex, @Nullable Network network) {
- MdnsPacketReader reader = new MdnsPacketReader(packet);
+ return decode(packet.getData(), packet.getLength(), responses, interfaceIndex, network);
+ }
- List<MdnsRecord> records;
+ /**
+ * Decodes all mDNS responses for the desired service type from a packet. The class does not
+ * check
+ * the responses for completeness; the caller should do that.
+ *
+ * @param recvbuf The received data buffer to read from.
+ * @param length The length of received data buffer.
+ * @param interfaceIndex the network interface index (or {@link
+ * MdnsSocket#INTERFACE_INDEX_UNSPECIFIED} if not known) at which the packet was received
+ * @param network the network at which the packet was received, or null if it is unknown.
+ * @return A list of mDNS responses, or null if the packet contained no appropriate responses.
+ */
+ public int decode(@NonNull byte[] recvbuf, int length, @NonNull List<MdnsResponse> responses,
+ int interfaceIndex, @Nullable Network network) {
+ MdnsPacketReader reader = new MdnsPacketReader(recvbuf, length);
+
+ final MdnsPacket mdnsPacket;
try {
reader.readUInt16(); // transaction ID (not used)
int flags = reader.readUInt16();
@@ -111,111 +120,25 @@
return MdnsResponseErrorCode.ERROR_NOT_RESPONSE_MESSAGE;
}
- int numQuestions = reader.readUInt16();
- int numAnswers = reader.readUInt16();
- int numAuthority = reader.readUInt16();
- int numRecords = reader.readUInt16();
-
- LOGGER.log(String.format(
- "num questions: %d, num answers: %d, num authority: %d, num records: %d",
- numQuestions, numAnswers, numAuthority, numRecords));
-
- if (numAnswers < 1) {
+ mdnsPacket = MdnsPacket.parseRecordsSection(reader, flags);
+ if (mdnsPacket.answers.size() < 1) {
return MdnsResponseErrorCode.ERROR_NO_ANSWERS;
}
-
- records = new LinkedList<>();
-
- for (int i = 0; i < (numAnswers + numAuthority + numRecords); ++i) {
- String[] name;
- try {
- name = reader.readLabels();
- } catch (IOException e) {
- LOGGER.e("Failed to read labels from mDNS response.", e);
- return MdnsResponseErrorCode.ERROR_READING_RECORD_NAME;
- }
- int type = reader.readUInt16();
-
- switch (type) {
- case MdnsRecord.TYPE_A: {
- try {
- records.add(new MdnsInetAddressRecord(name, MdnsRecord.TYPE_A, reader));
- } catch (IOException e) {
- LOGGER.e("Failed to read A record from mDNS response.", e);
- return MdnsResponseErrorCode.ERROR_READING_A_RDATA;
- }
- break;
- }
-
- case MdnsRecord.TYPE_AAAA: {
- try {
- // AAAA should only contain the IPv6 address.
- MdnsInetAddressRecord record =
- new MdnsInetAddressRecord(name, MdnsRecord.TYPE_AAAA, reader);
- if (record.getInet6Address() != null) {
- records.add(record);
- }
- } catch (IOException e) {
- LOGGER.e("Failed to read AAAA record from mDNS response.", e);
- return MdnsResponseErrorCode.ERROR_READING_AAAA_RDATA;
- }
- break;
- }
-
- case MdnsRecord.TYPE_PTR: {
- try {
- records.add(new MdnsPointerRecord(name, reader));
- } catch (IOException e) {
- LOGGER.e("Failed to read PTR record from mDNS response.", e);
- return MdnsResponseErrorCode.ERROR_READING_PTR_RDATA;
- }
- break;
- }
-
- case MdnsRecord.TYPE_SRV: {
- if (name.length == 4) {
- try {
- records.add(new MdnsServiceRecord(name, reader));
- } catch (IOException e) {
- LOGGER.e("Failed to read SRV record from mDNS response.", e);
- return MdnsResponseErrorCode.ERROR_READING_SRV_RDATA;
- }
- } else {
- try {
- skipMdnsRecord(reader);
- } catch (IOException e) {
- LOGGER.e("Failed to skip SVR record from mDNS response.", e);
- return MdnsResponseErrorCode.ERROR_SKIPPING_SRV_RDATA;
- }
- }
- break;
- }
-
- case MdnsRecord.TYPE_TXT: {
- try {
- records.add(new MdnsTextRecord(name, reader));
- } catch (IOException e) {
- LOGGER.e("Failed to read TXT record from mDNS response.", e);
- return MdnsResponseErrorCode.ERROR_READING_TXT_RDATA;
- }
- break;
- }
-
- default: {
- try {
- skipMdnsRecord(reader);
- } catch (IOException e) {
- LOGGER.e("Failed to skip mDNS record.", e);
- return MdnsResponseErrorCode.ERROR_SKIPPING_UNKNOWN_RECORD;
- }
- }
- }
- }
} catch (EOFException e) {
LOGGER.e("Reached the end of the mDNS response unexpectedly.", e);
return MdnsResponseErrorCode.ERROR_END_OF_FILE;
+ } catch (MdnsPacket.ParseException e) {
+ LOGGER.e(e.getMessage(), e);
+ return e.code;
}
+ final ArrayList<MdnsRecord> records = new ArrayList<>(
+ mdnsPacket.questions.size() + mdnsPacket.answers.size()
+ + mdnsPacket.authorityRecords.size() + mdnsPacket.additionalRecords.size());
+ records.addAll(mdnsPacket.answers);
+ records.addAll(mdnsPacket.authorityRecords);
+ records.addAll(mdnsPacket.additionalRecords);
+
// The response records are structured in a hierarchy, where some records reference
// others, as follows:
//
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsResponseErrorCode.java b/service-t/src/com/android/server/mdns/MdnsResponseErrorCode.java
similarity index 92%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsResponseErrorCode.java
rename to service-t/src/com/android/server/mdns/MdnsResponseErrorCode.java
index fcf9058..73a7e3a 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsResponseErrorCode.java
+++ b/service-t/src/com/android/server/mdns/MdnsResponseErrorCode.java
@@ -35,4 +35,6 @@
public static final int ERROR_READING_TXT_RDATA = 10;
public static final int ERROR_SKIPPING_UNKNOWN_RECORD = 11;
public static final int ERROR_END_OF_FILE = 12;
+ public static final int ERROR_READING_NSEC_RDATA = 13;
+ public static final int ERROR_READING_ANY_RDATA = 14;
}
\ No newline at end of file
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsSearchOptions.java b/service-t/src/com/android/server/mdns/MdnsSearchOptions.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsSearchOptions.java
rename to service-t/src/com/android/server/mdns/MdnsSearchOptions.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsServiceBrowserListener.java b/service-t/src/com/android/server/mdns/MdnsServiceBrowserListener.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsServiceBrowserListener.java
rename to service-t/src/com/android/server/mdns/MdnsServiceBrowserListener.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsServiceInfo.java b/service-t/src/com/android/server/mdns/MdnsServiceInfo.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsServiceInfo.java
rename to service-t/src/com/android/server/mdns/MdnsServiceInfo.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsServiceRecord.java b/service-t/src/com/android/server/mdns/MdnsServiceRecord.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsServiceRecord.java
rename to service-t/src/com/android/server/mdns/MdnsServiceRecord.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsServiceTypeClient.java b/service-t/src/com/android/server/mdns/MdnsServiceTypeClient.java
similarity index 96%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsServiceTypeClient.java
rename to service-t/src/com/android/server/mdns/MdnsServiceTypeClient.java
index 538f376..d26fbdb 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsServiceTypeClient.java
+++ b/service-t/src/com/android/server/mdns/MdnsServiceTypeClient.java
@@ -20,6 +20,7 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
+import android.net.Network;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.ArraySet;
@@ -52,7 +53,7 @@
private final String serviceType;
private final String[] serviceTypeLabels;
- private final MdnsSocketClient socketClient;
+ private final MdnsSocketClientBase socketClient;
private final ScheduledExecutorService executor;
private final Object lock = new Object();
private final Set<MdnsServiceBrowserListener> listeners = new ArraySet<>();
@@ -77,11 +78,11 @@
* Constructor of {@link MdnsServiceTypeClient}.
*
* @param socketClient Sends and receives mDNS packet.
- * @param executor A {@link ScheduledExecutorService} used to schedule query tasks.
+ * @param executor A {@link ScheduledExecutorService} used to schedule query tasks.
*/
public MdnsServiceTypeClient(
@NonNull String serviceType,
- @NonNull MdnsSocketClient socketClient,
+ @NonNull MdnsSocketClientBase socketClient,
@NonNull ScheduledExecutorService executor) {
this.serviceType = serviceType;
this.socketClient = socketClient;
@@ -169,7 +170,8 @@
new QueryTaskConfig(
searchOptions.getSubtypes(),
searchOptions.isPassiveMode(),
- ++currentSessionId)));
+ ++currentSessionId,
+ searchOptions.getNetwork())));
}
}
@@ -322,9 +324,10 @@
private int burstCounter;
private int timeToRunNextTaskInMs;
private boolean isFirstBurst;
+ @Nullable private final Network network;
QueryTaskConfig(@NonNull Collection<String> subtypes, boolean usePassiveMode,
- long sessionId) {
+ long sessionId, @Nullable Network network) {
this.usePassiveMode = usePassiveMode;
this.subtypes = new ArrayList<>(subtypes);
this.queriesPerBurst = QUERIES_PER_BURST;
@@ -346,6 +349,7 @@
// doubles until it maxes out at TIME_BETWEEN_BURSTS_MS.
this.timeBetweenBurstsInMs = INITIAL_TIME_BETWEEN_BURSTS_MS;
}
+ this.network = network;
}
QueryTaskConfig getConfigForNextRun() {
@@ -405,7 +409,8 @@
serviceType,
config.subtypes,
config.expectUnicastResponse,
- config.transactionId)
+ config.transactionId,
+ config.network)
.call();
} catch (RuntimeException e) {
LOGGER.e(String.format("Failed to run EnqueueMdnsQueryCallable for subtype: %s",
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsSocket.java b/service-t/src/com/android/server/mdns/MdnsSocket.java
similarity index 97%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsSocket.java
rename to service-t/src/com/android/server/mdns/MdnsSocket.java
index 64c4495..5fd1354 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsSocket.java
+++ b/service-t/src/com/android/server/mdns/MdnsSocket.java
@@ -40,9 +40,9 @@
private static final MdnsLogger LOGGER = new MdnsLogger("MdnsSocket");
static final int INTERFACE_INDEX_UNSPECIFIED = -1;
- protected static final InetSocketAddress MULTICAST_IPV4_ADDRESS =
+ public static final InetSocketAddress MULTICAST_IPV4_ADDRESS =
new InetSocketAddress(MdnsConstants.getMdnsIPv4Address(), MdnsConstants.MDNS_PORT);
- protected static final InetSocketAddress MULTICAST_IPV6_ADDRESS =
+ public static final InetSocketAddress MULTICAST_IPV6_ADDRESS =
new InetSocketAddress(MdnsConstants.getMdnsIPv6Address(), MdnsConstants.MDNS_PORT);
private final MulticastNetworkInterfaceProvider multicastNetworkInterfaceProvider;
private final MulticastSocket multicastSocket;
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsSocketClient.java b/service-t/src/com/android/server/mdns/MdnsSocketClient.java
similarity index 98%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsSocketClient.java
rename to service-t/src/com/android/server/mdns/MdnsSocketClient.java
index 6a321d1..907687e 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsSocketClient.java
+++ b/service-t/src/com/android/server/mdns/MdnsSocketClient.java
@@ -16,6 +16,8 @@
package com.android.server.connectivity.mdns;
+import static com.android.server.connectivity.mdns.MdnsSocketClientBase.Callback;
+
import android.Manifest.permission;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -47,7 +49,7 @@
*
* <p>See https://tools.ietf.org/html/rfc6763 (namely sections 4 and 5).
*/
-public class MdnsSocketClient {
+public class MdnsSocketClient implements MdnsSocketClientBase {
private static final String TAG = "MdnsClient";
// TODO: The following values are copied from cast module. We need to think about the
@@ -116,11 +118,13 @@
}
}
+ @Override
public synchronized void setCallback(@Nullable Callback callback) {
this.callback = callback;
}
@RequiresPermission(permission.CHANGE_WIFI_MULTICAST_STATE)
+ @Override
public synchronized void startDiscovery() throws IOException {
if (multicastSocket != null) {
LOGGER.w("Discovery is already in progress.");
@@ -160,6 +164,7 @@
}
@RequiresPermission(permission.CHANGE_WIFI_MULTICAST_STATE)
+ @Override
public void stopDiscovery() {
LOGGER.log("Stop discovery.");
if (multicastSocket == null && unicastSocket == null) {
@@ -195,11 +200,13 @@
}
/** Sends a mDNS request packet that asks for multicast response. */
+ @Override
public void sendMulticastPacket(@NonNull DatagramPacket packet) {
sendMdnsPacket(packet, multicastPacketQueue);
}
/** Sends a mDNS request packet that asks for unicast response. */
+ @Override
public void sendUnicastPacket(DatagramPacket packet) {
if (useSeparateSocketForUnicast) {
sendMdnsPacket(packet, unicastPacketQueue);
@@ -512,11 +519,4 @@
public boolean isOnIPv6OnlyNetwork() {
return multicastSocket != null && multicastSocket.isOnIPv6OnlyNetwork();
}
-
- /** Callback for {@link MdnsSocketClient}. */
- public interface Callback {
- void onResponseReceived(@NonNull MdnsResponse response);
-
- void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode);
- }
}
\ No newline at end of file
diff --git a/service-t/src/com/android/server/mdns/MdnsSocketClientBase.java b/service-t/src/com/android/server/mdns/MdnsSocketClientBase.java
new file mode 100644
index 0000000..23504a0
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MdnsSocketClientBase.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2021 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 com.android.server.connectivity.mdns;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.net.Network;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+
+/**
+ * Base class for multicast socket client.
+ *
+ * @hide
+ */
+public interface MdnsSocketClientBase {
+ /*** Start mDns discovery on given network. */
+ default void startDiscovery() throws IOException { }
+
+ /*** Stop mDns discovery. */
+ default void stopDiscovery() { }
+
+ /*** Set callback for receiving mDns response */
+ void setCallback(@Nullable Callback callback);
+
+ /*** Sends a mDNS request packet that asks for multicast response. */
+ void sendMulticastPacket(@NonNull DatagramPacket packet);
+
+ /**
+ * Sends a mDNS request packet via given network that asks for multicast response. Null network
+ * means sending packet via all networks.
+ */
+ default void sendMulticastPacket(@NonNull DatagramPacket packet, @Nullable Network network) {
+ throw new UnsupportedOperationException(
+ "This socket client doesn't support per-network sending");
+ }
+
+ /*** Sends a mDNS request packet that asks for unicast response. */
+ void sendUnicastPacket(@NonNull DatagramPacket packet);
+
+ /**
+ * Sends a mDNS request packet via given network that asks for unicast response. Null network
+ * means sending packet via all networks.
+ */
+ default void sendUnicastPacket(@NonNull DatagramPacket packet, @Nullable Network network) {
+ throw new UnsupportedOperationException(
+ "This socket client doesn't support per-network sending");
+ }
+
+ /*** Notify that the given network is requested for mdns discovery / resolution */
+ default void notifyNetworkRequested(@NonNull MdnsServiceBrowserListener listener,
+ @Nullable Network network) { }
+
+ /*** Notify that the network is unrequested */
+ default void notifyNetworkUnrequested(@NonNull MdnsServiceBrowserListener listener) { }
+
+ /*** Callback for mdns response */
+ interface Callback {
+ /*** Receive a mdns response */
+ void onResponseReceived(@NonNull MdnsResponse response);
+
+ /*** Parse a mdns response failed */
+ void onFailedToParseMdnsResponse(int receivedPacketNumber, int errorCode);
+ }
+}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsSocketProvider.java b/service-t/src/com/android/server/mdns/MdnsSocketProvider.java
similarity index 85%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsSocketProvider.java
rename to service-t/src/com/android/server/mdns/MdnsSocketProvider.java
index b8c324e..9298852 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsSocketProvider.java
+++ b/service-t/src/com/android/server/mdns/MdnsSocketProvider.java
@@ -35,6 +35,7 @@
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
+import com.android.net.module.util.CollectionUtils;
import com.android.net.module.util.LinkPropertiesUtils.CompareResult;
import com.android.net.module.util.ip.NetlinkMonitor;
import com.android.net.module.util.netlink.NetlinkConstants;
@@ -42,7 +43,6 @@
import com.android.server.connectivity.mdns.util.MdnsLogger;
import java.io.IOException;
-import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
@@ -60,8 +60,13 @@
public class MdnsSocketProvider {
private static final String TAG = MdnsSocketProvider.class.getSimpleName();
private static final boolean DBG = MdnsDiscoveryManager.DBG;
+ // This buffer size matches what MdnsSocketClient uses currently.
+ // But 1440 should generally be enough because of standard Ethernet.
+ // Note: mdnsresponder mDNSEmbeddedAPI.h uses 8940 for Ethernet jumbo frames.
+ private static final int READ_BUFFER_SIZE = 2048;
private static final MdnsLogger LOGGER = new MdnsLogger(TAG);
@NonNull private final Context mContext;
+ @NonNull private final Looper mLooper;
@NonNull private final Handler mHandler;
@NonNull private final Dependencies mDependencies;
@NonNull private final NetworkCallback mNetworkCallback;
@@ -75,6 +80,7 @@
new ArrayMap<>();
private final List<String> mLocalOnlyInterfaces = new ArrayList<>();
private final List<String> mTetheredInterfaces = new ArrayList<>();
+ private final byte[] mPacketReadBuffer = new byte[READ_BUFFER_SIZE];
private boolean mMonitoringSockets = false;
public MdnsSocketProvider(@NonNull Context context, @NonNull Looper looper) {
@@ -84,6 +90,7 @@
MdnsSocketProvider(@NonNull Context context, @NonNull Looper looper,
@NonNull Dependencies deps) {
mContext = context;
+ mLooper = looper;
mHandler = new Handler(looper);
mDependencies = deps;
mNetworkCallback = new NetworkCallback() {
@@ -119,32 +126,33 @@
@VisibleForTesting
public static class Dependencies {
/*** Get network interface by given interface name */
- public NetworkInterfaceWrapper getNetworkInterfaceByName(String interfaceName)
+ public NetworkInterfaceWrapper getNetworkInterfaceByName(@NonNull String interfaceName)
throws SocketException {
final NetworkInterface ni = NetworkInterface.getByName(interfaceName);
return ni == null ? null : new NetworkInterfaceWrapper(ni);
}
/*** Check whether given network interface can support mdns */
- public boolean canScanOnInterface(NetworkInterfaceWrapper networkInterface) {
+ public boolean canScanOnInterface(@NonNull NetworkInterfaceWrapper networkInterface) {
return MulticastNetworkInterfaceProvider.canScanOnInterface(networkInterface);
}
/*** Create a MdnsInterfaceSocket */
- public MdnsInterfaceSocket createMdnsInterfaceSocket(NetworkInterface networkInterface,
- int port) throws IOException {
- return new MdnsInterfaceSocket(networkInterface, port);
+ public MdnsInterfaceSocket createMdnsInterfaceSocket(
+ @NonNull NetworkInterface networkInterface, int port, @NonNull Looper looper,
+ @NonNull byte[] packetReadBuffer) throws IOException {
+ return new MdnsInterfaceSocket(networkInterface, port, looper, packetReadBuffer);
}
}
/*** Data class for storing socket related info */
private static class SocketInfo {
final MdnsInterfaceSocket mSocket;
- final List<LinkAddress> mAddresses = new ArrayList<>();
+ final List<LinkAddress> mAddresses;
SocketInfo(MdnsInterfaceSocket socket, List<LinkAddress> addresses) {
mSocket = socket;
- mAddresses.addAll(addresses);
+ mAddresses = new ArrayList<>(addresses);
}
}
@@ -160,8 +168,9 @@
}
}
- private void ensureRunningOnHandlerThread() {
- if (mHandler.getLooper().getThread() != Thread.currentThread()) {
+ /*** Ensure that current running thread is same as given handler thread */
+ public static void ensureRunningOnHandlerThread(Handler handler) {
+ if (handler.getLooper().getThread() != Thread.currentThread()) {
throw new IllegalStateException(
"Not running on Handler thread: " + Thread.currentThread().getName());
}
@@ -169,7 +178,7 @@
/*** Start monitoring sockets by listening callbacks for sockets creation or removal */
public void startMonitoringSockets() {
- ensureRunningOnHandlerThread();
+ ensureRunningOnHandlerThread(mHandler);
if (mMonitoringSockets) {
Log.d(TAG, "Already monitoring sockets.");
return;
@@ -188,7 +197,7 @@
/*** Stop monitoring sockets and unregister callbacks */
public void stopMonitoringSockets() {
- ensureRunningOnHandlerThread();
+ ensureRunningOnHandlerThread(mHandler);
if (!mMonitoringSockets) {
Log.d(TAG, "Monitoring sockets hasn't been started.");
return;
@@ -204,19 +213,15 @@
mMonitoringSockets = false;
}
- private static boolean isNetworkMatched(@Nullable Network targetNetwork,
+ /*** Check whether the target network is matched current network */
+ public static boolean isNetworkMatched(@Nullable Network targetNetwork,
@NonNull Network currentNetwork) {
return targetNetwork == null || targetNetwork.equals(currentNetwork);
}
private boolean matchRequestedNetwork(Network network) {
- for (int i = 0; i < mCallbacksToRequestedNetworks.size(); i++) {
- final Network requestedNetwork = mCallbacksToRequestedNetworks.valueAt(i);
- if (isNetworkMatched(requestedNetwork, network)) {
- return true;
- }
- }
- return false;
+ return hasAllNetworksRequest()
+ || mCallbacksToRequestedNetworks.containsValue(network);
}
private boolean hasAllNetworksRequest() {
@@ -244,7 +249,7 @@
// Try to join the group again.
socketInfo.mSocket.joinGroup(addresses);
- notifyAddressesChanged(network, lp);
+ notifyAddressesChanged(network, socketInfo.mSocket, lp);
}
}
@@ -279,15 +284,6 @@
current.addAll(updated);
}
- private static List<LinkAddress> getLinkAddressFromNetworkInterface(
- NetworkInterfaceWrapper networkInterface) {
- List<LinkAddress> addresses = new ArrayList<>();
- for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
- addresses.add(new LinkAddress(address));
- }
- return addresses;
- }
-
private void createSocket(Network network, LinkProperties lp) {
final String interfaceName = lp.getInterfaceName();
if (interfaceName == null) {
@@ -307,10 +303,12 @@
+ " with interfaceName:" + interfaceName);
}
final MdnsInterfaceSocket socket = mDependencies.createMdnsInterfaceSocket(
- networkInterface.getNetworkInterface(), MdnsConstants.MDNS_PORT);
+ networkInterface.getNetworkInterface(), MdnsConstants.MDNS_PORT, mLooper,
+ mPacketReadBuffer);
final List<LinkAddress> addresses;
if (network.netId == INetd.LOCAL_NET_ID) {
- addresses = getLinkAddressFromNetworkInterface(networkInterface);
+ addresses = CollectionUtils.map(
+ networkInterface.getInterfaceAddresses(), LinkAddress::new);
mTetherInterfaceSockets.put(interfaceName, new SocketInfo(socket, addresses));
} else {
addresses = lp.getLinkAddresses();
@@ -355,12 +353,13 @@
}
}
- private void notifyAddressesChanged(Network network, LinkProperties lp) {
+ private void notifyAddressesChanged(Network network, MdnsInterfaceSocket socket,
+ LinkProperties lp) {
for (int i = 0; i < mCallbacksToRequestedNetworks.size(); i++) {
final Network requestedNetwork = mCallbacksToRequestedNetworks.valueAt(i);
if (isNetworkMatched(requestedNetwork, network)) {
mCallbacksToRequestedNetworks.keyAt(i)
- .onAddressesChanged(network, lp.getLinkAddresses());
+ .onAddressesChanged(network, socket, lp.getLinkAddresses());
}
}
}
@@ -401,7 +400,7 @@
* @param cb the callback to listen the socket creation.
*/
public void requestSocket(@Nullable Network network, @NonNull SocketCallback cb) {
- ensureRunningOnHandlerThread();
+ ensureRunningOnHandlerThread(mHandler);
mCallbacksToRequestedNetworks.put(cb, network);
if (network == null) {
// Does not specify a required network, create sockets for all possible
@@ -424,7 +423,7 @@
/*** Unrequest the socket */
public void unrequestSocket(@NonNull SocketCallback cb) {
- ensureRunningOnHandlerThread();
+ ensureRunningOnHandlerThread(mHandler);
mCallbacksToRequestedNetworks.remove(cb);
if (hasAllNetworksRequest()) {
// Still has a request for all networks (interfaces).
@@ -433,16 +432,24 @@
// Check if remaining requests are matched any of sockets.
for (int i = mNetworkSockets.size() - 1; i >= 0; i--) {
- if (matchRequestedNetwork(mNetworkSockets.keyAt(i))) continue;
- mNetworkSockets.removeAt(i).mSocket.destroy();
+ final Network network = mNetworkSockets.keyAt(i);
+ if (matchRequestedNetwork(network)) continue;
+ final SocketInfo info = mNetworkSockets.removeAt(i);
+ info.mSocket.destroy();
+ // Still notify to unrequester for socket destroy.
+ cb.onInterfaceDestroyed(network, info.mSocket);
}
// Remove all sockets for tethering interface because these sockets do not have associated
// networks, and they should invoke by a request for all networks (interfaces). If there is
// no such request, the sockets for tethering interface should be removed.
for (int i = mTetherInterfaceSockets.size() - 1; i >= 0; i--) {
- mTetherInterfaceSockets.removeAt(i).mSocket.destroy();
+ final SocketInfo info = mTetherInterfaceSockets.valueAt(i);
+ info.mSocket.destroy();
+ // Still notify to unrequester for socket destroy.
+ cb.onInterfaceDestroyed(new Network(INetd.LOCAL_NET_ID), info.mSocket);
}
+ mTetherInterfaceSockets.clear();
}
/*** Callbacks for listening socket changes */
@@ -455,6 +462,6 @@
@NonNull MdnsInterfaceSocket socket) {}
/*** Notify the addresses is changed on the network */
default void onAddressesChanged(@NonNull Network network,
- @NonNull List<LinkAddress> addresses) {}
+ @NonNull MdnsInterfaceSocket socket, @NonNull List<LinkAddress> addresses) {}
}
}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsTextRecord.java b/service-t/src/com/android/server/mdns/MdnsTextRecord.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsTextRecord.java
rename to service-t/src/com/android/server/mdns/MdnsTextRecord.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/MulticastNetworkInterfaceProvider.java b/service-t/src/com/android/server/mdns/MulticastNetworkInterfaceProvider.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/MulticastNetworkInterfaceProvider.java
rename to service-t/src/com/android/server/mdns/MulticastNetworkInterfaceProvider.java
diff --git a/service-t/src/com/android/server/mdns/MulticastPacketReader.java b/service-t/src/com/android/server/mdns/MulticastPacketReader.java
new file mode 100644
index 0000000..b597f0a
--- /dev/null
+++ b/service-t/src/com/android/server/mdns/MulticastPacketReader.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import static com.android.server.connectivity.mdns.MdnsSocketProvider.ensureRunningOnHandlerThread;
+
+import android.annotation.NonNull;
+import android.os.Handler;
+import android.os.ParcelFileDescriptor;
+import android.system.Os;
+import android.util.ArraySet;
+
+import com.android.net.module.util.FdEventsReader;
+
+import java.io.FileDescriptor;
+import java.net.InetSocketAddress;
+import java.util.Set;
+
+/** Simple reader for mDNS packets. */
+public class MulticastPacketReader extends FdEventsReader<MulticastPacketReader.RecvBuffer> {
+ @NonNull
+ private final String mLogTag;
+ @NonNull
+ private final ParcelFileDescriptor mSocket;
+ @NonNull
+ private final Handler mHandler;
+ @NonNull
+ private final Set<PacketHandler> mPacketHandlers = new ArraySet<>();
+
+ interface PacketHandler {
+ void handlePacket(byte[] recvbuf, int length, InetSocketAddress src);
+ }
+
+ public static final class RecvBuffer {
+ final byte[] data;
+ final InetSocketAddress src;
+
+ private RecvBuffer(byte[] data, InetSocketAddress src) {
+ this.data = data;
+ this.src = src;
+ }
+ }
+
+ /**
+ * Create a new {@link MulticastPacketReader}.
+ * @param socket Socket to read from. This will *not* be closed when the reader terminates.
+ * @param buffer Buffer to read packets into. Will only be used from the handler thread.
+ */
+ protected MulticastPacketReader(@NonNull String interfaceTag,
+ @NonNull ParcelFileDescriptor socket, @NonNull Handler handler,
+ @NonNull byte[] buffer) {
+ super(handler, new RecvBuffer(buffer, new InetSocketAddress()));
+ mLogTag = MulticastPacketReader.class.getSimpleName() + "/" + interfaceTag;
+ mSocket = socket;
+ mHandler = handler;
+ }
+
+ @Override
+ protected int recvBufSize(@NonNull RecvBuffer buffer) {
+ return buffer.data.length;
+ }
+
+ @Override
+ protected FileDescriptor createFd() {
+ // Keep a reference to the PFD as it would close the fd in its finalizer otherwise
+ return mSocket.getFileDescriptor();
+ }
+
+ @Override
+ protected void onStop() {
+ // Do nothing (do not close the FD)
+ }
+
+ @Override
+ protected int readPacket(@NonNull FileDescriptor fd, @NonNull RecvBuffer buffer)
+ throws Exception {
+ return Os.recvfrom(
+ fd, buffer.data, 0, buffer.data.length, 0 /* flags */, buffer.src);
+ }
+
+ @Override
+ protected void handlePacket(@NonNull RecvBuffer recvbuf, int length) {
+ for (PacketHandler handler : mPacketHandlers) {
+ handler.handlePacket(recvbuf.data, length, recvbuf.src);
+ }
+ }
+
+ /**
+ * Add a packet handler to deal with received packets. If the handler is already set,
+ * this is a no-op.
+ */
+ public void addPacketHandler(@NonNull PacketHandler handler) {
+ ensureRunningOnHandlerThread(mHandler);
+ mPacketHandlers.add(handler);
+ }
+
+ /**
+ * Remove a packet handler added via {@link #addPacketHandler}. If the handler was not set,
+ * this is a no-op.
+ */
+ public void removePacketHandler(@NonNull PacketHandler handler) {
+ ensureRunningOnHandlerThread(mHandler);
+ mPacketHandlers.remove(handler);
+ }
+}
+
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsAdvertiser.java b/service-t/src/com/android/server/mdns/NameConflictException.java
similarity index 64%
rename from service/mdns/com/android/server/connectivity/mdns/MdnsAdvertiser.java
rename to service-t/src/com/android/server/mdns/NameConflictException.java
index dee78fd..c123d02 100644
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsAdvertiser.java
+++ b/service-t/src/com/android/server/mdns/NameConflictException.java
@@ -16,14 +16,15 @@
package com.android.server.connectivity.mdns;
-import android.util.Log;
-
/**
- * MdnsAdvertiser manages advertising services per {@link com.android.server.NsdService} requests.
- *
- * TODO: implement
+ * An exception thrown when a service name conflicts with an existing service.
*/
-public class MdnsAdvertiser {
- private static final String TAG = MdnsAdvertiser.class.getSimpleName();
- public static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
+public class NameConflictException extends Exception {
+ /**
+ * ID of the existing service that conflicted.
+ */
+ public final int conflictingServiceId;
+ public NameConflictException(int conflictingServiceId) {
+ this.conflictingServiceId = conflictingServiceId;
+ }
}
diff --git a/service/mdns/com/android/server/connectivity/mdns/NetworkInterfaceWrapper.java b/service-t/src/com/android/server/mdns/NetworkInterfaceWrapper.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/NetworkInterfaceWrapper.java
rename to service-t/src/com/android/server/mdns/NetworkInterfaceWrapper.java
diff --git a/service/mdns/com/android/server/connectivity/mdns/util/MdnsLogger.java b/service-t/src/com/android/server/mdns/util/MdnsLogger.java
similarity index 100%
rename from service/mdns/com/android/server/connectivity/mdns/util/MdnsLogger.java
rename to service-t/src/com/android/server/mdns/util/MdnsLogger.java
diff --git a/service-t/src/com/android/server/net/NetworkStatsFactory.java b/service-t/src/com/android/server/net/NetworkStatsFactory.java
index 8161f50..e0abdf1 100644
--- a/service-t/src/com/android/server/net/NetworkStatsFactory.java
+++ b/service-t/src/com/android/server/net/NetworkStatsFactory.java
@@ -200,16 +200,6 @@
}
/**
- * Parse and return interface-level summary {@link NetworkStats} measured
- * using {@code /proc/net/dev} style hooks, which may include non IP layer
- * traffic. Values monotonically increase since device boot, and may include
- * details about inactive interfaces.
- */
- public NetworkStats readNetworkStatsSummaryDev() throws IOException {
- return mDeps.getNetworkStatsDev();
- }
-
- /**
* Parse and return interface-level summary {@link NetworkStats}. Designed
* to return only IP layer traffic. Values monotonically increase since
* device boot, and may include details about inactive interfaces.
diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java
index cf53002..5852a30 100644
--- a/service-t/src/com/android/server/net/NetworkStatsService.java
+++ b/service-t/src/com/android/server/net/NetworkStatsService.java
@@ -69,6 +69,7 @@
import android.annotation.Nullable;
import android.annotation.TargetApi;
import android.app.AlarmManager;
+import android.app.BroadcastOptions;
import android.app.PendingIntent;
import android.app.usage.NetworkStatsManager;
import android.content.ApexEnvironment;
@@ -114,6 +115,7 @@
import android.net.netstats.provider.NetworkStatsProvider;
import android.os.Binder;
import android.os.Build;
+import android.os.Bundle;
import android.os.DropBoxManager;
import android.os.Environment;
import android.os.Handler;
@@ -149,6 +151,7 @@
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.FileRotator;
+import com.android.modules.utils.build.SdkLevel;
import com.android.net.module.util.BaseNetdUnsolicitedEventListener;
import com.android.net.module.util.BestClock;
import com.android.net.module.util.BinderUtils;
@@ -166,6 +169,9 @@
import com.android.net.module.util.Struct.U8;
import com.android.net.module.util.bpf.CookieTagMapKey;
import com.android.net.module.util.bpf.CookieTagMapValue;
+import com.android.networkstack.apishim.BroadcastOptionsShimImpl;
+import com.android.networkstack.apishim.ConstantsShim;
+import com.android.networkstack.apishim.common.UnsupportedApiLevelException;
import com.android.server.BpfNetMaps;
import java.io.File;
@@ -333,13 +339,11 @@
}
}
- Config getDevConfig();
Config getXtConfig();
Config getUidConfig();
Config getUidTagConfig();
long getGlobalAlertBytes(long def);
- long getDevPersistBytes(long def);
long getXtPersistBytes(long def);
long getUidPersistBytes(long def);
long getUidTagPersistBytes(long def);
@@ -389,8 +393,6 @@
private final Semaphore mStatsProviderSem = new Semaphore(0, true);
@GuardedBy("mStatsLock")
- private NetworkStatsRecorder mDevRecorder;
- @GuardedBy("mStatsLock")
private NetworkStatsRecorder mXtRecorder;
@GuardedBy("mStatsLock")
private NetworkStatsRecorder mUidRecorder;
@@ -526,8 +528,22 @@
case MSG_BROADCAST_NETWORK_STATS_UPDATED: {
final Intent updatedIntent = new Intent(ACTION_NETWORK_STATS_UPDATED);
updatedIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
+ Bundle opts = null;
+ if (SdkLevel.isAtLeastU()) {
+ try {
+ // This allows us to discard older broadcasts still waiting to
+ // be delivered.
+ opts = BroadcastOptionsShimImpl.newInstance(
+ BroadcastOptions.makeBasic())
+ .setDeliveryGroupPolicy(
+ ConstantsShim.DELIVERY_GROUP_POLICY_MOST_RECENT)
+ .toBundle();
+ } catch (UnsupportedApiLevelException e) {
+ Log.wtf(TAG, "Using unsupported API" + e);
+ }
+ }
mContext.sendBroadcastAsUser(updatedIntent, UserHandle.ALL,
- READ_NETWORK_USAGE_HISTORY);
+ READ_NETWORK_USAGE_HISTORY, opts);
break;
}
}
@@ -844,8 +860,6 @@
mSystemReady = true;
// create data recorders along with historical rotators
- mDevRecorder = buildRecorder(PREFIX_DEV, mSettings.getDevConfig(), false, mStatsDir,
- true /* wipeOnError */);
mXtRecorder = buildRecorder(PREFIX_XT, mSettings.getXtConfig(), false, mStatsDir,
true /* wipeOnError */);
mUidRecorder = buildRecorder(PREFIX_UID, mSettings.getUidConfig(), false, mStatsDir,
@@ -943,7 +957,6 @@
final long currentTime = mClock.millis();
// persist any pending stats
- mDevRecorder.forcePersistLocked(currentTime);
mXtRecorder.forcePersistLocked(currentTime);
mUidRecorder.forcePersistLocked(currentTime);
mUidTagRecorder.forcePersistLocked(currentTime);
@@ -1010,8 +1023,17 @@
Log.i(TAG, "Starting import : attempts " + attempts + "/" + targetAttempts);
}
+ // Still create a legacy dev recorder locally but the service doesn't really use it.
+ // This is for backward compatibility where the OEMs might call readPlatformCollection to
+ // perform proprietary operations and relying on the side-effects to complete the follow-up
+ // import process.
+ final NetworkStatsSettings.Config devConfig =
+ new NetworkStatsSettings.Config(HOUR_IN_MILLIS,
+ 15 * DAY_IN_MILLIS, 90 * DAY_IN_MILLIS);
+ final NetworkStatsRecorder devRecorder = buildRecorder(PREFIX_DEV, devConfig,
+ false, mStatsDir, true /* wipeOnError */);
final MigrationInfo[] migrations = new MigrationInfo[]{
- new MigrationInfo(mDevRecorder), new MigrationInfo(mXtRecorder),
+ new MigrationInfo(devRecorder), new MigrationInfo(mXtRecorder),
new MigrationInfo(mUidRecorder), new MigrationInfo(mUidTagRecorder)
};
@@ -1021,9 +1043,10 @@
final File legacyBaseDir = mDeps.getLegacyStatsDir();
// Set wipeOnError flag false so the recorder won't damage persistent data if reads
// failed and calling deleteAll.
+ // Set DEV legacy recorder as null since the DEV recorder has been removed.
+ // Thus it doesn't need to build DEV legacy recorder for comparing with imported data.
legacyRecorders = new NetworkStatsRecorder[]{
- buildRecorder(PREFIX_DEV, mSettings.getDevConfig(), false, legacyBaseDir,
- false /* wipeOnError */),
+ null /* dev Recorder */,
buildRecorder(PREFIX_XT, mSettings.getXtConfig(), false, legacyBaseDir,
false /* wipeOnError */),
buildRecorder(PREFIX_UID, mSettings.getUidConfig(), false, legacyBaseDir,
@@ -1040,7 +1063,6 @@
// commit any data to disk until all are read.
for (int i = 0; i < migrations.length; i++) {
final MigrationInfo migration = migrations[i];
-
// Read the collection from platform code, and set fallbacks counter if throws
// for better debugging.
try {
@@ -1074,6 +1096,7 @@
// Find the latest end time.
for (final MigrationInfo migration : migrations) {
+ if (PREFIX_DEV.equals(migration.recorder.getCookie())) continue;
final long migrationEnd = migration.collection.getEndMillis();
if (migrationEnd > migrationEndTime) migrationEndTime = migrationEnd;
}
@@ -1090,7 +1113,8 @@
for (final MigrationInfo migration : migrations) {
migration.imported = true;
migration.recorder.removeDataBefore(migrationEndTime);
- if (migration.collection.isEmpty()) continue;
+ if (migration.collection.isEmpty()
+ || PREFIX_DEV.equals(migration.recorder.getCookie())) continue;
migration.recorder.importCollectionLocked(migration.collection);
}
@@ -1113,6 +1137,7 @@
if (migrationEndTime > Long.MIN_VALUE) {
try {
for (final MigrationInfo migration : migrations) {
+ if (PREFIX_DEV.equals(migration.recorder.getCookie())) continue;
if (migration.imported) {
migration.recorder.removeDataBefore(migrationEndTime);
}
@@ -1123,6 +1148,7 @@
// framework will reboot, and if there are remaining tries, the migration
// process will retry, which is fine because it's idempotent.
for (final MigrationInfo migration : migrations) {
+ if (PREFIX_DEV.equals(migration.recorder.getCookie())) continue;
migration.recorder.recoverAndDeleteData();
}
}
@@ -1176,11 +1202,14 @@
/**
* Compare imported data with the data returned by legacy recorders.
*
- * @return true if the data matches, false if the data does not match or throw with exceptions.
+ * @return true if the data matches or if {@code legacyRecorder} is null, false if the data
+ * does not match or throw with exceptions.
*/
private boolean compareImportedToLegacyStats(@NonNull MigrationInfo migration,
- @NonNull NetworkStatsRecorder legacyRecorder) {
+ @Nullable NetworkStatsRecorder legacyRecorder) {
final NetworkStatsCollection legacyStats;
+ // Skip the recorder that doesn't need to be compared.
+ if (legacyRecorder == null) return true;
try {
legacyStats = legacyRecorder.getOrLoadCompleteLocked();
} catch (Throwable e) {
@@ -1834,7 +1863,6 @@
updatePersistThresholdsLocked();
- mDevRecorder.maybePersistLocked(currentTime);
mXtRecorder.maybePersistLocked(currentTime);
mUidRecorder.maybePersistLocked(currentTime);
mUidTagRecorder.maybePersistLocked(currentTime);
@@ -1950,7 +1978,6 @@
*/
@GuardedBy("mStatsLock")
private void updatePersistThresholdsLocked() {
- mDevRecorder.setPersistThreshold(mSettings.getDevPersistBytes(mPersistThreshold));
mXtRecorder.setPersistThreshold(mSettings.getXtPersistBytes(mPersistThreshold));
mUidRecorder.setPersistThreshold(mSettings.getUidPersistBytes(mPersistThreshold));
mUidTagRecorder.setPersistThreshold(mSettings.getUidTagPersistBytes(mPersistThreshold));
@@ -2230,30 +2257,23 @@
@GuardedBy("mStatsLock")
private void recordSnapshotLocked(long currentTime) throws RemoteException {
// snapshot and record current counters; read UID stats first to
- // avoid over counting dev stats.
+ // avoid over counting xt stats.
Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotUid");
final NetworkStats uidSnapshot = getNetworkStatsUidDetail(INTERFACES_ALL);
Trace.traceEnd(TRACE_TAG_NETWORK);
Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotXt");
final NetworkStats xtSnapshot = readNetworkStatsSummaryXt();
Trace.traceEnd(TRACE_TAG_NETWORK);
- Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotDev");
- final NetworkStats devSnapshot = readNetworkStatsSummaryDev();
- Trace.traceEnd(TRACE_TAG_NETWORK);
- // Snapshot for dev/xt stats from all custom stats providers. Counts per-interface data
- // from stats providers that isn't already counted by dev and XT stats.
+ // Snapshot for xt stats from all custom stats providers. Counts per-interface data
+ // from stats providers that isn't already counted by XT stats.
Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotStatsProvider");
final NetworkStats providersnapshot = getNetworkStatsFromProviders(STATS_PER_IFACE);
Trace.traceEnd(TRACE_TAG_NETWORK);
xtSnapshot.combineAllValues(providersnapshot);
- devSnapshot.combineAllValues(providersnapshot);
- // For xt/dev, we pass a null VPN array because usage is aggregated by UID, so VPN traffic
+ // For xt, we pass a null VPN array because usage is aggregated by UID, so VPN traffic
// can't be reattributed to responsible apps.
- Trace.traceBegin(TRACE_TAG_NETWORK, "recordDev");
- mDevRecorder.recordSnapshotLocked(devSnapshot, mActiveIfaces, currentTime);
- Trace.traceEnd(TRACE_TAG_NETWORK);
Trace.traceBegin(TRACE_TAG_NETWORK, "recordXt");
mXtRecorder.recordSnapshotLocked(xtSnapshot, mActiveIfaces, currentTime);
Trace.traceEnd(TRACE_TAG_NETWORK);
@@ -2333,13 +2353,11 @@
// persist any pending data depending on requested flags
Trace.traceBegin(TRACE_TAG_NETWORK, "[persisting]");
if (persistForce) {
- mDevRecorder.forcePersistLocked(currentTime);
mXtRecorder.forcePersistLocked(currentTime);
mUidRecorder.forcePersistLocked(currentTime);
mUidTagRecorder.forcePersistLocked(currentTime);
} else {
if (persistNetwork) {
- mDevRecorder.maybePersistLocked(currentTime);
mXtRecorder.maybePersistLocked(currentTime);
}
if (persistUid) {
@@ -2396,30 +2414,25 @@
final long currentTime = mClock.millis();
NetworkTemplate template;
- NetworkStats.Entry devTotal;
NetworkStats.Entry xtTotal;
NetworkStats.Entry uidTotal;
// collect mobile sample
template = new NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES).build();
- devTotal = mDevRecorder.getTotalSinceBootLocked(template);
xtTotal = mXtRecorder.getTotalSinceBootLocked(template);
uidTotal = mUidRecorder.getTotalSinceBootLocked(template);
EventLog.writeEvent(LOG_TAG_NETSTATS_MOBILE_SAMPLE,
- devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets,
xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets,
uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets,
currentTime);
// collect wifi sample
template = new NetworkTemplate.Builder(MATCH_WIFI).build();
- devTotal = mDevRecorder.getTotalSinceBootLocked(template);
xtTotal = mXtRecorder.getTotalSinceBootLocked(template);
uidTotal = mUidRecorder.getTotalSinceBootLocked(template);
EventLog.writeEvent(LOG_TAG_NETSTATS_WIFI_SAMPLE,
- devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets,
xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets,
uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets,
currentTime);
@@ -2721,7 +2734,12 @@
pw.println("Dev stats:");
pw.increaseIndent();
- mDevRecorder.dumpLocked(pw, fullHistory);
+ pw.println("Pending bytes: ");
+ if (fullHistory) {
+ pw.println("Complete history:");
+ } else {
+ pw.println("History since boot:");
+ }
pw.decreaseIndent();
pw.println("Xt stats:");
@@ -2781,7 +2799,6 @@
mActiveIfaces);
dumpInterfaces(proto, NetworkStatsServiceDumpProto.ACTIVE_UID_INTERFACES,
mActiveUidIfaces);
- mDevRecorder.dumpDebugLocked(proto, NetworkStatsServiceDumpProto.DEV_STATS);
mXtRecorder.dumpDebugLocked(proto, NetworkStatsServiceDumpProto.XT_STATS);
mUidRecorder.dumpDebugLocked(proto, NetworkStatsServiceDumpProto.UID_STATS);
mUidTagRecorder.dumpDebugLocked(proto,
@@ -2797,7 +2814,7 @@
return;
}
if (map.isEmpty()) {
- pw.println("No entries");
+ pw.println("");
return;
}
// If there is a concurrent entry deletion, value could be null. http://b/220084230.
@@ -2916,14 +2933,6 @@
});
}
- private NetworkStats readNetworkStatsSummaryDev() {
- try {
- return mStatsFactory.readNetworkStatsSummaryDev();
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
-
private NetworkStats readNetworkStatsSummaryXt() {
try {
return mStatsFactory.readNetworkStatsSummaryXt();
@@ -3232,12 +3241,8 @@
return false;
}
@Override
- public Config getDevConfig() {
- return new Config(HOUR_IN_MILLIS, 15 * DAY_IN_MILLIS, 90 * DAY_IN_MILLIS);
- }
- @Override
public Config getXtConfig() {
- return getDevConfig();
+ return new Config(HOUR_IN_MILLIS, 15 * DAY_IN_MILLIS, 90 * DAY_IN_MILLIS);
}
@Override
public Config getUidConfig() {
@@ -3248,10 +3253,6 @@
return new Config(2 * HOUR_IN_MILLIS, 5 * DAY_IN_MILLIS, 15 * DAY_IN_MILLIS);
}
@Override
- public long getDevPersistBytes(long def) {
- return def;
- }
- @Override
public long getXtPersistBytes(long def) {
return def;
}
diff --git a/service/Android.bp b/service/Android.bp
index 224fa19..c8d2fdd 100644
--- a/service/Android.bp
+++ b/service/Android.bp
@@ -195,26 +195,6 @@
],
}
-// TODO: Remove this temporary library and put code into module when test coverage is enough.
-java_library {
- name: "service-mdns",
- sdk_version: "system_server_current",
- min_sdk_version: "30",
- srcs: [
- "mdns/**/*.java",
- ],
- libs: [
- "framework-annotations-lib",
- "framework-connectivity-pre-jarjar",
- "framework-tethering",
- "framework-wifi",
- "service-connectivity-pre-jarjar",
- ],
- visibility: [
- "//packages/modules/Connectivity/tests:__subpackages__",
- ],
-}
-
java_library {
name: "service-connectivity-protos",
sdk_version: "system_current",
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsPacket.java b/service/mdns/com/android/server/connectivity/mdns/MdnsPacket.java
deleted file mode 100644
index eae084a..0000000
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsPacket.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
-
-import java.util.Collections;
-import java.util.List;
-
-/**
- * A class holding data that can be included in a mDNS packet.
- */
-public class MdnsPacket {
- public final int flags;
- public final List<MdnsRecord> questions;
- public final List<MdnsRecord> answers;
- public final List<MdnsRecord> authorityRecords;
- public final List<MdnsRecord> additionalRecords;
-
- MdnsPacket(int flags,
- List<MdnsRecord> questions,
- List<MdnsRecord> answers,
- List<MdnsRecord> authorityRecords,
- List<MdnsRecord> additionalRecords) {
- this.flags = flags;
- this.questions = Collections.unmodifiableList(questions);
- this.answers = Collections.unmodifiableList(answers);
- this.authorityRecords = Collections.unmodifiableList(authorityRecords);
- this.additionalRecords = Collections.unmodifiableList(additionalRecords);
- }
-}
diff --git a/service/mdns/com/android/server/connectivity/mdns/MdnsReplySender.java b/service/mdns/com/android/server/connectivity/mdns/MdnsReplySender.java
deleted file mode 100644
index 1fdbc5c..0000000
--- a/service/mdns/com/android/server/connectivity/mdns/MdnsReplySender.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
-
-import android.annotation.NonNull;
-import android.os.Looper;
-
-import java.io.IOException;
-import java.net.DatagramPacket;
-import java.net.MulticastSocket;
-import java.net.SocketAddress;
-
-/**
- * A class that handles sending mDNS replies to a {@link MulticastSocket}, possibly queueing them
- * to be sent after some delay.
- *
- * TODO: implement sending after a delay, combining queued replies and duplicate answer suppression
- */
-public class MdnsReplySender {
- @NonNull
- private final MulticastSocket mSocket;
- @NonNull
- private final Looper mLooper;
- @NonNull
- private final byte[] mPacketCreationBuffer;
-
- public MdnsReplySender(@NonNull Looper looper,
- @NonNull MulticastSocket socket, @NonNull byte[] packetCreationBuffer) {
- mLooper = looper;
- mSocket = socket;
- mPacketCreationBuffer = packetCreationBuffer;
- }
-
- /**
- * Send a packet immediately.
- *
- * Must be called on the looper thread used by the {@link MdnsReplySender}.
- */
- public void sendNow(@NonNull MdnsPacket packet, @NonNull SocketAddress destination)
- throws IOException {
- if (Thread.currentThread() != mLooper.getThread()) {
- throw new IllegalStateException("sendNow must be called in the handler thread");
- }
-
- // TODO: support packets over size (send in multiple packets with TC bit set)
- final MdnsPacketWriter writer = new MdnsPacketWriter(mPacketCreationBuffer);
-
- writer.writeUInt16(0); // Transaction ID (advertisement: 0)
- writer.writeUInt16(packet.flags); // Response, authoritative (rfc6762 18.4)
- writer.writeUInt16(packet.questions.size()); // questions count
- writer.writeUInt16(packet.answers.size()); // answers count
- writer.writeUInt16(packet.authorityRecords.size()); // authority entries count
- writer.writeUInt16(packet.additionalRecords.size()); // additional records count
-
- for (MdnsRecord record : packet.questions) {
- // Questions do not have TTL or data
- record.writeHeaderFields(writer);
- }
- for (MdnsRecord record : packet.answers) {
- record.write(writer, 0L);
- }
- for (MdnsRecord record : packet.authorityRecords) {
- record.write(writer, 0L);
- }
- for (MdnsRecord record : packet.additionalRecords) {
- record.write(writer, 0L);
- }
-
- final int len = writer.getWritePosition();
- final byte[] outBuffer = new byte[len];
- System.arraycopy(mPacketCreationBuffer, 0, outBuffer, 0, len);
-
- mSocket.send(new DatagramPacket(outBuffer, 0, len, destination));
- }
-}
diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
index 004b4d2..b9d2760 100755
--- a/service/src/com/android/server/ConnectivityService.java
+++ b/service/src/com/android/server/ConnectivityService.java
@@ -242,6 +242,8 @@
import android.util.SparseArray;
import android.util.SparseIntArray;
+import androidx.annotation.RequiresApi;
+
import com.android.connectivity.resources.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
@@ -262,7 +264,12 @@
import com.android.net.module.util.PermissionUtils;
import com.android.net.module.util.TcUtils;
import com.android.net.module.util.netlink.InetDiagMessage;
+import com.android.networkstack.apishim.BroadcastOptionsShimImpl;
+import com.android.networkstack.apishim.ConstantsShim;
+import com.android.networkstack.apishim.common.BroadcastOptionsShim;
+import com.android.networkstack.apishim.common.UnsupportedApiLevelException;
import com.android.server.connectivity.AutodestructReference;
+import com.android.server.connectivity.AutomaticOnOffKeepaliveTracker;
import com.android.server.connectivity.CarrierPrivilegeAuthenticator;
import com.android.server.connectivity.ClatCoordinator;
import com.android.server.connectivity.ConnectivityFlags;
@@ -372,6 +379,10 @@
private static final int DEFAULT_LINGER_DELAY_MS = 30_000;
private static final int DEFAULT_NASCENT_DELAY_MS = 5_000;
+ // Delimiter used when creating the broadcast delivery group for sending
+ // CONNECTIVITY_ACTION broadcast.
+ private static final char DELIVERY_GROUP_KEY_DELIMITER = ';';
+
// The maximum value for the blocking validation result, in milliseconds.
public static final int MAX_VALIDATION_IGNORE_AFTER_ROAM_TIME_MS = 10000;
@@ -833,7 +844,7 @@
private final LocationPermissionChecker mLocationPermissionChecker;
- private final KeepaliveTracker mKeepaliveTracker;
+ private final AutomaticOnOffKeepaliveTracker mKeepaliveTracker;
private final QosCallbackTracker mQosCallbackTracker;
private final NetworkNotificationManager mNotifier;
private final LingerMonitor mLingerMonitor;
@@ -1411,6 +1422,16 @@
+ ", ingress=true, PRIO_POLICE, ETH_P_ALL) failure: ", e);
}
}
+
+ /**
+ * Wraps {@link BroadcastOptionsShimImpl#newInstance(BroadcastOptions)}
+ */
+ // TODO: when available in all active branches:
+ // @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
+ @RequiresApi(Build.VERSION_CODES.CUR_DEVELOPMENT)
+ public BroadcastOptionsShim makeBroadcastOptionsShim(BroadcastOptions options) {
+ return BroadcastOptionsShimImpl.newInstance(options);
+ }
}
public ConnectivityService(Context context) {
@@ -1545,7 +1566,7 @@
mSettingsObserver = new SettingsObserver(mContext, mHandler);
registerSettingsCallbacks();
- mKeepaliveTracker = new KeepaliveTracker(mContext, mHandler);
+ mKeepaliveTracker = new AutomaticOnOffKeepaliveTracker(mContext, mHandler);
mNotifier = new NetworkNotificationManager(mContext, mTelephonyManager);
mQosCallbackTracker = new QosCallbackTracker(mHandler, mNetworkRequestCounter);
@@ -3037,6 +3058,7 @@
ConnectivityManager.EXTRA_NETWORK_INFO);
final BroadcastOptions opts = BroadcastOptions.makeBasic();
opts.setMaxManifestReceiverApiLevel(Build.VERSION_CODES.M);
+ applyMostRecentPolicyForConnectivityAction(opts, ni);
options = opts.toBundle();
intent.addFlags(Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS);
}
@@ -3048,6 +3070,32 @@
}
}
+ private void applyMostRecentPolicyForConnectivityAction(BroadcastOptions options,
+ NetworkInfo info) {
+ // Delivery group policy APIs are only available on U+.
+ if (!SdkLevel.isAtLeastU()) return;
+
+ final BroadcastOptionsShim optsShim = mDeps.makeBroadcastOptionsShim(options);
+ try {
+ // This allows us to discard older broadcasts still waiting to be delivered
+ // which have the same namespace and key.
+ optsShim.setDeliveryGroupPolicy(ConstantsShim.DELIVERY_GROUP_POLICY_MOST_RECENT);
+ optsShim.setDeliveryGroupMatchingKey(ConnectivityManager.CONNECTIVITY_ACTION,
+ createDeliveryGroupKeyForConnectivityAction(info));
+ } catch (UnsupportedApiLevelException e) {
+ Log.wtf(TAG, "Using unsupported API" + e);
+ }
+ }
+
+ @VisibleForTesting
+ static String createDeliveryGroupKeyForConnectivityAction(NetworkInfo info) {
+ final StringBuilder sb = new StringBuilder();
+ sb.append(info.getType()).append(DELIVERY_GROUP_KEY_DELIMITER);
+ sb.append(info.getSubtype()).append(DELIVERY_GROUP_KEY_DELIMITER);
+ sb.append(info.getExtraInfo());
+ return sb.toString();
+ }
+
/**
* Called by SystemServer through ConnectivityManager when the system is ready.
*/
@@ -4366,6 +4414,9 @@
mNetworkForNetId.remove(nai.network.getNetId());
}
propagateUnderlyingNetworkCapabilities(nai.network);
+ // Update allowed network lists in netd. This should be called after removing nai
+ // from mNetworkAgentInfos.
+ updateProfileAllowedNetworks();
// Remove all previously satisfied requests.
for (int i = 0; i < nai.numNetworkRequests(); i++) {
final NetworkRequest request = nai.requestAt(i);
@@ -4800,6 +4851,7 @@
}
}
}
+
nri.mPerUidCounter.decrementCount(nri.mUid);
mNetworkRequestInfoLogs.log("RELEASE " + nri);
checkNrisConsistency(nri);
@@ -5493,6 +5545,33 @@
mKeepaliveTracker.handleStartKeepalive(msg);
break;
}
+ case NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE: {
+ final Network network = (Network) msg.obj;
+ final int slot = msg.arg1;
+
+ boolean networkFound = false;
+ final ArrayList<NetworkAgentInfo> vpnsRunningOnThisNetwork = new ArrayList<>();
+ for (NetworkAgentInfo n : mNetworkAgentInfos) {
+ if (n.network.equals(network)) networkFound = true;
+ if (n.isVPN() && n.everConnected() && hasUnderlyingNetwork(n, network)) {
+ vpnsRunningOnThisNetwork.add(n);
+ }
+ }
+
+ // If the network no longer exists, then the keepalive should have been
+ // cleaned up already. There is no point trying to resume keepalives.
+ if (!networkFound) return;
+
+ if (!vpnsRunningOnThisNetwork.isEmpty()) {
+ mKeepaliveTracker.handleMonitorAutomaticKeepalive(network, slot,
+ // TODO: check all the VPNs running on top of this network
+ vpnsRunningOnThisNetwork.get(0).network.netId);
+ } else {
+ // If no VPN, then make sure the keepalive is running.
+ mKeepaliveTracker.handleMaybeResumeKeepalive(network, slot);
+ }
+ break;
+ }
// Sent by KeepaliveTracker to process an app request on the state machine thread.
case NetworkAgent.CMD_STOP_SOCKET_KEEPALIVE: {
NetworkAgentInfo nai = getNetworkAgentInfoForNetwork((Network) msg.obj);
@@ -6166,12 +6245,16 @@
if (mOemNetworkPreferences.getNetworkPreferences().size() > 0) {
handleSetOemNetworkPreference(mOemNetworkPreferences, null);
}
+ if (!mProfileNetworkPreferences.isEmpty()) {
+ updateProfileAllowedNetworks();
+ }
}
private void onUserRemoved(@NonNull final UserHandle user) {
// If there was a network preference for this user, remove it.
handleSetProfileNetworkPreference(
- List.of(new ProfileNetworkPreferenceInfo(user, null, true)),
+ List.of(new ProfileNetworkPreferenceInfo(user, null, true,
+ false /* blockingNonEnterprise */)),
null /* listener */);
if (mOemNetworkPreferences.getNetworkPreferences().size() > 0) {
handleSetOemNetworkPreference(mOemNetworkPreferences, null);
@@ -8688,6 +8771,73 @@
}
}
+ /**
+ * Collect restricted uid ranges for the given network and UserHandle, these uids
+ * are not restricted for matched enterprise networks but being restricted for non-matched
+ * enterprise networks and non-enterprise networks.
+ */
+ @NonNull
+ private ArraySet<UidRange> getRestrictedUidRangesForEnterpriseBlocking(
+ @NonNull NetworkAgentInfo nai, @NonNull UserHandle user) {
+ final ArraySet<UidRange> restrictedUidRanges = new ArraySet<>();
+ for (final ProfileNetworkPreferenceInfo pref : mProfileNetworkPreferences) {
+ if (!pref.user.equals(user) || !pref.blockingNonEnterprise) continue;
+
+ if (nai.networkCapabilities.hasCapability(NET_CAPABILITY_ENTERPRISE)) {
+ // The NC is built from a `ProfileNetworkPreference` which has only one
+ // enterprise ID, so it's guaranteed to have exactly one.
+ final int prefId = pref.capabilities.getEnterpriseIds()[0];
+ if (nai.networkCapabilities.hasEnterpriseId(prefId)) {
+ continue;
+ }
+ }
+
+ if (UidRangeUtils.doesRangeSetOverlap(restrictedUidRanges,
+ pref.capabilities.getUidRanges())) {
+ throw new IllegalArgumentException(
+ "Overlapping uid range in preference: " + pref);
+ }
+ restrictedUidRanges.addAll(pref.capabilities.getUidRanges());
+ }
+ return restrictedUidRanges;
+ }
+
+ private void updateProfileAllowedNetworks() {
+ ensureRunningOnConnectivityServiceThread();
+ final ArrayList<NativeUidRangeConfig> configs = new ArrayList<>();
+ final List<UserHandle> users = mContext.getSystemService(UserManager.class)
+ .getUserHandles(true /* excludeDying */);
+ if (users.isEmpty()) {
+ throw new IllegalStateException("No user is available");
+ }
+
+ for (final NetworkAgentInfo nai : mNetworkAgentInfos) {
+ ArraySet<UidRange> allowedUidRanges = new ArraySet<>();
+ for (final UserHandle user : users) {
+ final ArraySet<UidRange> restrictedUidRanges =
+ getRestrictedUidRangesForEnterpriseBlocking(nai, user);
+ allowedUidRanges.addAll(UidRangeUtils.removeRangeSetFromUidRange(
+ UidRange.createForUser(user), restrictedUidRanges));
+ }
+
+ final UidRangeParcel[] rangesParcel = toUidRangeStableParcels(allowedUidRanges);
+ configs.add(new NativeUidRangeConfig(
+ nai.network.netId, rangesParcel, 0 /* subPriority */));
+ }
+
+ // The netd API replaces the previous configs with the current configs.
+ // Thus, for network disconnection or preference removal, no need to
+ // unset previous config. Instead, collecting all currently needed
+ // configs and issue to netd.
+ try {
+ mNetd.setNetworkAllowlist(configs.toArray(new NativeUidRangeConfig[0]));
+ } catch (ServiceSpecificException e) {
+ // Has the interface disappeared since the network was built?
+ } catch (RemoteException e) {
+ // Netd died. This usually causes a runtime restart anyway.
+ }
+ }
+
private void makeDefaultNetwork(@Nullable final NetworkAgentInfo newDefaultNetwork) {
try {
if (null != newDefaultNetwork) {
@@ -9320,6 +9470,7 @@
networkAgent.setCreated();
networkAgent.onNetworkCreated();
updateAllowedUids(networkAgent, null, networkAgent.networkCapabilities);
+ updateProfileAllowedNetworks();
}
if (!networkAgent.everConnected() && state == NetworkInfo.State.CONNECTED) {
@@ -9665,20 +9816,23 @@
enforceKeepalivePermission();
mKeepaliveTracker.startNattKeepalive(
getNetworkAgentInfoForNetwork(network), null /* fd */,
- intervalSeconds, cb,
- srcAddr, srcPort, dstAddr, NattSocketKeepalive.NATT_PORT);
+ intervalSeconds, cb, srcAddr, srcPort, dstAddr, NattSocketKeepalive.NATT_PORT,
+ // Keep behavior of the deprecated method as it is. Set automaticOnOffKeepalives to
+ // false because there is no way and no plan to configure automaticOnOffKeepalives
+ // in this deprecated method.
+ false /* automaticOnOffKeepalives */);
}
@Override
public void startNattKeepaliveWithFd(Network network, ParcelFileDescriptor pfd, int resourceId,
int intervalSeconds, ISocketKeepaliveCallback cb, String srcAddr,
- String dstAddr) {
+ String dstAddr, boolean automaticOnOffKeepalives) {
try {
final FileDescriptor fd = pfd.getFileDescriptor();
mKeepaliveTracker.startNattKeepalive(
getNetworkAgentInfoForNetwork(network), fd, resourceId,
intervalSeconds, cb,
- srcAddr, dstAddr, NattSocketKeepalive.NATT_PORT);
+ srcAddr, dstAddr, NattSocketKeepalive.NATT_PORT, automaticOnOffKeepalives);
} finally {
// FileDescriptors coming from AIDL calls must be manually closed to prevent leaks.
// startNattKeepalive calls Os.dup(fd) before returning, so we can close immediately.
@@ -10856,6 +11010,7 @@
for (final ProfileNetworkPreference preference : preferences) {
final NetworkCapabilities nc;
boolean allowFallback = true;
+ boolean blockingNonEnterprise = false;
switch (preference.getPreference()) {
case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT:
nc = null;
@@ -10865,6 +11020,9 @@
"Invalid enterprise identifier in setProfileNetworkPreferences");
}
break;
+ case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING:
+ blockingNonEnterprise = true;
+ // continue to process the enterprise preference.
case ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK:
allowFallback = false;
// continue to process the enterprise preference.
@@ -10898,7 +11056,8 @@
throw new IllegalArgumentException(
"Invalid preference in setProfileNetworkPreferences");
}
- preferenceList.add(new ProfileNetworkPreferenceInfo(profile, nc, allowFallback));
+ preferenceList.add(new ProfileNetworkPreferenceInfo(
+ profile, nc, allowFallback, blockingNonEnterprise));
if (hasDefaultPreference && preferenceList.size() > 1) {
throw new IllegalArgumentException(
"Default profile preference should not be set along with other preference");
@@ -11011,6 +11170,7 @@
removeDefaultNetworkRequestsForPreference(PREFERENCE_ORDER_PROFILE);
addPerAppDefaultNetworkRequests(
createNrisFromProfileNetworkPreferences(mProfileNetworkPreferences));
+ updateProfileAllowedNetworks();
// Finally, rematch.
rematchAllNetworksAndRequests();
diff --git a/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java
new file mode 100644
index 0000000..d220835
--- /dev/null
+++ b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java
@@ -0,0 +1,693 @@
+/*
+ * Copyright (C) 2023 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 com.android.server.connectivity;
+
+import static android.net.NetworkAgent.CMD_START_SOCKET_KEEPALIVE;
+import static android.net.SocketKeepalive.ERROR_INVALID_SOCKET;
+import static android.net.SocketKeepalive.SUCCESS;
+import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY;
+import static android.system.OsConstants.AF_INET;
+import static android.system.OsConstants.AF_INET6;
+import static android.system.OsConstants.SOL_SOCKET;
+import static android.system.OsConstants.SO_SNDTIMEO;
+
+import static com.android.net.module.util.netlink.NetlinkConstants.NLMSG_DONE;
+import static com.android.net.module.util.netlink.NetlinkConstants.SOCKDIAG_MSG_HEADER_SIZE;
+import static com.android.net.module.util.netlink.NetlinkConstants.SOCK_DIAG_BY_FAMILY;
+import static com.android.net.module.util.netlink.NetlinkUtils.IO_TIMEOUT_MS;
+
+import android.annotation.IntDef;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.INetd;
+import android.net.ISocketKeepaliveCallback;
+import android.net.MarkMaskParcel;
+import android.net.Network;
+import android.net.NetworkAgent;
+import android.net.SocketKeepalive;
+import android.net.SocketKeepalive.InvalidSocketException;
+import android.os.FileUtils;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.Message;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.system.ErrnoException;
+import android.system.Os;
+import android.system.StructTimeval;
+import android.util.Log;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.IndentingPrintWriter;
+import com.android.modules.utils.build.SdkLevel;
+import com.android.net.module.util.DeviceConfigUtils;
+import com.android.net.module.util.HexDump;
+import com.android.net.module.util.SocketUtils;
+import com.android.net.module.util.netlink.InetDiagMessage;
+import com.android.net.module.util.netlink.NetlinkUtils;
+import com.android.net.module.util.netlink.StructNlAttr;
+
+import java.io.FileDescriptor;
+import java.io.InterruptedIOException;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.net.SocketException;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Objects;
+
+/**
+ * Manages automatic on/off socket keepalive requests.
+ *
+ * Provides methods to stop and start automatic keepalive requests, and keeps track of keepalives
+ * across all networks. For non-automatic on/off keepalive request, this class just forwards the
+ * requests to KeepaliveTracker. This class is tightly coupled to ConnectivityService. It is not
+ * thread-safe and its handle* methods must be called only from the ConnectivityService handler
+ * thread.
+ */
+public class AutomaticOnOffKeepaliveTracker {
+ private static final String TAG = "AutomaticOnOffKeepaliveTracker";
+ private static final int[] ADDRESS_FAMILIES = new int[] {AF_INET6, AF_INET};
+ private static final String ACTION_TCP_POLLING_ALARM =
+ "com.android.server.connectivity.KeepaliveTracker.TCP_POLLING_ALARM";
+ private static final String EXTRA_NETWORK = "network_id";
+ private static final String EXTRA_SLOT = "slot";
+ private static final long DEFAULT_TCP_POLLING_INTERVAL_MS = 120_000L;
+ private static final String AUTOMATIC_ON_OFF_KEEPALIVE_VERSION =
+ "automatic_on_off_keepalive_version";
+ /**
+ * States for {@code #AutomaticOnOffKeepalive}.
+ *
+ * A new AutomaticOnOffKeepalive starts with STATE_ENABLED. The system will monitor
+ * the TCP sockets on VPN networks running on top of the specified network, and turn off
+ * keepalive if there is no TCP socket any of the VPN networks. Conversely, it will turn
+ * keepalive back on if any TCP socket is open on any of the VPN networks.
+ *
+ * When there is no TCP socket on any of the VPN networks, the state becomes STATE_SUSPENDED.
+ * The {@link KeepaliveTracker.KeepaliveInfo} object is kept to remember the parameters so it
+ * is possible to resume keepalive later with the same parameters.
+ *
+ * When the system detects some TCP socket is open on one of the VPNs while in STATE_SUSPENDED,
+ * this AutomaticOnOffKeepalive goes to STATE_ENABLED again.
+ *
+ * When finishing keepalive, this object is deleted.
+ */
+ private static final int STATE_ENABLED = 0;
+ private static final int STATE_SUSPENDED = 1;
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef(prefix = { "STATE_" }, value = {
+ STATE_ENABLED,
+ STATE_SUSPENDED
+ })
+ private @interface AutomaticOnOffState {}
+
+ @NonNull
+ private final Handler mConnectivityServiceHandler;
+ @NonNull
+ private final KeepaliveTracker mKeepaliveTracker;
+ @NonNull
+ private final Context mContext;
+ @NonNull
+ private final AlarmManager mAlarmManager;
+
+ /**
+ * The {@code inetDiagReqV2} messages for different IP family.
+ *
+ * Key: Ip family type.
+ * Value: Bytes array represent the {@code inetDiagReqV2}.
+ *
+ * This should only be accessed in the connectivity service handler thread.
+ */
+ private final SparseArray<byte[]> mSockDiagMsg = new SparseArray<>();
+ private final Dependencies mDependencies;
+ private final INetd mNetd;
+ /**
+ * Keeps track of automatic on/off keepalive requests.
+ * This should be only updated in ConnectivityService handler thread.
+ */
+ private final ArrayList<AutomaticOnOffKeepalive> mAutomaticOnOffKeepalives = new ArrayList<>();
+
+ private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (ACTION_TCP_POLLING_ALARM.equals(intent.getAction())) {
+ Log.d(TAG, "Received TCP polling intent");
+ final Network network = intent.getParcelableExtra(EXTRA_NETWORK);
+ final int slot = intent.getIntExtra(EXTRA_SLOT, -1);
+ mConnectivityServiceHandler.obtainMessage(
+ NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE,
+ slot, 0 , network).sendToTarget();
+ }
+ }
+ };
+
+ private static class AutomaticOnOffKeepalive {
+ @NonNull
+ private final KeepaliveTracker.KeepaliveInfo mKi;
+ @NonNull
+ private final FileDescriptor mFd;
+ @NonNull
+ private final PendingIntent mTcpPollingAlarm;
+ private final int mSlot;
+ @AutomaticOnOffState
+ private int mAutomaticOnOffState = STATE_ENABLED;
+
+ AutomaticOnOffKeepalive(@NonNull KeepaliveTracker.KeepaliveInfo ki,
+ @NonNull Context context) throws InvalidSocketException {
+ this.mKi = Objects.requireNonNull(ki);
+ // A null fd is acceptable in KeepaliveInfo for backward compatibility of
+ // PacketKeepalive API, but it should not happen here because legacy API cannot setup
+ // automatic keepalive.
+ Objects.requireNonNull(ki.mFd);
+
+ // Get the slot from keepalive because the slot information may be missing when the
+ // keepalive is stopped.
+ this.mSlot = ki.getSlot();
+ try {
+ this.mFd = Os.dup(ki.mFd);
+ } catch (ErrnoException e) {
+ Log.e(TAG, "Cannot dup fd: ", e);
+ throw new InvalidSocketException(ERROR_INVALID_SOCKET, e);
+ }
+ mTcpPollingAlarm = createTcpPollingAlarmIntent(
+ context, ki.getNai().network(), ki.getSlot());
+ }
+
+ public boolean match(Network network, int slot) {
+ return this.mKi.getNai().network().equals(network) && this.mSlot == slot;
+ }
+
+ private static PendingIntent createTcpPollingAlarmIntent(@NonNull Context context,
+ @NonNull Network network, int slot) {
+ final Intent intent = new Intent(ACTION_TCP_POLLING_ALARM);
+ intent.putExtra(EXTRA_NETWORK, network);
+ intent.putExtra(EXTRA_SLOT, slot);
+ return PendingIntent.getBroadcast(
+ context, 0 /* requestCode */, intent, PendingIntent.FLAG_IMMUTABLE);
+ }
+ }
+
+ public AutomaticOnOffKeepaliveTracker(@NonNull Context context, @NonNull Handler handler) {
+ this(context, handler, new Dependencies(context));
+ }
+
+ @VisibleForTesting
+ public AutomaticOnOffKeepaliveTracker(@NonNull Context context, @NonNull Handler handler,
+ @NonNull Dependencies dependencies) {
+ mContext = Objects.requireNonNull(context);
+ mDependencies = Objects.requireNonNull(dependencies);
+ mConnectivityServiceHandler = Objects.requireNonNull(handler);
+ mNetd = mDependencies.getNetd();
+ mKeepaliveTracker = mDependencies.newKeepaliveTracker(
+ mContext, mConnectivityServiceHandler);
+
+ if (SdkLevel.isAtLeastU()) {
+ mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_TCP_POLLING_ALARM),
+ null, handler);
+ }
+ mAlarmManager = mContext.getSystemService(AlarmManager.class);
+ }
+
+ private void startTcpPollingAlarm(@NonNull PendingIntent alarm) {
+ final long triggerAtMillis =
+ SystemClock.elapsedRealtime() + DEFAULT_TCP_POLLING_INTERVAL_MS;
+ // Setup a non-wake up alarm.
+ mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, triggerAtMillis, alarm);
+ }
+
+ /**
+ * Determine if any state transition is needed for the specific automatic keepalive.
+ */
+ public void handleMonitorAutomaticKeepalive(@NonNull Network network, int slot, int vpnNetId) {
+ final AutomaticOnOffKeepalive autoKi = findAutomaticOnOffKeepalive(network, slot);
+ // This may happen if the keepalive is removed by the app, and the alarm is fired at the
+ // same time.
+ if (autoKi == null) return;
+
+ handleMonitorTcpConnections(autoKi, vpnNetId);
+ }
+
+ /**
+ * Determine if disable or re-enable keepalive is needed or not based on TCP sockets status.
+ */
+ private void handleMonitorTcpConnections(@NonNull AutomaticOnOffKeepalive ki, int vpnNetId) {
+ if (!isAnyTcpSocketConnected(vpnNetId)) {
+ // No TCP socket exists. Stop keepalive if ENABLED, and remain SUSPENDED if currently
+ // SUSPENDED.
+ if (ki.mAutomaticOnOffState == STATE_ENABLED) {
+ ki.mAutomaticOnOffState = STATE_SUSPENDED;
+ handleSuspendKeepalive(ki.mKi.mNai, ki.mSlot, SUCCESS);
+ }
+ } else {
+ handleMaybeResumeKeepalive(ki);
+ }
+ // TODO: listen to socket status instead of periodically check.
+ startTcpPollingAlarm(ki.mTcpPollingAlarm);
+ }
+
+ /**
+ * Resume keepalive for this slot on this network, if it wasn't already resumed.
+ */
+ public void handleMaybeResumeKeepalive(@NonNull final Network network, final int slot) {
+ final AutomaticOnOffKeepalive autoKi = findAutomaticOnOffKeepalive(network, slot);
+ // This may happen if the keepalive is removed by the app, and the alarm is fired at
+ // the same time.
+ if (autoKi == null) return;
+ handleMaybeResumeKeepalive(autoKi);
+ }
+
+ private void handleMaybeResumeKeepalive(@NonNull AutomaticOnOffKeepalive autoKi) {
+ if (autoKi.mAutomaticOnOffState == STATE_ENABLED) return;
+ KeepaliveTracker.KeepaliveInfo newKi;
+ try {
+ // Get fd from AutomaticOnOffKeepalive since the fd in the original
+ // KeepaliveInfo should be closed.
+ newKi = autoKi.mKi.withFd(autoKi.mFd);
+ } catch (InvalidSocketException | IllegalArgumentException | SecurityException e) {
+ Log.e(TAG, "Fail to construct keepalive", e);
+ mKeepaliveTracker.notifyErrorCallback(autoKi.mKi.mCallback, ERROR_INVALID_SOCKET);
+ return;
+ }
+ autoKi.mAutomaticOnOffState = STATE_ENABLED;
+ handleResumeKeepalive(mConnectivityServiceHandler.obtainMessage(
+ NetworkAgent.CMD_START_SOCKET_KEEPALIVE,
+ autoKi.mAutomaticOnOffState, 0, newKi));
+ }
+
+ private int findAutomaticOnOffKeepaliveIndex(@NonNull Network network, int slot) {
+ ensureRunningOnHandlerThread();
+
+ int index = 0;
+ for (AutomaticOnOffKeepalive ki : mAutomaticOnOffKeepalives) {
+ if (ki.match(network, slot)) {
+ return index;
+ }
+ index++;
+ }
+ return -1;
+ }
+
+ @Nullable
+ private AutomaticOnOffKeepalive findAutomaticOnOffKeepalive(@NonNull Network network,
+ int slot) {
+ ensureRunningOnHandlerThread();
+
+ final int index = findAutomaticOnOffKeepaliveIndex(network, slot);
+ return (index >= 0) ? mAutomaticOnOffKeepalives.get(index) : null;
+ }
+
+ /**
+ * Handle keepalive events from lower layer.
+ *
+ * Forward to KeepaliveTracker.
+ */
+ public void handleEventSocketKeepalive(@NonNull NetworkAgentInfo nai, int slot, int reason) {
+ mKeepaliveTracker.handleEventSocketKeepalive(nai, slot, reason);
+ }
+
+ /**
+ * Handle stop all keepalives on the specific network.
+ */
+ public void handleStopAllKeepalives(NetworkAgentInfo nai, int reason) {
+ mKeepaliveTracker.handleStopAllKeepalives(nai, reason);
+ final Iterator<AutomaticOnOffKeepalive> iterator = mAutomaticOnOffKeepalives.iterator();
+ while (iterator.hasNext()) {
+ final AutomaticOnOffKeepalive autoKi = iterator.next();
+ if (autoKi.mKi.getNai() == nai) {
+ cleanupAutoOnOffKeepalive(autoKi);
+ iterator.remove();
+ }
+ }
+ }
+
+ /**
+ * Handle start keepalive contained within a message.
+ *
+ * The message is expected to contain a KeepaliveTracker.KeepaliveInfo.
+ */
+ public void handleStartKeepalive(Message message) {
+ mKeepaliveTracker.handleStartKeepalive(message);
+
+ // Add automatic on/off request into list to track its life cycle.
+ final boolean automaticOnOff = message.arg1 != 0
+ && mDependencies.isFeatureEnabled(AUTOMATIC_ON_OFF_KEEPALIVE_VERSION);
+ if (automaticOnOff) {
+ final KeepaliveTracker.KeepaliveInfo ki = (KeepaliveTracker.KeepaliveInfo) message.obj;
+ AutomaticOnOffKeepalive autoKi;
+ try {
+ // CAREFUL : mKeepaliveTracker.handleStartKeepalive will assign |ki.mSlot| after
+ // pulling |ki| from the message. The constructor below will read this member
+ // (through ki.getSlot()) and therefore actively relies on handleStartKeepalive
+ // having assigned this member before this is called.
+ // TODO : clean this up by assigning the slot at the start of this method instead
+ // and ideally removing the mSlot member from KeepaliveInfo.
+ autoKi = new AutomaticOnOffKeepalive(ki, mContext);
+ } catch (SocketKeepalive.InvalidSocketException | IllegalArgumentException e) {
+ Log.e(TAG, "Fail to construct keepalive", e);
+ mKeepaliveTracker.notifyErrorCallback(ki.mCallback, ERROR_INVALID_SOCKET);
+ return;
+ }
+ mAutomaticOnOffKeepalives.add(autoKi);
+ startTcpPollingAlarm(autoKi.mTcpPollingAlarm);
+ }
+ }
+
+ private void handleResumeKeepalive(Message message) {
+ mKeepaliveTracker.handleStartKeepalive(message);
+ }
+
+ private void handleSuspendKeepalive(NetworkAgentInfo nai, int slot, int reason) {
+ mKeepaliveTracker.handleStopKeepalive(nai, slot, reason);
+ }
+
+ /**
+ * Handle stop keepalives on the specific network with given slot.
+ */
+ public void handleStopKeepalive(NetworkAgentInfo nai, int slot, int reason) {
+ final AutomaticOnOffKeepalive autoKi = findAutomaticOnOffKeepalive(nai.network, slot);
+
+ // Let the original keepalive do the stop first, and then clean up the keepalive if it's an
+ // automatic keepalive.
+ if (autoKi == null || autoKi.mAutomaticOnOffState == STATE_ENABLED) {
+ mKeepaliveTracker.handleStopKeepalive(nai, slot, reason);
+ }
+
+ // Not an AutomaticOnOffKeepalive.
+ if (autoKi == null) return;
+
+ cleanupAutoOnOffKeepalive(autoKi);
+ mAutomaticOnOffKeepalives.remove(autoKi);
+ }
+
+ private void cleanupAutoOnOffKeepalive(@NonNull final AutomaticOnOffKeepalive autoKi) {
+ ensureRunningOnHandlerThread();
+ mAlarmManager.cancel(autoKi.mTcpPollingAlarm);
+ // Close the duplicated fd that maintains the lifecycle of socket.
+ FileUtils.closeQuietly(autoKi.mFd);
+ }
+
+ /**
+ * Called when requesting that keepalives be started on a IPsec NAT-T socket. See
+ * {@link android.net.SocketKeepalive}.
+ *
+ * Forward to KeepaliveTracker.
+ **/
+ public void startNattKeepalive(@Nullable NetworkAgentInfo nai,
+ @Nullable FileDescriptor fd,
+ int intervalSeconds,
+ @NonNull ISocketKeepaliveCallback cb,
+ @NonNull String srcAddrString,
+ int srcPort,
+ @NonNull String dstAddrString,
+ int dstPort, boolean automaticOnOffKeepalives) {
+ final KeepaliveTracker.KeepaliveInfo ki = mKeepaliveTracker.makeNattKeepaliveInfo(nai, fd,
+ intervalSeconds, cb, srcAddrString, srcPort, dstAddrString, dstPort);
+ if (null != ki) {
+ mConnectivityServiceHandler.obtainMessage(NetworkAgent.CMD_START_SOCKET_KEEPALIVE,
+ // TODO : move ConnectivityService#encodeBool to a static lib.
+ automaticOnOffKeepalives ? 1 : 0, 0, ki).sendToTarget();
+ }
+ }
+
+ /**
+ * Called when requesting that keepalives be started on a IPsec NAT-T socket. See
+ * {@link android.net.SocketKeepalive}.
+ *
+ * Forward to KeepaliveTracker.
+ **/
+ public void startNattKeepalive(@Nullable NetworkAgentInfo nai,
+ @Nullable FileDescriptor fd,
+ int resourceId,
+ int intervalSeconds,
+ @NonNull ISocketKeepaliveCallback cb,
+ @NonNull String srcAddrString,
+ @NonNull String dstAddrString,
+ int dstPort,
+ boolean automaticOnOffKeepalives) {
+ final KeepaliveTracker.KeepaliveInfo ki = mKeepaliveTracker.makeNattKeepaliveInfo(nai, fd,
+ resourceId, intervalSeconds, cb, srcAddrString, dstAddrString, dstPort);
+ if (null != ki) {
+ mConnectivityServiceHandler.obtainMessage(NetworkAgent.CMD_START_SOCKET_KEEPALIVE,
+ // TODO : move ConnectivityService#encodeBool to a static lib.
+ automaticOnOffKeepalives ? 1 : 0, 0, ki).sendToTarget();
+ }
+ }
+
+ /**
+ * Called by ConnectivityService to start TCP keepalive on a file descriptor.
+ *
+ * In order to offload keepalive for application correctly, sequence number, ack number and
+ * other fields are needed to form the keepalive packet. Thus, this function synchronously
+ * puts the socket into repair mode to get the necessary information. After the socket has been
+ * put into repair mode, the application cannot access the socket until reverted to normal.
+ * See {@link android.net.SocketKeepalive}.
+ *
+ * Forward to KeepaliveTracker.
+ **/
+ public void startTcpKeepalive(@Nullable NetworkAgentInfo nai,
+ @NonNull FileDescriptor fd,
+ int intervalSeconds,
+ @NonNull ISocketKeepaliveCallback cb) {
+ final KeepaliveTracker.KeepaliveInfo ki = mKeepaliveTracker.makeTcpKeepaliveInfo(nai, fd,
+ intervalSeconds, cb);
+ if (null != ki) {
+ mConnectivityServiceHandler.obtainMessage(CMD_START_SOCKET_KEEPALIVE, ki)
+ .sendToTarget();
+ }
+ }
+
+ /**
+ * Dump AutomaticOnOffKeepaliveTracker state.
+ */
+ public void dump(IndentingPrintWriter pw) {
+ // TODO: Dump the necessary information for automatic on/off keepalive.
+ mKeepaliveTracker.dump(pw);
+ }
+
+ /**
+ * Check all keepalives on the network are still valid.
+ *
+ * Forward to KeepaliveTracker.
+ */
+ public void handleCheckKeepalivesStillValid(NetworkAgentInfo nai) {
+ mKeepaliveTracker.handleCheckKeepalivesStillValid(nai);
+ }
+
+ @VisibleForTesting
+ boolean isAnyTcpSocketConnected(int netId) {
+ FileDescriptor fd = null;
+
+ try {
+ fd = mDependencies.createConnectedNetlinkSocket();
+
+ // Get network mask
+ final MarkMaskParcel parcel = mNetd.getFwmarkForNetwork(netId);
+ final int networkMark = (parcel != null) ? parcel.mark : NetlinkUtils.UNKNOWN_MARK;
+ final int networkMask = (parcel != null) ? parcel.mask : NetlinkUtils.NULL_MASK;
+
+ // Send request for each IP family
+ for (final int family : ADDRESS_FAMILIES) {
+ if (isAnyTcpSocketConnectedForFamily(fd, family, networkMark, networkMask)) {
+ return true;
+ }
+ }
+ } catch (ErrnoException | SocketException | InterruptedIOException | RemoteException e) {
+ Log.e(TAG, "Fail to get socket info via netlink.", e);
+ } finally {
+ SocketUtils.closeSocketQuietly(fd);
+ }
+
+ return false;
+ }
+
+ private boolean isAnyTcpSocketConnectedForFamily(FileDescriptor fd, int family, int networkMark,
+ int networkMask) throws ErrnoException, InterruptedIOException {
+ ensureRunningOnHandlerThread();
+ // Build SocketDiag messages and cache it.
+ if (mSockDiagMsg.get(family) == null) {
+ mSockDiagMsg.put(family, InetDiagMessage.buildInetDiagReqForAliveTcpSockets(family));
+ }
+ mDependencies.sendRequest(fd, mSockDiagMsg.get(family));
+
+ // Iteration limitation as a protection to avoid possible infinite loops.
+ // DEFAULT_RECV_BUFSIZE could read more than 20 sockets per time. Max iteration
+ // should be enough to go through reasonable TCP sockets in the device.
+ final int maxIteration = 100;
+ int parsingIteration = 0;
+ while (parsingIteration < maxIteration) {
+ final ByteBuffer bytes = mDependencies.recvSockDiagResponse(fd);
+
+ try {
+ while (NetlinkUtils.enoughBytesRemainForValidNlMsg(bytes)) {
+ final int startPos = bytes.position();
+
+ final int nlmsgLen = bytes.getInt();
+ final int nlmsgType = bytes.getShort();
+ if (isEndOfMessageOrError(nlmsgType)) return false;
+ // TODO: Parse InetDiagMessage to get uid and dst address information to filter
+ // socket via NetlinkMessage.parse.
+
+ // Skip the header to move to data part.
+ bytes.position(startPos + SOCKDIAG_MSG_HEADER_SIZE);
+
+ if (isTargetTcpSocket(bytes, nlmsgLen, networkMark, networkMask)) {
+ return true;
+ }
+ }
+ } catch (BufferUnderflowException e) {
+ // The exception happens in random place in either header position or any data
+ // position. Partial bytes from the middle of the byte buffer may not be enough to
+ // clarify, so print out the content before the error to possibly prevent printing
+ // the whole 8K buffer.
+ final int exceptionPos = bytes.position();
+ final String hex = HexDump.dumpHexString(bytes.array(), 0, exceptionPos);
+ Log.e(TAG, "Unexpected socket info parsing: " + hex, e);
+ }
+
+ parsingIteration++;
+ }
+ return false;
+ }
+
+ private boolean isEndOfMessageOrError(int nlmsgType) {
+ return nlmsgType == NLMSG_DONE || nlmsgType != SOCK_DIAG_BY_FAMILY;
+ }
+
+ private boolean isTargetTcpSocket(@NonNull ByteBuffer bytes, int nlmsgLen, int networkMark,
+ int networkMask) {
+ final int mark = readSocketDataAndReturnMark(bytes, nlmsgLen);
+ return (mark & networkMask) == networkMark;
+ }
+
+ private int readSocketDataAndReturnMark(@NonNull ByteBuffer bytes, int nlmsgLen) {
+ final int nextMsgOffset = bytes.position() + nlmsgLen - SOCKDIAG_MSG_HEADER_SIZE;
+ int mark = NetlinkUtils.INIT_MARK_VALUE;
+ // Get socket mark
+ // TODO: Add a parsing method in NetlinkMessage.parse to support this to skip the remaining
+ // data.
+ while (bytes.position() < nextMsgOffset) {
+ final StructNlAttr nlattr = StructNlAttr.parse(bytes);
+ if (nlattr != null && nlattr.nla_type == NetlinkUtils.INET_DIAG_MARK) {
+ mark = nlattr.getValueAsInteger();
+ }
+ }
+ return mark;
+ }
+
+ private void ensureRunningOnHandlerThread() {
+ if (mConnectivityServiceHandler.getLooper().getThread() != Thread.currentThread()) {
+ throw new IllegalStateException(
+ "Not running on handler thread: " + Thread.currentThread().getName());
+ }
+ }
+
+ /**
+ * Dependencies class for testing.
+ */
+ @VisibleForTesting
+ public static class Dependencies {
+ private final Context mContext;
+
+ public Dependencies(final Context context) {
+ mContext = context;
+ }
+
+ /**
+ * Create a netlink socket connected to the kernel.
+ *
+ * @return fd the fileDescriptor of the socket.
+ */
+ public FileDescriptor createConnectedNetlinkSocket()
+ throws ErrnoException, SocketException {
+ final FileDescriptor fd = NetlinkUtils.createNetLinkInetDiagSocket();
+ NetlinkUtils.connectSocketToNetlink(fd);
+ Os.setsockoptTimeval(fd, SOL_SOCKET, SO_SNDTIMEO,
+ StructTimeval.fromMillis(IO_TIMEOUT_MS));
+ return fd;
+ }
+
+ /**
+ * Send composed message request to kernel.
+ *
+ * The given FileDescriptor is expected to be created by
+ * {@link #createConnectedNetlinkSocket} or equivalent way.
+ *
+ * @param fd a netlink socket {@code FileDescriptor} connected to the kernel.
+ * @param msg the byte array representing the request message to write to kernel.
+ */
+ public void sendRequest(@NonNull final FileDescriptor fd,
+ @NonNull final byte[] msg)
+ throws ErrnoException, InterruptedIOException {
+ Os.write(fd, msg, 0 /* byteOffset */, msg.length);
+ }
+
+ /**
+ * Get an INetd connector.
+ */
+ public INetd getNetd() {
+ return INetd.Stub.asInterface(
+ (IBinder) mContext.getSystemService(Context.NETD_SERVICE));
+ }
+
+ /**
+ * Receive the response message from kernel via given {@code FileDescriptor}.
+ * The usage should follow the {@code #sendRequest} call with the same
+ * FileDescriptor.
+ *
+ * The overall response may be large but the individual messages should not be
+ * excessively large(8-16kB) because trying to get the kernel to return
+ * everything in one big buffer is inefficient as it forces the kernel to allocate
+ * large chunks of linearly physically contiguous memory. The usage should iterate the
+ * call of this method until the end of the overall message.
+ *
+ * The default receiving buffer size should be small enough that it is always
+ * processed within the {@link NetlinkUtils#IO_TIMEOUT_MS} timeout.
+ */
+ public ByteBuffer recvSockDiagResponse(@NonNull final FileDescriptor fd)
+ throws ErrnoException, InterruptedIOException {
+ return NetlinkUtils.recvMessage(
+ fd, NetlinkUtils.DEFAULT_RECV_BUFSIZE, NetlinkUtils.IO_TIMEOUT_MS);
+ }
+
+ /**
+ * Construct a new KeepaliveTracker.
+ */
+ public KeepaliveTracker newKeepaliveTracker(@NonNull Context context,
+ @NonNull Handler connectivityserviceHander) {
+ return new KeepaliveTracker(mContext, connectivityserviceHander);
+ }
+
+ /**
+ * Find out if a feature is enabled from DeviceConfig.
+ *
+ * @param name The name of the property to look up.
+ * @return whether the feature is enabled
+ */
+ public boolean isFeatureEnabled(@NonNull final String name) {
+ return DeviceConfigUtils.isFeatureEnabled(mContext, NAMESPACE_CONNECTIVITY, name);
+ }
+ }
+}
diff --git a/service/src/com/android/server/connectivity/KeepaliveTracker.java b/service/src/com/android/server/connectivity/KeepaliveTracker.java
index 3b58823..03f8f3e 100644
--- a/service/src/com/android/server/connectivity/KeepaliveTracker.java
+++ b/service/src/com/android/server/connectivity/KeepaliveTracker.java
@@ -18,7 +18,6 @@
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.net.NattSocketKeepalive.NATT_PORT;
-import static android.net.NetworkAgent.CMD_START_SOCKET_KEEPALIVE;
import static android.net.SocketKeepalive.BINDER_DIED;
import static android.net.SocketKeepalive.DATA_RECEIVED;
import static android.net.SocketKeepalive.ERROR_INSUFFICIENT_RESOURCES;
@@ -88,7 +87,6 @@
/** Keeps track of keepalive requests. */
private final HashMap <NetworkAgentInfo, HashMap<Integer, KeepaliveInfo>> mKeepalives =
new HashMap<> ();
- private final Handler mConnectivityServiceHandler;
@NonNull
private final TcpKeepaliveController mTcpController;
@NonNull
@@ -109,9 +107,9 @@
private final int mAllowedUnprivilegedSlotsForUid;
public KeepaliveTracker(Context context, Handler handler) {
- mConnectivityServiceHandler = handler;
mTcpController = new TcpKeepaliveController(handler);
mContext = context;
+
mSupportedKeepalives = KeepaliveUtils.getSupportedKeepalives(mContext);
final ConnectivityResources res = new ConnectivityResources(mContext);
@@ -129,13 +127,13 @@
*/
class KeepaliveInfo implements IBinder.DeathRecipient {
// Bookkeeping data.
- private final ISocketKeepaliveCallback mCallback;
+ public final ISocketKeepaliveCallback mCallback;
private final int mUid;
private final int mPid;
private final boolean mPrivileged;
- private final NetworkAgentInfo mNai;
+ public final NetworkAgentInfo mNai;
private final int mType;
- private final FileDescriptor mFd;
+ public final FileDescriptor mFd;
public static final int TYPE_NATT = 1;
public static final int TYPE_TCP = 2;
@@ -243,6 +241,10 @@
}
}
+ public int getSlot() {
+ return mSlot;
+ }
+
private int checkNetworkConnected() {
if (!mNai.networkInfo.isConnectedOrConnecting()) {
return ERROR_INVALID_NETWORK;
@@ -415,6 +417,13 @@
void onFileDescriptorInitiatedStop(final int socketKeepaliveReason) {
handleStopKeepalive(mNai, mSlot, socketKeepaliveReason);
}
+
+ /**
+ * Construct a new KeepaliveInfo from existing KeepaliveInfo with a new fd.
+ */
+ public KeepaliveInfo withFd(@NonNull FileDescriptor fd) throws InvalidSocketException {
+ return new KeepaliveInfo(mCallback, mNai, mPacket, mInterval, mType, fd);
+ }
}
void notifyErrorCallback(ISocketKeepaliveCallback cb, int error) {
@@ -444,6 +453,9 @@
return slot;
}
+ /**
+ * Handle start keepalives with the message.
+ */
public void handleStartKeepalive(Message message) {
KeepaliveInfo ki = (KeepaliveInfo) message.obj;
NetworkAgentInfo nai = ki.getNai();
@@ -604,7 +616,8 @@
* Called when requesting that keepalives be started on a IPsec NAT-T socket. See
* {@link android.net.SocketKeepalive}.
**/
- public void startNattKeepalive(@Nullable NetworkAgentInfo nai,
+ @Nullable
+ public KeepaliveInfo makeNattKeepaliveInfo(@Nullable NetworkAgentInfo nai,
@Nullable FileDescriptor fd,
int intervalSeconds,
@NonNull ISocketKeepaliveCallback cb,
@@ -614,7 +627,7 @@
int dstPort) {
if (nai == null) {
notifyErrorCallback(cb, ERROR_INVALID_NETWORK);
- return;
+ return null;
}
InetAddress srcAddress, dstAddress;
@@ -623,7 +636,7 @@
dstAddress = InetAddresses.parseNumericAddress(dstAddrString);
} catch (IllegalArgumentException e) {
notifyErrorCallback(cb, ERROR_INVALID_IP_ADDRESS);
- return;
+ return null;
}
KeepalivePacketData packet;
@@ -632,7 +645,7 @@
srcAddress, srcPort, dstAddress, NATT_PORT);
} catch (InvalidPacketException e) {
notifyErrorCallback(cb, e.getError());
- return;
+ return null;
}
KeepaliveInfo ki = null;
try {
@@ -641,15 +654,14 @@
} catch (InvalidSocketException | IllegalArgumentException | SecurityException e) {
Log.e(TAG, "Fail to construct keepalive", e);
notifyErrorCallback(cb, ERROR_INVALID_SOCKET);
- return;
+ return null;
}
- Log.d(TAG, "Created keepalive: " + ki.toString());
- mConnectivityServiceHandler.obtainMessage(
- NetworkAgent.CMD_START_SOCKET_KEEPALIVE, ki).sendToTarget();
+ Log.d(TAG, "Created keepalive: " + ki);
+ return ki;
}
/**
- * Called by ConnectivityService to start TCP keepalive on a file descriptor.
+ * Make a KeepaliveInfo for a TCP socket.
*
* In order to offload keepalive for application correctly, sequence number, ack number and
* other fields are needed to form the keepalive packet. Thus, this function synchronously
@@ -658,13 +670,14 @@
*
* See {@link android.net.SocketKeepalive}.
**/
- public void startTcpKeepalive(@Nullable NetworkAgentInfo nai,
+ @Nullable
+ public KeepaliveInfo makeTcpKeepaliveInfo(@Nullable NetworkAgentInfo nai,
@NonNull FileDescriptor fd,
int intervalSeconds,
@NonNull ISocketKeepaliveCallback cb) {
if (nai == null) {
notifyErrorCallback(cb, ERROR_INVALID_NETWORK);
- return;
+ return null;
}
final TcpKeepalivePacketData packet;
@@ -672,10 +685,10 @@
packet = TcpKeepaliveController.getTcpKeepalivePacket(fd);
} catch (InvalidSocketException e) {
notifyErrorCallback(cb, e.error);
- return;
+ return null;
} catch (InvalidPacketException e) {
notifyErrorCallback(cb, e.getError());
- return;
+ return null;
}
KeepaliveInfo ki = null;
try {
@@ -684,20 +697,22 @@
} catch (InvalidSocketException | IllegalArgumentException | SecurityException e) {
Log.e(TAG, "Fail to construct keepalive e=" + e);
notifyErrorCallback(cb, ERROR_INVALID_SOCKET);
- return;
+ return null;
}
Log.d(TAG, "Created keepalive: " + ki.toString());
- mConnectivityServiceHandler.obtainMessage(CMD_START_SOCKET_KEEPALIVE, ki).sendToTarget();
+ return ki;
}
- /**
- * Called when requesting that keepalives be started on a IPsec NAT-T socket. This function is
- * identical to {@link #startNattKeepalive}, but also takes a {@code resourceId}, which is the
- * resource index bound to the {@link UdpEncapsulationSocket} when creating by
- * {@link com.android.server.IpSecService} to verify whether the given
- * {@link UdpEncapsulationSocket} is legitimate.
- **/
- public void startNattKeepalive(@Nullable NetworkAgentInfo nai,
+ /**
+ * Make a KeepaliveInfo for an IPSec NAT-T socket.
+ *
+ * This function is identical to {@link #makeNattKeepaliveInfo}, but also takes a
+ * {@code resourceId}, which is the resource index bound to the {@link UdpEncapsulationSocket}
+ * when creating by {@link com.android.server.IpSecService} to verify whether the given
+ * {@link UdpEncapsulationSocket} is legitimate.
+ **/
+ @Nullable
+ public KeepaliveInfo makeNattKeepaliveInfo(@Nullable NetworkAgentInfo nai,
@Nullable FileDescriptor fd,
int resourceId,
int intervalSeconds,
@@ -708,6 +723,7 @@
// Ensure that the socket is created by IpSecService.
if (!isNattKeepaliveSocketValid(fd, resourceId)) {
notifyErrorCallback(cb, ERROR_INVALID_SOCKET);
+ return null;
}
// Get src port to adopt old API.
@@ -717,10 +733,11 @@
srcPort = ((InetSocketAddress) srcSockAddr).getPort();
} catch (ErrnoException e) {
notifyErrorCallback(cb, ERROR_INVALID_SOCKET);
+ return null;
}
// Forward request to old API.
- startNattKeepalive(nai, fd, intervalSeconds, cb, srcAddrString, srcPort,
+ return makeNattKeepaliveInfo(nai, fd, intervalSeconds, cb, srcAddrString, srcPort,
dstAddrString, dstPort);
}
@@ -739,6 +756,9 @@
return true;
}
+ /**
+ * Dump KeepaliveTracker state.
+ */
public void dump(IndentingPrintWriter pw) {
pw.println("Supported Socket keepalives: " + Arrays.toString(mSupportedKeepalives));
pw.println("Reserved Privileged keepalives: " + mReservedPrivilegedSlots);
diff --git a/service/src/com/android/server/connectivity/ProfileNetworkPreferenceInfo.java b/service/src/com/android/server/connectivity/ProfileNetworkPreferenceInfo.java
index 10f3886..7679660 100644
--- a/service/src/com/android/server/connectivity/ProfileNetworkPreferenceInfo.java
+++ b/service/src/com/android/server/connectivity/ProfileNetworkPreferenceInfo.java
@@ -32,13 +32,15 @@
@Nullable
public final NetworkCapabilities capabilities;
public final boolean allowFallback;
+ public final boolean blockingNonEnterprise;
public ProfileNetworkPreferenceInfo(@NonNull final UserHandle user,
@Nullable final NetworkCapabilities capabilities,
- final boolean allowFallback) {
+ final boolean allowFallback, final boolean blockingNonEnterprise) {
this.user = user;
this.capabilities = null == capabilities ? null : new NetworkCapabilities(capabilities);
this.allowFallback = allowFallback;
+ this.blockingNonEnterprise = blockingNonEnterprise;
}
@Override
@@ -57,6 +59,7 @@
return "[ProfileNetworkPreference user=" + user
+ " caps=" + capabilities
+ " allowFallback=" + allowFallback
+ + " blockingNonEnterprise=" + blockingNonEnterprise
+ "]";
}
}
diff --git a/tests/common/java/android/net/netstats/NetworkTemplateTest.kt b/tests/common/java/android/net/netstats/NetworkTemplateTest.kt
index 3c2340c..99f1e0b 100644
--- a/tests/common/java/android/net/netstats/NetworkTemplateTest.kt
+++ b/tests/common/java/android/net/netstats/NetworkTemplateTest.kt
@@ -26,10 +26,8 @@
import android.net.NetworkTemplate.MATCH_CARRIER
import android.net.NetworkTemplate.MATCH_ETHERNET
import android.net.NetworkTemplate.MATCH_MOBILE
-import android.net.NetworkTemplate.MATCH_MOBILE_WILDCARD
import android.net.NetworkTemplate.MATCH_PROXY
import android.net.NetworkTemplate.MATCH_WIFI
-import android.net.NetworkTemplate.MATCH_WIFI_WILDCARD
import android.net.NetworkTemplate.NETWORK_TYPE_ALL
import android.net.NetworkTemplate.OEM_MANAGED_ALL
import android.telephony.TelephonyManager
@@ -64,10 +62,8 @@
}
// Verify hidden match rules cannot construct templates.
- listOf(MATCH_WIFI_WILDCARD, MATCH_MOBILE_WILDCARD, MATCH_PROXY).forEach {
- assertFailsWith<IllegalArgumentException> {
- NetworkTemplate.Builder(it).build()
- }
+ assertFailsWith<IllegalArgumentException> {
+ NetworkTemplate.Builder(MATCH_PROXY).build()
}
// Verify template which matches metered cellular and carrier networks with
@@ -75,10 +71,9 @@
listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule ->
NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1))
.setMeteredness(METERED_YES).build().let {
- val expectedTemplate = NetworkTemplate(matchRule, TEST_IMSI1,
- arrayOf(TEST_IMSI1), emptyArray<String>(), METERED_YES,
- ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
- OEM_MANAGED_ALL)
+ val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1),
+ emptyArray<String>(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL,
+ NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
assertEquals(expectedTemplate, it)
}
}
@@ -88,10 +83,9 @@
listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule ->
NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1))
.setRoaming(ROAMING_YES).setMeteredness(METERED_YES).build().let {
- val expectedTemplate = NetworkTemplate(matchRule, TEST_IMSI1,
- arrayOf(TEST_IMSI1), emptyArray<String>(), METERED_YES,
- ROAMING_YES, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
- OEM_MANAGED_ALL)
+ val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1),
+ emptyArray<String>(), METERED_YES, ROAMING_YES, DEFAULT_NETWORK_ALL,
+ NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
assertEquals(expectedTemplate, it)
}
}
@@ -104,42 +98,39 @@
// Verify template which matches metered cellular networks,
// regardless of IMSI. See buildTemplateMobileWildcard.
NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES).build().let {
- val expectedTemplate = NetworkTemplate(MATCH_MOBILE_WILDCARD, null /*subscriberId*/,
- emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
+ val expectedTemplate = NetworkTemplate(MATCH_MOBILE,
+ emptyArray<String>() /*subscriberIds*/, emptyArray<String>() /*wifiNetworkKey*/,
METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
OEM_MANAGED_ALL)
assertEquals(expectedTemplate, it)
}
// Verify template which matches metered cellular networks and ratType.
- // See NetworkTemplate#buildTemplateMobileWithRatType.
NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(TEST_IMSI1))
.setMeteredness(METERED_YES).setRatType(TelephonyManager.NETWORK_TYPE_UMTS)
.build().let {
- val expectedTemplate = NetworkTemplate(MATCH_MOBILE, TEST_IMSI1,
- arrayOf(TEST_IMSI1), emptyArray<String>(), METERED_YES,
- ROAMING_ALL, DEFAULT_NETWORK_ALL, TelephonyManager.NETWORK_TYPE_UMTS,
- OEM_MANAGED_ALL)
+ val expectedTemplate = NetworkTemplate(MATCH_MOBILE, arrayOf(TEST_IMSI1),
+ emptyArray<String>(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL,
+ TelephonyManager.NETWORK_TYPE_UMTS, OEM_MANAGED_ALL)
assertEquals(expectedTemplate, it)
}
// Verify template which matches all wifi networks,
// regardless of Wifi Network Key. See buildTemplateWifiWildcard and buildTemplateWifi.
NetworkTemplate.Builder(MATCH_WIFI).build().let {
- val expectedTemplate = NetworkTemplate(MATCH_WIFI_WILDCARD, null /*subscriberId*/,
- emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
- METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
- OEM_MANAGED_ALL)
+ val expectedTemplate = NetworkTemplate(MATCH_WIFI,
+ emptyArray<String>() /*subscriberIds*/, emptyArray<String>(), METERED_ALL,
+ ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
assertEquals(expectedTemplate, it)
}
// Verify template which matches wifi networks with the given Wifi Network Key.
// See buildTemplateWifi(wifiNetworkKey).
NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let {
- val expectedTemplate = NetworkTemplate(MATCH_WIFI, null /*subscriberId*/,
- emptyArray<String>() /*subscriberIds*/, arrayOf(TEST_WIFI_KEY1),
- METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
- OEM_MANAGED_ALL)
+ val expectedTemplate =
+ NetworkTemplate(MATCH_WIFI, emptyArray<String>() /*subscriberIds*/,
+ arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
+ NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
assertEquals(expectedTemplate, it)
}
@@ -147,10 +138,9 @@
// given Wifi Network Key, and IMSI. See buildTemplateWifi(wifiNetworkKey, subscriberId).
NetworkTemplate.Builder(MATCH_WIFI).setSubscriberIds(setOf(TEST_IMSI1))
.setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let {
- val expectedTemplate = NetworkTemplate(MATCH_WIFI, TEST_IMSI1,
- arrayOf(TEST_IMSI1), arrayOf(TEST_WIFI_KEY1),
- METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
- OEM_MANAGED_ALL)
+ val expectedTemplate = NetworkTemplate(MATCH_WIFI, arrayOf(TEST_IMSI1),
+ arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
+ NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
assertEquals(expectedTemplate, it)
}
@@ -158,7 +148,7 @@
// See buildTemplateEthernet and buildTemplateBluetooth.
listOf(MATCH_ETHERNET, MATCH_BLUETOOTH).forEach { matchRule ->
NetworkTemplate.Builder(matchRule).build().let {
- val expectedTemplate = NetworkTemplate(matchRule, null /*subscriberId*/,
+ val expectedTemplate = NetworkTemplate(matchRule,
emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
OEM_MANAGED_ALL)
@@ -193,7 +183,7 @@
// Verify template which matches wifi wildcard with the given empty key set.
NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf<String>()).build().let {
- val expectedTemplate = NetworkTemplate(MATCH_WIFI_WILDCARD, null /*subscriberId*/,
+ val expectedTemplate = NetworkTemplate(MATCH_WIFI,
emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
OEM_MANAGED_ALL)
diff --git a/tests/cts/OWNERS b/tests/cts/OWNERS
index 089d06f..8388cb7 100644
--- a/tests/cts/OWNERS
+++ b/tests/cts/OWNERS
@@ -2,6 +2,5 @@
set noparent
file:platform/packages/modules/Connectivity:master:/OWNERS_core_networking_xts
-# Only temporary ownership to improve ethernet code quality (b/236280707)
-# TODO: remove by 12/31/2022
-per-file net/src/android/net/cts/EthernetManagerTest.kt = prohr@google.com #{LAST_RESORT_SUGGESTION}
+# IPsec
+per-file **IpSec* = benedictwong@google.com, nharold@google.com
diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
index 0e04a80..61b597a 100644
--- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
@@ -2391,7 +2391,7 @@
getHistory().add(new CallbackEntry.BlockedStatusInt(network, blockedReasons));
}
private void assertNoBlockedStatusCallback() {
- super.assertNoCallbackThat(NO_CALLBACK_TIMEOUT_MS,
+ super.assertNoCallback(NO_CALLBACK_TIMEOUT_MS,
c -> c instanceof CallbackEntry.BlockedStatus);
}
}
@@ -2979,7 +2979,7 @@
defaultCb.eventuallyExpect(CallbackEntry.AVAILABLE, NETWORK_CALLBACK_TIMEOUT_MS,
entry -> cellNetwork.equals(entry.getNetwork()));
// The network should not validate again.
- wifiCb.assertNoCallbackThat(NO_CALLBACK_TIMEOUT_MS, c -> isValidatedCaps(c));
+ wifiCb.assertNoCallback(NO_CALLBACK_TIMEOUT_MS, c -> isValidatedCaps(c));
} finally {
resetAvoidBadWifi(previousAvoidBadWifi);
mHttpServer.stop();
@@ -3151,7 +3151,7 @@
*/
private void assertNoCallbackExceptCapOrLpChange(
@NonNull final TestableNetworkCallback cb) {
- cb.assertNoCallbackThat(NO_CALLBACK_TIMEOUT_MS,
+ cb.assertNoCallback(NO_CALLBACK_TIMEOUT_MS,
c -> !(c instanceof CallbackEntry.CapabilitiesChanged
|| c instanceof CallbackEntry.LinkPropertiesChanged));
}
diff --git a/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt b/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt
index 7e91478..b924f65 100644
--- a/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt
+++ b/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt
@@ -57,8 +57,8 @@
import android.os.Handler
import android.os.Looper
import android.os.OutcomeReceiver
-import android.os.SystemProperties
import android.os.Process
+import android.os.SystemProperties
import android.platform.test.annotations.AppModeFull
import androidx.test.platform.app.InstrumentationRegistry
import com.android.net.module.util.ArrayTrackRecord
@@ -77,16 +77,10 @@
import com.android.testutils.assertThrows
import com.android.testutils.runAsShell
import com.android.testutils.waitForIdle
-import org.junit.After
-import org.junit.Assume.assumeFalse
-import org.junit.Assume.assumeTrue
-import org.junit.Before
-import org.junit.Test
-import org.junit.runner.RunWith
import java.io.IOException
import java.net.Inet6Address
-import java.util.Random
import java.net.Socket
+import java.util.Random
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
@@ -99,6 +93,12 @@
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.test.fail
+import org.junit.After
+import org.junit.Assume.assumeFalse
+import org.junit.Assume.assumeTrue
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
private const val TAG = "EthernetManagerTest"
// This timeout does not affect the test duration for passing tests. It needs to be long enough to
@@ -530,12 +530,10 @@
eventuallyExpect(Lost::class) { n?.equals(it.network) ?: true }
private fun TestableNetworkCallback.assertNeverLost(n: Network? = null) =
- assertNoCallbackThat() {
- it is Lost && (n?.equals(it.network) ?: true)
- }
+ assertNoCallback { it is Lost && (n?.equals(it.network) ?: true) }
private fun TestableNetworkCallback.assertNeverAvailable(n: Network? = null) =
- assertNoCallbackThat { it is Available && (n?.equals(it.network) ?: true) }
+ assertNoCallback { it is Available && (n?.equals(it.network) ?: true) }
private fun TestableNetworkCallback.expectCapabilitiesWithInterfaceName(name: String) =
expect<CapabilitiesChanged> { it.caps.networkSpecifier == EthernetNetworkSpecifier(name) }
diff --git a/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java b/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java
index a9a3380..5fc3068 100644
--- a/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java
+++ b/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java
@@ -32,10 +32,13 @@
import static android.system.OsConstants.AF_INET;
import static android.system.OsConstants.AF_INET6;
+import static com.android.compatibility.common.util.PropertyUtil.getVsrApiLevel;
+
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
@@ -80,6 +83,11 @@
private static final String TAG = IpSecManagerTunnelTest.class.getSimpleName();
+ // Redefine this flag here so that IPsec code shipped in a mainline module can build on old
+ // platforms before FEATURE_IPSEC_TUNNEL_MIGRATION API is released.
+ private static final String FEATURE_IPSEC_TUNNEL_MIGRATION =
+ "android.software.ipsec_tunnel_migration";
+
private static final InetAddress LOCAL_OUTER_4 = InetAddress.parseNumericAddress("192.0.2.1");
private static final InetAddress REMOTE_OUTER_4 = InetAddress.parseNumericAddress("192.0.2.2");
private static final InetAddress LOCAL_OUTER_6 =
@@ -994,6 +1002,41 @@
checkTunnelInput(innerFamily, outerFamily, useEncap, transportInTunnelMode);
}
+ /** Checks if FEATURE_IPSEC_TUNNEL_MIGRATION is enabled on the device */
+ private static boolean hasIpsecTunnelMigrateFeature() {
+ return sContext.getPackageManager().hasSystemFeature(FEATURE_IPSEC_TUNNEL_MIGRATION);
+ }
+
+ @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
+ @Test
+ public void testHasIpSecTunnelMigrateFeature() throws Exception {
+ // FEATURE_IPSEC_TUNNEL_MIGRATION is required when VSR API is U/U+
+ if (getVsrApiLevel() > Build.VERSION_CODES.TIRAMISU) {
+ assertTrue(hasIpsecTunnelMigrateFeature());
+ }
+ }
+
+ @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
+ @Test
+ public void testMigrateTunnelModeTransform() throws Exception {
+ assumeTrue(mCtsNetUtils.hasIpsecTunnelsFeature());
+ assumeTrue(hasIpsecTunnelMigrateFeature());
+
+ IpSecTransform.Builder transformBuilder = new IpSecTransform.Builder(sContext);
+ transformBuilder.setEncryption(new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, CRYPT_KEY));
+ transformBuilder.setAuthentication(
+ new IpSecAlgorithm(IpSecAlgorithm.AUTH_HMAC_SHA256, AUTH_KEY, AUTH_KEY.length * 4));
+ int spi = getRandomSpi(LOCAL_OUTER_4, REMOTE_OUTER_4);
+
+ try (IpSecManager.SecurityParameterIndex outSpi =
+ mISM.allocateSecurityParameterIndex(REMOTE_OUTER_4, spi);
+ IpSecTransform outTunnelTransform =
+ transformBuilder.buildTunnelModeTransform(LOCAL_INNER_4, outSpi)) {
+ mISM.startTunnelModeTransformMigration(
+ outTunnelTransform, LOCAL_OUTER_4_NEW, REMOTE_OUTER_4_NEW);
+ }
+ }
+
// Transport-in-Tunnel mode tests
@Test
public void testTransportInTunnelModeV4InV4() throws Exception {
diff --git a/tests/cts/net/src/android/net/cts/NsdManagerTest.kt b/tests/cts/net/src/android/net/cts/NsdManagerTest.kt
index 2b5c305..b7eb009 100644
--- a/tests/cts/net/src/android/net/cts/NsdManagerTest.kt
+++ b/tests/cts/net/src/android/net/cts/NsdManagerTest.kt
@@ -41,7 +41,9 @@
import android.net.cts.NsdManagerTest.NsdRegistrationRecord.RegistrationEvent.ServiceUnregistered
import android.net.cts.NsdManagerTest.NsdRegistrationRecord.RegistrationEvent.UnregistrationFailed
import android.net.cts.NsdManagerTest.NsdResolveRecord.ResolveEvent.ResolveFailed
+import android.net.cts.NsdManagerTest.NsdResolveRecord.ResolveEvent.ResolveStopped
import android.net.cts.NsdManagerTest.NsdResolveRecord.ResolveEvent.ServiceResolved
+import android.net.cts.NsdManagerTest.NsdResolveRecord.ResolveEvent.StopResolutionFailed
import android.net.nsd.NsdManager
import android.net.nsd.NsdManager.DiscoveryListener
import android.net.nsd.NsdManager.RegistrationListener
@@ -66,15 +68,6 @@
import com.android.testutils.runAsShell
import com.android.testutils.tryTest
import com.android.testutils.waitForIdle
-import org.junit.After
-import org.junit.Assert.assertArrayEquals
-import org.junit.Assert.assertFalse
-import org.junit.Assert.assertTrue
-import org.junit.Assume.assumeTrue
-import org.junit.Before
-import org.junit.Rule
-import org.junit.Test
-import org.junit.runner.RunWith
import java.io.File
import java.net.ServerSocket
import java.nio.charset.StandardCharsets
@@ -86,6 +79,15 @@
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.test.fail
+import org.junit.After
+import org.junit.Assert.assertArrayEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Assume.assumeTrue
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
private const val TAG = "NsdManagerTest"
private const val TIMEOUT_MS = 2000L
@@ -182,10 +184,10 @@
val errorCode: Int
) : RegistrationEvent()
- data class ServiceRegistered(override val serviceInfo: NsdServiceInfo)
- : RegistrationEvent()
- data class ServiceUnregistered(override val serviceInfo: NsdServiceInfo)
- : RegistrationEvent()
+ data class ServiceRegistered(override val serviceInfo: NsdServiceInfo) :
+ RegistrationEvent()
+ data class ServiceUnregistered(override val serviceInfo: NsdServiceInfo) :
+ RegistrationEvent()
}
override fun onRegistrationFailed(si: NsdServiceInfo, err: Int) {
@@ -208,11 +210,11 @@
private class NsdDiscoveryRecord(expectedThreadId: Int? = null) :
DiscoveryListener, NsdRecord<NsdDiscoveryRecord.DiscoveryEvent>(expectedThreadId) {
sealed class DiscoveryEvent : NsdEvent {
- data class StartDiscoveryFailed(val serviceType: String, val errorCode: Int)
- : DiscoveryEvent()
+ data class StartDiscoveryFailed(val serviceType: String, val errorCode: Int) :
+ DiscoveryEvent()
- data class StopDiscoveryFailed(val serviceType: String, val errorCode: Int)
- : DiscoveryEvent()
+ data class StopDiscoveryFailed(val serviceType: String, val errorCode: Int) :
+ DiscoveryEvent()
data class DiscoveryStarted(val serviceType: String) : DiscoveryEvent()
data class DiscoveryStopped(val serviceType: String) : DiscoveryEvent()
@@ -259,10 +261,13 @@
private class NsdResolveRecord : ResolveListener,
NsdRecord<NsdResolveRecord.ResolveEvent>() {
sealed class ResolveEvent : NsdEvent {
- data class ResolveFailed(val serviceInfo: NsdServiceInfo, val errorCode: Int)
- : ResolveEvent()
+ data class ResolveFailed(val serviceInfo: NsdServiceInfo, val errorCode: Int) :
+ ResolveEvent()
data class ServiceResolved(val serviceInfo: NsdServiceInfo) : ResolveEvent()
+ data class ResolveStopped(val serviceInfo: NsdServiceInfo) : ResolveEvent()
+ data class StopResolutionFailed(val serviceInfo: NsdServiceInfo, val errorCode: Int) :
+ ResolveEvent()
}
override fun onResolveFailed(si: NsdServiceInfo, err: Int) {
@@ -272,6 +277,14 @@
override fun onServiceResolved(si: NsdServiceInfo) {
add(ServiceResolved(si))
}
+
+ override fun onResolveStopped(si: NsdServiceInfo) {
+ add(ResolveStopped(si))
+ }
+
+ override fun onStopResolutionFailed(si: NsdServiceInfo, err: Int) {
+ add(StopResolutionFailed(si, err))
+ }
}
@Before
@@ -739,6 +752,26 @@
NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS_T_AND_LATER))
}
+ @Test
+ fun testStopServiceResolution() {
+ // This test requires shims supporting U+ APIs (NsdManager.stopServiceResolution)
+ assumeTrue(TestUtils.shouldTestUApis())
+
+ val si = NsdServiceInfo()
+ si.serviceType = this@NsdManagerTest.serviceType
+ si.serviceName = this@NsdManagerTest.serviceName
+ si.port = 12345 // Test won't try to connect so port does not matter
+
+ val resolveRecord = NsdResolveRecord()
+ // Try to resolve an unknown service then stop it immediately.
+ // Expected ResolveStopped callback.
+ nsdShim.resolveService(nsdManager, si, { it.run() }, resolveRecord)
+ nsdShim.stopServiceResolution(nsdManager, resolveRecord)
+ val stoppedCb = resolveRecord.expectCallback<ResolveStopped>()
+ assertEquals(si.serviceName, stoppedCb.serviceInfo.serviceName)
+ assertEquals(si.serviceType, stoppedCb.serviceInfo.serviceType)
+ }
+
/**
* Register a service and return its registration record.
*/
diff --git a/tests/cts/net/src/android/net/cts/RateLimitTest.java b/tests/cts/net/src/android/net/cts/RateLimitTest.java
index 28cec1a..36b98fc 100644
--- a/tests/cts/net/src/android/net/cts/RateLimitTest.java
+++ b/tests/cts/net/src/android/net/cts/RateLimitTest.java
@@ -301,29 +301,32 @@
public void testIngressRateLimit_testLimit() throws Exception {
assumeKernelSupport();
+ // These tests are not very precise, especially on lower-end devices.
+ // Add 30% tolerance to reduce test flakiness. Burst size is constant at 128KiB.
+ final double toleranceFactor = 1.3;
+
// If this value is too low, this test might become flaky because of the burst value that
// allows to send at a higher data rate for a short period of time. The faster the data rate
// and the longer the test, the less this test will be affected.
final long dataLimitInBytesPerSecond = 2_000_000; // 2MB/s
long resultInBytesPerSecond = runIngressDataRateMeasurement(Duration.ofSeconds(1));
assertGreaterThan("Failed initial test with rate limit disabled", resultInBytesPerSecond,
- dataLimitInBytesPerSecond);
+ (long) (dataLimitInBytesPerSecond * toleranceFactor));
// enable rate limit and wait until the tc filter is installed before starting the test.
ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext,
dataLimitInBytesPerSecond);
waitForTcPoliceFilterInstalled(Duration.ofSeconds(1));
- resultInBytesPerSecond = runIngressDataRateMeasurement(Duration.ofSeconds(10));
- // Add 10% tolerance to reduce test flakiness. Burst size is constant at 128KiB.
+ resultInBytesPerSecond = runIngressDataRateMeasurement(Duration.ofSeconds(15));
assertLessThan("Failed test with rate limit enabled", resultInBytesPerSecond,
- (long) (dataLimitInBytesPerSecond * 1.1));
+ (long) (dataLimitInBytesPerSecond * toleranceFactor));
ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, -1);
resultInBytesPerSecond = runIngressDataRateMeasurement(Duration.ofSeconds(1));
assertGreaterThan("Failed test with rate limit disabled", resultInBytesPerSecond,
- dataLimitInBytesPerSecond);
+ (long) (dataLimitInBytesPerSecond * toleranceFactor));
}
@Test
diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp
index 209430a..e0de246 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -73,8 +73,6 @@
"java/com/android/server/connectivity/NetdEventListenerServiceTest.java",
"java/com/android/server/connectivity/VpnTest.java",
"java/com/android/server/net/ipmemorystore/*.java",
- "java/com/android/server/connectivity/mdns/**/*.java",
- "java/com/android/server/connectivity/mdns/**/*.kt",
]
}
@@ -149,7 +147,6 @@
static_libs: [
"services.core",
"services.net",
- "service-mdns",
],
jni_libs: [
"libandroid_net_connectivity_com_android_net_module_util_jni",
diff --git a/tests/unit/java/android/net/IpSecTransformTest.java b/tests/unit/java/android/net/IpSecTransformTest.java
index ec59064..8bc1bbd 100644
--- a/tests/unit/java/android/net/IpSecTransformTest.java
+++ b/tests/unit/java/android/net/IpSecTransformTest.java
@@ -143,8 +143,9 @@
@Test
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
- public void testStartMigration() throws Exception {
- mIpSecManager.startMigration(buildTestTransform(), SRC_ADDRESS_V6, DST_ADDRESS_V6);
+ public void testStartTransformMigration() throws Exception {
+ mIpSecManager.startTunnelModeTransformMigration(
+ buildTestTransform(), SRC_ADDRESS_V6, DST_ADDRESS_V6);
verify(mMockIpSecService)
.migrateTransform(
anyInt(),
@@ -155,9 +156,10 @@
@Test
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.TIRAMISU)
- public void testStartMigrationOnSdkBeforeU() throws Exception {
+ public void testStartTransformMigrationOnSdkBeforeU() throws Exception {
try {
- mIpSecManager.startMigration(buildTestTransform(), SRC_ADDRESS_V6, DST_ADDRESS_V6);
+ mIpSecManager.startTunnelModeTransformMigration(
+ buildTestTransform(), SRC_ADDRESS_V6, DST_ADDRESS_V6);
fail("Expect to fail since migration is not supported before U");
} catch (UnsupportedOperationException expected) {
}
diff --git a/tests/unit/java/android/net/NetworkTemplateTest.kt b/tests/unit/java/android/net/NetworkTemplateTest.kt
index c3440c5..78854fb 100644
--- a/tests/unit/java/android/net/NetworkTemplateTest.kt
+++ b/tests/unit/java/android/net/NetworkTemplateTest.kt
@@ -34,10 +34,8 @@
import android.net.NetworkStats.ROAMING_ALL
import android.net.NetworkTemplate.MATCH_CARRIER
import android.net.NetworkTemplate.MATCH_MOBILE
-import android.net.NetworkTemplate.MATCH_MOBILE_WILDCARD
import android.net.NetworkTemplate.MATCH_TEST
import android.net.NetworkTemplate.MATCH_WIFI
-import android.net.NetworkTemplate.MATCH_WIFI_WILDCARD
import android.net.NetworkTemplate.NETWORK_TYPE_ALL
import android.net.NetworkTemplate.OEM_MANAGED_ALL
import android.net.NetworkTemplate.OEM_MANAGED_NO
@@ -231,7 +229,6 @@
val templateMobileWildcard = buildTemplateMobileWildcard()
val templateMobileNullImsiWithRatType = NetworkTemplate.Builder(MATCH_MOBILE)
.setRatType(TelephonyManager.NETWORK_TYPE_UMTS).build()
-
val mobileImsi1 = buildMobileNetworkState(TEST_IMSI1)
val identMobile1 = buildNetworkIdentity(mockContext, mobileImsi1,
false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
@@ -447,18 +444,18 @@
@Test
fun testParcelUnparcel() {
- val templateMobile = NetworkTemplate(MATCH_MOBILE, TEST_IMSI1, arrayOf(TEST_IMSI1),
+ val templateMobile = NetworkTemplate(MATCH_MOBILE, arrayOf(TEST_IMSI1),
emptyArray<String>(), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
TelephonyManager.NETWORK_TYPE_LTE, OEM_MANAGED_ALL)
- val templateWifi = NetworkTemplate(MATCH_WIFI, null, emptyArray<String>(),
+ val templateWifi = NetworkTemplate(MATCH_WIFI, emptyArray<String>(),
arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, 0,
OEM_MANAGED_ALL)
- val templateOem = NetworkTemplate(MATCH_MOBILE_WILDCARD, null, emptyArray<String>(),
+ val templateOem = NetworkTemplate(MATCH_MOBILE, emptyArray<String>(),
emptyArray<String>(), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, 0,
OEM_MANAGED_YES)
- assertParcelSane(templateMobile, 9)
- assertParcelSane(templateWifi, 9)
- assertParcelSane(templateOem, 9)
+ assertParcelSane(templateMobile, 8)
+ assertParcelSane(templateWifi, 8)
+ assertParcelSane(templateOem, 8)
}
// Verify NETWORK_TYPE_* constants in NetworkTemplate do not conflict with
@@ -491,13 +488,14 @@
* @param matchType A match rule from {@code NetworkTemplate.MATCH_*} corresponding to the
* networkType.
* @param subscriberId To be populated with {@code TEST_IMSI*} only if networkType is
- * {@code TYPE_MOBILE}. May be left as null when matchType is
- * {@link NetworkTemplate.MATCH_MOBILE_WILDCARD}.
- * @param templateWifiKey Top be populated with {@code TEST_WIFI_KEY*} only if networkType is
- * {@code TYPE_WIFI}. May be left as null when matchType is
- * {@link NetworkTemplate.MATCH_WIFI_WILDCARD}.
- * @param identWifiKey If networkType is {@code TYPE_WIFI}, this value must *NOT* be null. Provide
- * one of {@code TEST_WIFI_KEY*}.
+ * {@code TYPE_MOBILE}. Note that {@code MATCH_MOBILE} with an empty subscriberId list
+ * will match any subscriber ID.
+ * @param templateWifiKey To be populated with {@code TEST_WIFI_KEY*} only if networkType is
+ * {@code TYPE_WIFI}. Note that {@code MATCH_WIFI} with both an empty subscriberId list
+ * and an empty wifiNetworkKey list will match any subscriber ID and/or any wifi network
+ * key.
+ * @param identWifiKey If networkType is {@code TYPE_WIFI}, this value must *NOT* be null.
+ * Provide one of {@code TEST_WIFI_KEY*}.
*/
private fun matchOemManagedIdent(
networkType: Int,
@@ -507,13 +505,15 @@
identWifiKey: String? = null
) {
val oemManagedStates = arrayOf(OEM_NONE, OEM_PAID, OEM_PRIVATE, OEM_PAID or OEM_PRIVATE)
- val matchSubscriberIds = arrayOf(subscriberId)
- val matchWifiNetworkKeys = arrayOf(templateWifiKey)
+ val matchSubscriberIds =
+ if (subscriberId == null) emptyArray<String>() else arrayOf(subscriberId)
+ val matchWifiNetworkKeys =
+ if (templateWifiKey == null) emptyArray<String>() else arrayOf(templateWifiKey)
- val templateOemYes = NetworkTemplate(matchType, subscriberId, matchSubscriberIds,
+ val templateOemYes = NetworkTemplate(matchType, matchSubscriberIds,
matchWifiNetworkKeys, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_YES)
- val templateOemAll = NetworkTemplate(matchType, subscriberId, matchSubscriberIds,
+ val templateOemAll = NetworkTemplate(matchType, matchSubscriberIds,
matchWifiNetworkKeys, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
@@ -524,7 +524,7 @@
// Create a template with each OEM managed type and match it against the NetworkIdentity
for (templateOemManagedState in oemManagedStates) {
- val template = NetworkTemplate(matchType, subscriberId, matchSubscriberIds,
+ val template = NetworkTemplate(matchType, matchSubscriberIds,
matchWifiNetworkKeys, METERED_ALL, ROAMING_ALL,
DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, templateOemManagedState)
if (identityOemManagedState == templateOemManagedState) {
@@ -547,11 +547,10 @@
@Test
fun testOemManagedMatchesIdent() {
matchOemManagedIdent(TYPE_MOBILE, MATCH_MOBILE, subscriberId = TEST_IMSI1)
- matchOemManagedIdent(TYPE_MOBILE, MATCH_MOBILE_WILDCARD)
+ matchOemManagedIdent(TYPE_MOBILE, MATCH_MOBILE)
matchOemManagedIdent(TYPE_WIFI, MATCH_WIFI, templateWifiKey = TEST_WIFI_KEY1,
identWifiKey = TEST_WIFI_KEY1)
- matchOemManagedIdent(TYPE_WIFI, MATCH_WIFI_WILDCARD,
- identWifiKey = TEST_WIFI_KEY1)
+ matchOemManagedIdent(TYPE_WIFI, MATCH_WIFI, identWifiKey = TEST_WIFI_KEY1)
}
@Test
diff --git a/tests/unit/java/android/net/nsd/NsdServiceInfoTest.java b/tests/unit/java/android/net/nsd/NsdServiceInfoTest.java
index 64355ed..9ce0693 100644
--- a/tests/unit/java/android/net/nsd/NsdServiceInfoTest.java
+++ b/tests/unit/java/android/net/nsd/NsdServiceInfoTest.java
@@ -21,6 +21,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import android.net.InetAddresses;
import android.net.Network;
import android.os.Build;
import android.os.Bundle;
@@ -38,6 +39,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
+import java.util.List;
import java.util.Map;
@RunWith(DevSdkIgnoreRunner.class)
@@ -45,6 +47,8 @@
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
public class NsdServiceInfoTest {
+ private static final InetAddress IPV4_ADDRESS = InetAddresses.parseNumericAddress("192.0.2.1");
+ private static final InetAddress IPV6_ADDRESS = InetAddresses.parseNumericAddress("2001:db8::");
public final static InetAddress LOCALHOST;
static {
// Because test.
@@ -124,6 +128,7 @@
fullInfo.setServiceType("_kitten._tcp");
fullInfo.setPort(4242);
fullInfo.setHost(LOCALHOST);
+ fullInfo.setHostAddresses(List.of(IPV4_ADDRESS));
fullInfo.setNetwork(new Network(123));
fullInfo.setInterfaceIndex(456);
checkParcelable(fullInfo);
@@ -139,6 +144,7 @@
attributedInfo.setServiceType("_kitten._tcp");
attributedInfo.setPort(4242);
attributedInfo.setHost(LOCALHOST);
+ fullInfo.setHostAddresses(List.of(IPV6_ADDRESS, IPV4_ADDRESS));
attributedInfo.setAttribute("color", "pink");
attributedInfo.setAttribute("sound", (new String("にゃあ")).getBytes("UTF-8"));
attributedInfo.setAttribute("adorable", (String) null);
diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java
index edc1775..17e769c 100755
--- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java
+++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java
@@ -64,6 +64,7 @@
import static android.net.ConnectivityManager.FIREWALL_RULE_DENY;
import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT;
import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE;
+import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING;
import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK;
import static android.net.ConnectivityManager.TYPE_ETHERNET;
import static android.net.ConnectivityManager.TYPE_MOBILE;
@@ -150,6 +151,7 @@
import static com.android.server.ConnectivityService.PREFERENCE_ORDER_OEM;
import static com.android.server.ConnectivityService.PREFERENCE_ORDER_PROFILE;
import static com.android.server.ConnectivityService.PREFERENCE_ORDER_VPN;
+import static com.android.server.ConnectivityService.createDeliveryGroupKeyForConnectivityAction;
import static com.android.server.ConnectivityServiceTestUtils.transportToLegacyType;
import static com.android.server.NetworkAgentWrapper.CallbackType.OnQosCallbackRegister;
import static com.android.server.NetworkAgentWrapper.CallbackType.OnQosCallbackUnregister;
@@ -167,6 +169,15 @@
import static com.android.testutils.MiscAsserts.assertRunsInAtMost;
import static com.android.testutils.MiscAsserts.assertSameElements;
import static com.android.testutils.MiscAsserts.assertThrows;
+import static com.android.testutils.RecorderCallback.CallbackEntry.AVAILABLE;
+import static com.android.testutils.RecorderCallback.CallbackEntry.BLOCKED_STATUS;
+import static com.android.testutils.RecorderCallback.CallbackEntry.LINK_PROPERTIES_CHANGED;
+import static com.android.testutils.RecorderCallback.CallbackEntry.LOSING;
+import static com.android.testutils.RecorderCallback.CallbackEntry.LOST;
+import static com.android.testutils.RecorderCallback.CallbackEntry.NETWORK_CAPS_UPDATED;
+import static com.android.testutils.RecorderCallback.CallbackEntry.RESUMED;
+import static com.android.testutils.RecorderCallback.CallbackEntry.SUSPENDED;
+import static com.android.testutils.RecorderCallback.CallbackEntry.UNAVAILABLE;
import static com.android.testutils.TestPermissionUtil.runAsShell;
import static org.junit.Assert.assertEquals;
@@ -209,6 +220,7 @@
import android.annotation.Nullable;
import android.app.AlarmManager;
import android.app.AppOpsManager;
+import android.app.BroadcastOptions;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.admin.DevicePolicyManager;
@@ -359,6 +371,8 @@
import com.android.net.module.util.NetworkMonitorUtils;
import com.android.networkstack.apishim.ConstantsShim;
import com.android.networkstack.apishim.NetworkAgentConfigShimImpl;
+import com.android.networkstack.apishim.common.BroadcastOptionsShim;
+import com.android.networkstack.apishim.common.UnsupportedApiLevelException;
import com.android.server.ConnectivityService.ConnectivityDiagnosticsCallbackInfo;
import com.android.server.ConnectivityService.NetworkRequestInfo;
import com.android.server.ConnectivityServiceTest.ConnectivityServiceDependencies.ReportedInterfaces;
@@ -525,9 +539,9 @@
private ConnectivityServiceDependencies mDeps;
private ConnectivityService mService;
private WrappedConnectivityManager mCm;
- private TestNetworkAgentWrapper mWiFiNetworkAgent;
- private TestNetworkAgentWrapper mCellNetworkAgent;
- private TestNetworkAgentWrapper mEthernetNetworkAgent;
+ private TestNetworkAgentWrapper mWiFiAgent;
+ private TestNetworkAgentWrapper mCellAgent;
+ private TestNetworkAgentWrapper mEthernetAgent;
private MockVpn mMockVpn;
private Context mContext;
private NetworkPolicyCallback mPolicyCallback;
@@ -573,6 +587,7 @@
@Mock BpfNetMaps mBpfNetMaps;
@Mock CarrierPrivilegeAuthenticator mCarrierPrivilegeAuthenticator;
@Mock TetheringManager mTetheringManager;
+ @Mock BroadcastOptionsShim mBroadcastOptionsShim;
// BatteryStatsManager is final and cannot be mocked with regular mockito, so just mock the
// underlying binder calls.
@@ -820,6 +835,25 @@
// null should not pass the test
return null;
}
+
+ @Override
+ public void sendStickyBroadcast(Intent intent, Bundle options) {
+ // Verify that delivery group policy APIs were used on U.
+ if (SdkLevel.isAtLeastU() && CONNECTIVITY_ACTION.equals(intent.getAction())) {
+ final NetworkInfo ni = intent.getParcelableExtra(EXTRA_NETWORK_INFO,
+ NetworkInfo.class);
+ try {
+ verify(mBroadcastOptionsShim).setDeliveryGroupPolicy(
+ eq(ConstantsShim.DELIVERY_GROUP_POLICY_MOST_RECENT));
+ verify(mBroadcastOptionsShim).setDeliveryGroupMatchingKey(
+ eq(CONNECTIVITY_ACTION),
+ eq(createDeliveryGroupKeyForConnectivityAction(ni)));
+ } catch (UnsupportedApiLevelException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ super.sendStickyBroadcast(intent, options);
+ }
}
// This was only added in the T SDK, but this test needs to build against the R+S SDKs, too.
@@ -850,9 +884,9 @@
private void waitForIdle() {
HandlerUtils.waitForIdle(mCsHandlerThread, TIMEOUT_MS);
- waitForIdle(mCellNetworkAgent, TIMEOUT_MS);
- waitForIdle(mWiFiNetworkAgent, TIMEOUT_MS);
- waitForIdle(mEthernetNetworkAgent, TIMEOUT_MS);
+ waitForIdle(mCellAgent, TIMEOUT_MS);
+ waitForIdle(mWiFiAgent, TIMEOUT_MS);
+ waitForIdle(mEthernetAgent, TIMEOUT_MS);
HandlerUtils.waitForIdle(mCsHandlerThread, TIMEOUT_MS);
HandlerUtils.waitForIdle(ConnectivityThread.get(), TIMEOUT_MS);
}
@@ -875,15 +909,15 @@
// Bring up a network that we can use to send messages to ConnectivityService.
ExpectedBroadcast b = expectConnectivityAction(TYPE_WIFI, DetailedState.CONNECTED);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
b.expectBroadcast();
- Network n = mWiFiNetworkAgent.getNetwork();
+ Network n = mWiFiAgent.getNetwork();
assertNotNull(n);
// Tests that calling waitForIdle waits for messages to be processed.
for (int i = 0; i < attempts; i++) {
- mWiFiNetworkAgent.setSignalStrength(i);
+ mWiFiAgent.setSignalStrength(i);
waitForIdle();
assertEquals(i, mCm.getNetworkCapabilities(n).getSignalStrength());
}
@@ -895,16 +929,16 @@
public void verifyThatNotWaitingForIdleCausesRaceConditions() throws Exception {
// Bring up a network that we can use to send messages to ConnectivityService.
ExpectedBroadcast b = expectConnectivityAction(TYPE_WIFI, DetailedState.CONNECTED);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
b.expectBroadcast();
- Network n = mWiFiNetworkAgent.getNetwork();
+ Network n = mWiFiAgent.getNetwork();
assertNotNull(n);
// Ensure that not calling waitForIdle causes a race condition.
final int attempts = 50; // Causes the test to take about 200ms on bullhead-eng.
for (int i = 0; i < attempts; i++) {
- mWiFiNetworkAgent.setSignalStrength(i);
+ mWiFiAgent.setSignalStrength(i);
if (i != mCm.getNetworkCapabilities(n).getSignalStrength()) {
// We hit a race condition, as expected. Pass the test.
return;
@@ -2052,6 +2086,12 @@
assertNotEquals(-1L, (long) mActiveRateLimit.getOrDefault(iface, -1L));
mActiveRateLimit.put(iface, -1L);
}
+
+ @Override
+ public BroadcastOptionsShim makeBroadcastOptionsShim(BroadcastOptions options) {
+ reset(mBroadcastOptionsShim);
+ return mBroadcastOptionsShim;
+ }
}
private static void initAlarmManager(final AlarmManager am, final Handler alarmHandler) {
@@ -2085,17 +2125,17 @@
unregisterDefaultNetworkCallbacks();
maybeTearDownEnterpriseNetwork();
setAlwaysOnNetworks(false);
- if (mCellNetworkAgent != null) {
- mCellNetworkAgent.disconnect();
- mCellNetworkAgent = null;
+ if (mCellAgent != null) {
+ mCellAgent.disconnect();
+ mCellAgent = null;
}
- if (mWiFiNetworkAgent != null) {
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent = null;
+ if (mWiFiAgent != null) {
+ mWiFiAgent.disconnect();
+ mWiFiAgent = null;
}
- if (mEthernetNetworkAgent != null) {
- mEthernetNetworkAgent.disconnect();
- mEthernetNetworkAgent = null;
+ if (mEthernetAgent != null) {
+ mEthernetAgent.disconnect();
+ mEthernetAgent = null;
}
if (mQosCallbackMockHelper != null) {
@@ -2158,13 +2198,13 @@
}
switch (transport) {
case TRANSPORT_WIFI:
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
break;
case TRANSPORT_CELLULAR:
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
break;
case TRANSPORT_ETHERNET:
- assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mEthernetAgent.getNetwork(), mCm.getActiveNetwork());
break;
default:
break;
@@ -2338,10 +2378,10 @@
@Test
public void testNetworkFeature() throws Exception {
// Connect the cell agent and wait for the connected broadcast.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_SUPL);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.addCapability(NET_CAPABILITY_SUPL);
ExpectedBroadcast b = expectConnectivityAction(TYPE_MOBILE, DetailedState.CONNECTED);
- mCellNetworkAgent.connect(true);
+ mCellAgent.connect(true);
b.expectBroadcast();
// Build legacy request for SUPL.
@@ -2355,45 +2395,45 @@
b = registerConnectivityBroadcast(1);
final TestNetworkCallback callback = new TestNetworkCallback();
mCm.requestNetwork(legacyRequest, callback);
- callback.expect(CallbackEntry.AVAILABLE, mCellNetworkAgent);
+ callback.expect(AVAILABLE, mCellAgent);
mCm.unregisterNetworkCallback(callback);
b.expectNoBroadcast(800); // 800ms long enough to at least flake if this is sent
// Disconnect the network and expect mobile disconnected broadcast.
b = expectConnectivityAction(TYPE_MOBILE, DetailedState.DISCONNECTED);
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
b.expectBroadcast();
}
@Test
public void testLingering() throws Exception {
verifyNoNetwork();
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
assertNull(mCm.getActiveNetworkInfo());
assertNull(mCm.getActiveNetwork());
// Test bringing up validated cellular.
ExpectedBroadcast b = expectConnectivityAction(TYPE_MOBILE, DetailedState.CONNECTED);
- mCellNetworkAgent.connect(true);
+ mCellAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
assertLength(2, mCm.getAllNetworks());
- assertTrue(mCm.getAllNetworks()[0].equals(mCm.getActiveNetwork()) ||
- mCm.getAllNetworks()[1].equals(mCm.getActiveNetwork()));
- assertTrue(mCm.getAllNetworks()[0].equals(mWiFiNetworkAgent.getNetwork()) ||
- mCm.getAllNetworks()[1].equals(mWiFiNetworkAgent.getNetwork()));
+ assertTrue(mCm.getAllNetworks()[0].equals(mCm.getActiveNetwork())
+ || mCm.getAllNetworks()[1].equals(mCm.getActiveNetwork()));
+ assertTrue(mCm.getAllNetworks()[0].equals(mWiFiAgent.getNetwork())
+ || mCm.getAllNetworks()[1].equals(mWiFiAgent.getNetwork()));
// Test bringing up validated WiFi.
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
assertLength(2, mCm.getAllNetworks());
- assertTrue(mCm.getAllNetworks()[0].equals(mCm.getActiveNetwork()) ||
- mCm.getAllNetworks()[1].equals(mCm.getActiveNetwork()));
- assertTrue(mCm.getAllNetworks()[0].equals(mCellNetworkAgent.getNetwork()) ||
- mCm.getAllNetworks()[1].equals(mCellNetworkAgent.getNetwork()));
+ assertTrue(mCm.getAllNetworks()[0].equals(mCm.getActiveNetwork())
+ || mCm.getAllNetworks()[1].equals(mCm.getActiveNetwork()));
+ assertTrue(mCm.getAllNetworks()[0].equals(mCellAgent.getNetwork())
+ || mCm.getAllNetworks()[1].equals(mCellAgent.getNetwork()));
// Test cellular linger timeout.
- mCellNetworkAgent.expectDisconnected();
+ mCellAgent.expectDisconnected();
waitForIdle();
assertLength(1, mCm.getAllNetworks());
verifyActiveNetwork(TRANSPORT_WIFI);
@@ -2401,7 +2441,7 @@
assertEquals(mCm.getAllNetworks()[0], mCm.getActiveNetwork());
// Test WiFi disconnect.
b = registerConnectivityBroadcast(1);
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
b.expectBroadcast();
verifyNoNetwork();
}
@@ -2418,43 +2458,43 @@
// 1. Create a network that is not requested by anyone, and does not satisfy any of the
// default requests. Verify that the network will be inactive instead of torn down.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithoutInternet();
- listenCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connectWithoutInternet();
+ listenCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
listenCallback.assertNoCallback();
// Verify that the network will be torn down after nascent expiry. A small period of time
// is added in case of flakiness.
final int nascentTimeoutMs =
mService.mNascentDelayMs + mService.mNascentDelayMs / 4;
- listenCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent, nascentTimeoutMs);
+ listenCallback.expect(LOST, mWiFiAgent, nascentTimeoutMs);
// 2. Create a network that is satisfied by a request comes later.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithoutInternet();
- listenCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connectWithoutInternet();
+ listenCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
final NetworkRequest wifiRequest = new NetworkRequest.Builder()
.addTransportType(TRANSPORT_WIFI).build();
final TestNetworkCallback wifiCallback = new TestNetworkCallback();
mCm.requestNetwork(wifiRequest, wifiCallback);
- wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// Verify that the network will be kept since the request is still satisfied. And is able
// to get disconnected as usual if the request is released after the nascent timer expires.
listenCallback.assertNoCallback(nascentTimeoutMs);
mCm.unregisterNetworkCallback(wifiCallback);
- listenCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ listenCallback.expect(LOST, mWiFiAgent);
// 3. Create a network that is satisfied by a request comes later.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithoutInternet();
- listenCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connectWithoutInternet();
+ listenCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
mCm.requestNetwork(wifiRequest, wifiCallback);
- wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// Verify that the network will still be torn down after the request gets removed.
mCm.unregisterNetworkCallback(wifiCallback);
- listenCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ listenCallback.expect(LOST, mWiFiAgent);
// There is no need to ensure that LOSING is never sent in the common case that the
// network immediately satisfies a request that was already present, because it is already
@@ -2487,20 +2527,20 @@
.addCapability(NET_CAPABILITY_FOREGROUND).build(), fgMobileListenCallback);
// Connect wifi, which satisfies default request.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- wifiListenCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ wifiListenCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
// Connect a cellular network, verify that satisfies only the background callback.
setAlwaysOnNetworks(true);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- bgMobileListenCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ bgMobileListenCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
fgMobileListenCallback.assertNoCallback();
- assertFalse(isForegroundNetwork(mCellNetworkAgent));
+ assertFalse(isForegroundNetwork(mCellAgent));
- mCellNetworkAgent.disconnect();
- bgMobileListenCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ bgMobileListenCallback.expect(LOST, mCellAgent);
fgMobileListenCallback.assertNoCallback();
mCm.unregisterNetworkCallback(wifiListenCallback);
@@ -2565,34 +2605,34 @@
final boolean cellRadioTimesharingCapable) throws Exception {
mService.mCellularRadioTimesharingCapable = cellRadioTimesharingCapable;
// Test bringing up unvalidated WiFi
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
ExpectedBroadcast b = registerConnectivityBroadcast(1);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent.connect(false);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test bringing up unvalidated cellular
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false);
waitForIdle();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test cellular disconnect.
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
waitForIdle();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test bringing up validated cellular
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
b = registerConnectivityBroadcast(2);
- mCellNetworkAgent.connect(true);
+ mCellAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Test cellular disconnect.
b = registerConnectivityBroadcast(2);
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test WiFi disconnect.
b = registerConnectivityBroadcast(1);
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
b.expectBroadcast();
verifyNoNetwork();
}
@@ -2613,25 +2653,25 @@
final boolean cellRadioTimesharingCapable) throws Exception {
mService.mCellularRadioTimesharingCapable = cellRadioTimesharingCapable;
// Test bringing up unvalidated cellular.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
ExpectedBroadcast b = registerConnectivityBroadcast(1);
- mCellNetworkAgent.connect(false);
+ mCellAgent.connect(false);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Test bringing up unvalidated WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent.connect(false);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test WiFi disconnect.
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Test cellular disconnect.
b = registerConnectivityBroadcast(1);
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
b.expectBroadcast();
verifyNoNetwork();
}
@@ -2652,28 +2692,28 @@
final boolean cellRadioTimesharingCapable) throws Exception {
mService.mCellularRadioTimesharingCapable = cellRadioTimesharingCapable;
// Test bringing up unvalidated WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
ExpectedBroadcast b = registerConnectivityBroadcast(1);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent.connect(false);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
- assertFalse(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
+ assertFalse(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
// Test bringing up validated cellular.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
b = registerConnectivityBroadcast(2);
- mCellNetworkAgent.connect(true);
+ mCellAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
- assertFalse(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
+ assertFalse(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
// Test cellular disconnect.
b = registerConnectivityBroadcast(2);
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Unlingering a network should not cause it to be marked as validated.
- assertFalse(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
+ assertFalse(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
}
@@ -2745,7 +2785,7 @@
net1.expectDisconnected(TEST_CALLBACK_TIMEOUT_MS);
}
net1.disconnect();
- generalCb.expect(CallbackEntry.LOST, net1);
+ generalCb.expect(LOST, net1);
// Remove primary from net 2
net2.setScore(new NetworkScore.Builder().build());
@@ -2775,13 +2815,14 @@
// for any other request.
generalCb.expectLosing(net2);
net2.assertNotDisconnected(TEST_CALLBACK_TIMEOUT_MS);
- generalCb.assertNoCallback();
+ // Timeout 0 because after a while LOST will actually arrive
+ generalCb.assertNoCallback(0 /* timeoutMs */);
net2.expectDisconnected(UNREASONABLY_LONG_ALARM_WAIT_MS);
} else {
net2.expectDisconnected(TEST_CALLBACK_TIMEOUT_MS);
}
net2.disconnect();
- generalCb.expect(CallbackEntry.LOST, net2);
+ generalCb.expect(LOST, net2);
defaultCb.assertNoCallback();
net3.disconnect();
@@ -2806,25 +2847,25 @@
final boolean cellRadioTimesharingCapable) throws Exception {
mService.mCellularRadioTimesharingCapable = cellRadioTimesharingCapable;
// Test bringing up validated cellular.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
ExpectedBroadcast b = registerConnectivityBroadcast(1);
- mCellNetworkAgent.connect(true);
+ mCellAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Test bringing up validated WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test WiFi getting really weak.
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.adjustScore(-11);
+ mWiFiAgent.adjustScore(-11);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Test WiFi restoring signal strength.
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.adjustScore(11);
+ mWiFiAgent.adjustScore(11);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
}
@@ -2846,29 +2887,29 @@
mService.mCellularRadioTimesharingCapable = cellRadioTimesharingCapable;
// Test bringing up WiFi without NET_CAPABILITY_INTERNET.
// Expect it to be torn down immediately because it satisfies no requests.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithoutInternet();
- mWiFiNetworkAgent.expectDisconnected();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connectWithoutInternet();
+ mWiFiAgent.expectDisconnected();
// Test bringing up cellular without NET_CAPABILITY_INTERNET.
// Expect it to be torn down immediately because it satisfies no requests.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mCellNetworkAgent.connectWithoutInternet();
- mCellNetworkAgent.expectDisconnected();
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mCellAgent.connectWithoutInternet();
+ mCellAgent.expectDisconnected();
// Test bringing up validated WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
final ExpectedBroadcast b = expectConnectivityAction(TYPE_WIFI, DetailedState.CONNECTED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test bringing up unvalidated cellular.
// Expect it to be torn down because it could never be the highest scoring network
// satisfying the default request even if it validated.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false);
- mCellNetworkAgent.expectDisconnected();
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false);
+ mCellAgent.expectDisconnected();
verifyActiveNetwork(TRANSPORT_WIFI);
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent.expectDisconnected();
+ mWiFiAgent.disconnect();
+ mWiFiAgent.expectDisconnected();
}
// TODO : migrate to @Parameterized
@@ -2887,37 +2928,37 @@
final boolean cellRadioTimesharingCapable) throws Exception {
mService.mCellularRadioTimesharingCapable = cellRadioTimesharingCapable;
// Test bringing up validated cellular.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
ExpectedBroadcast b = registerConnectivityBroadcast(1);
- mCellNetworkAgent.connect(true);
+ mCellAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Test bringing up validated WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Reevaluate WiFi (it'll instantly fail DNS).
b = registerConnectivityBroadcast(2);
- assertTrue(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
+ assertTrue(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
- mCm.reportBadNetwork(mWiFiNetworkAgent.getNetwork());
+ mCm.reportBadNetwork(mWiFiAgent.getNetwork());
// Should quickly fall back to Cellular.
b.expectBroadcast();
- assertFalse(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
+ assertFalse(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Reevaluate cellular (it'll instantly fail DNS).
b = registerConnectivityBroadcast(2);
- assertTrue(mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork()).hasCapability(
+ assertTrue(mCm.getNetworkCapabilities(mCellAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
- mCm.reportBadNetwork(mCellNetworkAgent.getNetwork());
+ mCm.reportBadNetwork(mCellAgent.getNetwork());
// Should quickly fall back to WiFi.
b.expectBroadcast();
- assertFalse(mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork()).hasCapability(
+ assertFalse(mCm.getNetworkCapabilities(mCellAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
- assertFalse(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
+ assertFalse(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
verifyActiveNetwork(TRANSPORT_WIFI);
}
@@ -2938,25 +2979,25 @@
final boolean cellRadioTimesharingCapable) throws Exception {
mService.mCellularRadioTimesharingCapable = cellRadioTimesharingCapable;
// Test bringing up unvalidated WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
ExpectedBroadcast b = registerConnectivityBroadcast(1);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent.connect(false);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
// Test bringing up validated cellular.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
b = registerConnectivityBroadcast(2);
- mCellNetworkAgent.connect(true);
+ mCellAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
// Reevaluate cellular (it'll instantly fail DNS).
b = registerConnectivityBroadcast(2);
- assertTrue(mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork()).hasCapability(
+ assertTrue(mCm.getNetworkCapabilities(mCellAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
- mCm.reportBadNetwork(mCellNetworkAgent.getNetwork());
+ mCm.reportBadNetwork(mCellAgent.getNetwork());
// Should quickly fall back to WiFi.
b.expectBroadcast();
- assertFalse(mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork()).hasCapability(
+ assertFalse(mCm.getNetworkCapabilities(mCellAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
verifyActiveNetwork(TRANSPORT_WIFI);
}
@@ -2975,18 +3016,16 @@
*/
private class TestNetworkCallback extends TestableNetworkCallback {
TestNetworkCallback() {
- super(TEST_CALLBACK_TIMEOUT_MS);
- }
-
- @Override
- public void assertNoCallback() {
- // TODO: better support this use case in TestableNetworkCallback
- waitForIdle();
- assertNoCallback(0 /* timeout */);
+ // In the context of this test, the testable network callbacks should use waitForIdle
+ // before calling assertNoCallback in an effort to detect issues where a callback is
+ // not yet sent but a message currently in the queue of a handler will cause it to
+ // be sent soon.
+ super(TEST_CALLBACK_TIMEOUT_MS, TEST_CALLBACK_TIMEOUT_MS,
+ ConnectivityServiceTest.this::waitForIdle);
}
public CallbackEntry.Losing expectLosing(final HasNetwork n, final long timeoutMs) {
- final CallbackEntry.Losing losing = expect(CallbackEntry.LOSING, n, timeoutMs);
+ final CallbackEntry.Losing losing = expect(LOSING, n, timeoutMs);
final int maxMsToLive = losing.getMaxMsToLive();
if (maxMsToLive < 0 || maxMsToLive > mService.mLingerDelayMs) {
// maxMsToLive is the value that was received in the onLosing callback. That must
@@ -3008,27 +3047,33 @@
// Can't be part of TestNetworkCallback because "cannot be declared static; static methods can
// only be declared in a static or top level type".
+ static void assertNoCallbacks(final long timeoutMs, TestNetworkCallback ... callbacks) {
+ for (TestNetworkCallback c : callbacks) {
+ c.assertNoCallback(timeoutMs);
+ }
+ }
+
static void assertNoCallbacks(TestNetworkCallback ... callbacks) {
for (TestNetworkCallback c : callbacks) {
- c.assertNoCallback();
+ c.assertNoCallback(); // each callback uses its own timeout
}
}
static void expectOnLost(TestNetworkAgentWrapper network, TestNetworkCallback ... callbacks) {
for (TestNetworkCallback c : callbacks) {
- c.expect(CallbackEntry.LOST, network);
+ c.expect(LOST, network);
}
}
static void expectAvailableCallbacksUnvalidatedWithSpecifier(TestNetworkAgentWrapper network,
NetworkSpecifier specifier, TestNetworkCallback ... callbacks) {
for (TestNetworkCallback c : callbacks) {
- c.expect(CallbackEntry.AVAILABLE, network);
+ c.expect(AVAILABLE, network);
c.expectCapabilitiesThat(network, (nc) ->
!nc.hasCapability(NET_CAPABILITY_VALIDATED)
&& Objects.equals(specifier, nc.getNetworkSpecifier()));
- c.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, network);
- c.expect(CallbackEntry.BLOCKED_STATUS, network);
+ c.expect(LINK_PROPERTIES_CHANGED, network);
+ c.expect(BLOCKED_STATUS, network);
}
}
@@ -3038,12 +3083,12 @@
final NetworkRequest wifiRequest = new NetworkRequest.Builder()
.addTransportType(TRANSPORT_WIFI).build();
mCm.requestNetwork(wifiRequest, cb);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
// Updating the score triggers a rematch.
- mWiFiNetworkAgent.setScore(new NetworkScore.Builder().build());
+ mWiFiAgent.setScore(new NetworkScore.Builder().build());
cb.assertNoCallback();
- mWiFiNetworkAgent.connect(false);
- cb.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent.connect(false);
+ cb.expectAvailableCallbacksUnvalidated(mWiFiAgent);
cb.assertNoCallback();
mCm.unregisterNetworkCallback(cb);
}
@@ -3054,13 +3099,13 @@
final NetworkRequest wifiRequest = new NetworkRequest.Builder()
.addTransportType(TRANSPORT_WIFI).build();
mCm.registerNetworkCallback(wifiRequest, cb);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- final NetworkCapabilities nc = mWiFiNetworkAgent.getNetworkCapabilities();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ final NetworkCapabilities nc = mWiFiAgent.getNetworkCapabilities();
nc.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
- mWiFiNetworkAgent.setNetworkCapabilities(nc, true /* sendToConnectivityService */);
+ mWiFiAgent.setNetworkCapabilities(nc, true /* sendToConnectivityService */);
cb.assertNoCallback();
- mWiFiNetworkAgent.connect(false);
- cb.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent.connect(false);
+ cb.expectAvailableCallbacksUnvalidated(mWiFiAgent);
final CallbackEntry found = CollectionUtils.findLast(cb.getHistory(),
it -> it instanceof CallbackEntry.CapabilitiesChanged);
assertTrue(((CallbackEntry.CapabilitiesChanged) found).getCaps()
@@ -3086,76 +3131,79 @@
// Test unvalidated networks
ExpectedBroadcast b = registerConnectivityBroadcast(1);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false);
- genericNetworkCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- cellNetworkCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false);
+ genericNetworkCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ cellNetworkCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
b.expectBroadcast();
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
// This should not trigger spurious onAvailable() callbacks, b/21762680.
- mCellNetworkAgent.adjustScore(-1);
+ mCellAgent.adjustScore(-1);
waitForIdle();
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- genericNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ genericNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
b.expectBroadcast();
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
b = registerConnectivityBroadcast(2);
- mWiFiNetworkAgent.disconnect();
- genericNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- wifiNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ genericNetworkCallback.expect(LOST, mWiFiAgent);
+ wifiNetworkCallback.expect(LOST, mWiFiAgent);
cellNetworkCallback.assertNoCallback();
b.expectBroadcast();
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
b = registerConnectivityBroadcast(1);
- mCellNetworkAgent.disconnect();
- genericNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ genericNetworkCallback.expect(LOST, mCellAgent);
+ cellNetworkCallback.expect(LOST, mCellAgent);
b.expectBroadcast();
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
// Test validated networks
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- genericNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ genericNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
// This should not trigger spurious onAvailable() callbacks, b/21762680.
- mCellNetworkAgent.adjustScore(-1);
+ mCellAgent.adjustScore(-1);
waitForIdle();
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- genericNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- genericNetworkCallback.expectLosing(mCellNetworkAgent);
- genericNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- wifiNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- cellNetworkCallback.expectLosing(mCellNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ genericNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ genericNetworkCallback.expectLosing(mCellAgent);
+ genericNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ wifiNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ cellNetworkCallback.expectLosing(mCellAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
+ // Cell will disconnect after the lingering period. Before that elapses check that
+ // there have been no callbacks.
+ assertNoCallbacks(0 /* timeoutMs */,
+ genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
+
+ mWiFiAgent.disconnect();
+ genericNetworkCallback.expect(LOST, mWiFiAgent);
+ wifiNetworkCallback.expect(LOST, mWiFiAgent);
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
- mWiFiNetworkAgent.disconnect();
- genericNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- wifiNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
-
- mCellNetworkAgent.disconnect();
- genericNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ genericNetworkCallback.expect(LOST, mCellAgent);
+ cellNetworkCallback.expect(LOST, mCellAgent);
assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
}
@@ -3167,10 +3215,10 @@
mCm.registerNetworkCallback(wifiRequest, callback);
mCm.registerDefaultNetworkCallback(defaultCallback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
final LinkProperties newLp = new LinkProperties();
final Uri capportUrl = Uri.parse("https://capport.example.com/api");
@@ -3179,20 +3227,20 @@
final Uri expectedCapportUrl = sanitized ? null : capportUrl;
newLp.setCaptivePortalApiUrl(capportUrl);
- mWiFiNetworkAgent.sendLinkProperties(newLp);
- callback.expectLinkPropertiesThat(mWiFiNetworkAgent, lp ->
+ mWiFiAgent.sendLinkProperties(newLp);
+ callback.expectLinkPropertiesThat(mWiFiAgent, lp ->
Objects.equals(expectedCapportUrl, lp.getCaptivePortalApiUrl()));
- defaultCallback.expectLinkPropertiesThat(mWiFiNetworkAgent, lp ->
+ defaultCallback.expectLinkPropertiesThat(mWiFiAgent, lp ->
Objects.equals(expectedCapportUrl, lp.getCaptivePortalApiUrl()));
final CaptivePortalData expectedCapportData = sanitized ? null : capportData;
- mWiFiNetworkAgent.notifyCapportApiDataChanged(capportData);
- callback.expectLinkPropertiesThat(mWiFiNetworkAgent, lp ->
+ mWiFiAgent.notifyCapportApiDataChanged(capportData);
+ callback.expectLinkPropertiesThat(mWiFiAgent, lp ->
Objects.equals(expectedCapportData, lp.getCaptivePortalData()));
- defaultCallback.expectLinkPropertiesThat(mWiFiNetworkAgent, lp ->
+ defaultCallback.expectLinkPropertiesThat(mWiFiAgent, lp ->
Objects.equals(expectedCapportData, lp.getCaptivePortalData()));
- final LinkProperties lp = mCm.getLinkProperties(mWiFiNetworkAgent.getNetwork());
+ final LinkProperties lp = mCm.getLinkProperties(mWiFiAgent.getNetwork());
assertEquals(expectedCapportUrl, lp.getCaptivePortalApiUrl());
assertEquals(expectedCapportData, lp.getCaptivePortalData());
}
@@ -3227,19 +3275,18 @@
final int originalOwnerUid = Process.myUid();
ncTemplate.setOwnerUid(originalOwnerUid);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, new LinkProperties(),
- ncTemplate);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, new LinkProperties(), ncTemplate);
+ mWiFiAgent.connect(false);
waitForIdle();
- // Send ConnectivityService an update to the mWiFiNetworkAgent's capabilities that changes
+ // Send ConnectivityService an update to the mWiFiAgent's capabilities that changes
// the owner UID and an unrelated capability.
- NetworkCapabilities agentCapabilities = mWiFiNetworkAgent.getNetworkCapabilities();
+ NetworkCapabilities agentCapabilities = mWiFiAgent.getNetworkCapabilities();
assertEquals(originalOwnerUid, agentCapabilities.getOwnerUid());
agentCapabilities.setOwnerUid(42);
assertFalse(agentCapabilities.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
agentCapabilities.addCapability(NET_CAPABILITY_NOT_CONGESTED);
- mWiFiNetworkAgent.setNetworkCapabilities(agentCapabilities, true);
+ mWiFiAgent.setNetworkCapabilities(agentCapabilities, true);
waitForIdle();
// Owner UIDs are not visible without location permission.
@@ -3247,7 +3294,7 @@
Manifest.permission.ACCESS_FINE_LOCATION);
// Check that the capability change has been applied but the owner UID is not modified.
- NetworkCapabilities nc = mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork());
+ NetworkCapabilities nc = mCm.getNetworkCapabilities(mWiFiAgent.getNetwork());
assertEquals(originalOwnerUid, nc.getOwnerUid());
assertTrue(nc.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
}
@@ -3269,56 +3316,56 @@
TestNetworkCallback defaultCallback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(defaultCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mEthernetNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mCellAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mEthernetAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- defaultCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
// We get AVAILABLE on wifi when wifi connects and satisfies our unmetered request.
// We then get LOSING when wifi validates and cell is outscored.
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// TODO: Investigate sending validated before losing.
- callback.expectLosing(mCellNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ callback.expectLosing(mCellAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- mEthernetNetworkAgent.connect(true);
- callback.expectAvailableCallbacksUnvalidated(mEthernetNetworkAgent);
+ mEthernetAgent.connect(true);
+ callback.expectAvailableCallbacksUnvalidated(mEthernetAgent);
// TODO: Investigate sending validated before losing.
- callback.expectLosing(mWiFiNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mEthernetNetworkAgent);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mEthernetNetworkAgent);
- assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ callback.expectLosing(mWiFiAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mEthernetAgent);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mEthernetAgent);
+ assertEquals(mEthernetAgent.getNetwork(), mCm.getActiveNetwork());
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- mEthernetNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
- defaultCallback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
- defaultCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
+ mEthernetAgent.disconnect();
+ callback.expect(LOST, mEthernetAgent);
+ defaultCallback.expect(LOST, mEthernetAgent);
+ defaultCallback.expectAvailableCallbacksValidated(mWiFiAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
for (int i = 0; i < 4; i++) {
TestNetworkAgentWrapper oldNetwork, newNetwork;
if (i % 2 == 0) {
- mWiFiNetworkAgent.adjustScore(-15);
- oldNetwork = mWiFiNetworkAgent;
- newNetwork = mCellNetworkAgent;
+ mWiFiAgent.adjustScore(-15);
+ oldNetwork = mWiFiAgent;
+ newNetwork = mCellAgent;
} else {
- mWiFiNetworkAgent.adjustScore(15);
- oldNetwork = mCellNetworkAgent;
- newNetwork = mWiFiNetworkAgent;
+ mWiFiAgent.adjustScore(15);
+ oldNetwork = mCellAgent;
+ newNetwork = mWiFiAgent;
}
callback.expectLosing(oldNetwork);
@@ -3327,28 +3374,28 @@
defaultCallback.expectAvailableCallbacksValidated(newNetwork);
assertEquals(newNetwork.getNetwork(), mCm.getActiveNetwork());
}
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
// Verify that if a network no longer satisfies a request, we send LOST and not LOSING, even
// if the network is still up.
- mWiFiNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
// We expect a notification about the capabilities change, and nothing else.
- defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_METERED, mWiFiNetworkAgent);
+ defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_METERED, mWiFiAgent);
defaultCallback.assertNoCallback();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ callback.expect(LOST, mWiFiAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Wifi no longer satisfies our listen, which is for an unmetered network.
// But because its score is 55, it's still up (and the default network).
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
// Disconnect our test networks.
- mWiFiNetworkAgent.disconnect();
- defaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mWiFiAgent.disconnect();
+ defaultCallback.expect(LOST, mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksValidated(mCellAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- mCellNetworkAgent.disconnect();
- defaultCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ defaultCallback.expect(LOST, mCellAgent);
waitForIdle();
assertEquals(null, mCm.getActiveNetwork());
@@ -3362,64 +3409,64 @@
mCm.registerNetworkCallback(request, callback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false); // Score: 10
- callback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false); // Score: 10
+ callback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Bring up wifi with a score of 20.
// Cell stays up because it would satisfy the default request if it validated.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false); // Score: 20
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false); // Score: 20
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
+ defaultCallback.expect(LOST, mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Bring up wifi, then validate it. Previous versions would immediately tear down cell, but
// it's arguably correct to linger it, since it was the default network before it validated.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// TODO: Investigate sending validated before losing.
- callback.expectLosing(mCellNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- defaultCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ callback.expectLosing(mCellAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- mCellNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- defaultCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
+ defaultCallback.expect(LOST, mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ mCellAgent.disconnect();
+ callback.expect(LOST, mCellAgent);
+ defaultCallback.expect(LOST, mCellAgent);
waitForIdle();
assertEquals(null, mCm.getActiveNetwork());
// If a network is lingering, and we add and remove a request from it, resume lingering.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- defaultCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// TODO: Investigate sending validated before losing.
- callback.expectLosing(mCellNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ callback.expectLosing(mCellAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
NetworkRequest cellRequest = new NetworkRequest.Builder()
@@ -3429,14 +3476,14 @@
// TODO: should this cause an AVAILABLE callback, to indicate that the network is no longer
// lingering?
mCm.unregisterNetworkCallback(noopCallback);
- callback.expectLosing(mCellNetworkAgent);
+ callback.expectLosing(mCellAgent);
// Similar to the above: lingering can start even after the lingered request is removed.
// Disconnect wifi and switch to cell.
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
+ defaultCallback.expect(LOST, mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksValidated(mCellAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Cell is now the default network. Pin it with a cell-specific request.
@@ -3444,44 +3491,44 @@
mCm.requestNetwork(cellRequest, noopCallback);
// Now connect wifi, and expect it to become the default network.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// The default request is lingering on cell, but nothing happens to cell, and we send no
// callbacks for it, because it's kept up by cellRequest.
callback.assertNoCallback();
// Now unregister cellRequest and expect cell to start lingering.
mCm.unregisterNetworkCallback(noopCallback);
- callback.expectLosing(mCellNetworkAgent);
+ callback.expectLosing(mCellAgent);
// Let linger run its course.
- callback.assertNoCallback();
+ callback.assertNoCallback(0 /* timeoutMs */);
final int lingerTimeoutMs = mService.mLingerDelayMs + mService.mLingerDelayMs / 4;
- callback.expect(CallbackEntry.LOST, mCellNetworkAgent, lingerTimeoutMs);
+ callback.expect(LOST, mCellAgent, lingerTimeoutMs);
// Register a TRACK_DEFAULT request and check that it does not affect lingering.
TestNetworkCallback trackDefaultCallback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(trackDefaultCallback);
- trackDefaultCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
- mEthernetNetworkAgent.connect(true);
- callback.expectAvailableCallbacksUnvalidated(mEthernetNetworkAgent);
- callback.expectLosing(mWiFiNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mEthernetNetworkAgent);
- trackDefaultCallback.expectAvailableDoubleValidatedCallbacks(mEthernetNetworkAgent);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mEthernetNetworkAgent);
+ trackDefaultCallback.expectAvailableCallbacksValidated(mWiFiAgent);
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
+ mEthernetAgent.connect(true);
+ callback.expectAvailableCallbacksUnvalidated(mEthernetAgent);
+ callback.expectLosing(mWiFiAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mEthernetAgent);
+ trackDefaultCallback.expectAvailableDoubleValidatedCallbacks(mEthernetAgent);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mEthernetAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Let linger run its course.
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent, lingerTimeoutMs);
+ callback.expect(LOST, mWiFiAgent, lingerTimeoutMs);
// Clean up.
- mEthernetNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
- defaultCallback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
- trackDefaultCallback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
+ mEthernetAgent.disconnect();
+ callback.expect(LOST, mEthernetAgent);
+ defaultCallback.expect(LOST, mEthernetAgent);
+ trackDefaultCallback.expect(LOST, mEthernetAgent);
mCm.unregisterNetworkCallback(callback);
mCm.unregisterNetworkCallback(defaultCallback);
@@ -3517,19 +3564,19 @@
TestNetworkCallback defaultCallback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(defaultCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- defaultCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
// Wifi comes up and cell lingers.
- mWiFiNetworkAgent.connect(true);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- callback.expectLosing(mCellNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ mWiFiAgent.connect(true);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ callback.expectLosing(mCellAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
// File a request for cellular, then release it.
NetworkRequest cellRequest = new NetworkRequest.Builder()
@@ -3537,13 +3584,12 @@
NetworkCallback noopCallback = new NetworkCallback();
mCm.requestNetwork(cellRequest, noopCallback);
mCm.unregisterNetworkCallback(noopCallback);
- callback.expectLosing(mCellNetworkAgent);
+ callback.expectLosing(mCellAgent);
// Let linger run its course.
callback.assertNoCallback();
final int lingerTimeoutMs = TEST_LINGER_DELAY_MS + TEST_LINGER_DELAY_MS / 4;
- callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent,
- lingerTimeoutMs);
+ callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellAgent, lingerTimeoutMs);
// Clean up.
mCm.unregisterNetworkCallback(defaultCallback);
@@ -3598,7 +3644,7 @@
private void expectDisconnectAndClearNotifications(TestNetworkCallback callback,
TestNetworkAgentWrapper agent, NotificationType type) {
- callback.expect(CallbackEntry.LOST, agent);
+ callback.expect(LOST, agent);
expectClearNotification(agent, type);
}
@@ -3669,7 +3715,7 @@
final NetworkAgentWrapper.Callbacks callbacks = new NetworkAgentWrapper.Callbacks(
onNetworkCreated, onNetworkUnwanted, onNetworkDisconnected);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, callbacks);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, callbacks);
// Connect a network, and file a request for it after it has come up, to ensure the nascent
// timer is cleared and the test does not have to wait for it. Filing the request after the
@@ -3677,13 +3723,13 @@
// nascent timer if the first request satisfied by the network was filed before the network
// connected.
// TODO: fix this bug, file the request before connecting, and remove the waitForIdle.
- mWiFiNetworkAgent.connectWithoutInternet();
+ mWiFiAgent.connectWithoutInternet();
waitForIdle();
mCm.requestNetwork(request, callback);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// Set teardown delay and make sure CS has processed it.
- mWiFiNetworkAgent.getNetworkAgent().setTeardownDelayMillis(300);
+ mWiFiAgent.getNetworkAgent().setTeardownDelayMillis(300);
waitForIdle();
// Post the duringTeardown lambda to the handler so it fires while teardown is in progress.
@@ -3691,7 +3737,7 @@
// down the network and started the teardown timer, and short enough that the lambda is
// scheduled to run before the teardown timer.
final Handler h = new Handler(mCsHandlerThread.getLooper());
- h.postDelayed(() -> duringTeardown.accept(mWiFiNetworkAgent.getNetwork()), 150);
+ h.postDelayed(() -> duringTeardown.accept(mWiFiAgent.getNetwork()), 150);
// Disconnect the network and check that events happened in the right order.
mCm.unregisterNetworkCallback(callback);
@@ -3712,106 +3758,104 @@
mCm.registerNetworkCallback(request, callback);
// Bring up validated cell.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
// Bring up unvalidated wifi with explicitlySelected=true.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.explicitlySelected(true, false);
- mWiFiNetworkAgent.connect(false);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.explicitlySelected(true, false);
+ mWiFiAgent.connect(false);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// Cell remains the default.
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
// Expect a high-priority NO_INTERNET notification.
- expectUnvalidationCheckWillNotify(mWiFiNetworkAgent, NotificationType.NO_INTERNET);
+ expectUnvalidationCheckWillNotify(mWiFiAgent, NotificationType.NO_INTERNET);
// Lower WiFi's score to lower than cell, and check that it doesn't disconnect because
// it's explicitly selected.
- mWiFiNetworkAgent.adjustScore(-40);
- mWiFiNetworkAgent.adjustScore(40);
+ mWiFiAgent.adjustScore(-40);
+ mWiFiAgent.adjustScore(40);
callback.assertNoCallback();
// If the user chooses yes on the "No Internet access, stay connected?" dialog, we switch to
// wifi even though it's unvalidated.
- mCm.setAcceptUnvalidated(mWiFiNetworkAgent.getNetwork(), true, false);
- callback.expectLosing(mCellNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mCm.setAcceptUnvalidated(mWiFiAgent.getNetwork(), true, false);
+ callback.expectLosing(mCellAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
// Disconnect wifi, and then reconnect, again with explicitlySelected=true.
- mWiFiNetworkAgent.disconnect();
- expectDisconnectAndClearNotifications(callback, mWiFiNetworkAgent,
- NotificationType.NO_INTERNET);
+ mWiFiAgent.disconnect();
+ expectDisconnectAndClearNotifications(callback, mWiFiAgent, NotificationType.NO_INTERNET);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.explicitlySelected(true, false);
- mWiFiNetworkAgent.connect(false);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.explicitlySelected(true, false);
+ mWiFiAgent.connect(false);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// Expect a high-priority NO_INTERNET notification.
- expectUnvalidationCheckWillNotify(mWiFiNetworkAgent, NotificationType.NO_INTERNET);
+ expectUnvalidationCheckWillNotify(mWiFiAgent, NotificationType.NO_INTERNET);
// If the user chooses no on the "No Internet access, stay connected?" dialog, we ask the
// network to disconnect.
- mCm.setAcceptUnvalidated(mWiFiNetworkAgent.getNetwork(), false, false);
- expectDisconnectAndClearNotifications(callback, mWiFiNetworkAgent,
- NotificationType.NO_INTERNET);
+ mCm.setAcceptUnvalidated(mWiFiAgent.getNetwork(), false, false);
+ expectDisconnectAndClearNotifications(callback, mWiFiAgent, NotificationType.NO_INTERNET);
reset(mNotificationManager);
// Reconnect, again with explicitlySelected=true, but this time validate.
// Expect no notifications.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.explicitlySelected(true, false);
- mWiFiNetworkAgent.connect(true);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- callback.expectLosing(mCellNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
- expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.explicitlySelected(true, false);
+ mWiFiAgent.connect(true);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ callback.expectLosing(mCellAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
+ expectUnvalidationCheckWillNotNotify(mWiFiAgent);
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
- mEthernetNetworkAgent.connect(true);
- callback.expectAvailableCallbacksUnvalidated(mEthernetNetworkAgent);
- callback.expectLosing(mWiFiNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mEthernetNetworkAgent);
- assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
+ mEthernetAgent.connect(true);
+ callback.expectAvailableCallbacksUnvalidated(mEthernetAgent);
+ callback.expectLosing(mWiFiAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mEthernetAgent);
+ assertEquals(mEthernetAgent.getNetwork(), mCm.getActiveNetwork());
callback.assertNoCallback();
// Disconnect wifi, and then reconnect as if the user had selected "yes, don't ask again"
// (i.e., with explicitlySelected=true and acceptUnvalidated=true). Expect to switch to
// wifi immediately.
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.explicitlySelected(true, true);
- mWiFiNetworkAgent.connect(false);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- callback.expectLosing(mEthernetNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
- mEthernetNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
- expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.explicitlySelected(true, true);
+ mWiFiAgent.connect(false);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ callback.expectLosing(mEthernetAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
+ mEthernetAgent.disconnect();
+ callback.expect(LOST, mEthernetAgent);
+ expectUnvalidationCheckWillNotNotify(mWiFiAgent);
// Disconnect and reconnect with explicitlySelected=false and acceptUnvalidated=true.
// Check that the network is not scored specially and that the device prefers cell data.
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.explicitlySelected(false, true);
- mWiFiNetworkAgent.connect(false);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
- expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.explicitlySelected(false, true);
+ mWiFiAgent.connect(false);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
+ expectUnvalidationCheckWillNotNotify(mWiFiAgent);
// Clean up.
- mWiFiNetworkAgent.disconnect();
- mCellNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
+ mCellAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- callback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ callback.expect(LOST, mWiFiAgent);
+ callback.expect(LOST, mCellAgent);
}
private void doTestFirstEvaluation(
@@ -3825,36 +3869,36 @@
TestNetworkCallback callback = new TestNetworkCallback();
mCm.registerNetworkCallback(request, callback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- doConnect.accept(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ doConnect.accept(mWiFiAgent);
// Expect the available callbacks, but don't require specific values for their arguments
// since this method doesn't know how the network was connected.
- callback.expect(CallbackEntry.AVAILABLE, mWiFiNetworkAgent);
- callback.expect(CallbackEntry.NETWORK_CAPS_UPDATED, mWiFiNetworkAgent);
- callback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mWiFiNetworkAgent);
- callback.expect(CallbackEntry.BLOCKED_STATUS, mWiFiNetworkAgent);
+ callback.expect(AVAILABLE, mWiFiAgent);
+ callback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
+ callback.expect(LINK_PROPERTIES_CHANGED, mWiFiAgent);
+ callback.expect(BLOCKED_STATUS, mWiFiAgent);
if (waitForSecondCaps) {
// This is necessary because of b/245893397, the same bug that happens where we use
// expectAvailableDoubleValidatedCallbacks.
- callback.expect(CallbackEntry.NETWORK_CAPS_UPDATED, mWiFiNetworkAgent);
+ callback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
}
final NetworkAgentInfo nai =
- mService.getNetworkAgentInfoForNetwork(mWiFiNetworkAgent.getNetwork());
+ mService.getNetworkAgentInfoForNetwork(mWiFiAgent.getNetwork());
final long firstEvaluation = nai.getFirstEvaluationConcludedTime();
if (evaluatedByValidation) {
assertNotEquals(0L, firstEvaluation);
} else {
assertEquals(0L, firstEvaluation);
}
- mService.scheduleEvaluationTimeout(mWiFiNetworkAgent.getNetwork(), 0L /* timeout */);
+ mService.scheduleEvaluationTimeout(mWiFiAgent.getNetwork(), 0L /* timeout */);
waitForIdle();
if (evaluatedByValidation) {
assertEquals(firstEvaluation, nai.getFirstEvaluationConcludedTime());
} else {
assertNotEquals(0L, nai.getFirstEvaluationConcludedTime());
}
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
mCm.unregisterNetworkCallback(callback);
}
@@ -4023,9 +4067,9 @@
@Test
public void testRegisterIgnoringScore() throws Exception {
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.setScore(new NetworkScore.Builder().setLegacyInt(90).build());
- mWiFiNetworkAgent.connect(true /* validated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.setScore(new NetworkScore.Builder().setLegacyInt(90).build());
+ mWiFiAgent.connect(true /* validated */);
// Make sure the factory sees the default network
final NetworkCapabilities filter = new NetworkCapabilities();
@@ -4048,13 +4092,13 @@
testFactoryAll.expectRequestAdd();
// The legacy int will be ignored anyway, set the only other knob to true
- mWiFiNetworkAgent.setScore(new NetworkScore.Builder().setLegacyInt(110)
+ mWiFiAgent.setScore(new NetworkScore.Builder().setLegacyInt(110)
.setTransportPrimary(true).build());
expectNoRequestChanged(testFactory); // still not seeing the request
expectNoRequestChanged(testFactoryAll); // still seeing the request
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
}
@Test
@@ -4142,18 +4186,18 @@
@Test
public void testMMSonWiFi() throws Exception {
// Test bringing up cellular without MMS NetworkRequest gets reaped
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_MMS);
- mCellNetworkAgent.connectWithoutInternet();
- mCellNetworkAgent.expectDisconnected();
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.addCapability(NET_CAPABILITY_MMS);
+ mCellAgent.connectWithoutInternet();
+ mCellAgent.expectDisconnected();
waitForIdle();
assertEmpty(mCm.getAllNetworks());
verifyNoNetwork();
// Test bringing up validated WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
final ExpectedBroadcast b = expectConnectivityAction(TYPE_WIFI, DetailedState.CONNECTED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
@@ -4164,24 +4208,24 @@
mCm.requestNetwork(builder.build(), networkCallback);
// Test bringing up unvalidated cellular with MMS
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_MMS);
- mCellNetworkAgent.connectWithoutInternet();
- networkCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.addCapability(NET_CAPABILITY_MMS);
+ mCellAgent.connectWithoutInternet();
+ networkCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
verifyActiveNetwork(TRANSPORT_WIFI);
// Test releasing NetworkRequest disconnects cellular with MMS
mCm.unregisterNetworkCallback(networkCallback);
- mCellNetworkAgent.expectDisconnected();
+ mCellAgent.expectDisconnected();
verifyActiveNetwork(TRANSPORT_WIFI);
}
@Test
public void testMMSonCell() throws Exception {
// Test bringing up cellular without MMS
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
ExpectedBroadcast b = expectConnectivityAction(TYPE_MOBILE, DetailedState.CONNECTED);
- mCellNetworkAgent.connect(false);
+ mCellAgent.connect(false);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_CELLULAR);
@@ -4215,143 +4259,139 @@
mCm.registerNetworkCallback(request, callback);
// Bring up validated mobile data.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
// Bring up wifi with partial connectivity.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithPartialConnectivity();
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connectWithPartialConnectivity();
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiAgent);
// Mobile data should be the default network.
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
callback.assertNoCallback();
// Expect a PARTIAL_CONNECTIVITY notification. The notification appears as soon as partial
// connectivity is detected, and is low priority because the network was not explicitly
// selected by the user. This happens if we reconnect to a network where the user previously
// accepted partial connectivity without checking "always".
- expectNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY);
+ expectNotification(mWiFiAgent, NotificationType.PARTIAL_CONNECTIVITY);
// With HTTPS probe disabled, NetworkMonitor should pass the network validation with http
// probe.
- mWiFiNetworkAgent.setNetworkPartialValid(false /* privateDnsProbeSent */);
+ mWiFiAgent.setNetworkPartialValid(false /* privateDnsProbeSent */);
// If the user chooses yes to use this partial connectivity wifi, switch the default
// network to wifi and check if wifi becomes valid or not.
- mCm.setAcceptPartialConnectivity(mWiFiNetworkAgent.getNetwork(), true /* accept */,
+ mCm.setAcceptPartialConnectivity(mWiFiAgent.getNetwork(), true /* accept */,
false /* always */);
// If user accepts partial connectivity network,
// NetworkMonitor#setAcceptPartialConnectivity() should be called too.
waitForIdle();
- verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
+ verify(mWiFiAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
// Need a trigger point to let NetworkMonitor tell ConnectivityService that the network is
// validated.
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- callback.expectLosing(mCellNetworkAgent);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ callback.expectLosing(mCellAgent);
NetworkCapabilities nc = callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED,
- mWiFiNetworkAgent);
+ mWiFiAgent);
assertTrue(nc.hasCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
// Once the network validates, the notification disappears.
- expectClearNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY);
+ expectClearNotification(mWiFiAgent, NotificationType.PARTIAL_CONNECTIVITY);
// Disconnect and reconnect wifi with partial connectivity again.
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithPartialConnectivity();
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connectWithPartialConnectivity();
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiAgent);
// Mobile data should be the default network.
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
waitForIdle();
// Expect a low-priority PARTIAL_CONNECTIVITY notification as soon as partial connectivity
// is detected.
- expectNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY);
+ expectNotification(mWiFiAgent, NotificationType.PARTIAL_CONNECTIVITY);
// If the user chooses no, disconnect wifi immediately.
- mCm.setAcceptPartialConnectivity(mWiFiNetworkAgent.getNetwork(), false /* accept */,
+ mCm.setAcceptPartialConnectivity(mWiFiAgent.getNetwork(), false /* accept */,
false /* always */);
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- expectClearNotification(mWiFiNetworkAgent, NotificationType.PARTIAL_CONNECTIVITY);
+ callback.expect(LOST, mWiFiAgent);
+ expectClearNotification(mWiFiAgent, NotificationType.PARTIAL_CONNECTIVITY);
reset(mNotificationManager);
// If the user accepted partial connectivity before, and the device connects to that network
// again, but now the network has full connectivity, then the network shouldn't contain
// NET_CAPABILITY_PARTIAL_CONNECTIVITY.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
// acceptUnvalidated is also used as setting for accepting partial networks.
- mWiFiNetworkAgent.explicitlySelected(true /* explicitlySelected */,
- true /* acceptUnvalidated */);
- mWiFiNetworkAgent.connect(true);
- expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
+ mWiFiAgent.explicitlySelected(true /* explicitlySelected */, true /* acceptUnvalidated */);
+ mWiFiAgent.connect(true);
+ expectUnvalidationCheckWillNotNotify(mWiFiAgent);
// If user accepted partial connectivity network before,
// NetworkMonitor#setAcceptPartialConnectivity() will be called in
// ConnectivityService#updateNetworkInfo().
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
- callback.expectLosing(mCellNetworkAgent);
- nc = callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ verify(mWiFiAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
+ callback.expectLosing(mCellAgent);
+ nc = callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
assertFalse(nc.hasCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY));
// Wifi should be the default network.
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
// The user accepted partial connectivity and selected "don't ask again". Now the user
// reconnects to the partial connectivity network. Switch to wifi as soon as partial
// connectivity is detected.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.explicitlySelected(true /* explicitlySelected */,
- true /* acceptUnvalidated */);
- mWiFiNetworkAgent.connectWithPartialConnectivity();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.explicitlySelected(true /* explicitlySelected */, true /* acceptUnvalidated */);
+ mWiFiAgent.connectWithPartialConnectivity();
// If user accepted partial connectivity network before,
// NetworkMonitor#setAcceptPartialConnectivity() will be called in
// ConnectivityService#updateNetworkInfo().
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
- callback.expectLosing(mCellNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
- callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiNetworkAgent);
- expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ verify(mWiFiAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
+ callback.expectLosing(mCellAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
+ callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiAgent);
+ expectUnvalidationCheckWillNotNotify(mWiFiAgent);
- mWiFiNetworkAgent.setNetworkValid(false /* privateDnsProbeSent */);
+ mWiFiAgent.setNetworkValid(false /* privateDnsProbeSent */);
// Need a trigger point to let NetworkMonitor tell ConnectivityService that the network is
// validated.
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
// If the user accepted partial connectivity, and the device auto-reconnects to the partial
// connectivity network, it should contain both PARTIAL_CONNECTIVITY and VALIDATED.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.explicitlySelected(false /* explicitlySelected */,
- true /* acceptUnvalidated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.explicitlySelected(false /* explicitlySelected */, true /* acceptUnvalidated */);
// NetworkMonitor will immediately (once the HTTPS probe fails...) report the network as
// valid, because ConnectivityService calls setAcceptPartialConnectivity before it calls
// notifyNetworkConnected.
- mWiFiNetworkAgent.connectWithPartialValidConnectivity(
- false /* privateDnsProbeSent */);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
- callback.expectLosing(mCellNetworkAgent);
+ mWiFiAgent.connectWithPartialValidConnectivity(false /* privateDnsProbeSent */);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ verify(mWiFiAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
+ callback.expectLosing(mCellAgent);
callback.expectCapabilitiesWith(
- NET_CAPABILITY_PARTIAL_CONNECTIVITY | NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ NET_CAPABILITY_PARTIAL_CONNECTIVITY | NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ expectUnvalidationCheckWillNotNotify(mWiFiAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
verifyNoMoreInteractions(mNotificationManager);
}
@@ -4370,43 +4410,39 @@
// Bring up a network with a captive portal.
// Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
String redirectUrl = "http://android.com/path";
- mWiFiNetworkAgent.connectWithCaptivePortal(redirectUrl,
- false /* privateDnsProbeSent */);
- wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), redirectUrl);
+ mWiFiAgent.connectWithCaptivePortal(redirectUrl, false /* privateDnsProbeSent */);
+ wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.waitForRedirectUrl(), redirectUrl);
// This is necessary because of b/245893397, the same bug that happens where we use
// expectAvailableDoubleValidatedCallbacks.
// TODO : fix b/245893397 and remove this.
- wifiCallback.expectCapabilitiesWith(NET_CAPABILITY_CAPTIVE_PORTAL, mWiFiNetworkAgent);
+ wifiCallback.expectCapabilitiesWith(NET_CAPABILITY_CAPTIVE_PORTAL, mWiFiAgent);
// Check that startCaptivePortalApp sends the expected command to NetworkMonitor.
- mCm.startCaptivePortalApp(mWiFiNetworkAgent.getNetwork());
- verify(mWiFiNetworkAgent.mNetworkMonitor, timeout(TIMEOUT_MS).times(1))
- .launchCaptivePortalApp();
+ mCm.startCaptivePortalApp(mWiFiAgent.getNetwork());
+ verify(mWiFiAgent.mNetworkMonitor, timeout(TIMEOUT_MS).times(1)).launchCaptivePortalApp();
// Report that the captive portal is dismissed with partial connectivity, and check that
// callbacks are fired with PARTIAL and without CAPTIVE_PORTAL.
- mWiFiNetworkAgent.setNetworkPartial();
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
+ mWiFiAgent.setNetworkPartial();
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
waitForIdle();
- wifiCallback.expectCapabilitiesThat(
- mWiFiNetworkAgent, nc ->
+ wifiCallback.expectCapabilitiesThat(mWiFiAgent, nc ->
nc.hasCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY)
- && !nc.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
+ && !nc.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
// Report partial connectivity is accepted.
- mWiFiNetworkAgent.setNetworkPartialValid(false /* privateDnsProbeSent */);
- mCm.setAcceptPartialConnectivity(mWiFiNetworkAgent.getNetwork(), true /* accept */,
+ mWiFiAgent.setNetworkPartialValid(false /* privateDnsProbeSent */);
+ mCm.setAcceptPartialConnectivity(mWiFiAgent.getNetwork(), true /* accept */,
false /* always */);
waitForIdle();
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- wifiCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- validatedCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- validatedCallback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY,
- mWiFiNetworkAgent);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ wifiCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ validatedCallback.expectAvailableCallbacksValidated(mWiFiAgent);
+ validatedCallback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiAgent);
mCm.unregisterNetworkCallback(wifiCallback);
mCm.unregisterNetworkCallback(validatedCallback);
@@ -4426,41 +4462,39 @@
// Bring up a network with a captive portal.
// Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
String firstRedirectUrl = "http://example.com/firstPath";
- mWiFiNetworkAgent.connectWithCaptivePortal(firstRedirectUrl,
- false /* privateDnsProbeSent */);
- captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), firstRedirectUrl);
+ mWiFiAgent.connectWithCaptivePortal(firstRedirectUrl, false /* privateDnsProbeSent */);
+ captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.waitForRedirectUrl(), firstRedirectUrl);
// Take down network.
// Expect onLost callback.
- mWiFiNetworkAgent.disconnect();
- captivePortalCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ captivePortalCallback.expect(LOST, mWiFiAgent);
// Bring up a network with a captive portal.
// Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
String secondRedirectUrl = "http://example.com/secondPath";
- mWiFiNetworkAgent.connectWithCaptivePortal(secondRedirectUrl,
- false /* privateDnsProbeSent */);
- captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), secondRedirectUrl);
+ mWiFiAgent.connectWithCaptivePortal(secondRedirectUrl, false /* privateDnsProbeSent */);
+ captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.waitForRedirectUrl(), secondRedirectUrl);
// Make captive portal disappear then revalidate.
// Expect onLost callback because network no longer provides NET_CAPABILITY_CAPTIVE_PORTAL.
- mWiFiNetworkAgent.setNetworkValid(false /* privateDnsProbeSent */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- captivePortalCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkValid(false /* privateDnsProbeSent */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ captivePortalCallback.expect(LOST, mWiFiAgent);
// Expect NET_CAPABILITY_VALIDATED onAvailable callback.
- validatedCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
+ validatedCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
// Break network connectivity.
// Expect NET_CAPABILITY_VALIDATED onLost callback.
- mWiFiNetworkAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
- validatedCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), false);
+ validatedCallback.expect(LOST, mWiFiAgent);
}
private Intent startCaptivePortalApp(TestNetworkAgentWrapper networkAgent) throws Exception {
@@ -4497,37 +4531,35 @@
mCm.registerNetworkCallback(validatedRequest, validatedCallback);
// Bring up wifi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- validatedCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- Network wifiNetwork = mWiFiNetworkAgent.getNetwork();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ validatedCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ Network wifiNetwork = mWiFiAgent.getNetwork();
// Check that calling startCaptivePortalApp does nothing.
final int fastTimeoutMs = 100;
mCm.startCaptivePortalApp(wifiNetwork);
waitForIdle();
- verify(mWiFiNetworkAgent.mNetworkMonitor, never()).launchCaptivePortalApp();
+ verify(mWiFiAgent.mNetworkMonitor, never()).launchCaptivePortalApp();
mServiceContext.expectNoStartActivityIntent(fastTimeoutMs);
// Turn into a captive portal.
- mWiFiNetworkAgent.setNetworkPortal("http://example.com",
- false /* privateDnsProbeSent */);
+ mWiFiAgent.setNetworkPortal("http://example.com", false /* privateDnsProbeSent */);
mCm.reportNetworkConnectivity(wifiNetwork, false);
- captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- validatedCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ validatedCallback.expect(LOST, mWiFiAgent);
// This is necessary because of b/245893397, the same bug that happens where we use
// expectAvailableDoubleValidatedCallbacks.
// TODO : fix b/245893397 and remove this.
- captivePortalCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mWiFiNetworkAgent);
+ captivePortalCallback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
- startCaptivePortalApp(mWiFiNetworkAgent);
+ startCaptivePortalApp(mWiFiAgent);
// Report that the captive portal is dismissed, and check that callbacks are fired
- mWiFiNetworkAgent.setNetworkValid(false /* privateDnsProbeSent */);
- mWiFiNetworkAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
- validatedCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- captivePortalCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkValid(false /* privateDnsProbeSent */);
+ mWiFiAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
+ validatedCallback.expectAvailableCallbacksValidated(mWiFiAgent);
+ captivePortalCallback.expect(LOST, mWiFiAgent);
mCm.unregisterNetworkCallback(validatedCallback);
mCm.unregisterNetworkCallback(captivePortalCallback);
@@ -4540,11 +4572,11 @@
.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithCaptivePortal(TEST_REDIRECT_URL, false);
- captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connectWithCaptivePortal(TEST_REDIRECT_URL, false);
+ captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
- final Intent signInIntent = startCaptivePortalApp(mWiFiNetworkAgent);
+ final Intent signInIntent = startCaptivePortalApp(mWiFiAgent);
final CaptivePortal captivePortal = signInIntent
.getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
@@ -4552,14 +4584,14 @@
waitForIdle();
// Since network will disconnect, ensure no notification of response to NetworkMonitor
- verify(mWiFiNetworkAgent.mNetworkMonitor, never())
+ verify(mWiFiAgent.mNetworkMonitor, never())
.notifyCaptivePortalAppFinished(CaptivePortal.APP_RETURN_UNWANTED);
// Report that the network is disconnected
- mWiFiNetworkAgent.expectDisconnected();
- mWiFiNetworkAgent.expectPreventReconnectReceived();
- verify(mWiFiNetworkAgent.mNetworkMonitor).notifyNetworkDisconnected();
- captivePortalCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.expectDisconnected();
+ mWiFiAgent.expectPreventReconnectReceived();
+ verify(mWiFiAgent.mNetworkMonitor).notifyNetworkDisconnected();
+ captivePortalCallback.expect(LOST, mWiFiAgent);
mCm.unregisterNetworkCallback(captivePortalCallback);
}
@@ -4579,13 +4611,12 @@
setCaptivePortalMode(ConnectivitySettingsManager.CAPTIVE_PORTAL_MODE_AVOID);
// Bring up a network with a captive portal.
// Expect it to fail to connect and not result in any callbacks.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- String firstRedirectUrl = "http://example.com/firstPath";
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ final String firstRedirectUrl = "http://example.com/firstPath";
- mWiFiNetworkAgent.connectWithCaptivePortal(firstRedirectUrl,
- false /* privateDnsProbeSent */);
- mWiFiNetworkAgent.expectDisconnected();
- mWiFiNetworkAgent.expectPreventReconnectReceived();
+ mWiFiAgent.connectWithCaptivePortal(firstRedirectUrl, false /* privateDnsProbeSent */);
+ mWiFiAgent.expectDisconnected();
+ mWiFiAgent.expectPreventReconnectReceived();
assertNoCallbacks(captivePortalCallback, validatedCallback);
}
@@ -4599,28 +4630,28 @@
.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
final String redirectUrl = "http://example.com/firstPath";
- mWiFiNetworkAgent.connectWithCaptivePortal(redirectUrl,
+ mWiFiAgent.connectWithCaptivePortal(redirectUrl,
false /* privateDnsProbeSent */);
- captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
final CaptivePortalData testData = new CaptivePortalData.Builder()
.setUserPortalUrl(Uri.parse(redirectUrl))
.setBytesRemaining(12345L)
.build();
- mWiFiNetworkAgent.notifyCapportApiDataChanged(testData);
+ mWiFiAgent.notifyCapportApiDataChanged(testData);
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> testData.equals(lp.getCaptivePortalData()));
final LinkProperties newLps = new LinkProperties();
newLps.setMtu(1234);
- mWiFiNetworkAgent.sendLinkProperties(newLps);
+ mWiFiAgent.sendLinkProperties(newLps);
// CaptivePortalData is not lost and unchanged when LPs are received from the NetworkAgent
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> testData.equals(lp.getCaptivePortalData()) && lp.getMtu() == 1234);
}
@@ -4634,11 +4665,11 @@
.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connectWithCaptivePortal(TEST_REDIRECT_URL,
+ mWiFiAgent.connectWithCaptivePortal(TEST_REDIRECT_URL,
false /* privateDnsProbeSent */);
- captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ captivePortalCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
return captivePortalCallback;
}
@@ -4713,52 +4744,52 @@
final CaptivePortalTestData captivePortalTestData = setupCaptivePortalData();
// Baseline capport data
- mWiFiNetworkAgent.notifyCapportApiDataChanged(captivePortalTestData.mCapportData);
+ mWiFiAgent.notifyCapportApiDataChanged(captivePortalTestData.mCapportData);
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mCapportData.equals(lp.getCaptivePortalData()));
// Venue URL, T&C URL and friendly name from Network agent with Passpoint source, confirm
// that API data gets precedence on the bytes remaining.
final LinkProperties linkProperties = new LinkProperties();
linkProperties.setCaptivePortalData(captivePortalTestData.mNaPasspointData);
- mWiFiNetworkAgent.sendLinkProperties(linkProperties);
+ mWiFiAgent.sendLinkProperties(linkProperties);
// Make sure that the capport data is merged
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mExpectedMergedPasspointData
.equals(lp.getCaptivePortalData()));
// Now send this information from non-Passpoint source, confirm that Capport data takes
// precedence
linkProperties.setCaptivePortalData(captivePortalTestData.mNaOtherData);
- mWiFiNetworkAgent.sendLinkProperties(linkProperties);
+ mWiFiAgent.sendLinkProperties(linkProperties);
// Make sure that the capport data is merged
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mExpectedMergedOtherData
.equals(lp.getCaptivePortalData()));
// Create a new LP with no Network agent capport data
final LinkProperties newLps = new LinkProperties();
newLps.setMtu(1234);
- mWiFiNetworkAgent.sendLinkProperties(newLps);
+ mWiFiAgent.sendLinkProperties(newLps);
// CaptivePortalData is not lost and has the original values when LPs are received from the
// NetworkAgent
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mCapportData.equals(lp.getCaptivePortalData())
&& lp.getMtu() == 1234);
// Now send capport data only from the Network agent
- mWiFiNetworkAgent.notifyCapportApiDataChanged(null);
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ mWiFiAgent.notifyCapportApiDataChanged(null);
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> lp.getCaptivePortalData() == null);
newLps.setCaptivePortalData(captivePortalTestData.mNaPasspointData);
- mWiFiNetworkAgent.sendLinkProperties(newLps);
+ mWiFiAgent.sendLinkProperties(newLps);
// Make sure that only the network agent capport data is available
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mNaPasspointData.equals(lp.getCaptivePortalData()));
}
@@ -4771,27 +4802,27 @@
// on the bytes remaining.
final LinkProperties linkProperties = new LinkProperties();
linkProperties.setCaptivePortalData(captivePortalTestData.mNaPasspointData);
- mWiFiNetworkAgent.sendLinkProperties(linkProperties);
+ mWiFiAgent.sendLinkProperties(linkProperties);
// Make sure that the data is saved correctly
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mNaPasspointData.equals(lp.getCaptivePortalData()));
// Expected merged data: Network agent data is preferred, and values that are not used by
// it are merged from capport data
- mWiFiNetworkAgent.notifyCapportApiDataChanged(captivePortalTestData.mCapportData);
+ mWiFiAgent.notifyCapportApiDataChanged(captivePortalTestData.mCapportData);
// Make sure that the Capport data is merged correctly
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mExpectedMergedPasspointData.equals(
lp.getCaptivePortalData()));
// Now set the naData to null
linkProperties.setCaptivePortalData(null);
- mWiFiNetworkAgent.sendLinkProperties(linkProperties);
+ mWiFiAgent.sendLinkProperties(linkProperties);
// Make sure that the Capport data is retained correctly
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mCapportData.equals(lp.getCaptivePortalData()));
}
@@ -4805,18 +4836,18 @@
// on the bytes remaining.
final LinkProperties linkProperties = new LinkProperties();
linkProperties.setCaptivePortalData(captivePortalTestData.mNaOtherData);
- mWiFiNetworkAgent.sendLinkProperties(linkProperties);
+ mWiFiAgent.sendLinkProperties(linkProperties);
// Make sure that the data is saved correctly
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mNaOtherData.equals(lp.getCaptivePortalData()));
// Expected merged data: Network agent data is preferred, and values that are not used by
// it are merged from capport data
- mWiFiNetworkAgent.notifyCapportApiDataChanged(captivePortalTestData.mCapportData);
+ mWiFiAgent.notifyCapportApiDataChanged(captivePortalTestData.mCapportData);
// Make sure that the Capport data is merged correctly
- captivePortalCallback.expectLinkPropertiesThat(mWiFiNetworkAgent,
+ captivePortalCallback.expectLinkPropertiesThat(mWiFiAgent,
lp -> captivePortalTestData.mExpectedMergedOtherData.equals(
lp.getCaptivePortalData()));
}
@@ -4938,51 +4969,50 @@
LocalStringNetworkSpecifier nsFoo = new LocalStringNetworkSpecifier("foo");
LocalStringNetworkSpecifier nsBar = new LocalStringNetworkSpecifier("bar");
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiNetworkAgent, null /* specifier */,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiAgent, null /* specifier */,
cEmpty1, cEmpty2, cEmpty3, cEmpty4);
assertNoCallbacks(cFoo, cBar);
- mWiFiNetworkAgent.disconnect();
- expectOnLost(mWiFiNetworkAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4);
+ mWiFiAgent.disconnect();
+ expectOnLost(mWiFiAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.setNetworkSpecifier(nsFoo);
- mWiFiNetworkAgent.connect(false);
- expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiNetworkAgent, nsFoo,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.setNetworkSpecifier(nsFoo);
+ mWiFiAgent.connect(false);
+ expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiAgent, nsFoo,
cEmpty1, cEmpty2, cEmpty3, cEmpty4, cFoo);
cBar.assertNoCallback();
assertEquals(nsFoo,
- mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).getNetworkSpecifier());
+ mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).getNetworkSpecifier());
assertNoCallbacks(cEmpty1, cEmpty2, cEmpty3, cEmpty4, cFoo);
- mWiFiNetworkAgent.disconnect();
- expectOnLost(mWiFiNetworkAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4, cFoo);
+ mWiFiAgent.disconnect();
+ expectOnLost(mWiFiAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4, cFoo);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.setNetworkSpecifier(nsBar);
- mWiFiNetworkAgent.connect(false);
- expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiNetworkAgent, nsBar,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.setNetworkSpecifier(nsBar);
+ mWiFiAgent.connect(false);
+ expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiAgent, nsBar,
cEmpty1, cEmpty2, cEmpty3, cEmpty4, cBar);
cFoo.assertNoCallback();
assertEquals(nsBar,
- mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).getNetworkSpecifier());
+ mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).getNetworkSpecifier());
- mWiFiNetworkAgent.disconnect();
- expectOnLost(mWiFiNetworkAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4, cBar);
+ mWiFiAgent.disconnect();
+ expectOnLost(mWiFiAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4, cBar);
cFoo.assertNoCallback();
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.setNetworkSpecifier(new ConfidentialMatchAllNetworkSpecifier());
- mWiFiNetworkAgent.connect(false);
- expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiNetworkAgent, null /* specifier */,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.setNetworkSpecifier(new ConfidentialMatchAllNetworkSpecifier());
+ mWiFiAgent.connect(false);
+ expectAvailableCallbacksUnvalidatedWithSpecifier(mWiFiAgent, null /* specifier */,
cEmpty1, cEmpty2, cEmpty3, cEmpty4, cFoo, cBar);
- assertNull(
- mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).getNetworkSpecifier());
+ assertNull(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).getNetworkSpecifier());
- mWiFiNetworkAgent.disconnect();
- expectOnLost(mWiFiNetworkAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4, cFoo, cBar);
+ mWiFiAgent.disconnect();
+ expectOnLost(mWiFiAgent, cEmpty1, cEmpty2, cEmpty3, cEmpty4, cFoo, cBar);
}
/**
@@ -5061,8 +5091,8 @@
@Test
public void testNetworkRequestUidSpoofSecurityException() throws Exception {
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
NetworkRequest networkRequest = newWifiRequestBuilder().build();
TestNetworkCallback networkCallback = new TestNetworkCallback();
doThrow(new SecurityException()).when(mAppOpsManager).checkPackage(anyInt(), anyString());
@@ -5117,35 +5147,35 @@
cellNetworkCallback.assertNoCallback();
// Bring up cell and expect CALLBACK_AVAILABLE.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- systemDefaultCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ systemDefaultCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
assertEquals(defaultNetworkCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
assertEquals(systemDefaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Bring up wifi and expect CALLBACK_AVAILABLE.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
cellNetworkCallback.assertNoCallback();
- defaultNetworkCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- systemDefaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
+ defaultNetworkCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ systemDefaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
assertEquals(defaultNetworkCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
assertEquals(systemDefaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Bring down cell. Expect no default network callback, since it wasn't the default.
- mCellNetworkAgent.disconnect();
- cellNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ cellNetworkCallback.expect(LOST, mCellAgent);
defaultNetworkCallback.assertNoCallback();
systemDefaultCallback.assertNoCallback();
assertEquals(defaultNetworkCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
assertEquals(systemDefaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
// Bring up cell. Expect no default network callback, since it won't be the default.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
defaultNetworkCallback.assertNoCallback();
systemDefaultCallback.assertNoCallback();
assertEquals(defaultNetworkCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
@@ -5153,16 +5183,16 @@
// Bring down wifi. Expect the default network callback to notified of LOST wifi
// followed by AVAILABLE cell.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
cellNetworkCallback.assertNoCallback();
- defaultNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- systemDefaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- systemDefaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- mCellNetworkAgent.disconnect();
- cellNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- defaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- systemDefaultCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ defaultNetworkCallback.expect(LOST, mWiFiAgent);
+ defaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
+ systemDefaultCallback.expect(LOST, mWiFiAgent);
+ systemDefaultCallback.expectAvailableCallbacksValidated(mCellAgent);
+ mCellAgent.disconnect();
+ cellNetworkCallback.expect(LOST, mCellAgent);
+ defaultNetworkCallback.expect(LOST, mCellAgent);
+ systemDefaultCallback.expect(LOST, mCellAgent);
waitForIdle();
assertEquals(null, mCm.getActiveNetwork());
@@ -5174,7 +5204,7 @@
assertEquals(null, systemDefaultCallback.getLastAvailableNetwork());
mMockVpn.disconnect();
- defaultNetworkCallback.expect(CallbackEntry.LOST, mMockVpn);
+ defaultNetworkCallback.expect(LOST, mMockVpn);
systemDefaultCallback.assertNoCallback();
waitForIdle();
assertEquals(null, mCm.getActiveNetwork());
@@ -5189,29 +5219,27 @@
mCm.requestNetwork(cellRequest, cellNetworkCallback);
// Bring up the mobile network.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
// We should get onAvailable(), onCapabilitiesChanged(), and
// onLinkPropertiesChanged() in rapid succession. Additionally, we
// should get onCapabilitiesChanged() when the mobile network validates.
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
cellNetworkCallback.assertNoCallback();
// Update LinkProperties.
final LinkProperties lp = new LinkProperties();
lp.setInterfaceName("foonet_data0");
- mCellNetworkAgent.sendLinkProperties(lp);
+ mCellAgent.sendLinkProperties(lp);
// We should get onLinkPropertiesChanged().
- cellNetworkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- mCellNetworkAgent);
+ cellNetworkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
cellNetworkCallback.assertNoCallback();
// Suspend the network.
- mCellNetworkAgent.suspend();
- cellNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_SUSPENDED,
- mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.SUSPENDED, mCellNetworkAgent);
+ mCellAgent.suspend();
+ cellNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_SUSPENDED, mCellAgent);
+ cellNetworkCallback.expect(SUSPENDED, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertEquals(NetworkInfo.State.SUSPENDED, mCm.getActiveNetworkInfo().getState());
@@ -5220,21 +5248,20 @@
mCm.registerDefaultNetworkCallback(dfltNetworkCallback);
// We should get onAvailable(), onCapabilitiesChanged(), onLinkPropertiesChanged(),
// as well as onNetworkSuspended() in rapid succession.
- dfltNetworkCallback.expectAvailableAndSuspendedCallbacks(mCellNetworkAgent, true);
+ dfltNetworkCallback.expectAvailableAndSuspendedCallbacks(mCellAgent, true);
dfltNetworkCallback.assertNoCallback();
mCm.unregisterNetworkCallback(dfltNetworkCallback);
- mCellNetworkAgent.resume();
- cellNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_SUSPENDED,
- mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.RESUMED, mCellNetworkAgent);
+ mCellAgent.resume();
+ cellNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_SUSPENDED, mCellAgent);
+ cellNetworkCallback.expect(RESUMED, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertEquals(NetworkInfo.State.CONNECTED, mCm.getActiveNetworkInfo().getState());
dfltNetworkCallback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(dfltNetworkCallback);
// This time onNetworkSuspended should not be called.
- dfltNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ dfltNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
dfltNetworkCallback.assertNoCallback();
mCm.unregisterNetworkCallback(dfltNetworkCallback);
@@ -5243,8 +5270,8 @@
@Test
public void testRegisterPrivilegedDefaultCallbacksRequirePermissions() throws Exception {
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false /* validated */);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false /* validated */);
mServiceContext.setPermission(CONNECTIVITY_USE_RESTRICTED_NETWORKS, PERMISSION_DENIED);
final Handler handler = new Handler(ConnectivityThread.getInstanceLooper());
@@ -5259,16 +5286,16 @@
mServiceContext.setPermission(CONNECTIVITY_USE_RESTRICTED_NETWORKS, PERMISSION_GRANTED);
mCm.registerSystemDefaultNetworkCallback(callback, handler);
mServiceContext.setPermission(CONNECTIVITY_USE_RESTRICTED_NETWORKS, PERMISSION_DENIED);
- callback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mCellAgent);
mCm.unregisterNetworkCallback(callback);
mServiceContext.setPermission(NETWORK_SETTINGS, PERMISSION_GRANTED);
mCm.registerSystemDefaultNetworkCallback(callback, handler);
- callback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mCellAgent);
mCm.unregisterNetworkCallback(callback);
mCm.registerDefaultNetworkCallbackForUid(APP1_UID, callback, handler);
- callback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mCellAgent);
mCm.unregisterNetworkCallback(callback);
}
@@ -5299,15 +5326,15 @@
mCm.registerNetworkCallback(includeOtherUidsRequest, includeOtherUidsCallback);
// Both callbacks see a network with no specifier that applies to their UID.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false /* validated */);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- otherUidCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- includeOtherUidsCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- otherUidCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- includeOtherUidsCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false /* validated */);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ otherUidCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ includeOtherUidsCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
+ otherUidCallback.expect(LOST, mWiFiAgent);
+ includeOtherUidsCallback.expect(LOST, mWiFiAgent);
// Only the includeOtherUidsCallback sees a VPN that does not apply to its UID.
final UidRange range = UidRange.createForUser(UserHandle.of(RESTRICTED_USER));
@@ -5318,7 +5345,7 @@
otherUidCallback.assertNoCallback();
mMockVpn.disconnect();
- includeOtherUidsCallback.expect(CallbackEntry.LOST, mMockVpn);
+ includeOtherUidsCallback.expect(LOST, mMockVpn);
callback.assertNoCallback();
otherUidCallback.assertNoCallback();
}
@@ -5383,10 +5410,10 @@
final NetworkCapabilities ncTemplate = new NetworkCapabilities()
.addTransportType(TRANSPORT_WIFI)
.setNetworkSpecifier(specifier);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, emptyLp, ncTemplate);
- mWiFiNetworkAgent.connect(false /* validated */);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- otherUidCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, emptyLp, ncTemplate);
+ mWiFiAgent.connect(false /* validated */);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ otherUidCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
includeOtherUidsCallback.assertNoCallback();
}
@@ -5441,63 +5468,63 @@
mCm.registerNetworkCallback(request, callback);
mCm.registerNetworkCallback(fgRequest, fgCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- fgCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- assertTrue(isForegroundNetwork(mCellNetworkAgent));
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ fgCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ assertTrue(isForegroundNetwork(mCellAgent));
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
// When wifi connects, cell lingers.
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- callback.expectLosing(mCellNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- fgCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- fgCallback.expectLosing(mCellNetworkAgent);
- fgCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- assertTrue(isForegroundNetwork(mCellNetworkAgent));
- assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ callback.expectLosing(mCellAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ fgCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ fgCallback.expectLosing(mCellAgent);
+ fgCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ assertTrue(isForegroundNetwork(mCellAgent));
+ assertTrue(isForegroundNetwork(mWiFiAgent));
// When lingering is complete, cell is still there but is now in the background.
waitForIdle();
int timeoutMs = TEST_LINGER_DELAY_MS + TEST_LINGER_DELAY_MS / 4;
- fgCallback.expect(CallbackEntry.LOST, mCellNetworkAgent, timeoutMs);
+ fgCallback.expect(LOST, mCellAgent, timeoutMs);
// Expect a network capabilities update sans FOREGROUND.
- callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
- assertFalse(isForegroundNetwork(mCellNetworkAgent));
- assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+ callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellAgent);
+ assertFalse(isForegroundNetwork(mCellAgent));
+ assertTrue(isForegroundNetwork(mWiFiAgent));
// File a cell request and check that cell comes into the foreground.
final NetworkRequest cellRequest = new NetworkRequest.Builder()
.addTransportType(TRANSPORT_CELLULAR).build();
final TestNetworkCallback cellCallback = new TestNetworkCallback();
mCm.requestNetwork(cellRequest, cellCallback);
- cellCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- fgCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ cellCallback.expectAvailableCallbacksValidated(mCellAgent);
+ fgCallback.expectAvailableCallbacksValidated(mCellAgent);
// Expect a network capabilities update with FOREGROUND, because the most recent
// request causes its state to change.
- cellCallback.expectCapabilitiesWith(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
- callback.expectCapabilitiesWith(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
- assertTrue(isForegroundNetwork(mCellNetworkAgent));
- assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+ cellCallback.expectCapabilitiesWith(NET_CAPABILITY_FOREGROUND, mCellAgent);
+ callback.expectCapabilitiesWith(NET_CAPABILITY_FOREGROUND, mCellAgent);
+ assertTrue(isForegroundNetwork(mCellAgent));
+ assertTrue(isForegroundNetwork(mWiFiAgent));
// Release the request. The network immediately goes into the background, since it was not
// lingering.
mCm.unregisterNetworkCallback(cellCallback);
- fgCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ fgCallback.expect(LOST, mCellAgent);
// Expect a network capabilities update sans FOREGROUND.
- callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
- assertFalse(isForegroundNetwork(mCellNetworkAgent));
- assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+ callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellAgent);
+ assertFalse(isForegroundNetwork(mCellAgent));
+ assertTrue(isForegroundNetwork(mWiFiAgent));
// Disconnect wifi and check that cell is foreground again.
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- fgCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- fgCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- assertTrue(isForegroundNetwork(mCellNetworkAgent));
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
+ fgCallback.expect(LOST, mWiFiAgent);
+ fgCallback.expectAvailableCallbacksValidated(mCellAgent);
+ assertTrue(isForegroundNetwork(mCellAgent));
mCm.unregisterNetworkCallback(callback);
mCm.unregisterNetworkCallback(fgCallback);
@@ -5538,11 +5565,11 @@
}
});
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
// Don't request that the network validate, because otherwise connect() will block until
// the network gets NET_CAPABILITY_VALIDATED, after all the callbacks below have fired,
// and we won't actually measure anything.
- mCellNetworkAgent.connect(false);
+ mCellAgent.connect(false);
long onAvailableDispatchingDuration = durationOf(() -> {
await(availableLatch, 10 * CONNECT_TIME_LIMIT_MS);
@@ -5555,9 +5582,9 @@
onAvailableDispatchingDuration <= CONNECT_TIME_LIMIT_MS);
// Give wifi a high enough score that we'll linger cell when wifi comes up.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.adjustScore(40);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.adjustScore(40);
+ mWiFiAgent.connect(false);
long onLostDispatchingDuration = durationOf(() -> {
await(losingLatch, 10 * SWITCH_TIME_LIMIT_MS);
@@ -5603,9 +5630,9 @@
assertTrue(testFactory.getMyStartRequested());
// Bring up wifi. The factory stops looking for a network.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
// Score 60 - 40 penalty for not validated yet, then 60 when it validates
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
// The network connects with a low score, so the offer can still beat it and
// nothing happens. Then the network validates, and the offer with its filter score
// of 40 can no longer beat it and the request is removed.
@@ -5624,9 +5651,9 @@
// Bring up cell data and check that the factory stops looking.
assertLength(1, mCm.getAllNetworks());
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false);
- cellNetworkCallback.expectAvailableCallbacks(mCellNetworkAgent, false, false, false,
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false);
+ cellNetworkCallback.expectAvailableCallbacks(mCellAgent, false, false, false,
TEST_CALLBACK_TIMEOUT_MS);
// When cell connects, it will satisfy the "mobile always on request" right away
// by virtue of being the only network that can satisfy the request. However, its
@@ -5636,11 +5663,11 @@
// Next, cell validates. This gives it a score of 50 and the test factory can't
// hope to beat that according to its filters. It will see the message that its
// offer is now unnecessary.
- mCellNetworkAgent.setNetworkValid(true);
+ mCellAgent.setNetworkValid(true);
// Need a trigger point to let NetworkMonitor tell ConnectivityService that network is
// validated – see testPartialConnectivity.
- mCm.reportNetworkConnectivity(mCellNetworkAgent.getNetwork(), true);
- cellNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mCellNetworkAgent);
+ mCm.reportNetworkConnectivity(mCellAgent.getNetwork(), true);
+ cellNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mCellAgent);
testFactory.expectRequestRemove();
testFactory.assertRequestCountEquals(0);
// Accordingly, the factory shouldn't be started.
@@ -5653,8 +5680,8 @@
// Cell disconnects. There is still the "mobile data always on" request outstanding,
// and the test factory should see it now that it isn't hopelessly outscored.
- mCellNetworkAgent.disconnect();
- cellNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ cellNetworkCallback.expect(LOST, mCellAgent);
// Wait for the network to be removed from internal structures before
// calling synchronous getter
waitForIdle();
@@ -5665,9 +5692,9 @@
// Reconnect cell validated, see the request disappear again. Then withdraw the
// mobile always on request. This will tear down cell, and there shouldn't be a
// blip where the test factory briefly sees the request or anything.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
waitForIdle();
assertLength(2, mCm.getAllNetworks());
testFactory.expectRequestRemove();
@@ -5677,7 +5704,7 @@
testFactory.assertRequestCountEquals(0);
assertFalse(testFactory.getMyStartRequested());
// ... and cell data to be torn down immediately since it is no longer nascent.
- cellNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ cellNetworkCallback.expect(LOST, mCellAgent);
waitForIdle();
assertLength(1, mCm.getAllNetworks());
testFactory.terminate();
@@ -5822,39 +5849,39 @@
mCm.registerNetworkCallback(wifiRequest, wifiNetworkCallback);
// Cell connects and validates.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR,
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR,
new LinkProperties(), null /* ncTemplate */, cellProvider);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
cellCallback.assertNoCallback();
wifiCallback.assertNoCallback();
// Bring up wifi. At first it's invalidated, so cell is still needed.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI,
new LinkProperties(), null /* ncTemplate */, wifiProvider);
- mWiFiNetworkAgent.connect(false);
- wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent.connect(false);
+ wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
cellCallback.assertNoCallback();
wifiCallback.assertNoCallback();
// Wifi validates. Cell is no longer needed, because it's outscored.
- mWiFiNetworkAgent.setNetworkValid(true /* privateDnsProbeSent */);
+ mWiFiAgent.setNetworkValid(true /* privateDnsProbeSent */);
// Have CS reconsider the network (see testPartialConnectivity)
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- wifiNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ wifiNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
cellCallback.expectOnNetworkUnneeded(defaultCaps);
wifiCallback.assertNoCallback();
// Wifi is no longer validated. Cell is needed again.
- mWiFiNetworkAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
- wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), false);
+ wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiAgent);
cellCallback.expectOnNetworkNeeded(defaultCaps);
wifiCallback.assertNoCallback();
// Disconnect wifi and pretend the carrier restricts moving away from bad wifi.
- mWiFiNetworkAgent.disconnect();
- wifiNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ wifiNetworkCallback.expect(LOST, mWiFiAgent);
// This has getAvoidBadWifi return false. This test doesn't change the value of the
// associated setting.
doReturn(0).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
@@ -5862,23 +5889,23 @@
waitForIdle();
// Connect wifi again, cell is needed until wifi validates.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI,
new LinkProperties(), null /* ncTemplate */, wifiProvider);
- mWiFiNetworkAgent.connect(false);
- wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent.connect(false);
+ wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
cellCallback.assertNoCallback();
wifiCallback.assertNoCallback();
- mWiFiNetworkAgent.setNetworkValid(true /* privateDnsProbeSent */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- wifiNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkValid(true /* privateDnsProbeSent */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ wifiNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
cellCallback.expectOnNetworkUnneeded(defaultCaps);
wifiCallback.assertNoCallback();
// Wifi loses validation. Because the device doesn't avoid bad wifis, cell is
// not needed.
- mWiFiNetworkAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
- wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), false);
+ wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiAgent);
cellCallback.assertNoCallback();
wifiCallback.assertNoCallback();
}
@@ -5896,23 +5923,23 @@
.clearCapabilities()
.addTransportType(TRANSPORT_WIFI)
.build();
- final TestableNetworkCallback wifiCallback = new TestableNetworkCallback();
+ final TestNetworkCallback wifiCallback = new TestNetworkCallback();
mCm.registerNetworkCallback(wifiRequest, wifiCallback);
// Bring up validated cell and unvalidated wifi.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ wifiCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
if (preferBadWifi) {
- expectUnvalidationCheckWillNotify(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
- mDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ expectUnvalidationCheckWillNotify(mWiFiAgent, NotificationType.LOST_INTERNET);
+ mDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
} else {
- expectUnvalidationCheckWillNotNotify(mWiFiNetworkAgent);
+ expectUnvalidationCheckWillNotNotify(mWiFiAgent);
mDefaultNetworkCallback.assertNoCallback();
}
}
@@ -5957,25 +5984,25 @@
mPolicyTracker.reevaluate();
// Bring up validated cell.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- defaultCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- Network cellNetwork = mCellNetworkAgent.getNetwork();
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ Network cellNetwork = mCellAgent.getNetwork();
// Bring up validated wifi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- validatedWifiCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- Network wifiNetwork = mWiFiNetworkAgent.getNetwork();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ validatedWifiCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ Network wifiNetwork = mWiFiAgent.getNetwork();
// Fail validation on wifi.
- mWiFiNetworkAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
+ mWiFiAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
mCm.reportNetworkConnectivity(wifiNetwork, false);
- defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- validatedWifiCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- expectNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
+ defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ validatedWifiCallback.expect(LOST, mWiFiAgent);
+ expectNotification(mWiFiAgent, NotificationType.LOST_INTERNET);
// Because avoid bad wifi is off, we don't switch to cellular.
defaultCallback.assertNoCallback();
@@ -5989,24 +6016,24 @@
// that we switch back to cell.
doReturn(1).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
mPolicyTracker.reevaluate();
- defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ defaultCallback.expectAvailableCallbacksValidated(mCellAgent);
assertEquals(mCm.getActiveNetwork(), cellNetwork);
- expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
+ expectClearNotification(mWiFiAgent, NotificationType.LOST_INTERNET);
// Switch back to a restrictive carrier.
doReturn(0).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
mPolicyTracker.reevaluate();
- defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
assertEquals(mCm.getActiveNetwork(), wifiNetwork);
// A notification was already shown for this very network.
- expectNoNotification(mWiFiNetworkAgent);
+ expectNoNotification(mWiFiAgent);
// Simulate the user selecting "switch" on the dialog, and check that we switch to cell.
// In principle this is a little bit unrealistic because the switch to a less restrictive
// carrier above should have remove the notification but this doesn't matter for the
// purposes of this test.
mCm.setAvoidUnvalidated(wifiNetwork);
- defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ defaultCallback.expectAvailableCallbacksValidated(mCellAgent);
assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(
NET_CAPABILITY_VALIDATED));
assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(
@@ -6014,54 +6041,54 @@
assertEquals(mCm.getActiveNetwork(), cellNetwork);
// Disconnect and reconnect wifi to clear the one-time switch above.
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- validatedWifiCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- wifiNetwork = mWiFiNetworkAgent.getNetwork();
+ mWiFiAgent.disconnect();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ validatedWifiCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ wifiNetwork = mWiFiAgent.getNetwork();
// Fail validation on wifi and expect the dialog to appear.
- mWiFiNetworkAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
+ mWiFiAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
mCm.reportNetworkConnectivity(wifiNetwork, false);
- defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- validatedWifiCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- expectNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
+ defaultCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ validatedWifiCallback.expect(LOST, mWiFiAgent);
+ expectNotification(mWiFiAgent, NotificationType.LOST_INTERNET);
// Simulate the user selecting "switch" and checking the don't ask again checkbox.
Settings.Global.putInt(cr, ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI, 1);
mPolicyTracker.reevaluate();
// We now switch to cell.
- defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ defaultCallback.expectAvailableCallbacksValidated(mCellAgent);
assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(
NET_CAPABILITY_VALIDATED));
assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(
NET_CAPABILITY_VALIDATED));
assertEquals(mCm.getActiveNetwork(), cellNetwork);
- expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
+ expectClearNotification(mWiFiAgent, NotificationType.LOST_INTERNET);
// Simulate the user turning the cellular fallback setting off and then on.
// We switch to wifi and then to cell.
Settings.Global.putString(cr, ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI, null);
mPolicyTracker.reevaluate();
- defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
assertEquals(mCm.getActiveNetwork(), wifiNetwork);
// Notification is cleared again because CS doesn't particularly remember that it has
// cleared it before, and if it hasn't cleared it before then it should do so now.
- expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
+ expectClearNotification(mWiFiAgent, NotificationType.LOST_INTERNET);
Settings.Global.putInt(cr, ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI, 1);
mPolicyTracker.reevaluate();
- defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ defaultCallback.expectAvailableCallbacksValidated(mCellAgent);
assertEquals(mCm.getActiveNetwork(), cellNetwork);
// If cell goes down, we switch to wifi.
- mCellNetworkAgent.disconnect();
- defaultCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mCellAgent.disconnect();
+ defaultCallback.expect(LOST, mCellAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
validatedWifiCallback.assertNoCallback();
// Notification is cleared yet again because the device switched to wifi.
- expectClearNotification(mWiFiNetworkAgent, NotificationType.LOST_INTERNET);
+ expectClearNotification(mWiFiAgent, NotificationType.LOST_INTERNET);
mCm.unregisterNetworkCallback(cellNetworkCallback);
mCm.unregisterNetworkCallback(validatedWifiCallback);
@@ -6098,9 +6125,9 @@
final TestNetworkCallback networkCallback = new TestNetworkCallback();
mCm.requestNetwork(nr, networkCallback, TEST_REQUEST_TIMEOUT_MS);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, false, false,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ networkCallback.expectAvailableCallbacks(mWiFiAgent, false, false, false,
TEST_CALLBACK_TIMEOUT_MS);
// pass timeout and validate that UNAVAILABLE is not called
@@ -6118,12 +6145,12 @@
final TestNetworkCallback networkCallback = new TestNetworkCallback();
mCm.requestNetwork(nr, networkCallback, TEST_REQUEST_TIMEOUT_MS);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, false, false,
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ networkCallback.expectAvailableCallbacks(mWiFiAgent, false, false, false,
TEST_CALLBACK_TIMEOUT_MS);
- mWiFiNetworkAgent.disconnect();
- networkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ networkCallback.expect(LOST, mWiFiAgent);
// Validate that UNAVAILABLE is not called
networkCallback.assertNoCallback();
@@ -6143,11 +6170,11 @@
mCm.requestNetwork(nr, networkCallback, timeoutMs);
// pass timeout and validate that UNAVAILABLE is called
- networkCallback.expect(CallbackEntry.UNAVAILABLE);
+ networkCallback.expect(UNAVAILABLE);
// create a network satisfying request - validate that request not triggered
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
networkCallback.assertNoCallback();
}
@@ -6169,8 +6196,8 @@
networkCallback.assertNoCallback();
// create a network satisfying request - validate that request not triggered
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
networkCallback.assertNoCallback();
}
@@ -6227,7 +6254,7 @@
// onUnavailable!
testFactory.triggerUnfulfillable(newRequest);
- networkCallback.expect(CallbackEntry.UNAVAILABLE);
+ networkCallback.expect(UNAVAILABLE);
// Declaring a request unfulfillable releases it automatically.
testFactory.expectRequestRemove();
@@ -6408,18 +6435,18 @@
private Network connectKeepaliveNetwork(LinkProperties lp) throws Exception {
// Ensure the network is disconnected before anything else occurs
- if (mWiFiNetworkAgent != null) {
- assertNull(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()));
+ if (mWiFiAgent != null) {
+ assertNull(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()));
}
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
ExpectedBroadcast b = expectConnectivityAction(TYPE_WIFI, DetailedState.CONNECTED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
b.expectBroadcast();
verifyActiveNetwork(TRANSPORT_WIFI);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent.sendLinkProperties(lp);
waitForIdle();
- return mWiFiNetworkAgent.getNetwork();
+ return mWiFiAgent.getNetwork();
}
@Test
@@ -6476,10 +6503,10 @@
callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
// Check that a started keepalive can be stopped.
- mWiFiNetworkAgent.setStartKeepaliveEvent(PacketKeepalive.SUCCESS);
+ mWiFiAgent.setStartKeepaliveEvent(PacketKeepalive.SUCCESS);
ka = mCm.startNattKeepalive(myNet, validKaInterval, callback, myIPv4, 12345, dstIPv4);
callback.expectStarted();
- mWiFiNetworkAgent.setStopKeepaliveEvent(PacketKeepalive.SUCCESS);
+ mWiFiAgent.setStopKeepaliveEvent(PacketKeepalive.SUCCESS);
ka.stop();
callback.expectStopped();
@@ -6489,15 +6516,15 @@
callback.expectStarted();
bogusLp.removeLinkAddress(new LinkAddress(myIPv4, 25));
bogusLp.addLinkAddress(new LinkAddress(notMyIPv4, 25));
- mWiFiNetworkAgent.sendLinkProperties(bogusLp);
+ mWiFiAgent.sendLinkProperties(bogusLp);
callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent.sendLinkProperties(lp);
// Check that a started keepalive is stopped correctly when the network disconnects.
ka = mCm.startNattKeepalive(myNet, validKaInterval, callback, myIPv4, 12345, dstIPv4);
callback.expectStarted();
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent.expectDisconnected();
+ mWiFiAgent.disconnect();
+ mWiFiAgent.expectDisconnected();
callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
// ... and that stopping it after that has no adverse effects.
@@ -6508,15 +6535,15 @@
// Reconnect.
myNet = connectKeepaliveNetwork(lp);
- mWiFiNetworkAgent.setStartKeepaliveEvent(PacketKeepalive.SUCCESS);
+ mWiFiAgent.setStartKeepaliveEvent(PacketKeepalive.SUCCESS);
// Check that keepalive slots start from 1 and increment. The first one gets slot 1.
- mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
+ mWiFiAgent.setExpectedKeepaliveSlot(1);
ka = mCm.startNattKeepalive(myNet, validKaInterval, callback, myIPv4, 12345, dstIPv4);
callback.expectStarted();
// The second one gets slot 2.
- mWiFiNetworkAgent.setExpectedKeepaliveSlot(2);
+ mWiFiAgent.setExpectedKeepaliveSlot(2);
TestKeepaliveCallback callback2 = new TestKeepaliveCallback();
PacketKeepalive ka2 = mCm.startNattKeepalive(
myNet, validKaInterval, callback2, myIPv4, 6789, dstIPv4);
@@ -6526,7 +6553,7 @@
ka.stop();
callback.expectStopped();
- mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
+ mWiFiAgent.setExpectedKeepaliveSlot(1);
TestKeepaliveCallback callback3 = new TestKeepaliveCallback();
PacketKeepalive ka3 = mCm.startNattKeepalive(
myNet, validKaInterval, callback3, myIPv4, 9876, dstIPv4);
@@ -6628,12 +6655,12 @@
}
// Check that a started keepalive can be stopped.
- mWiFiNetworkAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
+ mWiFiAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
try (SocketKeepalive ka = mCm.createSocketKeepalive(
myNet, testSocket, myIPv4, dstIPv4, executor, callback)) {
ka.start(validKaInterval);
callback.expectStarted();
- mWiFiNetworkAgent.setStopKeepaliveEvent(SocketKeepalive.SUCCESS);
+ mWiFiAgent.setStopKeepaliveEvent(SocketKeepalive.SUCCESS);
ka.stop();
callback.expectStopped();
@@ -6662,9 +6689,9 @@
callback.expectStarted();
bogusLp.removeLinkAddress(new LinkAddress(myIPv4, 25));
bogusLp.addLinkAddress(new LinkAddress(notMyIPv4, 25));
- mWiFiNetworkAgent.sendLinkProperties(bogusLp);
+ mWiFiAgent.sendLinkProperties(bogusLp);
callback.expectError(SocketKeepalive.ERROR_INVALID_IP_ADDRESS);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent.sendLinkProperties(lp);
}
// Check that a started keepalive is stopped correctly when the network disconnects.
@@ -6672,21 +6699,20 @@
myNet, testSocket, myIPv4, dstIPv4, executor, callback)) {
ka.start(validKaInterval);
callback.expectStarted();
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent.expectDisconnected();
+ mWiFiAgent.disconnect();
+ mWiFiAgent.expectDisconnected();
callback.expectError(SocketKeepalive.ERROR_INVALID_NETWORK);
// ... and that stopping it after that has no adverse effects.
waitForIdle();
- final Network myNetAlias = myNet;
- assertNull(mCm.getNetworkCapabilities(myNetAlias));
+ assertNull(mCm.getNetworkCapabilities(myNet));
ka.stop();
callback.assertNoCallback();
}
// Reconnect.
myNet = connectKeepaliveNetwork(lp);
- mWiFiNetworkAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
+ mWiFiAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
// Check that a stop followed by network disconnects does not result in crash.
try (SocketKeepalive ka = mCm.createSocketKeepalive(
@@ -6695,7 +6721,7 @@
callback.expectStarted();
// Delay the response of keepalive events in networkAgent long enough to make sure
// the follow-up network disconnection will be processed first.
- mWiFiNetworkAgent.setKeepaliveResponseDelay(3 * TIMEOUT_MS);
+ mWiFiAgent.setKeepaliveResponseDelay(3 * TIMEOUT_MS);
ka.stop();
// Call stop() twice shouldn't result in crash, b/182586681.
ka.stop();
@@ -6705,8 +6731,8 @@
waitForIdleSerialExecutor(executor, TIMEOUT_MS);
waitForIdle();
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent.expectDisconnected();
+ mWiFiAgent.disconnect();
+ mWiFiAgent.expectDisconnected();
callback.expectStopped();
callback.assertNoCallback();
}
@@ -6714,20 +6740,18 @@
// Reconnect.
waitForIdle();
myNet = connectKeepaliveNetwork(lp);
- mWiFiNetworkAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
+ mWiFiAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
// Check that keepalive slots start from 1 and increment. The first one gets slot 1.
- mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
- int srcPort2 = 0;
+ mWiFiAgent.setExpectedKeepaliveSlot(1);
try (SocketKeepalive ka = mCm.createSocketKeepalive(
myNet, testSocket, myIPv4, dstIPv4, executor, callback)) {
ka.start(validKaInterval);
callback.expectStarted();
// The second one gets slot 2.
- mWiFiNetworkAgent.setExpectedKeepaliveSlot(2);
+ mWiFiAgent.setExpectedKeepaliveSlot(2);
final UdpEncapsulationSocket testSocket2 = mIpSec.openUdpEncapsulationSocket();
- srcPort2 = testSocket2.getPort();
TestSocketKeepaliveCallback callback2 = new TestSocketKeepaliveCallback(executor);
try (SocketKeepalive ka2 = mCm.createSocketKeepalive(
myNet, testSocket2, myIPv4, dstIPv4, executor, callback2)) {
@@ -6750,9 +6774,9 @@
// assertFalse(isUdpPortInUse(srcPort));
// assertFalse(isUdpPortInUse(srcPort2));
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent.expectDisconnected();
- mWiFiNetworkAgent = null;
+ mWiFiAgent.disconnect();
+ mWiFiAgent.expectDisconnected();
+ mWiFiAgent = null;
}
@Test
@@ -6826,9 +6850,9 @@
testSocketV4.close();
testSocketV6.close();
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent.expectDisconnected();
- mWiFiNetworkAgent = null;
+ mWiFiAgent.disconnect();
+ mWiFiAgent.expectDisconnected();
+ mWiFiAgent = null;
}
private void doTestNattSocketKeepalivesFdWithExecutor(Executor executor) throws Exception {
@@ -6843,8 +6867,8 @@
lp.addLinkAddress(new LinkAddress(myIPv4, 25));
lp.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
Network myNet = connectKeepaliveNetwork(lp);
- mWiFiNetworkAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
- mWiFiNetworkAgent.setStopKeepaliveEvent(SocketKeepalive.SUCCESS);
+ mWiFiAgent.setStartKeepaliveEvent(SocketKeepalive.SUCCESS);
+ mWiFiAgent.setStopKeepaliveEvent(SocketKeepalive.SUCCESS);
TestSocketKeepaliveCallback callback = new TestSocketKeepaliveCallback(executor);
@@ -6879,9 +6903,9 @@
// TODO: enable this check after ensuring a valid free port. See b/129512753#comment7.
// assertFalse(isUdpPortInUse(srcPort));
- mWiFiNetworkAgent.disconnect();
- mWiFiNetworkAgent.expectDisconnected();
- mWiFiNetworkAgent = null;
+ mWiFiAgent.disconnect();
+ mWiFiAgent.expectDisconnected();
+ mWiFiAgent = null;
}
private static boolean isUdpPortInUse(int port) {
@@ -6919,18 +6943,18 @@
}
private void assertPinnedToWifiWithCellDefault() {
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getBoundNetworkForProcess());
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getBoundNetworkForProcess());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
}
private void assertPinnedToWifiWithWifiDefault() {
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getBoundNetworkForProcess());
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getBoundNetworkForProcess());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
}
private void assertNotPinnedToWifi() {
assertNull(mCm.getBoundNetworkForProcess());
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
}
@Test
@@ -6943,23 +6967,23 @@
TestNetworkPinner.pin(mServiceContext, wifiRequest);
assertNull(mCm.getBoundNetworkForProcess());
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
// When wi-fi connects, expect to be pinned.
assertTrue(TestNetworkPinner.awaitPin(100));
assertPinnedToWifiWithCellDefault();
// Disconnect and expect the pin to drop.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
assertTrue(TestNetworkPinner.awaitUnpin(100));
assertNotPinnedToWifi();
// Reconnecting does not cause the pin to come back.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
assertFalse(TestNetworkPinner.awaitPin(100));
assertNotPinnedToWifi();
@@ -6974,21 +6998,21 @@
// Disconnect cell and wifi.
ExpectedBroadcast b = registerConnectivityBroadcast(3); // cell down, wifi up, wifi down.
- mCellNetworkAgent.disconnect();
- mWiFiNetworkAgent.disconnect();
+ mCellAgent.disconnect();
+ mWiFiAgent.disconnect();
b.expectBroadcast();
// Pinning takes effect even if the pinned network is the default when the pin is set...
TestNetworkPinner.pin(mServiceContext, wifiRequest);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
assertTrue(TestNetworkPinner.awaitPin(100));
assertPinnedToWifiWithWifiDefault();
// ... and is maintained even when that network is no longer the default.
b = registerConnectivityBroadcast(1);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mCellAgent.connect(true);
b.expectBroadcast();
assertPinnedToWifiWithCellDefault();
}
@@ -7192,7 +7216,7 @@
// Disconnect wifi aware network.
wifiAware.disconnect();
- callback.expect(CallbackEntry.LOST, TIMEOUT_MS);
+ callback.expect(LOST, TIMEOUT_MS);
mCm.unregisterNetworkCallback(callback);
verifyNoNetwork();
@@ -7239,12 +7263,11 @@
// ConnectivityService.
TestNetworkAgentWrapper networkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
networkAgent.connect(true);
- networkCallback.expect(CallbackEntry.AVAILABLE, networkAgent);
- networkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED, networkAgent);
+ networkCallback.expect(AVAILABLE, networkAgent);
+ networkCallback.expect(NETWORK_CAPS_UPDATED, networkAgent);
CallbackEntry.LinkPropertiesChanged cbi =
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- networkAgent);
- networkCallback.expect(CallbackEntry.BLOCKED_STATUS, networkAgent);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, networkAgent);
+ networkCallback.expect(BLOCKED_STATUS, networkAgent);
networkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, networkAgent);
networkCallback.assertNoCallback();
checkDirectlyConnectedRoutes(cbi.getLp(), asList(myIpv4Address),
@@ -7259,7 +7282,7 @@
newLp.addLinkAddress(myIpv6Address1);
newLp.addLinkAddress(myIpv6Address2);
networkAgent.sendLinkProperties(newLp);
- cbi = networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, networkAgent);
+ cbi = networkCallback.expect(LINK_PROPERTIES_CHANGED, networkAgent);
networkCallback.assertNoCallback();
checkDirectlyConnectedRoutes(cbi.getLp(),
asList(myIpv4Address, myIpv6Address1, myIpv6Address2),
@@ -7320,15 +7343,15 @@
}
private List<Network> onlyCell() {
- return List.of(mCellNetworkAgent.getNetwork());
+ return List.of(mCellAgent.getNetwork());
}
private List<Network> onlyWifi() {
- return List.of(mWiFiNetworkAgent.getNetwork());
+ return List.of(mWiFiAgent.getNetwork());
}
private List<Network> cellAndWifi() {
- return List.of(mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork());
+ return List.of(mCellAgent.getNetwork(), mWiFiAgent.getNetwork());
}
@Test
@@ -7338,11 +7361,11 @@
LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
// Simple connection with initial LP should have updated ifaces.
- mCellNetworkAgent.connect(false);
+ mCellAgent.connect(false);
waitForIdle();
expectNotifyNetworkStatus(onlyCell(), onlyCell(), MOBILE_IFNAME);
reset(mStatsManager);
@@ -7352,13 +7375,13 @@
cellLp.addRoute(new RouteInfo((IpPrefix) null, InetAddress.getByName("192.0.2.4"),
MOBILE_IFNAME));
cellLp.setDnsServers(List.of(InetAddress.getAllByName("8.8.8.8")));
- mCellNetworkAgent.sendLinkProperties(cellLp);
+ mCellAgent.sendLinkProperties(cellLp);
verifyNoMoreInteractions(mStatsManager);
reset(mStatsManager);
// Default network switch should update ifaces.
- mWiFiNetworkAgent.connect(false);
- mWiFiNetworkAgent.sendLinkProperties(wifiLp);
+ mWiFiAgent.connect(false);
+ mWiFiAgent.sendLinkProperties(wifiLp);
waitForIdle();
assertEquals(wifiLp, mService.getActiveLinkProperties());
expectNotifyNetworkStatus(cellAndWifi(), onlyWifi(), WIFI_IFNAME);
@@ -7370,52 +7393,52 @@
// notifyNetworkStatus is called again, traffic on that interface will be accounted to the
// disconnected network. This is likely a bug in ConnectivityService; it should probably
// call notifyNetworkStatus again without the disconnected network.
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
waitForIdle();
expectNotifyNetworkStatus(cellAndWifi(), onlyWifi(), WIFI_IFNAME);
verifyNoMoreInteractions(mStatsManager);
reset(mStatsManager);
// Connecting a network updates ifaces even if the network doesn't become default.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(false);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(false);
waitForIdle();
expectNotifyNetworkStatus(cellAndWifi(), onlyWifi(), WIFI_IFNAME);
reset(mStatsManager);
// Disconnect should update ifaces.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
waitForIdle();
expectNotifyNetworkStatus(onlyCell(), onlyCell(), MOBILE_IFNAME);
reset(mStatsManager);
// Metered change should update ifaces
- mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
+ mCellAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
waitForIdle();
expectNotifyNetworkStatus(onlyCell(), onlyCell(), MOBILE_IFNAME);
reset(mStatsManager);
- mCellNetworkAgent.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
+ mCellAgent.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
waitForIdle();
expectNotifyNetworkStatus(onlyCell(), onlyCell(), MOBILE_IFNAME);
reset(mStatsManager);
// Temp metered change shouldn't update ifaces
- mCellNetworkAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
+ mCellAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
waitForIdle();
verify(mStatsManager, never()).notifyNetworkStatus(eq(onlyCell()),
any(List.class), eq(MOBILE_IFNAME), any(List.class));
reset(mStatsManager);
// Congested change shouldn't update ifaces
- mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED);
+ mCellAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED);
waitForIdle();
verify(mStatsManager, never()).notifyNetworkStatus(eq(onlyCell()),
any(List.class), eq(MOBILE_IFNAME), any(List.class));
reset(mStatsManager);
// Roaming change should update ifaces
- mCellNetworkAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
+ mCellAgent.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
waitForIdle();
expectNotifyNetworkStatus(onlyCell(), onlyCell(), MOBILE_IFNAME);
reset(mStatsManager);
@@ -7427,25 +7450,22 @@
mMockVpn.establishForMyUid(lp);
assertUidRangesUpdatedForMyUid(true);
- final List<Network> cellAndVpn =
- List.of(mCellNetworkAgent.getNetwork(), mMockVpn.getNetwork());
+ final List<Network> cellAndVpn = List.of(mCellAgent.getNetwork(), mMockVpn.getNetwork());
// A VPN with default (null) underlying networks sets the underlying network's interfaces...
expectNotifyNetworkStatus(cellAndVpn, cellAndVpn, MOBILE_IFNAME, Process.myUid(),
VPN_IFNAME, List.of(MOBILE_IFNAME));
// ...and updates them as the default network switches.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- mWiFiNetworkAgent.sendLinkProperties(wifiLp);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ mWiFiAgent.sendLinkProperties(wifiLp);
final Network[] onlyNull = new Network[]{null};
- final List<Network> wifiAndVpn =
- List.of(mWiFiNetworkAgent.getNetwork(), mMockVpn.getNetwork());
- final List<Network> cellWifiAndVpn =
- List.of(mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork(),
- mMockVpn.getNetwork());
+ final List<Network> wifiAndVpn = List.of(mWiFiAgent.getNetwork(), mMockVpn.getNetwork());
+ final List<Network> cellWifiAndVpn = List.of(mCellAgent.getNetwork(),
+ mWiFiAgent.getNetwork(), mMockVpn.getNetwork());
final Network[] cellNullAndWifi =
- new Network[]{mCellNetworkAgent.getNetwork(), null, mWiFiNetworkAgent.getNetwork()};
+ new Network[] { mCellAgent.getNetwork(), null, mWiFiAgent.getNetwork() };
waitForIdle();
assertEquals(wifiLp, mService.getActiveLinkProperties());
@@ -7485,9 +7505,9 @@
// could result in incorrect data usage measurements if the interface used by the
// disconnected network is reused by a system component that does not register an agent for
// it (e.g., tethering).
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
waitForIdle();
- assertNull(mService.getLinkProperties(mCellNetworkAgent.getNetwork()));
+ assertNull(mService.getLinkProperties(mCellAgent.getNetwork()));
expectNotifyNetworkStatus(cellWifiAndVpn, wifiAndVpn, MOBILE_IFNAME, Process.myUid(),
VPN_IFNAME, List.of(MOBILE_IFNAME, WIFI_IFNAME));
@@ -7503,16 +7523,16 @@
// ... but if something else happens that causes notifyIfacesChangedForNetworkStats to be
// called again, it does. For example, connect Ethernet, but with a low score, such that it
// does not become the default network.
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
- mEthernetNetworkAgent.setScore(
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
+ mEthernetAgent.setScore(
new NetworkScore.Builder().setLegacyInt(30).setExiting(true).build());
- mEthernetNetworkAgent.connect(false);
+ mEthernetAgent.connect(false);
waitForIdle();
verify(mStatsManager).notifyNetworkStatus(any(List.class),
any(List.class), any() /* anyString() doesn't match null */,
argThat(vpnInfos -> vpnInfos.get(0).getUnderlyingInterfaces().size() == 1
&& WIFI_IFNAME.equals(vpnInfos.get(0).getUnderlyingInterfaces().get(0))));
- mEthernetNetworkAgent.disconnect();
+ mEthernetAgent.disconnect();
waitForIdle();
reset(mStatsManager);
@@ -7552,18 +7572,18 @@
final int[] adminUids = new int[] {Process.myUid() + 1};
final NetworkCapabilities ncTemplate = new NetworkCapabilities();
ncTemplate.setAdministratorUids(adminUids);
- mCellNetworkAgent =
+ mCellAgent =
new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(), ncTemplate);
- mCellNetworkAgent.connect(false /* validated */);
+ mCellAgent.connect(false /* validated */);
// Verify case where caller has permission
mServiceContext.setPermission(
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, PERMISSION_GRANTED);
TestNetworkCallback callback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(callback);
- callback.expect(CallbackEntry.AVAILABLE, mCellNetworkAgent);
+ callback.expect(AVAILABLE, mCellAgent);
callback.expectCapabilitiesThat(
- mCellNetworkAgent, nc -> Arrays.equals(adminUids, nc.getAdministratorUids()));
+ mCellAgent, nc -> Arrays.equals(adminUids, nc.getAdministratorUids()));
mCm.unregisterNetworkCallback(callback);
// Verify case where caller does NOT have permission
@@ -7572,9 +7592,8 @@
mServiceContext.setPermission(NETWORK_STACK, PERMISSION_DENIED);
callback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(callback);
- callback.expect(CallbackEntry.AVAILABLE, mCellNetworkAgent);
- callback.expectCapabilitiesThat(
- mCellNetworkAgent, nc -> nc.getAdministratorUids().length == 0);
+ callback.expect(AVAILABLE, mCellAgent);
+ callback.expectCapabilitiesThat(mCellAgent, nc -> nc.getAdministratorUids().length == 0);
}
@Test
@@ -7591,10 +7610,10 @@
// Connect a VCN-managed wifi network.
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.removeCapability(NET_CAPABILITY_NOT_VCN_MANAGED);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true /* validated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.removeCapability(NET_CAPABILITY_NOT_VCN_MANAGED);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true /* validated */);
final List<Network> none = List.of();
expectNotifyNetworkStatus(onlyWifi(), none, null); // Wifi is not the default network
@@ -7609,7 +7628,7 @@
final LinkProperties lp = new LinkProperties();
lp.setInterfaceName(vcnIface);
final TestNetworkAgentWrapper vcn = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, lp, nc);
- vcn.setUnderlyingNetworks(List.of(mWiFiNetworkAgent.getNetwork()));
+ vcn.setUnderlyingNetworks(List.of(mWiFiAgent.getNetwork()));
vcn.connect(false /* validated */);
final TestNetworkCallback callback = new TestNetworkCallback();
@@ -7622,12 +7641,12 @@
assertFalse(nc.hasTransport(TRANSPORT_WIFI));
assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_METERED));
final List<Network> onlyVcn = List.of(vcn.getNetwork());
- final List<Network> vcnAndWifi = List.of(vcn.getNetwork(), mWiFiNetworkAgent.getNetwork());
+ final List<Network> vcnAndWifi = List.of(vcn.getNetwork(), mWiFiAgent.getNetwork());
expectNotifyNetworkStatus(vcnAndWifi, onlyVcn, vcnIface, ownerUid, vcnIface,
List.of(WIFI_IFNAME));
// Add NOT_METERED to the underlying network, check that it is not propagated.
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
callback.assertNoCallback();
nc = mCm.getNetworkCapabilities(vcn.getNetwork());
assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_METERED));
@@ -7635,11 +7654,11 @@
// Switch underlying networks.
final LinkProperties cellLp = new LinkProperties();
cellLp.setInterfaceName(MOBILE_IFNAME);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_VCN_MANAGED);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_ROAMING);
- mCellNetworkAgent.connect(false /* validated */);
- vcn.setUnderlyingNetworks(List.of(mCellNetworkAgent.getNetwork()));
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_VCN_MANAGED);
+ mCellAgent.addCapability(NET_CAPABILITY_NOT_ROAMING);
+ mCellAgent.connect(false /* validated */);
+ vcn.setUnderlyingNetworks(List.of(mCellAgent.getNetwork()));
// The underlying capability changes do not propagate to the virtual network, but
// NetworkStatsService is informed of the new underlying interface.
@@ -7648,7 +7667,7 @@
assertFalse(nc.hasTransport(TRANSPORT_WIFI));
assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_ROAMING));
final List<Network> vcnWifiAndCell = List.of(vcn.getNetwork(),
- mWiFiNetworkAgent.getNetwork(), mCellNetworkAgent.getNetwork());
+ mWiFiAgent.getNetwork(), mCellAgent.getNetwork());
expectNotifyNetworkStatus(vcnWifiAndCell, onlyVcn, vcnIface, ownerUid, vcnIface,
List.of(MOBILE_IFNAME));
}
@@ -7657,7 +7676,7 @@
public void testBasicDnsConfigurationPushed() throws Exception {
setPrivateDnsSettings(PRIVATE_DNS_MODE_OPPORTUNISTIC, "ignored.example.com");
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
waitForIdle();
verify(mMockDnsResolver, never()).setResolverConfiguration(any());
verifyNoMoreInteractions(mMockDnsResolver);
@@ -7673,18 +7692,17 @@
cellLp.addLinkAddress(new LinkAddress("2001:db8:1::1/64"));
cellLp.addRoute(new RouteInfo((IpPrefix) null, InetAddress.getByName("2001:db8:1::1"),
MOBILE_IFNAME));
- mCellNetworkAgent.sendLinkProperties(cellLp);
- mCellNetworkAgent.connect(false);
+ mCellAgent.sendLinkProperties(cellLp);
+ mCellAgent.connect(false);
waitForIdle();
- verify(mMockDnsResolver, times(1)).createNetworkCache(
- eq(mCellNetworkAgent.getNetwork().netId));
+ verify(mMockDnsResolver, times(1)).createNetworkCache(eq(mCellAgent.getNetwork().netId));
// CS tells dnsresolver about the empty DNS config for this network.
verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration(any());
reset(mMockDnsResolver);
cellLp.addDnsServer(InetAddress.getByName("2001:db8::1"));
- mCellNetworkAgent.sendLinkProperties(cellLp);
+ mCellAgent.sendLinkProperties(cellLp);
waitForIdle();
verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration(
mResolverParamsParcelCaptor.capture());
@@ -7696,7 +7714,7 @@
reset(mMockDnsResolver);
cellLp.addDnsServer(InetAddress.getByName("192.0.2.1"));
- mCellNetworkAgent.sendLinkProperties(cellLp);
+ mCellAgent.sendLinkProperties(cellLp);
waitForIdle();
verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration(
mResolverParamsParcelCaptor.capture());
@@ -7714,7 +7732,7 @@
final String TLS_SERVER6 = "2001:db8:53::53";
final InetAddress[] TLS_IPS = new InetAddress[]{ InetAddress.getByName(TLS_SERVER6) };
final String[] TLS_SERVERS = new String[]{ TLS_SERVER6 };
- mCellNetworkAgent.mNmCallbacks.notifyPrivateDnsConfigResolved(
+ mCellAgent.mNmCallbacks.notifyPrivateDnsConfigResolved(
new PrivateDnsConfig(TLS_SPECIFIER, TLS_IPS).toParcel());
waitForIdle();
@@ -7735,11 +7753,10 @@
final TestNetworkCallback callback = new TestNetworkCallback();
mCm.registerNetworkCallback(request, callback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- verify(mMockDnsResolver, times(1)).createNetworkCache(
- eq(mWiFiNetworkAgent.getNetwork().netId));
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ verify(mMockDnsResolver, times(1)).createNetworkCache(eq(mWiFiAgent.getNetwork().netId));
verify(mMockDnsResolver, times(2)).setResolverConfiguration(
mResolverParamsParcelCaptor.capture());
final ResolverParamsParcel resolverParams = mResolverParamsParcelCaptor.getValue();
@@ -7755,31 +7772,31 @@
TestNetworkCallback callback = new TestNetworkCallback();
mCm.registerNetworkCallback(request, callback);
// Bring up wifi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// Private DNS resolution failed, checking if the notification will be shown or not.
- mWiFiNetworkAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
- mWiFiNetworkAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
+ mWiFiAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
+ mWiFiAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
waitForIdle();
// If network validation failed, NetworkMonitor will re-evaluate the network.
// ConnectivityService should filter the redundant notification. This part is trying to
// simulate that situation and check if ConnectivityService could filter that case.
- mWiFiNetworkAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
+ mWiFiAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
waitForIdle();
verify(mNotificationManager, timeout(TIMEOUT_MS).times(1)).notify(anyString(),
eq(NotificationType.PRIVATE_DNS_BROKEN.eventId), any());
// If private DNS resolution successful, the PRIVATE_DNS_BROKEN notification shouldn't be
// shown.
- mWiFiNetworkAgent.setNetworkValid(true /* privateDnsProbeSent */);
- mWiFiNetworkAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
+ mWiFiAgent.setNetworkValid(true /* privateDnsProbeSent */);
+ mWiFiAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
waitForIdle();
verify(mNotificationManager, timeout(TIMEOUT_MS).times(1)).cancel(anyString(),
eq(NotificationType.PRIVATE_DNS_BROKEN.eventId));
// If private DNS resolution failed again, the PRIVATE_DNS_BROKEN notification should be
// shown again.
- mWiFiNetworkAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
- mWiFiNetworkAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
+ mWiFiAgent.setNetworkInvalid(true /* invalidBecauseOfPrivateDns */);
+ mWiFiAgent.mNetworkMonitor.forceReevaluation(Process.myUid());
waitForIdle();
verify(mNotificationManager, timeout(TIMEOUT_MS).times(2)).notify(anyString(),
eq(NotificationType.PRIVATE_DNS_BROKEN.eventId), any());
@@ -7795,7 +7812,7 @@
.addTransportType(TRANSPORT_CELLULAR).build();
mCm.requestNetwork(cellRequest, cellNetworkCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
waitForIdle();
// CS tells netd about the empty DNS config for this network.
verify(mMockDnsResolver, never()).setResolverConfiguration(any());
@@ -7815,11 +7832,10 @@
cellLp.addDnsServer(InetAddress.getByName("2001:db8::1"));
cellLp.addDnsServer(InetAddress.getByName("192.0.2.1"));
- mCellNetworkAgent.sendLinkProperties(cellLp);
- mCellNetworkAgent.connect(false);
+ mCellAgent.sendLinkProperties(cellLp);
+ mCellAgent.connect(false);
waitForIdle();
- verify(mMockDnsResolver, times(1)).createNetworkCache(
- eq(mCellNetworkAgent.getNetwork().netId));
+ verify(mMockDnsResolver, times(1)).createNetworkCache(eq(mCellAgent.getNetwork().netId));
verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration(
mResolverParamsParcelCaptor.capture());
ResolverParamsParcel resolvrParams = mResolverParamsParcelCaptor.getValue();
@@ -7831,12 +7847,11 @@
assertTrue(new ArraySet<>(resolvrParams.tlsServers).containsAll(
asList("2001:db8::1", "192.0.2.1")));
reset(mMockDnsResolver);
- cellNetworkCallback.expect(CallbackEntry.AVAILABLE, mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mCellNetworkAgent);
+ cellNetworkCallback.expect(AVAILABLE, mCellAgent);
+ cellNetworkCallback.expect(NETWORK_CAPS_UPDATED, mCellAgent);
CallbackEntry.LinkPropertiesChanged cbi = cellNetworkCallback.expect(
- CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.BLOCKED_STATUS, mCellNetworkAgent);
+ LINK_PROPERTIES_CHANGED, mCellAgent);
+ cellNetworkCallback.expect(BLOCKED_STATUS, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertFalse(cbi.getLp().isPrivateDnsActive());
assertNull(cbi.getLp().getPrivateDnsServerName());
@@ -7867,8 +7882,7 @@
setPrivateDnsSettings(PRIVATE_DNS_MODE_PROVIDER_HOSTNAME, "strict.example.com");
// Can't test dns configuration for strict mode without properly mocking
// out the DNS lookups, but can test that LinkProperties is updated.
- cbi = cellNetworkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- mCellNetworkAgent);
+ cbi = cellNetworkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertTrue(cbi.getLp().isPrivateDnsActive());
assertEquals("strict.example.com", cbi.getLp().getPrivateDnsServerName());
@@ -7894,18 +7908,17 @@
.addTransportType(TRANSPORT_CELLULAR).build();
mCm.requestNetwork(cellRequest, cellNetworkCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
waitForIdle();
LinkProperties lp = new LinkProperties();
- mCellNetworkAgent.sendLinkProperties(lp);
- mCellNetworkAgent.connect(false);
+ mCellAgent.sendLinkProperties(lp);
+ mCellAgent.connect(false);
waitForIdle();
- cellNetworkCallback.expect(CallbackEntry.AVAILABLE, mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mCellNetworkAgent);
+ cellNetworkCallback.expect(AVAILABLE, mCellAgent);
+ cellNetworkCallback.expect(NETWORK_CAPS_UPDATED, mCellAgent);
CallbackEntry.LinkPropertiesChanged cbi = cellNetworkCallback.expect(
- CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
- cellNetworkCallback.expect(CallbackEntry.BLOCKED_STATUS, mCellNetworkAgent);
+ LINK_PROPERTIES_CHANGED, mCellAgent);
+ cellNetworkCallback.expect(BLOCKED_STATUS, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertFalse(cbi.getLp().isPrivateDnsActive());
assertNull(cbi.getLp().getPrivateDnsServerName());
@@ -7915,16 +7928,15 @@
// Send a validation event for a server that is not part of the current
// resolver config. The validation event should be ignored.
mService.mResolverUnsolEventCallback.onPrivateDnsValidationEvent(
- makePrivateDnsValidationEvent(mCellNetworkAgent.getNetwork().netId, "",
+ makePrivateDnsValidationEvent(mCellAgent.getNetwork().netId, "",
"145.100.185.18", VALIDATION_RESULT_SUCCESS));
cellNetworkCallback.assertNoCallback();
// Add a dns server to the LinkProperties.
LinkProperties lp2 = new LinkProperties(lp);
lp2.addDnsServer(InetAddress.getByName("145.100.185.16"));
- mCellNetworkAgent.sendLinkProperties(lp2);
- cbi = cellNetworkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(lp2);
+ cbi = cellNetworkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertFalse(cbi.getLp().isPrivateDnsActive());
assertNull(cbi.getLp().getPrivateDnsServerName());
@@ -7934,13 +7946,13 @@
// Send a validation event containing a hostname that is not part of
// the current resolver config. The validation event should be ignored.
mService.mResolverUnsolEventCallback.onPrivateDnsValidationEvent(
- makePrivateDnsValidationEvent(mCellNetworkAgent.getNetwork().netId,
+ makePrivateDnsValidationEvent(mCellAgent.getNetwork().netId,
"145.100.185.16", "hostname", VALIDATION_RESULT_SUCCESS));
cellNetworkCallback.assertNoCallback();
// Send a validation event where validation failed.
mService.mResolverUnsolEventCallback.onPrivateDnsValidationEvent(
- makePrivateDnsValidationEvent(mCellNetworkAgent.getNetwork().netId,
+ makePrivateDnsValidationEvent(mCellAgent.getNetwork().netId,
"145.100.185.16", "", VALIDATION_RESULT_FAILURE));
cellNetworkCallback.assertNoCallback();
@@ -7948,10 +7960,9 @@
// the current resolver config. A LinkProperties callback with updated
// private dns fields should be sent.
mService.mResolverUnsolEventCallback.onPrivateDnsValidationEvent(
- makePrivateDnsValidationEvent(mCellNetworkAgent.getNetwork().netId,
+ makePrivateDnsValidationEvent(mCellAgent.getNetwork().netId,
"145.100.185.16", "", VALIDATION_RESULT_SUCCESS));
- cbi = cellNetworkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- mCellNetworkAgent);
+ cbi = cellNetworkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertTrue(cbi.getLp().isPrivateDnsActive());
assertNull(cbi.getLp().getPrivateDnsServerName());
@@ -7961,9 +7972,8 @@
// the network agent sends unrelated changes.
LinkProperties lp3 = new LinkProperties(lp2);
lp3.setMtu(1300);
- mCellNetworkAgent.sendLinkProperties(lp3);
- cbi = cellNetworkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(lp3);
+ cbi = cellNetworkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertTrue(cbi.getLp().isPrivateDnsActive());
assertNull(cbi.getLp().getPrivateDnsServerName());
@@ -7974,9 +7984,8 @@
// fields in LinkProperties.
LinkProperties lp4 = new LinkProperties(lp3);
lp4.removeDnsServer(InetAddress.getByName("145.100.185.16"));
- mCellNetworkAgent.sendLinkProperties(lp4);
- cbi = cellNetworkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(lp4);
+ cbi = cellNetworkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
cellNetworkCallback.assertNoCallback();
assertFalse(cbi.getLp().isPrivateDnsActive());
assertNull(cbi.getLp().getPrivateDnsServerName());
@@ -8011,10 +8020,10 @@
@Test
public void testApplyUnderlyingCapabilities() throws Exception {
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mCellNetworkAgent.connect(false /* validated */);
- mWiFiNetworkAgent.connect(false /* validated */);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mCellAgent.connect(false /* validated */);
+ mWiFiAgent.connect(false /* validated */);
final NetworkCapabilities cellNc = new NetworkCapabilities()
.addTransportType(TRANSPORT_CELLULAR)
@@ -8031,12 +8040,12 @@
.addCapability(NET_CAPABILITY_NOT_SUSPENDED)
.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED)
.setLinkUpstreamBandwidthKbps(20);
- mCellNetworkAgent.setNetworkCapabilities(cellNc, true /* sendToConnectivityService */);
- mWiFiNetworkAgent.setNetworkCapabilities(wifiNc, true /* sendToConnectivityService */);
+ mCellAgent.setNetworkCapabilities(cellNc, true /* sendToConnectivityService */);
+ mWiFiAgent.setNetworkCapabilities(wifiNc, true /* sendToConnectivityService */);
waitForIdle();
- final Network mobile = mCellNetworkAgent.getNetwork();
- final Network wifi = mWiFiNetworkAgent.getNetwork();
+ final Network mobile = mCellAgent.getNetwork();
+ final Network wifi = mWiFiAgent.getNetwork();
final NetworkCapabilities initialCaps = new NetworkCapabilities();
initialCaps.addTransportType(TRANSPORT_VPN);
@@ -8167,8 +8176,7 @@
mMockVpn.setUnderlyingNetworks(new Network[]{wifiNetwork});
// onCapabilitiesChanged() should be called because
// NetworkCapabilities#mUnderlyingNetworks is updated.
- CallbackEntry ce = callback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mMockVpn);
+ CallbackEntry ce = callback.expect(NETWORK_CAPS_UPDATED, mMockVpn);
final NetworkCapabilities vpnNc1 = ((CallbackEntry.CapabilitiesChanged) ce).getCaps();
// Since the wifi network hasn't brought up,
// ConnectivityService#applyUnderlyingCapabilities cannot find it. Update
@@ -8191,9 +8199,9 @@
// Make that underlying network connect, and expect to see its capabilities immediately
// reflected in the VPN's capabilities.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- assertEquals(wifiNetwork, mWiFiNetworkAgent.getNetwork());
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ assertEquals(wifiNetwork, mWiFiAgent.getNetwork());
+ mWiFiAgent.connect(false);
// TODO: the callback for the VPN happens before any callbacks are called for the wifi
// network that has just connected. There appear to be two issues here:
// 1. The VPN code will accept an underlying network as soon as getNetworkCapabilities()
@@ -8204,21 +8212,21 @@
// 2. When a network connects, updateNetworkInfo propagates underlying network
// capabilities before rematching networks.
// Given that this scenario can't really happen, this is probably fine for now.
- ce = callback.expect(CallbackEntry.NETWORK_CAPS_UPDATED, mMockVpn);
+ ce = callback.expect(NETWORK_CAPS_UPDATED, mMockVpn);
final NetworkCapabilities vpnNc2 = ((CallbackEntry.CapabilitiesChanged) ce).getCaps();
// The wifi network is brought up, NetworkCapabilities#mUnderlyingNetworks is updated to
// it.
underlyingNetwork.add(wifiNetwork);
assertEquals(underlyingNetwork, vpnNc2.getUnderlyingNetworks());
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
assertTrue(mCm.getNetworkCapabilities(mMockVpn.getNetwork())
.hasTransport(TRANSPORT_VPN));
assertTrue(mCm.getNetworkCapabilities(mMockVpn.getNetwork())
.hasTransport(TRANSPORT_WIFI));
// Disconnect the network, and expect to see the VPN capabilities change accordingly.
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
callback.expectCapabilitiesThat(mMockVpn, (nc) ->
nc.getTransportTypes().length == 1 && nc.hasTransport(TRANSPORT_VPN));
@@ -8244,8 +8252,8 @@
callback.expectAvailableCallbacksUnvalidated(mMockVpn);
// Connect cellular data.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false /* validated */);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false /* validated */);
callback.expectCapabilitiesThat(mMockVpn,
nc -> nc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED)
&& nc.hasTransport(TRANSPORT_CELLULAR));
@@ -8260,11 +8268,11 @@
assertGetNetworkInfoOfGetActiveNetworkIsConnected(true);
// Suspend the cellular network and expect the VPN to be suspended.
- mCellNetworkAgent.suspend();
+ mCellAgent.suspend();
callback.expectCapabilitiesThat(mMockVpn,
nc -> !nc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED)
&& nc.hasTransport(TRANSPORT_CELLULAR));
- callback.expect(CallbackEntry.SUSPENDED, mMockVpn);
+ callback.expect(SUSPENDED, mMockVpn);
callback.assertNoCallback();
assertFalse(mCm.getNetworkCapabilities(mMockVpn.getNetwork())
@@ -8277,12 +8285,12 @@
assertGetNetworkInfoOfGetActiveNetworkIsConnected(false);
// Switch to another network. The VPN should no longer be suspended.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false /* validated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false /* validated */);
callback.expectCapabilitiesThat(mMockVpn,
nc -> nc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED)
&& nc.hasTransport(TRANSPORT_WIFI));
- callback.expect(CallbackEntry.RESUMED, mMockVpn);
+ callback.expect(RESUMED, mMockVpn);
callback.assertNoCallback();
assertTrue(mCm.getNetworkCapabilities(mMockVpn.getNetwork())
@@ -8294,9 +8302,9 @@
assertGetNetworkInfoOfGetActiveNetworkIsConnected(true);
// Unsuspend cellular and then switch back to it. The VPN remains not suspended.
- mCellNetworkAgent.resume();
+ mCellAgent.resume();
callback.assertNoCallback();
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
callback.expectCapabilitiesThat(mMockVpn,
nc -> nc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED)
&& nc.hasTransport(TRANSPORT_CELLULAR));
@@ -8315,11 +8323,11 @@
assertGetNetworkInfoOfGetActiveNetworkIsConnected(true);
// Suspend cellular and expect no connectivity.
- mCellNetworkAgent.suspend();
+ mCellAgent.suspend();
callback.expectCapabilitiesThat(mMockVpn,
nc -> !nc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED)
&& nc.hasTransport(TRANSPORT_CELLULAR));
- callback.expect(CallbackEntry.SUSPENDED, mMockVpn);
+ callback.expect(SUSPENDED, mMockVpn);
callback.assertNoCallback();
assertFalse(mCm.getNetworkCapabilities(mMockVpn.getNetwork())
@@ -8331,11 +8339,11 @@
assertGetNetworkInfoOfGetActiveNetworkIsConnected(false);
// Resume cellular and expect that connectivity comes back.
- mCellNetworkAgent.resume();
+ mCellAgent.resume();
callback.expectCapabilitiesThat(mMockVpn,
nc -> nc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED)
&& nc.hasTransport(TRANSPORT_CELLULAR));
- callback.expect(CallbackEntry.RESUMED, mMockVpn);
+ callback.expect(RESUMED, mMockVpn);
callback.assertNoCallback();
assertTrue(mCm.getNetworkCapabilities(mMockVpn.getNetwork())
@@ -8374,14 +8382,14 @@
new Handler(ConnectivityThread.getInstanceLooper()));
defaultCallback.assertNoCallback();
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false);
- genericNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- genericNotVpnNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- systemDefaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ genericNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ genericNotVpnNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ systemDefaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
vpnNetworkCallback.assertNoCallback();
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
@@ -8407,16 +8415,15 @@
defaultCallback.expectAvailableThenValidatedCallbacks(mMockVpn);
systemDefaultCallback.assertNoCallback();
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
- assertEquals(mWiFiNetworkAgent.getNetwork(),
- systemDefaultCallback.getLastAvailableNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), systemDefaultCallback.getLastAvailableNetwork());
ranges.clear();
mMockVpn.setUids(ranges);
- genericNetworkCallback.expect(CallbackEntry.LOST, mMockVpn);
+ genericNetworkCallback.expect(LOST, mMockVpn);
genericNotVpnNetworkCallback.assertNoCallback();
wifiNetworkCallback.assertNoCallback();
- vpnNetworkCallback.expect(CallbackEntry.LOST, mMockVpn);
+ vpnNetworkCallback.expect(LOST, mMockVpn);
// TODO : The default network callback should actually get a LOST call here (also see the
// comment below for AVAILABLE). This is because ConnectivityService does not look at UID
@@ -8424,7 +8431,7 @@
// can't currently update their UIDs without disconnecting, so this does not matter too
// much, but that is the reason the test here has to check for an update to the
// capabilities instead of the expected LOST then AVAILABLE.
- defaultCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED, mMockVpn);
+ defaultCallback.expect(NETWORK_CAPS_UPDATED, mMockVpn);
systemDefaultCallback.assertNoCallback();
ranges.add(new UidRange(uid, uid));
@@ -8436,25 +8443,25 @@
vpnNetworkCallback.expectAvailableCallbacksValidated(mMockVpn);
// TODO : Here like above, AVAILABLE would be correct, but because this can't actually
// happen outside of the test, ConnectivityService does not rematch callbacks.
- defaultCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED, mMockVpn);
+ defaultCallback.expect(NETWORK_CAPS_UPDATED, mMockVpn);
systemDefaultCallback.assertNoCallback();
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
- genericNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- genericNotVpnNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- wifiNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ genericNetworkCallback.expect(LOST, mWiFiAgent);
+ genericNotVpnNetworkCallback.expect(LOST, mWiFiAgent);
+ wifiNetworkCallback.expect(LOST, mWiFiAgent);
vpnNetworkCallback.assertNoCallback();
defaultCallback.assertNoCallback();
- systemDefaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ systemDefaultCallback.expect(LOST, mWiFiAgent);
mMockVpn.disconnect();
- genericNetworkCallback.expect(CallbackEntry.LOST, mMockVpn);
+ genericNetworkCallback.expect(LOST, mMockVpn);
genericNotVpnNetworkCallback.assertNoCallback();
wifiNetworkCallback.assertNoCallback();
- vpnNetworkCallback.expect(CallbackEntry.LOST, mMockVpn);
- defaultCallback.expect(CallbackEntry.LOST, mMockVpn);
+ vpnNetworkCallback.expect(LOST, mMockVpn);
+ defaultCallback.expect(LOST, mMockVpn);
systemDefaultCallback.assertNoCallback();
assertEquals(null, mCm.getActiveNetwork());
@@ -8472,10 +8479,10 @@
final TestNetworkCallback defaultCallback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(defaultCallback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
- defaultCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
mMockVpn.establishForMyUid(true /* validated */, false /* hasInternet */,
@@ -8498,10 +8505,10 @@
final TestNetworkCallback defaultCallback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(defaultCallback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
- defaultCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
mMockVpn.establishForMyUid(true /* validated */, true /* hasInternet */,
@@ -8512,8 +8519,8 @@
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
mMockVpn.disconnect();
- defaultCallback.expect(CallbackEntry.LOST, mMockVpn);
- defaultCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
+ defaultCallback.expect(LOST, mMockVpn);
+ defaultCallback.expectAvailableCallbacksValidated(mWiFiAgent);
mCm.unregisterNetworkCallback(defaultCallback);
}
@@ -8524,9 +8531,9 @@
mCm.registerDefaultNetworkCallback(callback);
// Bring up Ethernet.
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
- mEthernetNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mEthernetNetworkAgent);
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
+ mEthernetAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mEthernetAgent);
callback.assertNoCallback();
// Bring up a VPN that has the INTERNET capability, initially unvalidated.
@@ -8561,8 +8568,8 @@
callback.assertNoCallback();
mMockVpn.disconnect();
- callback.expect(CallbackEntry.LOST, mMockVpn);
- callback.expectAvailableCallbacksValidated(mEthernetNetworkAgent);
+ callback.expect(LOST, mMockVpn);
+ callback.expectAvailableCallbacksValidated(mEthernetAgent);
}
@Test
@@ -8579,9 +8586,9 @@
// Connect cell. It will become the default network, and in the absence of setting
// underlying networks explicitly it will become the sole underlying network for the vpn.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
+ mCellAgent.connect(true);
mMockVpn.establishForMyUid(true /* validated */, false /* hasInternet */,
false /* privateDnsProbeSent */);
@@ -8646,38 +8653,36 @@
assertDefaultNetworkCapabilities(userId /* no networks */);
// Connect cell and use it as an underlying network.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
+ mCellAgent.connect(true);
- mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork() });
+ mMockVpn.setUnderlyingNetworks(new Network[] { mCellAgent.getNetwork() });
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_CELLULAR) && !caps.hasTransport(TRANSPORT_WIFI)
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- assertDefaultNetworkCapabilities(userId, mCellNetworkAgent);
+ assertDefaultNetworkCapabilities(userId, mCellAgent);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
+ mWiFiAgent.connect(true);
mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork() });
+ new Network[] { mCellAgent.getNetwork(), mWiFiAgent.getNetwork() });
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_CELLULAR) && caps.hasTransport(TRANSPORT_WIFI)
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- assertDefaultNetworkCapabilities(userId, mCellNetworkAgent, mWiFiNetworkAgent);
+ assertDefaultNetworkCapabilities(userId, mCellAgent, mWiFiAgent);
// Don't disconnect, but note the VPN is not using wifi any more.
- mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork() });
+ mMockVpn.setUnderlyingNetworks(new Network[] { mCellAgent.getNetwork() });
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
@@ -8686,86 +8691,84 @@
&& caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
// The return value of getDefaultNetworkCapabilitiesForUser always includes the default
// network (wifi) as well as the underlying networks (cell).
- assertDefaultNetworkCapabilities(userId, mCellNetworkAgent, mWiFiNetworkAgent);
+ assertDefaultNetworkCapabilities(userId, mCellAgent, mWiFiAgent);
// Remove NOT_SUSPENDED from the only network and observe VPN is now suspended.
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_SUSPENDED);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_SUSPENDED);
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_CELLULAR) && !caps.hasTransport(TRANSPORT_WIFI)
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& !caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- vpnNetworkCallback.expect(CallbackEntry.SUSPENDED, mMockVpn);
+ vpnNetworkCallback.expect(SUSPENDED, mMockVpn);
// Add NOT_SUSPENDED again and observe VPN is no longer suspended.
- mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
+ mCellAgent.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_CELLULAR) && !caps.hasTransport(TRANSPORT_WIFI)
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- vpnNetworkCallback.expect(CallbackEntry.RESUMED, mMockVpn);
+ vpnNetworkCallback.expect(RESUMED, mMockVpn);
// Use Wifi but not cell. Note the VPN is now unmetered and not suspended.
- mMockVpn.setUnderlyingNetworks(
- new Network[] { mWiFiNetworkAgent.getNetwork() });
+ mMockVpn.setUnderlyingNetworks(new Network[] { mWiFiAgent.getNetwork() });
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& !caps.hasTransport(TRANSPORT_CELLULAR) && caps.hasTransport(TRANSPORT_WIFI)
&& caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- assertDefaultNetworkCapabilities(userId, mWiFiNetworkAgent);
+ assertDefaultNetworkCapabilities(userId, mWiFiAgent);
// Use both again.
mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork() });
+ new Network[] { mCellAgent.getNetwork(), mWiFiAgent.getNetwork() });
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_CELLULAR) && caps.hasTransport(TRANSPORT_WIFI)
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- assertDefaultNetworkCapabilities(userId, mCellNetworkAgent, mWiFiNetworkAgent);
+ assertDefaultNetworkCapabilities(userId, mCellAgent, mWiFiAgent);
// Cell is suspended again. As WiFi is not, this should not cause a callback.
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_SUSPENDED);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_SUSPENDED);
vpnNetworkCallback.assertNoCallback();
// Stop using WiFi. The VPN is suspended again.
- mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork() });
+ mMockVpn.setUnderlyingNetworks(new Network[] { mCellAgent.getNetwork() });
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_CELLULAR)
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& !caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- vpnNetworkCallback.expect(CallbackEntry.SUSPENDED, mMockVpn);
- assertDefaultNetworkCapabilities(userId, mCellNetworkAgent, mWiFiNetworkAgent);
+ vpnNetworkCallback.expect(SUSPENDED, mMockVpn);
+ assertDefaultNetworkCapabilities(userId, mCellAgent, mWiFiAgent);
// Use both again.
mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork() });
+ new Network[] { mCellAgent.getNetwork(), mWiFiAgent.getNetwork() });
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_CELLULAR) && caps.hasTransport(TRANSPORT_WIFI)
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED)
&& caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
- vpnNetworkCallback.expect(CallbackEntry.RESUMED, mMockVpn);
- assertDefaultNetworkCapabilities(userId, mCellNetworkAgent, mWiFiNetworkAgent);
+ vpnNetworkCallback.expect(RESUMED, mMockVpn);
+ assertDefaultNetworkCapabilities(userId, mCellAgent, mWiFiAgent);
// Disconnect cell. Receive update without even removing the dead network from the
// underlying networks – it's dead anyway. Not metered any more.
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& !caps.hasTransport(TRANSPORT_CELLULAR) && caps.hasTransport(TRANSPORT_WIFI)
&& caps.hasCapability(NET_CAPABILITY_NOT_METERED));
- assertDefaultNetworkCapabilities(userId, mWiFiNetworkAgent);
+ assertDefaultNetworkCapabilities(userId, mWiFiAgent);
// Disconnect wifi too. No underlying networks means this is now metered.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
&& !caps.hasTransport(TRANSPORT_CELLULAR) && !caps.hasTransport(TRANSPORT_WIFI)
@@ -8807,8 +8810,8 @@
assertVpnTransportInfo(nc, VpnManager.TYPE_VPN_SERVICE);
// Connect to Cell; Cell is the default network.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
@@ -8816,9 +8819,9 @@
&& !caps.hasCapability(NET_CAPABILITY_NOT_METERED));
// Connect to WiFi; WiFi is the new default.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true);
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
@@ -8827,10 +8830,10 @@
// Disconnect Cell. The default network did not change, so there shouldn't be any changes in
// the capabilities.
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
// Disconnect wifi too. Now we have no default network.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
vpnNetworkCallback.expectCapabilitiesThat(mMockVpn,
(caps) -> caps.hasTransport(TRANSPORT_VPN)
@@ -8864,13 +8867,13 @@
assertVpnTransportInfo(nc, VpnManager.TYPE_VPN_SERVICE);
// Set an underlying network and expect to see the VPN transports change.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
callback.expectCapabilitiesThat(mMockVpn, (caps)
-> caps.hasTransport(TRANSPORT_VPN)
&& caps.hasTransport(TRANSPORT_WIFI));
- callback.expectCapabilitiesThat(mWiFiNetworkAgent, (caps)
+ callback.expectCapabilitiesThat(mWiFiAgent, (caps)
-> caps.hasCapability(NET_CAPABILITY_VALIDATED));
doReturn(UserHandle.getUid(RESTRICTED_USER, VPN_UID)).when(mPackageManager)
@@ -8892,8 +8895,8 @@
&& caps.hasTransport(TRANSPORT_WIFI));
// Change the VPN's capabilities somehow (specifically, disconnect wifi).
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
callback.expectCapabilitiesThat(mMockVpn, (caps)
-> caps.getUids().size() == 2
&& caps.getUids().contains(singleUidRange)
@@ -8933,8 +8936,8 @@
final int uid = Process.myUid();
// Connect wifi and check that UIDs in the main and restricted profiles have network access.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true /* validated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true /* validated */);
final int restrictedUid = UserHandle.getUid(RESTRICTED_USER, 42 /* appId */);
assertNotNull(mCm.getActiveNetworkForUid(uid));
assertNotNull(mCm.getActiveNetworkForUid(restrictedUid));
@@ -8983,9 +8986,9 @@
public void testIsActiveNetworkMeteredOverWifi() throws Exception {
// Returns true by default when no network is available.
assertTrue(mCm.isActiveNetworkMetered());
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true);
waitForIdle();
assertFalse(mCm.isActiveNetworkMetered());
@@ -8995,9 +8998,9 @@
public void testIsActiveNetworkMeteredOverCell() throws Exception {
// Returns true by default when no network is available.
assertTrue(mCm.isActiveNetworkMetered());
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
+ mCellAgent.connect(true);
waitForIdle();
assertTrue(mCm.isActiveNetworkMetered());
@@ -9007,9 +9010,9 @@
public void testIsActiveNetworkMeteredOverVpnTrackingPlatformDefault() throws Exception {
// Returns true by default when no network is available.
assertTrue(mCm.isActiveNetworkMetered());
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
+ mCellAgent.connect(true);
waitForIdle();
assertTrue(mCm.isActiveNetworkMetered());
@@ -9024,9 +9027,9 @@
assertTrue(mCm.isActiveNetworkMetered());
// Connect WiFi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true);
waitForIdle();
// VPN should still be the active network.
assertEquals(mMockVpn.getNetwork(), mCm.getActiveNetwork());
@@ -9035,13 +9038,13 @@
assertFalse(mCm.isActiveNetworkMetered());
// Disconnecting Cell should not affect VPN's meteredness.
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
waitForIdle();
assertFalse(mCm.isActiveNetworkMetered());
// Disconnect WiFi; Now there is no platform default network.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
waitForIdle();
// VPN without any underlying networks is treated as metered.
@@ -9054,15 +9057,15 @@
public void testIsActiveNetworkMeteredOverVpnSpecifyingUnderlyingNetworks() throws Exception {
// Returns true by default when no network is available.
assertTrue(mCm.isActiveNetworkMetered());
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
+ mCellAgent.connect(true);
waitForIdle();
assertTrue(mCm.isActiveNetworkMetered());
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true);
waitForIdle();
assertFalse(mCm.isActiveNetworkMetered());
@@ -9073,16 +9076,14 @@
// Ensure VPN is now the active network.
assertEquals(mMockVpn.getNetwork(), mCm.getActiveNetwork());
// VPN is using Cell
- mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork() });
+ mMockVpn.setUnderlyingNetworks(new Network[] { mCellAgent.getNetwork() });
waitForIdle();
// Expect VPN to be metered.
assertTrue(mCm.isActiveNetworkMetered());
// VPN is now using WiFi
- mMockVpn.setUnderlyingNetworks(
- new Network[] { mWiFiNetworkAgent.getNetwork() });
+ mMockVpn.setUnderlyingNetworks(new Network[] { mWiFiAgent.getNetwork() });
waitForIdle();
// Expect VPN to be unmetered
@@ -9090,7 +9091,7 @@
// VPN is using Cell | WiFi.
mMockVpn.setUnderlyingNetworks(
- new Network[] { mCellNetworkAgent.getNetwork(), mWiFiNetworkAgent.getNetwork() });
+ new Network[] { mCellAgent.getNetwork(), mWiFiAgent.getNetwork() });
waitForIdle();
// Expect VPN to be metered.
@@ -9098,7 +9099,7 @@
// VPN is using WiFi | Cell.
mMockVpn.setUnderlyingNetworks(
- new Network[] { mWiFiNetworkAgent.getNetwork(), mCellNetworkAgent.getNetwork() });
+ new Network[] { mWiFiAgent.getNetwork(), mCellAgent.getNetwork() });
waitForIdle();
// Order should not matter and VPN should still be metered.
@@ -9118,9 +9119,9 @@
public void testIsActiveNetworkMeteredOverAlwaysMeteredVpn() throws Exception {
// Returns true by default when no network is available.
assertTrue(mCm.isActiveNetworkMetered());
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true);
waitForIdle();
assertFalse(mCm.isActiveNetworkMetered());
@@ -9140,8 +9141,7 @@
// VPN explicitly declares WiFi as its underlying network.
- mMockVpn.setUnderlyingNetworks(
- new Network[] { mWiFiNetworkAgent.getNetwork() });
+ mMockVpn.setUnderlyingNetworks(new Network[] { mWiFiAgent.getNetwork() });
waitForIdle();
// Doesn't really matter whether VPN declares its underlying networks explicitly.
@@ -9149,7 +9149,7 @@
// With WiFi lost, VPN is basically without any underlying networks. And in that case it is
// anyways suppose to be metered.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
waitForIdle();
assertTrue(mCm.isActiveNetworkMetered());
@@ -9183,80 +9183,74 @@
mockUidNetworkingBlocked();
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- detailedCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent,
- BLOCKED_REASON_NONE);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ detailedCallback.expectAvailableThenValidatedCallbacks(mCellAgent, BLOCKED_REASON_NONE);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mCellNetworkAgent);
+ assertExtraInfoFromCmPresent(mCellAgent);
setBlockedReasonChanged(BLOCKED_REASON_BATTERY_SAVER);
- cellNetworkCallback.expectBlockedStatusCallback(true, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent,
- BLOCKED_REASON_BATTERY_SAVER);
+ cellNetworkCallback.expectBlockedStatusCallback(true, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_REASON_BATTERY_SAVER);
assertNull(mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
- assertExtraInfoFromCmBlocked(mCellNetworkAgent);
+ assertExtraInfoFromCmBlocked(mCellAgent);
// If blocked state does not change but blocked reason does, the boolean callback is called.
// TODO: investigate de-duplicating.
setBlockedReasonChanged(BLOCKED_METERED_REASON_USER_RESTRICTED);
- cellNetworkCallback.expectBlockedStatusCallback(true, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent,
+ cellNetworkCallback.expectBlockedStatusCallback(true, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent,
BLOCKED_METERED_REASON_USER_RESTRICTED);
setBlockedReasonChanged(BLOCKED_REASON_NONE);
- cellNetworkCallback.expectBlockedStatusCallback(false, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent, BLOCKED_REASON_NONE);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ cellNetworkCallback.expectBlockedStatusCallback(false, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_REASON_NONE);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mCellNetworkAgent);
+ assertExtraInfoFromCmPresent(mCellAgent);
setBlockedReasonChanged(BLOCKED_METERED_REASON_DATA_SAVER);
- cellNetworkCallback.expectBlockedStatusCallback(true, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent,
- BLOCKED_METERED_REASON_DATA_SAVER);
+ cellNetworkCallback.expectBlockedStatusCallback(true, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_METERED_REASON_DATA_SAVER);
assertNull(mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
- assertExtraInfoFromCmBlocked(mCellNetworkAgent);
+ assertExtraInfoFromCmBlocked(mCellAgent);
// Restrict the network based on UID rule and NOT_METERED capability change.
- mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- cellNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_METERED, mCellNetworkAgent);
- cellNetworkCallback.expectBlockedStatusCallback(false, mCellNetworkAgent);
- detailedCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_METERED, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent, BLOCKED_REASON_NONE);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ mCellAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ cellNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_METERED, mCellAgent);
+ cellNetworkCallback.expectBlockedStatusCallback(false, mCellAgent);
+ detailedCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_METERED, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_REASON_NONE);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mCellNetworkAgent);
+ assertExtraInfoFromCmPresent(mCellAgent);
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
- cellNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_METERED,
- mCellNetworkAgent);
- cellNetworkCallback.expectBlockedStatusCallback(true, mCellNetworkAgent);
- detailedCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_METERED,
- mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent,
- BLOCKED_METERED_REASON_DATA_SAVER);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
+ cellNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_METERED, mCellAgent);
+ cellNetworkCallback.expectBlockedStatusCallback(true, mCellAgent);
+ detailedCallback.expectCapabilitiesWithout(NET_CAPABILITY_NOT_METERED, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_METERED_REASON_DATA_SAVER);
assertNull(mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
- assertExtraInfoFromCmBlocked(mCellNetworkAgent);
+ assertExtraInfoFromCmBlocked(mCellAgent);
setBlockedReasonChanged(BLOCKED_REASON_NONE);
- cellNetworkCallback.expectBlockedStatusCallback(false, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent, BLOCKED_REASON_NONE);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ cellNetworkCallback.expectBlockedStatusCallback(false, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_REASON_NONE);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mCellNetworkAgent);
+ assertExtraInfoFromCmPresent(mCellAgent);
setBlockedReasonChanged(BLOCKED_REASON_NONE);
cellNetworkCallback.assertNoCallback();
@@ -9264,30 +9258,29 @@
// Restrict background data. Networking is not blocked because the network is unmetered.
setBlockedReasonChanged(BLOCKED_METERED_REASON_DATA_SAVER);
- cellNetworkCallback.expectBlockedStatusCallback(true, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent,
- BLOCKED_METERED_REASON_DATA_SAVER);
+ cellNetworkCallback.expectBlockedStatusCallback(true, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_METERED_REASON_DATA_SAVER);
assertNull(mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
- assertExtraInfoFromCmBlocked(mCellNetworkAgent);
+ assertExtraInfoFromCmBlocked(mCellAgent);
setBlockedReasonChanged(BLOCKED_METERED_REASON_DATA_SAVER);
cellNetworkCallback.assertNoCallback();
setBlockedReasonChanged(BLOCKED_REASON_NONE);
- cellNetworkCallback.expectBlockedStatusCallback(false, mCellNetworkAgent);
- detailedCallback.expectBlockedStatusCallback(mCellNetworkAgent, BLOCKED_REASON_NONE);
+ cellNetworkCallback.expectBlockedStatusCallback(false, mCellAgent);
+ detailedCallback.expectBlockedStatusCallback(mCellAgent, BLOCKED_REASON_NONE);
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mCellNetworkAgent);
+ assertExtraInfoFromCmPresent(mCellAgent);
setBlockedReasonChanged(BLOCKED_REASON_NONE);
cellNetworkCallback.assertNoCallback();
detailedCallback.assertNoCallback();
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mCellNetworkAgent);
+ assertExtraInfoFromCmPresent(mCellAgent);
mCm.unregisterNetworkCallback(cellNetworkCallback);
}
@@ -9304,34 +9297,34 @@
setBlockedReasonChanged(BLOCKED_METERED_REASON_DATA_SAVER);
defaultCallback.assertNoCallback();
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellNetworkAgent);
- defaultCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellAgent);
+ defaultCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mCellAgent);
// Allow to use the network after switching to NOT_METERED network.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true);
- defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true);
+ defaultCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
// Switch to METERED network. Restrict the use of the network.
- mWiFiNetworkAgent.disconnect();
- defaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksValidatedAndBlocked(mCellNetworkAgent);
+ mWiFiAgent.disconnect();
+ defaultCallback.expect(LOST, mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksValidatedAndBlocked(mCellAgent);
// Network becomes NOT_METERED.
- mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- defaultCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_METERED, mCellNetworkAgent);
- defaultCallback.expectBlockedStatusCallback(false, mCellNetworkAgent);
+ mCellAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ defaultCallback.expectCapabilitiesWith(NET_CAPABILITY_NOT_METERED, mCellAgent);
+ defaultCallback.expectBlockedStatusCallback(false, mCellAgent);
// Verify there's no Networkcallbacks invoked after data saver on/off.
setBlockedReasonChanged(BLOCKED_METERED_REASON_DATA_SAVER);
setBlockedReasonChanged(BLOCKED_REASON_NONE);
defaultCallback.assertNoCallback();
- mCellNetworkAgent.disconnect();
- defaultCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ defaultCallback.expect(LOST, mCellAgent);
defaultCallback.assertNoCallback();
mCm.unregisterNetworkCallback(defaultCallback);
@@ -9395,7 +9388,7 @@
// Expect exactly one blocked callback for each agent.
for (int i = 0; i < agents.length; i++) {
- final CallbackEntry e = callback.expect(CallbackEntry.BLOCKED_STATUS, TIMEOUT_MS,
+ final CallbackEntry e = callback.expect(BLOCKED_STATUS, TIMEOUT_MS,
c -> c.getBlocked() == blocked);
final Network network = e.getNetwork();
assertTrue("Received unexpected blocked callback for network " + network,
@@ -9448,14 +9441,14 @@
expectNetworkRejectNonSecureVpn(inOrder, true, uidRangeParcels);
// Connect a network when lockdown is active, expect to see it blocked.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(false /* validated */);
- callback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiNetworkAgent);
- vpnUidCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- vpnUidDefaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- vpnDefaultCallbackAsUid.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(false /* validated */);
+ callback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiAgent);
+ vpnUidCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ vpnUidDefaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ vpnDefaultCallbackAsUid.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
assertNull(mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.BLOCKED);
// Mobile is BLOCKED even though it's not actually connected.
@@ -9464,14 +9457,14 @@
// Disable lockdown, expect to see the network unblocked.
mMockVpn.setAlwaysOnPackage(null, false /* lockdown */, allowList);
- callback.expectBlockedStatusCallback(false, mWiFiNetworkAgent);
- defaultCallback.expectBlockedStatusCallback(false, mWiFiNetworkAgent);
+ callback.expectBlockedStatusCallback(false, mWiFiAgent);
+ defaultCallback.expectBlockedStatusCallback(false, mWiFiAgent);
vpnUidCallback.assertNoCallback();
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
expectNetworkRejectNonSecureVpn(inOrder, false, uidRangeParcels);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
@@ -9493,22 +9486,22 @@
final UidRangeParcel[] uidRangeParcelsAlsoExcludingUs = uidRangeParcelsExcludingUids(
excludedUids.toArray(new Integer[0]));
expectNetworkRejectNonSecureVpn(inOrder, true, uidRangeParcelsAlsoExcludingUs);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
// Connect a new network, expect it to be unblocked.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(false /* validated */);
- callback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(false /* validated */);
+ callback.expectAvailableCallbacksUnvalidated(mCellAgent);
defaultCallback.assertNoCallback();
- vpnUidCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ vpnUidCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
// Cellular is DISCONNECTED because it's not the default and there are no requests for it.
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
@@ -9523,12 +9516,12 @@
mMockVpn.setAlwaysOnPackage(ALWAYS_ON_PACKAGE, true /* lockdown */, allowList);
waitForIdle();
expectNetworkRejectNonSecureVpn(inOrder, true, uidRangeParcels);
- defaultCallback.expectBlockedStatusCallback(true, mWiFiNetworkAgent);
- assertBlockedCallbackInAnyOrder(callback, true, mWiFiNetworkAgent, mCellNetworkAgent);
+ defaultCallback.expectBlockedStatusCallback(true, mWiFiAgent);
+ assertBlockedCallbackInAnyOrder(callback, true, mWiFiAgent, mCellAgent);
vpnUidCallback.assertNoCallback();
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
assertNull(mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
@@ -9536,13 +9529,13 @@
// Disable lockdown. Everything is unblocked.
mMockVpn.setAlwaysOnPackage(null, false /* lockdown */, allowList);
- defaultCallback.expectBlockedStatusCallback(false, mWiFiNetworkAgent);
- assertBlockedCallbackInAnyOrder(callback, false, mWiFiNetworkAgent, mCellNetworkAgent);
+ defaultCallback.expectBlockedStatusCallback(false, mWiFiAgent);
+ assertBlockedCallbackInAnyOrder(callback, false, mWiFiAgent, mCellAgent);
vpnUidCallback.assertNoCallback();
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
@@ -9556,8 +9549,8 @@
vpnUidCallback.assertNoCallback();
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
@@ -9569,20 +9562,20 @@
vpnUidCallback.assertNoCallback();
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
// Enable lockdown and connect a VPN. The VPN is not blocked.
mMockVpn.setAlwaysOnPackage(ALWAYS_ON_PACKAGE, true /* lockdown */, allowList);
- defaultCallback.expectBlockedStatusCallback(true, mWiFiNetworkAgent);
- assertBlockedCallbackInAnyOrder(callback, true, mWiFiNetworkAgent, mCellNetworkAgent);
+ defaultCallback.expectBlockedStatusCallback(true, mWiFiAgent);
+ assertBlockedCallbackInAnyOrder(callback, true, mWiFiAgent, mCellAgent);
vpnUidCallback.assertNoCallback();
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
assertNull(mCm.getActiveNetwork());
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
@@ -9595,15 +9588,15 @@
vpnUidDefaultCallback.assertNoCallback(); // VPN does not apply to VPN_UID
vpnDefaultCallbackAsUid.assertNoCallback();
assertEquals(mMockVpn.getNetwork(), mCm.getActiveNetwork());
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(VPN_UID));
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_VPN, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
mMockVpn.disconnect();
- defaultCallback.expect(CallbackEntry.LOST, mMockVpn);
- defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiNetworkAgent);
+ defaultCallback.expect(LOST, mMockVpn);
+ defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiAgent);
vpnUidCallback.assertNoCallback();
vpnUidDefaultCallback.assertNoCallback();
vpnDefaultCallbackAsUid.assertNoCallback();
@@ -9622,8 +9615,8 @@
mServiceContext.setPermission(NETWORK_SETTINGS, PERMISSION_GRANTED);
// Connect Wi-Fi.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true /* validated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true /* validated */);
// Connect a VPN that excludes its UID from its UID ranges.
final LinkProperties lp = new LinkProperties();
@@ -9632,7 +9625,7 @@
final Set<UidRange> ranges = new ArraySet<>();
ranges.add(new UidRange(0, myUid - 1));
ranges.add(new UidRange(myUid + 1, UserHandle.PER_USER_RANGE - 1));
- mMockVpn.setUnderlyingNetworks(new Network[]{mWiFiNetworkAgent.getNetwork()});
+ mMockVpn.setUnderlyingNetworks(new Network[] { mWiFiAgent.getNetwork() });
mMockVpn.establish(lp, myUid, ranges);
// Wait for validation before registering callbacks.
@@ -9654,8 +9647,8 @@
perUidCb.expectAvailableCallbacksValidated(mMockVpn);
// getActiveNetwork is not affected by this bug.
assertEquals(mMockVpn.getNetwork(), mCm.getActiveNetworkForUid(myUid + 1));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(myUid));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(myUid));
doAsUid(otherUid, () -> mCm.unregisterNetworkCallback(otherUidCb));
mCm.unregisterNetworkCallback(defaultCb);
@@ -9738,11 +9731,11 @@
cellLp.setInterfaceName("rmnet0");
cellLp.addLinkAddress(new LinkAddress("2001:db8::1/64"));
cellLp.addRoute(new RouteInfo(new IpPrefix("::/0"), null, "rmnet0"));
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(false /* validated */);
- callback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellNetworkAgent);
- systemDefaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(false /* validated */);
+ callback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellAgent);
+ systemDefaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellAgent);
waitForIdle();
assertNull(mMockVpn.getAgent());
@@ -9751,37 +9744,36 @@
// TODO: consider fixing this.
cellLp.addLinkAddress(new LinkAddress("192.0.2.2/25"));
cellLp.addRoute(new RouteInfo(new IpPrefix("0.0.0.0/0"), null, "rmnet0"));
- mCellNetworkAgent.sendLinkProperties(cellLp);
- callback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
- defaultCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
- systemDefaultCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED,
- mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(cellLp);
+ callback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
+ defaultCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
+ systemDefaultCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
waitForIdle();
assertNull(mMockVpn.getAgent());
// Disconnect, then try again with a network that supports IPv4 at connection time.
// Expect lockdown VPN to come up.
ExpectedBroadcast b1 = expectConnectivityAction(TYPE_MOBILE, DetailedState.DISCONNECTED);
- mCellNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- defaultCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- systemDefaultCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ callback.expect(LOST, mCellAgent);
+ defaultCallback.expect(LOST, mCellAgent);
+ systemDefaultCallback.expect(LOST, mCellAgent);
b1.expectBroadcast();
// When lockdown VPN is active, the NetworkInfo state in CONNECTIVITY_ACTION is overwritten
// with the state of the VPN network. So expect a CONNECTING broadcast.
b1 = expectConnectivityAction(TYPE_MOBILE, DetailedState.CONNECTING);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(false /* validated */);
- callback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellNetworkAgent);
- defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellNetworkAgent);
- systemDefaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(false /* validated */);
+ callback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellAgent);
+ defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellAgent);
+ systemDefaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mCellAgent);
b1.expectBroadcast();
assertActiveNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_WIFI, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_VPN, DetailedState.BLOCKED);
- assertExtraInfoFromCmBlocked(mCellNetworkAgent);
+ assertExtraInfoFromCmBlocked(mCellAgent);
// TODO: it would be nice if we could simply rely on the production code here, and have
// LockdownVpnTracker start the VPN, have the VPN code register its NetworkAgent with
@@ -9799,7 +9791,7 @@
mMockVpn.expectStartLegacyVpnRunner();
b1 = expectConnectivityAction(TYPE_VPN, DetailedState.CONNECTED);
ExpectedBroadcast b2 = expectConnectivityAction(TYPE_MOBILE, DetailedState.CONNECTED);
- establishLegacyLockdownVpn(mCellNetworkAgent.getNetwork());
+ establishLegacyLockdownVpn(mCellAgent.getNetwork());
callback.expectAvailableThenValidatedCallbacks(mMockVpn);
defaultCallback.expectAvailableThenValidatedCallbacks(mMockVpn);
systemDefaultCallback.assertNoCallback();
@@ -9810,7 +9802,7 @@
assertNetworkInfo(TYPE_MOBILE, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_VPN, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mCellNetworkAgent);
+ assertExtraInfoFromCmPresent(mCellAgent);
assertTrue(vpnNc.hasTransport(TRANSPORT_VPN));
assertTrue(vpnNc.hasTransport(TRANSPORT_CELLULAR));
assertFalse(vpnNc.hasTransport(TRANSPORT_WIFI));
@@ -9825,13 +9817,13 @@
final NetworkCapabilities wifiNc = new NetworkCapabilities();
wifiNc.addTransportType(TRANSPORT_WIFI);
wifiNc.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp, wifiNc);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp, wifiNc);
b1 = expectConnectivityAction(TYPE_MOBILE, DetailedState.DISCONNECTED);
// Wifi is CONNECTING because the VPN isn't up yet.
b2 = expectConnectivityAction(TYPE_WIFI, DetailedState.CONNECTING);
ExpectedBroadcast b3 = expectConnectivityAction(TYPE_VPN, DetailedState.DISCONNECTED);
- mWiFiNetworkAgent.connect(false /* validated */);
+ mWiFiAgent.connect(false /* validated */);
b1.expectBroadcast();
b2.expectBroadcast();
b3.expectBroadcast();
@@ -9842,23 +9834,23 @@
// connected, so the network is not considered blocked by the lockdown UID ranges? But the
// fact that a VPN is connected should only result in the VPN itself being unblocked, not
// any other network. Bug in isUidBlockedByVpn?
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- callback.expect(CallbackEntry.LOST, mMockVpn);
- defaultCallback.expect(CallbackEntry.LOST, mMockVpn);
- defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiNetworkAgent);
- systemDefaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ callback.expect(LOST, mMockVpn);
+ defaultCallback.expect(LOST, mMockVpn);
+ defaultCallback.expectAvailableCallbacksUnvalidatedAndBlocked(mWiFiAgent);
+ systemDefaultCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// While the VPN is reconnecting on the new network, everything is blocked.
assertActiveNetworkInfo(TYPE_WIFI, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_MOBILE, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_WIFI, DetailedState.BLOCKED);
assertNetworkInfo(TYPE_VPN, DetailedState.BLOCKED);
- assertExtraInfoFromCmBlocked(mWiFiNetworkAgent);
+ assertExtraInfoFromCmBlocked(mWiFiAgent);
// The VPN comes up again on wifi.
b1 = expectConnectivityAction(TYPE_VPN, DetailedState.CONNECTED);
b2 = expectConnectivityAction(TYPE_WIFI, DetailedState.CONNECTED);
- establishLegacyLockdownVpn(mWiFiNetworkAgent.getNetwork());
+ establishLegacyLockdownVpn(mWiFiAgent.getNetwork());
callback.expectAvailableThenValidatedCallbacks(mMockVpn);
defaultCallback.expectAvailableThenValidatedCallbacks(mMockVpn);
systemDefaultCallback.assertNoCallback();
@@ -9868,7 +9860,7 @@
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_VPN, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mWiFiNetworkAgent);
+ assertExtraInfoFromCmPresent(mWiFiAgent);
vpnNc = mCm.getNetworkCapabilities(mMockVpn.getNetwork());
assertTrue(vpnNc.hasTransport(TRANSPORT_VPN));
assertTrue(vpnNc.hasTransport(TRANSPORT_WIFI));
@@ -9876,8 +9868,8 @@
assertTrue(vpnNc.hasCapability(NET_CAPABILITY_NOT_METERED));
// Disconnect cell. Nothing much happens since it's not the default network.
- mCellNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ callback.expect(LOST, mCellAgent);
defaultCallback.assertNoCallback();
systemDefaultCallback.assertNoCallback();
@@ -9885,17 +9877,17 @@
assertNetworkInfo(TYPE_MOBILE, DetailedState.DISCONNECTED);
assertNetworkInfo(TYPE_WIFI, DetailedState.CONNECTED);
assertNetworkInfo(TYPE_VPN, DetailedState.CONNECTED);
- assertExtraInfoFromCmPresent(mWiFiNetworkAgent);
+ assertExtraInfoFromCmPresent(mWiFiAgent);
b1 = expectConnectivityAction(TYPE_WIFI, DetailedState.DISCONNECTED);
b2 = expectConnectivityAction(TYPE_VPN, DetailedState.DISCONNECTED);
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- systemDefaultCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
+ systemDefaultCallback.expect(LOST, mWiFiAgent);
b1.expectBroadcast();
callback.expectCapabilitiesThat(mMockVpn, nc -> !nc.hasTransport(TRANSPORT_WIFI));
mMockVpn.expectStopVpnRunnerPrivileged();
- callback.expect(CallbackEntry.LOST, mMockVpn);
+ callback.expect(LOST, mMockVpn);
b2.expectBroadcast();
VMSHandlerThread.quitSafely();
@@ -10042,32 +10034,32 @@
callbackWithoutCap);
// Setup networks with testing capability and verify the default network changes.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.addCapability(testCap);
- mCellNetworkAgent.connect(true);
- callbackWithCap.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- callbackWithoutCap.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- verify(mMockNetd).networkSetDefault(eq(mCellNetworkAgent.getNetwork().netId));
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.addCapability(testCap);
+ mCellAgent.connect(true);
+ callbackWithCap.expectAvailableThenValidatedCallbacks(mCellAgent);
+ callbackWithoutCap.expectAvailableThenValidatedCallbacks(mCellAgent);
+ verify(mMockNetd).networkSetDefault(eq(mCellAgent.getNetwork().netId));
reset(mMockNetd);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(testCap);
- mWiFiNetworkAgent.connect(true);
- callbackWithCap.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- callbackWithoutCap.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
- verify(mMockNetd).networkSetDefault(eq(mWiFiNetworkAgent.getNetwork().netId));
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(testCap);
+ mWiFiAgent.connect(true);
+ callbackWithCap.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ callbackWithoutCap.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
+ verify(mMockNetd).networkSetDefault(eq(mWiFiAgent.getNetwork().netId));
reset(mMockNetd);
// Remove the testing capability on wifi, verify the callback and default network
// changes back to cellular.
- mWiFiNetworkAgent.removeCapability(testCap);
- callbackWithCap.expectAvailableCallbacksValidated(mCellNetworkAgent);
- callbackWithoutCap.expectCapabilitiesWithout(testCap, mWiFiNetworkAgent);
- verify(mMockNetd).networkSetDefault(eq(mCellNetworkAgent.getNetwork().netId));
+ mWiFiAgent.removeCapability(testCap);
+ callbackWithCap.expectAvailableCallbacksValidated(mCellAgent);
+ callbackWithoutCap.expectCapabilitiesWithout(testCap, mWiFiAgent);
+ verify(mMockNetd).networkSetDefault(eq(mCellAgent.getNetwork().netId));
reset(mMockNetd);
- mCellNetworkAgent.removeCapability(testCap);
- callbackWithCap.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.removeCapability(testCap);
+ callbackWithCap.expect(LOST, mCellAgent);
callbackWithoutCap.assertNoCallback();
verify(mMockNetd).networkClearDefault();
@@ -10080,8 +10072,8 @@
public final void testBatteryStatsNetworkType() throws Exception {
final LinkProperties cellLp = new LinkProperties();
cellLp.setInterfaceName("cell0");
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(true);
waitForIdle();
final ArrayTrackRecord<ReportedInterfaces>.ReadHead readHead =
mDeps.mReportedInterfaceHistory.newReadHead();
@@ -10091,24 +10083,24 @@
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName("wifi0");
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connect(true);
waitForIdle();
assertNotNull(readHead.poll(TIMEOUT_MS, ri -> ri.contentEquals(mServiceContext,
wifiLp.getInterfaceName(),
new int[] { TRANSPORT_WIFI })));
- mCellNetworkAgent.disconnect();
- mWiFiNetworkAgent.disconnect();
+ mCellAgent.disconnect();
+ mWiFiAgent.disconnect();
cellLp.setInterfaceName("wifi0");
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(true);
waitForIdle();
assertNotNull(readHead.poll(TIMEOUT_MS, ri -> ri.contentEquals(mServiceContext,
cellLp.getInterfaceName(),
new int[] { TRANSPORT_CELLULAR })));
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
}
/**
@@ -10229,12 +10221,12 @@
cellLp.addLinkAddress(myIpv6);
cellLp.addRoute(ipv6Default);
cellLp.addRoute(ipv6Subnet);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
reset(mClatCoordinator);
// Connect with ipv6 link properties. Expect prefix discovery to be started.
- mCellNetworkAgent.connect(true);
- int cellNetId = mCellNetworkAgent.getNetwork().netId;
+ mCellAgent.connect(true);
+ int cellNetId = mCellAgent.getNetwork().netId;
waitForIdle();
verify(mMockNetd, times(1)).networkCreate(nativeNetworkConfigPhysical(cellNetId,
@@ -10248,7 +10240,7 @@
cellLp.getInterfaceName(),
new int[] { TRANSPORT_CELLULAR })));
- networkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ networkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
verify(mMockDnsResolver, times(1)).startPrefix64Discovery(cellNetId);
// Switching default network updates TCP buffer sizes.
@@ -10256,8 +10248,8 @@
// Add an IPv4 address. Expect prefix discovery to be stopped. Netd doesn't tell us that
// the NAT64 prefix was removed because one was never discovered.
cellLp.addLinkAddress(myIpv4);
- mCellNetworkAgent.sendLinkProperties(cellLp);
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(cellLp);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
assertRoutesAdded(cellNetId, ipv4Subnet);
verify(mMockDnsResolver, times(1)).stopPrefix64Discovery(cellNetId);
verify(mMockDnsResolver, atLeastOnce()).setResolverConfiguration(any());
@@ -10279,37 +10271,37 @@
// Remove IPv4 address. Expect prefix discovery to be started again.
cellLp.removeLinkAddress(myIpv4);
- mCellNetworkAgent.sendLinkProperties(cellLp);
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(cellLp);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
verify(mMockDnsResolver, times(1)).startPrefix64Discovery(cellNetId);
assertRoutesRemoved(cellNetId, ipv4Subnet);
// When NAT64 prefix discovery succeeds, LinkProperties are updated and clatd is started.
- Nat464Xlat clat = getNat464Xlat(mCellNetworkAgent);
- assertNull(mCm.getLinkProperties(mCellNetworkAgent.getNetwork()).getNat64Prefix());
+ Nat464Xlat clat = getNat464Xlat(mCellAgent);
+ assertNull(mCm.getLinkProperties(mCellAgent.getNetwork()).getNat64Prefix());
mService.mResolverUnsolEventCallback.onNat64PrefixEvent(
makeNat64PrefixEvent(cellNetId, PREFIX_OPERATION_ADDED, kNat64PrefixString, 96));
LinkProperties lpBeforeClat = networkCallback.expect(
- CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent).getLp();
+ LINK_PROPERTIES_CHANGED, mCellAgent).getLp();
assertEquals(0, lpBeforeClat.getStackedLinks().size());
assertEquals(kNat64Prefix, lpBeforeClat.getNat64Prefix());
verifyClatdStart(null /* inOrder */, MOBILE_IFNAME, cellNetId, kNat64Prefix.toString());
// Clat iface comes up. Expect stacked link to be added.
clat.interfaceLinkStateChanged(CLAT_MOBILE_IFNAME, true);
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
- List<LinkProperties> stackedLps = mCm.getLinkProperties(mCellNetworkAgent.getNetwork())
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
+ List<LinkProperties> stackedLps = mCm.getLinkProperties(mCellAgent.getNetwork())
.getStackedLinks();
assertEquals(makeClatLinkProperties(myIpv4), stackedLps.get(0));
assertRoutesAdded(cellNetId, stackedDefault);
verify(mMockNetd, times(1)).networkAddInterface(cellNetId, CLAT_MOBILE_IFNAME);
// Change trivial linkproperties and see if stacked link is preserved.
cellLp.addDnsServer(InetAddress.getByName("8.8.8.8"));
- mCellNetworkAgent.sendLinkProperties(cellLp);
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(cellLp);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
List<LinkProperties> stackedLpsAfterChange =
- mCm.getLinkProperties(mCellNetworkAgent.getNetwork()).getStackedLinks();
+ mCm.getLinkProperties(mCellAgent.getNetwork()).getStackedLinks();
assertNotEquals(stackedLpsAfterChange, Collections.EMPTY_LIST);
assertEquals(makeClatLinkProperties(myIpv4), stackedLpsAfterChange.get(0));
@@ -10332,7 +10324,7 @@
// Expect clatd to be stopped and started with the new prefix.
mService.mResolverUnsolEventCallback.onNat64PrefixEvent(makeNat64PrefixEvent(
cellNetId, PREFIX_OPERATION_ADDED, kOtherNat64PrefixString, 96));
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
+ networkCallback.expectLinkPropertiesThat(mCellAgent,
(lp) -> lp.getStackedLinks().size() == 0);
verifyClatdStop(null /* inOrder */, MOBILE_IFNAME);
assertRoutesRemoved(cellNetId, stackedDefault);
@@ -10340,10 +10332,10 @@
verifyClatdStart(null /* inOrder */, MOBILE_IFNAME, cellNetId,
kOtherNat64Prefix.toString());
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
+ networkCallback.expectLinkPropertiesThat(mCellAgent,
(lp) -> lp.getNat64Prefix().equals(kOtherNat64Prefix));
clat.interfaceLinkStateChanged(CLAT_MOBILE_IFNAME, true);
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
+ networkCallback.expectLinkPropertiesThat(mCellAgent,
(lp) -> lp.getStackedLinks().size() == 1);
assertRoutesAdded(cellNetId, stackedDefault);
verify(mMockNetd, times(1)).networkAddInterface(cellNetId, CLAT_MOBILE_IFNAME);
@@ -10354,15 +10346,15 @@
// linkproperties are cleaned up.
cellLp.addLinkAddress(myIpv4);
cellLp.addRoute(ipv4Subnet);
- mCellNetworkAgent.sendLinkProperties(cellLp);
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(cellLp);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
assertRoutesAdded(cellNetId, ipv4Subnet);
verifyClatdStop(null /* inOrder */, MOBILE_IFNAME);
verify(mMockDnsResolver, times(1)).stopPrefix64Discovery(cellNetId);
// As soon as stop is called, the linkproperties lose the stacked interface.
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
- LinkProperties actualLpAfterIpv4 = mCm.getLinkProperties(mCellNetworkAgent.getNetwork());
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
+ LinkProperties actualLpAfterIpv4 = mCm.getLinkProperties(mCellAgent.getNetwork());
LinkProperties expected = new LinkProperties(cellLp);
expected.setNat64Prefix(kOtherNat64Prefix);
assertEquals(expected, actualLpAfterIpv4);
@@ -10385,49 +10377,49 @@
// Stopping prefix discovery causes netd to tell us that the NAT64 prefix is gone.
mService.mResolverUnsolEventCallback.onNat64PrefixEvent(makeNat64PrefixEvent(
cellNetId, PREFIX_OPERATION_REMOVED, kOtherNat64PrefixString, 96));
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
- (lp) -> lp.getNat64Prefix() == null);
+ networkCallback.expectLinkPropertiesThat(mCellAgent, lp -> lp.getNat64Prefix() == null);
// Remove IPv4 address and expect prefix discovery and clatd to be started again.
cellLp.removeLinkAddress(myIpv4);
cellLp.removeRoute(new RouteInfo(myIpv4, null, MOBILE_IFNAME));
cellLp.removeDnsServer(InetAddress.getByName("8.8.8.8"));
- mCellNetworkAgent.sendLinkProperties(cellLp);
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(cellLp);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
assertRoutesRemoved(cellNetId, ipv4Subnet); // Directly-connected routes auto-added.
verify(mMockDnsResolver, times(1)).startPrefix64Discovery(cellNetId);
mService.mResolverUnsolEventCallback.onNat64PrefixEvent(makeNat64PrefixEvent(
cellNetId, PREFIX_OPERATION_ADDED, kNat64PrefixString, 96));
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
verifyClatdStart(null /* inOrder */, MOBILE_IFNAME, cellNetId, kNat64Prefix.toString());
// Clat iface comes up. Expect stacked link to be added.
clat.interfaceLinkStateChanged(CLAT_MOBILE_IFNAME, true);
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
- (lp) -> lp.getStackedLinks().size() == 1 && lp.getNat64Prefix() != null);
+ networkCallback.expectLinkPropertiesThat(mCellAgent,
+ lp -> lp.getStackedLinks().size() == 1 && lp.getNat64Prefix() != null);
assertRoutesAdded(cellNetId, stackedDefault);
verify(mMockNetd, times(1)).networkAddInterface(cellNetId, CLAT_MOBILE_IFNAME);
// NAT64 prefix is removed. Expect that clat is stopped.
mService.mResolverUnsolEventCallback.onNat64PrefixEvent(makeNat64PrefixEvent(
cellNetId, PREFIX_OPERATION_REMOVED, kNat64PrefixString, 96));
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
- (lp) -> lp.getStackedLinks().size() == 0 && lp.getNat64Prefix() == null);
+ networkCallback.expectLinkPropertiesThat(mCellAgent,
+ lp -> lp.getStackedLinks().size() == 0 && lp.getNat64Prefix() == null);
assertRoutesRemoved(cellNetId, ipv4Subnet, stackedDefault);
// Stop has no effect because clat is already stopped.
verifyClatdStop(null /* inOrder */, MOBILE_IFNAME);
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
- (lp) -> lp.getStackedLinks().size() == 0);
+ networkCallback.expectLinkPropertiesThat(mCellAgent,
+ lp -> lp.getStackedLinks().size() == 0);
verify(mMockNetd, times(1)).networkRemoveInterface(cellNetId, CLAT_MOBILE_IFNAME);
verify(mMockNetd, times(1)).interfaceGetCfg(CLAT_MOBILE_IFNAME);
// Clean up.
- mCellNetworkAgent.disconnect();
- networkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ networkCallback.expect(LOST, mCellAgent);
networkCallback.assertNoCallback();
verify(mMockNetd, times(1)).idletimerRemoveInterface(eq(MOBILE_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_CELLULAR)));
verify(mMockNetd).networkDestroy(cellNetId);
+ verify(mMockNetd).setNetworkAllowlist(any());
verifyNoMoreInteractions(mMockNetd);
verifyNoMoreInteractions(mClatCoordinator);
reset(mMockNetd);
@@ -10439,20 +10431,20 @@
doReturn(getClatInterfaceConfigParcel(myIpv4)).when(mMockNetd)
.interfaceGetCfg(CLAT_MOBILE_IFNAME);
cellLp.setNat64Prefix(kNat64Prefix);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(false /* validated */);
- networkCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- cellNetId = mCellNetworkAgent.getNetwork().netId;
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(false /* validated */);
+ networkCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ cellNetId = mCellAgent.getNetwork().netId;
verify(mMockNetd, times(1)).networkCreate(nativeNetworkConfigPhysical(cellNetId,
INetd.PERMISSION_NONE));
assertRoutesAdded(cellNetId, ipv6Subnet, ipv6Default);
// Clatd is started and clat iface comes up. Expect stacked link to be added.
verifyClatdStart(null /* inOrder */, MOBILE_IFNAME, cellNetId, kNat64Prefix.toString());
- clat = getNat464Xlat(mCellNetworkAgent);
+ clat = getNat464Xlat(mCellAgent);
clat.interfaceLinkStateChanged(CLAT_MOBILE_IFNAME, true /* up */);
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
- (lp) -> lp.getStackedLinks().size() == 1
+ networkCallback.expectLinkPropertiesThat(mCellAgent,
+ lp -> lp.getStackedLinks().size() == 1
&& lp.getNat64Prefix().equals(kNat64Prefix));
verify(mMockNetd).networkAddInterface(cellNetId, CLAT_MOBILE_IFNAME);
// assertRoutesAdded sees all calls since last mMockNetd reset, so expect IPv6 routes again.
@@ -10461,20 +10453,21 @@
reset(mClatCoordinator);
// Disconnect the network. clat is stopped and the network is destroyed.
- mCellNetworkAgent.disconnect();
- networkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ networkCallback.expect(LOST, mCellAgent);
networkCallback.assertNoCallback();
verifyClatdStop(null /* inOrder */, MOBILE_IFNAME);
verify(mMockNetd).idletimerRemoveInterface(eq(MOBILE_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_CELLULAR)));
verify(mMockNetd).networkDestroy(cellNetId);
+ verify(mMockNetd).setNetworkAllowlist(any());
verifyNoMoreInteractions(mMockNetd);
verifyNoMoreInteractions(mClatCoordinator);
mCm.unregisterNetworkCallback(networkCallback);
}
- private void expectNat64PrefixChange(TestableNetworkCallback callback,
+ private void expectNat64PrefixChange(TestNetworkCallback callback,
TestNetworkAgentWrapper agent, IpPrefix prefix) {
callback.expectLinkPropertiesThat(agent, x -> Objects.equals(x.getNat64Prefix(), prefix));
}
@@ -10506,11 +10499,11 @@
// prefix discovery is never started.
LinkProperties lp = new LinkProperties(baseLp);
lp.setNat64Prefix(pref64FromRa);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
- mWiFiNetworkAgent.connect(false);
- final Network network = mWiFiNetworkAgent.getNetwork();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
+ mWiFiAgent.connect(false);
+ final Network network = mWiFiAgent.getNetwork();
int netId = network.getNetId();
- callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ callback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
verifyClatdStart(inOrder, iface, netId, pref64FromRa.toString());
inOrder.verify(mMockDnsResolver).setPrefix64(netId, pref64FromRa.toString());
inOrder.verify(mMockDnsResolver, never()).startPrefix64Discovery(netId);
@@ -10519,8 +10512,8 @@
// If the RA prefix is withdrawn, clatd is stopped and prefix discovery is started.
lp.setNat64Prefix(null);
- mWiFiNetworkAgent.sendLinkProperties(lp);
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, null);
+ mWiFiAgent.sendLinkProperties(lp);
+ expectNat64PrefixChange(callback, mWiFiAgent, null);
verifyClatdStop(inOrder, iface);
inOrder.verify(mMockDnsResolver).setPrefix64(netId, "");
inOrder.verify(mMockDnsResolver).startPrefix64Discovery(netId);
@@ -10528,8 +10521,8 @@
// If the RA prefix appears while DNS discovery is in progress, discovery is stopped and
// clatd is started with the prefix from the RA.
lp.setNat64Prefix(pref64FromRa);
- mWiFiNetworkAgent.sendLinkProperties(lp);
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, pref64FromRa);
+ mWiFiAgent.sendLinkProperties(lp);
+ expectNat64PrefixChange(callback, mWiFiAgent, pref64FromRa);
verifyClatdStart(inOrder, iface, netId, pref64FromRa.toString());
inOrder.verify(mMockDnsResolver).stopPrefix64Discovery(netId);
inOrder.verify(mMockDnsResolver).setPrefix64(netId, pref64FromRa.toString());
@@ -10537,21 +10530,21 @@
// Withdraw the RA prefix so we can test the case where an RA prefix appears after DNS
// discovery has succeeded.
lp.setNat64Prefix(null);
- mWiFiNetworkAgent.sendLinkProperties(lp);
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, null);
+ mWiFiAgent.sendLinkProperties(lp);
+ expectNat64PrefixChange(callback, mWiFiAgent, null);
verifyClatdStop(inOrder, iface);
inOrder.verify(mMockDnsResolver).setPrefix64(netId, "");
inOrder.verify(mMockDnsResolver).startPrefix64Discovery(netId);
mService.mResolverUnsolEventCallback.onNat64PrefixEvent(
makeNat64PrefixEvent(netId, PREFIX_OPERATION_ADDED, pref64FromDnsStr, 96));
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, pref64FromDns);
+ expectNat64PrefixChange(callback, mWiFiAgent, pref64FromDns);
verifyClatdStart(inOrder, iface, netId, pref64FromDns.toString());
// If an RA advertises the same prefix that was discovered by DNS, nothing happens: prefix
// discovery is not stopped, and there are no callbacks.
lp.setNat64Prefix(pref64FromDns);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent.sendLinkProperties(lp);
callback.assertNoCallback();
verifyNeverClatdStop(inOrder, iface);
verifyNeverClatdStart(inOrder, iface);
@@ -10561,7 +10554,7 @@
// If the RA is later withdrawn, nothing happens again.
lp.setNat64Prefix(null);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent.sendLinkProperties(lp);
callback.assertNoCallback();
verifyNeverClatdStop(inOrder, iface);
verifyNeverClatdStart(inOrder, iface);
@@ -10571,8 +10564,8 @@
// If the RA prefix changes, clatd is restarted and prefix discovery is stopped.
lp.setNat64Prefix(pref64FromRa);
- mWiFiNetworkAgent.sendLinkProperties(lp);
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, pref64FromRa);
+ mWiFiAgent.sendLinkProperties(lp);
+ expectNat64PrefixChange(callback, mWiFiAgent, pref64FromRa);
verifyClatdStop(inOrder, iface);
inOrder.verify(mMockDnsResolver).stopPrefix64Discovery(netId);
@@ -10586,8 +10579,8 @@
// If the RA prefix changes, clatd is restarted and prefix discovery is not started.
lp.setNat64Prefix(newPref64FromRa);
- mWiFiNetworkAgent.sendLinkProperties(lp);
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, newPref64FromRa);
+ mWiFiAgent.sendLinkProperties(lp);
+ expectNat64PrefixChange(callback, mWiFiAgent, newPref64FromRa);
verifyClatdStop(inOrder, iface);
inOrder.verify(mMockDnsResolver).setPrefix64(netId, "");
verifyClatdStart(inOrder, iface, netId, newPref64FromRa.toString());
@@ -10597,7 +10590,7 @@
// If the RA prefix changes to the same value, nothing happens.
lp.setNat64Prefix(newPref64FromRa);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent.sendLinkProperties(lp);
callback.assertNoCallback();
assertEquals(newPref64FromRa, mCm.getLinkProperties(network).getNat64Prefix());
verifyNeverClatdStop(inOrder, iface);
@@ -10611,19 +10604,19 @@
// If the same prefix is learned first by DNS and then by RA, and clat is later stopped,
// (e.g., because the network disconnects) setPrefix64(netid, "") is never called.
lp.setNat64Prefix(null);
- mWiFiNetworkAgent.sendLinkProperties(lp);
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, null);
+ mWiFiAgent.sendLinkProperties(lp);
+ expectNat64PrefixChange(callback, mWiFiAgent, null);
verifyClatdStop(inOrder, iface);
inOrder.verify(mMockDnsResolver).setPrefix64(netId, "");
inOrder.verify(mMockDnsResolver).startPrefix64Discovery(netId);
mService.mResolverUnsolEventCallback.onNat64PrefixEvent(
makeNat64PrefixEvent(netId, PREFIX_OPERATION_ADDED, pref64FromDnsStr, 96));
- expectNat64PrefixChange(callback, mWiFiNetworkAgent, pref64FromDns);
+ expectNat64PrefixChange(callback, mWiFiAgent, pref64FromDns);
verifyClatdStart(inOrder, iface, netId, pref64FromDns.toString());
inOrder.verify(mMockDnsResolver, never()).setPrefix64(eq(netId), any());
lp.setNat64Prefix(pref64FromDns);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent.sendLinkProperties(lp);
callback.assertNoCallback();
verifyNeverClatdStop(inOrder, iface);
verifyNeverClatdStart(inOrder, iface);
@@ -10635,8 +10628,8 @@
// before CONNECTIVITY_ACTION is sent. Wait for CONNECTIVITY_ACTION before verifying that
// clat has been stopped, or the test will be flaky.
ExpectedBroadcast b = expectConnectivityAction(TYPE_WIFI, DetailedState.DISCONNECTED);
- mWiFiNetworkAgent.disconnect();
- callback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ callback.expect(LOST, mWiFiAgent);
b.expectBroadcast();
verifyClatdStop(inOrder, iface);
@@ -10663,17 +10656,17 @@
cellLp.setInterfaceName(MOBILE_IFNAME);
cellLp.addLinkAddress(new LinkAddress("2001:db8:1::1/64"));
cellLp.addRoute(new RouteInfo(new IpPrefix("::/0"), null, MOBILE_IFNAME));
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.sendLinkProperties(cellLp);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- defaultCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- final int cellNetId = mCellNetworkAgent.getNetwork().netId;
+ mCellAgent.sendLinkProperties(cellLp);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ defaultCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ final int cellNetId = mCellAgent.getNetwork().netId;
waitForIdle();
verify(mMockDnsResolver, never()).startPrefix64Discovery(cellNetId);
- Nat464Xlat clat = getNat464Xlat(mCellNetworkAgent);
+ Nat464Xlat clat = getNat464Xlat(mCellAgent);
assertTrue("Nat464Xlat was not IDLE", !clat.isStarted());
// This cannot happen because prefix discovery cannot succeed if it is never started.
@@ -10693,25 +10686,25 @@
.build();
mCm.registerNetworkCallback(networkRequest, networkCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
final LinkProperties cellLp = new LinkProperties();
cellLp.setInterfaceName(MOBILE_IFNAME);
- mCellNetworkAgent.sendLinkProperties(cellLp);
- mCellNetworkAgent.connect(true);
- networkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(cellLp);
+ mCellAgent.connect(true);
+ networkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
verify(mMockNetd, times(1)).idletimerAddInterface(eq(MOBILE_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_CELLULAR)));
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent.sendLinkProperties(wifiLp);
+ mWiFiAgent.sendLinkProperties(wifiLp);
// Network switch
- mWiFiNetworkAgent.connect(true);
- networkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- networkCallback.expectLosing(mCellNetworkAgent);
- networkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ mWiFiAgent.connect(true);
+ networkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ networkCallback.expectLosing(mCellAgent);
+ networkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
verify(mMockNetd, times(1)).idletimerAddInterface(eq(WIFI_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_WIFI)));
verify(mMockNetd, times(1)).idletimerRemoveInterface(eq(MOBILE_IFNAME), anyInt(),
@@ -10719,8 +10712,8 @@
// Disconnect wifi and switch back to cell
reset(mMockNetd);
- mWiFiNetworkAgent.disconnect();
- networkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
+ mWiFiAgent.disconnect();
+ networkCallback.expect(LOST, mWiFiAgent);
assertNoCallbacks(networkCallback);
verify(mMockNetd, times(1)).idletimerRemoveInterface(eq(WIFI_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_WIFI)));
@@ -10729,13 +10722,13 @@
// reconnect wifi
reset(mMockNetd);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent.sendLinkProperties(wifiLp);
- mWiFiNetworkAgent.connect(true);
- networkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- networkCallback.expectLosing(mCellNetworkAgent);
- networkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
+ mWiFiAgent.sendLinkProperties(wifiLp);
+ mWiFiAgent.connect(true);
+ networkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ networkCallback.expectLosing(mCellAgent);
+ networkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
verify(mMockNetd, times(1)).idletimerAddInterface(eq(WIFI_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_WIFI)));
verify(mMockNetd, times(1)).idletimerRemoveInterface(eq(MOBILE_IFNAME), anyInt(),
@@ -10743,21 +10736,20 @@
// Disconnect cell
reset(mMockNetd);
- mCellNetworkAgent.disconnect();
- networkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ networkCallback.expect(LOST, mCellAgent);
// LOST callback is triggered earlier than removing idle timer. Broadcast should also be
// sent as network being switched. Ensure rule removal for cell will not be triggered
// unexpectedly before network being removed.
waitForIdle();
verify(mMockNetd, times(0)).idletimerRemoveInterface(eq(MOBILE_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_CELLULAR)));
- verify(mMockNetd, times(1)).networkDestroy(eq(mCellNetworkAgent.getNetwork().netId));
- verify(mMockDnsResolver, times(1))
- .destroyNetworkCache(eq(mCellNetworkAgent.getNetwork().netId));
+ verify(mMockNetd, times(1)).networkDestroy(eq(mCellAgent.getNetwork().netId));
+ verify(mMockDnsResolver, times(1)).destroyNetworkCache(eq(mCellAgent.getNetwork().netId));
// Disconnect wifi
ExpectedBroadcast b = expectConnectivityAction(TYPE_WIFI, DetailedState.DISCONNECTED);
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
b.expectBroadcast();
verify(mMockNetd, times(1)).idletimerRemoveInterface(eq(WIFI_IFNAME), anyInt(),
eq(Integer.toString(TRANSPORT_WIFI)));
@@ -10784,21 +10776,21 @@
final TestNetworkCallback networkCallback = new TestNetworkCallback();
mCm.registerNetworkCallback(networkRequest, networkCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
reset(mMockNetd);
// Switching default network updates TCP buffer sizes.
- mCellNetworkAgent.connect(false);
- networkCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ mCellAgent.connect(false);
+ networkCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
verifyTcpBufferSizeChange(ConnectivityService.DEFAULT_TCP_BUFFER_SIZES);
// Change link Properties should have updated tcp buffer size.
LinkProperties lp = new LinkProperties();
lp.setTcpBufferSizes(testTcpBufferSizes);
- mCellNetworkAgent.sendLinkProperties(lp);
- networkCallback.expect(CallbackEntry.LINK_PROPERTIES_CHANGED, mCellNetworkAgent);
+ mCellAgent.sendLinkProperties(lp);
+ networkCallback.expect(LINK_PROPERTIES_CHANGED, mCellAgent);
verifyTcpBufferSizeChange(testTcpBufferSizes);
// Clean up.
- mCellNetworkAgent.disconnect();
- networkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ networkCallback.expect(LOST, mCellAgent);
networkCallback.assertNoCallback();
mCm.unregisterNetworkCallback(networkCallback);
}
@@ -10806,8 +10798,8 @@
@Test
public void testGetGlobalProxyForNetwork() throws Exception {
final ProxyInfo testProxyInfo = ProxyInfo.buildDirectProxy("test", 8888);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- final Network wifiNetwork = mWiFiNetworkAgent.getNetwork();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ final Network wifiNetwork = mWiFiAgent.getNetwork();
mProxyTracker.setGlobalProxy(testProxyInfo);
assertEquals(testProxyInfo, mService.getProxyForNetwork(wifiNetwork));
}
@@ -10815,15 +10807,15 @@
@Test
public void testGetProxyForActiveNetwork() throws Exception {
final ProxyInfo testProxyInfo = ProxyInfo.buildDirectProxy("test", 8888);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
waitForIdle();
assertNull(mService.getProxyForNetwork(null));
final LinkProperties testLinkProperties = new LinkProperties();
testLinkProperties.setHttpProxy(testProxyInfo);
- mWiFiNetworkAgent.sendLinkProperties(testLinkProperties);
+ mWiFiAgent.sendLinkProperties(testLinkProperties);
waitForIdle();
assertEquals(testProxyInfo, mService.getProxyForNetwork(null));
@@ -10834,8 +10826,8 @@
final ProxyInfo testProxyInfo = ProxyInfo.buildDirectProxy("test", 8888);
// Set up a WiFi network with no proxy
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
waitForIdle();
assertNull(mService.getProxyForNetwork(null));
@@ -10848,7 +10840,7 @@
// Test that the VPN network returns a proxy, and the WiFi does not.
assertEquals(testProxyInfo, mService.getProxyForNetwork(mMockVpn.getNetwork()));
assertEquals(testProxyInfo, mService.getProxyForNetwork(null));
- assertNull(mService.getProxyForNetwork(mWiFiNetworkAgent.getNetwork()));
+ assertNull(mService.getProxyForNetwork(mWiFiAgent.getNetwork()));
// Test that the VPN network returns no proxy when it is set to null.
testLinkProperties.setHttpProxy(null);
@@ -10859,7 +10851,7 @@
// Set WiFi proxy and check that the vpn proxy is still null.
testLinkProperties.setHttpProxy(testProxyInfo);
- mWiFiNetworkAgent.sendLinkProperties(testLinkProperties);
+ mWiFiAgent.sendLinkProperties(testLinkProperties);
waitForIdle();
assertNull(mService.getProxyForNetwork(null));
@@ -10867,8 +10859,8 @@
// correct proxy setting.
mMockVpn.disconnect();
waitForIdle();
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
- assertEquals(testProxyInfo, mService.getProxyForNetwork(mWiFiNetworkAgent.getNetwork()));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(testProxyInfo, mService.getProxyForNetwork(mWiFiAgent.getNetwork()));
assertEquals(testProxyInfo, mService.getProxyForNetwork(null));
}
@@ -11047,15 +11039,15 @@
@Test
public void testLinkPropertiesWithWakeOnLanForActiveNetwork() throws Exception {
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_WOL_IFNAME);
wifiLp.setWakeOnLanSupported(false);
// Default network switch should update ifaces.
- mWiFiNetworkAgent.connect(false);
- mWiFiNetworkAgent.sendLinkProperties(wifiLp);
+ mWiFiAgent.connect(false);
+ mWiFiAgent.sendLinkProperties(wifiLp);
waitForIdle();
// ConnectivityService should have changed the WakeOnLanSupported to true
@@ -11447,18 +11439,17 @@
.addTransportType(TRANSPORT_WIFI).build();
mCm.registerNetworkCallback(wifiRequest, wifiNetworkCallback);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, new LinkProperties(),
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, new LinkProperties(),
ncTemplate);
- mWiFiNetworkAgent.connect(false);
+ mWiFiAgent.connect(false);
- wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ wifiNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
// Send network capabilities update with TransportInfo to trigger capabilities changed
// callback.
- mWiFiNetworkAgent.setNetworkCapabilities(
- ncTemplate.setTransportInfo(actualTransportInfo), true);
+ mWiFiAgent.setNetworkCapabilities(ncTemplate.setTransportInfo(actualTransportInfo), true);
- wifiNetworkCallback.expectCapabilitiesThat(mWiFiNetworkAgent,
+ wifiNetworkCallback.expectCapabilitiesThat(mWiFiAgent,
nc -> Objects.equals(expectedOwnerUid, nc.getOwnerUid())
&& Objects.equals(expectedTransportInfo, nc.getTransportInfo()));
}
@@ -11487,32 +11478,31 @@
.addTransportType(TRANSPORT_WIFI)
.setTransportInfo(new TestTransportInfo());
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, new LinkProperties(),
- ncTemplate);
- mWiFiNetworkAgent.connect(true /* validated; waits for callback */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, new LinkProperties(), ncTemplate);
+ mWiFiAgent.connect(true /* validated; waits for callback */);
// NETWORK_SETTINGS redaction is controlled by the NETWORK_SETTINGS permission
- assertTrue(getTestTransportInfo(mWiFiNetworkAgent).settingsRedacted);
+ assertTrue(getTestTransportInfo(mWiFiAgent).settingsRedacted);
withPermission(NETWORK_SETTINGS, () -> {
- assertFalse(getTestTransportInfo(mWiFiNetworkAgent).settingsRedacted);
+ assertFalse(getTestTransportInfo(mWiFiAgent).settingsRedacted);
});
- assertTrue(getTestTransportInfo(mWiFiNetworkAgent).settingsRedacted);
+ assertTrue(getTestTransportInfo(mWiFiAgent).settingsRedacted);
// LOCAL_MAC_ADDRESS redaction is controlled by the LOCAL_MAC_ADDRESS permission
- assertTrue(getTestTransportInfo(mWiFiNetworkAgent).localMacAddressRedacted);
+ assertTrue(getTestTransportInfo(mWiFiAgent).localMacAddressRedacted);
withPermission(LOCAL_MAC_ADDRESS, () -> {
- assertFalse(getTestTransportInfo(mWiFiNetworkAgent).localMacAddressRedacted);
+ assertFalse(getTestTransportInfo(mWiFiAgent).localMacAddressRedacted);
});
- assertTrue(getTestTransportInfo(mWiFiNetworkAgent).localMacAddressRedacted);
+ assertTrue(getTestTransportInfo(mWiFiAgent).localMacAddressRedacted);
// Synchronous getNetworkCapabilities calls never return unredacted location-sensitive
// information.
- assertTrue(getTestTransportInfo(mWiFiNetworkAgent).locationRedacted);
+ assertTrue(getTestTransportInfo(mWiFiAgent).locationRedacted);
setupLocationPermissions(Build.VERSION_CODES.S, true, AppOpsManager.OPSTR_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION);
- assertTrue(getTestTransportInfo(mWiFiNetworkAgent).locationRedacted);
+ assertTrue(getTestTransportInfo(mWiFiAgent).locationRedacted);
denyAllLocationPrivilegedPermissions();
- assertTrue(getTestTransportInfo(mWiFiNetworkAgent).locationRedacted);
+ assertTrue(getTestTransportInfo(mWiFiAgent).locationRedacted);
}
private void setupConnectionOwnerUid(int vpnOwnerUid, @VpnManager.VpnType int vpnType)
@@ -11937,9 +11927,9 @@
mCm.registerDefaultNetworkCallback(callback);
final LinkProperties linkProperties = new LinkProperties();
linkProperties.setInterfaceName(INTERFACE_NAME);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, linkProperties);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, linkProperties);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
callback.assertNoCallback();
final NetworkRequest request = new NetworkRequest.Builder().build();
@@ -11976,10 +11966,10 @@
final NetworkCapabilities ncTemplate = new NetworkCapabilities()
.addTransportType(TRANSPORT_CELLULAR)
.setTransportInfo(new TestTransportInfo());
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(),
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, new LinkProperties(),
ncTemplate);
- mCellNetworkAgent.connect(true);
- callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent.connect(true);
+ callback.expectAvailableThenValidatedCallbacks(mCellAgent);
callback.assertNoCallback();
// Make sure a report is sent and that the caps are suitably redacted.
@@ -12005,7 +11995,7 @@
// Trigger notifyDataStallSuspected() on the INetworkMonitorCallbacks instance in the
// cellular network agent
- mCellNetworkAgent.notifyDataStallSuspected();
+ mCellAgent.notifyDataStallSuspected();
// Verify onDataStallSuspected fired
verify(mConnectivityDiagnosticsCallback, timeout(TIMEOUT_MS)).onDataStallSuspected(
@@ -12016,7 +12006,7 @@
public void testConnectivityDiagnosticsCallbackOnConnectivityReported() throws Exception {
setUpConnectivityDiagnosticsCallback();
- final Network n = mCellNetworkAgent.getNetwork();
+ final Network n = mCellAgent.getNetwork();
final boolean hasConnectivity = true;
mService.reportNetworkConnectivity(n, hasConnectivity);
@@ -12049,7 +12039,7 @@
// report known Connectivity from a different uid. Verify that network is not re-validated
// and this callback is not notified.
- final Network n = mCellNetworkAgent.getNetwork();
+ final Network n = mCellAgent.getNetwork();
final boolean hasConnectivity = true;
doAsUid(Process.myUid() + 1, () -> mService.reportNetworkConnectivity(n, hasConnectivity));
@@ -12101,11 +12091,11 @@
final NetworkRequest request = new NetworkRequest.Builder().build();
final TestNetworkCallback networkCallback = new TestNetworkCallback();
mCm.registerNetworkCallback(request, networkCallback);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
reset(mMockNetd);
- mCellNetworkAgent.connect(false);
- networkCallback.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- final int netId = mCellNetworkAgent.getNetwork().netId;
+ mCellAgent.connect(false);
+ networkCallback.expectAvailableCallbacksUnvalidated(mCellAgent);
+ final int netId = mCellAgent.getNetwork().netId;
final String iface = "rmnet_data0";
final InetAddress gateway = InetAddress.getByName("fe80::5678");
@@ -12122,8 +12112,8 @@
lp.addRoute(direct);
lp.addRoute(rio1);
lp.addRoute(defaultRoute);
- mCellNetworkAgent.sendLinkProperties(lp);
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent, x -> x.getRoutes().size() == 3);
+ mCellAgent.sendLinkProperties(lp);
+ networkCallback.expectLinkPropertiesThat(mCellAgent, x -> x.getRoutes().size() == 3);
assertRoutesAdded(netId, direct, rio1, defaultRoute);
reset(mMockNetd);
@@ -12137,9 +12127,8 @@
assertFalse(lp.getRoutes().contains(defaultRoute));
assertTrue(lp.getRoutes().contains(defaultWithMtu));
- mCellNetworkAgent.sendLinkProperties(lp);
- networkCallback.expectLinkPropertiesThat(mCellNetworkAgent,
- x -> x.getRoutes().contains(rio2));
+ mCellAgent.sendLinkProperties(lp);
+ networkCallback.expectLinkPropertiesThat(mCellAgent, x -> x.getRoutes().contains(rio2));
assertRoutesRemoved(netId, rio1);
assertRoutesAdded(netId, rio2);
@@ -12251,8 +12240,8 @@
@Test
public void testVpnUidRangesUpdate() throws Exception {
// Set up a WiFi network without proxy.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
assertNull(mService.getProxyForNetwork(null));
assertNull(mCm.getDefaultProxy());
@@ -12304,11 +12293,11 @@
b5.expectBroadcast();
// Proxy is added in WiFi(default network), setDefaultProxy will be called.
- final LinkProperties wifiLp = mCm.getLinkProperties(mWiFiNetworkAgent.getNetwork());
+ final LinkProperties wifiLp = mCm.getLinkProperties(mWiFiAgent.getNetwork());
assertNotNull(wifiLp);
final ExpectedBroadcast b6 = expectProxyChangeAction(testProxyInfo);
wifiLp.setHttpProxy(testProxyInfo);
- mWiFiNetworkAgent.sendLinkProperties(wifiLp);
+ mWiFiAgent.sendLinkProperties(wifiLp);
waitForIdle();
b6.expectBroadcast();
}
@@ -12316,8 +12305,8 @@
@Test
public void testProxyBroadcastWillBeSentWhenVpnHasProxyAndConnects() throws Exception {
// Set up a WiFi network without proxy.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
assertNull(mService.getProxyForNetwork(null));
assertNull(mCm.getDefaultProxy());
@@ -12351,22 +12340,22 @@
public void testProxyBroadcastWillBeSentWhenTheProxyOfNonDefaultNetworkHasChanged()
throws Exception {
// Set up a CELLULAR network without proxy.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
assertNull(mService.getProxyForNetwork(null));
assertNull(mCm.getDefaultProxy());
// CELLULAR network should be the default network.
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
// Set up a WiFi network without proxy.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
assertNull(mService.getProxyForNetwork(null));
assertNull(mCm.getDefaultProxy());
// WiFi network should be the default network.
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetwork());
// CELLULAR network is not the default network.
- assertNotEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertNotEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
// CELLULAR network is not the system default network, but it might be a per-app default
// network. The proxy broadcast should be sent once its proxy has changed.
@@ -12375,7 +12364,7 @@
final ProxyInfo testProxyInfo = ProxyInfo.buildDirectProxy("test", 8888);
final ExpectedBroadcast b = registerPacProxyBroadcast();
cellularLp.setHttpProxy(testProxyInfo);
- mCellNetworkAgent.sendLinkProperties(cellularLp);
+ mCellAgent.sendLinkProperties(cellularLp);
b.expectBroadcast();
}
@@ -12404,66 +12393,64 @@
.build();
mCm.registerNetworkCallback(allNetworksRequest, allNetworksCb);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true /* validated */);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true /* validated */);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- allNetworksCb.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ allNetworksCb.expectAvailableThenValidatedCallbacks(mCellAgent);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true /* validated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true /* validated */);
- mDefaultNetworkCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
+ mDefaultNetworkCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
// While the default callback doesn't see the network before it's validated, the listen
// sees the network come up and validate later
- allNetworksCb.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
- allNetworksCb.expectLosing(mCellNetworkAgent);
- allNetworksCb.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
- allNetworksCb.expect(CallbackEntry.LOST, mCellNetworkAgent,
- TEST_LINGER_DELAY_MS * 2);
+ allNetworksCb.expectAvailableCallbacksUnvalidated(mWiFiAgent);
+ allNetworksCb.expectLosing(mCellAgent);
+ allNetworksCb.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ allNetworksCb.expect(LOST, mCellAgent, TEST_LINGER_DELAY_MS * 2);
// The cell network has disconnected (see LOST above) because it was outscored and
// had no requests (see setAlwaysOnNetworks(false) above)
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
final NetworkScore score = new NetworkScore.Builder().setLegacyInt(30).build();
- mCellNetworkAgent.setScore(score);
- mCellNetworkAgent.connect(false /* validated */);
+ mCellAgent.setScore(score);
+ mCellAgent.connect(false /* validated */);
// The cell network gets torn down right away.
- allNetworksCb.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
- allNetworksCb.expect(CallbackEntry.LOST, mCellNetworkAgent,
- TEST_NASCENT_DELAY_MS * 2);
+ allNetworksCb.expectAvailableCallbacksUnvalidated(mCellAgent);
+ allNetworksCb.expect(LOST, mCellAgent, TEST_NASCENT_DELAY_MS * 2);
allNetworksCb.assertNoCallback();
// Now create a cell network with KEEP_CONNECTED_FOR_HANDOVER and make sure it's
// not disconnected immediately when outscored.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
final NetworkScore scoreKeepup = new NetworkScore.Builder().setLegacyInt(30)
.setKeepConnectedReason(KEEP_CONNECTED_FOR_HANDOVER).build();
- mCellNetworkAgent.setScore(scoreKeepup);
- mCellNetworkAgent.connect(true /* validated */);
+ mCellAgent.setScore(scoreKeepup);
+ mCellAgent.connect(true /* validated */);
- allNetworksCb.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ allNetworksCb.expectAvailableThenValidatedCallbacks(mCellAgent);
mDefaultNetworkCallback.assertNoCallback();
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
- allNetworksCb.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- mDefaultNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ allNetworksCb.expect(LOST, mWiFiAgent);
+ mDefaultNetworkCallback.expect(LOST, mWiFiAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
// Reconnect a WiFi network and make sure the cell network is still not torn down.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true /* validated */);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true /* validated */);
- allNetworksCb.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- mDefaultNetworkCallback.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
+ allNetworksCb.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ mDefaultNetworkCallback.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
// Now remove the reason to keep connected and make sure the network lingers and is
// torn down.
- mCellNetworkAgent.setScore(new NetworkScore.Builder().setLegacyInt(30).build());
- allNetworksCb.expectLosing(mCellNetworkAgent, TEST_NASCENT_DELAY_MS * 2);
- allNetworksCb.expect(CallbackEntry.LOST, mCellNetworkAgent, TEST_LINGER_DELAY_MS * 2);
+ mCellAgent.setScore(new NetworkScore.Builder().setLegacyInt(30).build());
+ allNetworksCb.expectLosing(mCellAgent, TEST_NASCENT_DELAY_MS * 2);
+ allNetworksCb.expect(LOST, mCellAgent, TEST_LINGER_DELAY_MS * 2);
mDefaultNetworkCallback.assertNoCallback();
mCm.unregisterNetworkCallback(allNetworksCb);
@@ -12481,21 +12468,21 @@
mFilter = mock(QosFilter.class);
// Ensure the network is disconnected before anything else occurs
- assertNull(mCellNetworkAgent);
+ assertNull(mCellAgent);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
verifyActiveNetwork(TRANSPORT_CELLULAR);
waitForIdle();
- final Network network = mCellNetworkAgent.getNetwork();
+ final Network network = mCellAgent.getNetwork();
final Pair<IQosCallback, IBinder> pair = createQosCallback();
mCallback = pair.first;
doReturn(network).when(mFilter).getNetwork();
doReturn(QosCallbackException.EX_TYPE_FILTER_NONE).when(mFilter).validate();
- mAgentWrapper = mCellNetworkAgent;
+ mAgentWrapper = mCellAgent;
}
void registerQosCallback(@NonNull final QosFilter filter,
@@ -13085,12 +13072,12 @@
// Corresponds to a metered cellular network. Will be used for the default network.
case TRANSPORT_CELLULAR:
if (!connectAgent) {
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
break;
}
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
+ mCellAgent.connect(true);
break;
// Corresponds to a restricted ethernet network with OEM_PAID/OEM_PRIVATE.
case TRANSPORT_ETHERNET:
@@ -13103,12 +13090,12 @@
// Corresponds to unmetered Wi-Fi.
case TRANSPORT_WIFI:
if (!connectAgent) {
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
break;
}
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.connect(true);
break;
default:
throw new AssertionError("Unsupported transport type passed in.");
@@ -13118,15 +13105,15 @@
}
private void startOemManagedNetwork(final boolean isOemPaid) throws Exception {
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
- mEthernetNetworkAgent.addCapability(
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
+ mEthernetAgent.addCapability(
isOemPaid ? NET_CAPABILITY_OEM_PAID : NET_CAPABILITY_OEM_PRIVATE);
- mEthernetNetworkAgent.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
- mEthernetNetworkAgent.connect(true);
+ mEthernetAgent.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
+ mEthernetAgent.connect(true);
}
private void stopOemManagedNetwork() {
- mEthernetNetworkAgent.disconnect();
+ mEthernetAgent.disconnect();
waitForIdle();
}
@@ -13359,7 +13346,7 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
null,
- mEthernetNetworkAgent.getNetwork());
+ mEthernetAgent.getNetwork());
// Verify that the active network is correct
verifyActiveNetwork(TRANSPORT_ETHERNET);
@@ -13380,16 +13367,16 @@
assertTrue(mCm.isActiveNetworkMetered());
// Connect to an unmetered restricted network that will only be available to the OEM pref.
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
- mEthernetNetworkAgent.addCapability(NET_CAPABILITY_OEM_PAID);
- mEthernetNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mEthernetNetworkAgent.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
- mEthernetNetworkAgent.connect(true);
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET);
+ mEthernetAgent.addCapability(NET_CAPABILITY_OEM_PAID);
+ mEthernetAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mEthernetAgent.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
+ mEthernetAgent.connect(true);
waitForIdle();
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
null,
- mEthernetNetworkAgent.getNetwork());
+ mEthernetAgent.getNetwork());
assertFalse(mCm.isActiveNetworkMetered());
// default NCs will be unregistered in tearDown
@@ -13420,19 +13407,18 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
null,
- mEthernetNetworkAgent.getNetwork());
+ mEthernetAgent.getNetwork());
// At this point with a restricted network used, the available callback should trigger.
- defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mEthernetNetworkAgent);
- assertEquals(defaultNetworkCallback.getLastAvailableNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mEthernetAgent);
+ assertEquals(defaultNetworkCallback.getLastAvailableNetwork(), mEthernetAgent.getNetwork());
otherUidDefaultCallback.assertNoCallback();
// Now bring down the default network which should trigger a LOST callback.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, false);
// At this point, with no network is available, the lost callback should trigger
- defaultNetworkCallback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
+ defaultNetworkCallback.expect(LOST, mEthernetAgent);
otherUidDefaultCallback.assertNoCallback();
// Confirm we can unregister without issues.
@@ -13465,12 +13451,11 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
null,
- mEthernetNetworkAgent.getNetwork());
+ mEthernetAgent.getNetwork());
// At this point with a restricted network used, the available callback should trigger
- defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mEthernetNetworkAgent);
- assertEquals(defaultNetworkCallback.getLastAvailableNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mEthernetAgent);
+ assertEquals(defaultNetworkCallback.getLastAvailableNetwork(), mEthernetAgent.getNetwork());
otherUidDefaultCallback.assertNoCallback();
// Now bring down the default network which should trigger a LOST callback.
@@ -13478,7 +13463,7 @@
otherUidDefaultCallback.assertNoCallback();
// At this point, with no network is available, the lost callback should trigger
- defaultNetworkCallback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
+ defaultNetworkCallback.expect(LOST, mEthernetAgent);
otherUidDefaultCallback.assertNoCallback();
// Confirm we can unregister without issues.
@@ -13511,27 +13496,26 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
null,
- mEthernetNetworkAgent.getNetwork());
+ mEthernetAgent.getNetwork());
// As this callback does not have access to the OEM_PAID network, it will not fire.
defaultNetworkCallback.assertNoCallback();
assertDefaultNetworkCapabilities(userId /* no networks */);
// The other UID does have access, and gets a callback.
- otherUidDefaultCallback.expectAvailableThenValidatedCallbacks(mEthernetNetworkAgent);
+ otherUidDefaultCallback.expectAvailableThenValidatedCallbacks(mEthernetAgent);
// Bring up unrestricted cellular. This should now satisfy the default network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// At this point with an unrestricted network used, the available callback should trigger
// The other UID is unaffected and remains on the paid network.
- defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- assertEquals(defaultNetworkCallback.getLastAvailableNetwork(),
- mCellNetworkAgent.getNetwork());
- assertDefaultNetworkCapabilities(userId, mCellNetworkAgent);
+ defaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ assertEquals(defaultNetworkCallback.getLastAvailableNetwork(), mCellAgent.getNetwork());
+ assertDefaultNetworkCapabilities(userId, mCellAgent);
otherUidDefaultCallback.assertNoCallback();
// Now bring down the per-app network.
@@ -13540,13 +13524,13 @@
// Since the callback didn't use the per-app network, only the other UID gets a callback.
// Because the preference specifies no fallback, it does not switch to cellular.
defaultNetworkCallback.assertNoCallback();
- otherUidDefaultCallback.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
+ otherUidDefaultCallback.expect(LOST, mEthernetAgent);
// Now bring down the default network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, false);
// As this callback was tracking the default, this should now trigger.
- defaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ defaultNetworkCallback.expect(LOST, mCellAgent);
otherUidDefaultCallback.assertNoCallback();
// Confirm we can unregister without issues.
@@ -13699,22 +13683,22 @@
// Bring up metered cellular. This will satisfy the fallback network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
// Bring up ethernet with OEM_PAID. This will satisfy NET_CAPABILITY_OEM_PAID.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mEthernetNetworkAgent.getNetwork().netId, 1 /* times */,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mEthernetAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
// Bring up unmetered Wi-Fi. This will satisfy NET_CAPABILITY_NOT_METERED.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mWiFiNetworkAgent.getNetwork().netId, 1 /* times */,
- mEthernetNetworkAgent.getNetwork().netId, 1 /* times */,
+ mWiFiAgent.getNetwork().netId, 1 /* times */,
+ mEthernetAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
// Disconnecting OEM_PAID should have no effect as it is lower in priority then unmetered.
@@ -13728,15 +13712,15 @@
// Disconnecting unmetered should put PANS on lowest priority fallback request.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
- mWiFiNetworkAgent.getNetwork().netId, 0 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
+ mWiFiAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
// Disconnecting the fallback network should result in no connectivity.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
OEM_PREF_ANY_NET_ID, 0 /* times */,
- mCellNetworkAgent.getNetwork().netId, 0 /* times */,
+ mCellAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
}
@@ -13774,22 +13758,22 @@
// Bring up ethernet with OEM_PAID. This will satisfy NET_CAPABILITY_OEM_PAID.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mEthernetNetworkAgent.getNetwork().netId, 1 /* times */,
+ mEthernetAgent.getNetwork().netId, 1 /* times */,
mService.mNoServiceNetwork.network.getNetId(), 1 /* times */,
false /* shouldDestroyNetwork */);
// Bring up unmetered Wi-Fi. This will satisfy NET_CAPABILITY_NOT_METERED.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mWiFiNetworkAgent.getNetwork().netId, 1 /* times */,
- mEthernetNetworkAgent.getNetwork().netId, 1 /* times */,
+ mWiFiAgent.getNetwork().netId, 1 /* times */,
+ mEthernetAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
// Disconnecting unmetered should put PANS on OEM_PAID.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mEthernetNetworkAgent.getNetwork().netId, 1 /* times */,
- mWiFiNetworkAgent.getNetwork().netId, 0 /* times */,
+ mEthernetAgent.getNetwork().netId, 1 /* times */,
+ mWiFiAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
// Disconnecting OEM_PAID should result in no connectivity.
@@ -13797,7 +13781,7 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
mService.mNoServiceNetwork.network.getNetId(), 1 /* times */,
- mEthernetNetworkAgent.getNetwork().netId, 0 /* times */,
+ mEthernetAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
}
@@ -13841,7 +13825,7 @@
// Bring up ethernet with OEM_PAID. This will satisfy NET_CAPABILITY_OEM_PAID.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mEthernetNetworkAgent.getNetwork().netId, 1 /* times */,
+ mEthernetAgent.getNetwork().netId, 1 /* times */,
mService.mNoServiceNetwork.network.getNetId(), 1 /* times */,
false /* shouldDestroyNetwork */);
@@ -13849,7 +13833,7 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
mService.mNoServiceNetwork.network.getNetId(), 1 /* times */,
- mEthernetNetworkAgent.getNetwork().netId, 0 /* times */,
+ mEthernetAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
}
@@ -13893,7 +13877,7 @@
// Bring up ethernet with OEM_PRIVATE. This will satisfy NET_CAPABILITY_OEM_PRIVATE.
startOemManagedNetwork(false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mEthernetNetworkAgent.getNetwork().netId, 1 /* times */,
+ mEthernetAgent.getNetwork().netId, 1 /* times */,
mService.mNoServiceNetwork.network.getNetId(), 1 /* times */,
false /* shouldDestroyNetwork */);
@@ -13901,7 +13885,7 @@
stopOemManagedNetwork();
verifySetOemNetworkPreferenceForPreference(uidRanges,
mService.mNoServiceNetwork.network.getNetId(), 1 /* times */,
- mEthernetNetworkAgent.getNetwork().netId, 0 /* times */,
+ mEthernetAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
}
@@ -13934,7 +13918,7 @@
// Test that we correctly add the expected values for multiple users.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
@@ -13942,7 +13926,7 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
OEM_PREF_ANY_NET_ID, 0 /* times */,
- mCellNetworkAgent.getNetwork().netId, 0 /* times */,
+ mCellAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
}
@@ -13977,7 +13961,7 @@
// Test that we correctly add the expected values for multiple users.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifySetOemNetworkPreferenceForPreference(uidRangesSingleUser,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
@@ -13990,8 +13974,8 @@
// Test that we correctly add values for all users and remove for the single user.
verifySetOemNetworkPreferenceForPreference(uidRangesBothUsers, uidRangesSingleUser,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
// Send a broadcast indicating a user was removed.
@@ -14002,8 +13986,8 @@
// Test that we correctly add values for the single user and remove for the all users.
verifySetOemNetworkPreferenceForPreference(uidRangesSingleUser, uidRangesBothUsers,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
}
@@ -14033,7 +14017,7 @@
// Test that we correctly add the expected values for installed packages.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifySetOemNetworkPreferenceForPreference(uidRangesSinglePackage,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
@@ -14049,8 +14033,8 @@
// Test the single package is removed and the combined packages are added.
verifySetOemNetworkPreferenceForPreference(uidRangesAllPackages, uidRangesSinglePackage,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
// Set the system to no longer recognize the package to be installed
@@ -14063,8 +14047,8 @@
// Test the combined packages are removed and the single package is added.
verifySetOemNetworkPreferenceForPreference(uidRangesSinglePackage, uidRangesAllPackages,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
// Set the system to change the installed package's uid
@@ -14080,8 +14064,8 @@
// Test the original uid is removed and is replaced with the new uid.
verifySetOemNetworkPreferenceForPreference(uidRangesReplacedPackage, uidRangesSinglePackage,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
- mCellNetworkAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
+ mCellAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
}
@@ -14106,32 +14090,32 @@
// Bring up metered cellular. This will satisfy the fallback network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mCellNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mCellAgent.getNetwork());
// Bring up ethernet with OEM_PAID. This will satisfy NET_CAPABILITY_OEM_PAID.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Bring up unmetered Wi-Fi. This will satisfy NET_CAPABILITY_NOT_METERED.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mWiFiNetworkAgent.getNetwork(),
- mWiFiNetworkAgent.getNetwork());
+ mWiFiAgent.getNetwork(),
+ mWiFiAgent.getNetwork());
// Disconnecting unmetered Wi-Fi will put the pref on OEM_PAID and fallback on cellular.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Disconnecting cellular should keep OEM network on OEM_PAID and fallback will be null.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
null,
- mEthernetNetworkAgent.getNetwork());
+ mEthernetAgent.getNetwork());
// Disconnecting OEM_PAID will put both on null as it is the last network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, false);
@@ -14179,8 +14163,8 @@
oemPaidFactory.register();
oemPaidFactory.expectRequestAdd(); // Because nobody satisfies the request
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
// A network connected that satisfies the default internet request. For the OEM_PAID
// preference, this is not as good as an OEM_PAID network, so even if the score of
@@ -14215,7 +14199,7 @@
expectNoRequestChanged(internetFactory);
internetFactory.assertRequestCountEquals(0);
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
// The network satisfying the default internet request has disconnected, so the
// internetFactory sees the default request again. However there is a network with OEM_PAID
// connected, so the 2nd OEM_PAID req is already satisfied, so the oemPaidFactory doesn't
@@ -14226,8 +14210,8 @@
internetFactory.assertRequestCountEquals(1);
// Cell connects again, still with score 50. Back to the previous state.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
expectNoRequestChanged(oemPaidFactory);
oemPaidFactory.assertRequestCountEquals(1);
internetFactory.expectRequestRemove();
@@ -14239,18 +14223,18 @@
wifiCallback);
// Now WiFi connects and it's unmetered, but it's weaker than cell.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
- mWiFiNetworkAgent.setScore(new NetworkScore.Builder().setLegacyInt(30).setExiting(true)
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+ mWiFiAgent.setScore(new NetworkScore.Builder().setLegacyInt(30).setExiting(true)
.build()); // Not the best Internet network, but unmetered
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent.connect(true);
// The OEM_PAID preference prefers an unmetered network to an OEM_PAID network, so
// the oemPaidFactory can't beat wifi no matter how high its score.
oemPaidFactory.expectRequestRemove();
expectNoRequestChanged(internetFactory);
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
// Now that the best internet network (cell, with its 50 score compared to 30 for WiFi
// at this point), the default internet request is satisfied by a network worse than
// the internetFactory announced, so it gets the request. However, there is still an
@@ -14281,32 +14265,32 @@
// Bring up metered cellular. This will satisfy the fallback network but not the pref.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
+ mCellAgent.getNetwork(),
mService.mNoServiceNetwork.network());
// Bring up ethernet with OEM_PAID. This will satisfy NET_CAPABILITY_OEM_PAID.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Bring up unmetered Wi-Fi. This will satisfy NET_CAPABILITY_NOT_METERED.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mWiFiNetworkAgent.getNetwork(),
- mWiFiNetworkAgent.getNetwork());
+ mWiFiAgent.getNetwork(),
+ mWiFiAgent.getNetwork());
// Disconnecting unmetered Wi-Fi will put the OEM pref on OEM_PAID and fallback on cellular.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Disconnecting cellular should keep OEM network on OEM_PAID and fallback will be null.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
null,
- mEthernetNetworkAgent.getNetwork());
+ mEthernetAgent.getNetwork());
// Disconnecting OEM_PAID puts the fallback on null and the pref on the disconnected net.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, false);
@@ -14339,32 +14323,32 @@
// Bring up metered cellular. This will satisfy the fallback network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
+ mCellAgent.getNetwork(),
mService.mNoServiceNetwork.network());
// Bring up ethernet with OEM_PAID. This will satisfy NET_CAPABILITY_OEM_PAID.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Bring up unmetered Wi-Fi. The OEM network shouldn't change, the fallback will take Wi-Fi.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mWiFiNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mWiFiAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Disconnecting unmetered Wi-Fi shouldn't change the OEM network with fallback on cellular.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Disconnecting OEM_PAID will keep the fallback on cellular and nothing for OEM_PAID.
// OEM_PAID_ONLY not supporting a fallback now uses the disconnected network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_ETHERNET, false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
+ mCellAgent.getNetwork(),
mService.mNoServiceNetwork.network());
// Disconnecting cellular will put the fallback on null and the pref on disconnected.
@@ -14398,32 +14382,32 @@
// Bring up metered cellular. This will satisfy the fallback network.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
+ mCellAgent.getNetwork(),
mService.mNoServiceNetwork.network());
// Bring up ethernet with OEM_PRIVATE. This will satisfy NET_CAPABILITY_OEM_PRIVATE.
startOemManagedNetwork(false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Bring up unmetered Wi-Fi. The OEM network shouldn't change, the fallback will take Wi-Fi.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, true);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mWiFiNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mWiFiAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Disconnecting unmetered Wi-Fi shouldn't change the OEM network with fallback on cellular.
setOemNetworkPreferenceAgentConnected(TRANSPORT_WIFI, false);
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
- mEthernetNetworkAgent.getNetwork());
+ mCellAgent.getNetwork(),
+ mEthernetAgent.getNetwork());
// Disconnecting OEM_PRIVATE will keep the fallback on cellular.
// OEM_PRIVATE_ONLY not supporting a fallback now uses to the disconnected network.
stopOemManagedNetwork();
verifyMultipleDefaultNetworksTracksCorrectly(expectedOemPrefRequestSize,
- mCellNetworkAgent.getNetwork(),
+ mCellAgent.getNetwork(),
mService.mNoServiceNetwork.network());
// Disconnecting cellular will put the fallback on null and pref on disconnected.
@@ -14444,13 +14428,13 @@
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
- mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
- mSystemDefaultNetworkCallback.expectCapabilitiesThat(mCellNetworkAgent, nc ->
+ mCellAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
+ mSystemDefaultNetworkCallback.expectCapabilitiesThat(mCellAgent, nc ->
nc.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED));
- mDefaultNetworkCallback.expectCapabilitiesThat(mCellNetworkAgent, nc ->
+ mDefaultNetworkCallback.expectCapabilitiesThat(mCellAgent, nc ->
nc.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED));
// default callbacks will be unregistered in tearDown
@@ -14505,29 +14489,29 @@
final TestNetworkCallback cellCb = new TestNetworkCallback();
mCm.requestNetwork(new NetworkRequest.Builder().addCapability(NET_CAPABILITY_MMS).build(),
cellCb);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp, cellNcTemplate);
- mCellNetworkAgent.connect(true);
- cellCb.expectAvailableCallbacksUnvalidated(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp, cellNcTemplate);
+ mCellAgent.connect(true);
+ cellCb.expectAvailableCallbacksUnvalidated(mCellAgent);
List<NetworkStateSnapshot> snapshots = mCm.getAllNetworkStateSnapshots();
assertLength(1, snapshots);
// Compose the expected cellular snapshot for verification.
final NetworkCapabilities cellNc =
- mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork());
+ mCm.getNetworkCapabilities(mCellAgent.getNetwork());
final NetworkStateSnapshot cellSnapshot = new NetworkStateSnapshot(
- mCellNetworkAgent.getNetwork(), cellNc, cellLp,
+ mCellAgent.getNetwork(), cellNc, cellLp,
null, ConnectivityManager.TYPE_MOBILE);
assertEquals(cellSnapshot, snapshots.get(0));
// Connect wifi and verify the snapshots.
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
waitForIdle();
// Compose the expected wifi snapshot for verification.
final NetworkCapabilities wifiNc =
- mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork());
+ mCm.getNetworkCapabilities(mWiFiAgent.getNetwork());
final NetworkStateSnapshot wifiSnapshot = new NetworkStateSnapshot(
- mWiFiNetworkAgent.getNetwork(), wifiNc, new LinkProperties(), null,
+ mWiFiAgent.getNetwork(), wifiNc, new LinkProperties(), null,
ConnectivityManager.TYPE_WIFI);
snapshots = mCm.getAllNetworkStateSnapshots();
@@ -14535,33 +14519,33 @@
assertContainsAll(snapshots, cellSnapshot, wifiSnapshot);
// Set cellular as suspended, verify the snapshots will contain suspended networks.
- mCellNetworkAgent.suspend();
+ mCellAgent.suspend();
waitForIdle();
final NetworkCapabilities cellSuspendedNc =
- mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork());
+ mCm.getNetworkCapabilities(mCellAgent.getNetwork());
assertFalse(cellSuspendedNc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
final NetworkStateSnapshot cellSuspendedSnapshot = new NetworkStateSnapshot(
- mCellNetworkAgent.getNetwork(), cellSuspendedNc, cellLp,
+ mCellAgent.getNetwork(), cellSuspendedNc, cellLp,
null, ConnectivityManager.TYPE_MOBILE);
snapshots = mCm.getAllNetworkStateSnapshots();
assertLength(2, snapshots);
assertContainsAll(snapshots, cellSuspendedSnapshot, wifiSnapshot);
// Disconnect wifi, verify the snapshots contain only cellular.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
waitForIdle();
snapshots = mCm.getAllNetworkStateSnapshots();
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetwork());
assertLength(1, snapshots);
assertEquals(cellSuspendedSnapshot, snapshots.get(0));
- mCellNetworkAgent.resume();
+ mCellAgent.resume();
waitForIdle();
snapshots = mCm.getAllNetworkStateSnapshots();
assertLength(1, snapshots);
assertEquals(cellSnapshot, snapshots.get(0));
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
waitForIdle();
verifyNoNetwork();
mCm.unregisterNetworkCallback(cellCb);
@@ -14632,35 +14616,35 @@
new NetworkRequest.Builder().addCapability(NET_CAPABILITY_TRUSTED).build(),
bestMatchingCb, mCsHandlerThread.getThreadHandler());
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- bestMatchingCb.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ bestMatchingCb.expectAvailableThenValidatedCallbacks(mCellAgent);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- bestMatchingCb.expectAvailableDoubleValidatedCallbacks(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ bestMatchingCb.expectAvailableDoubleValidatedCallbacks(mWiFiAgent);
// Change something on cellular to trigger capabilities changed, since the callback
// only cares about the best network, verify it received nothing from cellular.
- mCellNetworkAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
+ mCellAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
bestMatchingCb.assertNoCallback();
// Make cellular the best network again, verify the callback now tracks cellular.
- mWiFiNetworkAgent.adjustScore(-50);
- bestMatchingCb.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mWiFiAgent.adjustScore(-50);
+ bestMatchingCb.expectAvailableCallbacksValidated(mCellAgent);
// Make cellular temporary non-trusted, which will not satisfying the request.
// Verify the callback switch from/to the other network accordingly.
- mCellNetworkAgent.removeCapability(NET_CAPABILITY_TRUSTED);
- bestMatchingCb.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- mCellNetworkAgent.addCapability(NET_CAPABILITY_TRUSTED);
- bestMatchingCb.expectAvailableDoubleValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent.removeCapability(NET_CAPABILITY_TRUSTED);
+ bestMatchingCb.expectAvailableCallbacksValidated(mWiFiAgent);
+ mCellAgent.addCapability(NET_CAPABILITY_TRUSTED);
+ bestMatchingCb.expectAvailableDoubleValidatedCallbacks(mCellAgent);
// Verify the callback doesn't care about wifi disconnect.
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
bestMatchingCb.assertNoCallback();
- mCellNetworkAgent.disconnect();
- bestMatchingCb.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ bestMatchingCb.expect(LOST, mCellAgent);
}
private UidRangeParcel[] uidRangeFor(final UserHandle handle) {
@@ -14757,18 +14741,17 @@
TestNetworkCallback disAllowProfileDefaultNetworkCallback) throws Exception {
final InOrder inOrder = inOrder(mMockNetd);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
- mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- profileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ profileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
if (disAllowProfileDefaultNetworkCallback != null) {
- disAllowProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(
- mCellNetworkAgent);
+ disAllowProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
}
inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
- mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
+ mCellAgent.getNetwork().netId, INetd.PERMISSION_NONE));
final TestNetworkAgentWrapper workAgent =
makeEnterpriseNetworkAgent(profileNetworkPreference.getPreferenceEnterpriseId());
@@ -14795,7 +14778,7 @@
// system default is not handled specially, the rules are always active as long as
// a preference is set.
inOrder.verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreference),
PREFERENCE_ORDER_PROFILE));
}
@@ -14805,7 +14788,7 @@
if (allowFallback && !connectWorkProfileAgentAhead) {
assertNoCallbacks(profileDefaultNetworkCallback);
} else if (!connectWorkProfileAgentAhead) {
- profileDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ profileDefaultNetworkCallback.expect(LOST, mCellAgent);
if (disAllowProfileDefaultNetworkCallback != null) {
assertNoCallbacks(disAllowProfileDefaultNetworkCallback);
}
@@ -14830,7 +14813,7 @@
if (allowFallback && !connectWorkProfileAgentAhead) {
inOrder.verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreference),
PREFERENCE_ORDER_PROFILE));
}
@@ -14860,13 +14843,13 @@
// Conversely, change a capability on the system-wide default network and make sure
// that only the apps outside of the work profile receive the callbacks.
- mCellNetworkAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
- mSystemDefaultNetworkCallback.expectCapabilitiesThat(mCellNetworkAgent, nc ->
+ mCellAgent.addCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
+ mSystemDefaultNetworkCallback.expectCapabilitiesThat(mCellAgent, nc ->
nc.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED));
- mDefaultNetworkCallback.expectCapabilitiesThat(mCellNetworkAgent, nc ->
+ mDefaultNetworkCallback.expectCapabilitiesThat(mCellAgent, nc ->
nc.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED));
if (disAllowProfileDefaultNetworkCallback != null) {
- disAllowProfileDefaultNetworkCallback.expectCapabilitiesThat(mCellNetworkAgent, nc ->
+ disAllowProfileDefaultNetworkCallback.expectCapabilitiesThat(mCellAgent, nc ->
nc.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED));
}
profileDefaultNetworkCallback.assertNoCallback();
@@ -14874,35 +14857,33 @@
// Disconnect and reconnect the system-wide default network and make sure that the
// apps on this network see the appropriate callbacks, and the app on the work profile
// doesn't because it continues to use the enterprise network.
- mCellNetworkAgent.disconnect();
- mSystemDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- mDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ mSystemDefaultNetworkCallback.expect(LOST, mCellAgent);
+ mDefaultNetworkCallback.expect(LOST, mCellAgent);
if (disAllowProfileDefaultNetworkCallback != null) {
- disAllowProfileDefaultNetworkCallback.expect(
- CallbackEntry.LOST, mCellNetworkAgent);
+ disAllowProfileDefaultNetworkCallback.expect(LOST, mCellAgent);
}
profileDefaultNetworkCallback.assertNoCallback();
- inOrder.verify(mMockNetd).networkDestroy(mCellNetworkAgent.getNetwork().netId);
+ inOrder.verify(mMockNetd).networkDestroy(mCellAgent.getNetwork().netId);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
if (disAllowProfileDefaultNetworkCallback != null) {
- disAllowProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(
- mCellNetworkAgent);
+ disAllowProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
}
profileDefaultNetworkCallback.assertNoCallback();
inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
- mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
+ mCellAgent.getNetwork().netId, INetd.PERMISSION_NONE));
// When the agent disconnects, test that the app on the work profile falls back to the
// default network.
workAgent.disconnect();
- profileDefaultNetworkCallback.expect(CallbackEntry.LOST, workAgent);
+ profileDefaultNetworkCallback.expect(LOST, workAgent);
if (allowFallback) {
- profileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ profileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
if (disAllowProfileDefaultNetworkCallback != null) {
assertNoCallbacks(disAllowProfileDefaultNetworkCallback);
}
@@ -14910,27 +14891,26 @@
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback);
if (allowFallback) {
inOrder.verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreference),
PREFERENCE_ORDER_PROFILE));
}
inOrder.verify(mMockNetd).networkDestroy(workAgent.getNetwork().netId);
- mCellNetworkAgent.disconnect();
- mSystemDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- mDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ mSystemDefaultNetworkCallback.expect(LOST, mCellAgent);
+ mDefaultNetworkCallback.expect(LOST, mCellAgent);
if (disAllowProfileDefaultNetworkCallback != null) {
- disAllowProfileDefaultNetworkCallback.expect(
- CallbackEntry.LOST, mCellNetworkAgent);
+ disAllowProfileDefaultNetworkCallback.expect(LOST, mCellAgent);
}
if (allowFallback) {
- profileDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ profileDefaultNetworkCallback.expect(LOST, mCellAgent);
}
// Waiting for the handler to be idle before checking for networkDestroy is necessary
// here because ConnectivityService calls onLost before the network is fully torn down.
waitForIdle();
- inOrder.verify(mMockNetd).networkDestroy(mCellNetworkAgent.getNetwork().netId);
+ inOrder.verify(mMockNetd).networkDestroy(mCellAgent.getNetwork().netId);
// If the control comes here, callbacks seem to behave correctly in the presence of
// a default network when the enterprise network goes up and down. Now, make sure they
@@ -14967,7 +14947,7 @@
// When the agent disconnects, test that the app on the work profile fall back to the
// default network.
workAgent2.disconnect();
- profileDefaultNetworkCallback.expect(CallbackEntry.LOST, workAgent2);
+ profileDefaultNetworkCallback.expect(LOST, workAgent2);
if (disAllowProfileDefaultNetworkCallback != null) {
assertNoCallbacks(disAllowProfileDefaultNetworkCallback);
}
@@ -15313,23 +15293,23 @@
registerDefaultNetworkCallbackAsUid(appCb3, testWorkProfileAppUid3);
// Connect both a regular cell agent and an enterprise network first.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
final TestNetworkAgentWrapper workAgent1 = makeEnterpriseNetworkAgent(NET_ENTERPRISE_ID_1);
final TestNetworkAgentWrapper workAgent2 = makeEnterpriseNetworkAgent(NET_ENTERPRISE_ID_2);
workAgent1.connect(true);
workAgent2.connect(true);
- mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
- appCb1.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- appCb2.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- appCb3.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ appCb1.expectAvailableThenValidatedCallbacks(mCellAgent);
+ appCb2.expectAvailableThenValidatedCallbacks(mCellAgent);
+ appCb3.expectAvailableThenValidatedCallbacks(mCellAgent);
verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
- mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
+ mCellAgent.getNetwork().netId, INetd.PERMISSION_NONE));
verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
workAgent1.getNetwork().netId, INetd.PERMISSION_SYSTEM));
verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
@@ -15396,8 +15376,8 @@
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback);
appCb3.expectAvailableCallbacksValidated(workAgent1);
- appCb2.expectAvailableCallbacksValidated(mCellNetworkAgent);
- appCb1.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ appCb2.expectAvailableCallbacksValidated(mCellAgent);
+ appCb1.expectAvailableCallbacksValidated(mCellAgent);
// Set the preferences for testHandle to default.
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
@@ -15414,9 +15394,9 @@
PREFERENCE_ORDER_PROFILE));
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback, appCb1, appCb2);
- appCb3.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ appCb3.expectAvailableCallbacksValidated(mCellAgent);
workAgent2.disconnect();
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
mCm.unregisterNetworkCallback(appCb1);
mCm.unregisterNetworkCallback(appCb2);
@@ -15461,8 +15441,8 @@
registerDefaultNetworkCallbackAsUid(appCb5, testWorkProfileAppUid5);
// Connect both a regular cell agent and an enterprise network first.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
final TestNetworkAgentWrapper workAgent1 = makeEnterpriseNetworkAgent(NET_ENTERPRISE_ID_1);
final TestNetworkAgentWrapper workAgent2 = makeEnterpriseNetworkAgent(NET_ENTERPRISE_ID_2);
@@ -15476,16 +15456,16 @@
workAgent4.connect(true);
workAgent5.connect(true);
- mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- appCb1.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- appCb2.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- appCb3.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- appCb4.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- appCb5.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ appCb1.expectAvailableThenValidatedCallbacks(mCellAgent);
+ appCb2.expectAvailableThenValidatedCallbacks(mCellAgent);
+ appCb3.expectAvailableThenValidatedCallbacks(mCellAgent);
+ appCb4.expectAvailableThenValidatedCallbacks(mCellAgent);
+ appCb5.expectAvailableThenValidatedCallbacks(mCellAgent);
verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
- mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
+ mCellAgent.getNetwork().netId, INetd.PERMISSION_NONE));
verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
workAgent1.getNetwork().netId, INetd.PERMISSION_SYSTEM));
verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
@@ -15577,36 +15557,36 @@
workAgent4.disconnect();
workAgent5.disconnect();
- appCb1.expect(CallbackEntry.LOST, workAgent1);
- appCb2.expect(CallbackEntry.LOST, workAgent2);
- appCb3.expect(CallbackEntry.LOST, workAgent3);
- appCb4.expect(CallbackEntry.LOST, workAgent4);
- appCb5.expect(CallbackEntry.LOST, workAgent5);
+ appCb1.expect(LOST, workAgent1);
+ appCb2.expect(LOST, workAgent2);
+ appCb3.expect(LOST, workAgent3);
+ appCb4.expect(LOST, workAgent4);
+ appCb5.expect(LOST, workAgent5);
- appCb1.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ appCb1.expectAvailableCallbacksValidated(mCellAgent);
appCb2.assertNoCallback();
- appCb3.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ appCb3.expectAvailableCallbacksValidated(mCellAgent);
appCb4.assertNoCallback();
- appCb5.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ appCb5.expectAvailableCallbacksValidated(mCellAgent);
verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder1.build()),
PREFERENCE_ORDER_PROFILE));
verify(mMockNetd, never()).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder2.build()),
PREFERENCE_ORDER_PROFILE));
verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder3.build()),
PREFERENCE_ORDER_PROFILE));
verify(mMockNetd, never()).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder4.build()),
PREFERENCE_ORDER_PROFILE));
verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId,
+ mCellAgent.getNetwork().netId,
uidRangeFor(testHandle, profileNetworkPreferenceBuilder5.build()),
PREFERENCE_ORDER_PROFILE));
@@ -15624,9 +15604,9 @@
listener.expectOnComplete();
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback, appCb1, appCb3,
appCb5);
- appCb2.expectAvailableCallbacksValidated(mCellNetworkAgent);
- appCb4.expectAvailableCallbacksValidated(mCellNetworkAgent);
- mCellNetworkAgent.disconnect();
+ appCb2.expectAvailableCallbacksValidated(mCellAgent);
+ appCb4.expectAvailableCallbacksValidated(mCellAgent);
+ mCellAgent.disconnect();
mCm.unregisterNetworkCallback(appCb1);
mCm.unregisterNetworkCallback(appCb2);
@@ -15646,8 +15626,8 @@
final UserHandle testHandle = setupEnterpriseNetwork();
// Connect both a regular cell agent and an enterprise network first.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
final TestNetworkAgentWrapper workAgent = makeEnterpriseNetworkAgent();
workAgent.connect(true);
@@ -15657,27 +15637,27 @@
r -> r.run(), listener);
listener.expectOnComplete();
inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
- mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
+ mCellAgent.getNetwork().netId, INetd.PERMISSION_NONE));
inOrder.verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
workAgent.getNetwork().netId, uidRangeFor(testHandle), PREFERENCE_ORDER_PROFILE));
registerDefaultNetworkCallbacks();
- mSystemDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mSystemDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(workAgent);
mCm.setProfileNetworkPreference(testHandle, PROFILE_NETWORK_PREFERENCE_DEFAULT,
r -> r.run(), listener);
listener.expectOnComplete();
- mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback);
inOrder.verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig(
workAgent.getNetwork().netId, uidRangeFor(testHandle), PREFERENCE_ORDER_PROFILE));
workAgent.disconnect();
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
// Callbacks will be unregistered by tearDown()
}
@@ -15699,18 +15679,18 @@
registerDefaultNetworkCallbackAsUid(app4Cb, testWorkProfileAppUid4);
// Connect both a regular cell agent and an enterprise network first.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
final TestNetworkAgentWrapper workAgent = makeEnterpriseNetworkAgent();
workAgent.connect(true);
- mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- app4Cb.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mSystemDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ app4Cb.expectAvailableThenValidatedCallbacks(mCellAgent);
inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
- mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
+ mCellAgent.getNetwork().netId, INetd.PERMISSION_NONE));
inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
workAgent.getNetwork().netId, INetd.PERMISSION_SYSTEM));
@@ -15741,12 +15721,12 @@
inOrder.verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig(
workAgent.getNetwork().netId, uidRangeFor(testHandle2), PREFERENCE_ORDER_PROFILE));
- mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
assertNoCallbacks(mSystemDefaultNetworkCallback, mDefaultNetworkCallback,
app4Cb);
workAgent.disconnect();
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
mCm.unregisterNetworkCallback(app4Cb);
// Other callbacks will be unregistered by tearDown()
@@ -15757,17 +15737,17 @@
final InOrder inOrder = inOrder(mMockNetd);
final UserHandle testHandle = setupEnterpriseNetwork();
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
final TestOnCompleteListener listener = new TestOnCompleteListener();
mCm.setProfileNetworkPreference(testHandle, PROFILE_NETWORK_PREFERENCE_ENTERPRISE,
r -> r.run(), listener);
listener.expectOnComplete();
inOrder.verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
- mCellNetworkAgent.getNetwork().netId, INetd.PERMISSION_NONE));
+ mCellAgent.getNetwork().netId, INetd.PERMISSION_NONE));
inOrder.verify(mMockNetd).networkAddUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId, uidRangeFor(testHandle),
+ mCellAgent.getNetwork().netId, uidRangeFor(testHandle),
PREFERENCE_ORDER_PROFILE));
final Intent removedIntent = new Intent(ACTION_USER_REMOVED);
@@ -15775,10 +15755,175 @@
processBroadcast(removedIntent);
inOrder.verify(mMockNetd).networkRemoveUidRangesParcel(new NativeUidRangeConfig(
- mCellNetworkAgent.getNetwork().netId, uidRangeFor(testHandle),
+ mCellAgent.getNetwork().netId, uidRangeFor(testHandle),
PREFERENCE_ORDER_PROFILE));
}
+ @Test
+ public void testProfileNetworkPreferenceBlocking_changePreference() throws Exception {
+ final InOrder inOrder = inOrder(mMockNetd);
+ final UserHandle testHandle = setupEnterpriseNetwork();
+ doReturn(asList(PRIMARY_USER_HANDLE, testHandle))
+ .when(mUserManager).getUserHandles(anyBoolean());
+
+ // Start with 1 default network and 1 enterprise network, both networks should
+ // not be restricted since the blocking preference is not set yet.
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+
+ // Verify uid ranges 0~99999, 200000~299999 are all allowed for cellular.
+ final UidRange profileUidRange =
+ UidRange.createForUser(UserHandle.of(TEST_WORK_PROFILE_USER_ID));
+ ArraySet<UidRange> allowedAllUidRanges = new ArraySet<>();
+ allowedAllUidRanges.add(PRIMARY_UIDRANGE);
+ allowedAllUidRanges.add(profileUidRange);
+ final UidRangeParcel[] allowAllUidRangesParcel = toUidRangeStableParcels(
+ allowedAllUidRanges);
+ final NativeUidRangeConfig cellAllAllowedConfig = new NativeUidRangeConfig(
+ mCellAgent.getNetwork().netId,
+ allowAllUidRangesParcel,
+ 0 /* subPriority */);
+ inOrder.verify(mMockNetd).setNetworkAllowlist(
+ new NativeUidRangeConfig[]{cellAllAllowedConfig});
+
+ // Verify the same uid ranges are also applied for enterprise network.
+ final TestNetworkAgentWrapper enterpriseAgent = makeEnterpriseNetworkAgent(
+ NET_ENTERPRISE_ID_1);
+ enterpriseAgent.connect(true);
+ final NativeUidRangeConfig enterpriseAllAllowedConfig = new NativeUidRangeConfig(
+ enterpriseAgent.getNetwork().netId,
+ allowAllUidRangesParcel,
+ 0 /* subPriority */);
+ // Network agents are stored in an ArraySet which does not guarantee the order and
+ // making the order of the list undeterministic. Thus, verify this in order insensitive way.
+ final ArgumentCaptor<NativeUidRangeConfig[]> configsCaptor = ArgumentCaptor.forClass(
+ NativeUidRangeConfig[].class);
+ inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
+ assertContainsAll(List.of(configsCaptor.getValue()),
+ List.of(cellAllAllowedConfig, enterpriseAllAllowedConfig));
+
+ // Setup profile preference which only applies to test app uid on the managed profile.
+ ProfileNetworkPreference.Builder prefBuilder = new ProfileNetworkPreference.Builder();
+ prefBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING)
+ .setIncludedUids(new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID)})
+ .setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
+ final TestOnCompleteListener listener = new TestOnCompleteListener();
+ mCm.setProfileNetworkPreferences(testHandle,
+ List.of(prefBuilder.build()),
+ r -> r.run(), listener);
+ listener.expectOnComplete();
+
+ // Verify Netd is called for the preferences changed.
+ // Cell: 0~99999, 200000~TEST_APP_UID-1, TEST_APP_UID+1~299999
+ // Enterprise: 0~99999, 200000~299999
+ final ArraySet<UidRange> excludeAppRanges = new ArraySet<>();
+ excludeAppRanges.add(PRIMARY_UIDRANGE);
+ excludeAppRanges.addAll(UidRangeUtils.removeRangeSetFromUidRange(
+ profileUidRange,
+ new ArraySet(new UidRange[]{
+ (new UidRange(TEST_WORK_PROFILE_APP_UID, TEST_WORK_PROFILE_APP_UID))})
+ ));
+ final UidRangeParcel[] excludeAppRangesParcel = toUidRangeStableParcels(excludeAppRanges);
+ final NativeUidRangeConfig cellExcludeAppConfig = new NativeUidRangeConfig(
+ mCellAgent.getNetwork().netId,
+ excludeAppRangesParcel,
+ 0 /* subPriority */);
+ inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
+ assertContainsAll(List.of(configsCaptor.getValue()),
+ List.of(cellExcludeAppConfig, enterpriseAllAllowedConfig));
+
+ // Verify unset by giving all allowed set for all users when the preference got removed.
+ mCm.setProfileNetworkPreference(testHandle, PROFILE_NETWORK_PREFERENCE_ENTERPRISE,
+ r -> r.run(), listener);
+ listener.expectOnComplete();
+ inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
+ assertContainsAll(List.of(configsCaptor.getValue()),
+ List.of(cellAllAllowedConfig, enterpriseAllAllowedConfig));
+
+ // Verify issuing with cellular set only when a network with enterprise capability
+ // disconnects.
+ enterpriseAgent.disconnect();
+ waitForIdle();
+ inOrder.verify(mMockNetd).setNetworkAllowlist(
+ new NativeUidRangeConfig[]{cellAllAllowedConfig});
+ }
+
+ @Test
+ public void testProfileNetworkPreferenceBlocking_networkChanges() throws Exception {
+ final InOrder inOrder = inOrder(mMockNetd);
+ final UserHandle testHandle = setupEnterpriseNetwork();
+ doReturn(asList(PRIMARY_USER_HANDLE, testHandle))
+ .when(mUserManager).getUserHandles(anyBoolean());
+
+ // Setup profile preference which only applies to test app uid on the managed profile.
+ ProfileNetworkPreference.Builder prefBuilder = new ProfileNetworkPreference.Builder();
+ prefBuilder.setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING)
+ .setIncludedUids(new int[]{testHandle.getUid(TEST_WORK_PROFILE_APP_UID)})
+ .setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
+ final TestOnCompleteListener listener = new TestOnCompleteListener();
+ mCm.setProfileNetworkPreferences(testHandle,
+ List.of(prefBuilder.build()),
+ r -> r.run(), listener);
+ listener.expectOnComplete();
+ inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{});
+
+ // Start with 1 default network, which should be restricted since the blocking
+ // preference is already set.
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+
+ // Verify cellular network applies to the allow list.
+ // Cell: 0~99999, 200000~TEST_APP_UID-1, TEST_APP_UID+1~299999
+ // Enterprise: 0~99999, 200000~299999
+ final ArraySet<UidRange> excludeAppRanges = new ArraySet<>();
+ final UidRange profileUidRange =
+ UidRange.createForUser(UserHandle.of(TEST_WORK_PROFILE_USER_ID));
+ excludeAppRanges.add(PRIMARY_UIDRANGE);
+ excludeAppRanges.addAll(UidRangeUtils.removeRangeSetFromUidRange(
+ profileUidRange,
+ new ArraySet(new UidRange[]{
+ (new UidRange(TEST_WORK_PROFILE_APP_UID, TEST_WORK_PROFILE_APP_UID))})
+ ));
+ final UidRangeParcel[] excludeAppRangesParcel = toUidRangeStableParcels(excludeAppRanges);
+ final NativeUidRangeConfig cellExcludeAppConfig = new NativeUidRangeConfig(
+ mCellAgent.getNetwork().netId,
+ excludeAppRangesParcel,
+ 0 /* subPriority */);
+ inOrder.verify(mMockNetd).setNetworkAllowlist(
+ new NativeUidRangeConfig[]{cellExcludeAppConfig});
+
+ // Verify enterprise network is not blocked for test app.
+ final TestNetworkAgentWrapper enterpriseAgent = makeEnterpriseNetworkAgent(
+ NET_ENTERPRISE_ID_1);
+ enterpriseAgent.connect(true);
+ ArraySet<UidRange> allowedAllUidRanges = new ArraySet<>();
+ allowedAllUidRanges.add(PRIMARY_UIDRANGE);
+ allowedAllUidRanges.add(profileUidRange);
+ final UidRangeParcel[] allowAllUidRangesParcel = toUidRangeStableParcels(
+ allowedAllUidRanges);
+ final NativeUidRangeConfig enterpriseAllAllowedConfig = new NativeUidRangeConfig(
+ enterpriseAgent.getNetwork().netId,
+ allowAllUidRangesParcel,
+ 0 /* subPriority */);
+ // Network agents are stored in an ArraySet which does not guarantee the order and
+ // making the order of the list undeterministic. Thus, verify this in order insensitive way.
+ final ArgumentCaptor<NativeUidRangeConfig[]> configsCaptor = ArgumentCaptor.forClass(
+ NativeUidRangeConfig[].class);
+ inOrder.verify(mMockNetd).setNetworkAllowlist(configsCaptor.capture());
+ assertContainsAll(List.of(configsCaptor.getValue()),
+ List.of(enterpriseAllAllowedConfig, cellExcludeAppConfig));
+
+ // Verify issuing with cellular set only when enterprise network disconnects.
+ enterpriseAgent.disconnect();
+ waitForIdle();
+ inOrder.verify(mMockNetd).setNetworkAllowlist(
+ new NativeUidRangeConfig[]{cellExcludeAppConfig});
+
+ mCellAgent.disconnect();
+ waitForIdle();
+ inOrder.verify(mMockNetd).setNetworkAllowlist(new NativeUidRangeConfig[]{});
+ }
+
/**
* Make sure wrong preferences for per-profile default networking are rejected.
*/
@@ -15789,7 +15934,7 @@
ProfileNetworkPreference.Builder profileNetworkPreferenceBuilder =
new ProfileNetworkPreference.Builder();
profileNetworkPreferenceBuilder.setPreference(
- PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK + 1);
+ PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING + 1);
profileNetworkPreferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1);
assertThrows("Should not be able to set an illegal preference",
IllegalArgumentException.class,
@@ -16024,7 +16169,7 @@
.addCapability(NET_CAPABILITY_NOT_VCN_MANAGED)
.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
- mEthernetNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET,
+ mEthernetAgent = new TestNetworkAgentWrapper(TRANSPORT_ETHERNET,
new LinkProperties(), ncb.build());
final ArraySet<Integer> serviceUidSet = new ArraySet<>();
@@ -16036,22 +16181,22 @@
.addTransportType(TRANSPORT_ETHERNET)
.removeCapability(NET_CAPABILITY_NOT_RESTRICTED)
.build(), cb);
- mEthernetNetworkAgent.connect(true);
- cb.expectAvailableThenValidatedCallbacks(mEthernetNetworkAgent);
+ mEthernetAgent.connect(true);
+ cb.expectAvailableThenValidatedCallbacks(mEthernetAgent);
// Cell gets to set the service UID as access UID
ncb.setAllowedUids(serviceUidSet);
- mEthernetNetworkAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
+ mEthernetAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
if (SdkLevel.isAtLeastT() && hasAutomotiveFeature) {
- cb.expectCapabilitiesThat(mEthernetNetworkAgent,
+ cb.expectCapabilitiesThat(mEthernetAgent,
caps -> caps.getAllowedUids().equals(serviceUidSet));
} else {
// S and no automotive feature must ignore access UIDs.
cb.assertNoCallback(TEST_CALLBACK_TIMEOUT_MS);
}
- mEthernetNetworkAgent.disconnect();
- cb.expect(CallbackEntry.LOST, mEthernetNetworkAgent);
+ mEthernetAgent.disconnect();
+ cb.expect(LOST, mEthernetAgent);
mCm.unregisterNetworkCallback(cb);
}
@@ -16074,7 +16219,7 @@
.removeCapability(NET_CAPABILITY_NOT_RESTRICTED)
.setNetworkSpecifier(new TelephonyNetworkSpecifier(1 /* subid */));
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR,
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR,
new LinkProperties(), ncb.build());
final ArraySet<Integer> serviceUidSet = new ArraySet<>();
@@ -16092,13 +16237,12 @@
.addTransportType(TRANSPORT_CELLULAR)
.removeCapability(NET_CAPABILITY_NOT_RESTRICTED)
.build(), cb);
- mCellNetworkAgent.connect(true);
- cb.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent.connect(true);
+ cb.expectAvailableThenValidatedCallbacks(mCellAgent);
ncb.setAllowedUids(serviceUidSet);
- mCellNetworkAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
+ mCellAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
if (SdkLevel.isAtLeastT()) {
- cb.expectCapabilitiesThat(mCellNetworkAgent,
- caps -> caps.getAllowedUids().equals(serviceUidSet));
+ cb.expectCapabilitiesThat(mCellAgent, cp -> cp.getAllowedUids().equals(serviceUidSet));
} else {
// S must ignore access UIDs.
cb.assertNoCallback(TEST_CALLBACK_TIMEOUT_MS);
@@ -16106,10 +16250,9 @@
// ...but not to some other UID. Rejection sets UIDs to the empty set
ncb.setAllowedUids(nonServiceUidSet);
- mCellNetworkAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
+ mCellAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
if (SdkLevel.isAtLeastT()) {
- cb.expectCapabilitiesThat(mCellNetworkAgent,
- caps -> caps.getAllowedUids().isEmpty());
+ cb.expectCapabilitiesThat(mCellAgent, cp -> cp.getAllowedUids().isEmpty());
} else {
// S must ignore access UIDs.
cb.assertNoCallback(TEST_CALLBACK_TIMEOUT_MS);
@@ -16117,11 +16260,11 @@
// ...and also not to multiple UIDs even including the service UID
ncb.setAllowedUids(serviceUidSetPlus);
- mCellNetworkAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
+ mCellAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
cb.assertNoCallback(TEST_CALLBACK_TIMEOUT_MS);
- mCellNetworkAgent.disconnect();
- cb.expect(CallbackEntry.LOST, mCellNetworkAgent);
+ mCellAgent.disconnect();
+ cb.expect(LOST, mCellAgent);
mCm.unregisterNetworkCallback(cb);
// Must be unset before touching the transports, because remove and add transport types
@@ -16135,12 +16278,11 @@
.addTransportType(TRANSPORT_WIFI)
.removeCapability(NET_CAPABILITY_NOT_RESTRICTED)
.build(), cb);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI,
- new LinkProperties(), ncb.build());
- mWiFiNetworkAgent.connect(true);
- cb.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, new LinkProperties(), ncb.build());
+ mWiFiAgent.connect(true);
+ cb.expectAvailableThenValidatedCallbacks(mWiFiAgent);
ncb.setAllowedUids(serviceUidSet);
- mWiFiNetworkAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
+ mWiFiAgent.setNetworkCapabilities(ncb.build(), true /* sendToCS */);
cb.assertNoCallback(TEST_CALLBACK_TIMEOUT_MS);
mCm.unregisterNetworkCallback(cb);
}
@@ -16211,7 +16353,7 @@
final NetworkCallback[] callbacks = new NetworkCallback[remainingCount];
doAsUid(otherAppUid, () -> {
for (int i = 0; i < remainingCount; ++i) {
- callbacks[i] = new TestableNetworkCallback();
+ callbacks[i] = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(callbacks[i]);
}
});
@@ -16327,12 +16469,12 @@
public void testMobileDataPreferredUidsChanged() throws Exception {
final InOrder inorder = inOrder(mMockNetd);
registerDefaultNetworkCallbacks();
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
- final int cellNetId = mCellNetworkAgent.getNetwork().netId;
+ final int cellNetId = mCellAgent.getNetwork().netId;
inorder.verify(mMockNetd, times(1)).networkCreate(nativeNetworkConfigPhysical(
cellNetId, INetd.PERMISSION_NONE));
@@ -16385,13 +16527,13 @@
cellNetworkCallback.assertNoCallback();
registerDefaultNetworkCallbacks();
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
- final int wifiNetId = mWiFiNetworkAgent.getNetwork().netId;
+ final int wifiNetId = mWiFiAgent.getNetwork().netId;
inorder.verify(mMockNetd, times(1)).networkCreate(nativeNetworkConfigPhysical(
wifiNetId, INetd.PERMISSION_NONE));
@@ -16412,14 +16554,14 @@
// Cellular network connected. mTestPackageDefaultNetworkCallback should receive
// callback with cellular network and net id and uid ranges should be updated to netd.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
mDefaultNetworkCallback.assertNoCallback();
- mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
- final int cellNetId = mCellNetworkAgent.getNetwork().netId;
+ final int cellNetId = mCellAgent.getNetwork().netId;
final NativeUidRangeConfig cellConfig = new NativeUidRangeConfig(cellNetId, uidRanges,
PREFERENCE_ORDER_MOBILE_DATA_PREFERERRED);
inorder.verify(mMockNetd, times(1)).networkCreate(nativeNetworkConfigPhysical(
@@ -16429,26 +16571,26 @@
// Cellular network disconnected. mTestPackageDefaultNetworkCallback should receive
// callback with wifi network from fallback request.
- mCellNetworkAgent.disconnect();
+ mCellAgent.disconnect();
mDefaultNetworkCallback.assertNoCallback();
- cellNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- mTestPackageDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- mTestPackageDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ cellNetworkCallback.expect(LOST, mCellAgent);
+ mTestPackageDefaultNetworkCallback.expect(LOST, mCellAgent);
+ mTestPackageDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
inorder.verify(mMockNetd, times(1)).networkAddUidRangesParcel(wifiConfig);
inorder.verify(mMockNetd, never()).networkRemoveUidRangesParcel(any());
inorder.verify(mMockNetd).networkDestroy(cellNetId);
// Cellular network comes back. mTestPackageDefaultNetworkCallback should receive
// callback with cellular network.
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
mDefaultNetworkCallback.assertNoCallback();
- mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
- final int cellNetId2 = mCellNetworkAgent.getNetwork().netId;
+ final int cellNetId2 = mCellAgent.getNetwork().netId;
final NativeUidRangeConfig cellConfig2 = new NativeUidRangeConfig(cellNetId2, uidRanges,
PREFERENCE_ORDER_MOBILE_DATA_PREFERERRED);
inorder.verify(mMockNetd, times(1)).networkCreate(nativeNetworkConfigPhysical(
@@ -16458,11 +16600,11 @@
// Wifi network disconnected. mTestPackageDefaultNetworkCallback should not receive
// any callback.
- mWiFiNetworkAgent.disconnect();
- mDefaultNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mWiFiAgent.disconnect();
+ mDefaultNetworkCallback.expect(LOST, mWiFiAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
mTestPackageDefaultNetworkCallback.assertNoCallback();
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
waitForIdle();
inorder.verify(mMockNetd, never()).networkAddUidRangesParcel(any());
inorder.verify(mMockNetd, never()).networkRemoveUidRangesParcel(any());
@@ -16494,15 +16636,15 @@
cellFactory.expectRequestAdds(2);
cellFactory.assertRequestCountEquals(2);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
// The cellFactory however is outscored, and should lose default internet request.
// But it should still see mobile data preferred request.
cellFactory.expectRequestRemove();
cellFactory.assertRequestCountEquals(1);
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
// The network satisfying the default internet request has disconnected, so the
// cellFactory sees the default internet requests again.
cellFactory.expectRequestAdd();
@@ -16544,7 +16686,7 @@
final UserHandle testHandle = setupEnterpriseNetwork();
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
- final int cellNetId = mCellNetworkAgent.getNetwork().netId;
+ final int cellNetId = mCellAgent.getNetwork().netId;
inorder.verify(mMockNetd, times(1)).networkCreate(nativeNetworkConfigPhysical(
cellNetId, INetd.PERMISSION_NONE));
@@ -16617,28 +16759,28 @@
// Register callbacks and have wifi network as default network.
registerDefaultNetworkCallbacks();
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.connect(true);
- mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- mProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(),
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.connect(true);
+ mDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ mProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(),
mCm.getActiveNetworkForUid(TEST_WORK_PROFILE_APP_UID));
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
// Set MOBILE_DATA_PREFERRED_UIDS setting with TEST_WORK_PROFILE_APP_UID and
// TEST_PACKAGE_UID. Both mProfileDefaultNetworkCallback and
// mTestPackageDefaultNetworkCallback should receive callback with cell network.
setAndUpdateMobileDataPreferredUids(Set.of(TEST_WORK_PROFILE_APP_UID, TEST_PACKAGE_UID));
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
- cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
+ cellNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
mDefaultNetworkCallback.assertNoCallback();
- mProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(),
+ mProfileDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ mTestPackageDefaultNetworkCallback.expectAvailableThenValidatedCallbacks(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(),
mCm.getActiveNetworkForUid(TEST_WORK_PROFILE_APP_UID));
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
// Set user profile network preference with test profile. mProfileDefaultNetworkCallback
// should receive callback with higher priority network preference (enterprise network).
@@ -16653,7 +16795,7 @@
assertNoCallbacks(mDefaultNetworkCallback, mTestPackageDefaultNetworkCallback);
mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(workAgent);
assertEquals(workAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_WORK_PROFILE_APP_UID));
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
// Set oem network preference with TEST_PACKAGE_UID. mTestPackageDefaultNetworkCallback
// should receive callback with higher priority network preference (current default network)
@@ -16664,8 +16806,8 @@
final UidRangeParcel[] uidRanges1 = toUidRangeStableParcels(uidRangesForUids(uids1));
setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges1, TEST_PACKAGE_NAME);
assertNoCallbacks(mDefaultNetworkCallback, mProfileDefaultNetworkCallback);
- mTestPackageDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ mTestPackageDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiAgent);
+ assertEquals(mWiFiAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
assertEquals(workAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_WORK_PROFILE_APP_UID));
// Set oem network preference with TEST_WORK_PROFILE_APP_UID. Both
@@ -16677,11 +16819,11 @@
setupSetOemNetworkPreferenceForPreferenceTest(
networkPref, uidRanges2, "com.android.test", testHandle);
mDefaultNetworkCallback.assertNoCallback();
- mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
- mTestPackageDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- assertEquals(mWiFiNetworkAgent.getNetwork(),
+ mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiAgent);
+ mTestPackageDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
+ assertEquals(mWiFiAgent.getNetwork(),
mCm.getActiveNetworkForUid(TEST_WORK_PROFILE_APP_UID));
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
// Remove oem network preference, mProfileDefaultNetworkCallback should receive callback
// with current highest priority network preference (enterprise network) and the others
@@ -16693,23 +16835,23 @@
assertNoCallbacks(mDefaultNetworkCallback, mTestPackageDefaultNetworkCallback);
mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(workAgent);
assertEquals(workAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_WORK_PROFILE_APP_UID));
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
// Remove user profile network preference.
mCm.setProfileNetworkPreference(testHandle, PROFILE_NETWORK_PREFERENCE_DEFAULT,
r -> r.run(), listener);
listener.expectOnComplete();
assertNoCallbacks(mDefaultNetworkCallback, mTestPackageDefaultNetworkCallback);
- mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- assertEquals(mCellNetworkAgent.getNetwork(),
+ mProfileDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
+ assertEquals(mCellAgent.getNetwork(),
mCm.getActiveNetworkForUid(TEST_WORK_PROFILE_APP_UID));
- assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
+ assertEquals(mCellAgent.getNetwork(), mCm.getActiveNetworkForUid(TEST_PACKAGE_UID));
// Disconnect wifi
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
assertNoCallbacks(mProfileDefaultNetworkCallback, mTestPackageDefaultNetworkCallback);
- mDefaultNetworkCallback.expect(CallbackEntry.LOST, mWiFiNetworkAgent);
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mDefaultNetworkCallback.expect(LOST, mWiFiAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
}
@Test
@@ -16723,13 +16865,13 @@
public void testUpdateRateLimit_EnableDisable() throws Exception {
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connect(true);
final LinkProperties cellLp = new LinkProperties();
cellLp.setInterfaceName(MOBILE_IFNAME);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(false);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(false);
waitForIdle();
@@ -16762,8 +16904,8 @@
public void testUpdateRateLimit_WhenNewNetworkIsAdded() throws Exception {
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connect(true);
waitForIdle();
@@ -16778,8 +16920,8 @@
final LinkProperties cellLp = new LinkProperties();
cellLp.setInterfaceName(MOBILE_IFNAME);
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
- mCellNetworkAgent.connect(false);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR, cellLp);
+ mCellAgent.connect(false);
assertNotNull(readHead.poll(TIMEOUT_MS, it -> it.first == cellLp.getInterfaceName()
&& it.second == rateLimitInBytesPerSec));
}
@@ -16789,8 +16931,8 @@
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connectWithoutInternet();
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connectWithoutInternet();
waitForIdle();
@@ -16814,8 +16956,8 @@
// - ensure network interface is not rate limited
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connect(true);
waitForIdle();
final ArrayTrackRecord<Pair<String, Long>>.ReadHead readHeadWifi =
@@ -16827,14 +16969,14 @@
it -> it.first == wifiLp.getInterfaceName()
&& it.second == rateLimitInBytesPerSec));
- mWiFiNetworkAgent.disconnect();
+ mWiFiAgent.disconnect();
assertNotNull(readHeadWifi.poll(TIMEOUT_MS,
it -> it.first == wifiLp.getInterfaceName() && it.second == -1));
setIngressRateLimit(-1);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connect(true);
assertNull(readHeadWifi.poll(TIMEOUT_MS, it -> it.first == wifiLp.getInterfaceName()));
}
@@ -16842,8 +16984,8 @@
public void testUpdateRateLimit_UpdateExistingRateLimit() throws Exception {
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connect(true);
waitForIdle();
final ArrayTrackRecord<Pair<String, Long>>.ReadHead readHeadWifi =
@@ -16872,8 +17014,8 @@
public void testUpdateRateLimit_DoesNothingBeforeT() throws Exception {
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp);
+ mWiFiAgent.connect(true);
waitForIdle();
final ArrayTrackRecord<Pair<String, Long>>.ReadHead readHead =
@@ -16912,8 +17054,8 @@
final String bssid1 = "AA:AA:AA:AA:AA:AA";
final String bssid2 = "BB:BB:BB:BB:BB:BB";
- mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
- mCellNetworkAgent.connect(true);
+ mCellAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
+ mCellAgent.connect(true);
NetworkCapabilities wifiNc1 = new NetworkCapabilities()
.addCapability(NET_CAPABILITY_INTERNET)
.addCapability(NET_CAPABILITY_NOT_VPN)
@@ -16926,106 +17068,98 @@
.setTransportInfo(new WifiInfo.Builder().setBssid(bssid2).build());
final LinkProperties wifiLp = new LinkProperties();
wifiLp.setInterfaceName(WIFI_IFNAME);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp, wifiNc1);
- mWiFiNetworkAgent.connect(true);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, wifiLp, wifiNc1);
+ mWiFiAgent.connect(true);
// The default network will be switching to Wi-Fi Network.
final TestNetworkCallback wifiNetworkCallback = new TestNetworkCallback();
final NetworkRequest wifiRequest = new NetworkRequest.Builder()
.addTransportType(TRANSPORT_WIFI).build();
mCm.requestNetwork(wifiRequest, wifiNetworkCallback);
- wifiNetworkCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
+ wifiNetworkCallback.expectAvailableCallbacksValidated(mWiFiAgent);
registerDefaultNetworkCallbacks();
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiAgent);
// There is a bug in the current code where ignoring validation after roam will not
// correctly change the default network if the result if the validation is partial or
// captive portal. TODO : fix the bug and reinstate this code.
if (false) {
// Wi-Fi roaming from wifiNc1 to wifiNc2 but the network is now behind a captive portal.
- mWiFiNetworkAgent.setNetworkCapabilities(wifiNc2, true /* sendToConnectivityService */);
+ mWiFiAgent.setNetworkCapabilities(wifiNc2, true /* sendToConnectivityService */);
// The only thing changed in this CAPS is the BSSID, which can't be tested for in this
// test because it's redacted.
- wifiNetworkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mWiFiNetworkAgent);
- mDefaultNetworkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mWiFiNetworkAgent);
- mWiFiNetworkAgent.setNetworkPortal(TEST_REDIRECT_URL,
- false /* privateDnsProbeSent */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
+ wifiNetworkCallback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
+ mDefaultNetworkCallback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
+ mWiFiAgent.setNetworkPortal(TEST_REDIRECT_URL, false /* privateDnsProbeSent */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), false);
// Wi-Fi is now detected to have a portal : cell should become the default network.
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
- wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED,
- mWiFiNetworkAgent);
- wifiNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_CAPTIVE_PORTAL,
- mWiFiNetworkAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
+ wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_VALIDATED, mWiFiAgent);
+ wifiNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_CAPTIVE_PORTAL, mWiFiAgent);
// Wi-Fi becomes valid again. The default network goes back to Wi-Fi.
- mWiFiNetworkAgent.setNetworkValid(false /* privateDnsProbeSent */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkValid(false /* privateDnsProbeSent */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiAgent);
wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_CAPTIVE_PORTAL,
- mWiFiNetworkAgent);
+ mWiFiAgent);
// Wi-Fi roaming from wifiNc2 to wifiNc1, and the network now has partial connectivity.
- mWiFiNetworkAgent.setNetworkCapabilities(wifiNc1, true);
- wifiNetworkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mWiFiNetworkAgent);
- mDefaultNetworkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mWiFiNetworkAgent);
- mWiFiNetworkAgent.setNetworkPartial();
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
+ mWiFiAgent.setNetworkCapabilities(wifiNc1, true);
+ wifiNetworkCallback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
+ mDefaultNetworkCallback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
+ mWiFiAgent.setNetworkPartial();
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), false);
// Wi-Fi now only offers partial connectivity, so in the absence of accepting partial
// connectivity explicitly for this network, it loses default status to cell.
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
wifiNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY,
- mWiFiNetworkAgent);
+ mWiFiAgent);
// Wi-Fi becomes valid again. The default network goes back to Wi-Fi.
- mWiFiNetworkAgent.setNetworkValid(false /* privateDnsProbeSent */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiNetworkAgent);
+ mWiFiAgent.setNetworkValid(false /* privateDnsProbeSent */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), true);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mWiFiAgent);
wifiNetworkCallback.expectCapabilitiesWithout(NET_CAPABILITY_PARTIAL_CONNECTIVITY,
- mWiFiNetworkAgent);
+ mWiFiAgent);
}
mCm.unregisterNetworkCallback(wifiNetworkCallback);
// Wi-Fi roams from wifiNc1 to wifiNc2, and now becomes really invalid. If validation
// failures after roam are not ignored, this will cause cell to become the default network.
// If they are ignored, this will not cause a switch until later.
- mWiFiNetworkAgent.setNetworkCapabilities(wifiNc2, true);
- mDefaultNetworkCallback.expect(CallbackEntry.NETWORK_CAPS_UPDATED,
- mWiFiNetworkAgent);
- mWiFiNetworkAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
+ mWiFiAgent.setNetworkCapabilities(wifiNc2, true);
+ mDefaultNetworkCallback.expect(NETWORK_CAPS_UPDATED, mWiFiAgent);
+ mWiFiAgent.setNetworkInvalid(false /* invalidBecauseOfPrivateDns */);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), false);
if (enabled) {
// Network validation failed, but the result will be ignored.
- assertTrue(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
+ assertTrue(mCm.getNetworkCapabilities(mWiFiAgent.getNetwork()).hasCapability(
NET_CAPABILITY_VALIDATED));
- mWiFiNetworkAgent.setNetworkValid(false);
+ mWiFiAgent.setNetworkValid(false);
// Behavior of after config_validationFailureAfterRoamIgnoreTimeMillis
ConditionVariable waitForValidationBlock = new ConditionVariable();
doReturn(50).when(mResources)
.getInteger(R.integer.config_validationFailureAfterRoamIgnoreTimeMillis);
// Wi-Fi roaming from wifiNc2 to wifiNc1.
- mWiFiNetworkAgent.setNetworkCapabilities(wifiNc1, true);
- mWiFiNetworkAgent.setNetworkInvalid(false);
+ mWiFiAgent.setNetworkCapabilities(wifiNc1, true);
+ mWiFiAgent.setNetworkInvalid(false);
waitForValidationBlock.block(150);
- mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mCm.reportNetworkConnectivity(mWiFiAgent.getNetwork(), false);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
} else {
- mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksValidated(mCellAgent);
}
// Wi-Fi is still connected and would become the default network if cell were to
// disconnect. This assertion ensures that the switch to cellular was not caused by
// Wi-Fi disconnecting (e.g., because the capability change to wifiNc2 caused it
// to stop satisfying the default request).
- mCellNetworkAgent.disconnect();
- mDefaultNetworkCallback.expect(CallbackEntry.LOST, mCellNetworkAgent);
- mDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
+ mCellAgent.disconnect();
+ mDefaultNetworkCallback.expect(LOST, mCellAgent);
+ mDefaultNetworkCallback.expectAvailableCallbacksUnvalidated(mWiFiAgent);
}
@@ -17108,8 +17242,8 @@
lp.setInterfaceName(WIFI_IFNAME);
lp.setMtu(mtu);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
- mWiFiNetworkAgent.sendLinkProperties(lp);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
+ mWiFiAgent.sendLinkProperties(lp);
waitForIdle();
verify(mMockNetd).interfaceSetMtu(eq(WIFI_IFNAME), eq(mtu));
@@ -17122,12 +17256,12 @@
lp.setInterfaceName(WIFI_IFNAME);
lp.setMtu(mtu);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
LinkProperties lp2 = new LinkProperties(lp);
lp2.setMtu(mtu2);
- mWiFiNetworkAgent.sendLinkProperties(lp2);
+ mWiFiAgent.sendLinkProperties(lp2);
waitForIdle();
verify(mMockNetd).interfaceSetMtu(eq(WIFI_IFNAME), eq(mtu2));
@@ -17140,8 +17274,8 @@
lp.setInterfaceName(WIFI_IFNAME);
lp.setMtu(mtu);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
- mWiFiNetworkAgent.sendLinkProperties(new LinkProperties(lp));
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
+ mWiFiAgent.sendLinkProperties(new LinkProperties(lp));
waitForIdle();
verify(mMockNetd, never()).interfaceSetMtu(eq(WIFI_IFNAME), anyInt());
@@ -17154,13 +17288,13 @@
lp.setInterfaceName(WIFI_IFNAME);
lp.setMtu(mtu);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
LinkProperties lp2 = new LinkProperties();
assertNull(lp2.getInterfaceName());
lp2.setMtu(mtu);
- mWiFiNetworkAgent.sendLinkProperties(new LinkProperties(lp2));
+ mWiFiAgent.sendLinkProperties(new LinkProperties(lp2));
waitForIdle();
verify(mMockNetd, never()).interfaceSetMtu(any(), anyInt());
@@ -17173,16 +17307,26 @@
lp.setInterfaceName(WIFI_IFNAME);
lp.setMtu(mtu);
- mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
+ mWiFiAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, lp);
final String ifaceName2 = WIFI_IFNAME + "_2";
LinkProperties lp2 = new LinkProperties();
lp2.setInterfaceName(ifaceName2);
lp2.setMtu(mtu);
- mWiFiNetworkAgent.sendLinkProperties(new LinkProperties(lp2));
+ mWiFiAgent.sendLinkProperties(new LinkProperties(lp2));
waitForIdle();
verify(mMockNetd).interfaceSetMtu(eq(ifaceName2), eq(mtu));
}
+
+ @Test
+ public void testCreateDeliveryGroupKeyForConnectivityAction() throws Exception {
+ final NetworkInfo info = new NetworkInfo(0 /* type */, 2 /* subtype */,
+ "MOBILE" /* typeName */, "LTE" /* subtypeName */);
+ assertEquals("0;2;null", createDeliveryGroupKeyForConnectivityAction(info));
+
+ info.setExtraInfo("test_info");
+ assertEquals("0;2;test_info", createDeliveryGroupKeyForConnectivityAction(info));
+ }
}
diff --git a/tests/unit/java/com/android/server/NsdServiceTest.java b/tests/unit/java/com/android/server/NsdServiceTest.java
index 5808beb..c7a6639 100644
--- a/tests/unit/java/com/android/server/NsdServiceTest.java
+++ b/tests/unit/java/com/android/server/NsdServiceTest.java
@@ -16,16 +16,25 @@
package com.android.server;
+import static android.net.InetAddresses.parseNumericAddress;
+import static android.net.nsd.NsdManager.FAILURE_BAD_PARAMETERS;
import static android.net.nsd.NsdManager.FAILURE_INTERNAL_ERROR;
+import static android.net.nsd.NsdManager.FAILURE_OPERATION_NOT_RUNNING;
+
+import static com.android.testutils.ContextUtils.mockService;
import static libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import static libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doReturn;
@@ -42,7 +51,7 @@
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;
import android.net.mdns.aidl.IMDnsEventListener;
@@ -63,10 +72,17 @@
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
+import android.os.RemoteException;
import androidx.annotation.NonNull;
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;
+import com.android.server.connectivity.mdns.MdnsSocketProvider;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.HandlerUtils;
@@ -82,7 +98,10 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import java.net.UnknownHostException;
import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
import java.util.Queue;
// TODOs:
@@ -96,11 +115,13 @@
private static final long CLEANUP_DELAY_MS = 500;
private static final long TIMEOUT_MS = 500;
private static final String SERVICE_NAME = "a_name";
- private static final String SERVICE_TYPE = "a_type";
+ private static final String SERVICE_TYPE = "_test._tcp";
private static final String SERVICE_FULL_NAME = SERVICE_NAME + "." + SERVICE_TYPE;
private static final String DOMAIN_NAME = "mytestdevice.local";
private static final int PORT = 2201;
private static final int IFACE_IDX_ANY = 0;
+ private static final String IPV4_ADDRESS = "192.0.2.0";
+ private static final String IPV6_ADDRESS = "2001:db8::";
// Records INsdManagerCallback created when NsdService#connect is called.
// Only accessed on the test thread, since NsdService#connect is called by the NsdManager
@@ -112,6 +133,10 @@
@Mock Context mContext;
@Mock ContentResolver mResolver;
@Mock MDnsManager mMockMDnsM;
+ @Mock Dependencies mDeps;
+ @Mock MdnsDiscoveryManager mDiscoveryManager;
+ @Mock MdnsAdvertiser mAdvertiser;
+ @Mock MdnsSocketProvider mSocketProvider;
HandlerThread mThread;
TestHandler mHandler;
NsdService mService;
@@ -133,9 +158,7 @@
mThread.start();
mHandler = new TestHandler(mThread.getLooper());
when(mContext.getContentResolver()).thenReturn(mResolver);
- doReturn(MDnsManager.MDNS_SERVICE).when(mContext)
- .getSystemServiceName(MDnsManager.class);
- doReturn(mMockMDnsM).when(mContext).getSystemService(MDnsManager.MDNS_SERVICE);
+ mockService(mContext, MDnsManager.class, MDnsManager.MDNS_SERVICE, mMockMDnsM);
if (mContext.getSystemService(MDnsManager.class) == null) {
// Test is using mockito-extended
doCallRealMethod().when(mContext).getSystemService(MDnsManager.class);
@@ -146,6 +169,7 @@
doReturn(true).when(mMockMDnsM).discover(anyInt(), anyString(), anyInt());
doReturn(true).when(mMockMDnsM).resolve(
anyInt(), anyString(), anyString(), anyString(), anyInt());
+ doReturn(false).when(mDeps).isMdnsDiscoveryManagerEnabled(any(Context.class));
mService = makeService();
}
@@ -382,7 +406,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());
@@ -555,12 +579,504 @@
anyInt()/* interfaceIdx */);
}
+ @Test
+ public void testStopServiceResolution() {
+ final NsdManager client = connectClient(mService);
+ final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
+ final ResolveListener resolveListener = mock(ResolveListener.class);
+ client.resolveService(request, resolveListener);
+ waitForIdle();
+
+ final ArgumentCaptor<Integer> resolvIdCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mMockMDnsM).resolve(resolvIdCaptor.capture(), eq(SERVICE_NAME), eq(SERVICE_TYPE),
+ eq("local.") /* domain */, eq(IFACE_IDX_ANY));
+
+ final int resolveId = resolvIdCaptor.getValue();
+ client.stopServiceResolution(resolveListener);
+ waitForIdle();
+
+ verify(mMockMDnsM).stopOperation(resolveId);
+ verify(resolveListener, timeout(TIMEOUT_MS)).onResolveStopped(argThat(ns ->
+ request.getServiceName().equals(ns.getServiceName())
+ && request.getServiceType().equals(ns.getServiceType())));
+ }
+
+ @Test
+ public void testStopResolutionFailed() {
+ final NsdManager client = connectClient(mService);
+ final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
+ final ResolveListener resolveListener = mock(ResolveListener.class);
+ client.resolveService(request, resolveListener);
+ waitForIdle();
+
+ final ArgumentCaptor<Integer> resolvIdCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mMockMDnsM).resolve(resolvIdCaptor.capture(), eq(SERVICE_NAME), eq(SERVICE_TYPE),
+ eq("local.") /* domain */, eq(IFACE_IDX_ANY));
+
+ final int resolveId = resolvIdCaptor.getValue();
+ doReturn(false).when(mMockMDnsM).stopOperation(anyInt());
+ client.stopServiceResolution(resolveListener);
+ waitForIdle();
+
+ verify(mMockMDnsM).stopOperation(resolveId);
+ verify(resolveListener, timeout(TIMEOUT_MS)).onStopResolutionFailed(argThat(ns ->
+ request.getServiceName().equals(ns.getServiceName())
+ && request.getServiceType().equals(ns.getServiceType())),
+ eq(FAILURE_OPERATION_NOT_RUNNING));
+ }
+
+ @Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
+ public void testStopResolutionDuringGettingAddress() throws RemoteException {
+ final NsdManager client = connectClient(mService);
+ final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
+ final ResolveListener resolveListener = mock(ResolveListener.class);
+ client.resolveService(request, resolveListener);
+ waitForIdle();
+
+ final IMDnsEventListener eventListener = getEventListener();
+ final ArgumentCaptor<Integer> resolvIdCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mMockMDnsM).resolve(resolvIdCaptor.capture(), eq(SERVICE_NAME), eq(SERVICE_TYPE),
+ eq("local.") /* domain */, eq(IFACE_IDX_ANY));
+
+ // Resolve service successfully.
+ final ResolutionInfo resolutionInfo = new ResolutionInfo(
+ resolvIdCaptor.getValue(),
+ IMDnsEventListener.SERVICE_RESOLVED,
+ null /* serviceName */,
+ null /* serviceType */,
+ null /* domain */,
+ SERVICE_FULL_NAME,
+ DOMAIN_NAME,
+ PORT,
+ new byte[0] /* txtRecord */,
+ IFACE_IDX_ANY);
+ doReturn(true).when(mMockMDnsM).getServiceAddress(anyInt(), any(), anyInt());
+ eventListener.onServiceResolutionStatus(resolutionInfo);
+ waitForIdle();
+
+ final ArgumentCaptor<Integer> getAddrIdCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mMockMDnsM).getServiceAddress(getAddrIdCaptor.capture(), eq(DOMAIN_NAME),
+ eq(IFACE_IDX_ANY));
+
+ final int getAddrId = getAddrIdCaptor.getValue();
+ client.stopServiceResolution(resolveListener);
+ waitForIdle();
+
+ verify(mMockMDnsM).stopOperation(getAddrId);
+ verify(resolveListener, timeout(TIMEOUT_MS)).onResolveStopped(argThat(ns ->
+ request.getServiceName().equals(ns.getServiceName())
+ && request.getServiceType().equals(ns.getServiceType())));
+ }
+
+ private void verifyUpdatedServiceInfo(NsdServiceInfo info, String serviceName,
+ String serviceType, String address, int port, int interfaceIndex, Network network) {
+ assertEquals(serviceName, info.getServiceName());
+ assertEquals(serviceType, info.getServiceType());
+ assertTrue(info.getHostAddresses().contains(parseNumericAddress(address)));
+ assertEquals(port, info.getPort());
+ assertEquals(network, info.getNetwork());
+ assertEquals(interfaceIndex, info.getInterfaceIndex());
+ }
+
+ @Test
+ public void testRegisterAndUnregisterServiceInfoCallback() throws RemoteException {
+ final NsdManager client = connectClient(mService);
+ final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
+ final NsdManager.ServiceInfoCallback serviceInfoCallback = mock(
+ NsdManager.ServiceInfoCallback.class);
+ client.registerServiceInfoCallback(request, Runnable::run, serviceInfoCallback);
+ waitForIdle();
+
+ final IMDnsEventListener eventListener = getEventListener();
+ final ArgumentCaptor<Integer> resolvIdCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mMockMDnsM).resolve(resolvIdCaptor.capture(), eq(SERVICE_NAME), eq(SERVICE_TYPE),
+ eq("local.") /* domain */, eq(IFACE_IDX_ANY));
+
+ // Resolve service successfully.
+ final ResolutionInfo resolutionInfo = new ResolutionInfo(
+ resolvIdCaptor.getValue(),
+ IMDnsEventListener.SERVICE_RESOLVED,
+ null /* serviceName */,
+ null /* serviceType */,
+ null /* domain */,
+ SERVICE_FULL_NAME,
+ DOMAIN_NAME,
+ PORT,
+ new byte[0] /* txtRecord */,
+ IFACE_IDX_ANY);
+ doReturn(true).when(mMockMDnsM).getServiceAddress(anyInt(), any(), anyInt());
+ eventListener.onServiceResolutionStatus(resolutionInfo);
+ waitForIdle();
+
+ final ArgumentCaptor<Integer> getAddrIdCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mMockMDnsM).getServiceAddress(getAddrIdCaptor.capture(), eq(DOMAIN_NAME),
+ eq(IFACE_IDX_ANY));
+
+ // First address info
+ final String v4Address = "192.0.2.1";
+ final String v6Address = "2001:db8::";
+ final GetAddressInfo addressInfo1 = new GetAddressInfo(
+ getAddrIdCaptor.getValue(),
+ IMDnsEventListener.SERVICE_GET_ADDR_SUCCESS,
+ SERVICE_FULL_NAME,
+ v4Address,
+ IFACE_IDX_ANY,
+ 999 /* netId */);
+ eventListener.onGettingServiceAddressStatus(addressInfo1);
+ waitForIdle();
+
+ final ArgumentCaptor<NsdServiceInfo> updateInfoCaptor =
+ ArgumentCaptor.forClass(NsdServiceInfo.class);
+ verify(serviceInfoCallback, timeout(TIMEOUT_MS).times(1))
+ .onServiceUpdated(updateInfoCaptor.capture());
+ verifyUpdatedServiceInfo(updateInfoCaptor.getAllValues().get(0) /* info */, SERVICE_NAME,
+ "." + SERVICE_TYPE, v4Address, PORT, IFACE_IDX_ANY, new Network(999));
+
+ // Second address info
+ final GetAddressInfo addressInfo2 = new GetAddressInfo(
+ getAddrIdCaptor.getValue(),
+ IMDnsEventListener.SERVICE_GET_ADDR_SUCCESS,
+ SERVICE_FULL_NAME,
+ v6Address,
+ IFACE_IDX_ANY,
+ 999 /* netId */);
+ eventListener.onGettingServiceAddressStatus(addressInfo2);
+ waitForIdle();
+
+ verify(serviceInfoCallback, timeout(TIMEOUT_MS).times(2))
+ .onServiceUpdated(updateInfoCaptor.capture());
+ verifyUpdatedServiceInfo(updateInfoCaptor.getAllValues().get(1) /* info */, SERVICE_NAME,
+ "." + SERVICE_TYPE, v6Address, PORT, IFACE_IDX_ANY, new Network(999));
+
+ client.unregisterServiceInfoCallback(serviceInfoCallback);
+ waitForIdle();
+
+ verify(serviceInfoCallback, timeout(TIMEOUT_MS)).onServiceInfoCallbackUnregistered();
+ }
+
+ @Test
+ public void testRegisterServiceCallbackFailed() throws Exception {
+ final NsdManager client = connectClient(mService);
+ final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, SERVICE_TYPE);
+ final NsdManager.ServiceInfoCallback subscribeListener = mock(
+ NsdManager.ServiceInfoCallback.class);
+ client.registerServiceInfoCallback(request, Runnable::run, subscribeListener);
+ waitForIdle();
+
+ final IMDnsEventListener eventListener = getEventListener();
+ final ArgumentCaptor<Integer> resolvIdCaptor = ArgumentCaptor.forClass(Integer.class);
+ verify(mMockMDnsM).resolve(resolvIdCaptor.capture(), eq(SERVICE_NAME), eq(SERVICE_TYPE),
+ eq("local.") /* domain */, eq(IFACE_IDX_ANY));
+
+ // Fail to resolve service.
+ final ResolutionInfo resolutionFailedInfo = new ResolutionInfo(
+ resolvIdCaptor.getValue(),
+ IMDnsEventListener.SERVICE_RESOLUTION_FAILED,
+ null /* serviceName */,
+ null /* serviceType */,
+ null /* domain */,
+ null /* serviceFullName */,
+ null /* domainName */,
+ 0 /* port */,
+ new byte[0] /* txtRecord */,
+ IFACE_IDX_ANY);
+ eventListener.onServiceResolutionStatus(resolutionFailedInfo);
+ verify(subscribeListener, timeout(TIMEOUT_MS))
+ .onServiceInfoCallbackRegistrationFailed(eq(FAILURE_BAD_PARAMETERS));
+ }
+
+ @Test
+ public void testUnregisterNotRegisteredCallback() {
+ final NsdManager client = connectClient(mService);
+ final NsdManager.ServiceInfoCallback serviceInfoCallback = mock(
+ NsdManager.ServiceInfoCallback.class);
+
+ assertThrows(IllegalArgumentException.class, () ->
+ client.unregisterServiceInfoCallback(serviceInfoCallback));
+ }
+
+ private void makeServiceWithMdnsDiscoveryManagerEnabled() {
+ doReturn(true).when(mDeps).isMdnsDiscoveryManagerEnabled(any(Context.class));
+ doReturn(mDiscoveryManager).when(mDeps).makeMdnsDiscoveryManager(any(), any());
+ doReturn(mSocketProvider).when(mDeps).makeMdnsSocketProvider(any(), any());
+
+ mService = makeService();
+ verify(mDeps).makeMdnsDiscoveryManager(any(), any());
+ 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.
+ connectClient(mService);
+ verify(mDeps, never()).makeMdnsDiscoveryManager(any(), any());
+ verify(mDeps, never()).makeMdnsSocketProvider(any(), any());
+
+ // Create NsdService again w/ feature enabled.
+ makeServiceWithMdnsDiscoveryManagerEnabled();
+ }
+
+ @Test
+ public void testDiscoveryWithMdnsDiscoveryManager() {
+ makeServiceWithMdnsDiscoveryManagerEnabled();
+
+ final NsdManager client = connectClient(mService);
+ final DiscoveryListener discListener = mock(DiscoveryListener.class);
+ final Network network = new Network(999);
+ final String serviceTypeWithLocalDomain = SERVICE_TYPE + ".local";
+ // Verify the discovery start / stop.
+ final ArgumentCaptor<MdnsServiceBrowserListener> listenerCaptor =
+ ArgumentCaptor.forClass(MdnsServiceBrowserListener.class);
+ client.discoverServices(SERVICE_TYPE, PROTOCOL, network, r -> r.run(), discListener);
+ waitForIdle();
+ verify(mSocketProvider).startMonitoringSockets();
+ verify(mDiscoveryManager).registerListener(eq(serviceTypeWithLocalDomain),
+ listenerCaptor.capture(), argThat(options -> network.equals(options.getNetwork())));
+ verify(discListener, timeout(TIMEOUT_MS)).onDiscoveryStarted(SERVICE_TYPE);
+
+ final MdnsServiceBrowserListener listener = listenerCaptor.getValue();
+ final MdnsServiceInfo foundInfo = new MdnsServiceInfo(
+ SERVICE_NAME, /* serviceInstanceName */
+ serviceTypeWithLocalDomain.split("\\."), /* serviceType */
+ List.of(), /* subtypes */
+ new String[] {"android", "local"}, /* hostName */
+ 12345, /* port */
+ IPV4_ADDRESS,
+ IPV6_ADDRESS,
+ List.of(), /* textStrings */
+ List.of(), /* textEntries */
+ 1234, /* interfaceIndex */
+ network);
+
+ // Verify onServiceNameDiscovered callback
+ listener.onServiceNameDiscovered(foundInfo);
+ verify(discListener, timeout(TIMEOUT_MS)).onServiceFound(argThat(info ->
+ info.getServiceName().equals(SERVICE_NAME)
+ && info.getServiceType().equals(SERVICE_TYPE)
+ && info.getNetwork().equals(network)));
+
+ final MdnsServiceInfo removedInfo = new MdnsServiceInfo(
+ SERVICE_NAME, /* serviceInstanceName */
+ serviceTypeWithLocalDomain.split("\\."), /* serviceType */
+ null, /* subtypes */
+ null, /* hostName */
+ 0, /* port */
+ null, /* ipv4Address */
+ null, /* ipv6Address */
+ null, /* textStrings */
+ null, /* textEntries */
+ 1234, /* interfaceIndex */
+ network);
+ // Verify onServiceNameRemoved callback
+ listener.onServiceNameRemoved(removedInfo);
+ verify(discListener, timeout(TIMEOUT_MS)).onServiceLost(argThat(info ->
+ info.getServiceName().equals(SERVICE_NAME)
+ && info.getServiceType().equals(SERVICE_TYPE)
+ && info.getNetwork().equals(network)));
+
+ client.stopServiceDiscovery(discListener);
+ waitForIdle();
+ verify(mDiscoveryManager).unregisterListener(eq(serviceTypeWithLocalDomain), any());
+ verify(discListener, timeout(TIMEOUT_MS)).onDiscoveryStopped(SERVICE_TYPE);
+ verify(mSocketProvider, timeout(CLEANUP_DELAY_MS + TIMEOUT_MS)).stopMonitoringSockets();
+ }
+
+ @Test
+ public void testDiscoveryWithMdnsDiscoveryManager_FailedWithInvalidServiceType() {
+ makeServiceWithMdnsDiscoveryManagerEnabled();
+
+ final NsdManager client = connectClient(mService);
+ final DiscoveryListener discListener = mock(DiscoveryListener.class);
+ final Network network = new Network(999);
+ final String invalidServiceType = "a_service";
+ client.discoverServices(
+ invalidServiceType, PROTOCOL, network, r -> r.run(), discListener);
+ waitForIdle();
+ verify(discListener, timeout(TIMEOUT_MS))
+ .onStartDiscoveryFailed(invalidServiceType, FAILURE_INTERNAL_ERROR);
+
+ final String serviceTypeWithLocalDomain = SERVICE_TYPE + ".local";
+ client.discoverServices(
+ serviceTypeWithLocalDomain, PROTOCOL, network, r -> r.run(), discListener);
+ waitForIdle();
+ verify(discListener, timeout(TIMEOUT_MS))
+ .onStartDiscoveryFailed(serviceTypeWithLocalDomain, FAILURE_INTERNAL_ERROR);
+
+ final String serviceTypeWithoutTcpOrUdpEnding = "_test._com";
+ client.discoverServices(
+ serviceTypeWithoutTcpOrUdpEnding, PROTOCOL, network, r -> r.run(), discListener);
+ waitForIdle();
+ verify(discListener, timeout(TIMEOUT_MS))
+ .onStartDiscoveryFailed(serviceTypeWithoutTcpOrUdpEnding, FAILURE_INTERNAL_ERROR);
+ }
+
+ @Test
+ public void testResolutionWithMdnsDiscoveryManager() throws UnknownHostException {
+ makeServiceWithMdnsDiscoveryManagerEnabled();
+
+ final NsdManager client = connectClient(mService);
+ final ResolveListener resolveListener = mock(ResolveListener.class);
+ final Network network = new Network(999);
+ final String serviceType = "_nsd._service._tcp";
+ final String constructedServiceType = "_nsd._sub._service._tcp.local";
+ final ArgumentCaptor<MdnsServiceBrowserListener> listenerCaptor =
+ ArgumentCaptor.forClass(MdnsServiceBrowserListener.class);
+ final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, serviceType);
+ request.setNetwork(network);
+ client.resolveService(request, resolveListener);
+ waitForIdle();
+ verify(mSocketProvider).startMonitoringSockets();
+ verify(mDiscoveryManager).registerListener(eq(constructedServiceType),
+ listenerCaptor.capture(), argThat(options -> network.equals(options.getNetwork())));
+
+ final MdnsServiceBrowserListener listener = listenerCaptor.getValue();
+ final MdnsServiceInfo mdnsServiceInfo = new MdnsServiceInfo(
+ SERVICE_NAME,
+ constructedServiceType.split("\\."),
+ List.of(), /* subtypes */
+ new String[]{"android", "local"}, /* hostName */
+ PORT,
+ IPV4_ADDRESS,
+ IPV6_ADDRESS,
+ List.of() /* textStrings */,
+ List.of(MdnsServiceInfo.TextEntry.fromBytes(new byte[]{
+ 'k', 'e', 'y', '=', (byte) 0xFF, (byte) 0xFE})) /* textEntries */,
+ 1234,
+ network);
+
+ // Verify onServiceFound callback
+ listener.onServiceFound(mdnsServiceInfo);
+ final ArgumentCaptor<NsdServiceInfo> infoCaptor =
+ ArgumentCaptor.forClass(NsdServiceInfo.class);
+ verify(resolveListener, timeout(TIMEOUT_MS)).onServiceResolved(infoCaptor.capture());
+ final NsdServiceInfo info = infoCaptor.getValue();
+ assertEquals(SERVICE_NAME, info.getServiceName());
+ assertEquals("." + serviceType, info.getServiceType());
+ assertEquals(PORT, info.getPort());
+ assertTrue(info.getAttributes().containsKey("key"));
+ assertEquals(1, info.getAttributes().size());
+ assertArrayEquals(new byte[]{(byte) 0xFF, (byte) 0xFE}, info.getAttributes().get("key"));
+ assertEquals(parseNumericAddress(IPV4_ADDRESS), info.getHost());
+ assertEquals(network, info.getNetwork());
+
+ // Verify the listener has been unregistered.
+ verify(mDiscoveryManager, timeout(TIMEOUT_MS))
+ .unregisterListener(eq(constructedServiceType), any());
+ 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);
}
NsdService makeService() {
- final NsdService service = new NsdService(mContext, mHandler, CLEANUP_DELAY_MS) {
+ final NsdService service = new NsdService(mContext, mHandler, CLEANUP_DELAY_MS, mDeps) {
@Override
public INsdServiceConnector connect(INsdManagerCallback baseCb) {
// Wrap the callback in a transparent mock, to mock asBinder returning a
@@ -599,6 +1115,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/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java
new file mode 100644
index 0000000..6c29d6e
--- /dev/null
+++ b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java
@@ -0,0 +1,231 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+
+import android.content.Context;
+import android.net.INetd;
+import android.net.MarkMaskParcel;
+import android.os.Build;
+import android.os.HandlerThread;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.testutils.DevSdkIgnoreRule;
+import com.android.testutils.DevSdkIgnoreRunner;
+
+import libcore.util.HexEncoding;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+@RunWith(DevSdkIgnoreRunner.class)
+@SmallTest
+@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
+public class AutomaticOnOffKeepaliveTrackerTest {
+ private static final int TEST_NETID = 0xA85;
+ private static final int TEST_NETID_FWMARK = 0x0A85;
+ private static final int OTHER_NETID = 0x1A85;
+ private static final int NETID_MASK = 0xffff;
+ private AutomaticOnOffKeepaliveTracker mAOOKeepaliveTracker;
+ private HandlerThread mHandlerThread;
+
+ @Mock INetd mNetd;
+ @Mock AutomaticOnOffKeepaliveTracker.Dependencies mDependencies;
+ @Mock Context mCtx;
+ @Mock KeepaliveTracker mKeepaliveTracker;
+
+ // Hexadecimal representation of a SOCK_DIAG response with tcp info.
+ private static final String SOCK_DIAG_TCP_INET_HEX =
+ // struct nlmsghdr.
+ "14010000" + // length = 276
+ "1400" + // type = SOCK_DIAG_BY_FAMILY
+ "0301" + // flags = NLM_F_REQUEST | NLM_F_DUMP
+ "00000000" + // seqno
+ "00000000" + // pid (0 == kernel)
+ // struct inet_diag_req_v2
+ "02" + // family = AF_INET
+ "06" + // state
+ "00" + // timer
+ "00" + // retrans
+ // inet_diag_sockid
+ "DEA5" + // idiag_sport = 42462
+ "71B9" + // idiag_dport = 47473
+ "0a006402000000000000000000000000" + // idiag_src = 10.0.100.2
+ "08080808000000000000000000000000" + // idiag_dst = 8.8.8.8
+ "00000000" + // idiag_if
+ "34ED000076270000" + // idiag_cookie = 43387759684916
+ "00000000" + // idiag_expires
+ "00000000" + // idiag_rqueue
+ "00000000" + // idiag_wqueue
+ "00000000" + // idiag_uid
+ "00000000" + // idiag_inode
+ // rtattr
+ "0500" + // len = 5
+ "0800" + // type = 8
+ "00000000" + // data
+ "0800" + // len = 8
+ "0F00" + // type = 15(INET_DIAG_MARK)
+ "850A0C00" + // data, socket mark=789125
+ "AC00" + // len = 172
+ "0200" + // type = 2(INET_DIAG_INFO)
+ // tcp_info
+ "01" + // state = TCP_ESTABLISHED
+ "00" + // ca_state = TCP_CA_OPEN
+ "05" + // retransmits = 5
+ "00" + // probes = 0
+ "00" + // backoff = 0
+ "07" + // option = TCPI_OPT_WSCALE|TCPI_OPT_SACK|TCPI_OPT_TIMESTAMPS
+ "88" + // wscale = 8
+ "00" + // delivery_rate_app_limited = 0
+ "4A911B00" + // rto = 1806666
+ "00000000" + // ato = 0
+ "2E050000" + // sndMss = 1326
+ "18020000" + // rcvMss = 536
+ "00000000" + // unsacked = 0
+ "00000000" + // acked = 0
+ "00000000" + // lost = 0
+ "00000000" + // retrans = 0
+ "00000000" + // fackets = 0
+ "BB000000" + // lastDataSent = 187
+ "00000000" + // lastAckSent = 0
+ "BB000000" + // lastDataRecv = 187
+ "BB000000" + // lastDataAckRecv = 187
+ "DC050000" + // pmtu = 1500
+ "30560100" + // rcvSsthresh = 87600
+ "3E2C0900" + // rttt = 601150
+ "1F960400" + // rttvar = 300575
+ "78050000" + // sndSsthresh = 1400
+ "0A000000" + // sndCwnd = 10
+ "A8050000" + // advmss = 1448
+ "03000000" + // reordering = 3
+ "00000000" + // rcvrtt = 0
+ "30560100" + // rcvspace = 87600
+ "00000000" + // totalRetrans = 0
+ "53AC000000000000" + // pacingRate = 44115
+ "FFFFFFFFFFFFFFFF" + // maxPacingRate = 18446744073709551615
+ "0100000000000000" + // bytesAcked = 1
+ "0000000000000000" + // bytesReceived = 0
+ "0A000000" + // SegsOut = 10
+ "00000000" + // SegsIn = 0
+ "00000000" + // NotSentBytes = 0
+ "3E2C0900" + // minRtt = 601150
+ "00000000" + // DataSegsIn = 0
+ "00000000" + // DataSegsOut = 0
+ "0000000000000000"; // deliverRate = 0
+ private static final String SOCK_DIAG_NO_TCP_INET_HEX =
+ // struct nlmsghdr
+ "14000000" // length = 20
+ + "0300" // type = NLMSG_DONE
+ + "0301" // flags = NLM_F_REQUEST | NLM_F_DUMP
+ + "00000000" // seqno
+ + "00000000" // pid (0 == kernel)
+ // struct inet_diag_req_v2
+ + "02" // family = AF_INET
+ + "06" // state
+ + "00" // timer
+ + "00"; // retrans
+ private static final byte[] SOCK_DIAG_NO_TCP_INET_BYTES =
+ HexEncoding.decode(SOCK_DIAG_NO_TCP_INET_HEX.toCharArray(), false);
+ private static final String TEST_RESPONSE_HEX =
+ SOCK_DIAG_TCP_INET_HEX + SOCK_DIAG_NO_TCP_INET_HEX;
+ private static final byte[] TEST_RESPONSE_BYTES =
+ HexEncoding.decode(TEST_RESPONSE_HEX.toCharArray(), false);
+
+ @Before
+ public void setup() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ doReturn(mNetd).when(mDependencies).getNetd();
+ doReturn(makeMarkMaskParcel(NETID_MASK, TEST_NETID_FWMARK)).when(mNetd)
+ .getFwmarkForNetwork(TEST_NETID);
+
+ doNothing().when(mDependencies).sendRequest(any(), any());
+
+ mHandlerThread = new HandlerThread("KeepaliveTrackerTest");
+ mHandlerThread.start();
+ doReturn(mKeepaliveTracker).when(mDependencies).newKeepaliveTracker(
+ mCtx, mHandlerThread.getThreadHandler());
+ doReturn(true).when(mDependencies).isFeatureEnabled(any());
+ mAOOKeepaliveTracker = new AutomaticOnOffKeepaliveTracker(
+ mCtx, mHandlerThread.getThreadHandler(), mDependencies);
+ }
+
+ @Test
+ public void testIsAnyTcpSocketConnected_runOnNonHandlerThread() throws Exception {
+ setupResponseWithSocketExisting();
+ assertThrows(IllegalStateException.class,
+ () -> mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID));
+ }
+
+ @Test
+ public void testIsAnyTcpSocketConnected_withTargetNetId() throws Exception {
+ setupResponseWithSocketExisting();
+ mHandlerThread.getThreadHandler().post(
+ () -> assertTrue(mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID)));
+ }
+
+ @Test
+ public void testIsAnyTcpSocketConnected_withIncorrectNetId() throws Exception {
+ setupResponseWithSocketExisting();
+ mHandlerThread.getThreadHandler().post(
+ () -> assertFalse(mAOOKeepaliveTracker.isAnyTcpSocketConnected(OTHER_NETID)));
+ }
+
+ @Test
+ public void testIsAnyTcpSocketConnected_noSocketExists() throws Exception {
+ setupResponseWithoutSocketExisting();
+ mHandlerThread.getThreadHandler().post(
+ () -> assertFalse(mAOOKeepaliveTracker.isAnyTcpSocketConnected(TEST_NETID)));
+ }
+
+ private void setupResponseWithSocketExisting() throws Exception {
+ final ByteBuffer tcpBufferV6 = getByteBuffer(TEST_RESPONSE_BYTES);
+ final ByteBuffer tcpBufferV4 = getByteBuffer(TEST_RESPONSE_BYTES);
+ doReturn(tcpBufferV6, tcpBufferV4).when(mDependencies).recvSockDiagResponse(any());
+ }
+
+ private void setupResponseWithoutSocketExisting() throws Exception {
+ final ByteBuffer tcpBufferV6 = getByteBuffer(SOCK_DIAG_NO_TCP_INET_BYTES);
+ final ByteBuffer tcpBufferV4 = getByteBuffer(SOCK_DIAG_NO_TCP_INET_BYTES);
+ doReturn(tcpBufferV6, tcpBufferV4).when(mDependencies).recvSockDiagResponse(any());
+ }
+
+ private MarkMaskParcel makeMarkMaskParcel(final int mask, final int mark) {
+ final MarkMaskParcel parcel = new MarkMaskParcel();
+ parcel.mask = mask;
+ parcel.mark = mark;
+ return parcel;
+ }
+
+ private ByteBuffer getByteBuffer(final byte[] bytes) {
+ final ByteBuffer buffer = ByteBuffer.wrap(bytes);
+ buffer.order(ByteOrder.nativeOrder());
+ return buffer;
+ }
+}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsAdvertiserTest.kt b/tests/unit/java/com/android/server/connectivity/mdns/MdnsAdvertiserTest.kt
new file mode 100644
index 0000000..1febe6d
--- /dev/null
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsAdvertiserTest.kt
@@ -0,0 +1,233 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns
+
+import android.net.InetAddresses.parseNumericAddress
+import android.net.LinkAddress
+import android.net.Network
+import android.net.nsd.NsdServiceInfo
+import android.os.Build
+import android.os.Handler
+import android.os.HandlerThread
+import com.android.server.connectivity.mdns.MdnsAdvertiser.AdvertiserCallback
+import com.android.server.connectivity.mdns.MdnsSocketProvider.SocketCallback
+import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
+import com.android.testutils.DevSdkIgnoreRunner
+import com.android.testutils.waitForIdle
+import java.util.Objects
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentCaptor
+import org.mockito.ArgumentMatchers.eq
+import org.mockito.Mockito.any
+import org.mockito.Mockito.anyInt
+import org.mockito.Mockito.argThat
+import org.mockito.Mockito.atLeastOnce
+import org.mockito.Mockito.doReturn
+import org.mockito.Mockito.mock
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+private const val SERVICE_ID_1 = 1
+private const val SERVICE_ID_2 = 2
+private const val TIMEOUT_MS = 10_000L
+private val TEST_ADDR = parseNumericAddress("2001:db8::123")
+private val TEST_LINKADDR = LinkAddress(TEST_ADDR, 64 /* prefixLength */)
+private val TEST_NETWORK_1 = mock(Network::class.java)
+private val TEST_NETWORK_2 = mock(Network::class.java)
+
+private val SERVICE_1 = NsdServiceInfo("TestServiceName", "_advertisertest._tcp").apply {
+ port = 12345
+ host = TEST_ADDR
+ network = TEST_NETWORK_1
+}
+
+private val ALL_NETWORKS_SERVICE = NsdServiceInfo("TestServiceName", "_advertisertest._tcp").apply {
+ port = 12345
+ host = TEST_ADDR
+ network = null
+}
+
+@RunWith(DevSdkIgnoreRunner::class)
+@IgnoreUpTo(Build.VERSION_CODES.S_V2)
+class MdnsAdvertiserTest {
+ private val thread = HandlerThread(MdnsAdvertiserTest::class.simpleName)
+ private val handler by lazy { Handler(thread.looper) }
+ private val socketProvider = mock(MdnsSocketProvider::class.java)
+ private val cb = mock(AdvertiserCallback::class.java)
+
+ private val mockSocket1 = mock(MdnsInterfaceSocket::class.java)
+ private val mockSocket2 = mock(MdnsInterfaceSocket::class.java)
+ private val mockInterfaceAdvertiser1 = mock(MdnsInterfaceAdvertiser::class.java)
+ private val mockInterfaceAdvertiser2 = mock(MdnsInterfaceAdvertiser::class.java)
+ private val mockDeps = mock(MdnsAdvertiser.Dependencies::class.java)
+
+ @Before
+ fun setUp() {
+ thread.start()
+ doReturn(mockInterfaceAdvertiser1).`when`(mockDeps).makeAdvertiser(eq(mockSocket1),
+ any(), any(), any(), any())
+ doReturn(mockInterfaceAdvertiser2).`when`(mockDeps).makeAdvertiser(eq(mockSocket2),
+ any(), any(), any(), any())
+ doReturn(true).`when`(mockInterfaceAdvertiser1).isProbing(anyInt())
+ doReturn(true).`when`(mockInterfaceAdvertiser2).isProbing(anyInt())
+ }
+
+ @After
+ fun tearDown() {
+ thread.quitSafely()
+ }
+
+ @Test
+ fun testAddService_OneNetwork() {
+ val advertiser = MdnsAdvertiser(thread.looper, socketProvider, cb, mockDeps)
+ postSync { advertiser.addService(SERVICE_ID_1, SERVICE_1) }
+
+ val socketCbCaptor = ArgumentCaptor.forClass(SocketCallback::class.java)
+ verify(socketProvider).requestSocket(eq(TEST_NETWORK_1), socketCbCaptor.capture())
+
+ val socketCb = socketCbCaptor.value
+ postSync { socketCb.onSocketCreated(TEST_NETWORK_1, mockSocket1, listOf(TEST_LINKADDR)) }
+
+ val intAdvCbCaptor = ArgumentCaptor.forClass(MdnsInterfaceAdvertiser.Callback::class.java)
+ verify(mockDeps).makeAdvertiser(eq(mockSocket1),
+ eq(listOf(TEST_LINKADDR)), eq(thread.looper), any(), intAdvCbCaptor.capture())
+
+ doReturn(false).`when`(mockInterfaceAdvertiser1).isProbing(SERVICE_ID_1)
+ postSync { intAdvCbCaptor.value.onRegisterServiceSucceeded(
+ mockInterfaceAdvertiser1, SERVICE_ID_1) }
+ verify(cb).onRegisterServiceSucceeded(eq(SERVICE_ID_1), argThat { it.matches(SERVICE_1) })
+
+ postSync { socketCb.onInterfaceDestroyed(TEST_NETWORK_1, mockSocket1) }
+ verify(mockInterfaceAdvertiser1).destroyNow()
+ }
+
+ @Test
+ fun testAddService_AllNetworks() {
+ val advertiser = MdnsAdvertiser(thread.looper, socketProvider, cb, mockDeps)
+ postSync { advertiser.addService(SERVICE_ID_1, ALL_NETWORKS_SERVICE) }
+
+ val socketCbCaptor = ArgumentCaptor.forClass(SocketCallback::class.java)
+ verify(socketProvider).requestSocket(eq(ALL_NETWORKS_SERVICE.network),
+ socketCbCaptor.capture())
+
+ val socketCb = socketCbCaptor.value
+ postSync { socketCb.onSocketCreated(TEST_NETWORK_1, mockSocket1, listOf(TEST_LINKADDR)) }
+ postSync { socketCb.onSocketCreated(TEST_NETWORK_2, mockSocket2, listOf(TEST_LINKADDR)) }
+
+ val intAdvCbCaptor1 = ArgumentCaptor.forClass(MdnsInterfaceAdvertiser.Callback::class.java)
+ val intAdvCbCaptor2 = ArgumentCaptor.forClass(MdnsInterfaceAdvertiser.Callback::class.java)
+ verify(mockDeps).makeAdvertiser(eq(mockSocket1), eq(listOf(TEST_LINKADDR)),
+ eq(thread.looper), any(), intAdvCbCaptor1.capture())
+ verify(mockDeps).makeAdvertiser(eq(mockSocket2), eq(listOf(TEST_LINKADDR)),
+ eq(thread.looper), any(), intAdvCbCaptor2.capture())
+
+ doReturn(false).`when`(mockInterfaceAdvertiser1).isProbing(SERVICE_ID_1)
+ postSync { intAdvCbCaptor1.value.onRegisterServiceSucceeded(
+ mockInterfaceAdvertiser1, SERVICE_ID_1) }
+
+ // Need both advertisers to finish probing and call onRegisterServiceSucceeded
+ verify(cb, never()).onRegisterServiceSucceeded(anyInt(), any())
+ doReturn(false).`when`(mockInterfaceAdvertiser2).isProbing(SERVICE_ID_1)
+ postSync { intAdvCbCaptor2.value.onRegisterServiceSucceeded(
+ mockInterfaceAdvertiser2, SERVICE_ID_1) }
+ verify(cb).onRegisterServiceSucceeded(eq(SERVICE_ID_1),
+ argThat { it.matches(ALL_NETWORKS_SERVICE) })
+
+ // Unregister the service
+ postSync { advertiser.removeService(SERVICE_ID_1) }
+ verify(mockInterfaceAdvertiser1).removeService(SERVICE_ID_1)
+ verify(mockInterfaceAdvertiser2).removeService(SERVICE_ID_1)
+
+ // Interface advertisers call onDestroyed after sending exit announcements
+ postSync { intAdvCbCaptor1.value.onDestroyed(mockSocket1) }
+ verify(socketProvider, never()).unrequestSocket(any())
+ postSync { intAdvCbCaptor2.value.onDestroyed(mockSocket2) }
+ verify(socketProvider).unrequestSocket(socketCb)
+ }
+
+ @Test
+ fun testAddService_Conflicts() {
+ val advertiser = MdnsAdvertiser(thread.looper, socketProvider, cb, mockDeps)
+ postSync { advertiser.addService(SERVICE_ID_1, SERVICE_1) }
+
+ val oneNetSocketCbCaptor = ArgumentCaptor.forClass(SocketCallback::class.java)
+ verify(socketProvider).requestSocket(eq(TEST_NETWORK_1), oneNetSocketCbCaptor.capture())
+ val oneNetSocketCb = oneNetSocketCbCaptor.value
+
+ // Register a service with the same name on all networks (name conflict)
+ postSync { advertiser.addService(SERVICE_ID_2, ALL_NETWORKS_SERVICE) }
+ val allNetSocketCbCaptor = ArgumentCaptor.forClass(SocketCallback::class.java)
+ verify(socketProvider).requestSocket(eq(null), allNetSocketCbCaptor.capture())
+ val allNetSocketCb = allNetSocketCbCaptor.value
+
+ // Callbacks for matching network and all networks both get the socket
+ postSync {
+ oneNetSocketCb.onSocketCreated(TEST_NETWORK_1, mockSocket1, listOf(TEST_LINKADDR))
+ allNetSocketCb.onSocketCreated(TEST_NETWORK_1, mockSocket1, listOf(TEST_LINKADDR))
+ }
+
+ val expectedRenamed = NsdServiceInfo(
+ "${ALL_NETWORKS_SERVICE.serviceName} (2)", ALL_NETWORKS_SERVICE.serviceType).apply {
+ port = ALL_NETWORKS_SERVICE.port
+ host = ALL_NETWORKS_SERVICE.host
+ network = ALL_NETWORKS_SERVICE.network
+ }
+
+ val intAdvCbCaptor = ArgumentCaptor.forClass(MdnsInterfaceAdvertiser.Callback::class.java)
+ verify(mockDeps).makeAdvertiser(eq(mockSocket1), eq(listOf(TEST_LINKADDR)),
+ eq(thread.looper), any(), intAdvCbCaptor.capture())
+ verify(mockInterfaceAdvertiser1).addService(eq(SERVICE_ID_1),
+ argThat { it.matches(SERVICE_1) })
+ verify(mockInterfaceAdvertiser1).addService(eq(SERVICE_ID_2),
+ argThat { it.matches(expectedRenamed) })
+
+ doReturn(false).`when`(mockInterfaceAdvertiser1).isProbing(SERVICE_ID_1)
+ postSync { intAdvCbCaptor.value.onRegisterServiceSucceeded(
+ mockInterfaceAdvertiser1, SERVICE_ID_1) }
+ verify(cb).onRegisterServiceSucceeded(eq(SERVICE_ID_1), argThat { it.matches(SERVICE_1) })
+
+ doReturn(false).`when`(mockInterfaceAdvertiser1).isProbing(SERVICE_ID_2)
+ postSync { intAdvCbCaptor.value.onRegisterServiceSucceeded(
+ mockInterfaceAdvertiser1, SERVICE_ID_2) }
+ verify(cb).onRegisterServiceSucceeded(eq(SERVICE_ID_2),
+ argThat { it.matches(expectedRenamed) })
+
+ postSync { oneNetSocketCb.onInterfaceDestroyed(TEST_NETWORK_1, mockSocket1) }
+ postSync { allNetSocketCb.onInterfaceDestroyed(TEST_NETWORK_1, mockSocket1) }
+
+ // destroyNow can be called multiple times
+ verify(mockInterfaceAdvertiser1, atLeastOnce()).destroyNow()
+ }
+
+ private fun postSync(r: () -> Unit) {
+ handler.post(r)
+ handler.waitForIdle(TIMEOUT_MS)
+ }
+}
+
+// NsdServiceInfo does not implement equals; this is useful to use in argument matchers
+private fun NsdServiceInfo.matches(other: NsdServiceInfo): Boolean {
+ return Objects.equals(serviceName, other.serviceName) &&
+ Objects.equals(serviceType, other.serviceType) &&
+ Objects.equals(attributes, other.attributes) &&
+ Objects.equals(host, other.host) &&
+ port == other.port &&
+ Objects.equals(network, other.network)
+}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsAnnouncerTest.kt b/tests/unit/java/com/android/server/connectivity/mdns/MdnsAnnouncerTest.kt
index e9325d5..6c3f729 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsAnnouncerTest.kt
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsAnnouncerTest.kt
@@ -22,13 +22,11 @@
import android.os.SystemClock
import com.android.internal.util.HexDump
import com.android.server.connectivity.mdns.MdnsAnnouncer.AnnouncementInfo
+import com.android.server.connectivity.mdns.MdnsAnnouncer.BaseAnnouncementInfo
+import com.android.server.connectivity.mdns.MdnsRecordRepository.getReverseDnsAddress
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
import com.android.testutils.DevSdkIgnoreRunner
import java.net.DatagramPacket
-import java.net.Inet6Address
-import java.net.InetAddress
-import java.net.InetSocketAddress
-import java.net.MulticastSocket
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import org.junit.After
@@ -38,6 +36,7 @@
import org.mockito.ArgumentCaptor
import org.mockito.Mockito.any
import org.mockito.Mockito.atLeast
+import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.mockito.Mockito.timeout
import org.mockito.Mockito.verify
@@ -47,19 +46,17 @@
private const val NEXT_ANNOUNCES_DELAY = 1L
private const val TEST_TIMEOUT_MS = 1000L
-private val destinationsSupplier = {
- listOf(InetSocketAddress(MdnsConstants.getMdnsIPv6Address(), MdnsConstants.MDNS_PORT)) }
-
@RunWith(DevSdkIgnoreRunner::class)
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
class MdnsAnnouncerTest {
private val thread = HandlerThread(MdnsAnnouncerTest::class.simpleName)
- private val socket = mock(MulticastSocket::class.java)
+ private val socket = mock(MdnsInterfaceSocket::class.java)
private val buffer = ByteArray(1500)
@Before
fun setUp() {
+ doReturn(true).`when`(socket).hasJoinedIpv6()
thread.start()
}
@@ -71,8 +68,7 @@
private class TestAnnouncementInfo(
announcedRecords: List<MdnsRecord>,
additionalRecords: List<MdnsRecord>
- )
- : AnnouncementInfo(announcedRecords, additionalRecords, destinationsSupplier) {
+ ) : AnnouncementInfo(1 /* serviceId */, announcedRecords, additionalRecords) {
override fun getDelayMs(nextIndex: Int) =
if (nextIndex < FIRST_ANNOUNCES_COUNT) {
FIRST_ANNOUNCES_DELAY
@@ -83,10 +79,10 @@
@Test
fun testAnnounce() {
- val replySender = MdnsReplySender(thread.looper, socket, buffer)
+ val replySender = MdnsReplySender("testiface", thread.looper, socket, buffer)
@Suppress("UNCHECKED_CAST")
val cb = mock(MdnsPacketRepeater.PacketRepeaterCallback::class.java)
- as MdnsPacketRepeater.PacketRepeaterCallback<AnnouncementInfo>
+ as MdnsPacketRepeater.PacketRepeaterCallback<BaseAnnouncementInfo>
val announcer = MdnsAnnouncer("testiface", thread.looper, replySender, cb)
/*
The expected packet replicates records announced when registering a service, as observed in
@@ -95,7 +91,7 @@
scapy.raw(scapy.dns_compress(scapy.DNS(rd=0, qr=1, aa=1,
qd = None,
an =
- scapy.DNSRR(type='PTR', rrname='123.0.2.192.in-addr.arpa.', rdata='Android.local',
+ scapy.DNSRR(type='PTR', rrname='123.2.0.192.in-addr.arpa.', rdata='Android.local',
rclass=0x8001, ttl=120) /
scapy.DNSRR(type='PTR',
rrname='3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.0.1.0.0.2.ip6.arpa',
@@ -115,8 +111,8 @@
scapy.DNSRR(type='AAAA', rrname='Android.local', rclass=0x8001, rdata='2001:db8::456',
ttl=120),
ar =
- scapy.DNSRRNSEC(rrname='123.0.2.192.in-addr.arpa.', rclass=0x8001, ttl=120,
- nextname='123.0.2.192.in-addr.arpa.', typebitmaps=[12]) /
+ scapy.DNSRRNSEC(rrname='123.2.0.192.in-addr.arpa.', rclass=0x8001, ttl=120,
+ nextname='123.2.0.192.in-addr.arpa.', typebitmaps=[12]) /
scapy.DNSRRNSEC(
rrname='3.2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.0.1.0.0.2.ip6.arpa',
rclass=0x8001, ttl=120,
@@ -135,7 +131,7 @@
typebitmaps=[1, 28]))
)).hex().upper()
*/
- val expected = "00008400000000090000000503313233013001320331393207696E2D61646472046172706" +
+ val expected = "00008400000000090000000503313233013201300331393207696E2D61646472046172706" +
"100000C800100000078000F07416E64726F6964056C6F63616C00013301320131013001300130013" +
"00130013001300130013001300130013001300130013001300130013001300130013001380142014" +
"40130013101300130013203697036C020000C8001000000780002C030013601350134C045000C800" +
@@ -153,9 +149,9 @@
val v4Addr = parseNumericAddress("192.0.2.123")
val v6Addr1 = parseNumericAddress("2001:DB8::123")
val v6Addr2 = parseNumericAddress("2001:DB8::456")
- val v4AddrRev = arrayOf("123", "0", "2", "192", "in-addr", "arpa")
- val v6Addr1Rev = getReverseV6AddressName(v6Addr1)
- val v6Addr2Rev = getReverseV6AddressName(v6Addr2)
+ val v4AddrRev = getReverseDnsAddress(v4Addr)
+ val v6Addr1Rev = getReverseDnsAddress(v6Addr1)
+ val v6Addr2Rev = getReverseDnsAddress(v6Addr2)
val announcedRecords = listOf(
// Reverse address records
@@ -258,7 +254,10 @@
verify(socket, atLeast(i + 1)).send(any())
val now = SystemClock.elapsedRealtime()
assertTrue(now > timeStart + startDelay + i * FIRST_ANNOUNCES_DELAY)
- assertTrue(now < timeStart + startDelay + (i + 1) * FIRST_ANNOUNCES_DELAY)
+ // Loops can be much slower than the expected timing (>100ms delay), use
+ // TEST_TIMEOUT_MS as tolerance.
+ assertTrue(now < timeStart + startDelay + (i + 1) * FIRST_ANNOUNCES_DELAY +
+ TEST_TIMEOUT_MS)
}
// Subsequent announces should happen quickly (NEXT_ANNOUNCES_DELAY)
@@ -271,13 +270,3 @@
}
}
}
-
-/**
- * Compute 2001:db8::1 --> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.1.0.0.2.ip6.arpa
- */
-private fun getReverseV6AddressName(addr: InetAddress): Array<String> {
- assertTrue(addr is Inet6Address)
- return addr.address.flatMapTo(mutableListOf("arpa", "ip6")) {
- HexDump.toHexString(it).toCharArray().map(Char::toString)
- }.reversed().toTypedArray()
-}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsDiscoveryManagerTests.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsDiscoveryManagerTests.java
index 3e3c3bf..83e7696 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsDiscoveryManagerTests.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsDiscoveryManagerTests.java
@@ -46,7 +46,7 @@
private static final String SERVICE_TYPE_2 = "_test._tcp.local";
@Mock private ExecutorProvider executorProvider;
- @Mock private MdnsSocketClient socketClient;
+ @Mock private MdnsSocketClientBase socketClient;
@Mock private MdnsServiceTypeClient mockServiceTypeClientOne;
@Mock private MdnsServiceTypeClient mockServiceTypeClientTwo;
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsInterfaceAdvertiserTest.kt b/tests/unit/java/com/android/server/connectivity/mdns/MdnsInterfaceAdvertiserTest.kt
new file mode 100644
index 0000000..4a806b1
--- /dev/null
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsInterfaceAdvertiserTest.kt
@@ -0,0 +1,275 @@
+/*
+ * Copyright (C) 2023 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 com.android.server.connectivity.mdns
+
+import android.net.InetAddresses.parseNumericAddress
+import android.net.LinkAddress
+import android.net.nsd.NsdServiceInfo
+import android.os.Build
+import android.os.HandlerThread
+import com.android.net.module.util.HexDump
+import com.android.server.connectivity.mdns.MdnsAnnouncer.AnnouncementInfo
+import com.android.server.connectivity.mdns.MdnsAnnouncer.BaseAnnouncementInfo
+import com.android.server.connectivity.mdns.MdnsAnnouncer.ExitAnnouncementInfo
+import com.android.server.connectivity.mdns.MdnsInterfaceAdvertiser.EXIT_ANNOUNCEMENT_DELAY_MS
+import com.android.server.connectivity.mdns.MdnsPacketRepeater.PacketRepeaterCallback
+import com.android.server.connectivity.mdns.MdnsProber.ProbingInfo
+import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
+import com.android.testutils.DevSdkIgnoreRunner
+import com.android.testutils.waitForIdle
+import java.net.InetSocketAddress
+import kotlin.test.assertContentEquals
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentCaptor
+import org.mockito.Mockito.any
+import org.mockito.Mockito.anyInt
+import org.mockito.Mockito.anyString
+import org.mockito.Mockito.doAnswer
+import org.mockito.Mockito.doReturn
+import org.mockito.Mockito.eq
+import org.mockito.Mockito.mock
+import org.mockito.Mockito.times
+import org.mockito.Mockito.verify
+
+private const val LOG_TAG = "testlogtag"
+private const val TIMEOUT_MS = 10_000L
+
+private val TEST_ADDRS = listOf(LinkAddress(parseNumericAddress("2001:db8::123"), 64))
+private val TEST_BUFFER = ByteArray(1300)
+
+private const val TEST_SERVICE_ID_1 = 42
+private val TEST_SERVICE_1 = NsdServiceInfo().apply {
+ serviceType = "_testservice._tcp"
+ serviceName = "MyTestService"
+ port = 12345
+}
+
+@RunWith(DevSdkIgnoreRunner::class)
+@IgnoreUpTo(Build.VERSION_CODES.S_V2)
+class MdnsInterfaceAdvertiserTest {
+ private val socket = mock(MdnsInterfaceSocket::class.java)
+ private val thread = HandlerThread(MdnsInterfaceAdvertiserTest::class.simpleName)
+ private val cb = mock(MdnsInterfaceAdvertiser.Callback::class.java)
+ private val deps = mock(MdnsInterfaceAdvertiser.Dependencies::class.java)
+ private val repository = mock(MdnsRecordRepository::class.java)
+ private val replySender = mock(MdnsReplySender::class.java)
+ private val announcer = mock(MdnsAnnouncer::class.java)
+ private val prober = mock(MdnsProber::class.java)
+ @Suppress("UNCHECKED_CAST")
+ private val probeCbCaptor = ArgumentCaptor.forClass(PacketRepeaterCallback::class.java)
+ as ArgumentCaptor<PacketRepeaterCallback<ProbingInfo>>
+ @Suppress("UNCHECKED_CAST")
+ private val announceCbCaptor = ArgumentCaptor.forClass(PacketRepeaterCallback::class.java)
+ as ArgumentCaptor<PacketRepeaterCallback<BaseAnnouncementInfo>>
+ private val packetHandlerCaptor = ArgumentCaptor.forClass(
+ MulticastPacketReader.PacketHandler::class.java)
+
+ private val probeCb get() = probeCbCaptor.value
+ private val announceCb get() = announceCbCaptor.value
+ private val packetHandler get() = packetHandlerCaptor.value
+
+ private val advertiser by lazy {
+ MdnsInterfaceAdvertiser(LOG_TAG, socket, TEST_ADDRS, thread.looper, TEST_BUFFER, cb, deps)
+ }
+
+ @Before
+ fun setUp() {
+ doReturn(repository).`when`(deps).makeRecordRepository(any())
+ doReturn(replySender).`when`(deps).makeReplySender(anyString(), any(), any(), any())
+ doReturn(announcer).`when`(deps).makeMdnsAnnouncer(anyString(), any(), any(), any())
+ doReturn(prober).`when`(deps).makeMdnsProber(anyString(), any(), any(), any())
+
+ val knownServices = mutableSetOf<Int>()
+ doAnswer { inv ->
+ knownServices.add(inv.getArgument(0))
+ -1
+ }.`when`(repository).addService(anyInt(), any())
+ doAnswer { inv ->
+ knownServices.remove(inv.getArgument(0))
+ null
+ }.`when`(repository).removeService(anyInt())
+ doAnswer {
+ knownServices.toIntArray().also { knownServices.clear() }
+ }.`when`(repository).clearServices()
+ doAnswer { inv ->
+ knownServices.contains(inv.getArgument(0))
+ }.`when`(repository).hasActiveService(anyInt())
+ thread.start()
+ advertiser.start()
+
+ verify(socket).addPacketHandler(packetHandlerCaptor.capture())
+ verify(deps).makeMdnsProber(any(), any(), any(), probeCbCaptor.capture())
+ verify(deps).makeMdnsAnnouncer(any(), any(), any(), announceCbCaptor.capture())
+ }
+
+ @After
+ fun tearDown() {
+ thread.quitSafely()
+ }
+
+ @Test
+ fun testAddRemoveService() {
+ val testAnnouncementInfo = addServiceAndFinishProbing(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+
+ verify(announcer).startSending(TEST_SERVICE_ID_1, testAnnouncementInfo,
+ 0L /* initialDelayMs */)
+
+ thread.waitForIdle(TIMEOUT_MS)
+ verify(cb).onRegisterServiceSucceeded(advertiser, TEST_SERVICE_ID_1)
+
+ // Remove the service: expect exit announcements
+ val testExitInfo = mock(ExitAnnouncementInfo::class.java)
+ doReturn(testExitInfo).`when`(repository).exitService(TEST_SERVICE_ID_1)
+ advertiser.removeService(TEST_SERVICE_ID_1)
+
+ verify(prober).stop(TEST_SERVICE_ID_1)
+ verify(announcer).stop(TEST_SERVICE_ID_1)
+ verify(announcer).startSending(TEST_SERVICE_ID_1, testExitInfo, EXIT_ANNOUNCEMENT_DELAY_MS)
+
+ // Exit announcements finish: the advertiser has no left service and destroys itself
+ announceCb.onFinished(testExitInfo)
+ thread.waitForIdle(TIMEOUT_MS)
+ verify(cb).onDestroyed(socket)
+ }
+
+ @Test
+ fun testDoubleRemove() {
+ addServiceAndFinishProbing(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+
+ val testExitInfo = mock(ExitAnnouncementInfo::class.java)
+ doReturn(testExitInfo).`when`(repository).exitService(TEST_SERVICE_ID_1)
+ advertiser.removeService(TEST_SERVICE_ID_1)
+
+ verify(prober).stop(TEST_SERVICE_ID_1)
+ verify(announcer).stop(TEST_SERVICE_ID_1)
+ verify(announcer).startSending(TEST_SERVICE_ID_1, testExitInfo, EXIT_ANNOUNCEMENT_DELAY_MS)
+
+ doReturn(false).`when`(repository).hasActiveService(TEST_SERVICE_ID_1)
+ advertiser.removeService(TEST_SERVICE_ID_1)
+ // Prober, announcer were still stopped only one time
+ verify(prober, times(1)).stop(TEST_SERVICE_ID_1)
+ verify(announcer, times(1)).stop(TEST_SERVICE_ID_1)
+ }
+
+ @Test
+ fun testReplyToQuery() {
+ addServiceAndFinishProbing(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+
+ val mockReply = mock(MdnsRecordRepository.ReplyInfo::class.java)
+ doReturn(mockReply).`when`(repository).getReply(any(), any())
+
+ // Query obtained with:
+ // scapy.raw(scapy.DNS(
+ // qd = scapy.DNSQR(qtype='PTR', qname='_testservice._tcp.local'))
+ // ).hex().upper()
+ val query = HexDump.hexStringToByteArray(
+ "0000010000010000000000000C5F7465737473657276696365045F746370056C6F63616C00000C0001"
+ )
+ val src = InetSocketAddress(parseNumericAddress("2001:db8::456"), MdnsConstants.MDNS_PORT)
+ packetHandler.handlePacket(query, query.size, src)
+
+ val packetCaptor = ArgumentCaptor.forClass(MdnsPacket::class.java)
+ verify(repository).getReply(packetCaptor.capture(), eq(src))
+
+ packetCaptor.value.let {
+ assertEquals(1, it.questions.size)
+ assertEquals(0, it.answers.size)
+ assertEquals(0, it.authorityRecords.size)
+ assertEquals(0, it.additionalRecords.size)
+
+ assertTrue(it.questions[0] is MdnsPointerRecord)
+ assertContentEquals(arrayOf("_testservice", "_tcp", "local"), it.questions[0].name)
+ }
+
+ verify(replySender).queueReply(mockReply)
+ }
+
+ @Test
+ fun testConflict() {
+ addServiceAndFinishProbing(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ doReturn(setOf(TEST_SERVICE_ID_1)).`when`(repository).getConflictingServices(any())
+
+ // Reply obtained with:
+ // scapy.raw(scapy.DNS(
+ // qd = None,
+ // an = scapy.DNSRR(type='TXT', rrname='_testservice._tcp.local'))
+ // ).hex().upper()
+ val query = HexDump.hexStringToByteArray("0000010000000001000000000C5F7465737473657276696" +
+ "365045F746370056C6F63616C0000100001000000000000")
+ val src = InetSocketAddress(parseNumericAddress("2001:db8::456"), MdnsConstants.MDNS_PORT)
+ packetHandler.handlePacket(query, query.size, src)
+
+ val packetCaptor = ArgumentCaptor.forClass(MdnsPacket::class.java)
+ verify(repository).getConflictingServices(packetCaptor.capture())
+
+ packetCaptor.value.let {
+ assertEquals(0, it.questions.size)
+ assertEquals(1, it.answers.size)
+ assertEquals(0, it.authorityRecords.size)
+ assertEquals(0, it.additionalRecords.size)
+
+ assertTrue(it.answers[0] is MdnsTextRecord)
+ assertContentEquals(arrayOf("_testservice", "_tcp", "local"), it.answers[0].name)
+ }
+
+ thread.waitForIdle(TIMEOUT_MS)
+ verify(cb).onServiceConflict(advertiser, TEST_SERVICE_ID_1)
+ }
+
+ @Test
+ fun testRestartProbingForConflict() {
+ val mockProbingInfo = mock(ProbingInfo::class.java)
+ doReturn(mockProbingInfo).`when`(repository).setServiceProbing(TEST_SERVICE_ID_1)
+
+ advertiser.restartProbingForConflict(TEST_SERVICE_ID_1)
+
+ verify(prober).restartForConflict(mockProbingInfo)
+ }
+
+ @Test
+ fun testRenameServiceForConflict() {
+ val mockProbingInfo = mock(ProbingInfo::class.java)
+ doReturn(mockProbingInfo).`when`(repository).renameServiceForConflict(
+ TEST_SERVICE_ID_1, TEST_SERVICE_1)
+
+ advertiser.renameServiceForConflict(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+
+ verify(prober).restartForConflict(mockProbingInfo)
+ }
+
+ private fun addServiceAndFinishProbing(serviceId: Int, serviceInfo: NsdServiceInfo):
+ AnnouncementInfo {
+ val testProbingInfo = mock(ProbingInfo::class.java)
+ doReturn(serviceId).`when`(testProbingInfo).serviceId
+ doReturn(testProbingInfo).`when`(repository).setServiceProbing(serviceId)
+
+ advertiser.addService(serviceId, serviceInfo)
+ verify(repository).addService(serviceId, serviceInfo)
+ verify(prober).startProbing(testProbingInfo)
+
+ // Simulate probing success: continues to announcing
+ val testAnnouncementInfo = mock(AnnouncementInfo::class.java)
+ doReturn(testAnnouncementInfo).`when`(repository).onProbingSucceeded(testProbingInfo)
+ probeCb.onFinished(testProbingInfo)
+ return testAnnouncementInfo
+ }
+}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClientTest.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClientTest.java
new file mode 100644
index 0000000..9d42a65
--- /dev/null
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClientTest.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns;
+
+import static com.android.server.connectivity.mdns.MdnsSocketProvider.SocketCallback;
+import static com.android.server.connectivity.mdns.MulticastPacketReader.PacketHandler;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
+
+import android.net.InetAddresses;
+import android.net.Network;
+import android.os.Build;
+import android.os.Handler;
+import android.os.HandlerThread;
+
+import com.android.net.module.util.HexDump;
+import com.android.testutils.DevSdkIgnoreRule;
+import com.android.testutils.DevSdkIgnoreRunner;
+import com.android.testutils.HandlerUtils;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.net.DatagramPacket;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.List;
+
+@RunWith(DevSdkIgnoreRunner.class)
+@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
+public class MdnsMultinetworkSocketClientTest {
+ private static final byte[] BUFFER = new byte[10];
+ private static final long DEFAULT_TIMEOUT = 2000L;
+ @Mock private Network mNetwork;
+ @Mock private MdnsSocketProvider mProvider;
+ @Mock private MdnsInterfaceSocket mSocket;
+ @Mock private MdnsServiceBrowserListener mListener;
+ @Mock private MdnsSocketClientBase.Callback mCallback;
+ private MdnsMultinetworkSocketClient mSocketClient;
+ private Handler mHandler;
+
+ @Before
+ public void setUp() throws SocketException {
+ MockitoAnnotations.initMocks(this);
+ final HandlerThread thread = new HandlerThread("MdnsMultinetworkSocketClientTest");
+ thread.start();
+ mHandler = new Handler(thread.getLooper());
+ mSocketClient = new MdnsMultinetworkSocketClient(thread.getLooper(), mProvider);
+ mHandler.post(() -> mSocketClient.setCallback(mCallback));
+ }
+
+ private SocketCallback expectSocketCallback() {
+ final ArgumentCaptor<SocketCallback> callbackCaptor =
+ ArgumentCaptor.forClass(SocketCallback.class);
+ mHandler.post(() -> mSocketClient.notifyNetworkRequested(mListener, mNetwork));
+ verify(mProvider, timeout(DEFAULT_TIMEOUT))
+ .requestSocket(eq(mNetwork), callbackCaptor.capture());
+ return callbackCaptor.getValue();
+ }
+
+ private NetworkInterface createEmptyNetworkInterface() {
+ try {
+ Constructor<NetworkInterface> constructor =
+ NetworkInterface.class.getDeclaredConstructor();
+ constructor.setAccessible(true);
+ return constructor.newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ public void testSendPacket() throws IOException {
+ final SocketCallback callback = expectSocketCallback();
+ final DatagramPacket ipv4Packet = new DatagramPacket(BUFFER, 0 /* offset */, BUFFER.length,
+ InetAddresses.parseNumericAddress("192.0.2.1"), 0 /* port */);
+ final DatagramPacket ipv6Packet = new DatagramPacket(BUFFER, 0 /* offset */, BUFFER.length,
+ InetAddresses.parseNumericAddress("2001:db8::"), 0 /* port */);
+ doReturn(true).when(mSocket).hasJoinedIpv4();
+ doReturn(true).when(mSocket).hasJoinedIpv6();
+ doReturn(createEmptyNetworkInterface()).when(mSocket).getInterface();
+ // Notify socket created
+ callback.onSocketCreated(mNetwork, mSocket, List.of());
+
+ // Send packet to IPv4 with target network and verify sending has been called.
+ mSocketClient.sendMulticastPacket(ipv4Packet, mNetwork);
+ HandlerUtils.waitForIdle(mHandler, DEFAULT_TIMEOUT);
+ verify(mSocket).send(ipv4Packet);
+
+ // Send packet to IPv6 without target network and verify sending has been called.
+ mSocketClient.sendMulticastPacket(ipv6Packet);
+ HandlerUtils.waitForIdle(mHandler, DEFAULT_TIMEOUT);
+ verify(mSocket).send(ipv6Packet);
+ }
+
+ @Test
+ public void testReceivePacket() {
+ final SocketCallback callback = expectSocketCallback();
+ final byte[] data = HexDump.hexStringToByteArray(
+ // scapy.raw(scapy.dns_compress(
+ // scapy.DNS(rd=0, qr=1, aa=1, qd = None,
+ // an =
+ // scapy.DNSRR(type='PTR', rrname='_testtype._tcp.local',
+ // rdata='testservice._testtype._tcp.local', rclass='IN', ttl=4500) /
+ // scapy.DNSRRSRV(rrname='testservice._testtype._tcp.local', rclass=0x8001,
+ // port=31234, target='Android.local', ttl=120))
+ // )).hex().upper()
+ "000084000000000200000000095F7465737474797065045F746370056C6F63616C00000C0001000011"
+ + "94000E0B7465737473657276696365C00CC02C00218001000000780010000000007A0207"
+ + "416E64726F6964C01B");
+
+ doReturn(createEmptyNetworkInterface()).when(mSocket).getInterface();
+ // Notify socket created
+ callback.onSocketCreated(mNetwork, mSocket, List.of());
+
+ final ArgumentCaptor<PacketHandler> handlerCaptor =
+ ArgumentCaptor.forClass(PacketHandler.class);
+ verify(mSocket).addPacketHandler(handlerCaptor.capture());
+
+ // Send the data and verify the received records.
+ final PacketHandler handler = handlerCaptor.getValue();
+ handler.handlePacket(data, data.length, null /* src */);
+ final ArgumentCaptor<MdnsResponse> responseCaptor =
+ ArgumentCaptor.forClass(MdnsResponse.class);
+ verify(mCallback).onResponseReceived(responseCaptor.capture());
+ final MdnsResponse response = responseCaptor.getValue();
+ assertTrue(response.hasPointerRecords());
+ assertArrayEquals("_testtype._tcp.local".split("\\."),
+ response.getPointerRecords().get(0).getName());
+ assertTrue(response.hasServiceRecord());
+ assertEquals("testservice", response.getServiceRecord().getServiceInstanceName());
+ assertEquals("Android.local".split("\\."),
+ response.getServiceRecord().getServiceHost());
+ }
+}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketTest.kt b/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketTest.kt
new file mode 100644
index 0000000..f88da1f
--- /dev/null
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketTest.kt
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns
+
+import android.net.InetAddresses
+import com.android.net.module.util.HexDump
+import com.android.testutils.DevSdkIgnoreRunner
+import kotlin.test.assertContentEquals
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@RunWith(DevSdkIgnoreRunner::class)
+class MdnsPacketTest {
+ @Test
+ fun testParseQuery() {
+ // Probe packet with 1 question for Android.local, and 4 additionalRecords with 4 addresses
+ // for Android.local (similar to legacy mdnsresponder probes, although it used to put 4
+ // identical questions(!!) for Android.local when there were 4 addresses).
+ val packetHex = "00000000000100000004000007416e64726f6964056c6f63616c0000ff0001c00c000100" +
+ "01000000780004c000027bc00c001c000100000078001020010db8000000000000000000000123c0" +
+ "0c001c000100000078001020010db8000000000000000000000456c00c001c000100000078001020" +
+ "010db8000000000000000000000789"
+
+ val bytes = HexDump.hexStringToByteArray(packetHex)
+ val reader = MdnsPacketReader(bytes, bytes.size)
+ val packet = MdnsPacket.parse(reader)
+
+ assertEquals(1, packet.questions.size)
+ assertEquals(0, packet.answers.size)
+ assertEquals(4, packet.authorityRecords.size)
+ assertEquals(0, packet.additionalRecords.size)
+
+ val hostname = arrayOf("Android", "local")
+ packet.questions[0].let {
+ assertTrue(it is MdnsAnyRecord)
+ assertContentEquals(hostname, it.name)
+ }
+
+ packet.authorityRecords.forEach {
+ assertTrue(it is MdnsInetAddressRecord)
+ assertContentEquals(hostname, it.name)
+ assertEquals(120000, it.ttl)
+ }
+
+ assertEquals(InetAddresses.parseNumericAddress("192.0.2.123"),
+ (packet.authorityRecords[0] as MdnsInetAddressRecord).inet4Address)
+ assertEquals(InetAddresses.parseNumericAddress("2001:db8::123"),
+ (packet.authorityRecords[1] as MdnsInetAddressRecord).inet6Address)
+ assertEquals(InetAddresses.parseNumericAddress("2001:db8::456"),
+ (packet.authorityRecords[2] as MdnsInetAddressRecord).inet6Address)
+ assertEquals(InetAddresses.parseNumericAddress("2001:db8::789"),
+ (packet.authorityRecords[3] as MdnsInetAddressRecord).inet6Address)
+ }
+}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsProberTest.kt b/tests/unit/java/com/android/server/connectivity/mdns/MdnsProberTest.kt
index 419121c..a2dbbc6 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsProberTest.kt
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsProberTest.kt
@@ -25,8 +25,6 @@
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
import com.android.testutils.DevSdkIgnoreRunner
import java.net.DatagramPacket
-import java.net.InetSocketAddress
-import java.net.MulticastSocket
import java.util.concurrent.CompletableFuture
import java.util.concurrent.TimeUnit
import kotlin.test.assertEquals
@@ -38,15 +36,13 @@
import org.mockito.ArgumentCaptor
import org.mockito.Mockito.any
import org.mockito.Mockito.atLeast
+import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.timeout
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
-private val destinationsSupplier = {
- listOf(InetSocketAddress(MdnsConstants.getMdnsIPv6Address(), MdnsConstants.MDNS_PORT)) }
-
private const val TEST_TIMEOUT_MS = 10_000L
private const val SHORT_TIMEOUT_MS = 200L
@@ -57,7 +53,7 @@
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
class MdnsProberTest {
private val thread = HandlerThread(MdnsProberTest::class.simpleName)
- private val socket = mock(MulticastSocket::class.java)
+ private val socket = mock(MdnsInterfaceSocket::class.java)
@Suppress("UNCHECKED_CAST")
private val cb = mock(MdnsPacketRepeater.PacketRepeaterCallback::class.java)
as MdnsPacketRepeater.PacketRepeaterCallback<ProbingInfo>
@@ -65,6 +61,7 @@
@Before
fun setUp() {
+ doReturn(true).`when`(socket).hasJoinedIpv6()
thread.start()
}
@@ -74,7 +71,7 @@
}
private class TestProbeInfo(probeRecords: List<MdnsRecord>, private val delayMs: Long = 1L) :
- ProbingInfo(1 /* serviceId */, probeRecords, destinationsSupplier) {
+ ProbingInfo(1 /* serviceId */, probeRecords) {
// Just send the packets quickly. Timing-related tests for MdnsPacketRepeater are already
// done in MdnsAnnouncerTest.
override fun getDelayMs(nextIndex: Int) = delayMs
@@ -117,7 +114,7 @@
@Test
fun testProbe() {
- val replySender = MdnsReplySender(thread.looper, socket, buffer)
+ val replySender = MdnsReplySender("testiface", thread.looper, socket, buffer)
val prober = TestProber(thread.looper, replySender, cb)
val probeInfo = TestProbeInfo(
listOf(makeServiceRecord(TEST_SERVICE_NAME_1, 37890)))
@@ -132,7 +129,7 @@
@Test
fun testProbeMultipleRecords() {
- val replySender = MdnsReplySender(thread.looper, socket, buffer)
+ val replySender = MdnsReplySender("testiface", thread.looper, socket, buffer)
val prober = TestProber(thread.looper, replySender, cb)
val probeInfo = TestProbeInfo(listOf(
makeServiceRecord(TEST_SERVICE_NAME_1, 37890),
@@ -170,7 +167,7 @@
@Test
fun testStopProbing() {
- val replySender = MdnsReplySender(thread.looper, socket, buffer)
+ val replySender = MdnsReplySender("testiface", thread.looper, socket, buffer)
val prober = TestProber(thread.looper, replySender, cb)
val probeInfo = TestProbeInfo(
listOf(makeServiceRecord(TEST_SERVICE_NAME_1, 37890)),
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsRecordRepositoryTest.kt b/tests/unit/java/com/android/server/connectivity/mdns/MdnsRecordRepositoryTest.kt
new file mode 100644
index 0000000..ecc11ec
--- /dev/null
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsRecordRepositoryTest.kt
@@ -0,0 +1,470 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.connectivity.mdns
+
+import android.net.InetAddresses.parseNumericAddress
+import android.net.LinkAddress
+import android.net.nsd.NsdServiceInfo
+import android.os.Build
+import android.os.HandlerThread
+import com.android.server.connectivity.mdns.MdnsAnnouncer.AnnouncementInfo
+import com.android.server.connectivity.mdns.MdnsRecordRepository.Dependencies
+import com.android.server.connectivity.mdns.MdnsRecordRepository.getReverseDnsAddress
+import com.android.server.connectivity.mdns.MdnsServiceInfo.TextEntry
+import com.android.testutils.DevSdkIgnoreRule
+import com.android.testutils.DevSdkIgnoreRunner
+import java.net.InetSocketAddress
+import java.net.NetworkInterface
+import java.util.Collections
+import kotlin.test.assertContentEquals
+import kotlin.test.assertEquals
+import kotlin.test.assertFailsWith
+import kotlin.test.assertFalse
+import kotlin.test.assertNotNull
+import kotlin.test.assertTrue
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+
+private const val TEST_SERVICE_ID_1 = 42
+private const val TEST_SERVICE_ID_2 = 43
+private const val TEST_PORT = 12345
+private val TEST_HOSTNAME = arrayOf("Android_000102030405060708090A0B0C0D0E0F", "local")
+private val TEST_ADDRESSES = listOf(
+ LinkAddress(parseNumericAddress("192.0.2.111"), 24),
+ LinkAddress(parseNumericAddress("2001:db8::111"), 64),
+ LinkAddress(parseNumericAddress("2001:db8::222"), 64))
+
+private val TEST_SERVICE_1 = NsdServiceInfo().apply {
+ serviceType = "_testservice._tcp"
+ serviceName = "MyTestService"
+ port = TEST_PORT
+}
+
+private val TEST_SERVICE_2 = NsdServiceInfo().apply {
+ serviceType = "_testservice._tcp"
+ serviceName = "MyOtherTestService"
+ port = TEST_PORT
+}
+
+@RunWith(DevSdkIgnoreRunner::class)
+@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
+class MdnsRecordRepositoryTest {
+ private val thread = HandlerThread(MdnsRecordRepositoryTest::class.simpleName)
+ private val deps = object : Dependencies() {
+ override fun getHostname() = TEST_HOSTNAME
+ override fun getInterfaceInetAddresses(iface: NetworkInterface) =
+ Collections.enumeration(TEST_ADDRESSES.map { it.address })
+ }
+
+ @Before
+ fun setUp() {
+ thread.start()
+ }
+
+ @After
+ fun tearDown() {
+ thread.quitSafely()
+ }
+
+ @Test
+ fun testAddServiceAndProbe() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ assertEquals(0, repository.servicesCount)
+ assertEquals(-1, repository.addService(TEST_SERVICE_ID_1, TEST_SERVICE_1))
+ assertEquals(1, repository.servicesCount)
+
+ val probingInfo = repository.setServiceProbing(TEST_SERVICE_ID_1)
+ assertNotNull(probingInfo)
+ assertTrue(repository.isProbing(TEST_SERVICE_ID_1))
+
+ assertEquals(TEST_SERVICE_ID_1, probingInfo.serviceId)
+ val packet = probingInfo.getPacket(0)
+
+ assertEquals(MdnsConstants.FLAGS_QUERY, packet.flags)
+ assertEquals(0, packet.answers.size)
+ assertEquals(0, packet.additionalRecords.size)
+
+ assertEquals(1, packet.questions.size)
+ val expectedName = arrayOf("MyTestService", "_testservice", "_tcp", "local")
+ assertEquals(MdnsAnyRecord(expectedName, false /* unicast */), packet.questions[0])
+
+ assertEquals(1, packet.authorityRecords.size)
+ assertEquals(MdnsServiceRecord(expectedName,
+ 0L /* receiptTimeMillis */,
+ false /* cacheFlush */,
+ 120_000L /* ttlMillis */,
+ 0 /* servicePriority */, 0 /* serviceWeight */,
+ TEST_PORT, TEST_HOSTNAME), packet.authorityRecords[0])
+
+ assertContentEquals(intArrayOf(TEST_SERVICE_ID_1), repository.clearServices())
+ }
+
+ @Test
+ fun testAddAndConflicts() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ repository.addService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ assertFailsWith(NameConflictException::class) {
+ repository.addService(TEST_SERVICE_ID_2, TEST_SERVICE_1)
+ }
+ }
+
+ @Test
+ fun testInvalidReuseOfServiceId() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ repository.addService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ assertFailsWith(IllegalArgumentException::class) {
+ repository.addService(TEST_SERVICE_ID_1, TEST_SERVICE_2)
+ }
+ }
+
+ @Test
+ fun testHasActiveService() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ assertFalse(repository.hasActiveService(TEST_SERVICE_ID_1))
+
+ repository.addService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ assertTrue(repository.hasActiveService(TEST_SERVICE_ID_1))
+
+ val probingInfo = repository.setServiceProbing(TEST_SERVICE_ID_1)
+ repository.onProbingSucceeded(probingInfo)
+ repository.onAdvertisementSent(TEST_SERVICE_ID_1)
+ assertTrue(repository.hasActiveService(TEST_SERVICE_ID_1))
+
+ repository.exitService(TEST_SERVICE_ID_1)
+ assertFalse(repository.hasActiveService(TEST_SERVICE_ID_1))
+ }
+
+ @Test
+ fun testExitAnnouncements() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ repository.initWithService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ repository.onAdvertisementSent(TEST_SERVICE_ID_1)
+
+ val exitAnnouncement = repository.exitService(TEST_SERVICE_ID_1)
+ assertNotNull(exitAnnouncement)
+ assertEquals(1, repository.servicesCount)
+ val packet = exitAnnouncement.getPacket(0)
+
+ assertEquals(0x8400 /* response, authoritative */, packet.flags)
+ assertEquals(0, packet.questions.size)
+ assertEquals(0, packet.authorityRecords.size)
+ assertEquals(0, packet.additionalRecords.size)
+
+ assertContentEquals(listOf(
+ MdnsPointerRecord(
+ arrayOf("_testservice", "_tcp", "local"),
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 0L /* ttlMillis */,
+ arrayOf("MyTestService", "_testservice", "_tcp", "local"))
+ ), packet.answers)
+
+ repository.removeService(TEST_SERVICE_ID_1)
+ assertEquals(0, repository.servicesCount)
+ }
+
+ @Test
+ fun testExitingServiceReAdded() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ repository.initWithService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ repository.onAdvertisementSent(TEST_SERVICE_ID_1)
+ repository.exitService(TEST_SERVICE_ID_1)
+
+ assertEquals(TEST_SERVICE_ID_1, repository.addService(TEST_SERVICE_ID_2, TEST_SERVICE_1))
+ assertEquals(1, repository.servicesCount)
+
+ repository.removeService(TEST_SERVICE_ID_2)
+ assertEquals(0, repository.servicesCount)
+ }
+
+ @Test
+ fun testOnProbingSucceeded() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ val announcementInfo = repository.initWithService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ repository.onAdvertisementSent(TEST_SERVICE_ID_1)
+ val packet = announcementInfo.getPacket(0)
+
+ assertEquals(0x8400 /* response, authoritative */, packet.flags)
+ assertEquals(0, packet.questions.size)
+ assertEquals(0, packet.authorityRecords.size)
+
+ val serviceType = arrayOf("_testservice", "_tcp", "local")
+ val serviceName = arrayOf("MyTestService", "_testservice", "_tcp", "local")
+ val v4AddrRev = getReverseDnsAddress(TEST_ADDRESSES[0].address)
+ val v6Addr1Rev = getReverseDnsAddress(TEST_ADDRESSES[1].address)
+ val v6Addr2Rev = getReverseDnsAddress(TEST_ADDRESSES[2].address)
+
+ assertContentEquals(listOf(
+ // Reverse address and address records for the hostname
+ MdnsPointerRecord(v4AddrRev,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ TEST_HOSTNAME),
+ MdnsInetAddressRecord(TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ TEST_ADDRESSES[0].address),
+ MdnsPointerRecord(v6Addr1Rev,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ TEST_HOSTNAME),
+ MdnsInetAddressRecord(TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ TEST_ADDRESSES[1].address),
+ MdnsPointerRecord(v6Addr2Rev,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ TEST_HOSTNAME),
+ MdnsInetAddressRecord(TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ TEST_ADDRESSES[2].address),
+ // Service registration records (RFC6763)
+ MdnsPointerRecord(
+ serviceType,
+ 0L /* receiptTimeMillis */,
+ // Not a unique name owned by the announcer, so cacheFlush=false
+ false /* cacheFlush */,
+ 4500000L /* ttlMillis */,
+ serviceName),
+ MdnsServiceRecord(
+ serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ 0 /* servicePriority */,
+ 0 /* serviceWeight */,
+ TEST_PORT /* servicePort */,
+ TEST_HOSTNAME),
+ MdnsTextRecord(
+ serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 4500000L /* ttlMillis */,
+ emptyList() /* entries */),
+ // Service type enumeration record (RFC6763 9.)
+ MdnsPointerRecord(
+ arrayOf("_services", "_dns-sd", "_udp", "local"),
+ 0L /* receiptTimeMillis */,
+ false /* cacheFlush */,
+ 4500000L /* ttlMillis */,
+ serviceType)
+ ), packet.answers)
+
+ assertContentEquals(listOf(
+ MdnsNsecRecord(v4AddrRev,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ v4AddrRev,
+ intArrayOf(MdnsRecord.TYPE_PTR)),
+ MdnsNsecRecord(TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ TEST_HOSTNAME,
+ intArrayOf(MdnsRecord.TYPE_A, MdnsRecord.TYPE_AAAA)),
+ MdnsNsecRecord(v6Addr1Rev,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ v6Addr1Rev,
+ intArrayOf(MdnsRecord.TYPE_PTR)),
+ MdnsNsecRecord(v6Addr2Rev,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 120000L /* ttlMillis */,
+ v6Addr2Rev,
+ intArrayOf(MdnsRecord.TYPE_PTR)),
+ MdnsNsecRecord(serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ 4500000L /* ttlMillis */,
+ serviceName,
+ intArrayOf(MdnsRecord.TYPE_TXT, MdnsRecord.TYPE_SRV))
+ ), packet.additionalRecords)
+ }
+
+ @Test
+ fun testGetReverseDnsAddress() {
+ val expectedV6 = "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.0.1.0.0.2.ip6.arpa"
+ .split(".").toTypedArray()
+ assertContentEquals(expectedV6, getReverseDnsAddress(parseNumericAddress("2001:db8::1")))
+ val expectedV4 = "123.2.0.192.in-addr.arpa".split(".").toTypedArray()
+ assertContentEquals(expectedV4, getReverseDnsAddress(parseNumericAddress("192.0.2.123")))
+ }
+
+ @Test
+ fun testGetReply() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ repository.initWithService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ val questions = listOf(MdnsPointerRecord(arrayOf("_testservice", "_tcp", "local"),
+ 0L /* receiptTimeMillis */,
+ false /* cacheFlush */,
+ // TTL and data is empty for a question
+ 0L /* ttlMillis */,
+ null /* pointer */))
+ val query = MdnsPacket(0 /* flags */, questions, listOf() /* answers */,
+ listOf() /* authorityRecords */, listOf() /* additionalRecords */)
+ val src = InetSocketAddress(parseNumericAddress("192.0.2.123"), 5353)
+ val reply = repository.getReply(query, src)
+
+ assertNotNull(reply)
+ // Source address is IPv4
+ assertEquals(MdnsConstants.getMdnsIPv4Address(), reply.destination.address)
+ assertEquals(MdnsConstants.MDNS_PORT, reply.destination.port)
+
+ // TTLs as per RFC6762 10.
+ val longTtl = 4_500_000L
+ val shortTtl = 120_000L
+ val serviceName = arrayOf("MyTestService", "_testservice", "_tcp", "local")
+
+ assertEquals(listOf(
+ MdnsPointerRecord(
+ arrayOf("_testservice", "_tcp", "local"),
+ 0L /* receiptTimeMillis */,
+ false /* cacheFlush */,
+ longTtl,
+ serviceName),
+ ), reply.answers)
+
+ assertEquals(listOf(
+ MdnsTextRecord(
+ serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ longTtl,
+ listOf() /* entries */),
+ MdnsServiceRecord(
+ serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ shortTtl,
+ 0 /* servicePriority */,
+ 0 /* serviceWeight */,
+ TEST_PORT,
+ TEST_HOSTNAME),
+ MdnsInetAddressRecord(
+ TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ shortTtl,
+ TEST_ADDRESSES[0].address),
+ MdnsInetAddressRecord(
+ TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ shortTtl,
+ TEST_ADDRESSES[1].address),
+ MdnsInetAddressRecord(
+ TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ shortTtl,
+ TEST_ADDRESSES[2].address),
+ MdnsNsecRecord(
+ serviceName,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ longTtl,
+ serviceName /* nextDomain */,
+ intArrayOf(MdnsRecord.TYPE_TXT, MdnsRecord.TYPE_SRV)),
+ MdnsNsecRecord(
+ TEST_HOSTNAME,
+ 0L /* receiptTimeMillis */,
+ true /* cacheFlush */,
+ shortTtl,
+ TEST_HOSTNAME /* nextDomain */,
+ intArrayOf(MdnsRecord.TYPE_A, MdnsRecord.TYPE_AAAA)),
+ ), reply.additionalAnswers)
+ }
+
+ @Test
+ fun testGetConflictingServices() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ repository.addService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ repository.addService(TEST_SERVICE_ID_2, TEST_SERVICE_2)
+
+ val packet = MdnsPacket(
+ 0 /* flags */,
+ emptyList() /* questions */,
+ listOf(
+ MdnsServiceRecord(
+ arrayOf("MyTestService", "_testservice", "_tcp", "local"),
+ 0L /* receiptTimeMillis */, true /* cacheFlush */, 0L /* ttlMillis */,
+ 0 /* servicePriority */, 0 /* serviceWeight */,
+ TEST_SERVICE_1.port + 1,
+ TEST_HOSTNAME),
+ MdnsTextRecord(
+ arrayOf("MyOtherTestService", "_testservice", "_tcp", "local"),
+ 0L /* receiptTimeMillis */, true /* cacheFlush */, 0L /* ttlMillis */,
+ listOf(TextEntry.fromString("somedifferent=entry"))),
+ ) /* answers */,
+ emptyList() /* authorityRecords */,
+ emptyList() /* additionalRecords */)
+
+ assertEquals(setOf(TEST_SERVICE_ID_1, TEST_SERVICE_ID_2),
+ repository.getConflictingServices(packet))
+ }
+
+ @Test
+ fun testGetConflictingServices_IdenticalService() {
+ val repository = MdnsRecordRepository(thread.looper, deps)
+ repository.addService(TEST_SERVICE_ID_1, TEST_SERVICE_1)
+ repository.addService(TEST_SERVICE_ID_2, TEST_SERVICE_2)
+
+ val otherTtlMillis = 1234L
+ val packet = MdnsPacket(
+ 0 /* flags */,
+ emptyList() /* questions */,
+ listOf(
+ MdnsServiceRecord(
+ arrayOf("MyTestService", "_testservice", "_tcp", "local"),
+ 0L /* receiptTimeMillis */, true /* cacheFlush */,
+ otherTtlMillis, 0 /* servicePriority */, 0 /* serviceWeight */,
+ TEST_SERVICE_1.port,
+ TEST_HOSTNAME),
+ MdnsTextRecord(
+ arrayOf("MyOtherTestService", "_testservice", "_tcp", "local"),
+ 0L /* receiptTimeMillis */, true /* cacheFlush */,
+ otherTtlMillis, emptyList()),
+ ) /* answers */,
+ emptyList() /* authorityRecords */,
+ emptyList() /* additionalRecords */)
+
+ // Above records are identical to the actual registrations: no conflict
+ assertEquals(emptySet(), repository.getConflictingServices(packet))
+ }
+}
+
+private fun MdnsRecordRepository.initWithService(serviceId: Int, serviceInfo: NsdServiceInfo):
+ AnnouncementInfo {
+ updateAddresses(TEST_ADDRESSES)
+ addService(serviceId, serviceInfo)
+ val probingInfo = setServiceProbing(serviceId)
+ assertNotNull(probingInfo)
+ return onProbingSucceeded(probingInfo)
+}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java
index 697116c..a45ca68 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsServiceTypeClientTests.java
@@ -62,7 +62,7 @@
import java.net.DatagramPacket;
import java.net.Inet4Address;
import java.net.Inet6Address;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -80,7 +80,10 @@
private static final int INTERFACE_INDEX = 999;
private static final String SERVICE_TYPE = "_googlecast._tcp.local";
private static final String[] SERVICE_TYPE_LABELS = TextUtils.split(SERVICE_TYPE, "\\.");
- private static final Network NETWORK = mock(Network.class);
+ private static final InetSocketAddress IPV4_ADDRESS = new InetSocketAddress(
+ MdnsConstants.getMdnsIPv4Address(), MdnsConstants.MDNS_PORT);
+ private static final InetSocketAddress IPV6_ADDRESS = new InetSocketAddress(
+ MdnsConstants.getMdnsIPv6Address(), MdnsConstants.MDNS_PORT);
@Mock
private MdnsServiceBrowserListener mockListenerOne;
@@ -89,13 +92,16 @@
@Mock
private MdnsPacketWriter mockPacketWriter;
@Mock
- private MdnsSocketClient mockSocketClient;
+ private MdnsMultinetworkSocketClient mockSocketClient;
+ @Mock
+ private Network mockNetwork;
@Captor
private ArgumentCaptor<MdnsServiceInfo> serviceInfoCaptor;
private final byte[] buf = new byte[10];
- private DatagramPacket[] expectedPackets;
+ private DatagramPacket[] expectedIPv4Packets;
+ private DatagramPacket[] expectedIPv6Packets;
private ScheduledFuture<?>[] expectedSendFutures;
private FakeExecutor currentThreadExecutor = new FakeExecutor();
@@ -106,30 +112,52 @@
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
- expectedPackets = new DatagramPacket[16];
+ expectedIPv4Packets = new DatagramPacket[16];
+ expectedIPv6Packets = new DatagramPacket[16];
expectedSendFutures = new ScheduledFuture<?>[16];
for (int i = 0; i < expectedSendFutures.length; ++i) {
- expectedPackets[i] = new DatagramPacket(buf, 0, 5);
+ expectedIPv4Packets[i] = new DatagramPacket(buf, 0 /* offset */, 5 /* length */,
+ MdnsConstants.getMdnsIPv4Address(), MdnsConstants.MDNS_PORT);
+ expectedIPv6Packets[i] = new DatagramPacket(buf, 0 /* offset */, 5 /* length */,
+ MdnsConstants.getMdnsIPv6Address(), MdnsConstants.MDNS_PORT);
expectedSendFutures[i] = Mockito.mock(ScheduledFuture.class);
}
- when(mockPacketWriter.getPacket(any(SocketAddress.class)))
- .thenReturn(expectedPackets[0])
- .thenReturn(expectedPackets[1])
- .thenReturn(expectedPackets[2])
- .thenReturn(expectedPackets[3])
- .thenReturn(expectedPackets[4])
- .thenReturn(expectedPackets[5])
- .thenReturn(expectedPackets[6])
- .thenReturn(expectedPackets[7])
- .thenReturn(expectedPackets[8])
- .thenReturn(expectedPackets[9])
- .thenReturn(expectedPackets[10])
- .thenReturn(expectedPackets[11])
- .thenReturn(expectedPackets[12])
- .thenReturn(expectedPackets[13])
- .thenReturn(expectedPackets[14])
- .thenReturn(expectedPackets[15]);
+ when(mockPacketWriter.getPacket(IPV4_ADDRESS))
+ .thenReturn(expectedIPv4Packets[0])
+ .thenReturn(expectedIPv4Packets[1])
+ .thenReturn(expectedIPv4Packets[2])
+ .thenReturn(expectedIPv4Packets[3])
+ .thenReturn(expectedIPv4Packets[4])
+ .thenReturn(expectedIPv4Packets[5])
+ .thenReturn(expectedIPv4Packets[6])
+ .thenReturn(expectedIPv4Packets[7])
+ .thenReturn(expectedIPv4Packets[8])
+ .thenReturn(expectedIPv4Packets[9])
+ .thenReturn(expectedIPv4Packets[10])
+ .thenReturn(expectedIPv4Packets[11])
+ .thenReturn(expectedIPv4Packets[12])
+ .thenReturn(expectedIPv4Packets[13])
+ .thenReturn(expectedIPv4Packets[14])
+ .thenReturn(expectedIPv4Packets[15]);
+
+ when(mockPacketWriter.getPacket(IPV6_ADDRESS))
+ .thenReturn(expectedIPv6Packets[0])
+ .thenReturn(expectedIPv6Packets[1])
+ .thenReturn(expectedIPv6Packets[2])
+ .thenReturn(expectedIPv6Packets[3])
+ .thenReturn(expectedIPv6Packets[4])
+ .thenReturn(expectedIPv6Packets[5])
+ .thenReturn(expectedIPv6Packets[6])
+ .thenReturn(expectedIPv6Packets[7])
+ .thenReturn(expectedIPv6Packets[8])
+ .thenReturn(expectedIPv6Packets[9])
+ .thenReturn(expectedIPv6Packets[10])
+ .thenReturn(expectedIPv6Packets[11])
+ .thenReturn(expectedIPv6Packets[12])
+ .thenReturn(expectedIPv6Packets[13])
+ .thenReturn(expectedIPv6Packets[14])
+ .thenReturn(expectedIPv6Packets[15]);
client =
new MdnsServiceTypeClient(SERVICE_TYPE, mockSocketClient, currentThreadExecutor) {
@@ -282,8 +310,8 @@
//MdnsConfigsFlagsImpl.alwaysAskForUnicastResponseInEachBurst.override(true);
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(false).build();
- QueryTaskConfig config =
- new QueryTaskConfig(searchOptions.getSubtypes(), searchOptions.isPassiveMode(), 1);
+ QueryTaskConfig config = new QueryTaskConfig(
+ searchOptions.getSubtypes(), searchOptions.isPassiveMode(), 1, mockNetwork);
// This is the first query. We will ask for unicast response.
assertTrue(config.expectUnicastResponse);
@@ -311,8 +339,8 @@
public void testQueryTaskConfig_askForUnicastInFirstQuery() {
MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(false).build();
- QueryTaskConfig config =
- new QueryTaskConfig(searchOptions.getSubtypes(), searchOptions.isPassiveMode(), 1);
+ QueryTaskConfig config = new QueryTaskConfig(
+ searchOptions.getSubtypes(), searchOptions.isPassiveMode(), 1, mockNetwork);
// This is the first query. We will ask for unicast response.
assertTrue(config.expectUnicastResponse);
@@ -409,7 +437,7 @@
MdnsResponse response = mock(MdnsResponse.class);
when(response.getServiceInstanceName()).thenReturn("service-instance-1");
doReturn(INTERFACE_INDEX).when(response).getInterfaceIndex();
- doReturn(NETWORK).when(response).getNetwork();
+ doReturn(mockNetwork).when(response).getNetwork();
when(response.isComplete()).thenReturn(false);
client.processResponse(response);
@@ -423,7 +451,7 @@
List.of() /* subTypes */,
Collections.singletonMap("key", null) /* attributes */,
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
verify(mockListenerOne, never()).onServiceFound(any(MdnsServiceInfo.class));
verify(mockListenerOne, never()).onServiceUpdated(any(MdnsServiceInfo.class));
@@ -443,7 +471,7 @@
/* subtype= */ "ABCDE",
Collections.emptyMap(),
/* interfaceIndex= */ 20,
- NETWORK);
+ mockNetwork);
client.processResponse(initialResponse);
// Process a second response with a different port and updated text attributes.
@@ -455,7 +483,7 @@
/* subtype= */ "ABCDE",
Collections.singletonMap("key", "value"),
/* interfaceIndex= */ 20,
- NETWORK);
+ mockNetwork);
client.processResponse(secondResponse);
// Verify onServiceNameDiscovered was called once for the initial response.
@@ -469,7 +497,7 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", null) /* attributes */,
20 /* interfaceIndex */,
- NETWORK);
+ mockNetwork);
// Verify onServiceFound was called once for the initial response.
verify(mockListenerOne).onServiceFound(serviceInfoCaptor.capture());
@@ -480,7 +508,7 @@
assertEquals(initialServiceInfo.getSubtypes(), Collections.singletonList("ABCDE"));
assertNull(initialServiceInfo.getAttributeByKey("key"));
assertEquals(initialServiceInfo.getInterfaceIndex(), 20);
- assertEquals(NETWORK, initialServiceInfo.getNetwork());
+ assertEquals(mockNetwork, initialServiceInfo.getNetwork());
// Verify onServiceUpdated was called once for the second response.
verify(mockListenerOne).onServiceUpdated(serviceInfoCaptor.capture());
@@ -492,7 +520,7 @@
assertEquals(updatedServiceInfo.getSubtypes(), Collections.singletonList("ABCDE"));
assertEquals(updatedServiceInfo.getAttributeByKey("key"), "value");
assertEquals(updatedServiceInfo.getInterfaceIndex(), 20);
- assertEquals(NETWORK, updatedServiceInfo.getNetwork());
+ assertEquals(mockNetwork, updatedServiceInfo.getNetwork());
}
@Test
@@ -509,7 +537,7 @@
/* subtype= */ "ABCDE",
Collections.emptyMap(),
/* interfaceIndex= */ 20,
- NETWORK);
+ mockNetwork);
client.processResponse(initialResponse);
// Process a second response with a different port and updated text attributes.
@@ -521,7 +549,7 @@
/* subtype= */ "ABCDE",
Collections.singletonMap("key", "value"),
/* interfaceIndex= */ 20,
- NETWORK);
+ mockNetwork);
client.processResponse(secondResponse);
System.out.println("secondResponses ip"
@@ -538,7 +566,7 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", null) /* attributes */,
20 /* interfaceIndex */,
- NETWORK);
+ mockNetwork);
// Verify onServiceFound was called once for the initial response.
verify(mockListenerOne).onServiceFound(serviceInfoCaptor.capture());
@@ -549,7 +577,7 @@
assertEquals(initialServiceInfo.getSubtypes(), Collections.singletonList("ABCDE"));
assertNull(initialServiceInfo.getAttributeByKey("key"));
assertEquals(initialServiceInfo.getInterfaceIndex(), 20);
- assertEquals(NETWORK, initialServiceInfo.getNetwork());
+ assertEquals(mockNetwork, initialServiceInfo.getNetwork());
// Verify onServiceUpdated was called once for the second response.
verify(mockListenerOne).onServiceUpdated(serviceInfoCaptor.capture());
@@ -561,7 +589,7 @@
assertEquals(updatedServiceInfo.getSubtypes(), Collections.singletonList("ABCDE"));
assertEquals(updatedServiceInfo.getAttributeByKey("key"), "value");
assertEquals(updatedServiceInfo.getInterfaceIndex(), 20);
- assertEquals(NETWORK, updatedServiceInfo.getNetwork());
+ assertEquals(mockNetwork, updatedServiceInfo.getNetwork());
}
private void verifyServiceRemovedNoCallback(MdnsServiceBrowserListener listener) {
@@ -599,12 +627,12 @@
/* subtype= */ "ABCDE",
Collections.emptyMap(),
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
client.processResponse(initialResponse);
MdnsResponse response = mock(MdnsResponse.class);
doReturn("goodbye-service").when(response).getServiceInstanceName();
doReturn(INTERFACE_INDEX).when(response).getInterfaceIndex();
- doReturn(NETWORK).when(response).getNetwork();
+ doReturn(mockNetwork).when(response).getNetwork();
doReturn(true).when(response).isGoodbye();
client.processResponse(response);
// Verify removed callback won't be called if the service is not existed.
@@ -615,9 +643,9 @@
doReturn(serviceName).when(response).getServiceInstanceName();
client.processResponse(response);
verifyServiceRemovedCallback(
- mockListenerOne, serviceName, SERVICE_TYPE_LABELS, INTERFACE_INDEX, NETWORK);
+ mockListenerOne, serviceName, SERVICE_TYPE_LABELS, INTERFACE_INDEX, mockNetwork);
verifyServiceRemovedCallback(
- mockListenerTwo, serviceName, SERVICE_TYPE_LABELS, INTERFACE_INDEX, NETWORK);
+ mockListenerTwo, serviceName, SERVICE_TYPE_LABELS, INTERFACE_INDEX, mockNetwork);
}
@Test
@@ -631,7 +659,7 @@
/* subtype= */ "ABCDE",
Collections.emptyMap(),
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
client.processResponse(initialResponse);
client.startSendAndReceive(mockListenerOne, MdnsSearchOptions.getDefaultOptions());
@@ -647,7 +675,7 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", null) /* attributes */,
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
// Verify onServiceFound was called once for the existing response.
verify(mockListenerOne).onServiceFound(serviceInfoCaptor.capture());
@@ -684,7 +712,7 @@
MdnsResponse initialResponse =
createMockResponse(
serviceInstanceName, "192.168.1.1", 5353, List.of("ABCDE"),
- Map.of(), INTERFACE_INDEX, NETWORK);
+ Map.of(), INTERFACE_INDEX, mockNetwork);
client.processResponse(initialResponse);
// Clear the scheduled runnable.
@@ -718,7 +746,7 @@
MdnsResponse initialResponse =
createMockResponse(
serviceInstanceName, "192.168.1.1", 5353, List.of("ABCDE"),
- Map.of(), INTERFACE_INDEX, NETWORK);
+ Map.of(), INTERFACE_INDEX, mockNetwork);
client.processResponse(initialResponse);
// Clear the scheduled runnable.
@@ -737,7 +765,7 @@
// Verify removed callback was called.
verifyServiceRemovedCallback(mockListenerOne, serviceInstanceName, SERVICE_TYPE_LABELS,
- INTERFACE_INDEX, NETWORK);
+ INTERFACE_INDEX, mockNetwork);
}
@Test
@@ -758,7 +786,7 @@
MdnsResponse initialResponse =
createMockResponse(
serviceInstanceName, "192.168.1.1", 5353, List.of("ABCDE"),
- Map.of(), INTERFACE_INDEX, NETWORK);
+ Map.of(), INTERFACE_INDEX, mockNetwork);
client.processResponse(initialResponse);
// Clear the scheduled runnable.
@@ -792,7 +820,7 @@
MdnsResponse initialResponse =
createMockResponse(
serviceInstanceName, "192.168.1.1", 5353, List.of("ABCDE"),
- Map.of(), INTERFACE_INDEX, NETWORK);
+ Map.of(), INTERFACE_INDEX, mockNetwork);
client.processResponse(initialResponse);
// Clear the scheduled runnable.
@@ -804,7 +832,7 @@
// Verify removed callback was called.
verifyServiceRemovedCallback(mockListenerOne, serviceInstanceName, SERVICE_TYPE_LABELS,
- INTERFACE_INDEX, NETWORK);
+ INTERFACE_INDEX, mockNetwork);
}
@Test
@@ -824,7 +852,7 @@
"ABCDE" /* subtype */,
Collections.emptyMap(),
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
client.processResponse(initialResponse);
// Process a second response which has ip address to make response become complete.
@@ -836,7 +864,7 @@
"ABCDE" /* subtype */,
Collections.emptyMap(),
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
client.processResponse(secondResponse);
// Process a third response with a different ip address, port and updated text attributes.
@@ -848,7 +876,7 @@
"ABCDE" /* subtype */,
Collections.singletonMap("key", "value"),
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
client.processResponse(thirdResponse);
// Process the last response which is goodbye message.
@@ -868,7 +896,7 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", null) /* attributes */,
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
// Verify onServiceFound was second called for the second response.
inOrder.verify(mockListenerOne).onServiceFound(serviceInfoCaptor.capture());
@@ -881,7 +909,7 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", null) /* attributes */,
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
// Verify onServiceUpdated was third called for the third response.
inOrder.verify(mockListenerOne).onServiceUpdated(serviceInfoCaptor.capture());
@@ -894,7 +922,7 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", "value") /* attributes */,
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
// Verify onServiceRemoved was called for the last response.
inOrder.verify(mockListenerOne).onServiceRemoved(serviceInfoCaptor.capture());
@@ -907,7 +935,7 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", "value") /* attributes */,
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
// Verify onServiceNameRemoved was called for the last response.
inOrder.verify(mockListenerOne).onServiceNameRemoved(serviceInfoCaptor.capture());
@@ -920,18 +948,34 @@
Collections.singletonList("ABCDE") /* subTypes */,
Collections.singletonMap("key", "value") /* attributes */,
INTERFACE_INDEX,
- NETWORK);
+ mockNetwork);
}
// verifies that the right query was enqueued with the right delay, and send query by executing
// the runnable.
private void verifyAndSendQuery(int index, long timeInMs, boolean expectsUnicastResponse) {
+ verifyAndSendQuery(
+ index, timeInMs, expectsUnicastResponse, true /* multipleSocketDiscovery */);
+ }
+
+ private void verifyAndSendQuery(int index, long timeInMs, boolean expectsUnicastResponse,
+ boolean multipleSocketDiscovery) {
assertEquals(currentThreadExecutor.getAndClearLastScheduledDelayInMs(), timeInMs);
currentThreadExecutor.getAndClearLastScheduledRunnable().run();
if (expectsUnicastResponse) {
- verify(mockSocketClient).sendUnicastPacket(expectedPackets[index]);
+ verify(mockSocketClient).sendUnicastPacket(
+ expectedIPv4Packets[index], null /* network */);
+ if (multipleSocketDiscovery) {
+ verify(mockSocketClient).sendUnicastPacket(
+ expectedIPv6Packets[index], null /* network */);
+ }
} else {
- verify(mockSocketClient).sendMulticastPacket(expectedPackets[index]);
+ verify(mockSocketClient).sendMulticastPacket(
+ expectedIPv4Packets[index], null /* network */);
+ if (multipleSocketDiscovery) {
+ verify(mockSocketClient).sendMulticastPacket(
+ expectedIPv6Packets[index], null /* network */);
+ }
}
}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketProviderTest.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketProviderTest.java
index 2bb61a6a..635b296 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketProviderTest.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketProviderTest.java
@@ -24,6 +24,7 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -89,14 +90,22 @@
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
mockService(mContext, ConnectivityManager.class, Context.CONNECTIVITY_SERVICE, mCm);
+ if (mContext.getSystemService(ConnectivityManager.class) == null) {
+ // Test is using mockito-extended
+ doCallRealMethod().when(mContext).getSystemService(ConnectivityManager.class);
+ }
mockService(mContext, TetheringManager.class, Context.TETHERING_SERVICE, mTm);
+ if (mContext.getSystemService(TetheringManager.class) == null) {
+ // Test is using mockito-extended
+ doCallRealMethod().when(mContext).getSystemService(TetheringManager.class);
+ }
doReturn(true).when(mDeps).canScanOnInterface(any());
doReturn(mTestNetworkIfaceWrapper).when(mDeps).getNetworkInterfaceByName(TEST_IFACE_NAME);
doReturn(mLocalOnlyIfaceWrapper).when(mDeps)
.getNetworkInterfaceByName(LOCAL_ONLY_IFACE_NAME);
doReturn(mTetheredIfaceWrapper).when(mDeps).getNetworkInterfaceByName(TETHERED_IFACE_NAME);
doReturn(mock(MdnsInterfaceSocket.class))
- .when(mDeps).createMdnsInterfaceSocket(any(), anyInt());
+ .when(mDeps).createMdnsInterfaceSocket(any(), anyInt(), any(), any());
final HandlerThread thread = new HandlerThread("MdnsSocketProviderTest");
thread.start();
mHandler = new Handler(thread.getLooper());
@@ -159,12 +168,13 @@
}
@Override
- public void onAddressesChanged(Network network, List<LinkAddress> addresses) {
+ public void onAddressesChanged(Network network, MdnsInterfaceSocket socket,
+ List<LinkAddress> addresses) {
mHistory.add(new AddressesChangedEvent(network, addresses));
}
public void expectedSocketCreatedForNetwork(Network network, List<LinkAddress> addresses) {
- final SocketEvent event = mHistory.poll(DEFAULT_TIMEOUT, c -> true);
+ final SocketEvent event = mHistory.poll(0L /* timeoutMs */, c -> true);
assertNotNull(event);
assertTrue(event instanceof SocketCreatedEvent);
assertEquals(network, event.mNetwork);
@@ -172,7 +182,7 @@
}
public void expectedInterfaceDestroyedForNetwork(Network network) {
- final SocketEvent event = mHistory.poll(DEFAULT_TIMEOUT, c -> true);
+ final SocketEvent event = mHistory.poll(0L /* timeoutMs */, c -> true);
assertNotNull(event);
assertTrue(event instanceof InterfaceDestroyedEvent);
assertEquals(network, event.mNetwork);
@@ -180,7 +190,7 @@
public void expectedAddressesChangedForNetwork(Network network,
List<LinkAddress> addresses) {
- final SocketEvent event = mHistory.poll(DEFAULT_TIMEOUT, c -> true);
+ final SocketEvent event = mHistory.poll(0L /* timeoutMs */, c -> true);
assertNotNull(event);
assertTrue(event instanceof AddressesChangedEvent);
assertEquals(network, event.mNetwork);
@@ -259,7 +269,8 @@
HandlerUtils.waitForIdle(mHandler, DEFAULT_TIMEOUT);
testCallback1.expectedNoCallback();
testCallback2.expectedNoCallback();
- testCallback3.expectedNoCallback();
+ // Expect the socket destroy for tethered interface.
+ testCallback3.expectedInterfaceDestroyedForNetwork(LOCAL_NETWORK);
}
@Test
diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java
index d7c90d8..13a6a6f 100644
--- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java
+++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java
@@ -2198,7 +2198,7 @@
private NetworkStatsCollection getLegacyCollection(String prefix, boolean includeTags) {
final NetworkStatsRecorder recorder = makeTestRecorder(mLegacyStatsDir, prefix,
- mSettings.getDevConfig(), includeTags, false);
+ mSettings.getXtConfig(), includeTags, false);
return recorder.getOrLoadCompleteLocked();
}
@@ -2255,14 +2255,9 @@
}
private void mockNetworkStatsSummary(NetworkStats summary) throws Exception {
- mockNetworkStatsSummaryDev(summary.clone());
mockNetworkStatsSummaryXt(summary.clone());
}
- private void mockNetworkStatsSummaryDev(NetworkStats summary) throws Exception {
- doReturn(summary).when(mStatsFactory).readNetworkStatsSummaryDev();
- }
-
private void mockNetworkStatsSummaryXt(NetworkStats summary) throws Exception {
doReturn(summary).when(mStatsFactory).readNetworkStatsSummaryXt();
}
@@ -2293,13 +2288,11 @@
doReturn(false).when(mSettings).getCombineSubtypeEnabled();
final Config config = new Config(bucketDuration, deleteAge, deleteAge);
- doReturn(config).when(mSettings).getDevConfig();
doReturn(config).when(mSettings).getXtConfig();
doReturn(config).when(mSettings).getUidConfig();
doReturn(config).when(mSettings).getUidTagConfig();
doReturn(MB_IN_BYTES).when(mSettings).getGlobalAlertBytes(anyLong());
- doReturn(MB_IN_BYTES).when(mSettings).getDevPersistBytes(anyLong());
doReturn(MB_IN_BYTES).when(mSettings).getXtPersistBytes(anyLong());
doReturn(MB_IN_BYTES).when(mSettings).getUidPersistBytes(anyLong());
doReturn(MB_IN_BYTES).when(mSettings).getUidTagPersistBytes(anyLong());
diff --git a/tools/gn2bp/Android.bp.swp b/tools/gn2bp/Android.bp.swp
index 60af5f7..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",
@@ -31,38 +38,39 @@
name: "cronet_aml_api_sources",
srcs: [
":cronet_aml_components_cronet_android_interface_api_version",
- "components/cronet/android/api/src/org/chromium/net/BidirectionalStream.java",
- "components/cronet/android/api/src/org/chromium/net/CallbackException.java",
- "components/cronet/android/api/src/org/chromium/net/CronetEngine.java",
- "components/cronet/android/api/src/org/chromium/net/CronetException.java",
- "components/cronet/android/api/src/org/chromium/net/CronetProvider.java",
- "components/cronet/android/api/src/org/chromium/net/ExperimentalBidirectionalStream.java",
- "components/cronet/android/api/src/org/chromium/net/ExperimentalCronetEngine.java",
- "components/cronet/android/api/src/org/chromium/net/ExperimentalUrlRequest.java",
- "components/cronet/android/api/src/org/chromium/net/ICronetEngineBuilder.java",
- "components/cronet/android/api/src/org/chromium/net/InlineExecutionProhibitedException.java",
- "components/cronet/android/api/src/org/chromium/net/NetworkException.java",
- "components/cronet/android/api/src/org/chromium/net/NetworkQualityRttListener.java",
- "components/cronet/android/api/src/org/chromium/net/NetworkQualityThroughputListener.java",
- "components/cronet/android/api/src/org/chromium/net/QuicException.java",
- "components/cronet/android/api/src/org/chromium/net/RequestFinishedInfo.java",
- "components/cronet/android/api/src/org/chromium/net/UploadDataProvider.java",
- "components/cronet/android/api/src/org/chromium/net/UploadDataProviders.java",
- "components/cronet/android/api/src/org/chromium/net/UploadDataSink.java",
- "components/cronet/android/api/src/org/chromium/net/UrlRequest.java",
- "components/cronet/android/api/src/org/chromium/net/UrlResponseInfo.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/ByteArrayCronetCallback.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/ContentTypeParametersParser.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/CronetRequestCompletionListener.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/CronetResponse.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/ImplicitFlowControlCallback.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/InMemoryTransformCronetCallback.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/JsonCronetCallback.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/RedirectHandler.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/RedirectHandlers.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/StringCronetCallback.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/UploadDataProviders.java",
- "components/cronet/android/api/src/org/chromium/net/apihelpers/UrlRequestCallbacks.java",
+ "components/cronet/android/api/src/android/net/http/BidirectionalStream.java",
+ "components/cronet/android/api/src/android/net/http/CallbackException.java",
+ "components/cronet/android/api/src/android/net/http/ConnectionMigrationOptions.java",
+ "components/cronet/android/api/src/android/net/http/DnsOptions.java",
+ "components/cronet/android/api/src/android/net/http/ExperimentalBidirectionalStream.java",
+ "components/cronet/android/api/src/android/net/http/ExperimentalHttpEngine.java",
+ "components/cronet/android/api/src/android/net/http/ExperimentalUrlRequest.java",
+ "components/cronet/android/api/src/android/net/http/HttpEngine.java",
+ "components/cronet/android/api/src/android/net/http/HttpException.java",
+ "components/cronet/android/api/src/android/net/http/IHttpEngineBuilder.java",
+ "components/cronet/android/api/src/android/net/http/InlineExecutionProhibitedException.java",
+ "components/cronet/android/api/src/android/net/http/NetworkException.java",
+ "components/cronet/android/api/src/android/net/http/NetworkQualityRttListener.java",
+ "components/cronet/android/api/src/android/net/http/NetworkQualityThroughputListener.java",
+ "components/cronet/android/api/src/android/net/http/QuicException.java",
+ "components/cronet/android/api/src/android/net/http/QuicOptions.java",
+ "components/cronet/android/api/src/android/net/http/RequestFinishedInfo.java",
+ "components/cronet/android/api/src/android/net/http/UploadDataProvider.java",
+ "components/cronet/android/api/src/android/net/http/UploadDataSink.java",
+ "components/cronet/android/api/src/android/net/http/UrlRequest.java",
+ "components/cronet/android/api/src/android/net/http/UrlResponseInfo.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/ByteArrayCallback.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/ContentTypeParametersParser.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/HttpResponse.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/ImplicitFlowControlCallback.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/InMemoryTransformCallback.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/JsonCallback.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/RedirectHandler.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/RedirectHandlers.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/RequestCompletionListener.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/StringCallback.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/UploadDataProviders.java",
+ "components/cronet/android/api/src/android/net/http/apihelpers/UrlRequestCallbacks.java",
],
}
@@ -383,7 +391,9 @@
"--input_file " +
"java/lang/Runtime.class " +
"--javap " +
- "$$(find out/.path -name javap)",
+ "$$(find out/.path -name javap) " +
+ "--package_prefix " +
+ "android.net.http.internal",
out: [
"base/android_runtime_jni_headers/Runnable_jni.h",
"base/android_runtime_jni_headers/Runtime_jni.h",
@@ -1092,6 +1102,7 @@
"base/android/java/src/org/chromium/base/MemoryPressureListener.java",
"base/android/java/src/org/chromium/base/PathService.java",
"base/android/java/src/org/chromium/base/PathUtils.java",
+ "base/android/java/src/org/chromium/base/PiiElider.java",
"base/android/java/src/org/chromium/base/PowerMonitor.java",
"base/android/java/src/org/chromium/base/RadioUtils.java",
"base/android/java/src/org/chromium/base/SysUtils.java",
@@ -1165,6 +1176,8 @@
"--output_name " +
"PathUtils_jni.h " +
"--output_name " +
+ "PiiElider_jni.h " +
+ "--output_name " +
"PowerMonitor_jni.h " +
"--output_name " +
"RadioUtils_jni.h " +
@@ -1245,6 +1258,8 @@
"--input_file " +
"$(location base/android/java/src/org/chromium/base/PathUtils.java) " +
"--input_file " +
+ "$(location base/android/java/src/org/chromium/base/PiiElider.java) " +
+ "--input_file " +
"$(location base/android/java/src/org/chromium/base/PowerMonitor.java) " +
"--input_file " +
"$(location base/android/java/src/org/chromium/base/RadioUtils.java) " +
@@ -1275,7 +1290,9 @@
"--input_file " +
"$(location base/android/java/src/org/chromium/base/task/PostTask.java) " +
"--input_file " +
- "$(location base/android/java/src/org/chromium/base/task/TaskRunnerImpl.java)",
+ "$(location base/android/java/src/org/chromium/base/task/TaskRunnerImpl.java) " +
+ "--package_prefix " +
+ "android.net.http.internal",
out: [
"base/base_jni_headers/ApkAssets_jni.h",
"base/base_jni_headers/ApplicationStatus_jni.h",
@@ -1307,6 +1324,7 @@
"base/base_jni_headers/NativeUmaRecorder_jni.h",
"base/base_jni_headers/PathService_jni.h",
"base/base_jni_headers/PathUtils_jni.h",
+ "base/base_jni_headers/PiiElider_jni.h",
"base/base_jni_headers/PostTask_jni.h",
"base/base_jni_headers/PowerMonitor_jni.h",
"base/base_jni_headers/RadioUtils_jni.h",
@@ -1385,7 +1403,7 @@
cc_genrule {
name: "cronet_aml_base_build_date",
cmd: "$(location build/write_build_date_header.py) $(out) " +
- "1670130000",
+ "1672549200",
out: [
"base/generated_build_date.h",
],
@@ -1531,7 +1549,7 @@
// GN: //base:ios_cronet_buildflags
cc_genrule {
name: "cronet_aml_base_ios_cronet_buildflags",
- cmd: "echo '--flags CRONET_BUILD=\"false\"' | " +
+ cmd: "echo '--flags CRONET_BUILD=\"true\"' | " +
"$(location build/write_buildflag_header.py) --output " +
"$(out) " +
"--rulename " +
@@ -2362,8 +2380,6 @@
":cronet_aml_components_cronet_android_cronet_static",
":cronet_aml_components_cronet_cronet_common",
":cronet_aml_components_cronet_metrics_util",
- ":cronet_aml_components_cronet_native_cronet_native_impl",
- ":cronet_aml_components_grpc_support_grpc_support",
":cronet_aml_components_metrics_library_support",
"components/cronet/android/cronet_jni.cc",
],
@@ -2507,7 +2523,9 @@
"--input_file " +
"$(location components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequest.java) " +
"--input_file " +
- "$(location components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequestContext.java)",
+ "$(location components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequestContext.java) " +
+ "--package_prefix " +
+ "android.net.http.internal",
out: [
"components/cronet/android/cronet_jni_headers/CronetBidirectionalStream_jni.h",
"components/cronet/android/cronet_jni_headers/CronetLibraryLoader_jni.h",
@@ -2558,7 +2576,6 @@
"base/android/java/src/org/chromium/base/Function.java",
"base/android/java/src/org/chromium/base/ImportantFileWriterAndroid.java",
"base/android/java/src/org/chromium/base/IntStringCallback.java",
- "base/android/java/src/org/chromium/base/IntentUtils.java",
"base/android/java/src/org/chromium/base/JNIUtils.java",
"base/android/java/src/org/chromium/base/JavaExceptionReporter.java",
"base/android/java/src/org/chromium/base/JavaHandlerThread.java",
@@ -2652,19 +2669,6 @@
"base/android/java/src/org/chromium/base/metrics/TimingMetric.java",
"base/android/java/src/org/chromium/base/metrics/UmaRecorder.java",
"base/android/java/src/org/chromium/base/metrics/UmaRecorderHolder.java",
- "base/android/java/src/org/chromium/base/multidex/ChromiumMultiDexInstaller.java",
- "base/android/java/src/org/chromium/base/process_launcher/BindService.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildConnectionAllocator.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessConnection.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessConstants.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessLauncher.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessService.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessServiceDelegate.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnection.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionDelegate.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionFactory.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionImpl.java",
- "base/android/java/src/org/chromium/base/process_launcher/FileDescriptorInfo.java",
"base/android/java/src/org/chromium/base/supplier/BooleanSupplier.java",
"base/android/java/src/org/chromium/base/supplier/DestroyableObservableSupplier.java",
"base/android/java/src/org/chromium/base/supplier/ObservableSupplier.java",
@@ -2714,16 +2718,7 @@
"components/cronet/android/java/src/org/chromium/net/impl/CronetUploadDataStream.java",
"components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequest.java",
"components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequestContext.java",
- "components/cronet/android/java/src/org/chromium/net/impl/InputStreamChannel.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetEngine.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetEngineBuilderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetProvider.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUploadDataSinkBase.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUrlRequest.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUrlRequestUtils.java",
"components/cronet/android/java/src/org/chromium/net/impl/NativeCronetEngineBuilderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/NativeCronetEngineBuilderWithLibraryLoaderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/NativeCronetProvider.java",
"components/cronet/android/java/src/org/chromium/net/impl/NetworkExceptionImpl.java",
"components/cronet/android/java/src/org/chromium/net/impl/NoOpLogger.java",
"components/cronet/android/java/src/org/chromium/net/impl/Preconditions.java",
@@ -2783,6 +2778,8 @@
"--header-path " +
"$(genDir)/components/cronet/android/cronet_jni_registration.h " +
"--manual_jni_registration " +
+ "--package_prefix " +
+ "android.net.http.internal " +
";sed -i -e 's/OUT_SOONG_.TEMP_SBOX_.*_OUT/GEN/g' " +
"$(genDir)/components/cronet/android/cronet_jni_registration.h",
out: [
@@ -2832,7 +2829,6 @@
"base/android/java/src/org/chromium/base/Function.java",
"base/android/java/src/org/chromium/base/ImportantFileWriterAndroid.java",
"base/android/java/src/org/chromium/base/IntStringCallback.java",
- "base/android/java/src/org/chromium/base/IntentUtils.java",
"base/android/java/src/org/chromium/base/JNIUtils.java",
"base/android/java/src/org/chromium/base/JavaExceptionReporter.java",
"base/android/java/src/org/chromium/base/JavaHandlerThread.java",
@@ -2926,19 +2922,6 @@
"base/android/java/src/org/chromium/base/metrics/TimingMetric.java",
"base/android/java/src/org/chromium/base/metrics/UmaRecorder.java",
"base/android/java/src/org/chromium/base/metrics/UmaRecorderHolder.java",
- "base/android/java/src/org/chromium/base/multidex/ChromiumMultiDexInstaller.java",
- "base/android/java/src/org/chromium/base/process_launcher/BindService.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildConnectionAllocator.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessConnection.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessConstants.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessLauncher.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessService.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessServiceDelegate.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnection.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionDelegate.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionFactory.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionImpl.java",
- "base/android/java/src/org/chromium/base/process_launcher/FileDescriptorInfo.java",
"base/android/java/src/org/chromium/base/supplier/BooleanSupplier.java",
"base/android/java/src/org/chromium/base/supplier/DestroyableObservableSupplier.java",
"base/android/java/src/org/chromium/base/supplier/ObservableSupplier.java",
@@ -2988,16 +2971,7 @@
"components/cronet/android/java/src/org/chromium/net/impl/CronetUploadDataStream.java",
"components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequest.java",
"components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequestContext.java",
- "components/cronet/android/java/src/org/chromium/net/impl/InputStreamChannel.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetEngine.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetEngineBuilderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetProvider.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUploadDataSinkBase.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUrlRequest.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUrlRequestUtils.java",
"components/cronet/android/java/src/org/chromium/net/impl/NativeCronetEngineBuilderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/NativeCronetEngineBuilderWithLibraryLoaderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/NativeCronetProvider.java",
"components/cronet/android/java/src/org/chromium/net/impl/NetworkExceptionImpl.java",
"components/cronet/android/java/src/org/chromium/net/impl/NoOpLogger.java",
"components/cronet/android/java/src/org/chromium/net/impl/Preconditions.java",
@@ -3057,6 +3031,8 @@
"--header-path " +
"$(genDir)/components/cronet/android/cronet_jni_registration.h " +
"--manual_jni_registration " +
+ "--package_prefix " +
+ "android.net.http.internal " +
";sed -i -e 's/OUT_SOONG_.TEMP_SBOX_.*_OUT/GEN/g' " +
"$(genDir)/components/cronet/android/cronet_jni_registration.h",
out: [
@@ -3149,9 +3125,6 @@
"buildtools/third_party/libc++/",
"buildtools/third_party/libc++/trunk/include",
"buildtools/third_party/libc++abi/trunk/include",
- "components/cronet/native/generated/",
- "components/cronet/native/include/",
- "components/grpc_support/include/",
"net/third_party/quiche/overrides/",
"net/third_party/quiche/src/",
"net/third_party/quiche/src/quiche/common/platform/default/",
@@ -3270,7 +3243,7 @@
"'API_LEVEL=19' " +
"-o " +
"$(out) " +
- "$(location components/cronet/android/api/src/org/chromium/net/ApiVersion.template)",
+ "$(location components/cronet/android/api/src/android/net/http/ApiVersion.template)",
out: [
"components/cronet/android/templates/org/chromium/net/ApiVersion.java",
],
@@ -3279,7 +3252,7 @@
"build/util/android_chrome_version.py",
"build/util/version.py",
"chrome/VERSION",
- "components/cronet/android/api/src/org/chromium/net/ApiVersion.template",
+ "components/cronet/android/api/src/android/net/http/ApiVersion.template",
],
}
@@ -3624,186 +3597,6 @@
},
}
-// GN: //components/cronet/native:cronet_native_impl
-cc_object {
- name: "cronet_aml_components_cronet_native_cronet_native_impl",
- srcs: [
- "components/cronet/native/buffer.cc",
- "components/cronet/native/engine.cc",
- "components/cronet/native/generated/cronet.idl_impl_interface.cc",
- "components/cronet/native/generated/cronet.idl_impl_struct.cc",
- "components/cronet/native/io_buffer_with_cronet_buffer.cc",
- "components/cronet/native/native_metrics_util.cc",
- "components/cronet/native/runnables.cc",
- "components/cronet/native/upload_data_sink.cc",
- "components/cronet/native/url_request.cc",
- ],
- shared_libs: [
- "libandroid",
- "liblog",
- "libz",
- ],
- static_libs: [
- "cronet_aml_base_allocator_partition_allocator_partition_alloc",
- "cronet_aml_base_base",
- "cronet_aml_base_base_static",
- "cronet_aml_base_third_party_double_conversion_double_conversion",
- "cronet_aml_base_third_party_dynamic_annotations_dynamic_annotations",
- "cronet_aml_components_prefs_prefs",
- "cronet_aml_crypto_crypto",
- "cronet_aml_net_net",
- "cronet_aml_net_preload_decoder",
- "cronet_aml_net_third_party_quiche_quiche",
- "cronet_aml_net_uri_template",
- "cronet_aml_third_party_boringssl_boringssl",
- "cronet_aml_third_party_brotli_common",
- "cronet_aml_third_party_brotli_dec",
- "cronet_aml_third_party_icu_icui18n",
- "cronet_aml_third_party_icu_icuuc_private",
- "cronet_aml_third_party_libevent_libevent",
- "cronet_aml_third_party_modp_b64_modp_b64",
- "cronet_aml_third_party_protobuf_protobuf_lite",
- "cronet_aml_url_url",
- ],
- generated_headers: [
- "cronet_aml_components_cronet_cronet_buildflags",
- "cronet_aml_components_cronet_cronet_version_header_action",
- "cronet_aml_third_party_metrics_proto_metrics_proto_gen_headers",
- ],
- defaults: [
- "cronet_aml_defaults",
- ],
- cflags: [
- "-DANDROID",
- "-DANDROID_NDK_VERSION_ROLL=r23_1",
- "-DCR_CLANG_REVISION=\"llvmorg-16-init-6578-g0d30e92f-2\"",
- "-DCR_LIBCXX_REVISION=64d36e572d3f9719c5d75011a718f33f11126851",
- "-DDCHECK_ALWAYS_ON=1",
- "-DDYNAMIC_ANNOTATIONS_ENABLED=1",
- "-DGOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE=0",
- "-DGOOGLE_PROTOBUF_NO_RTTI",
- "-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER",
- "-DHAVE_PTHREAD",
- "-DHAVE_SYS_UIO_H",
- "-D_DEBUG",
- "-D_GNU_SOURCE",
- "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS",
- "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS",
- "-D__STDC_CONSTANT_MACROS",
- "-D__STDC_FORMAT_MACROS",
- ],
- local_include_dirs: [
- "./",
- "buildtools/third_party/libc++/",
- "buildtools/third_party/libc++/trunk/include",
- "buildtools/third_party/libc++abi/trunk/include",
- "components/cronet/native/generated/",
- "components/cronet/native/include/",
- "components/grpc_support/include/",
- "net/third_party/quiche/overrides/",
- "net/third_party/quiche/src/",
- "net/third_party/quiche/src/quiche/common/platform/default/",
- "third_party/abseil-cpp/",
- "third_party/boringssl/src/include/",
- "third_party/protobuf/src/",
- ],
- cpp_std: "c++17",
- target: {
- android_x86: {
- cflags: [
- "-msse3",
- ],
- },
- android_x86_64: {
- cflags: [
- "-msse3",
- ],
- },
- },
-}
-
-// GN: //components/grpc_support:grpc_support
-cc_object {
- name: "cronet_aml_components_grpc_support_grpc_support",
- srcs: [
- "components/grpc_support/bidirectional_stream.cc",
- "components/grpc_support/bidirectional_stream_c.cc",
- ],
- shared_libs: [
- "libandroid",
- "liblog",
- "libz",
- ],
- static_libs: [
- "cronet_aml_base_allocator_partition_allocator_partition_alloc",
- "cronet_aml_base_base",
- "cronet_aml_base_base_static",
- "cronet_aml_base_third_party_double_conversion_double_conversion",
- "cronet_aml_base_third_party_dynamic_annotations_dynamic_annotations",
- "cronet_aml_crypto_crypto",
- "cronet_aml_net_net",
- "cronet_aml_net_preload_decoder",
- "cronet_aml_net_third_party_quiche_quiche",
- "cronet_aml_net_uri_template",
- "cronet_aml_third_party_boringssl_boringssl",
- "cronet_aml_third_party_brotli_common",
- "cronet_aml_third_party_brotli_dec",
- "cronet_aml_third_party_icu_icui18n",
- "cronet_aml_third_party_icu_icuuc_private",
- "cronet_aml_third_party_libevent_libevent",
- "cronet_aml_third_party_modp_b64_modp_b64",
- "cronet_aml_third_party_protobuf_protobuf_lite",
- "cronet_aml_url_url",
- ],
- defaults: [
- "cronet_aml_defaults",
- ],
- cflags: [
- "-DANDROID",
- "-DANDROID_NDK_VERSION_ROLL=r23_1",
- "-DCR_CLANG_REVISION=\"llvmorg-16-init-6578-g0d30e92f-2\"",
- "-DCR_LIBCXX_REVISION=64d36e572d3f9719c5d75011a718f33f11126851",
- "-DDCHECK_ALWAYS_ON=1",
- "-DDYNAMIC_ANNOTATIONS_ENABLED=1",
- "-DGOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE=0",
- "-DGOOGLE_PROTOBUF_NO_RTTI",
- "-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER",
- "-DHAVE_PTHREAD",
- "-DHAVE_SYS_UIO_H",
- "-D_DEBUG",
- "-D_GNU_SOURCE",
- "-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS",
- "-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS",
- "-D__STDC_CONSTANT_MACROS",
- "-D__STDC_FORMAT_MACROS",
- ],
- local_include_dirs: [
- "./",
- "buildtools/third_party/libc++/",
- "buildtools/third_party/libc++/trunk/include",
- "buildtools/third_party/libc++abi/trunk/include",
- "net/third_party/quiche/overrides/",
- "net/third_party/quiche/src/",
- "net/third_party/quiche/src/quiche/common/platform/default/",
- "third_party/abseil-cpp/",
- "third_party/boringssl/src/include/",
- "third_party/protobuf/src/",
- ],
- cpp_std: "c++17",
- target: {
- android_x86: {
- cflags: [
- "-msse3",
- ],
- },
- android_x86_64: {
- cflags: [
- "-msse3",
- ],
- },
- },
-}
-
// GN: //components/metrics:library_support
cc_object {
name: "cronet_aml_components_metrics_library_support",
@@ -3895,7 +3688,9 @@
"--output_name " +
"PrefService_jni.h " +
"--input_file " +
- "$(location components/prefs/android/java/src/org/chromium/components/prefs/PrefService.java)",
+ "$(location components/prefs/android/java/src/org/chromium/components/prefs/PrefService.java) " +
+ "--package_prefix " +
+ "android.net.http.internal",
out: [
"components/prefs/android/jni_headers/PrefService_jni.h",
],
@@ -4210,7 +4005,6 @@
"base/android/java/src/org/chromium/base/Function.java",
"base/android/java/src/org/chromium/base/ImportantFileWriterAndroid.java",
"base/android/java/src/org/chromium/base/IntStringCallback.java",
- "base/android/java/src/org/chromium/base/IntentUtils.java",
"base/android/java/src/org/chromium/base/JNIUtils.java",
"base/android/java/src/org/chromium/base/JavaExceptionReporter.java",
"base/android/java/src/org/chromium/base/JavaHandlerThread.java",
@@ -4304,21 +4098,6 @@
"base/android/java/src/org/chromium/base/metrics/TimingMetric.java",
"base/android/java/src/org/chromium/base/metrics/UmaRecorder.java",
"base/android/java/src/org/chromium/base/metrics/UmaRecorderHolder.java",
- "base/android/java/src/org/chromium/base/multidex/ChromiumMultiDexInstaller.java",
- "base/android/java/src/org/chromium/base/process_launcher/BindService.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildConnectionAllocator.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessConnection.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessConstants.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessLauncher.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessService.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildProcessServiceDelegate.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnection.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionDelegate.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionFactory.java",
- "base/android/java/src/org/chromium/base/process_launcher/ChildServiceConnectionImpl.java",
- "base/android/java/src/org/chromium/base/process_launcher/FileDescriptorInfo.java",
- "base/android/java/src/org/chromium/base/process_launcher/IChildProcessService.aidl",
- "base/android/java/src/org/chromium/base/process_launcher/IParentProcess.aidl",
"base/android/java/src/org/chromium/base/supplier/BooleanSupplier.java",
"base/android/java/src/org/chromium/base/supplier/DestroyableObservableSupplier.java",
"base/android/java/src/org/chromium/base/supplier/ObservableSupplier.java",
@@ -4368,16 +4147,7 @@
"components/cronet/android/java/src/org/chromium/net/impl/CronetUploadDataStream.java",
"components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequest.java",
"components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequestContext.java",
- "components/cronet/android/java/src/org/chromium/net/impl/InputStreamChannel.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetEngine.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetEngineBuilderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaCronetProvider.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUploadDataSinkBase.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUrlRequest.java",
- "components/cronet/android/java/src/org/chromium/net/impl/JavaUrlRequestUtils.java",
"components/cronet/android/java/src/org/chromium/net/impl/NativeCronetEngineBuilderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/NativeCronetEngineBuilderWithLibraryLoaderImpl.java",
- "components/cronet/android/java/src/org/chromium/net/impl/NativeCronetProvider.java",
"components/cronet/android/java/src/org/chromium/net/impl/NetworkExceptionImpl.java",
"components/cronet/android/java/src/org/chromium/net/impl/NoOpLogger.java",
"components/cronet/android/java/src/org/chromium/net/impl/Preconditions.java",
@@ -4421,16 +4191,16 @@
"net/android/java/src/org/chromium/net/X509Util.java",
"url/android/java/src/org/chromium/url/IDNStringUtil.java",
],
+ static_libs: [
+ "modules-utils-build_system",
+ ],
apex_available: [
- "//apex_available:platform",
"com.android.tethering",
],
+ min_sdk_version: "30",
libs: [
- "android-support-multidex",
"androidx.annotation_annotation",
"androidx.annotation_annotation-experimental-nodeps",
- "androidx.collection_collection",
- "androidx.core_core-nodeps",
"cronet_aml_api_java",
"framework-connectivity-t.stubs.module_lib",
"framework-connectivity.stubs.module_lib",
@@ -4453,6 +4223,7 @@
sdk_version: "module_current",
javacflags: [
"-Aorg.chromium.chrome.skipGenJni",
+ "-Apackage_prefix=android.net.http.internal",
],
}
@@ -5017,7 +4788,7 @@
// GN: //net:ios_cronet_buildflags
cc_genrule {
name: "cronet_aml_net_ios_cronet_buildflags",
- cmd: "echo '--flags CRONET_BUILD=\"false\"' | " +
+ cmd: "echo '--flags CRONET_BUILD=\"true\"' | " +
"$(location build/write_buildflag_header.py) --output " +
"$(out) " +
"--rulename " +
@@ -5858,7 +5629,9 @@
"--input_file " +
"$(location net/android/java/src/org/chromium/net/ProxyChangeListener.java) " +
"--input_file " +
- "$(location net/android/java/src/org/chromium/net/X509Util.java)",
+ "$(location net/android/java/src/org/chromium/net/X509Util.java) " +
+ "--package_prefix " +
+ "android.net.http.internal",
out: [
"net/net_jni_headers/AndroidCertVerifyResult_jni.h",
"net/net_jni_headers/AndroidKeyStore_jni.h",
@@ -10860,7 +10633,9 @@
"--input_file " +
"$(location url/android/java/src/org/chromium/url/IDNStringUtil.java) " +
"--input_file " +
- "$(location url/android/java/src/org/chromium/url/Origin.java)",
+ "$(location url/android/java/src/org/chromium/url/Origin.java) " +
+ "--package_prefix " +
+ "android.net.http.internal",
out: [
"url/url_jni_headers/IDNStringUtil_jni.h",
"url/url_jni_headers/Origin_jni.h",
@@ -10877,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/desc_arm.json b/tools/gn2bp/desc_arm.json
index d3e9ecd..aa76d1c 100644
--- a/tools/gn2bp/desc_arm.json
+++ b/tools/gn2bp/desc_arm.json
Binary files differ
diff --git a/tools/gn2bp/desc_arm64.json b/tools/gn2bp/desc_arm64.json
index 980a7a1..2bdbc03 100644
--- a/tools/gn2bp/desc_arm64.json
+++ b/tools/gn2bp/desc_arm64.json
Binary files differ
diff --git a/tools/gn2bp/desc_x64.json b/tools/gn2bp/desc_x64.json
index d0d9954..ddf9d3e 100644
--- a/tools/gn2bp/desc_x64.json
+++ b/tools/gn2bp/desc_x64.json
Binary files differ
diff --git a/tools/gn2bp/desc_x86.json b/tools/gn2bp/desc_x86.json
index 6d02af7..f57f15a 100644
--- a/tools/gn2bp/desc_x86.json
+++ b/tools/gn2bp/desc_x86.json
Binary files differ
diff --git a/tools/gn2bp/gen_android_bp b/tools/gn2bp/gen_android_bp
index bc429f5..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')
@@ -962,6 +968,7 @@
self._delete_value_arg('--prev_output_dir', False)
self._update_list_arg('--input_file', self._sanitize_filepath)
self._update_list_arg('--input_file', self._add_location_tag_to_filepath)
+ self._append_arg('--package_prefix', 'android.net.http.internal')
super()._sanitize_args()
def _sanitize_outputs(self):
@@ -992,6 +999,7 @@
# update_jni_registration_module removes them from the srcs of the module
# It might be better to remove sources by '--sources-exclusions'
self._delete_value_arg('--sources-exclusions')
+ self._append_arg('--package_prefix', 'android.net.http.internal')
super()._sanitize_args()
def get_cmd(self):
@@ -1514,19 +1522,20 @@
module.libs = {
"androidx.annotation_annotation",
"jsr305",
- "androidx.core_core-nodeps",
- "androidx.collection_collection",
"androidx.annotation_annotation-experimental-nodeps",
- "android-support-multidex",
"framework-connectivity.stubs.module_lib",
"framework-connectivity-t.stubs.module_lib",
"framework-tethering.stubs.module_lib",
"framework-wifi.stubs.module_lib",
"framework-mediaprovider.stubs.module_lib",
}
+ module.static_libs = {
+ "modules-utils-build_system",
+ }
module.aidl["include_dirs"] = {"frameworks/base/core/java/"}
module.aidl["local_include_dirs"] = {"base/android/java/src/"}
module.sdk_version = "module_current"
+ module.min_sdk_version = 30
module.apex_available.add(tethering_apex)
# TODO: support for this flag is removed upstream in crrev/c/4062652.
# Consider reverting this change upstream, or worst-case downstream. As an
@@ -1534,8 +1543,7 @@
# would be less likely to conflict with upstream changes if the revert is not
# accepted.
module.javacflags.add("-Aorg.chromium.chrome.skipGenJni")
- # TODO: remove following workaround required to make this module visible to make (b/203203405)
- module.apex_available.add("//apex_available:platform")
+ module.javacflags.add("-Apackage_prefix=android.net.http.internal")
for dep in get_non_api_java_actions(gn):
target = gn.get_target(dep)
if target.script == '//build/android/gyp/gcc_preprocess.py':
@@ -1645,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(
@@ -1696,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
//
diff --git a/tools/gn2bp/gen_desc_json.sh b/tools/gn2bp/gen_desc_json.sh
index 510b967..ed684b3 100755
--- a/tools/gn2bp/gen_desc_json.sh
+++ b/tools/gn2bp/gen_desc_json.sh
@@ -30,6 +30,7 @@
"use_hashed_jni_names = true"
"treat_warnings_as_errors = false"
"enable_base_tracing = false"
+ "is_cronet_build = true"
)
gn_args+=("target_cpu = \"${1}\"")