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

Side by Side Diff: chrome/browser/component_updater/test/component_updater_service_unittest.cc

Issue 15908002: Differential updates for components. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: really removing the lzma files this time. Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/component_updater/component_updater_service.h"
6
7 #include <list> 5 #include <list>
8 #include <utility> 6 #include <utility>
9 7
10 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
11 #include "base/file_util.h" 9 #include "base/file_util.h"
12 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
13 #include "base/memory/scoped_vector.h" 11 #include "base/memory/scoped_vector.h"
14 #include "base/message_loop.h" 12 #include "base/message_loop.h"
15 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/stringprintf.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/component_updater/component_updater_service.h"
18 #include "chrome/browser/component_updater/test/component_patcher_mock.h"
17 #include "chrome/common/chrome_notification_types.h" 19 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/chrome_paths.h" 20 #include "chrome/common/chrome_paths.h"
19 #include "content/public/browser/notification_observer.h" 21 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_service.h" 22 #include "content/public/browser/notification_service.h"
21 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
22 #include "content/public/test/test_notification_tracker.h" 24 #include "content/public/test/test_notification_tracker.h"
23 #include "content/test/net/url_request_prepackaged_interceptor.h" 25 #include "content/test/net/url_request_prepackaged_interceptor.h"
24 #include "googleurl/src/gurl.h" 26 #include "googleurl/src/gurl.h"
25 #include "libxml/globals.h" 27 #include "libxml/globals.h"
28 #include "net/base/upload_bytes_element_reader.h"
26 #include "net/url_request/url_fetcher.h" 29 #include "net/url_request/url_fetcher.h"
30 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_filter.h"
32 #include "net/url_request/url_request_simple_job.h"
27 #include "net/url_request/url_request_test_util.h" 33 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
29 35
30 using content::BrowserThread; 36 using content::BrowserThread;
31 using content::TestNotificationTracker; 37 using content::TestNotificationTracker;
32 38
33 namespace { 39 namespace {
34 // Overrides some of the component updater behaviors so it is easier to test 40
35 // and loops faster. In actual usage it takes hours do to a full cycle. 41 // component 1 has extension id "jebgalgnebhfojomionfpkfelancnnkf", and
42 // the RSA public key the following hash:
43 const uint8 jebg_hash[] = {0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec,
44 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5,
45 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4,
46 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40};
47 // component 2 has extension id "abagagagagagagagagagagagagagagag", and
48 // the RSA public key the following hash:
49 const uint8 abag_hash[] = {0x01, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
50 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
51 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
52 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x01};
53 // component 3 has extension id "ihfokbkgjpifnbbojhneepfflplebdkc", and
54 // the RSA public key the following hash:
55 const uint8 ihfo_hash[] = {0x87, 0x5e, 0xa1, 0xa6, 0x9f, 0x85, 0xd1, 0x1e,
56 0x97, 0xd4, 0x4f, 0x55, 0xbf, 0xb4, 0x13, 0xa2,
57 0xe7, 0xc5, 0xc8, 0xf5, 0x60, 0x19, 0x78, 0x1b,
58 0x6d, 0xe9, 0x4c, 0xeb, 0x96, 0x05, 0x42, 0x17};
59
36 class TestConfigurator : public ComponentUpdateService::Configurator { 60 class TestConfigurator : public ComponentUpdateService::Configurator {
37 public: 61 public:
38 TestConfigurator() 62 TestConfigurator();
39 : times_(1), recheck_time_(0), ondemand_time_(0), cus_(NULL) {
40 }
41 63
42 virtual int InitialDelay() OVERRIDE { return 0; } 64 virtual int InitialDelay() OVERRIDE;
43 65
44 typedef std::pair<CrxComponent*, int> CheckAtLoopCount; 66 typedef std::pair<CrxComponent*, int> CheckAtLoopCount;
45 67
46 virtual int NextCheckDelay() OVERRIDE { 68 virtual int NextCheckDelay() OVERRIDE;
47 // This is called when a new full cycle of checking for updates is going
48 // to happen. In test we normally only test one cycle so it is a good
49 // time to break from the test messageloop Run() method so the test can
50 // finish.
51 if (--times_ <= 0) {
52 base::MessageLoop::current()->Quit();
53 return 0;
54 69
55 } 70 virtual int StepDelay() OVERRIDE;
56 71
57 // Look for checks to issue in the middle of the loop. 72 virtual int MinimumReCheckWait() OVERRIDE;
58 for (std::list<CheckAtLoopCount>::iterator
59 i = components_to_check_.begin();
60 i != components_to_check_.end(); ) {
61 if (i->second == times_) {
62 cus_->CheckForUpdateSoon(*i->first);
63 i = components_to_check_.erase(i);
64 } else {
65 ++i;
66 }
67 }
68 return 1;
69 }
70 73
71 virtual int StepDelay() OVERRIDE { 74 virtual int OnDemandDelay() OVERRIDE;
72 return 0;
73 }
74 75
75 virtual int MinimumReCheckWait() OVERRIDE { 76 virtual GURL UpdateUrl(CrxComponent::UrlSource source) OVERRIDE;
76 return recheck_time_;
77 }
78 77
79 virtual int OnDemandDelay() OVERRIDE { 78 virtual GURL PingUrl() OVERRIDE;
80 return ondemand_time_;
81 }
82 79
83 virtual GURL UpdateUrl(CrxComponent::UrlSource source) OVERRIDE { 80 virtual const char* ExtraRequestParams() OVERRIDE;
84 switch (source) {
85 case CrxComponent::BANDAID:
86 return GURL("http://localhost/upd");
87 case CrxComponent::CWS_PUBLIC:
88 return GURL("http://localhost/cws");
89 default:
90 return GURL("http://wronghost/bad");
91 };
92 }
93 81
94 virtual const char* ExtraRequestParams() OVERRIDE { return "extra=foo"; } 82 virtual size_t UrlSizeLimit() OVERRIDE;
95 83
96 virtual size_t UrlSizeLimit() OVERRIDE { return 256; } 84 virtual net::URLRequestContextGetter* RequestContext() OVERRIDE;
97
98 virtual net::URLRequestContextGetter* RequestContext() OVERRIDE {
99 return new net::TestURLRequestContextGetter(
100 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
101 }
102 85
103 // Don't use the utility process to decode files. 86 // Don't use the utility process to decode files.
104 virtual bool InProcess() OVERRIDE { return true; } 87 virtual bool InProcess() OVERRIDE;
105 88
106 virtual void OnEvent(Events event, int extra) OVERRIDE { } 89 virtual void OnEvent(Events event, int extra) OVERRIDE;
107 90
108 // Set how many update checks are called, the default value is just once. 91 virtual ComponentPatcher* CreateComponentPatcher() OVERRIDE;
109 void SetLoopCount(int times) { times_ = times; }
110 92
111 void SetRecheckTime(int seconds) { 93 virtual bool PingsEnabled() const OVERRIDE;
112 recheck_time_ = seconds;
113 }
114 94
115 void SetOnDemandTime(int seconds) { 95 virtual bool DeltasEnabled() const OVERRIDE;
116 ondemand_time_ = seconds;
117 }
118 96
119 void AddComponentToCheck(CrxComponent* com, int at_loop_iter) { 97 void SetLoopCount(int times);
120 components_to_check_.push_back(std::make_pair(com, at_loop_iter));
121 }
122 98
123 void SetComponentUpdateService(ComponentUpdateService* cus) { 99 void SetRecheckTime(int seconds);
124 cus_ = cus; 100
125 } 101 void SetOnDemandTime(int seconds);
102
103 void AddComponentToCheck(CrxComponent* com, int at_loop_iter);
104
105 void SetComponentUpdateService(ComponentUpdateService* cus);
126 106
127 private: 107 private:
128 int times_; 108 int times_;
129 int recheck_time_; 109 int recheck_time_;
130 int ondemand_time_; 110 int ondemand_time_;
131 111
132 std::list<CheckAtLoopCount> components_to_check_; 112 std::list<CheckAtLoopCount> components_to_check_;
133 ComponentUpdateService* cus_; 113 ComponentUpdateService* cus_;
134 }; 114 };
135 115
136 class TestInstaller : public ComponentInstaller { 116 class TestInstaller : public ComponentInstaller {
137 public : 117 public :
138 explicit TestInstaller() 118 explicit TestInstaller();
139 : error_(0), install_count_(0) { 119
140 } 120 virtual void OnUpdateError(int error) OVERRIDE;
141
142 virtual void OnUpdateError(int error) OVERRIDE {
143 EXPECT_NE(0, error);
144 error_ = error;
145 }
146 121
147 virtual bool Install(const base::DictionaryValue& manifest, 122 virtual bool Install(const base::DictionaryValue& manifest,
148 const base::FilePath& unpack_path) OVERRIDE { 123 const base::FilePath& unpack_path) OVERRIDE;
149 ++install_count_; 124
150 return file_util::Delete(unpack_path, true); 125 virtual bool GetInstalledFile(const std::string& file,
151 } 126 base::FilePath* installed_file) OVERRIDE;
152 127
153 int error() const { return error_; } 128 int error() const;
154 129
155 int install_count() const { return install_count_; } 130 int install_count() const;
156 131
157 private: 132 protected:
158 int error_; 133 int error_;
159 int install_count_; 134 int install_count_;
160 }; 135 };
161 136
162 // component 1 has extension id "jebgalgnebhfojomionfpkfelancnnkf", and 137 class PersistentTestInstaller : public TestInstaller {
163 // the RSA public key the following hash: 138 public :
164 const uint8 jebg_hash[] = {0x94,0x16,0x0b,0x6d,0x41,0x75,0xe9,0xec,0x8e,0xd5, 139 explicit PersistentTestInstaller();
165 0xfa,0x54,0xb0,0xd2,0xdd,0xa5,0x6e,0x05,0x6b,0xe8, 140
166 0x73,0x47,0xf6,0xc4,0x11,0x9f,0xbc,0xb3,0x09,0xb3, 141 virtual ~PersistentTestInstaller();
167 0x5b,0x40}; 142
168 // component 2 has extension id "abagagagagagagagagagagagagagagag", and 143 virtual bool Install(const base::DictionaryValue& manifest,
169 // the RSA public key the following hash: 144 const base::FilePath& unpack_path) OVERRIDE;
170 const uint8 abag_hash[] = {0x01,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, 145
171 0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, 146 virtual bool GetInstalledFile(const std::string& file,
172 0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, 147 base::FilePath* installed_file) OVERRIDE;
173 0x06,0x01}; 148
174 149 private:
175 const char expected_crx_url[] = 150 base::FilePath install_directory_;
176 "http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"; 151 Version current_version_;
177 152 };
178 } // namespace 153
179
180 // Common fixture for all the component updater tests.
181 class ComponentUpdaterTest : public testing::Test { 154 class ComponentUpdaterTest : public testing::Test {
182 public: 155 public:
183 enum TestComponents { 156 enum TestComponents {
184 kTestComponent_abag, 157 kTestComponent_abag,
185 kTestComponent_jebg 158 kTestComponent_jebg,
159 kTestComponent_ihfo,
186 }; 160 };
187 161
188 ComponentUpdaterTest() : test_config_(NULL) { 162 ComponentUpdaterTest();
189 // The component updater instance under test. 163
190 test_config_ = new TestConfigurator; 164 virtual ~ComponentUpdaterTest();
191 component_updater_.reset(ComponentUpdateServiceFactory(test_config_)); 165
192 test_config_->SetComponentUpdateService(component_updater_.get()); 166 virtual void TearDown();
193 // The test directory is chrome/test/data/components. 167
194 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); 168 ComponentUpdateService* component_updater();
195 test_data_dir_ = test_data_dir_.AppendASCII("components");
196
197 // Subscribe to all component updater notifications.
198 const int notifications[] = {
199 chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED,
200 chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING,
201 chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND,
202 chrome::NOTIFICATION_COMPONENT_UPDATE_READY
203 };
204
205 for (int ix = 0; ix != arraysize(notifications); ++ix) {
206 notification_tracker_.ListenFor(
207 notifications[ix], content::NotificationService::AllSources());
208 }
209 net::URLFetcher::SetEnableInterceptionForTests(true);
210 }
211
212 virtual ~ComponentUpdaterTest() {
213 net::URLFetcher::SetEnableInterceptionForTests(false);
214 }
215
216 virtual void TearDown() {
217 xmlCleanupGlobals();
218 }
219
220 ComponentUpdateService* component_updater() {
221 return component_updater_.get();
222 }
223 169
224 // Makes the full path to a component updater test file. 170 // Makes the full path to a component updater test file.
225 const base::FilePath test_file(const char* file) { 171 const base::FilePath test_file(const char* file);
226 return test_data_dir_.AppendASCII(file); 172
227 } 173 TestNotificationTracker& notification_tracker();
228 174
229 TestNotificationTracker& notification_tracker() { 175 TestConfigurator* test_configurator();
230 return notification_tracker_;
231 }
232
233 TestConfigurator* test_configurator() {
234 return test_config_;
235 }
236 176
237 ComponentUpdateService::Status RegisterComponent(CrxComponent* com, 177 ComponentUpdateService::Status RegisterComponent(CrxComponent* com,
238 TestComponents component, 178 TestComponents component,
239 const Version& version) { 179 const Version& version,
240 if (component == kTestComponent_abag) { 180 TestInstaller* installer);
241 com->name = "test_abag";
242 com->pk_hash.assign(abag_hash, abag_hash + arraysize(abag_hash));
243 } else {
244 com->name = "test_jebg";
245 com->pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
246 }
247 com->version = version;
248 TestInstaller* installer = new TestInstaller;
249 com->installer = installer;
250 test_installers_.push_back(installer);
251 return component_updater_->RegisterComponent(*com);
252 }
253 181
254 private: 182 private:
255 scoped_ptr<ComponentUpdateService> component_updater_; 183 scoped_ptr<ComponentUpdateService> component_updater_;
256 base::FilePath test_data_dir_; 184 base::FilePath test_data_dir_;
257 TestNotificationTracker notification_tracker_; 185 TestNotificationTracker notification_tracker_;
258 TestConfigurator* test_config_; 186 TestConfigurator* test_config_;
259 // ComponentInstaller objects to delete after each test. 187 };
260 ScopedVector<TestInstaller> test_installers_; 188
261 }; 189 const char expected_crx_url[] =
190 "http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx";
191
192 class PingChecker {
erikwright (departed) 2013/06/17 17:24:28 These classes are sufficiently complicated that I
waffles 2013/06/17 23:51:07 I've moved the POST interceptors into a separate f
193 public:
194 explicit PingChecker(const std::map<std::string, std::string>& attributes);
195
196 virtual ~PingChecker();
197
198 void Trial(net::URLRequest* request);
199
200 int NumHits() const {
201 return num_hits_;
202 }
203 int NumMisses() const {
204 return num_misses_;
205 }
206
207 private:
208 int num_hits_;
209 int num_misses_;
210 const std::map<std::string, std::string> attributes_;
211 virtual bool Test(net::URLRequest* request);
212 };
213
214 PingChecker::PingChecker(const std::map<std::string, std::string>& attributes)
215 : num_hits_(0), num_misses_(0), attributes_(attributes) {
216 }
217
218 PingChecker::~PingChecker() {}
219
220 void PingChecker::Trial(net::URLRequest* request) {
221 if (Test(request))
222 ++num_hits_;
223 else
224 ++num_misses_;
225 }
226
227 bool PingChecker::Test(net::URLRequest* request) {
228 if (request->has_upload()) {
229 const net::UploadDataStream* stream = request->get_upload();
230 const net::UploadBytesElementReader* reader =
231 stream->element_readers()[0]->AsBytesReader();
232 int size = reader->length();
233 scoped_refptr <net::IOBuffer> buffer = new net::IOBuffer(size);
234 std::string data(reader->bytes());
235 // For now, we assume that there is only one ping per POST.
236 std::string::size_type start = data.find("<o:event");
237 if (start != std::string::npos) {
238 std::string::size_type end = data.find(">", start);
239 if (end != std::string::npos) {
240 std::string ping = data.substr(start, end - start);
241 std::map<std::string, std::string>::const_iterator iter;
242 for (iter = attributes_.begin(); iter != attributes_.end(); ++iter) {
243 // does the ping contain the specified attribute/value?
244 if (ping.find(std::string(" ") + (iter->first) +
245 std::string("=") + (iter->second)) == string::npos) {
246 return false;
247 }
248 }
249 return true;
250 }
251 }
252 }
253 return false;
254 }
255
256 class URLRequestPostInterceptor {
257 public:
258 explicit URLRequestPostInterceptor(PingChecker* counter);
259 virtual ~URLRequestPostInterceptor();
260
261 private:
262 class Delegate;
263
264 // After creation, |delegate_| lives on the IO thread, and a task to delete it
265 // is posted from ~URLRequestPostInterceptor().
266 Delegate* delegate_;
267
268 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor);
269 };
270
271 class URLRequestPostInterceptor::Delegate
272 : public net::URLRequestJobFactory::ProtocolHandler {
273 public:
274 explicit Delegate(PingChecker* counter);
275 virtual ~Delegate() {}
276
277 void Register();
278
279 void Unregister();
280
281 private:
282 PingChecker* counter_;
283
284 virtual net::URLRequestJob* MaybeCreateJob(
285 net::URLRequest* request,
286 net::NetworkDelegate* network_delegate) const OVERRIDE;
287 };
288
289 class URLRequestPingMockJob : public net::URLRequestSimpleJob {
290 public:
291 URLRequestPingMockJob(net::URLRequest* request,
292 net::NetworkDelegate* network_delegate);
293
294 protected:
295 virtual int GetData(std::string* mime_type,
296 std::string* charset,
297 std::string* data,
298 const net::CompletionCallback& callback) const OVERRIDE;
299
300 private:
301 virtual ~URLRequestPingMockJob() {}
302
303 DISALLOW_COPY_AND_ASSIGN(URLRequestPingMockJob);
304 };
305
306 URLRequestPostInterceptor::URLRequestPostInterceptor(
307 PingChecker* counter) : delegate_(new Delegate(counter)) {
308 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
309 base::Bind(&Delegate::Register,
310 base::Unretained(delegate_)));
311 }
312
313 URLRequestPostInterceptor::~URLRequestPostInterceptor() {
314 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
315 base::Bind(&Delegate::Unregister,
316 base::Unretained(delegate_)));
317 }
318
319 URLRequestPostInterceptor::Delegate::Delegate(
320 PingChecker* counter) : counter_(counter) {
321 }
322
323 void URLRequestPostInterceptor::Delegate::Register() {
324 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler(
325 "http", "localhost2",
326 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this));
327 }
328
329 void URLRequestPostInterceptor::Delegate::Unregister() {
330 net::URLRequestFilter::GetInstance()->
331 RemoveHostnameHandler("http", "localhost2");
332 }
333
334 net::URLRequestJob* URLRequestPostInterceptor::Delegate::MaybeCreateJob(
335 net::URLRequest* request,
336 net::NetworkDelegate* network_delegate) const {
337 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
338 if (request->has_upload()) {
339 counter_->Trial(request);
340 return new URLRequestPingMockJob(request, network_delegate);
341 }
342 return NULL;
343 }
344
345 URLRequestPingMockJob::URLRequestPingMockJob(
346 net::URLRequest* request,
347 net::NetworkDelegate* network_delegate)
348 : net::URLRequestSimpleJob(request, network_delegate) {
349 }
350
351 int URLRequestPingMockJob::GetData(
352 std::string* mime_type,
353 std::string* charset,
354 std::string* data,
355 const net::CompletionCallback& callback) const {
356 mime_type->assign("text/plain");
357 charset->assign("US-ASCII");
358 data->assign(""); // There is no reason to have a response body in this test.
359 return net::OK;
360 }
361
362 } // namespace
363
364 TestInstaller::TestInstaller()
365 : error_(0), install_count_(0) {
366 }
367
368 void TestInstaller::OnUpdateError(int error) {
369 EXPECT_NE(0, error);
370 error_ = error;
371 }
372
373 bool TestInstaller::Install(const base::DictionaryValue& manifest,
374 const base::FilePath& unpack_path) {
375 ++install_count_;
376 return file_util::Delete(unpack_path, true);
377 }
378
379 bool TestInstaller::GetInstalledFile(const std::string& file,
380 base::FilePath* installed_file) {
381 return false;
382 }
383
384 int TestInstaller::error() const { return error_; }
385
386 int TestInstaller::install_count() const { return install_count_; }
387
388 PersistentTestInstaller::PersistentTestInstaller() {
389 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"),
390 &install_directory_);
391 }
392
393 PersistentTestInstaller::~PersistentTestInstaller() {
394 file_util::Delete(install_directory_, true);
395 }
396
397 bool PersistentTestInstaller::Install(const base::DictionaryValue& manifest,
398 const base::FilePath& unpack_path) {
399 std::string version_string;
400 manifest.GetStringASCII("version", &version_string);
401 Version version(version_string.c_str());
402
403 base::FilePath path;
404 path = install_directory_.AppendASCII(version.GetString());
405 file_util::CreateDirectory(path.DirName());
406 if (!file_util::Move(unpack_path, path))
407 return false;
408 current_version_ = version;
409 ++install_count_;
410 return true;
411 }
412
413 bool PersistentTestInstaller::GetInstalledFile(const std::string& file,
414 base::FilePath* installed_file) {
415 base::FilePath path;
416 path = install_directory_.AppendASCII(current_version_.GetString());
417 *installed_file = path.Append(base::FilePath::FromUTF8Unsafe(file));
418 return true;
419 }
420
421 TestConfigurator::TestConfigurator()
422 : times_(1), recheck_time_(0), ondemand_time_(0), cus_(NULL) {
423 }
424
425 int TestConfigurator::InitialDelay() { return 0; }
426
427 int TestConfigurator::NextCheckDelay() {
428 // This is called when a new full cycle of checking for updates is going
429 // to happen. In test we normally only test one cycle so it is a good
430 // time to break from the test messageloop Run() method so the test can
431 // finish.
432 if (--times_ <= 0) {
433 base::MessageLoop::current()->Quit();
434 return 0;
435 }
436
437 // Look for checks to issue in the middle of the loop.
438 for (std::list<CheckAtLoopCount>::iterator
439 i = components_to_check_.begin();
440 i != components_to_check_.end(); ) {
441 if (i->second == times_) {
442 cus_->CheckForUpdateSoon(*i->first);
443 i = components_to_check_.erase(i);
444 } else {
445 ++i;
446 }
447 }
448 return 1;
449 }
450
451 int TestConfigurator::StepDelay() {
452 return 0;
453 }
454
455 int TestConfigurator::MinimumReCheckWait() {
456 return recheck_time_;
457 }
458
459 int TestConfigurator::OnDemandDelay() {
460 return ondemand_time_;
461 }
462
463 GURL TestConfigurator::UpdateUrl(CrxComponent::UrlSource source) {
464 switch (source) {
465 case CrxComponent::BANDAID:
466 return GURL("http://localhost/upd");
467 case CrxComponent::CWS_PUBLIC:
468 return GURL("http://localhost/cws");
469 default:
470 return GURL("http://wronghost/bad");
471 };
472 }
473
474 GURL TestConfigurator::PingUrl() {
475 return GURL("http://localhost2/ping");
476 }
477
478 const char* TestConfigurator::ExtraRequestParams() { return "extra=foo"; }
479
480 size_t TestConfigurator::UrlSizeLimit() { return 256; }
481
482 net::URLRequestContextGetter* TestConfigurator::RequestContext() {
483 return new net::TestURLRequestContextGetter(
484 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
485 }
486
487 // Don't use the utility process to decode files.
488 bool TestConfigurator::InProcess() { return true; }
489
490 void TestConfigurator::OnEvent(Events event, int extra) { }
491
492 ComponentPatcher* TestConfigurator::CreateComponentPatcher() {
493 return new MockComponentPatcher();
494 }
495
496 bool TestConfigurator::PingsEnabled() const {
497 return true;
498 }
499
500 bool TestConfigurator::DeltasEnabled() const {
501 return true;
502 }
503
504 // Set how many update checks are called, the default value is just once.
505 void TestConfigurator::SetLoopCount(int times) { times_ = times; }
506
507 void TestConfigurator::SetRecheckTime(int seconds) {
508 recheck_time_ = seconds;
509 }
510
511 void TestConfigurator::SetOnDemandTime(int seconds) {
512 ondemand_time_ = seconds;
513 }
514
515 void TestConfigurator::AddComponentToCheck(CrxComponent* com,
516 int at_loop_iter) {
517 components_to_check_.push_back(std::make_pair(com, at_loop_iter));
518 }
519
520 void TestConfigurator::SetComponentUpdateService(ComponentUpdateService* cus) {
521 cus_ = cus;
522 }
523
524 ComponentUpdaterTest::ComponentUpdaterTest() : test_config_(NULL) {
525 // The component updater instance under test.
526 test_config_ = new TestConfigurator;
527 component_updater_.reset(ComponentUpdateServiceFactory(test_config_));
528 test_config_->SetComponentUpdateService(component_updater_.get());
529 // The test directory is chrome/test/data/components.
530 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
531 test_data_dir_ = test_data_dir_.AppendASCII("components");
532
533 // Subscribe to all component updater notifications.
534 const int notifications[] = {
535 chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED,
536 chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING,
537 chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND,
538 chrome::NOTIFICATION_COMPONENT_UPDATE_READY
539 };
540
541 for (int ix = 0; ix != arraysize(notifications); ++ix) {
542 notification_tracker_.ListenFor(
543 notifications[ix], content::NotificationService::AllSources());
544 }
545 net::URLFetcher::SetEnableInterceptionForTests(true);
546 }
547
548 ComponentUpdaterTest::~ComponentUpdaterTest() {
549 net::URLFetcher::SetEnableInterceptionForTests(false);
550 }
551
552 void ComponentUpdaterTest::TearDown() {
553 xmlCleanupGlobals();
554 }
555
556 ComponentUpdateService* ComponentUpdaterTest::component_updater() {
557 return component_updater_.get();
558 }
559
560 // Makes the full path to a component updater test file.
561 const base::FilePath ComponentUpdaterTest::test_file(const char* file) {
562 return test_data_dir_.AppendASCII(file);
563 }
564
565 TestNotificationTracker& ComponentUpdaterTest::notification_tracker() {
566 return notification_tracker_;
567 }
568
569 TestConfigurator* ComponentUpdaterTest::test_configurator() {
570 return test_config_;
571 }
572
573 ComponentUpdateService::Status ComponentUpdaterTest::RegisterComponent(
574 CrxComponent* com,
575 TestComponents component,
576 const Version& version,
577 TestInstaller* installer) {
578 if (component == kTestComponent_abag) {
579 com->name = "test_abag";
580 com->pk_hash.assign(abag_hash, abag_hash + arraysize(abag_hash));
581 } else if (component == kTestComponent_jebg) {
582 com->name = "test_jebg";
583 com->pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
584 } else {
585 com->name = "test_ihfo";
586 com->pk_hash.assign(ihfo_hash, ihfo_hash + arraysize(ihfo_hash));
587 }
588 com->version = version;
589 com->installer = installer;
590 return component_updater_->RegisterComponent(*com);
591 }
262 592
263 // Verify that our test fixture work and the component updater can 593 // Verify that our test fixture work and the component updater can
264 // be created and destroyed with no side effects. 594 // be created and destroyed with no side effects.
265 TEST_F(ComponentUpdaterTest, VerifyFixture) { 595 TEST_F(ComponentUpdaterTest, VerifyFixture) {
266 EXPECT_TRUE(component_updater() != NULL); 596 EXPECT_TRUE(component_updater() != NULL);
267 EXPECT_EQ(0ul, notification_tracker().size()); 597 EXPECT_EQ(0ul, notification_tracker().size());
268 } 598 }
269 599
270 // Verify that the component updater can be caught in a quick 600 // Verify that the component updater can be caught in a quick
271 // start-shutdown situation. Failure of this test will be a crash. Also 601 // start-shutdown situation. Failure of this test will be a crash. Also
(...skipping 16 matching lines...) Expand all
288 base::MessageLoop message_loop; 618 base::MessageLoop message_loop;
289 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 619 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
290 content::TestBrowserThread file_thread(BrowserThread::FILE); 620 content::TestBrowserThread file_thread(BrowserThread::FILE);
291 content::TestBrowserThread io_thread(BrowserThread::IO); 621 content::TestBrowserThread io_thread(BrowserThread::IO);
292 622
293 io_thread.StartIOThread(); 623 io_thread.StartIOThread();
294 file_thread.Start(); 624 file_thread.Start();
295 625
296 content::URLLocalHostRequestPrepackagedInterceptor interceptor; 626 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
297 627
628 TestInstaller installer;
298 CrxComponent com; 629 CrxComponent com;
299 EXPECT_EQ(ComponentUpdateService::kOk, 630 EXPECT_EQ(ComponentUpdateService::kOk,
300 RegisterComponent(&com, kTestComponent_abag, Version("1.1"))); 631 RegisterComponent(&com,
632 kTestComponent_abag,
633 Version("1.1"),
634 &installer));
301 635
302 const GURL expected_update_url( 636 const GURL expected_update_url(
303 "http://localhost/upd?extra=foo&x=id%3D" 637 "http://localhost/upd?extra=foo"
304 "abagagagagagagagagagagagagagagag%26v%3D1.1%26uc"); 638 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D1.1%26fp%3D%26uc");
305 639
306 interceptor.SetResponse(expected_update_url, 640 interceptor.SetResponse(expected_update_url,
307 test_file("updatecheck_reply_1.xml")); 641 test_file("updatecheck_reply_1.xml"));
308 642
309 // We loop twice, but there are no updates so we expect two sleep messages. 643 // We loop twice, but there are no updates so we expect two sleep messages.
310 test_configurator()->SetLoopCount(2); 644 test_configurator()->SetLoopCount(2);
311 component_updater()->Start(); 645 component_updater()->Start();
312 646
313 ASSERT_EQ(1ul, notification_tracker().size()); 647 ASSERT_EQ(1ul, notification_tracker().size());
314 TestNotificationTracker::Event ev1 = notification_tracker().at(0); 648 TestNotificationTracker::Event ev1 = notification_tracker().at(0);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); 686 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
353 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); 687 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count());
354 688
355 component_updater()->Stop(); 689 component_updater()->Stop();
356 } 690 }
357 691
358 // Verify that we can check for updates and install one component. Besides 692 // Verify that we can check for updates and install one component. Besides
359 // the notifications above NOTIFICATION_COMPONENT_UPDATE_FOUND and 693 // the notifications above NOTIFICATION_COMPONENT_UPDATE_FOUND and
360 // NOTIFICATION_COMPONENT_UPDATE_READY should have been fired. We do two loops 694 // NOTIFICATION_COMPONENT_UPDATE_READY should have been fired. We do two loops
361 // so the second time around there should be nothing left to do. 695 // so the second time around there should be nothing left to do.
362 // We also check that only 3 network requests are issued: 696 // We also check that only 3 non-ping network requests are issued:
363 // 1- manifest check 697 // 1- manifest check
364 // 2- download crx 698 // 2- download crx
365 // 3- second manifest check. 699 // 3- second manifest check.
366 TEST_F(ComponentUpdaterTest, InstallCrx) { 700 TEST_F(ComponentUpdaterTest, InstallCrx) {
367 base::MessageLoop message_loop; 701 base::MessageLoop message_loop;
368 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 702 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
369 content::TestBrowserThread file_thread(BrowserThread::FILE); 703 content::TestBrowserThread file_thread(BrowserThread::FILE);
370 content::TestBrowserThread io_thread(BrowserThread::IO); 704 content::TestBrowserThread io_thread(BrowserThread::IO);
371 705
372 io_thread.StartIOThread(); 706 io_thread.StartIOThread();
373 file_thread.Start(); 707 file_thread.Start();
374 708
709 std::map<std::string, std::string> map;
710 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
711 map.insert(std::pair<std::string, std::string>("eventresult", "\"1\""));
712 map.insert(std::pair<std::string, std::string>("previousversion",
713 "\"0.9\""));
714 map.insert(std::pair<std::string, std::string>("nextversion", "\"1.0\""));
715 PingChecker ping_checker(map);
716 URLRequestPostInterceptor post_interceptor(&ping_checker);
375 content::URLLocalHostRequestPrepackagedInterceptor interceptor; 717 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
376 718
719 TestInstaller installer1;
377 CrxComponent com1; 720 CrxComponent com1;
378 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9")); 721 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9"), &installer1);
722 TestInstaller installer2;
379 CrxComponent com2; 723 CrxComponent com2;
380 RegisterComponent(&com2, kTestComponent_abag, Version("2.2")); 724 RegisterComponent(&com2, kTestComponent_abag, Version("2.2"), &installer2);
381 725
382 const GURL expected_update_url_1( 726 const GURL expected_update_url_1(
383 "http://localhost/upd?extra=foo&x=id%3D" 727 "http://localhost/upd?extra=foo"
384 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc&x=id%3D" 728 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc"
385 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"); 729 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
386 730
387 const GURL expected_update_url_2( 731 const GURL expected_update_url_2(
388 "http://localhost/upd?extra=foo&x=id%3D" 732 "http://localhost/upd?extra=foo"
389 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc&x=id%3D" 733 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc"
390 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26uc"); 734 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26fp%3D%26uc");
391 735
392 interceptor.SetResponse(expected_update_url_1, 736 interceptor.SetResponse(expected_update_url_1,
393 test_file("updatecheck_reply_1.xml")); 737 test_file("updatecheck_reply_1.xml"));
394 interceptor.SetResponse(expected_update_url_2, 738 interceptor.SetResponse(expected_update_url_2,
395 test_file("updatecheck_reply_1.xml")); 739 test_file("updatecheck_reply_1.xml"));
396 interceptor.SetResponse(GURL(expected_crx_url), 740 interceptor.SetResponse(GURL(expected_crx_url),
397 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); 741 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
398 742
399 test_configurator()->SetLoopCount(2); 743 test_configurator()->SetLoopCount(2);
400 744
401 component_updater()->Start(); 745 component_updater()->Start();
402 message_loop.Run(); 746 message_loop.Run();
403 747
404 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); 748 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
405 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count()); 749 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count());
406 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error()); 750 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
407 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count()); 751 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count());
408 752
409 EXPECT_EQ(3, interceptor.GetHitCount()); 753 EXPECT_EQ(3, interceptor.GetHitCount());
754 EXPECT_EQ(1, ping_checker.NumHits());
755 EXPECT_EQ(0, ping_checker.NumMisses());
410 756
411 ASSERT_EQ(5ul, notification_tracker().size()); 757 ASSERT_EQ(5ul, notification_tracker().size());
412 758
413 TestNotificationTracker::Event ev1 = notification_tracker().at(1); 759 TestNotificationTracker::Event ev1 = notification_tracker().at(1);
414 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type); 760 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type);
415 761
416 TestNotificationTracker::Event ev2 = notification_tracker().at(2); 762 TestNotificationTracker::Event ev2 = notification_tracker().at(2);
417 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type); 763 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type);
418 764
419 TestNotificationTracker::Event ev3 = notification_tracker().at(3); 765 TestNotificationTracker::Event ev3 = notification_tracker().at(3);
(...skipping 10 matching lines...) Expand all
430 // checks to different urls, each only containing one component. 776 // checks to different urls, each only containing one component.
431 TEST_F(ComponentUpdaterTest, InstallCrxTwoSources) { 777 TEST_F(ComponentUpdaterTest, InstallCrxTwoSources) {
432 base::MessageLoop message_loop; 778 base::MessageLoop message_loop;
433 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 779 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
434 content::TestBrowserThread file_thread(BrowserThread::FILE); 780 content::TestBrowserThread file_thread(BrowserThread::FILE);
435 content::TestBrowserThread io_thread(BrowserThread::IO); 781 content::TestBrowserThread io_thread(BrowserThread::IO);
436 782
437 io_thread.StartIOThread(); 783 io_thread.StartIOThread();
438 file_thread.Start(); 784 file_thread.Start();
439 785
786 std::map<std::string, std::string> map;
787 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
788 map.insert(std::pair<std::string, std::string>("eventresult", "\"1\""));
789 map.insert(std::pair<std::string, std::string>("previousversion",
790 "\"0.9\""));
791 map.insert(std::pair<std::string, std::string>("nextversion", "\"1.0\""));
792 PingChecker ping_checker(map);
793 URLRequestPostInterceptor post_interceptor(&ping_checker);
440 content::URLLocalHostRequestPrepackagedInterceptor interceptor; 794 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
441 795
796 TestInstaller installer1;
442 CrxComponent com1; 797 CrxComponent com1;
443 RegisterComponent(&com1, kTestComponent_abag, Version("2.2")); 798 RegisterComponent(&com1, kTestComponent_abag, Version("2.2"), &installer1);
799 TestInstaller installer2;
444 CrxComponent com2; 800 CrxComponent com2;
445 com2.source = CrxComponent::CWS_PUBLIC; 801 com2.source = CrxComponent::CWS_PUBLIC;
446 RegisterComponent(&com2, kTestComponent_jebg, Version("0.9")); 802 RegisterComponent(&com2, kTestComponent_jebg, Version("0.9"), &installer2);
447 803
448 const GURL expected_update_url_1( 804 const GURL expected_update_url_1(
449 "http://localhost/upd?extra=foo&x=id%3D" 805 "http://localhost/upd?extra=foo&x=id%3D"
450 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"); 806 "abagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
451 807
452 const GURL expected_update_url_2( 808 const GURL expected_update_url_2(
453 "http://localhost/cws?extra=foo&x=id%3D" 809 "http://localhost/cws?extra=foo&x=id%3D"
454 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc"); 810 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc");
455 811
456 interceptor.SetResponse(expected_update_url_1, 812 interceptor.SetResponse(expected_update_url_1,
457 test_file("updatecheck_reply_3.xml")); 813 test_file("updatecheck_reply_3.xml"));
458 interceptor.SetResponse(expected_update_url_2, 814 interceptor.SetResponse(expected_update_url_2,
459 test_file("updatecheck_reply_1.xml")); 815 test_file("updatecheck_reply_1.xml"));
460 interceptor.SetResponse(GURL(expected_crx_url), 816 interceptor.SetResponse(GURL(expected_crx_url),
461 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); 817 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
462 818
463 test_configurator()->SetLoopCount(3); 819 test_configurator()->SetLoopCount(3);
464 820
465 // We have to set SetRecheckTime to something bigger than 0 or else the 821 // We have to set SetRecheckTime to something bigger than 0 or else the
466 // component updater will keep re-checking the 'abag' component because 822 // component updater will keep re-checking the 'abag' component because
467 // the default source pre-empts the other sources. 823 // the default source pre-empts the other sources.
468 test_configurator()->SetRecheckTime(60*60); 824 test_configurator()->SetRecheckTime(60*60);
469 825
470 component_updater()->Start(); 826 component_updater()->Start();
471 message_loop.Run(); 827 message_loop.Run();
472 828
473 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); 829 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
474 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->install_count()); 830 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->install_count());
475 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error()); 831 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
476 EXPECT_EQ(1, static_cast<TestInstaller*>(com2.installer)->install_count()); 832 EXPECT_EQ(1, static_cast<TestInstaller*>(com2.installer)->install_count());
477 833
834 EXPECT_EQ(1, ping_checker.NumHits());
835 EXPECT_EQ(0, ping_checker.NumMisses());
478 EXPECT_EQ(3, interceptor.GetHitCount()); 836 EXPECT_EQ(3, interceptor.GetHitCount());
479 837
480 ASSERT_EQ(6ul, notification_tracker().size()); 838 ASSERT_EQ(6ul, notification_tracker().size());
481 839
482 TestNotificationTracker::Event ev0 = notification_tracker().at(1); 840 TestNotificationTracker::Event ev0 = notification_tracker().at(1);
483 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev0.type); 841 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev0.type);
484 842
485 TestNotificationTracker::Event ev1 = notification_tracker().at(2); 843 TestNotificationTracker::Event ev1 = notification_tracker().at(2);
486 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type); 844 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type);
487 845
(...skipping 14 matching lines...) Expand all
502 // version is much higher than of chrome. 860 // version is much higher than of chrome.
503 TEST_F(ComponentUpdaterTest, ProdVersionCheck) { 861 TEST_F(ComponentUpdaterTest, ProdVersionCheck) {
504 base::MessageLoop message_loop; 862 base::MessageLoop message_loop;
505 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 863 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
506 content::TestBrowserThread file_thread(BrowserThread::FILE); 864 content::TestBrowserThread file_thread(BrowserThread::FILE);
507 content::TestBrowserThread io_thread(BrowserThread::IO); 865 content::TestBrowserThread io_thread(BrowserThread::IO);
508 866
509 io_thread.StartIOThread(); 867 io_thread.StartIOThread();
510 file_thread.Start(); 868 file_thread.Start();
511 869
870 std::map<std::string, std::string> map;
871 PingChecker ping_checker(map);
872 URLRequestPostInterceptor post_interceptor(&ping_checker);
512 content::URLLocalHostRequestPrepackagedInterceptor interceptor; 873 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
513 874
875 TestInstaller installer;
514 CrxComponent com; 876 CrxComponent com;
515 RegisterComponent(&com, kTestComponent_jebg, Version("0.9")); 877 RegisterComponent(&com, kTestComponent_jebg, Version("0.9"), &installer);
516 878
517 const GURL expected_update_url( 879 const GURL expected_update_url(
518 "http://localhost/upd?extra=foo&x=id%3D" 880 "http://localhost/upd?extra=foo&x=id%3D"
519 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc"); 881 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc");
520 882
521 interceptor.SetResponse(expected_update_url, 883 interceptor.SetResponse(expected_update_url,
522 test_file("updatecheck_reply_2.xml")); 884 test_file("updatecheck_reply_2.xml"));
523 interceptor.SetResponse(GURL(expected_crx_url), 885 interceptor.SetResponse(GURL(expected_crx_url),
524 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); 886 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
525 887
526 test_configurator()->SetLoopCount(1); 888 test_configurator()->SetLoopCount(1);
527 component_updater()->Start(); 889 component_updater()->Start();
528 message_loop.Run(); 890 message_loop.Run();
529 891
892 EXPECT_EQ(0, ping_checker.NumHits());
893 EXPECT_EQ(0, ping_checker.NumMisses());
530 EXPECT_EQ(1, interceptor.GetHitCount()); 894 EXPECT_EQ(1, interceptor.GetHitCount());
531 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); 895 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
532 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); 896 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count());
533 897
534 component_updater()->Stop(); 898 component_updater()->Stop();
535 } 899 }
536 900
537 // Test that a ping for an update check can cause installs. 901 // Test that a ping for an update check can cause installs.
538 // Here is the timeline: 902 // Here is the timeline:
539 // - First loop: we return a reply that indicates no update, so 903 // - First loop: we return a reply that indicates no update, so
540 // nothing happens. 904 // nothing happens.
541 // - We ping. 905 // - We ping.
542 // - This triggers a second loop, which has a reply that triggers an install. 906 // - This triggers a second loop, which has a reply that triggers an install.
543 TEST_F(ComponentUpdaterTest, CheckForUpdateSoon) { 907 TEST_F(ComponentUpdaterTest, CheckForUpdateSoon) {
544 base::MessageLoop message_loop; 908 base::MessageLoop message_loop;
545 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 909 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
546 content::TestBrowserThread file_thread(BrowserThread::FILE); 910 content::TestBrowserThread file_thread(BrowserThread::FILE);
547 content::TestBrowserThread io_thread(BrowserThread::IO); 911 content::TestBrowserThread io_thread(BrowserThread::IO);
548 912
549 io_thread.StartIOThread(); 913 io_thread.StartIOThread();
550 file_thread.Start(); 914 file_thread.Start();
551 915
916 std::map<std::string, std::string> map;
917 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
918 map.insert(std::pair<std::string, std::string>("eventresult", "\"1\""));
919 map.insert(std::pair<std::string, std::string>("previousversion",
920 "\"0.9\""));
921 map.insert(std::pair<std::string, std::string>("nextversion", "\"1.0\""));
922 PingChecker ping_checker(map);
923 URLRequestPostInterceptor post_interceptor(&ping_checker);
552 content::URLLocalHostRequestPrepackagedInterceptor interceptor; 924 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
553 925
926 TestInstaller installer1;
554 CrxComponent com1; 927 CrxComponent com1;
555 RegisterComponent(&com1, kTestComponent_abag, Version("2.2")); 928 RegisterComponent(&com1, kTestComponent_abag, Version("2.2"), &installer1);
929 TestInstaller installer2;
556 CrxComponent com2; 930 CrxComponent com2;
557 RegisterComponent(&com2, kTestComponent_jebg, Version("0.9")); 931 RegisterComponent(&com2, kTestComponent_jebg, Version("0.9"), &installer2);
558 932
559 const GURL expected_update_url_1( 933 const GURL expected_update_url_1(
560 "http://localhost/upd?extra=foo&x=id%3D" 934 "http://localhost/upd?extra=foo"
561 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc&x=id%3D" 935 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc"
562 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc"); 936 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc");
563 937
564 const GURL expected_update_url_2( 938 const GURL expected_update_url_2(
565 "http://localhost/upd?extra=foo&x=id%3D" 939 "http://localhost/upd?extra=foo"
566 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc&x=id%3D" 940 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc"
567 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"); 941 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
568 942
569 interceptor.SetResponse(expected_update_url_1, 943 interceptor.SetResponse(expected_update_url_1,
570 test_file("updatecheck_reply_empty")); 944 test_file("updatecheck_reply_empty"));
571 interceptor.SetResponse(expected_update_url_2, 945 interceptor.SetResponse(expected_update_url_2,
572 test_file("updatecheck_reply_1.xml")); 946 test_file("updatecheck_reply_1.xml"));
573 interceptor.SetResponse(GURL(expected_crx_url), 947 interceptor.SetResponse(GURL(expected_crx_url),
574 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); 948 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
575 // Test success. 949 // Test success.
576 test_configurator()->SetLoopCount(2); 950 test_configurator()->SetLoopCount(2);
577 test_configurator()->AddComponentToCheck(&com2, 1); 951 test_configurator()->AddComponentToCheck(&com2, 1);
(...skipping 28 matching lines...) Expand all
606 test_configurator()->SetOnDemandTime(60 * 60); 980 test_configurator()->SetOnDemandTime(60 * 60);
607 EXPECT_EQ(ComponentUpdateService::kError, 981 EXPECT_EQ(ComponentUpdateService::kError,
608 component_updater()->CheckForUpdateSoon(com2)); 982 component_updater()->CheckForUpdateSoon(com2));
609 // Okay, now reset to 0 for the other tests. 983 // Okay, now reset to 0 for the other tests.
610 test_configurator()->SetOnDemandTime(0); 984 test_configurator()->SetOnDemandTime(0);
611 component_updater()->Stop(); 985 component_updater()->Stop();
612 986
613 // Test a few error cases. NOTE: We don't have callbacks for 987 // Test a few error cases. NOTE: We don't have callbacks for
614 // when the updates failed yet. 988 // when the updates failed yet.
615 const GURL expected_update_url_3( 989 const GURL expected_update_url_3(
616 "http://localhost/upd?extra=foo&x=id%3D" 990 "http://localhost/upd?extra=foo"
617 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26uc&x=id%3D" 991 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26fp%3D%26uc"
618 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"); 992 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
619 993
620 // No update: error from no server response 994 // No update: error from no server response
621 interceptor.SetResponse(expected_update_url_3, 995 interceptor.SetResponse(expected_update_url_3,
622 test_file("updatecheck_reply_empty")); 996 test_file("updatecheck_reply_empty"));
623 notification_tracker().Reset(); 997 notification_tracker().Reset();
624 test_configurator()->SetLoopCount(1); 998 test_configurator()->SetLoopCount(1);
625 component_updater()->Start(); 999 component_updater()->Start();
626 EXPECT_EQ(ComponentUpdateService::kOk, 1000 EXPECT_EQ(ComponentUpdateService::kOk,
627 component_updater()->CheckForUpdateSoon(com2)); 1001 component_updater()->CheckForUpdateSoon(com2));
628 1002
(...skipping 10 matching lines...) Expand all
639 interceptor.SetResponse(expected_update_url_3, 1013 interceptor.SetResponse(expected_update_url_3,
640 test_file("updatecheck_reply_1.xml")); 1014 test_file("updatecheck_reply_1.xml"));
641 notification_tracker().Reset(); 1015 notification_tracker().Reset();
642 test_configurator()->SetLoopCount(1); 1016 test_configurator()->SetLoopCount(1);
643 component_updater()->Start(); 1017 component_updater()->Start();
644 EXPECT_EQ(ComponentUpdateService::kOk, 1018 EXPECT_EQ(ComponentUpdateService::kOk,
645 component_updater()->CheckForUpdateSoon(com2)); 1019 component_updater()->CheckForUpdateSoon(com2));
646 1020
647 message_loop.Run(); 1021 message_loop.Run();
648 1022
1023 EXPECT_EQ(1, ping_checker.NumHits());
1024 EXPECT_EQ(0, ping_checker.NumMisses());
649 ASSERT_EQ(2ul, notification_tracker().size()); 1025 ASSERT_EQ(2ul, notification_tracker().size());
650 ev0 = notification_tracker().at(0); 1026 ev0 = notification_tracker().at(0);
651 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type); 1027 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
652 ev1 = notification_tracker().at(1); 1028 ev1 = notification_tracker().at(1);
653 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type); 1029 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type);
654 component_updater()->Stop(); 1030 component_updater()->Stop();
655 } 1031 }
656 1032
657 // Verify that a previously registered component can get re-registered 1033 // Verify that a previously registered component can get re-registered
658 // with a different version. 1034 // with a different version.
659 TEST_F(ComponentUpdaterTest, CheckReRegistration) { 1035 TEST_F(ComponentUpdaterTest, CheckReRegistration) {
660 base::MessageLoop message_loop; 1036 base::MessageLoop message_loop;
661 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); 1037 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
662 content::TestBrowserThread file_thread(BrowserThread::FILE); 1038 content::TestBrowserThread file_thread(BrowserThread::FILE);
663 content::TestBrowserThread io_thread(BrowserThread::IO); 1039 content::TestBrowserThread io_thread(BrowserThread::IO);
664 1040
665 io_thread.StartIOThread(); 1041 io_thread.StartIOThread();
666 file_thread.Start(); 1042 file_thread.Start();
667 1043
1044 std::map<std::string, std::string> map;
1045 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
1046 map.insert(std::pair<std::string, std::string>("eventresult", "\"1\""));
1047 map.insert(std::pair<std::string, std::string>("previousversion",
1048 "\"0.9\""));
1049 map.insert(std::pair<std::string, std::string>("nextversion", "\"1.0\""));
1050 PingChecker ping_checker(map);
1051 URLRequestPostInterceptor post_interceptor(&ping_checker);
668 content::URLLocalHostRequestPrepackagedInterceptor interceptor; 1052 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
669 1053
1054 TestInstaller installer1;
670 CrxComponent com1; 1055 CrxComponent com1;
671 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9")); 1056 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9"), &installer1);
1057 TestInstaller installer2;
672 CrxComponent com2; 1058 CrxComponent com2;
673 RegisterComponent(&com2, kTestComponent_abag, Version("2.2")); 1059 RegisterComponent(&com2, kTestComponent_abag, Version("2.2"), &installer2);
674 1060
675 // Start with 0.9, and update to 1.0 1061 // Start with 0.9, and update to 1.0
676 const GURL expected_update_url_1( 1062 const GURL expected_update_url_1(
677 "http://localhost/upd?extra=foo&x=id%3D" 1063 "http://localhost/upd?extra=foo"
678 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc&x=id%3D" 1064 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc"
679 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"); 1065 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
680 1066
681 const GURL expected_update_url_2( 1067 const GURL expected_update_url_2(
682 "http://localhost/upd?extra=foo&x=id%3D" 1068 "http://localhost/upd?extra=foo"
683 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc&x=id%3D" 1069 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc"
684 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26uc"); 1070 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26fp%3D%26uc");
685 1071
686 interceptor.SetResponse(expected_update_url_1, 1072 interceptor.SetResponse(expected_update_url_1,
687 test_file("updatecheck_reply_1.xml")); 1073 test_file("updatecheck_reply_1.xml"));
688 interceptor.SetResponse(expected_update_url_2, 1074 interceptor.SetResponse(expected_update_url_2,
689 test_file("updatecheck_reply_1.xml")); 1075 test_file("updatecheck_reply_1.xml"));
690 interceptor.SetResponse(GURL(expected_crx_url), 1076 interceptor.SetResponse(GURL(expected_crx_url),
691 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); 1077 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
692 1078
693 // Loop twice to issue two checks: (1) with original 0.9 version 1079 // Loop twice to issue two checks: (1) with original 0.9 version
694 // and (2) with the updated 1.0 version. 1080 // and (2) with the updated 1.0 version.
695 test_configurator()->SetLoopCount(2); 1081 test_configurator()->SetLoopCount(2);
696 1082
697 component_updater()->Start(); 1083 component_updater()->Start();
698 message_loop.Run(); 1084 message_loop.Run();
699 1085
700 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); 1086 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
701 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count()); 1087 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count());
702 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error()); 1088 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
703 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count()); 1089 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count());
704 1090
1091 EXPECT_EQ(1, ping_checker.NumHits());
1092 EXPECT_EQ(0, ping_checker.NumMisses());
705 EXPECT_EQ(3, interceptor.GetHitCount()); 1093 EXPECT_EQ(3, interceptor.GetHitCount());
706 1094
707 ASSERT_EQ(5ul, notification_tracker().size()); 1095 ASSERT_EQ(5ul, notification_tracker().size());
708 1096
709 TestNotificationTracker::Event ev0 = notification_tracker().at(0); 1097 TestNotificationTracker::Event ev0 = notification_tracker().at(0);
710 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type); 1098 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
711 1099
712 TestNotificationTracker::Event ev1 = notification_tracker().at(1); 1100 TestNotificationTracker::Event ev1 = notification_tracker().at(1);
713 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type); 1101 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type);
714 1102
715 TestNotificationTracker::Event ev2 = notification_tracker().at(2); 1103 TestNotificationTracker::Event ev2 = notification_tracker().at(2);
716 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type); 1104 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type);
717 1105
718 TestNotificationTracker::Event ev3 = notification_tracker().at(3); 1106 TestNotificationTracker::Event ev3 = notification_tracker().at(3);
719 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev3.type); 1107 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev3.type);
720 1108
721 TestNotificationTracker::Event ev4 = notification_tracker().at(4); 1109 TestNotificationTracker::Event ev4 = notification_tracker().at(4);
722 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev4.type); 1110 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev4.type);
723 1111
724 // Now re-register, pretending to be an even newer version (2.2) 1112 // Now re-register, pretending to be an even newer version (2.2)
1113 TestInstaller installer3;
725 component_updater()->Stop(); 1114 component_updater()->Stop();
726 EXPECT_EQ(ComponentUpdateService::kReplaced, 1115 EXPECT_EQ(ComponentUpdateService::kReplaced,
727 RegisterComponent(&com1, kTestComponent_jebg, Version("2.2"))); 1116 RegisterComponent(&com1,
1117 kTestComponent_jebg,
1118 Version("2.2"),
1119 &installer3));
728 1120
729 // Check that we send out 2.2 as our version. 1121 // Check that we send out 2.2 as our version.
730 // Interceptor's hit count should go up by 1. 1122 // Interceptor's hit count should go up by 1.
731 const GURL expected_update_url_3( 1123 const GURL expected_update_url_3(
732 "http://localhost/upd?extra=foo&x=id%3D" 1124 "http://localhost/upd?extra=foo"
733 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D2.2%26uc&x=id%3D" 1125 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D2.2%26fp%3D%26uc"
734 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"); 1126 "&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
735 1127
736 interceptor.SetResponse(expected_update_url_3, 1128 interceptor.SetResponse(expected_update_url_3,
737 test_file("updatecheck_reply_1.xml")); 1129 test_file("updatecheck_reply_1.xml"));
738 1130
739 notification_tracker().Reset(); 1131 notification_tracker().Reset();
740 1132
741 // Loop once just to notice the check happening with the re-register version. 1133 // Loop once just to notice the check happening with the re-register version.
742 test_configurator()->SetLoopCount(1); 1134 test_configurator()->SetLoopCount(1);
743 component_updater()->Start(); 1135 component_updater()->Start();
744 message_loop.Run(); 1136 message_loop.Run();
745 1137
746 ASSERT_EQ(2ul, notification_tracker().size()); 1138 ASSERT_EQ(2ul, notification_tracker().size());
747 1139
748 ev0 = notification_tracker().at(0); 1140 ev0 = notification_tracker().at(0);
749 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type); 1141 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
750 1142
751 ev1 = notification_tracker().at(1); 1143 ev1 = notification_tracker().at(1);
752 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type); 1144 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type);
753 1145
754 EXPECT_EQ(4, interceptor.GetHitCount()); 1146 EXPECT_EQ(4, interceptor.GetHitCount());
755 1147
756 // The test harness's Register() function creates a new installer, 1148 // We created a new installer, so the counts go back to 0.
757 // so the counts go back to 0.
758 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); 1149 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
759 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->install_count()); 1150 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->install_count());
760 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error()); 1151 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
761 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count()); 1152 EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count());
762 1153
763 component_updater()->Stop(); 1154 component_updater()->Stop();
764 } 1155 }
1156
1157 // Verify that we can download and install a component and a differential
1158 // update to that component. We do three loops; the final loop should do
1159 // nothing.
1160 // We also check that exactly 5 non-ping network requests are issued:
1161 // 1- update check (response: v1 available)
1162 // 2- download crx (v1)
1163 // 3- update check (response: v2 available)
1164 // 4- download differential crx (v1 to v2)
1165 // 5- update check (response: no further update available)
1166 // There should be two pings, one for each update. The second will bear a
1167 // diffresult=1, while the first will not.
1168 TEST_F(ComponentUpdaterTest, DifferentialUpdate) {
1169 base::MessageLoop message_loop;
1170 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
1171 content::TestBrowserThread file_thread(BrowserThread::FILE);
1172 content::TestBrowserThread io_thread(BrowserThread::IO);
1173
1174 io_thread.StartIOThread();
1175 file_thread.Start();
1176
1177 std::map<std::string, std::string> map;
1178 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
1179 map.insert(std::pair<std::string, std::string>("eventresult", "\"1\""));
1180 map.insert(std::pair<std::string, std::string>("diffresult", "\"1\""));
1181 PingChecker ping_checker(map);
1182 URLRequestPostInterceptor post_interceptor(&ping_checker);
1183 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
1184
1185 PersistentTestInstaller installer;
1186 CrxComponent com;
1187 RegisterComponent(&com, kTestComponent_ihfo, Version("0.0"), &installer);
1188
1189 const GURL expected_update_url_0(
1190 "http://localhost/upd?extra=foo"
1191 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D0.0%26fp%3D%26uc");
1192 const GURL expected_update_url_1(
1193 "http://localhost/upd?extra=foo"
1194 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D1.0%26fp%3D1%26uc");
1195 const GURL expected_update_url_2(
1196 "http://localhost/upd?extra=foo"
1197 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D2.0%26fp%3Df22%26uc");
1198 const GURL expected_crx_url_1(
1199 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx");
1200 const GURL expected_crx_url_1_diff_2(
1201 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx");
1202
1203 interceptor.SetResponse(expected_update_url_0,
1204 test_file("updatecheck_diff_reply_1.xml"));
1205 interceptor.SetResponse(expected_update_url_1,
1206 test_file("updatecheck_diff_reply_2.xml"));
1207 interceptor.SetResponse(expected_update_url_2,
1208 test_file("updatecheck_diff_reply_3.xml"));
1209 interceptor.SetResponse(expected_crx_url_1,
1210 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
1211 interceptor.SetResponse(
1212 expected_crx_url_1_diff_2,
1213 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
1214
1215 test_configurator()->SetLoopCount(3);
1216
1217 component_updater()->Start();
1218 message_loop.Run();
1219
1220 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
1221 EXPECT_EQ(2, static_cast<TestInstaller*>(com.installer)->install_count());
1222
1223 EXPECT_EQ(1, ping_checker.NumHits());
1224 EXPECT_EQ(1, ping_checker.NumMisses());
1225 EXPECT_EQ(5, interceptor.GetHitCount());
1226
1227 component_updater()->Stop();
1228 }
1229
1230 // Verify that component installation falls back to downloading and installing
1231 // a full update if the differential update fails (in this case, because the
1232 // installer does not know about the existing files). We do two loops; the final
1233 // loop should do nothing.
1234 // We also check that exactly 4 non-ping network requests are issued:
1235 // 1- update check (loop 1)
1236 // 2- download differential crx
1237 // 3- download full crx
1238 // 4- update check (loop 2 - no update available)
1239 // There should be one ping for the first attempted update.
1240 TEST_F(ComponentUpdaterTest, DifferentialUpdateFails) {
1241 base::MessageLoop message_loop;
1242 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
1243 content::TestBrowserThread file_thread(BrowserThread::FILE);
1244 content::TestBrowserThread io_thread(BrowserThread::IO);
1245
1246 io_thread.StartIOThread();
1247 file_thread.Start();
1248
1249 std::map<std::string, std::string> map;
1250 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
1251 map.insert(std::pair<std::string, std::string>("eventresult", "\"1\""));
1252 map.insert(std::pair<std::string, std::string>("diffresult", "\"0\""));
1253 map.insert(std::pair<std::string, std::string>("differrorcode", "\"16\""));
1254 PingChecker ping_checker(map);
1255 URLRequestPostInterceptor post_interceptor(&ping_checker);
1256 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
1257
1258 TestInstaller installer;
1259 CrxComponent com;
1260 RegisterComponent(&com, kTestComponent_ihfo, Version("1.0"), &installer);
1261
1262 const GURL expected_update_url_1(
1263 "http://localhost/upd?extra=foo"
1264 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D1.0%26fp%3D%26uc");
1265 const GURL expected_update_url_2(
1266 "http://localhost/upd?extra=foo"
1267 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D2.0%26fp%3Df22%26uc");
1268 const GURL expected_crx_url_1(
1269 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx");
1270 const GURL expected_crx_url_1_diff_2(
1271 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx");
1272 const GURL expected_crx_url_2(
1273 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx");
1274
1275 interceptor.SetResponse(expected_update_url_1,
1276 test_file("updatecheck_diff_reply_2.xml"));
1277 interceptor.SetResponse(expected_update_url_2,
1278 test_file("updatecheck_diff_reply_3.xml"));
1279 interceptor.SetResponse(expected_crx_url_1,
1280 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
1281 interceptor.SetResponse(
1282 expected_crx_url_1_diff_2,
1283 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
1284 interceptor.SetResponse(expected_crx_url_2,
1285 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
1286
1287 test_configurator()->SetLoopCount(2);
1288
1289 component_updater()->Start();
1290 message_loop.Run();
1291
1292 // A failed differential update does not count as a failed install.
1293 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
1294 EXPECT_EQ(1, static_cast<TestInstaller*>(com.installer)->install_count());
1295
1296 EXPECT_EQ(1, ping_checker.NumHits());
1297 EXPECT_EQ(0, ping_checker.NumMisses());
1298 EXPECT_EQ(4, interceptor.GetHitCount());
1299
1300 component_updater()->Stop();
1301 }
1302
1303 // Verify that a failed installation causes an install failure ping.
1304 TEST_F(ComponentUpdaterTest, CheckFailedInstallPing) {
1305 base::MessageLoop message_loop;
1306 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
1307 content::TestBrowserThread file_thread(BrowserThread::FILE);
1308 content::TestBrowserThread io_thread(BrowserThread::IO);
1309
1310 io_thread.StartIOThread();
1311 file_thread.Start();
1312
1313 std::map<std::string, std::string> map;
1314 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
1315 map.insert(std::pair<std::string, std::string>("eventresult", "\"0\""));
1316 map.insert(std::pair<std::string, std::string>("errorcode", "\"9\""));
1317 map.insert(std::pair<std::string, std::string>("previousversion",
1318 "\"0.9\""));
1319 map.insert(std::pair<std::string, std::string>("nextversion", "\"1.0\""));
1320 PingChecker ping_checker(map);
1321 URLRequestPostInterceptor post_interceptor(&ping_checker);
1322 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
1323
1324 // This test installer reports installation failure.
1325 class : public TestInstaller {
1326 virtual bool Install(const base::DictionaryValue& manifest,
1327 const base::FilePath& unpack_path) OVERRIDE {
1328 ++install_count_;
1329 file_util::Delete(unpack_path, true);
1330 return false;
1331 }
1332 } installer;
1333
1334 CrxComponent com;
1335 RegisterComponent(&com, kTestComponent_jebg, Version("0.9"), &installer);
1336
1337 // Start with 0.9, and attempt update to 1.0
1338 const GURL expected_update_url_1(
1339 "http://localhost/upd?extra=foo"
1340 "&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc");
1341
1342 interceptor.SetResponse(expected_update_url_1,
1343 test_file("updatecheck_reply_1.xml"));
1344 interceptor.SetResponse(GURL(expected_crx_url),
1345 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
1346
1347 // Loop twice to issue two checks: (1) with original 0.9 version
1348 // and (2), which should retry with 0.9.
1349 test_configurator()->SetLoopCount(2);
1350 component_updater()->Start();
1351 message_loop.Run();
1352
1353 // Loop once more, but expect no ping because a noupdate response is issued.
1354 // This is necessary to clear out the fire-and-forget ping from the previous
1355 // iteration.
1356 interceptor.SetResponse(expected_update_url_1,
1357 test_file("updatecheck_reply_noupdate.xml"));
1358 test_configurator()->SetLoopCount(1);
1359 component_updater()->Start();
1360 message_loop.Run();
1361
1362 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
1363 EXPECT_EQ(2, static_cast<TestInstaller*>(com.installer)->install_count());
1364
1365 EXPECT_EQ(2, ping_checker.NumHits());
1366 EXPECT_EQ(0, ping_checker.NumMisses());
1367 EXPECT_EQ(5, interceptor.GetHitCount());
1368
1369 component_updater()->Stop();
1370 }
1371
1372 // Verify that we successfully propagate a patcher error.
1373 // ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx contains an incorrect
1374 // patching instruction that should fail.
1375 TEST_F(ComponentUpdaterTest, DifferentialUpdateFailErrorcode) {
1376 base::MessageLoop message_loop;
1377 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
1378 content::TestBrowserThread file_thread(BrowserThread::FILE);
1379 content::TestBrowserThread io_thread(BrowserThread::IO);
1380
1381 io_thread.StartIOThread();
1382 file_thread.Start();
1383
1384 std::map<std::string, std::string> map;
1385 map.insert(std::pair<std::string, std::string>("eventtype", "\"3\""));
1386 map.insert(std::pair<std::string, std::string>("eventresult", "\"1\""));
1387 map.insert(std::pair<std::string, std::string>("diffresult", "\"0\""));
1388 map.insert(std::pair<std::string, std::string>("differrorcode", "\"14\""));
1389 map.insert(std::pair<std::string, std::string>("diffextracode1", "\"305\""));
1390 PingChecker ping_checker(map);
1391 URLRequestPostInterceptor post_interceptor(&ping_checker);
1392 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
1393
1394 PersistentTestInstaller installer;
1395 CrxComponent com;
1396 RegisterComponent(&com, kTestComponent_ihfo, Version("0.0"), &installer);
1397
1398 const GURL expected_update_url_0(
1399 "http://localhost/upd?extra=foo"
1400 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D0.0%26fp%3D%26uc");
1401 const GURL expected_update_url_1(
1402 "http://localhost/upd?extra=foo"
1403 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D1.0%26fp%3D1%26uc");
1404 const GURL expected_update_url_2(
1405 "http://localhost/upd?extra=foo"
1406 "&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D2.0%26fp%3Df22%26uc");
1407 const GURL expected_crx_url_1(
1408 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx");
1409 const GURL expected_crx_url_1_diff_2(
1410 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx");
1411 const GURL expected_crx_url_2(
1412 "http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx");
1413
1414 interceptor.SetResponse(expected_update_url_0,
1415 test_file("updatecheck_diff_reply_1.xml"));
1416 interceptor.SetResponse(expected_update_url_1,
1417 test_file("updatecheck_diff_reply_2.xml"));
1418 interceptor.SetResponse(expected_update_url_2,
1419 test_file("updatecheck_diff_reply_3.xml"));
1420 interceptor.SetResponse(expected_crx_url_1,
1421 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
1422 interceptor.SetResponse(
1423 expected_crx_url_1_diff_2,
1424 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx"));
1425 interceptor.SetResponse(expected_crx_url_2,
1426 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
1427
1428 test_configurator()->SetLoopCount(3);
1429
1430 component_updater()->Start();
1431 message_loop.Run();
1432
1433 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
1434 EXPECT_EQ(2, static_cast<TestInstaller*>(com.installer)->install_count());
1435
1436 EXPECT_EQ(1, ping_checker.NumHits());
1437 EXPECT_EQ(1, ping_checker.NumMisses());
1438 EXPECT_EQ(6, interceptor.GetHitCount());
1439
1440 component_updater()->Stop();
1441 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698