aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 1 | // Copyright (c) 2014 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 | #ifndef NET_ANDROID_DUMMY_SPNEGO_AUTHENTICATOR_H_ |
| 6 | #define NET_ANDROID_DUMMY_SPNEGO_AUTHENTICATOR_H_ |
| 7 | |
| 8 | #include <jni.h> |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 11 | #include <cstdint> |
| 12 | #include <list> |
| 13 | #include <string> |
| 14 | |
| 15 | #include "base/android/scoped_java_ref.h" |
| 16 | |
| 17 | // Provides an interface for controlling the DummySpnegoAuthenticator service. |
| 18 | // This includes a basic stub of the Mock GSSAPI library, so that OS independent |
| 19 | // Negotiate authentication tests can be run on Android. |
| 20 | namespace net { |
| 21 | |
| 22 | // These constant values are arbitrary, and different from the real GSSAPI |
| 23 | // values, but must match those used in DummySpnegoAuthenticator.java |
| 24 | #define GSS_S_COMPLETE 0 |
| 25 | #define GSS_S_CONTINUE_NEEDED 1 |
| 26 | #define GSS_S_FAILURE 2 |
| 27 | |
aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 28 | typedef struct gss_OID_desc_struct { |
| 29 | uint32_t length; |
| 30 | void* elements; |
| 31 | } gss_OID_desc, *gss_OID; |
| 32 | |
| 33 | extern gss_OID CHROME_GSS_SPNEGO_MECH_OID_DESC; |
| 34 | |
| 35 | namespace test { |
| 36 | |
| 37 | // Copy of class in Mock GSSAPI library. |
| 38 | class GssContextMockImpl { |
| 39 | public: |
| 40 | GssContextMockImpl(); |
| 41 | GssContextMockImpl(const GssContextMockImpl& other); |
| 42 | GssContextMockImpl(const char* src_name, |
| 43 | const char* targ_name, |
| 44 | uint32_t lifetime_rec, |
| 45 | const gss_OID_desc& mech_type, |
| 46 | uint32_t ctx_flags, |
| 47 | int locally_initiated, |
| 48 | int open); |
| 49 | ~GssContextMockImpl(); |
| 50 | |
| 51 | void Assign(const GssContextMockImpl& other); |
| 52 | |
| 53 | std::string src_name; |
| 54 | std::string targ_name; |
| 55 | int32_t lifetime_rec; |
| 56 | gss_OID_desc mech_type; |
| 57 | int32_t ctx_flags; |
| 58 | int locally_initiated; |
| 59 | int open; |
| 60 | }; |
| 61 | |
| 62 | } // namespace test |
| 63 | |
| 64 | namespace android { |
| 65 | |
| 66 | // Interface to Java DummySpnegoAuthenticator. |
| 67 | class DummySpnegoAuthenticator { |
| 68 | public: |
| 69 | struct SecurityContextQuery { |
| 70 | SecurityContextQuery(const std::string& expected_package, |
| 71 | uint32_t response_code, |
| 72 | uint32_t minor_response_code, |
| 73 | const test::GssContextMockImpl& context_info, |
| 74 | const std::string& expected_input_token, |
| 75 | const std::string& output_token); |
| 76 | SecurityContextQuery(const std::string& expected_package, |
| 77 | uint32_t response_code, |
| 78 | uint32_t minor_response_code, |
| 79 | const test::GssContextMockImpl& context_info, |
| 80 | const char* expected_input_token, |
| 81 | const char* output_token); |
| 82 | SecurityContextQuery(); |
vmpstr | b88aa0b | 2016-04-12 17:32:35 | [diff] [blame] | 83 | SecurityContextQuery(const SecurityContextQuery& other); |
aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 84 | ~SecurityContextQuery(); |
| 85 | |
| 86 | // Note that many of these fields only exist for compatibility with the |
| 87 | // non-Android version of the tests. Only the response_code and tokens are |
| 88 | // used or checked on Android. |
| 89 | std::string expected_package; |
| 90 | uint32_t response_code; |
| 91 | uint32_t minor_response_code; |
| 92 | test::GssContextMockImpl context_info; |
| 93 | std::string expected_input_token; |
| 94 | std::string output_token; |
| 95 | |
| 96 | // Java callable members |
torne | 1a2325c4 | 2015-11-25 17:23:48 | [diff] [blame] | 97 | base::android::ScopedJavaLocalRef<jstring> GetTokenToReturn( |
| 98 | JNIEnv* env, |
| 99 | const base::android::JavaParamRef<jobject>& obj); |
| 100 | int GetResult(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); |
aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 101 | |
| 102 | // Called from Java to check the arguments passed to the GetToken. Has to |
| 103 | // be in C++ since these tests are driven by googletest, and can only report |
| 104 | // failures through the googletest C++ API. |
torne | 1a2325c4 | 2015-11-25 17:23:48 | [diff] [blame] | 105 | void CheckGetTokenArguments( |
| 106 | JNIEnv* env, |
| 107 | const base::android::JavaParamRef<jobject>& obj, |
| 108 | const base::android::JavaParamRef<jstring>& incoming_token); |
aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | DummySpnegoAuthenticator(); |
| 112 | |
| 113 | ~DummySpnegoAuthenticator(); |
| 114 | |
| 115 | void ExpectSecurityContext(const std::string& expected_package, |
| 116 | uint32_t response_code, |
| 117 | uint32_t minor_response_code, |
| 118 | const test::GssContextMockImpl& context_info, |
| 119 | const std::string& expected_input_token, |
| 120 | const std::string& output_token); |
| 121 | |
aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 122 | static void EnsureTestAccountExists(); |
| 123 | static void RemoveTestAccounts(); |
| 124 | |
torne | 1a2325c4 | 2015-11-25 17:23:48 | [diff] [blame] | 125 | long GetNextQuery(JNIEnv* env, |
| 126 | const base::android::JavaParamRef<jobject>& obj); |
aberent | ec894a5 | 2015-07-09 14:45:53 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | // Abandon the test if the query queue is empty. Has to be a void function to |
| 130 | // allow use of ASSERT_FALSE. |
| 131 | void CheckQueueNotEmpty(); |
| 132 | |
| 133 | std::list<SecurityContextQuery> expected_security_queries_; |
| 134 | // Needed to keep the current query alive once it has been pulled from the |
| 135 | // queue. This is simpler than transferring its ownership to Java. |
| 136 | SecurityContextQuery current_query_; |
| 137 | }; |
| 138 | |
| 139 | } // namespace android |
| 140 | } // namespace net |
| 141 | |
bnc | 3698b0a0 | 2016-12-09 23:36:50 | [diff] [blame] | 142 | #endif // NET_ANDROID_DUMMY_SPNEGO_AUTHENTICATOR_H_ |