aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Moore <sethmo@google.com>2022-04-25 16:59:06 -0700
committerSeth Moore <sethmo@google.com>2022-04-25 16:59:06 -0700
commit4824ee5f34f5ee5202f5920fb6ef4964d7682cb8 (patch)
tree4aa3390cc67dd3c388c1f91e4a24f2e863fb15c1
parent2c3de310805ec14e277bb14b8348b6d2bda0804a (diff)
downloadRemoteProvisioner-4824ee5f34f5ee5202f5920fb6ef4964d7682cb8.tar.gz
Fix toggling of airplane mode in remote provisioner tests
The original version of this test was not waiting for the network connection to go down/up. Added a simple loop to do this. Test: RemoteProvisionerUnitTests Bug: 230086197 Change-Id: Ibd909a981b9db28ea1bf44d133a6c1f920bd82c3
-rw-r--r--tests/unittests/Android.bp1
-rw-r--r--tests/unittests/src/com/android/remoteprovisioner/unittest/ServerToSystemTest.java38
2 files changed, 31 insertions, 8 deletions
diff --git a/tests/unittests/Android.bp b/tests/unittests/Android.bp
index 9364cea..c6a238d 100644
--- a/tests/unittests/Android.bp
+++ b/tests/unittests/Android.bp
@@ -19,6 +19,7 @@ android_test {
name: "RemoteProvisionerUnitTests",
srcs: ["src/**/*.java"],
static_libs: [
+ "Nene",
"androidx.test.core",
"androidx.test.rules",
"android.security.remoteprovisioning-java",
diff --git a/tests/unittests/src/com/android/remoteprovisioner/unittest/ServerToSystemTest.java b/tests/unittests/src/com/android/remoteprovisioner/unittest/ServerToSystemTest.java
index fce52e1..42281f9 100644
--- a/tests/unittests/src/com/android/remoteprovisioner/unittest/ServerToSystemTest.java
+++ b/tests/unittests/src/com/android/remoteprovisioner/unittest/ServerToSystemTest.java
@@ -26,8 +26,10 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import android.Manifest;
import android.content.Context;
-import android.os.ParcelFileDescriptor;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.security.KeyStoreException;
@@ -38,11 +40,13 @@ import android.security.remoteprovisioning.IRemoteProvisioning;
import android.security.remoteprovisioning.ImplInfo;
import android.system.keystore2.ResponseCode;
import android.util.Base64;
+import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
-import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
+import com.android.bedstead.nene.TestApis;
+import com.android.bedstead.nene.permissions.PermissionContext;
import com.android.remoteprovisioner.GeekResponse;
import com.android.remoteprovisioner.Provisioner;
import com.android.remoteprovisioner.ServerInterface;
@@ -70,6 +74,7 @@ import fi.iki.elonen.NanoHTTPD;
@RunWith(AndroidJUnit4.class)
public class ServerToSystemTest {
+ private static final String Tag = "ServerToSystemTest";
private static final boolean IS_TEST_MODE = false;
private static final String SERVICE = "android.security.remoteprovisioning";
private static final String RKP_ONLY_PROP = "remote_provisioning.tee.rkp_only";
@@ -256,12 +261,29 @@ public class ServerToSystemTest {
}
}
- private void setAirplaneMode(boolean isEnabled) throws IOException {
- String command = "cmd connectivity airplane-mode " + (isEnabled ? "enable" : "disable");
- ParcelFileDescriptor fd =
- InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
- command);
- fd.close();
+ private void setAirplaneMode(boolean enable) throws Exception {
+ ConnectivityManager cm = sContext.getSystemService(ConnectivityManager.class);
+ try (PermissionContext c = TestApis.permissions().withPermission(
+ Manifest.permission.NETWORK_SETTINGS)) {
+ cm.setAirplaneMode(enable);
+
+ // Now wait a "reasonable" time for the network to go down
+ for (int i = 0; i < 100; ++i) {
+ NetworkInfo networkInfo = cm.getActiveNetworkInfo();
+ Log.e(Tag, "Checking active network... " + networkInfo);
+ if (enable) {
+ if (networkInfo == null || !networkInfo.isConnected()) {
+ Log.e(Tag, "Successfully disconnected from to the network.");
+ return;
+ }
+ } else if (networkInfo != null && networkInfo.isConnected()) {
+ Log.e(Tag, "Successfully reconnected to the network.");
+ return;
+ }
+ Thread.sleep(300);
+ }
+ }
+ Assert.fail("Failed to successfully " + (enable ? "enable" : "disable") + " airplane mode");
}
@Test