1 // Copyright 2018 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/transitional_url_loader_factory_owner.h"
6 
7 #include <memory>
8 #include <string>
9 
10 #include "base/message_loop/message_pump_type.h"
11 #include "base/run_loop.h"
12 #include "base/test/bind_test_util.h"
13 #include "base/test/task_environment.h"
14 #include "base/threading/thread.h"
15 #include "net/test/embedded_test_server/default_handlers.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "services/network/public/cpp/resource_request.h"
20 #include "services/network/public/cpp/shared_url_loader_factory.h"
21 #include "services/network/public/cpp/simple_url_loader.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 
24 namespace network {
25 namespace {
26 
27 class TransitionalURLLoaderFactoryOwnerTest : public ::testing::Test {
28  public:
TransitionalURLLoaderFactoryOwnerTest()29   TransitionalURLLoaderFactoryOwnerTest()
30       : task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {}
31 
SetUp()32   void SetUp() override {
33     net::test_server::RegisterDefaultHandlers(&test_server_);
34     ASSERT_TRUE(test_server_.Start());
35   }
36 
TestOnTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner,base::OnceClosure flush_thread)37   void TestOnTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
38                         base::OnceClosure flush_thread) {
39     auto url_request_context_getter =
40         base::MakeRefCounted<net::TestURLRequestContextGetter>(task_runner);
41     auto owner = std::make_unique<TransitionalURLLoaderFactoryOwner>(
42         url_request_context_getter);
43 
44     scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
45         owner->GetURLLoaderFactory();
46     ASSERT_TRUE(url_loader_factory != nullptr);
47 
48     // Try fetching something --- see if |url_loader_factory| is usable.
49     auto request = std::make_unique<ResourceRequest>();
50     request->url = test_server_.GetURL("/cachetime");
51     std::unique_ptr<SimpleURLLoader> loader = SimpleURLLoader::Create(
52         std::move(request), TRAFFIC_ANNOTATION_FOR_TESTS);
53 
54     base::RunLoop run_loop;
55     loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
56         url_loader_factory.get(),
57         base::BindLambdaForTesting([&](std::unique_ptr<std::string> body) {
58           ASSERT_TRUE(body);
59           EXPECT_NE(std::string::npos, body->find("<title>Cache:")) << *body;
60           run_loop.Quit();
61         }));
62     run_loop.Run();
63 
64     // Clean stuff up, should be clean on lsan.
65     owner = nullptr;
66     std::move(flush_thread).Run();
67   }
68 
69  protected:
70   base::test::TaskEnvironment task_environment_;
71   net::EmbeddedTestServer test_server_;
72 };
73 
TEST_F(TransitionalURLLoaderFactoryOwnerTest,CrossThread)74 TEST_F(TransitionalURLLoaderFactoryOwnerTest, CrossThread) {
75   base::Thread io_thread("IO");
76   base::Thread::Options options;
77   options.message_pump_type = base::MessagePumpType::IO;
78   ASSERT_TRUE(io_thread.StartWithOptions(options));
79 
80   TestOnTaskRunner(io_thread.task_runner(), base::BindLambdaForTesting([&]() {
81                      io_thread.FlushForTesting();
82                    }));
83 }
84 
TEST_F(TransitionalURLLoaderFactoryOwnerTest,SameThread)85 TEST_F(TransitionalURLLoaderFactoryOwnerTest, SameThread) {
86   TestOnTaskRunner(
87       task_environment_.GetMainThreadTaskRunner(),
88       base::BindLambdaForTesting([&]() { task_environment_.RunUntilIdle(); }));
89 }
90 
91 }  // namespace
92 }  // namespace network
93