1 /* 2 * Copyright (C) 2011 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 android.admin.cts; 18 19 import static android.app.admin.DeviceAdminReceiver.ACTION_PASSWORD_CHANGED; 20 import static android.app.admin.DeviceAdminReceiver.ACTION_PASSWORD_FAILED; 21 import static android.app.admin.DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED; 22 import static android.app.admin.DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING; 23 24 import static org.mockito.Mockito.any; 25 import static org.mockito.Mockito.argThat; 26 import static org.mockito.Mockito.eq; 27 import static org.mockito.Mockito.verify; 28 29 import android.app.admin.DeviceAdminReceiver; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.content.pm.PackageManager; 33 import android.os.Process; 34 import android.os.UserHandle; 35 import android.platform.test.annotations.Presubmit; 36 import android.test.AndroidTestCase; 37 import android.util.Log; 38 39 import org.mockito.MockitoAnnotations; 40 import org.mockito.Spy; 41 import org.mockito.compat.ArgumentMatcher; 42 43 public class DeviceAdminReceiverTest extends AndroidTestCase { 44 45 private static final String TAG = DeviceAdminReceiverTest.class.getSimpleName(); 46 private static final String DISABLE_WARNING = "Disable Warning"; 47 private static final String BUGREPORT_HASH = "f4k3h45h"; 48 private static final long NETWORK_LOGS_TOKEN = (123L << 40L); 49 private static final int NETWORK_LOGS_COUNT = (321 << 20); 50 private static final UserHandle USER = Process.myUserHandle(); 51 52 private static final String ACTION_BUGREPORT_SHARING_DECLINED = 53 "android.app.action.BUGREPORT_SHARING_DECLINED"; 54 private static final String ACTION_BUGREPORT_FAILED = "android.app.action.BUGREPORT_FAILED"; 55 private static final String ACTION_BUGREPORT_SHARE = 56 "android.app.action.BUGREPORT_SHARE"; 57 private static final String ACTION_SECURITY_LOGS_AVAILABLE 58 = "android.app.action.SECURITY_LOGS_AVAILABLE"; 59 private static final String EXTRA_BUGREPORT_FAILURE_REASON = 60 "android.app.extra.BUGREPORT_FAILURE_REASON"; 61 private static final String EXTRA_BUGREPORT_HASH = "android.app.extra.BUGREPORT_HASH"; 62 63 private static final String ACTION_NETWORK_LOGS_AVAILABLE 64 = "android.app.action.NETWORK_LOGS_AVAILABLE"; 65 private static final String EXTRA_NETWORK_LOGS_TOKEN = 66 "android.app.extra.EXTRA_NETWORK_LOGS_TOKEN"; 67 private static final String EXTRA_NETWORK_LOGS_COUNT = 68 "android.app.extra.EXTRA_NETWORK_LOGS_COUNT"; 69 70 @Spy 71 public DeviceAdminReceiver mReceiver; 72 private boolean mDeviceAdmin; 73 74 @Override setUp()75 protected void setUp() throws Exception { 76 super.setUp(); 77 mReceiver = new DeviceAdminReceiver(); 78 mDeviceAdmin = 79 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN); 80 MockitoAnnotations.initMocks(this); 81 } 82 83 @Presubmit testOnReceivePasswordChanged()84 public void testOnReceivePasswordChanged() { 85 if (!mDeviceAdmin) { 86 Log.w(TAG, "Skipping testOnReceivePasswordChanged"); 87 return; 88 } 89 mReceiver.onReceive(mContext, new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED) 90 .putExtra(Intent.EXTRA_USER, USER)); 91 verify(mReceiver).onPasswordChanged(any(), actionEq(ACTION_PASSWORD_CHANGED), eq(USER)); 92 verify(mReceiver).onPasswordChanged(any(), actionEq(ACTION_PASSWORD_CHANGED)); 93 } 94 95 @Presubmit testOnReceivePasswordFailed()96 public void testOnReceivePasswordFailed() { 97 if (!mDeviceAdmin) { 98 Log.w(TAG, "Skipping testOnReceivePasswordFailed"); 99 } 100 mReceiver.onReceive(mContext, new Intent(DeviceAdminReceiver.ACTION_PASSWORD_FAILED) 101 .putExtra(Intent.EXTRA_USER, USER)); 102 verify(mReceiver).onPasswordFailed(any(), actionEq(ACTION_PASSWORD_FAILED), eq(USER)); 103 verify(mReceiver).onPasswordFailed(any(), actionEq(ACTION_PASSWORD_FAILED)); 104 } 105 106 @Presubmit testOnReceivePasswordSucceeded()107 public void testOnReceivePasswordSucceeded() { 108 if (!mDeviceAdmin) { 109 Log.w(TAG, "Skipping testOnReceivePasswordSucceeded"); 110 } 111 mReceiver.onReceive(mContext, new Intent(DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED) 112 .putExtra(Intent.EXTRA_USER, USER)); 113 verify(mReceiver).onPasswordSucceeded(any(), actionEq(ACTION_PASSWORD_SUCCEEDED), eq(USER)); 114 verify(mReceiver).onPasswordSucceeded(any(), actionEq(ACTION_PASSWORD_SUCCEEDED)); 115 } 116 117 @Presubmit testOnReceivePasswordExpiring()118 public void testOnReceivePasswordExpiring() { 119 if (!mDeviceAdmin) { 120 Log.w(TAG, "Skipping testOnReceivePasswordExpiring"); 121 } 122 mReceiver.onReceive(mContext, new Intent(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING) 123 .putExtra(Intent.EXTRA_USER, USER)); 124 verify(mReceiver).onPasswordExpiring(any(), actionEq(ACTION_PASSWORD_EXPIRING), eq(USER)); 125 verify(mReceiver).onPasswordExpiring(any(), actionEq(ACTION_PASSWORD_EXPIRING)); 126 } 127 128 @Presubmit testOnReceiveEnabled()129 public void testOnReceiveEnabled() { 130 if (!mDeviceAdmin) { 131 Log.w(TAG, "Skipping testOnReceiveEnabled"); 132 return; 133 } 134 mReceiver.onReceive(mContext, new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED)); 135 verify(mReceiver).onEnabled( 136 any(), actionEq(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED)); 137 } 138 139 @Presubmit testOnReceiveDisabled()140 public void testOnReceiveDisabled() { 141 if (!mDeviceAdmin) { 142 Log.w(TAG, "Skipping testOnReceiveDisabled"); 143 return; 144 } 145 mReceiver.onReceive(mContext, new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED)); 146 verify(mReceiver).onDisabled( 147 any(), actionEq(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED)); 148 } 149 150 @Presubmit testOnReceiveBugreportSharingDeclined()151 public void testOnReceiveBugreportSharingDeclined() { 152 if (!mDeviceAdmin) { 153 Log.w(TAG, "Skipping testOnReceiveBugreportSharingDeclined"); 154 return; 155 } 156 mReceiver.onReceive(mContext, new Intent(ACTION_BUGREPORT_SHARING_DECLINED)); 157 verify(mReceiver).onBugreportSharingDeclined( 158 any(), actionEq(ACTION_BUGREPORT_SHARING_DECLINED)); 159 } 160 161 @Presubmit testOnReceiveBugreportFailed()162 public void testOnReceiveBugreportFailed() { 163 if (!mDeviceAdmin) { 164 Log.w(TAG, "Skipping testOnReceiveBugreportFailed"); 165 return; 166 } 167 Intent bugreportFailedIntent = new Intent(ACTION_BUGREPORT_FAILED); 168 bugreportFailedIntent.putExtra(EXTRA_BUGREPORT_FAILURE_REASON, 169 DeviceAdminReceiver.BUGREPORT_FAILURE_FAILED_COMPLETING); 170 mReceiver.onReceive(mContext, bugreportFailedIntent); 171 verify(mReceiver).onBugreportFailed(any(), actionEq(ACTION_BUGREPORT_FAILED), 172 eq(DeviceAdminReceiver.BUGREPORT_FAILURE_FAILED_COMPLETING)); 173 } 174 175 @Presubmit testOnReceiveBugreportShared()176 public void testOnReceiveBugreportShared() { 177 if (!mDeviceAdmin) { 178 Log.w(TAG, "Skipping testOnReceiveBugreportShared"); 179 return; 180 } 181 Intent bugreportSharedIntent = new Intent(ACTION_BUGREPORT_SHARE); 182 bugreportSharedIntent.putExtra(EXTRA_BUGREPORT_HASH, BUGREPORT_HASH); 183 mReceiver.onReceive(mContext, bugreportSharedIntent); 184 verify(mReceiver).onBugreportShared( 185 any(), actionEq(ACTION_BUGREPORT_SHARE), eq(BUGREPORT_HASH)); 186 } 187 188 @Presubmit testOnReceiveSecurityLogsAvailable()189 public void testOnReceiveSecurityLogsAvailable() { 190 if (!mDeviceAdmin) { 191 Log.w(TAG, "Skipping testOnReceiveSecurityLogsAvailable"); 192 return; 193 } 194 mReceiver.onReceive(mContext, new Intent(ACTION_SECURITY_LOGS_AVAILABLE)); 195 verify(mReceiver).onSecurityLogsAvailable(any(), actionEq(ACTION_SECURITY_LOGS_AVAILABLE)); 196 } 197 198 @Presubmit testOnReceiveNetworkLogsAvailable()199 public void testOnReceiveNetworkLogsAvailable() { 200 if (!mDeviceAdmin) { 201 Log.w(TAG, "Skipping testOnReceiveNetworkLogsAvailable"); 202 return; 203 } 204 Intent networkLogsAvailableIntent = new Intent(ACTION_NETWORK_LOGS_AVAILABLE); 205 networkLogsAvailableIntent.putExtra(EXTRA_NETWORK_LOGS_TOKEN, NETWORK_LOGS_TOKEN); 206 networkLogsAvailableIntent.putExtra(EXTRA_NETWORK_LOGS_COUNT, NETWORK_LOGS_COUNT); 207 mReceiver.onReceive(mContext, networkLogsAvailableIntent); 208 verify(mReceiver).onNetworkLogsAvailable(any(), actionEq(ACTION_NETWORK_LOGS_AVAILABLE), 209 eq(NETWORK_LOGS_TOKEN), eq(NETWORK_LOGS_COUNT)); 210 } 211 212 // TODO: replace with inline argThat(x → e.equals(x.getAction())) when mockito is updated. actionEq(final String expected)213 private Intent actionEq(final String expected) { 214 return argThat(new ArgumentMatcher<Intent>() { 215 @Override 216 public boolean matchesObject(Object argument) { 217 return expected.equals(((Intent) argument).getAction()); 218 } 219 }); 220 } 221 } 222