1 /*
2  * Copyright (C) 2015 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.accounts.cts;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.accounts.AccountManagerFuture;
22 import android.accounts.AuthenticatorException;
23 import android.accounts.OperationCanceledException;
24 import android.accounts.cts.common.AuthenticatorContentProvider;
25 import android.accounts.cts.common.Fixtures;
26 import android.content.ContentProviderClient;
27 import android.content.ContentResolver;
28 import android.os.RemoteException;
29 import android.test.AndroidTestCase;
30 
31 import java.io.IOException;
32 
33 /**
34  * Tests for AccountManager and AbstractAccountAuthenticator related behavior using {@link
35  * android.accounts.cts.common.TestAccountAuthenticator} instances signed with different keys than
36  * the caller. This is important to test that portion of the {@link AccountManager} API intended
37  * for {@link android.accounts.AbstractAccountAuthenticator} implementers.
38  * <p>
39  * You can run those unit tests with the following command line:
40  * <p>
41  *  adb shell am instrument
42  *   -e debug false -w
43  *   -e class android.accounts.cts.AccountManagerUnaffiliatedAuthenticatorTests
44  * android.accounts.cts/android.support.test.runner.AndroidJUnitRunner
45  */
46 public class AccountManagerUnaffiliatedAuthenticatorTests extends AndroidTestCase {
47 
48     private AccountManager mAccountManager;
49     private ContentProviderClient mProviderClient;
50 
51     @Override
setUp()52     public void setUp() throws Exception {
53         // bind to the diagnostic service and set it up.
54         mAccountManager = AccountManager.get(getContext());
55         ContentResolver resolver = getContext().getContentResolver();
56         mProviderClient = resolver.acquireContentProviderClient(
57                 AuthenticatorContentProvider.AUTHORITY);
58         /*
59          * This will install a bunch of accounts on the device
60          * (see Fixtures.getFixtureAccountNames()).
61          */
62         mProviderClient.call(AuthenticatorContentProvider.METHOD_SETUP, null, null);
63     }
64 
65     @Override
tearDown()66     public void tearDown() throws RemoteException {
67         try {
68             mProviderClient.call(AuthenticatorContentProvider.METHOD_TEARDOWN, null, null);
69         } finally {
70             mProviderClient.release();
71         }
72     }
73 
testNotifyAccountAuthenticated()74     public void testNotifyAccountAuthenticated() {
75         try {
76             mAccountManager.notifyAccountAuthenticated(
77                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS);
78             fail("Expected to just barf if the caller doesn't share a signature.");
79         } catch (SecurityException expected) {}
80     }
81 
testEditProperties()82     public void testEditProperties()  {
83         try {
84             mAccountManager.editProperties(
85                     Fixtures.TYPE_STANDARD_UNAFFILIATED,
86                     null, // activity
87                     null, // callback
88                     null); // handler
89             fail("Expecting a OperationCanceledException.");
90         } catch (SecurityException expected) {
91 
92         }
93     }
94 
testAddAccountExplicitly()95     public void testAddAccountExplicitly() {
96         try {
97             mAccountManager.addAccountExplicitly(
98                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
99                     "shouldn't matter", // password
100                     null); // bundle
101             fail("addAccountExplicitly should just barf if the caller isn't permitted.");
102         } catch (SecurityException expected) {}
103     }
104 
testRemoveAccount_withBooleanResult()105     public void testRemoveAccount_withBooleanResult() {
106         try {
107             mAccountManager.removeAccount(
108                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
109                     null,
110                     null);
111             fail("removeAccount should just barf if the caller isn't permitted.");
112         } catch (SecurityException expected) {}
113     }
114 
testRemoveAccount_withBundleResult()115     public void testRemoveAccount_withBundleResult() {
116         try {
117             mAccountManager.removeAccount(
118                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
119                     null, // Activity
120                     null,
121                     null);
122             fail("removeAccount should just barf if the caller isn't permitted.");
123         } catch (SecurityException expected) {}
124     }
125 
testRemoveAccountExplicitly()126     public void testRemoveAccountExplicitly() {
127         try {
128             mAccountManager.removeAccountExplicitly(
129                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS);
130             fail("removeAccountExplicitly should just barf if the caller isn't permitted.");
131         } catch (SecurityException expected) {}
132     }
133 
testGetPassword()134     public void testGetPassword() {
135         try {
136             mAccountManager.getPassword(
137                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS);
138             fail("getPassword should just barf if the caller isn't permitted.");
139         } catch (SecurityException expected) {}
140     }
141 
testSetPassword()142     public void testSetPassword() {
143         try {
144             mAccountManager.setPassword(
145                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
146                     "Doesn't matter");
147             fail("setPassword should just barf if the caller isn't permitted.");
148         } catch (SecurityException expected) {}
149     }
150 
testClearPassword()151     public void testClearPassword() {
152         try {
153             mAccountManager.clearPassword(
154                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS);
155             fail("clearPassword should just barf if the caller isn't permitted.");
156         } catch (SecurityException expected) {}
157     }
158 
testGetUserData()159     public void testGetUserData() {
160         try {
161             mAccountManager.getUserData(
162                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
163                     "key");
164             fail("getUserData should just barf if the caller isn't permitted.");
165         } catch (SecurityException expected) {}
166     }
167 
testSetUserData()168     public void testSetUserData() {
169         try {
170             mAccountManager.setUserData(
171                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
172                     "key",
173                     "value");
174             fail("setUserData should just barf if the caller isn't permitted.");
175         } catch (SecurityException expected) {}
176     }
177 
setAuthToken()178     public void setAuthToken() {
179         try {
180             mAccountManager.setAuthToken(
181                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
182                     "tokenType",
183                     "token");
184             fail("setAuthToken should just barf if the caller isn't permitted.");
185         } catch (SecurityException expected) {}
186     }
187 
testPeekAuthToken()188     public void testPeekAuthToken() {
189         try {
190             mAccountManager.peekAuthToken(
191                     Fixtures.ACCOUNT_UNAFFILIATED_FIXTURE_SUCCESS,
192                     "tokenType");
193             fail("peekAuthToken should just barf if the caller isn't permitted.");
194         } catch (SecurityException expected) {}
195     }
196 
testGetAccounts()197     public void testGetAccounts() {
198         Account[] accounts = mAccountManager.getAccounts();
199         assertEquals(0, accounts.length);
200     }
201 
testGetAccountsByType()202     public void testGetAccountsByType() {
203         Account[] accounts = mAccountManager.getAccountsByType(
204                 Fixtures.TYPE_STANDARD_UNAFFILIATED);
205         assertEquals(0, accounts.length);
206     }
207 
testGetAccountsByTypeAndFeatures()208     public void testGetAccountsByTypeAndFeatures()
209             throws OperationCanceledException, AuthenticatorException, IOException {
210         AccountManagerFuture<Account[]> future = mAccountManager.getAccountsByTypeAndFeatures(
211                 Fixtures.TYPE_STANDARD_UNAFFILIATED,
212                 new String[] { "doesn't matter" },
213                 null,  // Callback
214                 null);  // Handler
215         Account[] accounts = future.getResult();
216         assertEquals(0, accounts.length);
217     }
218 
testGetAccountsByTypeForPackage()219     public void testGetAccountsByTypeForPackage() {
220         Account[] accounts = mAccountManager.getAccountsByTypeForPackage(
221                 Fixtures.TYPE_STANDARD_UNAFFILIATED,
222                 getContext().getPackageName());
223         assertEquals(0, accounts.length);
224     }
225 }
226 
227