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

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

Issue 2835803002: Refactor the UpdateEngine and its actions in the component updater. (Closed)
Patch Set: feedback up to #6 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
« no previous file with comments | « components/update_client/update_checker.cc ('k') | components/update_client/update_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/update_checker.h" 5 #include "components/update_client/update_checker.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/test/scoped_task_scheduler.h" 18 #include "base/test/scoped_task_scheduler.h"
19 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/version.h" 20 #include "base/version.h"
21 #include "build/build_config.h" 21 #include "build/build_config.h"
22 #include "components/prefs/testing_pref_service.h" 22 #include "components/prefs/testing_pref_service.h"
23 #include "components/update_client/crx_update_item.h" 23 #include "components/update_client/component.h"
24 #include "components/update_client/persisted_data.h" 24 #include "components/update_client/persisted_data.h"
25 #include "components/update_client/test_configurator.h" 25 #include "components/update_client/test_configurator.h"
26 #include "components/update_client/update_engine.h"
26 #include "components/update_client/url_request_post_interceptor.h" 27 #include "components/update_client/url_request_post_interceptor.h"
27 #include "net/url_request/url_request_test_util.h" 28 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
29 #include "url/gurl.h" 30 #include "url/gurl.h"
30 31
31 using std::string; 32 using std::string;
32 33
33 namespace update_client { 34 namespace update_client {
34 35
35 namespace { 36 namespace {
(...skipping 14 matching lines...) Expand all
50 51
51 class UpdateCheckerTest : public testing::Test { 52 class UpdateCheckerTest : public testing::Test {
52 public: 53 public:
53 UpdateCheckerTest(); 54 UpdateCheckerTest();
54 ~UpdateCheckerTest() override; 55 ~UpdateCheckerTest() override;
55 56
56 // Overrides from testing::Test. 57 // Overrides from testing::Test.
57 void SetUp() override; 58 void SetUp() override;
58 void TearDown() override; 59 void TearDown() override;
59 60
60 void UpdateCheckComplete(int error, 61 void UpdateCheckComplete(int error, int retry_after_sec);
61 const UpdateResponse::Results& results,
62 int retry_after_sec);
63 62
64 protected: 63 protected:
65 void Quit(); 64 void Quit();
66 void RunThreads(); 65 void RunThreads();
67 void RunThreadsUntilIdle(); 66 void RunThreadsUntilIdle();
68 67
69 std::unique_ptr<CrxUpdateItem> BuildCrxUpdateItem(); 68 std::unique_ptr<Component> MakeComponent() const;
70 69
71 scoped_refptr<TestConfigurator> config_; 70 scoped_refptr<TestConfigurator> config_;
72 std::unique_ptr<TestingPrefServiceSimple> pref_; 71 std::unique_ptr<TestingPrefServiceSimple> pref_;
73 std::unique_ptr<PersistedData> metadata_; 72 std::unique_ptr<PersistedData> metadata_;
74 73
75 std::unique_ptr<UpdateChecker> update_checker_; 74 std::unique_ptr<UpdateChecker> update_checker_;
76 75
77 std::unique_ptr<InterceptorFactory> interceptor_factory_; 76 std::unique_ptr<InterceptorFactory> interceptor_factory_;
78 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. 77 URLRequestPostInterceptor* post_interceptor_ =
78 nullptr; // Owned by the factory.
79 79
80 int error_; 80 int error_ = 0;
81 UpdateResponse::Results results_; 81 int retry_after_sec_ = 0;
82
83 std::unique_ptr<UpdateContext> update_context_;
82 84
83 private: 85 private:
86 std::unique_ptr<UpdateContext> MakeFakeUpdateContext() const;
87
84 base::MessageLoopForIO loop_; 88 base::MessageLoopForIO loop_;
85 base::test::ScopedTaskScheduler scoped_task_scheduler_; 89 base::test::ScopedTaskScheduler scoped_task_scheduler_;
86 base::Closure quit_closure_; 90 base::Closure quit_closure_;
87 91
88 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); 92 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
89 }; 93 };
90 94
91 UpdateCheckerTest::UpdateCheckerTest() 95 UpdateCheckerTest::UpdateCheckerTest() : scoped_task_scheduler_(&loop_) {}
92 : post_interceptor_(NULL), error_(0), scoped_task_scheduler_(&loop_) {}
93 96
94 UpdateCheckerTest::~UpdateCheckerTest() { 97 UpdateCheckerTest::~UpdateCheckerTest() {
95 } 98 }
96 99
97 void UpdateCheckerTest::SetUp() { 100 void UpdateCheckerTest::SetUp() {
98 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(), 101 config_ = base::MakeShared<TestConfigurator>(
99 base::ThreadTaskRunnerHandle::Get()); 102 base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get());
100 pref_.reset(new TestingPrefServiceSimple()); 103 pref_ = base::MakeUnique<TestingPrefServiceSimple>();
101 PersistedData::RegisterPrefs(pref_->registry()); 104 PersistedData::RegisterPrefs(pref_->registry());
102 metadata_.reset(new PersistedData(pref_.get())); 105 metadata_ = base::MakeUnique<PersistedData>(pref_.get());
103 interceptor_factory_.reset( 106 interceptor_factory_ =
104 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get())); 107 base::MakeUnique<InterceptorFactory>(base::ThreadTaskRunnerHandle::Get());
105 post_interceptor_ = interceptor_factory_->CreateInterceptor(); 108 post_interceptor_ = interceptor_factory_->CreateInterceptor();
106 EXPECT_TRUE(post_interceptor_); 109 EXPECT_TRUE(post_interceptor_);
107 110
108 update_checker_.reset(); 111 update_checker_ = nullptr;
109 112
110 error_ = 0; 113 error_ = 0;
111 results_ = UpdateResponse::Results(); 114 retry_after_sec_ = 0;
115 update_context_ = MakeFakeUpdateContext();
112 } 116 }
113 117
114 void UpdateCheckerTest::TearDown() { 118 void UpdateCheckerTest::TearDown() {
115 update_checker_.reset(); 119 update_checker_ = nullptr;
116 120
117 post_interceptor_ = NULL; 121 post_interceptor_ = nullptr;
118 interceptor_factory_.reset(); 122 interceptor_factory_ = nullptr;
119 123
120 config_ = nullptr; 124 config_ = nullptr;
121 125
122 // The PostInterceptor requires the message loop to run to destruct correctly. 126 // The PostInterceptor requires the message loop to run to destruct correctly.
123 // TODO(sorin): This is fragile and should be fixed. 127 // TODO(sorin): This is fragile and should be fixed.
124 RunThreadsUntilIdle(); 128 RunThreadsUntilIdle();
125 } 129 }
126 130
127 void UpdateCheckerTest::RunThreads() { 131 void UpdateCheckerTest::RunThreads() {
128 base::RunLoop runloop; 132 base::RunLoop runloop;
129 quit_closure_ = runloop.QuitClosure(); 133 quit_closure_ = runloop.QuitClosure();
130 runloop.Run(); 134 runloop.Run();
131 135
132 // Since some tests need to drain currently enqueued tasks such as network 136 // Since some tests need to drain currently enqueued tasks such as network
133 // intercepts on the IO thread, run the threads until they are 137 // intercepts on the IO thread, run the threads until they are
134 // idle. The component updater service won't loop again until the loop count 138 // idle. The component updater service won't loop again until the loop count
135 // is set and the service is started. 139 // is set and the service is started.
136 RunThreadsUntilIdle(); 140 RunThreadsUntilIdle();
137 } 141 }
138 142
139 void UpdateCheckerTest::RunThreadsUntilIdle() { 143 void UpdateCheckerTest::RunThreadsUntilIdle() {
140 base::RunLoop().RunUntilIdle(); 144 base::RunLoop().RunUntilIdle();
141 } 145 }
142 146
143 void UpdateCheckerTest::Quit() { 147 void UpdateCheckerTest::Quit() {
144 if (!quit_closure_.is_null()) 148 if (!quit_closure_.is_null())
145 quit_closure_.Run(); 149 quit_closure_.Run();
146 } 150 }
147 151
148 void UpdateCheckerTest::UpdateCheckComplete( 152 void UpdateCheckerTest::UpdateCheckComplete(int error, int retry_after_sec) {
149 int error,
150 const UpdateResponse::Results& results,
151 int retry_after_sec) {
152 error_ = error; 153 error_ = error;
153 results_ = results; 154 retry_after_sec_ = retry_after_sec;
154 Quit(); 155 Quit();
155 } 156 }
156 157
157 std::unique_ptr<CrxUpdateItem> UpdateCheckerTest::BuildCrxUpdateItem() { 158 std::unique_ptr<UpdateContext> UpdateCheckerTest::MakeFakeUpdateContext()
159 const {
160 return base::MakeUnique<UpdateContext>(
161 config_, false, std::vector<std::string>(),
162 UpdateClient::CrxDataCallback(), UpdateEngine::NotifyObserversCallback(),
163 UpdateEngine::Callback(), nullptr);
164 }
165
166 std::unique_ptr<Component> UpdateCheckerTest::MakeComponent() const {
158 CrxComponent crx_component; 167 CrxComponent crx_component;
159 crx_component.name = "test_jebg"; 168 crx_component.name = "test_jebg";
160 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); 169 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
161 crx_component.installer = NULL; 170 crx_component.installer = NULL;
162 crx_component.version = base::Version("0.9"); 171 crx_component.version = base::Version("0.9");
163 crx_component.fingerprint = "fp1"; 172 crx_component.fingerprint = "fp1";
164 173
165 std::unique_ptr<CrxUpdateItem> crx_update_item = 174 auto component = base::MakeUnique<Component>(*update_context_, kUpdateItemId);
166 base::MakeUnique<CrxUpdateItem>(); 175 component->state_ = base::MakeUnique<Component::StateNew>(component.get());
167 crx_update_item->state = CrxUpdateItem::State::kNew; 176 component->crx_component_ = crx_component;
168 crx_update_item->id = kUpdateItemId;
169 crx_update_item->component = crx_component;
170 177
171 return crx_update_item; 178 return component;
172 } 179 }
173 180
174 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) { 181 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) {
175 EXPECT_TRUE(post_interceptor_->ExpectRequest( 182 EXPECT_TRUE(post_interceptor_->ExpectRequest(
176 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 183 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
177 184
178 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 185 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
179 186
180 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 187 IdToComponentPtrMap components;
181 item->component.installer_attributes["ap"] = "some_ap"; 188 components[kUpdateItemId] = MakeComponent();
182 IdToCrxUpdateItemMap items_to_check; 189
183 items_to_check[kUpdateItemId] = std::move(item); 190 auto& component = components[kUpdateItemId];
191 component->crx_component_.installer_attributes["ap"] = "some_ap";
184 192
185 update_checker_->CheckForUpdates( 193 update_checker_->CheckForUpdates(
186 items_to_check, "extra=\"params\"", true, 194 std::vector<std::string>{kUpdateItemId}, components, "extra=\"params\"",
195 true,
187 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 196 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
188 base::Unretained(this))); 197 base::Unretained(this)));
189
190 RunThreads(); 198 RunThreads();
191 199
192 EXPECT_EQ(1, post_interceptor_->GetHitCount()) 200 EXPECT_EQ(1, post_interceptor_->GetHitCount())
193 << post_interceptor_->GetRequestsAsString(); 201 << post_interceptor_->GetRequestsAsString();
194 ASSERT_EQ(1, post_interceptor_->GetCount()) 202 ASSERT_EQ(1, post_interceptor_->GetCount())
195 << post_interceptor_->GetRequestsAsString(); 203 << post_interceptor_->GetRequestsAsString();
196 204
197 // Sanity check the request. 205 // Sanity check the request.
198 const auto request = post_interceptor_->GetRequests()[0]; 206 const auto request = post_interceptor_->GetRequests()[0];
199 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find( 207 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
(...skipping 11 matching lines...) Expand all
211 219
212 EXPECT_NE(string::npos, request.find("<hw physmemory=")); 220 EXPECT_NE(string::npos, request.find("<hw physmemory="));
213 221
214 // Tests that the progid is injected correctly from the configurator. 222 // Tests that the progid is injected correctly from the configurator.
215 EXPECT_NE( 223 EXPECT_NE(
216 string::npos, 224 string::npos,
217 request.find(" version=\"fake_prodid-30.0\" prodversion=\"30.0\" ")); 225 request.find(" version=\"fake_prodid-30.0\" prodversion=\"30.0\" "));
218 226
219 // Sanity check the arguments of the callback after parsing. 227 // Sanity check the arguments of the callback after parsing.
220 EXPECT_EQ(0, error_); 228 EXPECT_EQ(0, error_);
221 EXPECT_EQ(1ul, results_.list.size()); 229
222 EXPECT_STREQ(kUpdateItemId, results_.list[0].extension_id.c_str()); 230 EXPECT_EQ(base::Version("1.0"), component->next_version_);
223 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str()); 231 EXPECT_EQ(1u, component->crx_urls_.size());
232 EXPECT_EQ(
233 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"),
234 component->crx_urls_.front());
224 235
225 #if (OS_WIN) 236 #if (OS_WIN)
226 EXPECT_NE(string::npos, request.find(" domainjoined=")); 237 EXPECT_NE(string::npos, request.find(" domainjoined="));
227 #if defined(GOOGLE_CHROME_BUILD) 238 #if defined(GOOGLE_CHROME_BUILD)
228 // Check the Omaha updater state data in the request. 239 // Check the Omaha updater state data in the request.
229 EXPECT_NE(string::npos, request.find("<updater ")); 240 EXPECT_NE(string::npos, request.find("<updater "));
230 EXPECT_NE(string::npos, request.find(" name=\"Omaha\" ")); 241 EXPECT_NE(string::npos, request.find(" name=\"Omaha\" "));
231 #endif // GOOGLE_CHROME_BUILD 242 #endif // GOOGLE_CHROME_BUILD
232 #endif // OS_WINDOWS 243 #endif // OS_WINDOWS
233 } 244 }
234 245
235 // Tests that an invalid "ap" is not serialized. 246 // Tests that an invalid "ap" is not serialized.
236 TEST_F(UpdateCheckerTest, UpdateCheckInvalidAp) { 247 TEST_F(UpdateCheckerTest, UpdateCheckInvalidAp) {
237 EXPECT_TRUE(post_interceptor_->ExpectRequest( 248 EXPECT_TRUE(post_interceptor_->ExpectRequest(
238 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 249 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
239 250
240 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 251 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
241 252
242 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 253 IdToComponentPtrMap components;
254 components[kUpdateItemId] = MakeComponent();
255
243 // Make "ap" too long. 256 // Make "ap" too long.
244 item->component.installer_attributes["ap"] = std::string(257, 'a'); 257 auto& component = components[kUpdateItemId];
245 IdToCrxUpdateItemMap items_to_check; 258 component->crx_component_.installer_attributes["ap"] = std::string(257, 'a');
246 items_to_check[kUpdateItemId] = std::move(item);
247 259
248 update_checker_->CheckForUpdates( 260 update_checker_->CheckForUpdates(
249 items_to_check, "", true, 261 std::vector<std::string>{kUpdateItemId}, components, "", true,
250 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 262 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
251 base::Unretained(this))); 263 base::Unretained(this)));
252 264
253 RunThreads(); 265 RunThreads();
254 266
255 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find( 267 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
256 std::string("app appid=\"") + kUpdateItemId + 268 std::string("app appid=\"") + kUpdateItemId +
257 "\" version=\"0.9\" " 269 "\" version=\"0.9\" "
258 "brand=\"TEST\"><updatecheck/><ping rd=\"-2\" ")); 270 "brand=\"TEST\"><updatecheck/><ping rd=\"-2\" "));
259 EXPECT_NE(string::npos, 271 EXPECT_NE(string::npos,
260 post_interceptor_->GetRequests()[0].find( 272 post_interceptor_->GetRequests()[0].find(
261 "<packages><package fp=\"fp1\"/></packages></app>")); 273 "<packages><package fp=\"fp1\"/></packages></app>"));
262 } 274 }
263 275
264 TEST_F(UpdateCheckerTest, UpdateCheckSuccessNoBrand) { 276 TEST_F(UpdateCheckerTest, UpdateCheckSuccessNoBrand) {
265 EXPECT_TRUE(post_interceptor_->ExpectRequest( 277 EXPECT_TRUE(post_interceptor_->ExpectRequest(
266 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 278 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
267 279
268 config_->SetBrand("TOOLONG"); // Sets an invalid brand code. 280 config_->SetBrand("TOOLONG"); // Sets an invalid brand code.
269 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 281 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
270 282
271 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 283 IdToComponentPtrMap components;
272 IdToCrxUpdateItemMap items_to_check; 284 components[kUpdateItemId] = MakeComponent();
273 items_to_check[kUpdateItemId] = std::move(item);
274 285
275 update_checker_->CheckForUpdates( 286 update_checker_->CheckForUpdates(
276 items_to_check, "", true, 287 std::vector<std::string>{kUpdateItemId}, components, "", true,
277 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 288 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
278 base::Unretained(this))); 289 base::Unretained(this)));
279 290
280 RunThreads(); 291 RunThreads();
281 292
282 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find( 293 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
283 std::string("<app appid=\"") + kUpdateItemId + 294 std::string("<app appid=\"") + kUpdateItemId +
284 "\" version=\"0.9\">" 295 "\" version=\"0.9\">"
285 "<updatecheck/><ping rd=\"-2\" ")); 296 "<updatecheck/><ping rd=\"-2\" "));
286 EXPECT_NE(string::npos, 297 EXPECT_NE(string::npos,
287 post_interceptor_->GetRequests()[0].find( 298 post_interceptor_->GetRequests()[0].find(
288 "<packages><package fp=\"fp1\"/></packages></app>")); 299 "<packages><package fp=\"fp1\"/></packages></app>"));
289 } 300 }
290 301
291 // Simulates a 403 server response error. 302 // Simulates a 403 server response error.
292 TEST_F(UpdateCheckerTest, UpdateCheckError) { 303 TEST_F(UpdateCheckerTest, UpdateCheckError) {
293 EXPECT_TRUE( 304 EXPECT_TRUE(
294 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403)); 305 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403));
295 306
296 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 307 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
297 308
298 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 309 IdToComponentPtrMap components;
299 IdToCrxUpdateItemMap items_to_check; 310 components[kUpdateItemId] = MakeComponent();
300 items_to_check[kUpdateItemId] = std::move(item); 311
312 auto& component = components[kUpdateItemId];
301 313
302 update_checker_->CheckForUpdates( 314 update_checker_->CheckForUpdates(
303 items_to_check, "", true, 315 std::vector<std::string>{kUpdateItemId}, components, "", true,
304 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 316 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
305 base::Unretained(this))); 317 base::Unretained(this)));
306 RunThreads(); 318 RunThreads();
307 319
308 EXPECT_EQ(1, post_interceptor_->GetHitCount()) 320 EXPECT_EQ(1, post_interceptor_->GetHitCount())
309 << post_interceptor_->GetRequestsAsString(); 321 << post_interceptor_->GetRequestsAsString();
310 EXPECT_EQ(1, post_interceptor_->GetCount()) 322 EXPECT_EQ(1, post_interceptor_->GetCount())
311 << post_interceptor_->GetRequestsAsString(); 323 << post_interceptor_->GetRequestsAsString();
312 324
313 EXPECT_EQ(403, error_); 325 EXPECT_EQ(403, error_);
314 EXPECT_EQ(0ul, results_.list.size()); 326 EXPECT_FALSE(component->next_version_.IsValid());
315 } 327 }
316 328
317 TEST_F(UpdateCheckerTest, UpdateCheckDownloadPreference) { 329 TEST_F(UpdateCheckerTest, UpdateCheckDownloadPreference) {
318 EXPECT_TRUE(post_interceptor_->ExpectRequest( 330 EXPECT_TRUE(post_interceptor_->ExpectRequest(
319 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 331 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
320 332
321 config_->SetDownloadPreference(string("cacheable")); 333 config_->SetDownloadPreference(string("cacheable"));
322 334
323 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 335 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
324 336
325 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 337 IdToComponentPtrMap components;
326 IdToCrxUpdateItemMap items_to_check; 338 components[kUpdateItemId] = MakeComponent();
327 items_to_check[kUpdateItemId] = std::move(item);
328 339
329 update_checker_->CheckForUpdates( 340 update_checker_->CheckForUpdates(
330 items_to_check, "extra=\"params\"", true, 341 std::vector<std::string>{kUpdateItemId}, components, "extra=\"params\"",
342 true,
331 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 343 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
332 base::Unretained(this))); 344 base::Unretained(this)));
333 345
334 RunThreads(); 346 RunThreads();
335 347
336 // The request must contain dlpref="cacheable". 348 // The request must contain dlpref="cacheable".
337 EXPECT_NE(string::npos, 349 EXPECT_NE(string::npos,
338 post_interceptor_->GetRequests()[0].find(" dlpref=\"cacheable\"")); 350 post_interceptor_->GetRequests()[0].find(" dlpref=\"cacheable\""));
339 } 351 }
340 352
341 // This test is checking that an update check signed with CUP fails, since there 353 // This test is checking that an update check signed with CUP fails, since there
342 // is currently no entity that can respond with a valid signed response. 354 // is currently no entity that can respond with a valid signed response.
343 // A proper CUP test requires network mocks, which are not available now. 355 // A proper CUP test requires network mocks, which are not available now.
344 TEST_F(UpdateCheckerTest, UpdateCheckCupError) { 356 TEST_F(UpdateCheckerTest, UpdateCheckCupError) {
345 EXPECT_TRUE(post_interceptor_->ExpectRequest( 357 EXPECT_TRUE(post_interceptor_->ExpectRequest(
346 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 358 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
347 359
348 config_->SetEnabledCupSigning(true); 360 config_->SetEnabledCupSigning(true);
349 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 361 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
350 362
351 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 363 IdToComponentPtrMap components;
352 IdToCrxUpdateItemMap items_to_check; 364 components[kUpdateItemId] = MakeComponent();
353 items_to_check[kUpdateItemId] = std::move(item); 365
366 const auto& component = components[kUpdateItemId];
354 367
355 update_checker_->CheckForUpdates( 368 update_checker_->CheckForUpdates(
356 items_to_check, "", true, 369 std::vector<std::string>{kUpdateItemId}, components, "", true,
357 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 370 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
358 base::Unretained(this))); 371 base::Unretained(this)));
359 372
360 RunThreads(); 373 RunThreads();
361 374
362 EXPECT_EQ(1, post_interceptor_->GetHitCount()) 375 EXPECT_EQ(1, post_interceptor_->GetHitCount())
363 << post_interceptor_->GetRequestsAsString(); 376 << post_interceptor_->GetRequestsAsString();
364 ASSERT_EQ(1, post_interceptor_->GetCount()) 377 ASSERT_EQ(1, post_interceptor_->GetCount())
365 << post_interceptor_->GetRequestsAsString(); 378 << post_interceptor_->GetRequestsAsString();
366 379
367 // Sanity check the request. 380 // Sanity check the request.
368 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find( 381 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
369 std::string("<app appid=\"") + kUpdateItemId + 382 std::string("<app appid=\"") + kUpdateItemId +
370 "\" version=\"0.9\" " 383 "\" version=\"0.9\" "
371 "brand=\"TEST\"><updatecheck/><ping rd=\"-2\" ")); 384 "brand=\"TEST\"><updatecheck/><ping rd=\"-2\" "));
372 EXPECT_NE(string::npos, 385 EXPECT_NE(string::npos,
373 post_interceptor_->GetRequests()[0].find( 386 post_interceptor_->GetRequests()[0].find(
374 "<packages><package fp=\"fp1\"/></packages></app>")); 387 "<packages><package fp=\"fp1\"/></packages></app>"));
375 388
376 // Expect an error since the response is not trusted. 389 // Expect an error since the response is not trusted.
377 EXPECT_EQ(-10000, error_); 390 EXPECT_EQ(-10000, error_);
378 EXPECT_EQ(0ul, results_.list.size()); 391 EXPECT_FALSE(component->next_version_.IsValid());
379 } 392 }
380 393
381 // Tests that the UpdateCheckers will not make an update check for a 394 // Tests that the UpdateCheckers will not make an update check for a
382 // component that requires encryption when the update check URL is unsecure. 395 // component that requires encryption when the update check URL is unsecure.
383 TEST_F(UpdateCheckerTest, UpdateCheckRequiresEncryptionError) { 396 TEST_F(UpdateCheckerTest, UpdateCheckRequiresEncryptionError) {
384 config_->SetUpdateCheckUrl(GURL("http:\\foo\bar")); 397 config_->SetUpdateCheckUrl(GURL("http:\\foo\bar"));
385 398
386 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 399 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
387 400
388 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 401 IdToComponentPtrMap components;
389 item->component.requires_network_encryption = true; 402 components[kUpdateItemId] = MakeComponent();
390 IdToCrxUpdateItemMap items_to_check; 403
391 items_to_check[kUpdateItemId] = std::move(item); 404 auto& component = components[kUpdateItemId];
405 component->crx_component_.requires_network_encryption = true;
392 406
393 update_checker_->CheckForUpdates( 407 update_checker_->CheckForUpdates(
394 items_to_check, "", true, 408 std::vector<std::string>{kUpdateItemId}, components, "", true,
395 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 409 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
396 base::Unretained(this))); 410 base::Unretained(this)));
397 RunThreads(); 411 RunThreads();
398 412
399 EXPECT_EQ(-1, error_); 413 EXPECT_EQ(-1, error_);
400 EXPECT_EQ(0u, results_.list.size()); 414 EXPECT_FALSE(component->next_version_.IsValid());
401 } 415 }
402 416
403 // Tests that the PersistedData will get correctly update and reserialize 417 // Tests that the PersistedData will get correctly update and reserialize
404 // the elapsed_days value. 418 // the elapsed_days value.
405 TEST_F(UpdateCheckerTest, UpdateCheckDateLastRollCall) { 419 TEST_F(UpdateCheckerTest, UpdateCheckDateLastRollCall) {
406 EXPECT_TRUE(post_interceptor_->ExpectRequest( 420 EXPECT_TRUE(post_interceptor_->ExpectRequest(
407 new PartialMatch("updatecheck"), test_file("updatecheck_reply_4.xml"))); 421 new PartialMatch("updatecheck"), test_file("updatecheck_reply_4.xml")));
408 EXPECT_TRUE(post_interceptor_->ExpectRequest( 422 EXPECT_TRUE(post_interceptor_->ExpectRequest(
409 new PartialMatch("updatecheck"), test_file("updatecheck_reply_4.xml"))); 423 new PartialMatch("updatecheck"), test_file("updatecheck_reply_4.xml")));
410 424
411 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 425 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
412 426
413 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 427 IdToComponentPtrMap components;
414 IdToCrxUpdateItemMap items_to_check; 428 components[kUpdateItemId] = MakeComponent();
415 items_to_check[kUpdateItemId] = std::move(item);
416 429
417 // Do two update-checks. 430 // Do two update-checks.
418 update_checker_->CheckForUpdates( 431 update_checker_->CheckForUpdates(
419 items_to_check, "extra=\"params\"", true, 432 std::vector<std::string>{kUpdateItemId}, components, "extra=\"params\"",
433 true,
420 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 434 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
421 base::Unretained(this))); 435 base::Unretained(this)));
422 RunThreads(); 436 RunThreads();
437
423 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 438 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
424 update_checker_->CheckForUpdates( 439 update_checker_->CheckForUpdates(
425 items_to_check, "extra=\"params\"", true, 440 std::vector<std::string>{kUpdateItemId}, components, "extra=\"params\"",
441 true,
426 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 442 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
427 base::Unretained(this))); 443 base::Unretained(this)));
428 RunThreads(); 444 RunThreads();
429 445
430 EXPECT_EQ(2, post_interceptor_->GetHitCount()) 446 EXPECT_EQ(2, post_interceptor_->GetHitCount())
431 << post_interceptor_->GetRequestsAsString(); 447 << post_interceptor_->GetRequestsAsString();
432 ASSERT_EQ(2, post_interceptor_->GetCount()) 448 ASSERT_EQ(2, post_interceptor_->GetCount())
433 << post_interceptor_->GetRequestsAsString(); 449 << post_interceptor_->GetRequestsAsString();
434 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find( 450 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
435 "<ping rd=\"-2\" ping_freshness=")); 451 "<ping rd=\"-2\" ping_freshness="));
436 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find( 452 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find(
437 "<ping rd=\"3383\" ping_freshness=")); 453 "<ping rd=\"3383\" ping_freshness="));
438 } 454 }
439 455
440 TEST_F(UpdateCheckerTest, UpdateCheckUpdateDisabled) { 456 TEST_F(UpdateCheckerTest, UpdateCheckUpdateDisabled) {
441 EXPECT_TRUE(post_interceptor_->ExpectRequest( 457 EXPECT_TRUE(post_interceptor_->ExpectRequest(
442 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 458 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
443 459
444 config_->SetBrand(""); 460 config_->SetBrand("");
445 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 461 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
446 462
447 std::unique_ptr<CrxUpdateItem> item = BuildCrxUpdateItem(); 463 IdToComponentPtrMap components;
448 CrxUpdateItem* item_ptr = item.get(); 464 components[kUpdateItemId] = MakeComponent();
465
466 auto& component = components[kUpdateItemId];
449 467
450 // Tests the scenario where: 468 // Tests the scenario where:
451 // * the component does not support group policies. 469 // * the component does not support group policies.
452 // * the component updates are disabled. 470 // * the component updates are disabled.
453 // Expects the group policy to be ignored and the update check to not 471 // Expects the group policy to be ignored and the update check to not
454 // include the "updatedisabled" attribute. 472 // include the "updatedisabled" attribute.
455 EXPECT_FALSE( 473 EXPECT_FALSE(
456 item_ptr->component.supports_group_policy_enable_component_updates); 474 component->crx_component_.supports_group_policy_enable_component_updates);
457 IdToCrxUpdateItemMap items_to_check; 475
458 items_to_check[kUpdateItemId] = std::move(item);
459 update_checker_->CheckForUpdates( 476 update_checker_->CheckForUpdates(
460 items_to_check, "", false, 477 std::vector<std::string>{kUpdateItemId}, components, "", false,
461 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 478 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
462 base::Unretained(this))); 479 base::Unretained(this)));
463 RunThreads(); 480 RunThreads();
464 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find( 481 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
465 std::string("<app appid=\"") + kUpdateItemId + 482 std::string("<app appid=\"") + kUpdateItemId +
466 "\" version=\"0.9\">" 483 "\" version=\"0.9\">"
467 "<updatecheck/>")); 484 "<updatecheck/>"));
468 485
469 // Tests the scenario where: 486 // Tests the scenario where:
470 // * the component supports group policies. 487 // * the component supports group policies.
471 // * the component updates are disabled. 488 // * the component updates are disabled.
472 // Expects the update check to include the "updatedisabled" attribute. 489 // Expects the update check to include the "updatedisabled" attribute.
473 item_ptr->component.supports_group_policy_enable_component_updates = true; 490 component->crx_component_.supports_group_policy_enable_component_updates =
491 true;
474 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 492 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
475 update_checker_->CheckForUpdates( 493 update_checker_->CheckForUpdates(
476 items_to_check, "", false, 494 std::vector<std::string>{kUpdateItemId}, components, "", false,
477 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 495 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
478 base::Unretained(this))); 496 base::Unretained(this)));
479 RunThreads(); 497 RunThreads();
480 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find( 498 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find(
481 std::string("<app appid=\"") + kUpdateItemId + 499 std::string("<app appid=\"") + kUpdateItemId +
482 "\" version=\"0.9\">" 500 "\" version=\"0.9\">"
483 "<updatecheck updatedisabled=\"true\"/>")); 501 "<updatecheck updatedisabled=\"true\"/>"));
484 502
485 // Tests the scenario where: 503 // Tests the scenario where:
486 // * the component does not support group policies. 504 // * the component does not support group policies.
487 // * the component updates are enabled. 505 // * the component updates are enabled.
488 // Expects the update check to not include the "updatedisabled" attribute. 506 // Expects the update check to not include the "updatedisabled" attribute.
489 item_ptr->component.supports_group_policy_enable_component_updates = false; 507 component->crx_component_.supports_group_policy_enable_component_updates =
508 false;
490 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 509 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
491 update_checker_->CheckForUpdates( 510 update_checker_->CheckForUpdates(
492 items_to_check, "", true, 511 std::vector<std::string>{kUpdateItemId}, components, "", true,
493 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 512 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
494 base::Unretained(this))); 513 base::Unretained(this)));
495 RunThreads(); 514 RunThreads();
496 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[2].find( 515 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[2].find(
497 std::string("<app appid=\"") + kUpdateItemId + 516 std::string("<app appid=\"") + kUpdateItemId +
498 "\" version=\"0.9\">" 517 "\" version=\"0.9\">"
499 "<updatecheck/>")); 518 "<updatecheck/>"));
500 519
501 // Tests the scenario where: 520 // Tests the scenario where:
502 // * the component supports group policies. 521 // * the component supports group policies.
503 // * the component updates are enabled. 522 // * the component updates are enabled.
504 // Expects the update check to not include the "updatedisabled" attribute. 523 // Expects the update check to not include the "updatedisabled" attribute.
505 item_ptr->component.supports_group_policy_enable_component_updates = true; 524 component->crx_component_.supports_group_policy_enable_component_updates =
525 true;
506 update_checker_ = UpdateChecker::Create(config_, metadata_.get()); 526 update_checker_ = UpdateChecker::Create(config_, metadata_.get());
507 update_checker_->CheckForUpdates( 527 update_checker_->CheckForUpdates(
508 items_to_check, "", true, 528 std::vector<std::string>{kUpdateItemId}, components, "", true,
509 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 529 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
510 base::Unretained(this))); 530 base::Unretained(this)));
511 RunThreads(); 531 RunThreads();
512 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[3].find( 532 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[3].find(
513 std::string("<app appid=\"") + kUpdateItemId + 533 std::string("<app appid=\"") + kUpdateItemId +
514 "\" version=\"0.9\">" 534 "\" version=\"0.9\">"
515 "<updatecheck/>")); 535 "<updatecheck/>"));
516 } 536 }
517 537
518 } // namespace update_client 538 } // namespace update_client
OLDNEW
« no previous file with comments | « components/update_client/update_checker.cc ('k') | components/update_client/update_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698