blob: 4225f58cad7cba48a7991ea69e5f5036e81087f3 [file] [log] [blame]
sorin395c2ac2014-09-16 21:31:071// Copyright 2014 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
dchengd0fc6aa92016-04-22 18:03:125#include "components/update_client/request_sender.h"
6
7#include <memory>
Sorin Jianua8ef73d2017-11-02 16:55:178#include <utility>
dchengd0fc6aa92016-04-22 18:03:129
sorin5cb1f5492014-09-23 04:07:4410#include "base/macros.h"
sorin9797aba2015-04-17 17:15:0311#include "base/memory/ref_counted.h"
Sorin Jianud907dbf62018-02-21 17:18:2912#include "base/optional.h"
davidben74f67442016-10-01 01:45:2213#include "base/path_service.h"
sorin395c2ac2014-09-16 21:31:0714#include "base/run_loop.h"
sorin1bc5eff2016-02-17 18:45:1715#include "base/strings/string_util.h"
Francois Dorayff8a6452017-07-27 17:11:5816#include "base/test/scoped_task_environment.h"
gab7966d312016-05-11 20:35:0117#include "base/threading/thread_task_runner_handle.h"
sorinb120440b2015-04-27 16:34:1518#include "components/update_client/test_configurator.h"
19#include "components/update_client/url_request_post_interceptor.h"
sorin395c2ac2014-09-16 21:31:0720#include "net/url_request/url_fetcher.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
sorin52ac0882015-01-24 01:15:0023namespace update_client {
sorin395c2ac2014-09-16 21:31:0724
25namespace {
26
27const char kUrl1[] = "https://localhost2/path1";
28const char kUrl2[] = "https://localhost2/path2";
29const char kUrlPath1[] = "path1";
30const char kUrlPath2[] = "path2";
31
sorin1bc5eff2016-02-17 18:45:1732// TODO(sorin): refactor as a utility function for unit tests.
33base::FilePath test_file(const char* file) {
34 base::FilePath path;
35 PathService::Get(base::DIR_SOURCE_ROOT, &path);
36 return path.AppendASCII("components")
37 .AppendASCII("test")
38 .AppendASCII("data")
39 .AppendASCII("update_client")
40 .AppendASCII(file);
41}
42
sorin395c2ac2014-09-16 21:31:0743} // namespace
44
Sorin Jianud907dbf62018-02-21 17:18:2945class RequestSenderTest : public testing::Test,
46 public ::testing::WithParamInterface<bool> {
sorin395c2ac2014-09-16 21:31:0747 public:
48 RequestSenderTest();
dcheng30a1b1542014-10-29 21:27:5049 ~RequestSenderTest() override;
sorin395c2ac2014-09-16 21:31:0750
51 // Overrides from testing::Test.
dcheng30a1b1542014-10-29 21:27:5052 void SetUp() override;
53 void TearDown() override;
sorin395c2ac2014-09-16 21:31:0754
sorinfccbf2d2016-04-04 20:34:3455 void RequestSenderComplete(int error,
56 const std::string& response,
57 int retry_after_sec);
sorin395c2ac2014-09-16 21:31:0758
59 protected:
60 void Quit();
61 void RunThreads();
Francois Dorayff8a6452017-07-27 17:11:5862
63 base::test::ScopedTaskEnvironment scoped_task_environment_;
sorin395c2ac2014-09-16 21:31:0764
sorin9797aba2015-04-17 17:15:0365 scoped_refptr<TestConfigurator> config_;
dchengd0fc6aa92016-04-22 18:03:1266 std::unique_ptr<RequestSender> request_sender_;
67 std::unique_ptr<InterceptorFactory> interceptor_factory_;
sorin395c2ac2014-09-16 21:31:0768
sorin519656c2017-04-28 22:39:3469 // Owned by the factory.
70 URLRequestPostInterceptor* post_interceptor_1_ = nullptr;
71 URLRequestPostInterceptor* post_interceptor_2_ = nullptr;
sorin395c2ac2014-09-16 21:31:0772
sorin519656c2017-04-28 22:39:3473 int error_ = 0;
sorin1bc5eff2016-02-17 18:45:1774 std::string response_;
sorin395c2ac2014-09-16 21:31:0775
76 private:
Sorin Jianua8ef73d2017-11-02 16:55:1777 base::OnceClosure quit_closure_;
sorin395c2ac2014-09-16 21:31:0778
79 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest);
80};
81
Sorin Jianud907dbf62018-02-21 17:18:2982INSTANTIATE_TEST_CASE_P(IsForeground, RequestSenderTest, ::testing::Bool());
83
Francois Dorayff8a6452017-07-27 17:11:5884RequestSenderTest::RequestSenderTest()
85 : scoped_task_environment_(
86 base::test::ScopedTaskEnvironment::MainThreadType::IO) {}
sorin395c2ac2014-09-16 21:31:0787
sorin1bc5eff2016-02-17 18:45:1788RequestSenderTest::~RequestSenderTest() {}
sorin395c2ac2014-09-16 21:31:0789
90void RequestSenderTest::SetUp() {
Sorin Jianucc048f892017-07-26 02:05:5491 config_ = base::MakeRefCounted<TestConfigurator>();
sorin519656c2017-04-28 22:39:3492 interceptor_factory_ =
Jinho Bangda4e4282018-01-03 13:21:2393 std::make_unique<InterceptorFactory>(base::ThreadTaskRunnerHandle::Get());
sorin5cabcf0b2015-06-09 00:54:4494 post_interceptor_1_ =
sorin395c2ac2014-09-16 21:31:0795 interceptor_factory_->CreateInterceptorForPath(kUrlPath1);
sorin5cabcf0b2015-06-09 00:54:4496 post_interceptor_2_ =
sorin395c2ac2014-09-16 21:31:0797 interceptor_factory_->CreateInterceptorForPath(kUrlPath2);
sorin5cabcf0b2015-06-09 00:54:4498 EXPECT_TRUE(post_interceptor_1_);
99 EXPECT_TRUE(post_interceptor_2_);
sorin395c2ac2014-09-16 21:31:07100
sorin519656c2017-04-28 22:39:34101 request_sender_ = nullptr;
sorin395c2ac2014-09-16 21:31:07102}
103
104void RequestSenderTest::TearDown() {
sorin519656c2017-04-28 22:39:34105 request_sender_ = nullptr;
sorin395c2ac2014-09-16 21:31:07106
sorin5cabcf0b2015-06-09 00:54:44107 post_interceptor_1_ = nullptr;
108 post_interceptor_2_ = nullptr;
sorin395c2ac2014-09-16 21:31:07109
sorin519656c2017-04-28 22:39:34110 interceptor_factory_ = nullptr;
sorin395c2ac2014-09-16 21:31:07111
Sorin Jianuf805c222017-09-20 22:36:21112 // Run the threads until they are idle to allow the clean up
113 // of the network interceptors on the IO thread.
114 scoped_task_environment_.RunUntilIdle();
sorin9797aba2015-04-17 17:15:03115 config_ = nullptr;
sorin395c2ac2014-09-16 21:31:07116}
117
118void RequestSenderTest::RunThreads() {
119 base::RunLoop runloop;
120 quit_closure_ = runloop.QuitClosure();
121 runloop.Run();
sorin395c2ac2014-09-16 21:31:07122}
123
124void RequestSenderTest::Quit() {
125 if (!quit_closure_.is_null())
Sorin Jianua8ef73d2017-11-02 16:55:17126 std::move(quit_closure_).Run();
sorin395c2ac2014-09-16 21:31:07127}
128
sorin1bc5eff2016-02-17 18:45:17129void RequestSenderTest::RequestSenderComplete(int error,
sorinfccbf2d2016-04-04 20:34:34130 const std::string& response,
131 int retry_after_sec) {
sorin1bc5eff2016-02-17 18:45:17132 error_ = error;
133 response_ = response;
134
sorin395c2ac2014-09-16 21:31:07135 Quit();
136}
137
138// Tests that when a request to the first url succeeds, the subsequent urls are
139// not tried.
Sorin Jianud907dbf62018-02-21 17:18:29140TEST_P(RequestSenderTest, RequestSendSuccess) {
sorin1bc5eff2016-02-17 18:45:17141 EXPECT_TRUE(post_interceptor_1_->ExpectRequest(
142 new PartialMatch("test"), test_file("updatecheck_reply_1.xml")));
sorin395c2ac2014-09-16 21:31:07143
sorin519656c2017-04-28 22:39:34144 const std::vector<GURL> urls = {GURL(kUrl1), GURL(kUrl2)};
Sorin Jianud907dbf62018-02-21 17:18:29145 const bool is_foreground = GetParam();
Jinho Bangda4e4282018-01-03 13:21:23146 request_sender_ = std::make_unique<RequestSender>(config_);
Sorin Jianua8ef73d2017-11-02 16:55:17147 request_sender_->Send(
Sorin Jianud907dbf62018-02-21 17:18:29148 false, "test", base::make_optional(is_foreground), urls,
Sorin Jianua8ef73d2017-11-02 16:55:17149 base::BindOnce(&RequestSenderTest::RequestSenderComplete,
150 base::Unretained(this)));
sorin395c2ac2014-09-16 21:31:07151 RunThreads();
152
sorin5cabcf0b2015-06-09 00:54:44153 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
154 << post_interceptor_1_->GetRequestsAsString();
155 EXPECT_EQ(1, post_interceptor_1_->GetCount())
156 << post_interceptor_1_->GetRequestsAsString();
sorin395c2ac2014-09-16 21:31:07157
sorin30474f02017-04-27 00:45:48158 EXPECT_EQ(0, post_interceptor_2_->GetHitCount())
159 << post_interceptor_2_->GetRequestsAsString();
160 EXPECT_EQ(0, post_interceptor_2_->GetCount())
161 << post_interceptor_2_->GetRequestsAsString();
162
sorin1bc5eff2016-02-17 18:45:17163 // Sanity check the request.
Sorin Jianud907dbf62018-02-21 17:18:29164 EXPECT_STREQ("test", post_interceptor_1_->GetRequestBody(0).c_str());
sorin1bc5eff2016-02-17 18:45:17165
166 // Check the response post conditions.
167 EXPECT_EQ(0, error_);
168 EXPECT_TRUE(base::StartsWith(response_,
169 "<?xml version='1.0' encoding='UTF-8'?>",
170 base::CompareCase::SENSITIVE));
sorin519656c2017-04-28 22:39:34171 EXPECT_EQ(505ul, response_.size());
Sorin Jianuf805c222017-09-20 22:36:21172
Sorin Jianud907dbf62018-02-21 17:18:29173 // Check the interactivity header value.
174 const auto extra_request_headers =
175 post_interceptor_1_->GetRequests()[0].second;
176 EXPECT_TRUE(extra_request_headers.HasHeader("X-GoogleUpdate-Interactivity"));
177 std::string header;
178 extra_request_headers.GetHeader("X-GoogleUpdate-Interactivity", &header);
179 EXPECT_STREQ(is_foreground ? "fg" : "bg", header.c_str());
180
Sorin Jianuf805c222017-09-20 22:36:21181 interceptor_factory_ = nullptr;
sorin395c2ac2014-09-16 21:31:07182}
183
184// Tests that the request succeeds using the second url after the first url
185// has failed.
186TEST_F(RequestSenderTest, RequestSendSuccessWithFallback) {
sorin5cabcf0b2015-06-09 00:54:44187 EXPECT_TRUE(
188 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403));
189 EXPECT_TRUE(post_interceptor_2_->ExpectRequest(new PartialMatch("test")));
sorin395c2ac2014-09-16 21:31:07190
sorin519656c2017-04-28 22:39:34191 const std::vector<GURL> urls = {GURL(kUrl1), GURL(kUrl2)};
Jinho Bangda4e4282018-01-03 13:21:23192 request_sender_ = std::make_unique<RequestSender>(config_);
Sorin Jianua8ef73d2017-11-02 16:55:17193 request_sender_->Send(
Sorin Jianud907dbf62018-02-21 17:18:29194 false, "test", base::make_optional(false), urls,
Sorin Jianua8ef73d2017-11-02 16:55:17195 base::BindOnce(&RequestSenderTest::RequestSenderComplete,
196 base::Unretained(this)));
sorin395c2ac2014-09-16 21:31:07197 RunThreads();
198
sorin5cabcf0b2015-06-09 00:54:44199 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
200 << post_interceptor_1_->GetRequestsAsString();
201 EXPECT_EQ(1, post_interceptor_1_->GetCount())
202 << post_interceptor_1_->GetRequestsAsString();
203 EXPECT_EQ(1, post_interceptor_2_->GetHitCount())
204 << post_interceptor_2_->GetRequestsAsString();
205 EXPECT_EQ(1, post_interceptor_2_->GetCount())
206 << post_interceptor_2_->GetRequestsAsString();
sorin395c2ac2014-09-16 21:31:07207
Sorin Jianud907dbf62018-02-21 17:18:29208 EXPECT_STREQ("test", post_interceptor_1_->GetRequestBody(0).c_str());
209 EXPECT_STREQ("test", post_interceptor_2_->GetRequestBody(0).c_str());
sorin1bc5eff2016-02-17 18:45:17210 EXPECT_EQ(0, error_);
sorin395c2ac2014-09-16 21:31:07211}
212
213// Tests that the request fails when both urls have failed.
214TEST_F(RequestSenderTest, RequestSendFailed) {
sorin5cabcf0b2015-06-09 00:54:44215 EXPECT_TRUE(
216 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403));
217 EXPECT_TRUE(
218 post_interceptor_2_->ExpectRequest(new PartialMatch("test"), 403));
sorin395c2ac2014-09-16 21:31:07219
sorin519656c2017-04-28 22:39:34220 const std::vector<GURL> urls = {GURL(kUrl1), GURL(kUrl2)};
Jinho Bangda4e4282018-01-03 13:21:23221 request_sender_ = std::make_unique<RequestSender>(config_);
Sorin Jianua8ef73d2017-11-02 16:55:17222 request_sender_->Send(
Sorin Jianud907dbf62018-02-21 17:18:29223 false, "test", base::nullopt, urls,
Sorin Jianua8ef73d2017-11-02 16:55:17224 base::BindOnce(&RequestSenderTest::RequestSenderComplete,
225 base::Unretained(this)));
sorin395c2ac2014-09-16 21:31:07226 RunThreads();
227
sorin5cabcf0b2015-06-09 00:54:44228 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
229 << post_interceptor_1_->GetRequestsAsString();
230 EXPECT_EQ(1, post_interceptor_1_->GetCount())
231 << post_interceptor_1_->GetRequestsAsString();
232 EXPECT_EQ(1, post_interceptor_2_->GetHitCount())
233 << post_interceptor_2_->GetRequestsAsString();
234 EXPECT_EQ(1, post_interceptor_2_->GetCount())
235 << post_interceptor_2_->GetRequestsAsString();
sorin395c2ac2014-09-16 21:31:07236
Sorin Jianud907dbf62018-02-21 17:18:29237 EXPECT_STREQ("test", post_interceptor_1_->GetRequestBody(0).c_str());
238 EXPECT_STREQ("test", post_interceptor_2_->GetRequestBody(0).c_str());
sorin1bc5eff2016-02-17 18:45:17239 EXPECT_EQ(403, error_);
sorin395c2ac2014-09-16 21:31:07240}
241
sorin5cabcf0b2015-06-09 00:54:44242// Tests that the request fails when no urls are provided.
243TEST_F(RequestSenderTest, RequestSendFailedNoUrls) {
244 std::vector<GURL> urls;
Jinho Bangda4e4282018-01-03 13:21:23245 request_sender_ = std::make_unique<RequestSender>(config_);
Sorin Jianua8ef73d2017-11-02 16:55:17246 request_sender_->Send(
Sorin Jianud907dbf62018-02-21 17:18:29247 false, "test", base::nullopt, urls,
Sorin Jianua8ef73d2017-11-02 16:55:17248 base::BindOnce(&RequestSenderTest::RequestSenderComplete,
249 base::Unretained(this)));
sorin5cabcf0b2015-06-09 00:54:44250 RunThreads();
251
sorin1bc5eff2016-02-17 18:45:17252 EXPECT_EQ(-1, error_);
253}
254
255// Tests that a CUP request fails if the response is not signed.
256TEST_F(RequestSenderTest, RequestSendCupError) {
257 EXPECT_TRUE(post_interceptor_1_->ExpectRequest(
258 new PartialMatch("test"), test_file("updatecheck_reply_1.xml")));
259
sorin519656c2017-04-28 22:39:34260 const std::vector<GURL> urls = {GURL(kUrl1)};
Jinho Bangda4e4282018-01-03 13:21:23261 request_sender_ = std::make_unique<RequestSender>(config_);
Sorin Jianua8ef73d2017-11-02 16:55:17262 request_sender_->Send(
Sorin Jianud907dbf62018-02-21 17:18:29263 true, "test", base::nullopt, urls,
Sorin Jianua8ef73d2017-11-02 16:55:17264 base::BindOnce(&RequestSenderTest::RequestSenderComplete,
265 base::Unretained(this)));
sorin1bc5eff2016-02-17 18:45:17266 RunThreads();
267
268 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
269 << post_interceptor_1_->GetRequestsAsString();
270 EXPECT_EQ(1, post_interceptor_1_->GetCount())
271 << post_interceptor_1_->GetRequestsAsString();
272
Sorin Jianud907dbf62018-02-21 17:18:29273 EXPECT_STREQ("test", post_interceptor_1_->GetRequestBody(0).c_str());
sorin1bc5eff2016-02-17 18:45:17274 EXPECT_EQ(RequestSender::kErrorResponseNotTrusted, error_);
275 EXPECT_TRUE(response_.empty());
sorin5cabcf0b2015-06-09 00:54:44276}
277
sorin52ac0882015-01-24 01:15:00278} // namespace update_client