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