1 /*
2  * Copyright (C) 2014 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 package com.android.cts.deviceowner;
17 
18 import android.app.admin.DeviceAdminReceiver;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Process;
25 import android.os.UserHandle;
26 import android.support.v4.content.LocalBroadcastManager;
27 import android.test.AndroidTestCase;
28 
29 /**
30  * Base class for device-owner based tests.
31  *
32  * This class handles making sure that the test is the device owner
33  * and that it has an active admin registered, so that all tests may
34  * assume these are done. The admin component can be accessed through
35  * {@link #getWho()}.
36  */
37 public abstract class BaseDeviceOwnerTest extends AndroidTestCase {
38 
39     final static String ACTION_USER_ADDED = "com.android.cts.deviceowner.action.USER_ADDED";
40     final static String ACTION_USER_REMOVED = "com.android.cts.deviceowner.action.USER_REMOVED";
41     final static String EXTRA_USER_HANDLE = "com.android.cts.deviceowner.extra.USER_HANDLE";
42     final static String ACTION_NETWORK_LOGS_AVAILABLE =
43             "com.android.cts.deviceowner.action.ACTION_NETWORK_LOGS_AVAILABLE";
44     final static String EXTRA_NETWORK_LOGS_BATCH_TOKEN =
45             "com.android.cts.deviceowner.extra.NETWORK_LOGS_BATCH_TOKEN";
46 
47     public static class BasicAdminReceiver extends DeviceAdminReceiver {
48 
getComponentName(Context context)49         public static ComponentName getComponentName(Context context) {
50             return new ComponentName(context, BasicAdminReceiver.class);
51         }
52 
53         @Override
onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri, String suggestedAlias)54         public String onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri,
55                 String suggestedAlias) {
56             if (uid != Process.myUid() || uri == null) {
57                 return null;
58             }
59             return uri.getQueryParameter("alias");
60         }
61 
62         @Override
onUserAdded(Context context, Intent intent, UserHandle userHandle)63         public void onUserAdded(Context context, Intent intent, UserHandle userHandle) {
64             sendUserAddedOrRemovedBroadcast(context, ACTION_USER_ADDED, userHandle);
65         }
66 
67         @Override
onUserRemoved(Context context, Intent intent, UserHandle userHandle)68         public void onUserRemoved(Context context, Intent intent, UserHandle userHandle) {
69             sendUserAddedOrRemovedBroadcast(context, ACTION_USER_REMOVED, userHandle);
70         }
71 
72         @Override
onNetworkLogsAvailable(Context context, Intent intent, long batchToken, int networkLogsCount)73         public void onNetworkLogsAvailable(Context context, Intent intent, long batchToken,
74                 int networkLogsCount) {
75             // send the broadcast, the rest of the test happens in NetworkLoggingTest
76             Intent batchIntent = new Intent(ACTION_NETWORK_LOGS_AVAILABLE);
77             batchIntent.putExtra(EXTRA_NETWORK_LOGS_BATCH_TOKEN, batchToken);
78             LocalBroadcastManager.getInstance(context).sendBroadcast(batchIntent);
79         }
80 
sendUserAddedOrRemovedBroadcast(Context context, String action, UserHandle userHandle)81         private void sendUserAddedOrRemovedBroadcast(Context context, String action,
82                 UserHandle userHandle) {
83             Intent intent = new Intent(action);
84             intent.putExtra(EXTRA_USER_HANDLE, userHandle);
85             LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
86         }
87     }
88 
89     public static final String PACKAGE_NAME = BaseDeviceOwnerTest.class.getPackage().getName();
90 
91     protected DevicePolicyManager mDevicePolicyManager;
92 
93     @Override
setUp()94     protected void setUp() throws Exception {
95         super.setUp();
96 
97         mDevicePolicyManager = (DevicePolicyManager)
98                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
99         assertDeviceOwner(mDevicePolicyManager);
100     }
101 
assertDeviceOwner(DevicePolicyManager dpm)102     static void assertDeviceOwner(DevicePolicyManager dpm) {
103         assertNotNull(dpm);
104         assertTrue(dpm.isAdminActive(getWho()));
105         assertTrue(dpm.isDeviceOwnerApp(PACKAGE_NAME));
106         assertFalse(dpm.isManagedProfile(getWho()));
107     }
108 
getWho()109     protected static ComponentName getWho() {
110         return new ComponentName(PACKAGE_NAME, BasicAdminReceiver.class.getName());
111     }
112 }
113