[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
This commit is contained in:
Kangping Dong 2024-02-26 16:39:12 +08:00
commit a3c94d5273

View file

@ -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.DISALLOW_THREAD_NETWORK;
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.anyString;
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 @@ public final class ThreadNetworkControllerServiceTest {
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 @@ public final class ThreadNetworkControllerServiceTest {
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 @@ public final class ThreadNetworkControllerServiceTest {
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 @@ public final class ThreadNetworkControllerServiceTest {
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());