1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/http/http_auth_handler.h"
6
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/task_environment.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/network_anonymization_key.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_mock.h"
15 #include "net/http/http_request_info.h"
16 #include "net/log/net_log_event_type.h"
17 #include "net/log/net_log_source_type.h"
18 #include "net/log/test_net_log.h"
19 #include "net/log/test_net_log_util.h"
20 #include "net/ssl/ssl_info.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h"
23 #include "url/scheme_host_port.h"
24
25 namespace net {
26
TEST(HttpAuthHandlerTest,NetLog)27 TEST(HttpAuthHandlerTest, NetLog) {
28 base::test::TaskEnvironment task_environment;
29
30 url::SchemeHostPort scheme_host_port(GURL("http://www.example.com"));
31 std::string challenge = "Mock asdf";
32 AuthCredentials credentials(u"user", u"pass");
33 std::string auth_token;
34 HttpRequestInfo request;
35
36 for (auto async : {true, false}) {
37 for (auto target : {HttpAuth::AUTH_PROXY, HttpAuth::AUTH_SERVER}) {
38 TestCompletionCallback test_callback;
39 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end());
40 HttpAuthHandlerMock mock_handler;
41 RecordingNetLogObserver net_log_observer;
42
43 // set_connection_based(true) indicates that the HandleAnotherChallenge()
44 // call after GenerateAuthToken() is expected and does not result in
45 // AUTHORIZATION_RESULT_REJECT.
46 mock_handler.set_connection_based(true);
47 mock_handler.InitFromChallenge(
48 &tokenizer, target, SSLInfo(), NetworkAnonymizationKey(),
49 scheme_host_port, NetLogWithSource::Make(NetLogSourceType::NONE));
50 mock_handler.SetGenerateExpectation(async, OK);
51 mock_handler.GenerateAuthToken(&credentials, &request,
52 test_callback.callback(), &auth_token);
53 if (async)
54 test_callback.WaitForResult();
55
56 mock_handler.HandleAnotherChallenge(&tokenizer);
57
58 auto entries = net_log_observer.GetEntries();
59
60 ASSERT_EQ(5u, entries.size());
61 EXPECT_TRUE(LogContainsBeginEvent(entries, 0,
62 NetLogEventType::AUTH_HANDLER_INIT));
63 EXPECT_TRUE(
64 LogContainsEndEvent(entries, 1, NetLogEventType::AUTH_HANDLER_INIT));
65 EXPECT_TRUE(LogContainsBeginEvent(entries, 2,
66 NetLogEventType::AUTH_GENERATE_TOKEN));
67 EXPECT_TRUE(LogContainsEndEvent(entries, 3,
68 NetLogEventType::AUTH_GENERATE_TOKEN));
69 EXPECT_TRUE(LogContainsEntryWithType(
70 entries, 4, NetLogEventType::AUTH_HANDLE_CHALLENGE));
71 }
72 }
73 }
74
75 } // namespace net
76