1 /* 2 * Copyright (C) 2009 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.server.accounts; 18 19 import android.accounts.Account; 20 import android.accounts.AuthenticatorDescription; 21 import android.app.Notification; 22 import android.content.Context; 23 import android.content.pm.PackageManager; 24 import android.content.pm.RegisteredServicesCache.ServiceInfo; 25 import android.content.pm.RegisteredServicesCacheListener; 26 import android.os.Bundle; 27 import android.os.Handler; 28 import android.os.UserHandle; 29 import android.test.AndroidTestCase; 30 import android.test.IsolatedContext; 31 import android.test.RenamingDelegatingContext; 32 import android.test.mock.MockContentResolver; 33 import android.test.mock.MockContext; 34 import android.test.mock.MockPackageManager; 35 36 import java.io.FileDescriptor; 37 import java.io.PrintWriter; 38 import java.util.ArrayList; 39 import java.util.Arrays; 40 import java.util.Collection; 41 import java.util.Comparator; 42 43 public class AccountManagerServiceTest extends AndroidTestCase { 44 private AccountManagerService mAms; 45 46 @Override 47 protected void setUp() throws Exception { 48 final String filenamePrefix = "test."; 49 MockContentResolver resolver = new MockContentResolver(); 50 RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( 51 new MyMockContext(), // The context that most methods are delegated to 52 getContext(), // The context that file methods are delegated to 53 filenamePrefix); 54 Context context = new IsolatedContext(resolver, targetContextWrapper); 55 setContext(context); 56 mAms = new MyAccountManagerService(getContext(), 57 new MyMockPackageManager(), new MockAccountAuthenticatorCache()); 58 } 59 60 public class AccountSorter implements Comparator<Account> { 61 public int compare(Account object1, Account object2) { 62 if (object1 == object2) return 0; 63 if (object1 == null) return 1; 64 if (object2 == null) return -1; 65 int result = object1.type.compareTo(object2.type); 66 if (result != 0) return result; 67 return object1.name.compareTo(object2.name); 68 } 69 } 70 71 public void testCheckAddAccount() throws Exception { 72 Account a11 = new Account("account1", "type1"); 73 Account a21 = new Account("account2", "type1"); 74 Account a31 = new Account("account3", "type1"); 75 Account a12 = new Account("account1", "type2"); 76 Account a22 = new Account("account2", "type2"); 77 Account a32 = new Account("account3", "type2"); 78 mAms.addAccountExplicitly(a11, "p11", null); 79 mAms.addAccountExplicitly(a12, "p12", null); 80 mAms.addAccountExplicitly(a21, "p21", null); 81 mAms.addAccountExplicitly(a22, "p22", null); 82 mAms.addAccountExplicitly(a31, "p31", null); 83 mAms.addAccountExplicitly(a32, "p32", null); 84 85 Account[] accounts = mAms.getAccounts(null); 86 Arrays.sort(accounts, new AccountSorter()); 87 assertEquals(6, accounts.length); 88 assertEquals(a11, accounts[0]); 89 assertEquals(a21, accounts[1]); 90 assertEquals(a31, accounts[2]); 91 assertEquals(a12, accounts[3]); 92 assertEquals(a22, accounts[4]); 93 assertEquals(a32, accounts[5]); 94 95 accounts = mAms.getAccounts("type1" ); 96 Arrays.sort(accounts, new AccountSorter()); 97 assertEquals(3, accounts.length); 98 assertEquals(a11, accounts[0]); 99 assertEquals(a21, accounts[1]); 100 assertEquals(a31, accounts[2]); 101 102 mAms.removeAccountInternal(a21); 103 104 accounts = mAms.getAccounts("type1" ); 105 Arrays.sort(accounts, new AccountSorter()); 106 assertEquals(2, accounts.length); 107 assertEquals(a11, accounts[0]); 108 assertEquals(a31, accounts[1]); 109 } 110 111 public void testPasswords() throws Exception { 112 Account a11 = new Account("account1", "type1"); 113 Account a12 = new Account("account1", "type2"); 114 mAms.addAccountExplicitly(a11, "p11", null); 115 mAms.addAccountExplicitly(a12, "p12", null); 116 117 assertEquals("p11", mAms.getPassword(a11)); 118 assertEquals("p12", mAms.getPassword(a12)); 119 120 mAms.setPassword(a11, "p11b"); 121 122 assertEquals("p11b", mAms.getPassword(a11)); 123 assertEquals("p12", mAms.getPassword(a12)); 124 } 125 126 public void testUserdata() throws Exception { 127 Account a11 = new Account("account1", "type1"); 128 Bundle u11 = new Bundle(); 129 u11.putString("a", "a_a11"); 130 u11.putString("b", "b_a11"); 131 u11.putString("c", "c_a11"); 132 Account a12 = new Account("account1", "type2"); 133 Bundle u12 = new Bundle(); 134 u12.putString("a", "a_a12"); 135 u12.putString("b", "b_a12"); 136 u12.putString("c", "c_a12"); 137 mAms.addAccountExplicitly(a11, "p11", u11); 138 mAms.addAccountExplicitly(a12, "p12", u12); 139 140 assertEquals("a_a11", mAms.getUserData(a11, "a")); 141 assertEquals("b_a11", mAms.getUserData(a11, "b")); 142 assertEquals("c_a11", mAms.getUserData(a11, "c")); 143 assertEquals("a_a12", mAms.getUserData(a12, "a")); 144 assertEquals("b_a12", mAms.getUserData(a12, "b")); 145 assertEquals("c_a12", mAms.getUserData(a12, "c")); 146 147 mAms.setUserData(a11, "b", "b_a11b"); 148 mAms.setUserData(a12, "c", null); 149 150 assertEquals("a_a11", mAms.getUserData(a11, "a")); 151 assertEquals("b_a11b", mAms.getUserData(a11, "b")); 152 assertEquals("c_a11", mAms.getUserData(a11, "c")); 153 assertEquals("a_a12", mAms.getUserData(a12, "a")); 154 assertEquals("b_a12", mAms.getUserData(a12, "b")); 155 assertNull(mAms.getUserData(a12, "c")); 156 } 157 158 public void testAuthtokens() throws Exception { 159 Account a11 = new Account("account1", "type1"); 160 Account a12 = new Account("account1", "type2"); 161 mAms.addAccountExplicitly(a11, "p11", null); 162 mAms.addAccountExplicitly(a12, "p12", null); 163 164 mAms.setAuthToken(a11, "att1", "a11_att1"); 165 mAms.setAuthToken(a11, "att2", "a11_att2"); 166 mAms.setAuthToken(a11, "att3", "a11_att3"); 167 mAms.setAuthToken(a12, "att1", "a12_att1"); 168 mAms.setAuthToken(a12, "att2", "a12_att2"); 169 mAms.setAuthToken(a12, "att3", "a12_att3"); 170 171 assertEquals("a11_att1", mAms.peekAuthToken(a11, "att1")); 172 assertEquals("a11_att2", mAms.peekAuthToken(a11, "att2")); 173 assertEquals("a11_att3", mAms.peekAuthToken(a11, "att3")); 174 assertEquals("a12_att1", mAms.peekAuthToken(a12, "att1")); 175 assertEquals("a12_att2", mAms.peekAuthToken(a12, "att2")); 176 assertEquals("a12_att3", mAms.peekAuthToken(a12, "att3")); 177 178 mAms.setAuthToken(a11, "att3", "a11_att3b"); 179 mAms.invalidateAuthToken(a12.type, "a12_att2"); 180 181 assertEquals("a11_att1", mAms.peekAuthToken(a11, "att1")); 182 assertEquals("a11_att2", mAms.peekAuthToken(a11, "att2")); 183 assertEquals("a11_att3b", mAms.peekAuthToken(a11, "att3")); 184 assertEquals("a12_att1", mAms.peekAuthToken(a12, "att1")); 185 assertNull(mAms.peekAuthToken(a12, "att2")); 186 assertEquals("a12_att3", mAms.peekAuthToken(a12, "att3")); 187 188 assertNull(mAms.peekAuthToken(a12, "att2")); 189 } 190 191 static public class MockAccountAuthenticatorCache implements IAccountAuthenticatorCache { 192 private ArrayList<ServiceInfo<AuthenticatorDescription>> mServices; 193 194 public MockAccountAuthenticatorCache() { 195 mServices = new ArrayList<ServiceInfo<AuthenticatorDescription>>(); 196 AuthenticatorDescription d1 = new AuthenticatorDescription("type1", "p1", 0, 0, 0, 0); 197 AuthenticatorDescription d2 = new AuthenticatorDescription("type2", "p2", 0, 0, 0, 0); 198 mServices.add(new ServiceInfo<AuthenticatorDescription>(d1, null, 0)); 199 mServices.add(new ServiceInfo<AuthenticatorDescription>(d2, null, 0)); 200 } 201 202 @Override 203 public ServiceInfo<AuthenticatorDescription> getServiceInfo( 204 AuthenticatorDescription type, int userId) { 205 for (ServiceInfo<AuthenticatorDescription> service : mServices) { 206 if (service.type.equals(type)) { 207 return service; 208 } 209 } 210 return null; 211 } 212 213 @Override 214 public Collection<ServiceInfo<AuthenticatorDescription>> getAllServices(int userId) { 215 return mServices; 216 } 217 218 @Override 219 public void dump( 220 final FileDescriptor fd, final PrintWriter fout, final String[] args, int userId) { 221 } 222 223 @Override 224 public void setListener( 225 final RegisteredServicesCacheListener<AuthenticatorDescription> listener, 226 final Handler handler) { 227 } 228 229 @Override 230 public void invalidateCache(int userId) { 231 } 232 } 233 234 static public class MyMockContext extends MockContext { 235 @Override 236 public int checkCallingOrSelfPermission(final String permission) { 237 return PackageManager.PERMISSION_GRANTED; 238 } 239 } 240 241 static public class MyMockPackageManager extends MockPackageManager { 242 @Override 243 public int checkSignatures(final int uid1, final int uid2) { 244 return PackageManager.SIGNATURE_MATCH; 245 } 246 } 247 248 static public class MyAccountManagerService extends AccountManagerService { 249 public MyAccountManagerService(Context context, PackageManager packageManager, 250 IAccountAuthenticatorCache authenticatorCache) { 251 super(context, packageManager, authenticatorCache); 252 } 253 254 @Override 255 protected void installNotification(final int notificationId, final Notification n, UserHandle user) { 256 } 257 258 @Override 259 protected void cancelNotification(final int id, UserHandle user) { 260 } 261 } 262 } 263