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