Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.android; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.anyInt; |
Brad Ebinger | b989c7c | 2020-09-23 17:03:48 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.doAnswer; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 21 | |
| 22 | import android.content.BroadcastReceiver; |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Rambo Wang | 7e3bc12 | 2021-03-23 09:24:38 -0700 | [diff] [blame^] | 27 | import android.content.pm.PackageManager; |
| 28 | import android.os.Binder; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.PersistableBundle; |
| 31 | import android.telecom.TelecomManager; |
| 32 | import android.telephony.CarrierConfigManager; |
| 33 | import android.telephony.SubscriptionManager; |
| 34 | import android.telephony.TelephonyManager; |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 35 | import android.telephony.ims.ImsManager; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 36 | import android.test.mock.MockContext; |
Rambo Wang | 7e3bc12 | 2021-03-23 09:24:38 -0700 | [diff] [blame^] | 37 | import android.util.Log; |
Brad Ebinger | b989c7c | 2020-09-23 17:03:48 -0700 | [diff] [blame] | 38 | import android.util.SparseArray; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 39 | |
| 40 | import org.mockito.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
Brad Ebinger | b989c7c | 2020-09-23 17:03:48 -0700 | [diff] [blame] | 42 | import org.mockito.stubbing.Answer; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 43 | |
Rambo Wang | 7e3bc12 | 2021-03-23 09:24:38 -0700 | [diff] [blame^] | 44 | import java.util.HashSet; |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 45 | import java.util.concurrent.Executor; |
| 46 | |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 47 | public class TestContext extends MockContext { |
| 48 | |
Rambo Wang | 7e3bc12 | 2021-03-23 09:24:38 -0700 | [diff] [blame^] | 49 | private static final String TAG = "TestContext"; |
| 50 | // Stub used to grant all permissions |
| 51 | public static final String STUB_PERMISSION_ENABLE_ALL = "stub_permission_enable_all"; |
| 52 | |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 53 | @Mock CarrierConfigManager mMockCarrierConfigManager; |
| 54 | @Mock TelecomManager mMockTelecomManager; |
| 55 | @Mock TelephonyManager mMockTelephonyManager; |
| 56 | @Mock SubscriptionManager mMockSubscriptionManager; |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 57 | @Mock ImsManager mMockImsManager; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 58 | |
Brad Ebinger | b989c7c | 2020-09-23 17:03:48 -0700 | [diff] [blame] | 59 | private SparseArray<PersistableBundle> mCarrierConfigs = new SparseArray<>(); |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 60 | |
Rambo Wang | 7e3bc12 | 2021-03-23 09:24:38 -0700 | [diff] [blame^] | 61 | private final HashSet<String> mPermissionTable = new HashSet<>(); |
| 62 | |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 63 | public TestContext() { |
| 64 | MockitoAnnotations.initMocks(this); |
Brad Ebinger | b989c7c | 2020-09-23 17:03:48 -0700 | [diff] [blame] | 65 | doAnswer((Answer<PersistableBundle>) invocation -> { |
| 66 | int subId = (int) invocation.getArguments()[0]; |
| 67 | if (subId < 0) { |
| 68 | return new PersistableBundle(); |
| 69 | } |
| 70 | PersistableBundle b = mCarrierConfigs.get(subId); |
| 71 | |
| 72 | return (b != null ? b : new PersistableBundle()); |
| 73 | }).when(mMockCarrierConfigManager).getConfigForSubId(anyInt()); |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 77 | public Executor getMainExecutor() { |
| 78 | // Just run on current thread |
| 79 | return Runnable::run; |
| 80 | } |
| 81 | |
| 82 | @Override |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 83 | public Context getApplicationContext() { |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String getPackageName() { |
| 89 | return "com.android.phone.tests"; |
| 90 | } |
| 91 | |
| 92 | @Override |
Philip P. Moltmann | 8d34f0c | 2020-03-05 16:24:02 -0800 | [diff] [blame] | 93 | public String getAttributionTag() { |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 94 | return ""; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) { |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 109 | String broadcastPermission, Handler scheduler) { |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 115 | String broadcastPermission, Handler scheduler, int flags) { |
| 116 | return null; |
| 117 | } |
| 118 | |
| 119 | @Override |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 120 | public ContentResolver getContentResolver() { |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | @Override |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 125 | public Object getSystemService(String name) { |
| 126 | switch (name) { |
| 127 | case (Context.CARRIER_CONFIG_SERVICE) : { |
| 128 | return mMockCarrierConfigManager; |
| 129 | } |
| 130 | case (Context.TELECOM_SERVICE) : { |
| 131 | return mMockTelecomManager; |
| 132 | } |
| 133 | case (Context.TELEPHONY_SERVICE) : { |
| 134 | return mMockTelephonyManager; |
| 135 | } |
| 136 | case (Context.TELEPHONY_SUBSCRIPTION_SERVICE) : { |
| 137 | return mMockSubscriptionManager; |
| 138 | } |
James.cf Lin | cdad386 | 2020-02-25 15:55:03 +0800 | [diff] [blame] | 139 | case(Context.TELEPHONY_IMS_SERVICE) : { |
| 140 | return mMockImsManager; |
| 141 | } |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 142 | } |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public String getSystemServiceName(Class<?> serviceClass) { |
| 148 | if (serviceClass == CarrierConfigManager.class) { |
| 149 | return Context.CARRIER_CONFIG_SERVICE; |
| 150 | } |
| 151 | if (serviceClass == TelecomManager.class) { |
| 152 | return Context.TELECOM_SERVICE; |
| 153 | } |
| 154 | if (serviceClass == TelephonyManager.class) { |
| 155 | return Context.TELEPHONY_SERVICE; |
| 156 | } |
| 157 | if (serviceClass == SubscriptionManager.class) { |
| 158 | return Context.TELEPHONY_SUBSCRIPTION_SERVICE; |
| 159 | } |
| 160 | return null; |
| 161 | } |
| 162 | |
Brad Ebinger | b989c7c | 2020-09-23 17:03:48 -0700 | [diff] [blame] | 163 | /** |
| 164 | * @return CarrierConfig PersistableBundle for the subscription specified. |
| 165 | */ |
| 166 | public PersistableBundle getCarrierConfig(int subId) { |
| 167 | PersistableBundle b = mCarrierConfigs.get(subId); |
| 168 | if (b == null) { |
| 169 | b = new PersistableBundle(); |
| 170 | mCarrierConfigs.put(subId, b); |
| 171 | } |
| 172 | return b; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 173 | } |
Rambo Wang | 7e3bc12 | 2021-03-23 09:24:38 -0700 | [diff] [blame^] | 174 | |
| 175 | @Override |
| 176 | public void enforceCallingOrSelfPermission(String permission, String message) { |
| 177 | if (checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { |
| 178 | throw new SecurityException(permission + " denied: " + message); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void enforcePermission(String permission, int pid, int uid, String message) { |
| 184 | enforceCallingOrSelfPermission(permission, message); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void enforceCallingPermission(String permission, String message) { |
| 189 | enforceCallingOrSelfPermission(permission, message); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public int checkCallingOrSelfPermission(String permission) { |
| 194 | return checkPermission(permission, Binder.getCallingPid(), Binder.getCallingUid()); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public int checkPermission(String permission, int pid, int uid) { |
| 199 | synchronized (mPermissionTable) { |
| 200 | if (mPermissionTable.contains(permission) |
| 201 | || mPermissionTable.contains(STUB_PERMISSION_ENABLE_ALL)) { |
| 202 | logd("checkCallingOrSelfPermission: " + permission + " return GRANTED"); |
| 203 | return PackageManager.PERMISSION_GRANTED; |
| 204 | } else { |
| 205 | logd("checkCallingOrSelfPermission: " + permission + " return DENIED"); |
| 206 | return PackageManager.PERMISSION_DENIED; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | public void grantPermission(String permission) { |
| 212 | synchronized (mPermissionTable) { |
| 213 | if (mPermissionTable != null && permission != null) { |
| 214 | mPermissionTable.remove(STUB_PERMISSION_ENABLE_ALL); |
| 215 | mPermissionTable.add(permission); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | public void revokePermission(String permission) { |
| 221 | synchronized (mPermissionTable) { |
| 222 | if (mPermissionTable != null && permission != null) { |
| 223 | mPermissionTable.remove(permission); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | public void revokeAllPermissions() { |
| 229 | synchronized (mPermissionTable) { |
| 230 | mPermissionTable.clear(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private static void logd(String s) { |
| 235 | Log.d(TAG, s); |
| 236 | } |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 237 | } |