[Thread] pre-grant all permissions for ThreadNetworkControllerServiceTest
Existing ThreadNetworkControllerService tests are using RunAsShell to
grant the permissions for indivitual APIs. Given the permission checks
are tested in the CTS tests, this is not very useful with the overhead
of complicating the testing code. Instead, this CL grants all the
permissions in setUp() with spy-ed Context.
This commit also fixes the issue in b/326793058 that the tests doesn't
run on a U device where the THREAD_NETWORK_PRIVILEGED permission doesn't
exist.
Bug: 326793058
Test: m ThreadNetworkUnitTests
Change-Id: I477cf30d737969ccf12180eae48d3b8fd6ed4a51
diff --git a/thread/tests/unit/src/com/android/server/thread/ThreadNetworkControllerServiceTest.java b/thread/tests/unit/src/com/android/server/thread/ThreadNetworkControllerServiceTest.java
index b557e65..4948c22 100644
--- a/thread/tests/unit/src/com/android/server/thread/ThreadNetworkControllerServiceTest.java
+++ b/thread/tests/unit/src/com/android/server/thread/ThreadNetworkControllerServiceTest.java
@@ -16,6 +16,7 @@
package com.android.server.thread;
+import static android.Manifest.permission.ACCESS_NETWORK_STATE;
import static android.net.thread.ActiveOperationalDataset.CHANNEL_PAGE_24_GHZ;
import static android.net.thread.ThreadNetworkController.STATE_DISABLED;
import static android.net.thread.ThreadNetworkController.STATE_ENABLED;
@@ -25,7 +26,6 @@
import static android.net.thread.ThreadNetworkManager.PERMISSION_THREAD_NETWORK_PRIVILEGED;
import static com.android.server.thread.openthread.IOtDaemon.ErrorCode.OT_ERROR_INVALID_STATE;
-import static com.android.testutils.TestPermissionUtil.runAsShell;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat;
@@ -37,6 +37,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
@@ -130,6 +131,11 @@
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
+ doNothing()
+ .when(mContext)
+ .enforceCallingOrSelfPermission(
+ eq(PERMISSION_THREAD_NETWORK_PRIVILEGED), anyString());
+
mTestLooper = new TestLooper();
final Handler handler = new Handler(mTestLooper.getLooper());
NetworkProvider networkProvider =
@@ -174,9 +180,7 @@
final IOperationReceiver mockReceiver = mock(IOperationReceiver.class);
mFakeOtDaemon.setJoinException(new RemoteException("ot-daemon join() throws"));
- runAsShell(
- PERMISSION_THREAD_NETWORK_PRIVILEGED,
- () -> mService.join(DEFAULT_ACTIVE_DATASET, mockReceiver));
+ mService.join(DEFAULT_ACTIVE_DATASET, mockReceiver);
mTestLooper.dispatchAll();
verify(mockReceiver, never()).onSuccess();
@@ -188,9 +192,7 @@
mService.initialize();
final IOperationReceiver mockReceiver = mock(IOperationReceiver.class);
- runAsShell(
- PERMISSION_THREAD_NETWORK_PRIVILEGED,
- () -> mService.join(DEFAULT_ACTIVE_DATASET, mockReceiver));
+ mService.join(DEFAULT_ACTIVE_DATASET, mockReceiver);
// Here needs to call Testlooper#dispatchAll twices because TestLooper#moveTimeForward
// operates on only currently enqueued messages but the delayed message is posted from
// another Handler task.
@@ -274,9 +276,7 @@
mService.initialize();
CompletableFuture<Void> setEnabledFuture = new CompletableFuture<>();
- runAsShell(
- PERMISSION_THREAD_NETWORK_PRIVILEGED,
- () -> mService.setEnabled(true, newOperationReceiver(setEnabledFuture)));
+ mService.setEnabled(true, newOperationReceiver(setEnabledFuture));
mTestLooper.dispatchAll();
var thrown = assertThrows(ExecutionException.class, () -> setEnabledFuture.get());