Update V8 to version 4.7.53.
[chromium-blink-merge.git] / net / http / http_auth_handler_basic_unittest.cc
blob60e588291ca1fab44a2b9278e74932c2feff1c73
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.
5 #include <string>
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"
18 namespace net {
20 TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) {
21 static const struct {
22 const char* username;
23 const char* password;
24 const char* expected_credentials;
25 } tests[] = {
26 { "foo", "bar", "Basic Zm9vOmJhcg==" },
27 // Empty username
28 { "", "foobar", "Basic OmZvb2Jhcg==" },
29 // Empty password
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);
48 EXPECT_EQ(OK, rv);
49 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str());
53 TEST(HttpAuthHandlerBasicTest, HandleAnotherChallenge) {
54 static const struct {
55 const char* challenge;
56 HttpAuth::AuthorizationResult expected_rv;
57 } tests[] = {
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
61 // the same realm.
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(),
98 challenge.end());
99 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(&tok));
103 TEST(HttpAuthHandlerBasicTest, InitFromChallenge) {
104 static const struct {
105 const char* challenge;
106 int expected_rv;
107 const char* expected_realm;
108 } tests[] = {
109 // No realm (we allow this even though realm is supposed to be required
110 // according to RFC 2617.)
112 "Basic",
117 // Realm is empty string.
119 "Basic realm=\"\"",
124 // Realm is valid.
126 "Basic realm=\"test_realm\"",
128 "test_realm",
131 // The parser ignores tokens which aren't known.
133 "Basic realm=\"test_realm\",unknown_token=foobar",
135 "test_realm",
138 // The parser skips over tokens which aren't known.
140 "Basic unknown_token=foobar,realm=\"test_realm\"",
142 "test_realm",
145 #if 0
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",
152 "test_realm",
155 // The parser skips over tokens which aren't known.
157 "Basic unknown_token=foobar realm=\"test_realm\"",
159 "test_realm",
161 #endif
163 // The parser fails when the first token is not "Basic".
165 "Negotiate",
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\"",
175 "bar",
178 // Handle ISO-8859-1 character as part of the realm. The realm is converted
179 // to UTF-8.
181 "Basic realm=\"foo-\xE5\"",
183 "foo-\xC3\xA5",
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);
194 if (rv == OK)
195 EXPECT_EQ(tests[i].expected_realm, basic->realm());
199 } // namespace net