Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / android / dummy_spnego_authenticator.cc
blobd42f64c5f417fa5829f6542fae826647333c677a
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 #include "net/android/dummy_spnego_authenticator.h"
6 #include "base/android/jni_string.h"
7 #include "base/base64.h"
8 #include "net/test/jni/DummySpnegoAuthenticator_jni.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace net {
13 // iso.org.dod.internet.security.mechanism.snego (1.3.6.1.5.5.2)
14 // From RFC 4178, which uses SNEGO not SPNEGO.
15 static const unsigned char kSpnegoOid[] = {0x2b, 0x06, 0x01, 0x05, 0x05, 0x02};
16 gss_OID_desc CHROME_GSS_SPNEGO_MECH_OID_DESC_VAL = {
17 arraysize(kSpnegoOid),
18 const_cast<unsigned char*>(kSpnegoOid)};
20 gss_OID CHROME_GSS_SPNEGO_MECH_OID_DESC = &CHROME_GSS_SPNEGO_MECH_OID_DESC_VAL;
22 namespace {
24 // gss_OID helpers.
25 // NOTE: gss_OID's do not own the data they point to, which should be static.
26 void ClearOid(gss_OID dest) {
27 if (!dest)
28 return;
29 dest->length = 0;
30 dest->elements = NULL;
33 void SetOid(gss_OID dest, const void* src, size_t length) {
34 if (!dest)
35 return;
36 ClearOid(dest);
37 if (!src)
38 return;
39 dest->length = length;
40 if (length)
41 dest->elements = const_cast<void*>(src);
44 void CopyOid(gss_OID dest, const gss_OID_desc* src) {
45 if (!dest)
46 return;
47 ClearOid(dest);
48 if (!src)
49 return;
50 SetOid(dest, src->elements, src->length);
53 } // namespace
55 namespace test {
57 GssContextMockImpl::GssContextMockImpl()
58 : lifetime_rec(0), ctx_flags(0), locally_initiated(0), open(0) {
59 ClearOid(&mech_type);
62 GssContextMockImpl::GssContextMockImpl(const GssContextMockImpl& other)
63 : src_name(other.src_name),
64 targ_name(other.targ_name),
65 lifetime_rec(other.lifetime_rec),
66 ctx_flags(other.ctx_flags),
67 locally_initiated(other.locally_initiated),
68 open(other.open) {
69 CopyOid(&mech_type, &other.mech_type);
72 GssContextMockImpl::GssContextMockImpl(const char* src_name_in,
73 const char* targ_name_in,
74 uint32_t lifetime_rec_in,
75 const gss_OID_desc& mech_type_in,
76 uint32_t ctx_flags_in,
77 int locally_initiated_in,
78 int open_in)
79 : src_name(src_name_in ? src_name_in : ""),
80 targ_name(targ_name_in ? targ_name_in : ""),
81 lifetime_rec(lifetime_rec_in),
82 ctx_flags(ctx_flags_in),
83 locally_initiated(locally_initiated_in),
84 open(open_in) {
85 CopyOid(&mech_type, &mech_type_in);
88 GssContextMockImpl::~GssContextMockImpl() {
89 ClearOid(&mech_type);
92 } // namespace test
94 namespace android {
96 DummySpnegoAuthenticator::SecurityContextQuery::SecurityContextQuery(
97 const std::string& in_expected_package,
98 uint32_t in_response_code,
99 uint32_t in_minor_response_code,
100 const test::GssContextMockImpl& in_context_info,
101 const std::string& in_expected_input_token,
102 const std::string& in_output_token)
103 : expected_package(in_expected_package),
104 response_code(in_response_code),
105 minor_response_code(in_minor_response_code),
106 context_info(in_context_info),
107 expected_input_token(in_expected_input_token),
108 output_token(in_output_token) {
111 DummySpnegoAuthenticator::SecurityContextQuery::SecurityContextQuery(
112 const std::string& in_expected_package,
113 uint32_t in_response_code,
114 uint32_t in_minor_response_code,
115 const test::GssContextMockImpl& in_context_info,
116 const char* in_expected_input_token,
117 const char* in_output_token)
118 : expected_package(in_expected_package),
119 response_code(in_response_code),
120 minor_response_code(in_minor_response_code),
121 context_info(in_context_info) {
122 if (in_expected_input_token)
123 expected_input_token = in_expected_input_token;
124 if (in_output_token)
125 output_token = in_output_token;
128 DummySpnegoAuthenticator::SecurityContextQuery::SecurityContextQuery()
129 : response_code(0), minor_response_code(0) {
132 DummySpnegoAuthenticator::SecurityContextQuery::~SecurityContextQuery() {
135 base::android::ScopedJavaLocalRef<jstring>
136 DummySpnegoAuthenticator::SecurityContextQuery::GetTokenToReturn(
137 JNIEnv* env,
138 jobject /*obj*/) {
139 return base::android::ConvertUTF8ToJavaString(env, output_token.c_str());
141 int DummySpnegoAuthenticator::SecurityContextQuery::GetResult(JNIEnv* /*env*/,
142 jobject /*obj*/) {
143 return response_code;
146 void DummySpnegoAuthenticator::SecurityContextQuery::CheckGetTokenArguments(
147 JNIEnv* env,
148 jobject /*obj*/,
149 jstring j_incoming_token) {
150 std::string incoming_token =
151 base::android::ConvertJavaStringToUTF8(env, j_incoming_token);
152 EXPECT_EQ(expected_input_token, incoming_token);
155 // Needed to satisfy "complex class" clang requirements.
156 DummySpnegoAuthenticator::DummySpnegoAuthenticator() {
159 DummySpnegoAuthenticator::~DummySpnegoAuthenticator() {
162 void DummySpnegoAuthenticator::EnsureTestAccountExists() {
163 Java_DummySpnegoAuthenticator_ensureTestAccountExists(
164 base::android::AttachCurrentThread());
167 void DummySpnegoAuthenticator::RemoveTestAccounts() {
168 Java_DummySpnegoAuthenticator_removeTestAccounts(
169 base::android::AttachCurrentThread());
172 void DummySpnegoAuthenticator::ExpectSecurityContext(
173 const std::string& expected_package,
174 uint32_t response_code,
175 uint32_t minor_response_code,
176 const test::GssContextMockImpl& context_info,
177 const std::string& expected_input_token,
178 const std::string& output_token) {
179 SecurityContextQuery query(expected_package, response_code,
180 minor_response_code, context_info,
181 expected_input_token, output_token);
182 expected_security_queries_.push_back(query);
183 Java_DummySpnegoAuthenticator_setNativeAuthenticator(
184 base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this));
187 bool DummySpnegoAuthenticator::RegisterJni(JNIEnv* env) {
188 return RegisterNativesImpl(env);
191 long DummySpnegoAuthenticator::GetNextQuery(JNIEnv* /*env*/,
192 jobject /* obj */) {
193 CheckQueueNotEmpty();
194 current_query_ = expected_security_queries_.front();
195 expected_security_queries_.pop_front();
196 return reinterpret_cast<intptr_t>(&current_query_);
199 void DummySpnegoAuthenticator::CheckQueueNotEmpty() {
200 ASSERT_FALSE(expected_security_queries_.empty());
203 } // namespace android
204 } // namespace net