Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.internal.app; |
| 18 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertNotNull; |
| 21 | import static junit.framework.Assert.assertNull; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 22 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.any; |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.anyString; |
| 26 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | import static org.mockito.ArgumentMatchers.nullable; |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 28 | import static org.mockito.Mockito.mock; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 29 | import static org.mockito.Mockito.never; |
| 30 | import static org.mockito.Mockito.spy; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.Mockito.when; |
| 33 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 34 | import android.annotation.Nullable; |
| 35 | import android.content.ComponentName; |
| 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.pm.ActivityInfo; |
| 39 | import android.content.pm.ApplicationInfo; |
| 40 | import android.content.pm.IPackageManager; |
| 41 | import android.content.pm.PackageManager; |
| 42 | import android.content.pm.ResolveInfo; |
| 43 | import android.content.pm.UserInfo; |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 44 | import android.metrics.LogMaker; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 45 | import android.net.Uri; |
| 46 | import android.os.Bundle; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 47 | import android.os.IBinder; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 48 | import android.os.RemoteException; |
| 49 | import android.os.UserHandle; |
| 50 | import android.os.UserManager; |
Eric Sandness | e89d0e1 | 2020-03-13 17:55:51 +0000 | [diff] [blame^] | 51 | import android.provider.Settings; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 52 | |
| 53 | import androidx.test.InstrumentationRegistry; |
| 54 | import androidx.test.rule.ActivityTestRule; |
| 55 | import androidx.test.runner.AndroidJUnit4; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 56 | |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 57 | import com.android.internal.logging.MetricsLogger; |
| 58 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 59 | |
Eric Sandness | e89d0e1 | 2020-03-13 17:55:51 +0000 | [diff] [blame^] | 60 | import org.junit.After; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 61 | import org.junit.Before; |
| 62 | import org.junit.Rule; |
| 63 | import org.junit.Test; |
| 64 | import org.junit.runner.RunWith; |
| 65 | import org.mockito.ArgumentCaptor; |
| 66 | import org.mockito.Mock; |
| 67 | import org.mockito.MockitoAnnotations; |
| 68 | |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 69 | import java.util.ArrayList; |
| 70 | import java.util.List; |
| 71 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 72 | @RunWith(AndroidJUnit4.class) |
| 73 | public class IntentForwarderActivityTest { |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 74 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 75 | private static final ComponentName FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME = |
| 76 | new ComponentName( |
| 77 | "android", |
| 78 | IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE |
| 79 | ); |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 80 | private static final ComponentName FORWARD_TO_PARENT_COMPONENT_NAME = |
| 81 | new ComponentName( |
| 82 | "android", |
| 83 | IntentForwarderActivity.FORWARD_INTENT_TO_PARENT |
| 84 | ); |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 85 | private static final String TYPE_PLAIN_TEXT = "text/plain"; |
| 86 | |
| 87 | private static UserInfo MANAGED_PROFILE_INFO = new UserInfo(); |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 88 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 89 | static { |
| 90 | MANAGED_PROFILE_INFO.id = 10; |
| 91 | MANAGED_PROFILE_INFO.flags = UserInfo.FLAG_MANAGED_PROFILE; |
Eric Sandness | e89d0e1 | 2020-03-13 17:55:51 +0000 | [diff] [blame^] | 92 | MANAGED_PROFILE_INFO.userType = UserManager.USER_TYPE_PROFILE_MANAGED; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private static UserInfo CURRENT_USER_INFO = new UserInfo(); |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 96 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 97 | static { |
| 98 | CURRENT_USER_INFO.id = UserHandle.myUserId(); |
| 99 | CURRENT_USER_INFO.flags = 0; |
| 100 | } |
| 101 | |
| 102 | private static IntentForwarderActivity.Injector sInjector; |
| 103 | private static ComponentName sComponentName; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 104 | private static String sActivityName; |
| 105 | private static String sPackageName; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 106 | |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 107 | @Mock |
| 108 | private IPackageManager mIPm; |
| 109 | @Mock |
| 110 | private PackageManager mPm; |
| 111 | @Mock |
| 112 | private UserManager mUserManager; |
| 113 | @Mock |
| 114 | private ApplicationInfo mApplicationInfo; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 115 | |
| 116 | @Rule |
| 117 | public ActivityTestRule<IntentForwarderWrapperActivity> mActivityRule = |
| 118 | new ActivityTestRule<>(IntentForwarderWrapperActivity.class, true, false); |
| 119 | |
| 120 | private Context mContext; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 121 | public static final String PHONE_NUMBER = "123-456-789"; |
Eric Sandness | e89d0e1 | 2020-03-13 17:55:51 +0000 | [diff] [blame^] | 122 | private int mDeviceProvisionedInitialValue; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 123 | |
| 124 | @Before |
| 125 | public void setup() { |
| 126 | MockitoAnnotations.initMocks(this); |
| 127 | mContext = InstrumentationRegistry.getTargetContext(); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 128 | sInjector = spy(new TestInjector()); |
Eric Sandness | e89d0e1 | 2020-03-13 17:55:51 +0000 | [diff] [blame^] | 129 | mDeviceProvisionedInitialValue = Settings.Global.getInt(mContext.getContentResolver(), |
| 130 | Settings.Global.DEVICE_PROVISIONED, /* def= */ 0); |
| 131 | } |
| 132 | |
| 133 | @After |
| 134 | public void tearDown() { |
| 135 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, |
| 136 | mDeviceProvisionedInitialValue); |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void forwardToManagedProfile_canForward_sendIntent() throws Exception { |
| 141 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 142 | |
| 143 | // Intent can be forwarded. |
| 144 | when(mIPm.canForwardTo( |
| 145 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 146 | |
| 147 | // Managed profile exists. |
| 148 | List<UserInfo> profiles = new ArrayList<>(); |
| 149 | profiles.add(CURRENT_USER_INFO); |
| 150 | profiles.add(MANAGED_PROFILE_INFO); |
| 151 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 152 | |
| 153 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 154 | intent.setAction(Intent.ACTION_SEND); |
| 155 | intent.setType(TYPE_PLAIN_TEXT); |
| 156 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 157 | |
| 158 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 159 | verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt()); |
| 160 | assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction()); |
| 161 | |
| 162 | assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction()); |
| 163 | assertNotNull(activity.mStartActivityIntent); |
| 164 | assertEquals(Intent.ACTION_SEND, activity.mStartActivityIntent.getAction()); |
| 165 | assertNull(activity.mStartActivityIntent.getPackage()); |
| 166 | assertNull(activity.mStartActivityIntent.getComponent()); |
| 167 | assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint()); |
| 168 | |
| 169 | assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void forwardToManagedProfile_cannotForward_sendIntent() throws Exception { |
| 174 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 175 | |
| 176 | // Intent cannot be forwarded. |
| 177 | when(mIPm.canForwardTo( |
| 178 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(false); |
| 179 | |
| 180 | // Managed profile exists. |
| 181 | List<UserInfo> profiles = new ArrayList<>(); |
| 182 | profiles.add(CURRENT_USER_INFO); |
| 183 | profiles.add(MANAGED_PROFILE_INFO); |
| 184 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 185 | |
| 186 | // Create ACTION_SEND intent. |
| 187 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 188 | intent.setAction(Intent.ACTION_SEND); |
| 189 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 190 | |
| 191 | assertNull(activity.mStartActivityIntent); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void forwardToManagedProfile_noManagedProfile_sendIntent() throws Exception { |
| 196 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 197 | |
| 198 | // Intent can be forwarded. |
| 199 | when(mIPm.canForwardTo( |
| 200 | any(Intent.class), anyString(), anyInt(), anyInt())).thenReturn(true); |
| 201 | |
| 202 | // Managed profile does not exist. |
| 203 | List<UserInfo> profiles = new ArrayList<>(); |
| 204 | profiles.add(CURRENT_USER_INFO); |
| 205 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 206 | |
| 207 | // Create ACTION_SEND intent. |
| 208 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 209 | intent.setAction(Intent.ACTION_SEND); |
| 210 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 211 | |
| 212 | assertNull(activity.mStartActivityIntent); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void forwardToManagedProfile_canForward_chooserIntent() throws Exception { |
| 217 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 218 | |
| 219 | // Intent can be forwarded. |
| 220 | when(mIPm.canForwardTo( |
| 221 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 222 | |
| 223 | // Manage profile exists. |
| 224 | List<UserInfo> profiles = new ArrayList<>(); |
| 225 | profiles.add(CURRENT_USER_INFO); |
| 226 | profiles.add(MANAGED_PROFILE_INFO); |
| 227 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 228 | |
| 229 | // Create chooser Intent |
| 230 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 231 | intent.setAction(Intent.ACTION_CHOOSER); |
| 232 | Intent sendIntent = new Intent(Intent.ACTION_SEND); |
| 233 | sendIntent.setComponent(new ComponentName("xx", "yyy")); |
| 234 | sendIntent.setType(TYPE_PLAIN_TEXT); |
| 235 | intent.putExtra(Intent.EXTRA_INTENT, sendIntent); |
| 236 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 237 | |
| 238 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 239 | verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt()); |
| 240 | assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction()); |
| 241 | |
| 242 | assertNotNull(activity.mStartActivityIntent); |
| 243 | assertEquals(Intent.ACTION_CHOOSER, activity.mStartActivityIntent.getAction()); |
| 244 | assertNull(activity.mStartActivityIntent.getPackage()); |
| 245 | assertNull(activity.mStartActivityIntent.getComponent()); |
| 246 | |
| 247 | Intent innerIntent = activity.mStartActivityIntent.getParcelableExtra(Intent.EXTRA_INTENT); |
| 248 | assertNotNull(innerIntent); |
| 249 | assertEquals(Intent.ACTION_SEND, innerIntent.getAction()); |
| 250 | assertNull(innerIntent.getComponent()); |
| 251 | assertNull(innerIntent.getPackage()); |
| 252 | assertEquals(CURRENT_USER_INFO.id, innerIntent.getContentUserHint()); |
| 253 | |
| 254 | assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn); |
| 255 | } |
| 256 | |
| 257 | @Test |
| 258 | public void forwardToManagedProfile_canForward_selectorIntent() throws Exception { |
| 259 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 260 | |
| 261 | // Intent can be forwarded. |
| 262 | when(mIPm.canForwardTo( |
| 263 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 264 | |
| 265 | // Manage profile exists. |
| 266 | List<UserInfo> profiles = new ArrayList<>(); |
| 267 | profiles.add(CURRENT_USER_INFO); |
| 268 | profiles.add(MANAGED_PROFILE_INFO); |
| 269 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 270 | |
| 271 | // Create selector intent. |
| 272 | Intent intent = Intent.makeMainSelectorActivity( |
| 273 | Intent.ACTION_VIEW, Intent.CATEGORY_BROWSABLE); |
| 274 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 275 | |
| 276 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 277 | verify(mIPm).canForwardTo( |
| 278 | intentCaptor.capture(), nullable(String.class), anyInt(), anyInt()); |
| 279 | assertEquals(Intent.ACTION_VIEW, intentCaptor.getValue().getAction()); |
| 280 | |
| 281 | assertNotNull(activity.mStartActivityIntent); |
| 282 | assertEquals(Intent.ACTION_MAIN, activity.mStartActivityIntent.getAction()); |
| 283 | assertNull(activity.mStartActivityIntent.getPackage()); |
| 284 | assertNull(activity.mStartActivityIntent.getComponent()); |
| 285 | assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint()); |
| 286 | |
| 287 | Intent innerIntent = activity.mStartActivityIntent.getSelector(); |
| 288 | assertNotNull(innerIntent); |
| 289 | assertEquals(Intent.ACTION_VIEW, innerIntent.getAction()); |
| 290 | assertNull(innerIntent.getComponent()); |
| 291 | assertNull(innerIntent.getPackage()); |
| 292 | |
| 293 | assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn); |
| 294 | } |
| 295 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 296 | @Test |
| 297 | public void shouldSkipDisclosure_notWhitelisted() throws RemoteException { |
| 298 | setupShouldSkipDisclosureTest(); |
| 299 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 300 | .setAction(Intent.ACTION_SEND) |
| 301 | .setType(TYPE_PLAIN_TEXT); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 302 | |
| 303 | mActivityRule.launchActivity(intent); |
| 304 | |
| 305 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 306 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 307 | } |
| 308 | |
| 309 | @Test |
| 310 | public void shouldSkipDisclosure_withResolverActivity() throws RemoteException { |
| 311 | setupShouldSkipDisclosureTest(); |
| 312 | sActivityName = ResolverActivity.class.getName(); |
| 313 | sPackageName = "android"; |
| 314 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 315 | .setAction(Intent.ACTION_SEND) |
| 316 | .setType(TYPE_PLAIN_TEXT); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 317 | |
| 318 | mActivityRule.launchActivity(intent); |
| 319 | |
| 320 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 321 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 322 | } |
| 323 | |
| 324 | @Test |
| 325 | public void shouldSkipDisclosure_callIntent_call() throws RemoteException { |
| 326 | setupShouldSkipDisclosureTest(); |
| 327 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 328 | .setAction(Intent.ACTION_CALL); |
| 329 | |
| 330 | mActivityRule.launchActivity(intent); |
| 331 | |
| 332 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 333 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 334 | } |
| 335 | |
| 336 | @Test |
| 337 | public void shouldSkipDisclosure_callIntent_callPrivileged() throws RemoteException { |
| 338 | setupShouldSkipDisclosureTest(); |
| 339 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 340 | .setAction(Intent.ACTION_CALL_PRIVILEGED); |
| 341 | |
| 342 | mActivityRule.launchActivity(intent); |
| 343 | |
| 344 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 345 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 346 | } |
| 347 | |
| 348 | @Test |
| 349 | public void shouldSkipDisclosure_callIntent_callEmergency() throws RemoteException { |
| 350 | setupShouldSkipDisclosureTest(); |
| 351 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 352 | .setAction(Intent.ACTION_CALL_EMERGENCY); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 353 | |
| 354 | mActivityRule.launchActivity(intent); |
| 355 | |
| 356 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 357 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 358 | } |
| 359 | |
| 360 | @Test |
| 361 | public void shouldSkipDisclosure_callIntent_dial() throws RemoteException { |
| 362 | setupShouldSkipDisclosureTest(); |
| 363 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 364 | .setAction(Intent.ACTION_DIAL); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 365 | |
| 366 | mActivityRule.launchActivity(intent); |
| 367 | |
| 368 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 369 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 370 | } |
| 371 | |
| 372 | @Test |
| 373 | public void shouldSkipDisclosure_callIntent_notCallOrDial() throws RemoteException { |
| 374 | setupShouldSkipDisclosureTest(); |
| 375 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 376 | .setAction(Intent.ACTION_ALARM_CHANGED); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 377 | |
| 378 | mActivityRule.launchActivity(intent); |
| 379 | |
| 380 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 381 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 382 | } |
| 383 | |
| 384 | @Test |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 385 | public void shouldSkipDisclosure_callIntent_actionViewTel() throws RemoteException { |
| 386 | setupShouldSkipDisclosureTest(); |
| 387 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 388 | .setAction(Intent.ACTION_VIEW) |
| 389 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 390 | .setData(Uri.fromParts("tel", PHONE_NUMBER, null)); |
| 391 | |
| 392 | mActivityRule.launchActivity(intent); |
| 393 | |
| 394 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 395 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 396 | } |
| 397 | |
| 398 | @Test |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 399 | public void shouldSkipDisclosure_textMessageIntent_sms() throws RemoteException { |
| 400 | setupShouldSkipDisclosureTest(); |
| 401 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 402 | .setAction(Intent.ACTION_SENDTO) |
| 403 | .setData(Uri.fromParts("sms", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 404 | |
| 405 | mActivityRule.launchActivity(intent); |
| 406 | |
| 407 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 408 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 409 | } |
| 410 | |
| 411 | @Test |
| 412 | public void shouldSkipDisclosure_textMessageIntent_smsto() throws RemoteException { |
| 413 | setupShouldSkipDisclosureTest(); |
| 414 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 415 | .setAction(Intent.ACTION_SENDTO) |
| 416 | .setData(Uri.fromParts("smsto", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 417 | |
| 418 | mActivityRule.launchActivity(intent); |
| 419 | |
| 420 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 421 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 422 | } |
| 423 | |
| 424 | @Test |
| 425 | public void shouldSkipDisclosure_textMessageIntent_mms() throws RemoteException { |
| 426 | setupShouldSkipDisclosureTest(); |
| 427 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 428 | .setAction(Intent.ACTION_SENDTO) |
| 429 | .setData(Uri.fromParts("mms", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 430 | |
| 431 | mActivityRule.launchActivity(intent); |
| 432 | |
| 433 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 434 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 435 | } |
| 436 | |
| 437 | @Test |
| 438 | public void shouldSkipDisclosure_textMessageIntent_mmsto() throws RemoteException { |
| 439 | setupShouldSkipDisclosureTest(); |
| 440 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 441 | .setAction(Intent.ACTION_SENDTO) |
| 442 | .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null)); |
| 443 | |
| 444 | mActivityRule.launchActivity(intent); |
| 445 | |
| 446 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 447 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 448 | } |
| 449 | |
| 450 | @Test |
| 451 | public void shouldSkipDisclosure_textMessageIntent_actionViewSms() throws RemoteException { |
| 452 | setupShouldSkipDisclosureTest(); |
| 453 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 454 | .setAction(Intent.ACTION_VIEW) |
| 455 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 456 | .setData(Uri.fromParts("sms", PHONE_NUMBER, null)); |
| 457 | |
| 458 | mActivityRule.launchActivity(intent); |
| 459 | |
| 460 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 461 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 462 | } |
| 463 | |
| 464 | @Test |
| 465 | public void shouldSkipDisclosure_textMessageIntent_actionViewSmsto() throws RemoteException { |
| 466 | setupShouldSkipDisclosureTest(); |
| 467 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 468 | .setAction(Intent.ACTION_VIEW) |
| 469 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 470 | .setData(Uri.fromParts("smsto", PHONE_NUMBER, null)); |
| 471 | |
| 472 | mActivityRule.launchActivity(intent); |
| 473 | |
| 474 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 475 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 476 | } |
| 477 | |
| 478 | @Test |
| 479 | public void shouldSkipDisclosure_textMessageIntent_actionViewMms() throws RemoteException { |
| 480 | setupShouldSkipDisclosureTest(); |
| 481 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 482 | .setAction(Intent.ACTION_VIEW) |
| 483 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 484 | .setData(Uri.fromParts("mms", PHONE_NUMBER, null)); |
| 485 | |
| 486 | mActivityRule.launchActivity(intent); |
| 487 | |
| 488 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 489 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 490 | } |
| 491 | |
| 492 | @Test |
| 493 | public void shouldSkipDisclosure_textMessageIntent_actionViewMmsto() throws RemoteException { |
| 494 | setupShouldSkipDisclosureTest(); |
| 495 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 496 | .setAction(Intent.ACTION_VIEW) |
| 497 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 498 | .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 499 | |
| 500 | mActivityRule.launchActivity(intent); |
| 501 | |
| 502 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 503 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 504 | } |
| 505 | |
| 506 | @Test |
| 507 | public void shouldSkipDisclosure_textMessageIntent_invalidUri() throws RemoteException { |
| 508 | setupShouldSkipDisclosureTest(); |
| 509 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 510 | .setAction(Intent.ACTION_SENDTO) |
| 511 | .setData(Uri.fromParts("invalid", PHONE_NUMBER, null)); |
| 512 | |
| 513 | mActivityRule.launchActivity(intent); |
| 514 | |
| 515 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 516 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 517 | } |
| 518 | |
| 519 | @Test |
| 520 | public void shouldSkipDisclosure_viewBrowsableIntent_invalidUri() throws RemoteException { |
| 521 | setupShouldSkipDisclosureTest(); |
| 522 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 523 | .setAction(Intent.ACTION_VIEW) |
| 524 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 525 | .setData(Uri.fromParts("invalid", PHONE_NUMBER, null)); |
| 526 | |
| 527 | mActivityRule.launchActivity(intent); |
| 528 | |
| 529 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 530 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 531 | } |
| 532 | |
| 533 | @Test |
| 534 | public void shouldSkipDisclosure_viewBrowsableIntent_normalUrl() throws RemoteException { |
| 535 | setupShouldSkipDisclosureTest(); |
| 536 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 537 | .setAction(Intent.ACTION_VIEW) |
| 538 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 539 | .setData(Uri.fromParts("http", "apache.org", null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 540 | |
| 541 | mActivityRule.launchActivity(intent); |
| 542 | |
| 543 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 544 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 545 | } |
| 546 | |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 547 | @Test |
Eric Sandness | e89d0e1 | 2020-03-13 17:55:51 +0000 | [diff] [blame^] | 548 | public void shouldSkipDisclosure_duringDeviceSetup() throws RemoteException { |
| 549 | setupShouldSkipDisclosureTest(); |
| 550 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, |
| 551 | /* value= */ 0); |
| 552 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 553 | .setAction(Intent.ACTION_VIEW) |
| 554 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 555 | .setData(Uri.fromParts("http", "apache.org", null)); |
| 556 | |
| 557 | mActivityRule.launchActivity(intent); |
| 558 | |
| 559 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 560 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 561 | } |
| 562 | |
| 563 | @Test |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 564 | public void forwardToManagedProfile_LoggingTest() throws Exception { |
| 565 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 566 | |
| 567 | // Intent can be forwarded. |
| 568 | when(mIPm.canForwardTo( |
| 569 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 570 | |
| 571 | // Managed profile exists. |
| 572 | List<UserInfo> profiles = new ArrayList<>(); |
| 573 | profiles.add(CURRENT_USER_INFO); |
| 574 | profiles.add(MANAGED_PROFILE_INFO); |
| 575 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 576 | |
| 577 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 578 | intent.setAction(Intent.ACTION_SEND); |
| 579 | intent.setType(TYPE_PLAIN_TEXT); |
| 580 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 581 | |
| 582 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 583 | verify(activity.getMetricsLogger()).write(logMakerCaptor.capture()); |
| 584 | assertEquals(MetricsEvent.ACTION_SWITCH_SHARE_PROFILE, |
| 585 | logMakerCaptor.getValue().getCategory()); |
| 586 | assertEquals(MetricsEvent.MANAGED_PROFILE, |
| 587 | logMakerCaptor.getValue().getSubtype()); |
| 588 | } |
| 589 | |
| 590 | @Test |
| 591 | public void forwardToParent_LoggingTest() throws Exception { |
| 592 | sComponentName = FORWARD_TO_PARENT_COMPONENT_NAME; |
| 593 | |
| 594 | // Intent can be forwarded. |
| 595 | when(mIPm.canForwardTo( |
| 596 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 597 | |
| 598 | // Managed profile exists. |
| 599 | List<UserInfo> profiles = new ArrayList<>(); |
| 600 | profiles.add(CURRENT_USER_INFO); |
| 601 | profiles.add(MANAGED_PROFILE_INFO); |
| 602 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 603 | |
| 604 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 605 | intent.setAction(Intent.ACTION_SEND); |
| 606 | intent.setType(TYPE_PLAIN_TEXT); |
| 607 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 608 | |
| 609 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 610 | verify(activity.getMetricsLogger()).write(logMakerCaptor.capture()); |
| 611 | assertEquals(MetricsEvent.ACTION_SWITCH_SHARE_PROFILE, |
| 612 | logMakerCaptor.getValue().getCategory()); |
| 613 | assertEquals(MetricsEvent.PARENT_PROFILE, |
| 614 | logMakerCaptor.getValue().getSubtype()); |
| 615 | } |
| 616 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 617 | private void setupShouldSkipDisclosureTest() throws RemoteException { |
| 618 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 619 | sActivityName = "MyTestActivity"; |
| 620 | sPackageName = "test.package.name"; |
Eric Sandness | e89d0e1 | 2020-03-13 17:55:51 +0000 | [diff] [blame^] | 621 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, |
| 622 | /* value= */ 1); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 623 | when(mApplicationInfo.isSystemApp()).thenReturn(true); |
| 624 | // Managed profile exists. |
| 625 | List<UserInfo> profiles = new ArrayList<>(); |
| 626 | profiles.add(CURRENT_USER_INFO); |
| 627 | profiles.add(MANAGED_PROFILE_INFO); |
| 628 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 629 | // Intent can be forwarded. |
| 630 | when(mIPm.canForwardTo( |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 631 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 632 | } |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 633 | |
| 634 | public static class IntentForwarderWrapperActivity extends IntentForwarderActivity { |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 635 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 636 | private Intent mStartActivityIntent; |
| 637 | private int mUserIdActivityLaunchedIn; |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 638 | private MetricsLogger mMetricsLogger = mock(MetricsLogger.class); |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 639 | |
| 640 | @Override |
| 641 | public void onCreate(@Nullable Bundle savedInstanceState) { |
| 642 | getIntent().setComponent(sComponentName); |
| 643 | super.onCreate(savedInstanceState); |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | protected Injector createInjector() { |
| 648 | return sInjector; |
| 649 | } |
| 650 | |
| 651 | @Override |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 652 | public void startActivityAsCaller(Intent intent, @Nullable Bundle options, |
| 653 | IBinder permissionToken, boolean ignoreTargetSecurity, int userId) { |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 654 | mStartActivityIntent = intent; |
| 655 | mUserIdActivityLaunchedIn = userId; |
| 656 | } |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 657 | |
| 658 | @Override |
| 659 | protected MetricsLogger getMetricsLogger() { |
| 660 | return mMetricsLogger; |
| 661 | } |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 662 | } |
| 663 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 664 | public class TestInjector implements IntentForwarderActivity.Injector { |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 665 | |
| 666 | @Override |
| 667 | public IPackageManager getIPackageManager() { |
| 668 | return mIPm; |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public UserManager getUserManager() { |
| 673 | return mUserManager; |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public PackageManager getPackageManager() { |
| 678 | return mPm; |
| 679 | } |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 680 | |
| 681 | @Override |
| 682 | public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) { |
| 683 | ActivityInfo activityInfo = new ActivityInfo(); |
| 684 | activityInfo.packageName = sPackageName; |
| 685 | activityInfo.name = sActivityName; |
| 686 | activityInfo.applicationInfo = mApplicationInfo; |
| 687 | |
| 688 | ResolveInfo resolveInfo = new ResolveInfo(); |
| 689 | resolveInfo.activityInfo = activityInfo; |
| 690 | |
| 691 | return resolveInfo; |
| 692 | } |
| 693 | |
| 694 | @Override |
| 695 | public void showToast(int messageId, int duration) {} |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 696 | } |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 697 | } |