OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net.test; |
| 6 |
| 7 import android.accounts.AbstractAccountAuthenticator; |
| 8 import android.accounts.Account; |
| 9 import android.accounts.AccountAuthenticatorResponse; |
| 10 import android.accounts.AccountManager; |
| 11 import android.accounts.AuthenticatorException; |
| 12 import android.accounts.NetworkErrorException; |
| 13 import android.accounts.OperationCanceledException; |
| 14 import android.app.Activity; |
| 15 import android.content.Context; |
| 16 import android.os.Bundle; |
| 17 |
| 18 import org.chromium.base.ApplicationStatus; |
| 19 import org.chromium.base.CalledByNative; |
| 20 import org.chromium.base.JNINamespace; |
| 21 import org.chromium.base.NativeClassQualifiedName; |
| 22 import org.chromium.net.HttpNegotiateConstants; |
| 23 |
| 24 import java.io.IOException; |
| 25 |
| 26 /** |
| 27 * Dummy Android authenticator, to test SPNEGO/Keberos support on Android. This
is deliberately |
| 28 * minimal, and is not intended as an example of how to write a real SPNEGO Auth
enticator. |
| 29 */ |
| 30 @JNINamespace("net::android") |
| 31 public class DummySpnegoAuthenticator extends AbstractAccountAuthenticator { |
| 32 private static final String ACCOUNT_TYPE = "org.chromium.test.DummySpnegoAut
henticator"; |
| 33 private static final String ACCOUNT_NAME = "DummySpnegoAccount"; |
| 34 private static int sResult; |
| 35 private static String sToken; |
| 36 private static boolean sCheckArguments; |
| 37 private static long sNativeDummySpnegoAuthenticator; |
| 38 private static final int GSS_S_COMPLETE = 0; |
| 39 private static final int GSS_S_CONTINUE_NEEDED = 1; |
| 40 private static final int GSS_S_FAILURE = 2; |
| 41 |
| 42 /** |
| 43 * @param context |
| 44 */ |
| 45 public DummySpnegoAuthenticator(Context context) { |
| 46 super(context); |
| 47 } |
| 48 |
| 49 @Override |
| 50 public Bundle addAccount(AccountAuthenticatorResponse arg0, String accountTy
pe, String arg2, |
| 51 String[] arg3, Bundle arg4) throws NetworkErrorException { |
| 52 Bundle result = new Bundle(); |
| 53 result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_B
AD_REQUEST); |
| 54 result.putString(AccountManager.KEY_ERROR_MESSAGE, "Can't add new SPNEGO
accounts"); |
| 55 return result; |
| 56 } |
| 57 |
| 58 @Override |
| 59 public Bundle confirmCredentials(AccountAuthenticatorResponse arg0, Account
arg1, Bundle arg2) |
| 60 throws NetworkErrorException { |
| 61 Bundle result = new Bundle(); |
| 62 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 63 return result; |
| 64 } |
| 65 |
| 66 @Override |
| 67 public Bundle editProperties(AccountAuthenticatorResponse arg0, String arg1)
{ |
| 68 return new Bundle(); |
| 69 } |
| 70 |
| 71 @Override |
| 72 public Bundle getAuthToken(AccountAuthenticatorResponse response, Account ac
count, |
| 73 String authTokenType, Bundle options) throws NetworkErrorException { |
| 74 long nativeQuery = nativeGetNextQuery(sNativeDummySpnegoAuthenticator); |
| 75 String incomingToken = options.getString(HttpNegotiateConstants.KEY_INCO
MING_AUTH_TOKEN); |
| 76 nativeCheckGetTokenArguments(nativeQuery, incomingToken); |
| 77 Bundle result = new Bundle(); |
| 78 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 79 result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 80 result.putString(AccountManager.KEY_AUTHTOKEN, nativeGetTokenToReturn(na
tiveQuery)); |
| 81 result.putInt(HttpNegotiateConstants.KEY_SPNEGO_RESULT, |
| 82 decodeResult(nativeGetResult(nativeQuery))); |
| 83 return result; |
| 84 } |
| 85 |
| 86 /** |
| 87 * @param nativeGetResult |
| 88 * @return |
| 89 */ |
| 90 private int decodeResult(int gssApiResult) { |
| 91 // This only handles the result values currently used in the tests. |
| 92 switch (gssApiResult) { |
| 93 case GSS_S_COMPLETE: |
| 94 case GSS_S_CONTINUE_NEEDED: |
| 95 return 0; |
| 96 case GSS_S_FAILURE: |
| 97 return HttpNegotiateConstants.ERR_MISSING_AUTH_CREDENTIALS; |
| 98 default: |
| 99 return HttpNegotiateConstants.ERR_UNEXPECTED; |
| 100 } |
| 101 } |
| 102 |
| 103 @Override |
| 104 public String getAuthTokenLabel(String arg0) { |
| 105 return "Spnego " + arg0; |
| 106 } |
| 107 |
| 108 @Override |
| 109 public Bundle hasFeatures(AccountAuthenticatorResponse arg0, Account arg1, S
tring[] features) |
| 110 throws NetworkErrorException { |
| 111 Bundle result = new Bundle(); |
| 112 for (String feature : features) { |
| 113 if (!feature.equals(HttpNegotiateConstants.SPNEGO_FEATURE)) { |
| 114 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 115 return result; |
| 116 } |
| 117 } |
| 118 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 119 return result; |
| 120 } |
| 121 |
| 122 @Override |
| 123 public Bundle updateCredentials(AccountAuthenticatorResponse arg0, Account a
rg1, String arg2, |
| 124 Bundle arg3) throws NetworkErrorException { |
| 125 Bundle result = new Bundle(); |
| 126 result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_B
AD_REQUEST); |
| 127 result.putString(AccountManager.KEY_ERROR_MESSAGE, "Can't add new SPNEGO
accounts"); |
| 128 return result; |
| 129 } |
| 130 |
| 131 /** |
| 132 * Called from tests, sets up the test account, if it doesn't already exist |
| 133 */ |
| 134 @CalledByNative |
| 135 private static void ensureTestAccountExists() { |
| 136 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); |
| 137 AccountManager am = AccountManager.get(activity); |
| 138 Account account = new Account(ACCOUNT_NAME, ACCOUNT_TYPE); |
| 139 am.addAccountExplicitly(account, null, null); |
| 140 } |
| 141 |
| 142 /** |
| 143 * Called from tests to tidy up test accounts. |
| 144 */ |
| 145 @SuppressWarnings("deprecation") |
| 146 @CalledByNative |
| 147 private static void removeTestAccounts() { |
| 148 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); |
| 149 AccountManager am = AccountManager.get(activity); |
| 150 String features[] = {HttpNegotiateConstants.SPNEGO_FEATURE}; |
| 151 try { |
| 152 Account accounts[] = |
| 153 am.getAccountsByTypeAndFeatures(ACCOUNT_TYPE, features, null
, null).getResult(); |
| 154 for (Account account : accounts) { |
| 155 // Deprecated, but the replacement not available on Android JB. |
| 156 am.removeAccount(account, null, null).getResult(); |
| 157 } |
| 158 } catch (OperationCanceledException | AuthenticatorException | IOExcepti
on e) { |
| 159 // Should never happen. This is tidy-up after the tests. Ignore. |
| 160 } |
| 161 } |
| 162 |
| 163 @CalledByNative |
| 164 private static void setNativeAuthenticator(long nativeDummySpnegoAuthenticat
or) { |
| 165 sNativeDummySpnegoAuthenticator = nativeDummySpnegoAuthenticator; |
| 166 } |
| 167 |
| 168 /** |
| 169 * Send the relevant decoded arguments of getAuthToken to C++ for checking b
y googletest checks |
| 170 * If the checks fail then the C++ unit test using this authenticator will f
ail. |
| 171 * |
| 172 * @param authTokenType |
| 173 * @param spn |
| 174 * @param incomingToken |
| 175 */ |
| 176 @NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery") |
| 177 private native void nativeCheckGetTokenArguments(long nativeQuery, String in
comingToken); |
| 178 |
| 179 @NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery") |
| 180 private native String nativeGetTokenToReturn(long nativeQuery); |
| 181 |
| 182 @NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery") |
| 183 private native int nativeGetResult(long nativeQuery); |
| 184 |
| 185 private native long nativeGetNextQuery(long nativeDummySpnegoAuthenticator); |
| 186 } |
OLD | NEW |