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