1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.networkstack.tethering; 17 18 import static android.Manifest.permission.WRITE_SETTINGS; 19 import static android.content.pm.PackageManager.PERMISSION_GRANTED; 20 21 import static org.mockito.Mockito.mock; 22 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.Binder; 26 import android.os.IBinder; 27 import android.util.ArrayMap; 28 29 import androidx.annotation.NonNull; 30 import androidx.annotation.Nullable; 31 32 public class MockTetheringService extends TetheringService { 33 private final Tethering mTethering = mock(Tethering.class); 34 private final ArrayMap<String, Integer> mMockedPermissions = new ArrayMap<>(); 35 36 @Override onBind(Intent intent)37 public IBinder onBind(Intent intent) { 38 return new MockTetheringConnector(super.onBind(intent)); 39 } 40 41 @Override makeTethering(TetheringDependencies deps)42 public Tethering makeTethering(TetheringDependencies deps) { 43 return mTethering; 44 } 45 46 @Override checkAndNoteWriteSettingsOperation(@onNull Context context, int uid, @NonNull String callingPackage, @Nullable String callingAttributionTag, boolean throwException)47 boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid, 48 @NonNull String callingPackage, @Nullable String callingAttributionTag, 49 boolean throwException) { 50 // Test this does not verify the calling package / UID, as calling package could be shell 51 // and not match the UID. 52 return context.checkCallingOrSelfPermission(WRITE_SETTINGS) == PERMISSION_GRANTED; 53 } 54 55 @Override checkCallingOrSelfPermission(String permission)56 public int checkCallingOrSelfPermission(String permission) { 57 final Integer mocked = mMockedPermissions.getOrDefault(permission, null); 58 if (mocked != null) { 59 return mocked; 60 } 61 return super.checkCallingOrSelfPermission(permission); 62 } 63 getTethering()64 public Tethering getTethering() { 65 return mTethering; 66 } 67 68 public class MockTetheringConnector extends Binder { 69 final IBinder mBase; MockTetheringConnector(IBinder base)70 MockTetheringConnector(IBinder base) { 71 mBase = base; 72 } 73 getIBinder()74 public IBinder getIBinder() { 75 return mBase; 76 } 77 getService()78 public MockTetheringService getService() { 79 return MockTetheringService.this; 80 } 81 82 /** 83 * Mock a permission 84 * @param permission Permission to mock 85 * @param granted One of PackageManager.PERMISSION_*, or null to reset to default behavior 86 */ setPermission(String permission, Integer granted)87 public void setPermission(String permission, Integer granted) { 88 if (granted == null) { 89 mMockedPermissions.remove(permission); 90 } else { 91 mMockedPermissions.put(permission, granted); 92 } 93 } 94 } 95 } 96