| 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" | 5 #include "chrome/browser/component_updater/component_updater_service.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/component_updater/component_updater_interceptor.h" | |
| 15 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 17 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
| 20 #include "content/public/test/test_notification_tracker.h" | 19 #include "content/public/test/test_notification_tracker.h" |
| 20 #include "content/test/net/url_request_prepackaged_interceptor.h" |
| 21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "libxml/globals.h" | 22 #include "libxml/globals.h" |
| 23 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 using content::TestNotificationTracker; | 28 using content::TestNotificationTracker; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 0xfa,0x54,0xb0,0xd2,0xdd,0xa5,0x6e,0x05,0x6b,0xe8, | 113 0xfa,0x54,0xb0,0xd2,0xdd,0xa5,0x6e,0x05,0x6b,0xe8, |
| 114 0x73,0x47,0xf6,0xc4,0x11,0x9f,0xbc,0xb3,0x09,0xb3, | 114 0x73,0x47,0xf6,0xc4,0x11,0x9f,0xbc,0xb3,0x09,0xb3, |
| 115 0x5b,0x40}; | 115 0x5b,0x40}; |
| 116 // component 2 has extension id "abagagagagagagagagagagagagagagag", and | 116 // component 2 has extension id "abagagagagagagagagagagagagagagag", and |
| 117 // the RSA public key the following hash: | 117 // the RSA public key the following hash: |
| 118 const uint8 abag_hash[] = {0x01,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, | 118 const uint8 abag_hash[] = {0x01,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, |
| 119 0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, | 119 0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, |
| 120 0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, | 120 0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06, |
| 121 0x06,0x01}; | 121 0x06,0x01}; |
| 122 | 122 |
| 123 const char header_ok_reply[] = | |
| 124 "HTTP/1.1 200 OK\0" | |
| 125 "Content-type: text/html\0" | |
| 126 "\0"; | |
| 127 | |
| 128 const char expected_crx_url[] = | 123 const char expected_crx_url[] = |
| 129 "http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"; | 124 "http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 130 | 125 |
| 131 } // namespace | 126 } // namespace |
| 132 | 127 |
| 133 // Common fixture for all the component updater tests. | 128 // Common fixture for all the component updater tests. |
| 134 class ComponentUpdaterTest : public testing::Test { | 129 class ComponentUpdaterTest : public testing::Test { |
| 135 public: | 130 public: |
| 136 enum TestComponents { | 131 enum TestComponents { |
| 137 kTestComponent_abag, | 132 kTestComponent_abag, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 // are generated. | 233 // are generated. |
| 239 TEST_F(ComponentUpdaterTest, CheckCrxSleep) { | 234 TEST_F(ComponentUpdaterTest, CheckCrxSleep) { |
| 240 MessageLoop message_loop; | 235 MessageLoop message_loop; |
| 241 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 236 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
| 242 content::TestBrowserThread file_thread(BrowserThread::FILE); | 237 content::TestBrowserThread file_thread(BrowserThread::FILE); |
| 243 content::TestBrowserThread io_thread(BrowserThread::IO); | 238 content::TestBrowserThread io_thread(BrowserThread::IO); |
| 244 | 239 |
| 245 io_thread.StartIOThread(); | 240 io_thread.StartIOThread(); |
| 246 file_thread.Start(); | 241 file_thread.Start(); |
| 247 | 242 |
| 248 scoped_refptr<ComponentUpdateInterceptor> | 243 content::URLRequestPrepackagedInterceptor interceptor; |
| 249 interceptor(new ComponentUpdateInterceptor()); | |
| 250 | 244 |
| 251 CrxComponent com; | 245 CrxComponent com; |
| 252 RegisterComponent(&com, kTestComponent_abag, Version("1.1")); | 246 RegisterComponent(&com, kTestComponent_abag, Version("1.1")); |
| 253 | 247 |
| 254 const char expected_update_url[] = | 248 const GURL expected_update_url( |
| 255 "http://localhost/upd?extra=foo&x=id%3D" | 249 "http://localhost/upd?extra=foo&x=id%3D" |
| 256 "abagagagagagagagagagagagagagagag%26v%3D1.1%26uc"; | 250 "abagagagagagagagagagagagagagagag%26v%3D1.1%26uc"); |
| 257 | 251 |
| 258 interceptor->SetResponse(expected_update_url, | 252 interceptor.SetResponse(expected_update_url, |
| 259 header_ok_reply, | 253 test_file("updatecheck_reply_1.xml")); |
| 260 test_file("updatecheck_reply_1.xml")); | |
| 261 | 254 |
| 262 // We loop twice, but there are no updates so we expect two sleep messages. | 255 // We loop twice, but there are no updates so we expect two sleep messages. |
| 263 test_configurator()->SetLoopCount(2); | 256 test_configurator()->SetLoopCount(2); |
| 264 component_updater()->Start(); | 257 component_updater()->Start(); |
| 265 | 258 |
| 266 ASSERT_EQ(1ul, notification_tracker().size()); | 259 ASSERT_EQ(1ul, notification_tracker().size()); |
| 267 TestNotificationTracker::Event ev1 = notification_tracker().at(0); | 260 TestNotificationTracker::Event ev1 = notification_tracker().at(0); |
| 268 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type); | 261 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type); |
| 269 | 262 |
| 270 message_loop.Run(); | 263 message_loop.Run(); |
| 271 | 264 |
| 272 ASSERT_EQ(3ul, notification_tracker().size()); | 265 ASSERT_EQ(3ul, notification_tracker().size()); |
| 273 TestNotificationTracker::Event ev2 = notification_tracker().at(1); | 266 TestNotificationTracker::Event ev2 = notification_tracker().at(1); |
| 274 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); | 267 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 275 TestNotificationTracker::Event ev3 = notification_tracker().at(2); | 268 TestNotificationTracker::Event ev3 = notification_tracker().at(2); |
| 276 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); | 269 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 277 EXPECT_EQ(2, interceptor->hit_count()); | 270 EXPECT_EQ(2, interceptor.GetHitCount()); |
| 278 | 271 |
| 279 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); | 272 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); |
| 280 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); | 273 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); |
| 281 | 274 |
| 282 component_updater()->Stop(); | 275 component_updater()->Stop(); |
| 283 | 276 |
| 284 // Loop twice again but this case we simulate a server error by returning | 277 // Loop twice again but this case we simulate a server error by returning |
| 285 // an empty file. | 278 // an empty file. |
| 286 | 279 |
| 287 interceptor->SetResponse(expected_update_url, | 280 interceptor.SetResponse(expected_update_url, |
| 288 header_ok_reply, | 281 test_file("updatecheck_reply_empty")); |
| 289 test_file("updatecheck_reply_empty")); | |
| 290 | 282 |
| 291 notification_tracker().Reset(); | 283 notification_tracker().Reset(); |
| 292 test_configurator()->SetLoopCount(2); | 284 test_configurator()->SetLoopCount(2); |
| 293 component_updater()->Start(); | 285 component_updater()->Start(); |
| 294 | 286 |
| 295 message_loop.Run(); | 287 message_loop.Run(); |
| 296 | 288 |
| 297 ASSERT_EQ(3ul, notification_tracker().size()); | 289 ASSERT_EQ(3ul, notification_tracker().size()); |
| 298 ev1 = notification_tracker().at(0); | 290 ev1 = notification_tracker().at(0); |
| 299 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type); | 291 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type); |
| 300 ev2 = notification_tracker().at(1); | 292 ev2 = notification_tracker().at(1); |
| 301 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); | 293 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 302 ev3 = notification_tracker().at(2); | 294 ev3 = notification_tracker().at(2); |
| 303 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); | 295 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type); |
| 304 EXPECT_EQ(4, interceptor->hit_count()); | 296 EXPECT_EQ(4, interceptor.GetHitCount()); |
| 305 | 297 |
| 306 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); | 298 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); |
| 307 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); | 299 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); |
| 308 | 300 |
| 309 component_updater()->Stop(); | 301 component_updater()->Stop(); |
| 310 } | 302 } |
| 311 | 303 |
| 312 // Verify that we can check for updates and install one component. Besides | 304 // Verify that we can check for updates and install one component. Besides |
| 313 // the notifications above NOTIFICATION_COMPONENT_UPDATE_FOUND and | 305 // the notifications above NOTIFICATION_COMPONENT_UPDATE_FOUND and |
| 314 // NOTIFICATION_COMPONENT_UPDATE_READY should have been fired. We do two loops | 306 // NOTIFICATION_COMPONENT_UPDATE_READY should have been fired. We do two loops |
| 315 // so the second time around there should be nothing left to do. | 307 // so the second time around there should be nothing left to do. |
| 316 // We also check that only 3 network requests are issued: | 308 // We also check that only 3 network requests are issued: |
| 317 // 1- manifest check | 309 // 1- manifest check |
| 318 // 2- download crx | 310 // 2- download crx |
| 319 // 3- second manifest check. | 311 // 3- second manifest check. |
| 320 TEST_F(ComponentUpdaterTest, InstallCrx) { | 312 TEST_F(ComponentUpdaterTest, InstallCrx) { |
| 321 MessageLoop message_loop; | 313 MessageLoop message_loop; |
| 322 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 314 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
| 323 content::TestBrowserThread file_thread(BrowserThread::FILE); | 315 content::TestBrowserThread file_thread(BrowserThread::FILE); |
| 324 content::TestBrowserThread io_thread(BrowserThread::IO); | 316 content::TestBrowserThread io_thread(BrowserThread::IO); |
| 325 | 317 |
| 326 io_thread.StartIOThread(); | 318 io_thread.StartIOThread(); |
| 327 file_thread.Start(); | 319 file_thread.Start(); |
| 328 | 320 |
| 329 scoped_refptr<ComponentUpdateInterceptor> | 321 content::URLRequestPrepackagedInterceptor interceptor; |
| 330 interceptor(new ComponentUpdateInterceptor()); | |
| 331 | 322 |
| 332 CrxComponent com1; | 323 CrxComponent com1; |
| 333 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9")); | 324 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9")); |
| 334 CrxComponent com2; | 325 CrxComponent com2; |
| 335 RegisterComponent(&com2, kTestComponent_abag, Version("2.2")); | 326 RegisterComponent(&com2, kTestComponent_abag, Version("2.2")); |
| 336 | 327 |
| 337 const char expected_update_url_1[] = | 328 const GURL expected_update_url_1( |
| 338 "http://localhost/upd?extra=foo&x=id%3D" | 329 "http://localhost/upd?extra=foo&x=id%3D" |
| 339 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc&x=id%3D" | 330 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc&x=id%3D" |
| 340 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"; | 331 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc"); |
| 341 | 332 |
| 342 const char expected_update_url_2[] = | 333 const GURL expected_update_url_2( |
| 343 "http://localhost/upd?extra=foo&x=id%3D" | 334 "http://localhost/upd?extra=foo&x=id%3D" |
| 344 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc&x=id%3D" | 335 "abagagagagagagagagagagagagagagag%26v%3D2.2%26uc&x=id%3D" |
| 345 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26uc"; | 336 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26uc"); |
| 346 | 337 |
| 347 interceptor->SetResponse(expected_update_url_1, header_ok_reply, | 338 interceptor.SetResponse(expected_update_url_1, |
| 348 test_file("updatecheck_reply_1.xml")); | 339 test_file("updatecheck_reply_1.xml")); |
| 349 interceptor->SetResponse(expected_update_url_2, header_ok_reply, | 340 interceptor.SetResponse(expected_update_url_2, |
| 350 test_file("updatecheck_reply_1.xml")); | 341 test_file("updatecheck_reply_1.xml")); |
| 351 interceptor->SetResponse(expected_crx_url, header_ok_reply, | 342 interceptor.SetResponse(GURL(expected_crx_url), |
| 352 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 343 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 353 | 344 |
| 354 test_configurator()->SetLoopCount(2); | 345 test_configurator()->SetLoopCount(2); |
| 355 | 346 |
| 356 component_updater()->Start(); | 347 component_updater()->Start(); |
| 357 message_loop.Run(); | 348 message_loop.Run(); |
| 358 | 349 |
| 359 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); | 350 EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error()); |
| 360 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count()); | 351 EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count()); |
| 361 EXPECT_EQ(3, interceptor->hit_count()); | 352 EXPECT_EQ(3, interceptor.GetHitCount()); |
| 362 | 353 |
| 363 ASSERT_EQ(5ul, notification_tracker().size()); | 354 ASSERT_EQ(5ul, notification_tracker().size()); |
| 364 | 355 |
| 365 TestNotificationTracker::Event ev1 = notification_tracker().at(1); | 356 TestNotificationTracker::Event ev1 = notification_tracker().at(1); |
| 366 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type); | 357 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type); |
| 367 | 358 |
| 368 TestNotificationTracker::Event ev2 = notification_tracker().at(2); | 359 TestNotificationTracker::Event ev2 = notification_tracker().at(2); |
| 369 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type); | 360 EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type); |
| 370 | 361 |
| 371 TestNotificationTracker::Event ev3 = notification_tracker().at(3); | 362 TestNotificationTracker::Event ev3 = notification_tracker().at(3); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 382 // version is much higher than of chrome. | 373 // version is much higher than of chrome. |
| 383 TEST_F(ComponentUpdaterTest, ProdVersionCheck) { | 374 TEST_F(ComponentUpdaterTest, ProdVersionCheck) { |
| 384 MessageLoop message_loop; | 375 MessageLoop message_loop; |
| 385 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | 376 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); |
| 386 content::TestBrowserThread file_thread(BrowserThread::FILE); | 377 content::TestBrowserThread file_thread(BrowserThread::FILE); |
| 387 content::TestBrowserThread io_thread(BrowserThread::IO); | 378 content::TestBrowserThread io_thread(BrowserThread::IO); |
| 388 | 379 |
| 389 io_thread.StartIOThread(); | 380 io_thread.StartIOThread(); |
| 390 file_thread.Start(); | 381 file_thread.Start(); |
| 391 | 382 |
| 392 scoped_refptr<ComponentUpdateInterceptor> | 383 content::URLRequestPrepackagedInterceptor interceptor; |
| 393 interceptor(new ComponentUpdateInterceptor()); | |
| 394 | 384 |
| 395 CrxComponent com; | 385 CrxComponent com; |
| 396 RegisterComponent(&com, kTestComponent_jebg, Version("0.9")); | 386 RegisterComponent(&com, kTestComponent_jebg, Version("0.9")); |
| 397 | 387 |
| 398 const char expected_update_url[] = | 388 const GURL expected_update_url( |
| 399 "http://localhost/upd?extra=foo&x=id%3D" | 389 "http://localhost/upd?extra=foo&x=id%3D" |
| 400 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc"; | 390 "jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26uc"); |
| 401 | 391 |
| 402 interceptor->SetResponse(expected_update_url, | 392 interceptor.SetResponse(expected_update_url, |
| 403 header_ok_reply, | 393 test_file("updatecheck_reply_2.xml")); |
| 404 test_file("updatecheck_reply_2.xml")); | 394 interceptor.SetResponse(GURL(expected_crx_url), |
| 405 interceptor->SetResponse(expected_crx_url, header_ok_reply, | 395 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 406 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 407 | 396 |
| 408 test_configurator()->SetLoopCount(1); | 397 test_configurator()->SetLoopCount(1); |
| 409 component_updater()->Start(); | 398 component_updater()->Start(); |
| 410 message_loop.Run(); | 399 message_loop.Run(); |
| 411 | 400 |
| 412 EXPECT_EQ(1, interceptor->hit_count()); | 401 EXPECT_EQ(1, interceptor.GetHitCount()); |
| 413 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); | 402 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error()); |
| 414 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); | 403 EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count()); |
| 415 | 404 |
| 416 component_updater()->Stop(); | 405 component_updater()->Stop(); |
| 417 } | 406 } |
| OLD | NEW |