1 // Copyright (c) 2011 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.
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/test_completion_callback.h"
13 #include "net/http/http_auth_challenge_tokenizer.h"
14 #include "net/http/http_auth_handler_basic.h"
15 #include "net/http/http_request_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 TEST(HttpAuthHandlerBasicTest
, GenerateAuthToken
) {
24 const char* expected_credentials
;
26 { "foo", "bar", "Basic Zm9vOmJhcg==" },
28 { "", "foobar", "Basic OmZvb2Jhcg==" },
30 { "anon", "", "Basic YW5vbjo=" },
31 // Empty username and empty password.
32 { "", "", "Basic Og==" },
34 GURL
origin("http://www.example.com");
35 HttpAuthHandlerBasic::Factory factory
;
36 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
37 std::string challenge
= "Basic realm=\"Atlantis\"";
38 scoped_ptr
<HttpAuthHandler
> basic
;
39 EXPECT_EQ(OK
, factory
.CreateAuthHandlerFromString(
40 challenge
, HttpAuth::AUTH_SERVER
, origin
, BoundNetLog(), &basic
));
41 AuthCredentials
credentials(base::ASCIIToUTF16(tests
[i
].username
),
42 base::ASCIIToUTF16(tests
[i
].password
));
43 HttpRequestInfo request_info
;
44 std::string auth_token
;
45 TestCompletionCallback callback
;
46 int rv
= basic
->GenerateAuthToken(&credentials
, &request_info
,
47 callback
.callback(), &auth_token
);
49 EXPECT_STREQ(tests
[i
].expected_credentials
, auth_token
.c_str());
53 TEST(HttpAuthHandlerBasicTest
, HandleAnotherChallenge
) {
55 const char* challenge
;
56 HttpAuth::AuthorizationResult expected_rv
;
58 // The handler is initialized using this challenge. The first
59 // time HandleAnotherChallenge is called with it should cause it
60 // to treat the second challenge as a rejection since it is for
63 "Basic realm=\"First\"",
64 HttpAuth::AUTHORIZATION_RESULT_REJECT
67 // A challenge for a different realm.
69 "Basic realm=\"Second\"",
70 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM
73 // Although RFC 2617 isn't explicit about this case, if there is
74 // more than one realm directive, we pick the last one. So this
75 // challenge should be treated as being for "First" realm.
77 "Basic realm=\"Second\",realm=\"First\"",
78 HttpAuth::AUTHORIZATION_RESULT_REJECT
81 // And this one should be treated as if it was for "Second."
83 "basic realm=\"First\",realm=\"Second\"",
84 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM
88 GURL
origin("http://www.example.com");
89 HttpAuthHandlerBasic::Factory factory
;
90 scoped_ptr
<HttpAuthHandler
> basic
;
91 EXPECT_EQ(OK
, factory
.CreateAuthHandlerFromString(
92 tests
[0].challenge
, HttpAuth::AUTH_SERVER
, origin
,
93 BoundNetLog(), &basic
));
95 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
96 std::string
challenge(tests
[i
].challenge
);
97 HttpAuthChallengeTokenizer
tok(challenge
.begin(),
99 EXPECT_EQ(tests
[i
].expected_rv
, basic
->HandleAnotherChallenge(&tok
));
103 TEST(HttpAuthHandlerBasicTest
, InitFromChallenge
) {
104 static const struct {
105 const char* challenge
;
107 const char* expected_realm
;
109 // No realm (we allow this even though realm is supposed to be required
110 // according to RFC 2617.)
117 // Realm is empty string.
126 "Basic realm=\"test_realm\"",
131 // The parser ignores tokens which aren't known.
133 "Basic realm=\"test_realm\",unknown_token=foobar",
138 // The parser skips over tokens which aren't known.
140 "Basic unknown_token=foobar,realm=\"test_realm\"",
146 // TODO(cbentzel): It's unclear what the parser should do in these cases.
147 // It seems like this should either be treated as invalid,
148 // or the spaces should be used as a separator.
150 "Basic realm=\"test_realm\" unknown_token=foobar",
155 // The parser skips over tokens which aren't known.
157 "Basic unknown_token=foobar realm=\"test_realm\"",
163 // The parser fails when the first token is not "Basic".
166 ERR_INVALID_RESPONSE
,
170 // Although RFC 2617 isn't explicit about this case, if there is
171 // more than one realm directive, we pick the last one.
173 "Basic realm=\"foo\",realm=\"bar\"",
178 // Handle ISO-8859-1 character as part of the realm. The realm is converted
181 "Basic realm=\"foo-\xE5\"",
186 HttpAuthHandlerBasic::Factory factory
;
187 GURL
origin("http://www.example.com");
188 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
189 std::string challenge
= tests
[i
].challenge
;
190 scoped_ptr
<HttpAuthHandler
> basic
;
191 int rv
= factory
.CreateAuthHandlerFromString(
192 challenge
, HttpAuth::AUTH_SERVER
, origin
, BoundNetLog(), &basic
);
193 EXPECT_EQ(tests
[i
].expected_rv
, rv
);
195 EXPECT_EQ(tests
[i
].expected_realm
, basic
->realm());