OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_auth_controller.h" | 5 #include "net/http/http_auth_controller.h" |
6 | 6 |
7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
8 #include "net/base/net_log.h" | 8 #include "net/base/net_log.h" |
9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
10 #include "net/http/http_auth_cache.h" | 10 #include "net/http/http_auth_cache.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 enum HandlerRunMode { | 21 enum HandlerRunMode { |
22 RUN_HANDLER_SYNC, | 22 RUN_HANDLER_SYNC, |
23 RUN_HANDLER_ASYNC | 23 RUN_HANDLER_ASYNC |
24 }; | 24 }; |
25 | 25 |
26 enum SchemeState { | 26 enum SchemeState { |
27 SCHEME_IS_DISABLED, | 27 SCHEME_IS_DISABLED, |
28 SCHEME_IS_ENABLED | 28 SCHEME_IS_ENABLED |
29 }; | 29 }; |
30 | 30 |
31 scoped_refptr<HttpResponseHeaders> HeadersFromString(const char* string) { | |
32 std::string raw_string(string); | |
33 std::string headers_string = HttpUtil::AssembleRawHeaders( | |
34 raw_string.c_str(), raw_string.length()); | |
35 scoped_refptr<HttpResponseHeaders> headers( | |
36 new HttpResponseHeaders(headers_string)); | |
37 return headers; | |
38 } | |
39 | |
31 // Runs an HttpAuthController with a single round mock auth handler | 40 // Runs an HttpAuthController with a single round mock auth handler |
32 // that returns |handler_rv| on token generation. The handler runs in | 41 // that returns |handler_rv| on token generation. The handler runs in |
33 // async if |run_mode| is RUN_HANDLER_ASYNC. Upon completion, the | 42 // async if |run_mode| is RUN_HANDLER_ASYNC. Upon completion, the |
34 // return value of the controller is tested against | 43 // return value of the controller is tested against |
35 // |expected_controller_rv|. |scheme_state| indicates whether the | 44 // |expected_controller_rv|. |scheme_state| indicates whether the |
36 // auth scheme used should be disabled after this run. | 45 // auth scheme used should be disabled after this run. |
37 void RunSingleRoundAuthTest(HandlerRunMode run_mode, | 46 void RunSingleRoundAuthTest(HandlerRunMode run_mode, |
38 int handler_rv, | 47 int handler_rv, |
39 int expected_controller_rv, | 48 int expected_controller_rv, |
40 SchemeState scheme_state) { | 49 SchemeState scheme_state) { |
41 BoundNetLog dummy_log; | 50 BoundNetLog dummy_log; |
42 HttpAuthCache dummy_auth_cache; | 51 HttpAuthCache dummy_auth_cache; |
43 | 52 |
44 HttpRequestInfo request; | 53 HttpRequestInfo request; |
45 request.method = "GET"; | 54 request.method = "GET"; |
46 request.url = GURL("http://example.com"); | 55 request.url = GURL("http://example.com"); |
47 | 56 |
48 const std::string headers_raw_string = | 57 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( |
49 "HTTP/1.1 407\r\n" | 58 "HTTP/1.1 407\r\n" |
50 "Proxy-Authenticate: MOCK foo\r\n" | 59 "Proxy-Authenticate: MOCK foo\r\n" |
51 "\r\n"; | 60 "\r\n")); |
52 std::string headers_string = HttpUtil::AssembleRawHeaders( | |
53 headers_raw_string.c_str(), headers_raw_string.length()); | |
54 scoped_refptr<HttpResponseHeaders> headers( | |
55 new HttpResponseHeaders(headers_string)); | |
56 | 61 |
57 HttpAuthHandlerMock::Factory auth_handler_factory; | 62 HttpAuthHandlerMock::Factory auth_handler_factory; |
58 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); | 63 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
59 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), | 64 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), |
60 handler_rv); | 65 handler_rv); |
61 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); | 66 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
62 auth_handler_factory.set_do_init_from_challenge(true); | 67 auth_handler_factory.set_do_init_from_challenge(true); |
63 | 68 |
64 scoped_refptr<HttpAuthController> controller( | 69 scoped_refptr<HttpAuthController> controller( |
65 new HttpAuthController(HttpAuth::AUTH_PROXY, | 70 new HttpAuthController(HttpAuth::AUTH_PROXY, |
66 GURL("http://example.com"), | 71 GURL("http://example.com"), |
67 &dummy_auth_cache, &auth_handler_factory)); | 72 &dummy_auth_cache, &auth_handler_factory)); |
68 ASSERT_EQ(OK, | 73 ASSERT_EQ(OK, |
69 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 74 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
70 EXPECT_TRUE(controller->HaveAuthHandler()); | 75 ASSERT_TRUE(controller->HaveAuthHandler()); |
71 controller->ResetAuth(string16(), string16()); | 76 controller->ResetAuth(string16(), string16()); |
72 EXPECT_TRUE(controller->HaveAuth()); | 77 EXPECT_TRUE(controller->HaveAuth()); |
73 | 78 |
74 TestCompletionCallback callback; | 79 TestCompletionCallback callback; |
75 EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING: | 80 EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING: |
76 expected_controller_rv, | 81 expected_controller_rv, |
77 controller->MaybeGenerateAuthToken(&request, &callback, | 82 controller->MaybeGenerateAuthToken(&request, &callback, |
78 dummy_log)); | 83 dummy_log)); |
79 if (run_mode == RUN_HANDLER_ASYNC) | 84 if (run_mode == RUN_HANDLER_ASYNC) |
80 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); | 85 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); |
(...skipping 21 matching lines...) Expand all Loading... | |
102 // tokens. | 107 // tokens. |
103 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_MISSING_AUTH_CREDENTIALS, OK, | 108 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_MISSING_AUTH_CREDENTIALS, OK, |
104 SCHEME_IS_DISABLED); | 109 SCHEME_IS_DISABLED); |
105 | 110 |
106 // If a non-permanent error is returned by the handler, then the | 111 // If a non-permanent error is returned by the handler, then the |
107 // controller should report it unchanged. | 112 // controller should report it unchanged. |
108 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_INVALID_AUTH_CREDENTIALS, | 113 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_INVALID_AUTH_CREDENTIALS, |
109 ERR_INVALID_AUTH_CREDENTIALS, SCHEME_IS_ENABLED); | 114 ERR_INVALID_AUTH_CREDENTIALS, SCHEME_IS_ENABLED); |
110 } | 115 } |
111 | 116 |
117 // If an HttpAuthHandler indicates that it doesn't allow explicit | |
118 // credentials, don't prompt for credentials. | |
119 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { | |
120 BoundNetLog dummy_log; | |
121 HttpAuthCache dummy_auth_cache; | |
122 | |
123 HttpRequestInfo request; | |
124 request.method = "GET"; | |
125 request.url = GURL("http://example.com"); | |
126 | |
127 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( | |
128 "HTTP/1.1 401\r\n" | |
129 "WWW-Authenticate: MOCK\r\n" | |
130 "\r\n")); | |
131 | |
132 HttpAuthHandlerMock::Factory auth_handler_factory; | |
133 | |
134 // auth_handler_1 is the handler for the first attempt at authentication. It | |
135 // accepts the default identity and successfully constructs a token. | |
136 HttpAuthHandlerMock* auth_handler_1 = new HttpAuthHandlerMock(); | |
137 auth_handler_1->SetGenerateExpectation(true, OK); | |
138 auth_handler_1->set_allows_default_credentials(true); | |
139 auth_handler_1->set_allows_explicit_credentials(false); | |
140 auth_handler_1->set_connection_based(true); | |
141 auth_handler_factory.AddMockHandler(auth_handler_1, HttpAuth::AUTH_SERVER); | |
142 | |
143 // auth_handler_2 is the handler for the second attempt. It should not be | |
144 // used to generate a token. Instead the controller should realize that there | |
145 // are no viable identities to use with this handler and fail. | |
cbentzel
2011/08/30 14:16:51
Should you do test that we fallback to a less-secu
asanka
2011/08/30 18:55:44
I added a mock class that pretends to be Basic and
| |
146 HttpAuthHandlerMock* auth_handler_2 = new HttpAuthHandlerMock(); | |
147 auth_handler_2->SetGenerateExpectation(true, ERR_UNEXPECTED); | |
148 auth_handler_2->set_allows_default_credentials(true); | |
149 auth_handler_2->set_allows_explicit_credentials(false); | |
150 auth_handler_2->set_connection_based(true); | |
151 auth_handler_factory.AddMockHandler(auth_handler_2, HttpAuth::AUTH_SERVER); | |
152 auth_handler_factory.set_do_init_from_challenge(true); | |
153 | |
154 scoped_refptr<HttpAuthController> controller( | |
155 new HttpAuthController(HttpAuth::AUTH_SERVER, | |
156 GURL("http://example.com"), | |
157 &dummy_auth_cache, &auth_handler_factory)); | |
158 ASSERT_EQ(OK, | |
159 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | |
160 ASSERT_TRUE(controller->HaveAuthHandler()); | |
161 controller->ResetAuth(string16(), string16()); | |
162 EXPECT_TRUE(controller->HaveAuth()); | |
163 | |
164 { | |
165 TestCompletionCallback callback; | |
166 int result = controller->MaybeGenerateAuthToken(&request, &callback, | |
167 dummy_log); | |
168 EXPECT_EQ(ERR_IO_PENDING, result); | |
169 EXPECT_EQ(OK, callback.WaitForResult()); | |
170 } | |
171 | |
172 // Once a token is generated, simulate the receipt of a server response | |
173 // indicating that the authentication attempt was rejected. | |
174 EXPECT_EQ(OK, | |
175 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | |
176 EXPECT_FALSE(controller->HaveAuthHandler()); | |
177 EXPECT_TRUE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); | |
178 } | |
179 | |
112 } // namespace net | 180 } // namespace net |
OLD | NEW |