Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: components/update_client/request_sender_unittest.cc

Issue 2847023002: Parse update check run actions for the component updater. (Closed)
Patch Set: Added missing data file dependency. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/update_client/request_sender.h" 5 #include "components/update_client/request_sender.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
11 #include "base/path_service.h" 12 #include "base/path_service.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
14 #include "base/test/scoped_task_scheduler.h" 15 #include "base/test/scoped_task_scheduler.h"
15 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
16 #include "components/update_client/test_configurator.h" 17 #include "components/update_client/test_configurator.h"
17 #include "components/update_client/url_request_post_interceptor.h" 18 #include "components/update_client/url_request_post_interceptor.h"
18 #include "net/url_request/url_fetcher.h" 19 #include "net/url_request/url_fetcher.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 56
56 protected: 57 protected:
57 void Quit(); 58 void Quit();
58 void RunThreads(); 59 void RunThreads();
59 void RunThreadsUntilIdle(); 60 void RunThreadsUntilIdle();
60 61
61 scoped_refptr<TestConfigurator> config_; 62 scoped_refptr<TestConfigurator> config_;
62 std::unique_ptr<RequestSender> request_sender_; 63 std::unique_ptr<RequestSender> request_sender_;
63 std::unique_ptr<InterceptorFactory> interceptor_factory_; 64 std::unique_ptr<InterceptorFactory> interceptor_factory_;
64 65
65 URLRequestPostInterceptor* post_interceptor_1_; // Owned by the factory. 66 // Owned by the factory.
66 URLRequestPostInterceptor* post_interceptor_2_; // Owned by the factory. 67 URLRequestPostInterceptor* post_interceptor_1_ = nullptr;
68 URLRequestPostInterceptor* post_interceptor_2_ = nullptr;
67 69
68 int error_; 70 int error_ = 0;
69 std::string response_; 71 std::string response_;
70 72
71 private: 73 private:
72 base::MessageLoopForIO loop_; 74 base::MessageLoopForIO loop_;
73 base::test::ScopedTaskScheduler scoped_task_scheduler_; 75 base::test::ScopedTaskScheduler scoped_task_scheduler_;
74 base::Closure quit_closure_; 76 base::Closure quit_closure_;
75 77
76 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest); 78 DISALLOW_COPY_AND_ASSIGN(RequestSenderTest);
77 }; 79 };
78 80
79 RequestSenderTest::RequestSenderTest() 81 RequestSenderTest::RequestSenderTest() : scoped_task_scheduler_(&loop_) {}
80 : post_interceptor_1_(nullptr),
81 post_interceptor_2_(nullptr),
82 error_(0),
83 scoped_task_scheduler_(&loop_) {}
84 82
85 RequestSenderTest::~RequestSenderTest() {} 83 RequestSenderTest::~RequestSenderTest() {}
86 84
87 void RequestSenderTest::SetUp() { 85 void RequestSenderTest::SetUp() {
88 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(), 86 config_ = base::MakeShared<TestConfigurator>(
89 base::ThreadTaskRunnerHandle::Get()); 87 base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
90 interceptor_factory_.reset( 88 interceptor_factory_ =
91 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get())); 89 base::MakeUnique<InterceptorFactory>(base::ThreadTaskRunnerHandle::Get());
92 post_interceptor_1_ = 90 post_interceptor_1_ =
93 interceptor_factory_->CreateInterceptorForPath(kUrlPath1); 91 interceptor_factory_->CreateInterceptorForPath(kUrlPath1);
94 post_interceptor_2_ = 92 post_interceptor_2_ =
95 interceptor_factory_->CreateInterceptorForPath(kUrlPath2); 93 interceptor_factory_->CreateInterceptorForPath(kUrlPath2);
96 EXPECT_TRUE(post_interceptor_1_); 94 EXPECT_TRUE(post_interceptor_1_);
97 EXPECT_TRUE(post_interceptor_2_); 95 EXPECT_TRUE(post_interceptor_2_);
98 96
99 request_sender_.reset(); 97 request_sender_ = nullptr;
100 } 98 }
101 99
102 void RequestSenderTest::TearDown() { 100 void RequestSenderTest::TearDown() {
103 request_sender_.reset(); 101 request_sender_ = nullptr;
104 102
105 post_interceptor_1_ = nullptr; 103 post_interceptor_1_ = nullptr;
106 post_interceptor_2_ = nullptr; 104 post_interceptor_2_ = nullptr;
107 105
108 interceptor_factory_.reset(); 106 interceptor_factory_ = nullptr;
109 107
110 config_ = nullptr; 108 config_ = nullptr;
111 109
112 RunThreadsUntilIdle(); 110 RunThreadsUntilIdle();
113 } 111 }
114 112
115 void RequestSenderTest::RunThreads() { 113 void RequestSenderTest::RunThreads() {
116 base::RunLoop runloop; 114 base::RunLoop runloop;
117 quit_closure_ = runloop.QuitClosure(); 115 quit_closure_ = runloop.QuitClosure();
118 runloop.Run(); 116 runloop.Run();
(...skipping 22 matching lines...) Expand all
141 139
142 Quit(); 140 Quit();
143 } 141 }
144 142
145 // Tests that when a request to the first url succeeds, the subsequent urls are 143 // Tests that when a request to the first url succeeds, the subsequent urls are
146 // not tried. 144 // not tried.
147 TEST_F(RequestSenderTest, RequestSendSuccess) { 145 TEST_F(RequestSenderTest, RequestSendSuccess) {
148 EXPECT_TRUE(post_interceptor_1_->ExpectRequest( 146 EXPECT_TRUE(post_interceptor_1_->ExpectRequest(
149 new PartialMatch("test"), test_file("updatecheck_reply_1.xml"))); 147 new PartialMatch("test"), test_file("updatecheck_reply_1.xml")));
150 148
151 std::vector<GURL> urls; 149 const std::vector<GURL> urls = {GURL(kUrl1), GURL(kUrl2)};
152 urls.push_back(GURL(kUrl1)); 150 request_sender_ = base::MakeUnique<RequestSender>(config_);
153 urls.push_back(GURL(kUrl2));
154 request_sender_.reset(new RequestSender(config_));
155 request_sender_->Send(false, "test", urls, 151 request_sender_->Send(false, "test", urls,
156 base::Bind(&RequestSenderTest::RequestSenderComplete, 152 base::Bind(&RequestSenderTest::RequestSenderComplete,
157 base::Unretained(this))); 153 base::Unretained(this)));
158 RunThreads(); 154 RunThreads();
159 155
160 EXPECT_EQ(1, post_interceptor_1_->GetHitCount()) 156 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
161 << post_interceptor_1_->GetRequestsAsString(); 157 << post_interceptor_1_->GetRequestsAsString();
162 EXPECT_EQ(1, post_interceptor_1_->GetCount()) 158 EXPECT_EQ(1, post_interceptor_1_->GetCount())
163 << post_interceptor_1_->GetRequestsAsString(); 159 << post_interceptor_1_->GetRequestsAsString();
164 160
165 EXPECT_EQ(0, post_interceptor_2_->GetHitCount()) 161 EXPECT_EQ(0, post_interceptor_2_->GetHitCount())
166 << post_interceptor_2_->GetRequestsAsString(); 162 << post_interceptor_2_->GetRequestsAsString();
167 EXPECT_EQ(0, post_interceptor_2_->GetCount()) 163 EXPECT_EQ(0, post_interceptor_2_->GetCount())
168 << post_interceptor_2_->GetRequestsAsString(); 164 << post_interceptor_2_->GetRequestsAsString();
169 165
170 // Sanity check the request. 166 // Sanity check the request.
171 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str()); 167 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str());
172 168
173 // Check the response post conditions. 169 // Check the response post conditions.
174 EXPECT_EQ(0, error_); 170 EXPECT_EQ(0, error_);
175 EXPECT_TRUE(base::StartsWith(response_, 171 EXPECT_TRUE(base::StartsWith(response_,
176 "<?xml version='1.0' encoding='UTF-8'?>", 172 "<?xml version='1.0' encoding='UTF-8'?>",
177 base::CompareCase::SENSITIVE)); 173 base::CompareCase::SENSITIVE));
178 EXPECT_EQ(443ul, response_.size()); 174 EXPECT_EQ(505ul, response_.size());
179 } 175 }
180 176
181 // Tests that the request succeeds using the second url after the first url 177 // Tests that the request succeeds using the second url after the first url
182 // has failed. 178 // has failed.
183 TEST_F(RequestSenderTest, RequestSendSuccessWithFallback) { 179 TEST_F(RequestSenderTest, RequestSendSuccessWithFallback) {
184 EXPECT_TRUE( 180 EXPECT_TRUE(
185 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403)); 181 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403));
186 EXPECT_TRUE(post_interceptor_2_->ExpectRequest(new PartialMatch("test"))); 182 EXPECT_TRUE(post_interceptor_2_->ExpectRequest(new PartialMatch("test")));
187 183
188 std::vector<GURL> urls; 184 const std::vector<GURL> urls = {GURL(kUrl1), GURL(kUrl2)};
189 urls.push_back(GURL(kUrl1)); 185 request_sender_ = base::MakeUnique<RequestSender>(config_);
190 urls.push_back(GURL(kUrl2));
191 request_sender_.reset(new RequestSender(config_));
192 request_sender_->Send(false, "test", urls, 186 request_sender_->Send(false, "test", urls,
193 base::Bind(&RequestSenderTest::RequestSenderComplete, 187 base::Bind(&RequestSenderTest::RequestSenderComplete,
194 base::Unretained(this))); 188 base::Unretained(this)));
195 RunThreads(); 189 RunThreads();
196 190
197 EXPECT_EQ(1, post_interceptor_1_->GetHitCount()) 191 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
198 << post_interceptor_1_->GetRequestsAsString(); 192 << post_interceptor_1_->GetRequestsAsString();
199 EXPECT_EQ(1, post_interceptor_1_->GetCount()) 193 EXPECT_EQ(1, post_interceptor_1_->GetCount())
200 << post_interceptor_1_->GetRequestsAsString(); 194 << post_interceptor_1_->GetRequestsAsString();
201 EXPECT_EQ(1, post_interceptor_2_->GetHitCount()) 195 EXPECT_EQ(1, post_interceptor_2_->GetHitCount())
202 << post_interceptor_2_->GetRequestsAsString(); 196 << post_interceptor_2_->GetRequestsAsString();
203 EXPECT_EQ(1, post_interceptor_2_->GetCount()) 197 EXPECT_EQ(1, post_interceptor_2_->GetCount())
204 << post_interceptor_2_->GetRequestsAsString(); 198 << post_interceptor_2_->GetRequestsAsString();
205 199
206 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str()); 200 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str());
207 EXPECT_STREQ("test", post_interceptor_2_->GetRequests()[0].c_str()); 201 EXPECT_STREQ("test", post_interceptor_2_->GetRequests()[0].c_str());
208 EXPECT_EQ(0, error_); 202 EXPECT_EQ(0, error_);
209 } 203 }
210 204
211 // Tests that the request fails when both urls have failed. 205 // Tests that the request fails when both urls have failed.
212 TEST_F(RequestSenderTest, RequestSendFailed) { 206 TEST_F(RequestSenderTest, RequestSendFailed) {
213 EXPECT_TRUE( 207 EXPECT_TRUE(
214 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403)); 208 post_interceptor_1_->ExpectRequest(new PartialMatch("test"), 403));
215 EXPECT_TRUE( 209 EXPECT_TRUE(
216 post_interceptor_2_->ExpectRequest(new PartialMatch("test"), 403)); 210 post_interceptor_2_->ExpectRequest(new PartialMatch("test"), 403));
217 211
218 std::vector<GURL> urls; 212 const std::vector<GURL> urls = {GURL(kUrl1), GURL(kUrl2)};
219 urls.push_back(GURL(kUrl1)); 213 request_sender_ = base::MakeUnique<RequestSender>(config_);
220 urls.push_back(GURL(kUrl2));
221 request_sender_.reset(new RequestSender(config_));
222 request_sender_->Send(false, "test", urls, 214 request_sender_->Send(false, "test", urls,
223 base::Bind(&RequestSenderTest::RequestSenderComplete, 215 base::Bind(&RequestSenderTest::RequestSenderComplete,
224 base::Unretained(this))); 216 base::Unretained(this)));
225 RunThreads(); 217 RunThreads();
226 218
227 EXPECT_EQ(1, post_interceptor_1_->GetHitCount()) 219 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
228 << post_interceptor_1_->GetRequestsAsString(); 220 << post_interceptor_1_->GetRequestsAsString();
229 EXPECT_EQ(1, post_interceptor_1_->GetCount()) 221 EXPECT_EQ(1, post_interceptor_1_->GetCount())
230 << post_interceptor_1_->GetRequestsAsString(); 222 << post_interceptor_1_->GetRequestsAsString();
231 EXPECT_EQ(1, post_interceptor_2_->GetHitCount()) 223 EXPECT_EQ(1, post_interceptor_2_->GetHitCount())
232 << post_interceptor_2_->GetRequestsAsString(); 224 << post_interceptor_2_->GetRequestsAsString();
233 EXPECT_EQ(1, post_interceptor_2_->GetCount()) 225 EXPECT_EQ(1, post_interceptor_2_->GetCount())
234 << post_interceptor_2_->GetRequestsAsString(); 226 << post_interceptor_2_->GetRequestsAsString();
235 227
236 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str()); 228 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str());
237 EXPECT_STREQ("test", post_interceptor_2_->GetRequests()[0].c_str()); 229 EXPECT_STREQ("test", post_interceptor_2_->GetRequests()[0].c_str());
238 EXPECT_EQ(403, error_); 230 EXPECT_EQ(403, error_);
239 } 231 }
240 232
241 // Tests that the request fails when no urls are provided. 233 // Tests that the request fails when no urls are provided.
242 TEST_F(RequestSenderTest, RequestSendFailedNoUrls) { 234 TEST_F(RequestSenderTest, RequestSendFailedNoUrls) {
243 std::vector<GURL> urls; 235 std::vector<GURL> urls;
244 request_sender_.reset(new RequestSender(config_)); 236 request_sender_ = base::MakeUnique<RequestSender>(config_);
245 request_sender_->Send(false, "test", urls, 237 request_sender_->Send(false, "test", urls,
246 base::Bind(&RequestSenderTest::RequestSenderComplete, 238 base::Bind(&RequestSenderTest::RequestSenderComplete,
247 base::Unretained(this))); 239 base::Unretained(this)));
248 RunThreads(); 240 RunThreads();
249 241
250 EXPECT_EQ(-1, error_); 242 EXPECT_EQ(-1, error_);
251 } 243 }
252 244
253 // Tests that a CUP request fails if the response is not signed. 245 // Tests that a CUP request fails if the response is not signed.
254 TEST_F(RequestSenderTest, RequestSendCupError) { 246 TEST_F(RequestSenderTest, RequestSendCupError) {
255 EXPECT_TRUE(post_interceptor_1_->ExpectRequest( 247 EXPECT_TRUE(post_interceptor_1_->ExpectRequest(
256 new PartialMatch("test"), test_file("updatecheck_reply_1.xml"))); 248 new PartialMatch("test"), test_file("updatecheck_reply_1.xml")));
257 249
258 std::vector<GURL> urls; 250 const std::vector<GURL> urls = {GURL(kUrl1)};
259 urls.push_back(GURL(kUrl1)); 251 request_sender_ = base::MakeUnique<RequestSender>(config_);
260 request_sender_.reset(new RequestSender(config_));
261 request_sender_->Send(true, "test", urls, 252 request_sender_->Send(true, "test", urls,
262 base::Bind(&RequestSenderTest::RequestSenderComplete, 253 base::Bind(&RequestSenderTest::RequestSenderComplete,
263 base::Unretained(this))); 254 base::Unretained(this)));
264 RunThreads(); 255 RunThreads();
265 256
266 EXPECT_EQ(1, post_interceptor_1_->GetHitCount()) 257 EXPECT_EQ(1, post_interceptor_1_->GetHitCount())
267 << post_interceptor_1_->GetRequestsAsString(); 258 << post_interceptor_1_->GetRequestsAsString();
268 EXPECT_EQ(1, post_interceptor_1_->GetCount()) 259 EXPECT_EQ(1, post_interceptor_1_->GetCount())
269 << post_interceptor_1_->GetRequestsAsString(); 260 << post_interceptor_1_->GetRequestsAsString();
270 261
271 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str()); 262 EXPECT_STREQ("test", post_interceptor_1_->GetRequests()[0].c_str());
272 EXPECT_EQ(RequestSender::kErrorResponseNotTrusted, error_); 263 EXPECT_EQ(RequestSender::kErrorResponseNotTrusted, error_);
273 EXPECT_TRUE(response_.empty()); 264 EXPECT_TRUE(response_.empty());
274 } 265 }
275 266
276 } // namespace update_client 267 } // namespace update_client
OLDNEW
« no previous file with comments | « components/update_client/component.cc ('k') | components/update_client/update_checker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698