1 // Copyright 2019 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.
4
5 #include "services/network/dhcp_pac_file_fetcher_mojo.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "net/base/test_completion_callback.h"
9 #include "net/proxy_resolution/mock_pac_file_fetcher.h"
10 #include "net/test/gtest_util.h"
11 #include "net/test/test_with_task_environment.h"
12 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "services/network/dhcp_pac_file_fetcher_mojo.h"
15 #include "services/network/mock_mojo_dhcp_wpad_url_client.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace {
19 using net::test::IsError;
20 using net::test::IsOk;
21 } // namespace
22
23 namespace network {
24
25 class DhcpPacFileFetcherMojoTest : public testing::Test {
26 public:
27 DhcpPacFileFetcherMojoTest() = default;
~DhcpPacFileFetcherMojoTest()28 ~DhcpPacFileFetcherMojoTest() override {}
29
30 protected:
CreateFetcher(const std::string & pac_url)31 void CreateFetcher(const std::string& pac_url) {
32 net::TestURLRequestContext context;
33 dhcp_pac_file_fetcher_mojo_ = std::make_unique<DhcpPacFileFetcherMojo>(
34 &context,
35 network::MockMojoDhcpWpadUrlClient::CreateWithSelfOwnedReceiver(
36 pac_url));
37 mock_pac_file_fetcher_ = new net::MockPacFileFetcher();
38 dhcp_pac_file_fetcher_mojo_->SetPacFileFetcherForTesting(
39 base::WrapUnique(mock_pac_file_fetcher_));
40 }
41
42 std::unique_ptr<DhcpPacFileFetcherMojo> dhcp_pac_file_fetcher_mojo_;
43 net::MockPacFileFetcher* mock_pac_file_fetcher_;
44
45 private:
46 base::test::TaskEnvironment task_environment_;
47 };
48
49 // Test that the PAC URL set by the client is used.
TEST_F(DhcpPacFileFetcherMojoTest,UsePacSctipt)50 TEST_F(DhcpPacFileFetcherMojoTest, UsePacSctipt) {
51 GURL pac_url("http://wpad.test.com/wpad.dat");
52 CreateFetcher(pac_url.spec());
53
54 net::TestCompletionCallback callback;
55 base::string16 pac_text;
56 dhcp_pac_file_fetcher_mojo_->Fetch(&pac_text, callback.callback(),
57 net::NetLogWithSource(),
58 TRAFFIC_ANNOTATION_FOR_TESTS);
59 mock_pac_file_fetcher_->WaitUntilFetch();
60 EXPECT_EQ(pac_url, mock_pac_file_fetcher_->pending_request_url());
61 mock_pac_file_fetcher_->NotifyFetchCompletion(net::OK, "script");
62
63 EXPECT_THAT(callback.WaitForResult(), IsOk());
64 }
65
66 // Test that error is returned when PAC URL is missing.
TEST_F(DhcpPacFileFetcherMojoTest,PacScriptMissing)67 TEST_F(DhcpPacFileFetcherMojoTest, PacScriptMissing) {
68 CreateFetcher(std::string());
69
70 net::TestCompletionCallback callback;
71 base::string16 pac_text;
72 dhcp_pac_file_fetcher_mojo_->Fetch(&pac_text, callback.callback(),
73 net::NetLogWithSource(),
74 TRAFFIC_ANNOTATION_FOR_TESTS);
75
76 EXPECT_THAT(callback.WaitForResult(), net::ERR_PAC_NOT_IN_DHCP);
77 }
78
79 } // namespace network
80