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