| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <memory> | 5 #include <memory> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 public: | 141 public: |
| 142 UpdateClientTest(); | 142 UpdateClientTest(); |
| 143 ~UpdateClientTest() override; | 143 ~UpdateClientTest() override; |
| 144 | 144 |
| 145 protected: | 145 protected: |
| 146 void RunThreads(); | 146 void RunThreads(); |
| 147 | 147 |
| 148 // Returns the full path to a test file. | 148 // Returns the full path to a test file. |
| 149 static base::FilePath TestFilePath(const char* file); | 149 static base::FilePath TestFilePath(const char* file); |
| 150 | 150 |
| 151 scoped_refptr<update_client::Configurator> config() { return config_; } | 151 scoped_refptr<update_client::TestConfigurator> config() { return config_; } |
| 152 update_client::PersistedData* metadata() { return metadata_.get(); } | 152 update_client::PersistedData* metadata() { return metadata_.get(); } |
| 153 | 153 |
| 154 base::Closure quit_closure() { return quit_closure_; } | 154 base::Closure quit_closure() { return quit_closure_; } |
| 155 | 155 |
| 156 private: | 156 private: |
| 157 static const int kNumWorkerThreads_ = 2; | 157 static const int kNumWorkerThreads_ = 2; |
| 158 | 158 |
| 159 base::MessageLoopForUI message_loop_; | 159 base::MessageLoopForUI message_loop_; |
| 160 base::RunLoop runloop_; | 160 base::RunLoop runloop_; |
| 161 base::Closure quit_closure_; | 161 base::Closure quit_closure_; |
| 162 | 162 |
| 163 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_; | 163 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_; |
| 164 | 164 |
| 165 scoped_refptr<update_client::Configurator> config_; | 165 scoped_refptr<update_client::TestConfigurator> config_; |
| 166 std::unique_ptr<TestingPrefServiceSimple> pref_; | 166 std::unique_ptr<TestingPrefServiceSimple> pref_; |
| 167 std::unique_ptr<update_client::PersistedData> metadata_; | 167 std::unique_ptr<update_client::PersistedData> metadata_; |
| 168 | 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(UpdateClientTest); | 169 DISALLOW_COPY_AND_ASSIGN(UpdateClientTest); |
| 170 }; | 170 }; |
| 171 | 171 |
| 172 UpdateClientTest::UpdateClientTest() | 172 UpdateClientTest::UpdateClientTest() |
| 173 : worker_pool_( | 173 : worker_pool_( |
| 174 new base::SequencedWorkerPoolOwner(kNumWorkerThreads_, "test")) { | 174 new base::SequencedWorkerPoolOwner(kNumWorkerThreads_, "test")) { |
| 175 quit_closure_ = runloop_.QuitClosure(); | 175 quit_closure_ = runloop_.QuitClosure(); |
| (...skipping 2253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2429 base::RunLoop runloop; | 2429 base::RunLoop runloop; |
| 2430 update_client->Update( | 2430 update_client->Update( |
| 2431 ids, base::Bind(&DataCallbackFake::Callback), | 2431 ids, base::Bind(&DataCallbackFake::Callback), |
| 2432 base::Bind(&CompletionCallbackFake::Callback, runloop.QuitClosure())); | 2432 base::Bind(&CompletionCallbackFake::Callback, runloop.QuitClosure())); |
| 2433 runloop.Run(); | 2433 runloop.Run(); |
| 2434 } | 2434 } |
| 2435 | 2435 |
| 2436 update_client->RemoveObserver(&observer); | 2436 update_client->RemoveObserver(&observer); |
| 2437 } | 2437 } |
| 2438 | 2438 |
| 2439 // Tests the update check for two CRXs scenario. The first component supports |
| 2440 // the group policy to enable updates, and has its updates disabled. The second |
| 2441 // component has an update. The server does not honor the "updatedisabled" |
| 2442 // attribute and returns updates for both components. |
| 2443 TEST_F(UpdateClientTest, TwoCrxUpdateOneUpdateDisabled) { |
| 2444 class DataCallbackFake { |
| 2445 public: |
| 2446 static void Callback(const std::vector<std::string>& ids, |
| 2447 std::vector<CrxComponent>* components) { |
| 2448 CrxComponent crx1; |
| 2449 crx1.name = "test_jebg"; |
| 2450 crx1.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
| 2451 crx1.version = Version("0.9"); |
| 2452 crx1.installer = new TestInstaller; |
| 2453 crx1.supports_group_policy_enable_component_updates = true; |
| 2454 |
| 2455 CrxComponent crx2; |
| 2456 crx2.name = "test_ihfo"; |
| 2457 crx2.pk_hash.assign(ihfo_hash, ihfo_hash + arraysize(ihfo_hash)); |
| 2458 crx2.version = Version("0.8"); |
| 2459 crx2.installer = new TestInstaller; |
| 2460 |
| 2461 components->push_back(crx1); |
| 2462 components->push_back(crx2); |
| 2463 } |
| 2464 }; |
| 2465 |
| 2466 class CompletionCallbackFake { |
| 2467 public: |
| 2468 static void Callback(const base::Closure& quit_closure, int error) { |
| 2469 EXPECT_EQ(0, error); |
| 2470 quit_closure.Run(); |
| 2471 } |
| 2472 }; |
| 2473 |
| 2474 class FakeUpdateChecker : public UpdateChecker { |
| 2475 public: |
| 2476 static std::unique_ptr<UpdateChecker> Create( |
| 2477 const scoped_refptr<Configurator>& config, |
| 2478 PersistedData* metadata) { |
| 2479 return std::unique_ptr<UpdateChecker>(new FakeUpdateChecker()); |
| 2480 } |
| 2481 |
| 2482 bool CheckForUpdates( |
| 2483 const std::vector<CrxUpdateItem*>& items_to_check, |
| 2484 const std::string& additional_attributes, |
| 2485 const UpdateCheckCallback& update_check_callback) override { |
| 2486 /* |
| 2487 Fake the following response: |
| 2488 |
| 2489 <?xml version='1.0' encoding='UTF-8'?> |
| 2490 <response protocol='3.0'> |
| 2491 <app appid='jebgalgnebhfojomionfpkfelancnnkf'> |
| 2492 <updatecheck status='ok'> |
| 2493 <urls> |
| 2494 <url codebase='http://localhost/download/'/> |
| 2495 </urls> |
| 2496 <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 2497 <packages> |
| 2498 <package name='jebgalgnebhfojomionfpkfelancnnkf.crx' |
| 2499 hash_sha256='6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd |
| 2500 7c9b12cb7cc067667bde87'/> |
| 2501 </packages> |
| 2502 </manifest> |
| 2503 </updatecheck> |
| 2504 </app> |
| 2505 <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'> |
| 2506 <updatecheck status='ok'> |
| 2507 <urls> |
| 2508 <url codebase='http://localhost/download/'/> |
| 2509 </urls> |
| 2510 <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 2511 <packages> |
| 2512 <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx' |
| 2513 hash_sha256='813c59747e139a608b3b5fc49633affc6db574373f |
| 2514 309f156ea6d27229c0b3f9'/> |
| 2515 </packages> |
| 2516 </manifest> |
| 2517 </updatecheck> |
| 2518 </app> |
| 2519 </response> |
| 2520 */ |
| 2521 UpdateResponse::Result::Manifest::Package package1; |
| 2522 package1.name = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 2523 package1.hash_sha256 = |
| 2524 "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
| 2525 |
| 2526 UpdateResponse::Result result1; |
| 2527 result1.extension_id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 2528 result1.crx_urls.push_back(GURL("http://localhost/download/")); |
| 2529 result1.manifest.version = "1.0"; |
| 2530 result1.manifest.browser_min_version = "11.0.1.0"; |
| 2531 result1.manifest.packages.push_back(package1); |
| 2532 |
| 2533 UpdateResponse::Result::Manifest::Package package2; |
| 2534 package2.name = "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"; |
| 2535 package2.hash_sha256 = |
| 2536 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"; |
| 2537 |
| 2538 UpdateResponse::Result result2; |
| 2539 result2.extension_id = "ihfokbkgjpifnbbojhneepfflplebdkc"; |
| 2540 result2.crx_urls.push_back(GURL("http://localhost/download/")); |
| 2541 result2.manifest.version = "1.0"; |
| 2542 result2.manifest.browser_min_version = "11.0.1.0"; |
| 2543 result2.manifest.packages.push_back(package2); |
| 2544 |
| 2545 UpdateResponse::Results results; |
| 2546 results.list.push_back(result1); |
| 2547 results.list.push_back(result2); |
| 2548 |
| 2549 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 2550 FROM_HERE, base::Bind(update_check_callback, 0, results, 0)); |
| 2551 return true; |
| 2552 } |
| 2553 }; |
| 2554 |
| 2555 class FakeCrxDownloader : public CrxDownloader { |
| 2556 public: |
| 2557 static std::unique_ptr<CrxDownloader> Create( |
| 2558 bool is_background_download, |
| 2559 net::URLRequestContextGetter* context_getter, |
| 2560 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 2561 return std::unique_ptr<CrxDownloader>(new FakeCrxDownloader()); |
| 2562 } |
| 2563 |
| 2564 private: |
| 2565 FakeCrxDownloader() |
| 2566 : CrxDownloader(base::ThreadTaskRunnerHandle::Get(), nullptr) {} |
| 2567 ~FakeCrxDownloader() override {} |
| 2568 |
| 2569 void DoStartDownload(const GURL& url) override { |
| 2570 DownloadMetrics download_metrics; |
| 2571 FilePath path; |
| 2572 Result result; |
| 2573 if (url.path() == "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") { |
| 2574 download_metrics.url = url; |
| 2575 download_metrics.downloader = DownloadMetrics::kNone; |
| 2576 download_metrics.error = 0; |
| 2577 download_metrics.downloaded_bytes = 53638; |
| 2578 download_metrics.total_bytes = 53638; |
| 2579 download_metrics.download_time_ms = 2000; |
| 2580 |
| 2581 EXPECT_TRUE(MakeTestFile( |
| 2582 TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path)); |
| 2583 |
| 2584 result.error = 0; |
| 2585 result.response = path; |
| 2586 result.downloaded_bytes = 53638; |
| 2587 result.total_bytes = 53638; |
| 2588 } else { |
| 2589 NOTREACHED(); |
| 2590 } |
| 2591 |
| 2592 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 2593 FROM_HERE, base::Bind(&FakeCrxDownloader::OnDownloadProgress, |
| 2594 base::Unretained(this), result)); |
| 2595 |
| 2596 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 2597 FROM_HERE, |
| 2598 base::Bind(&FakeCrxDownloader::OnDownloadComplete, |
| 2599 base::Unretained(this), true, result, download_metrics)); |
| 2600 } |
| 2601 }; |
| 2602 |
| 2603 class FakePingManager : public FakePingManagerImpl { |
| 2604 public: |
| 2605 explicit FakePingManager(const scoped_refptr<Configurator>& config) |
| 2606 : FakePingManagerImpl(config) {} |
| 2607 ~FakePingManager() override { |
| 2608 const auto& ping_items = items(); |
| 2609 EXPECT_EQ(2U, ping_items.size()); |
| 2610 EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_items[0].id); |
| 2611 EXPECT_EQ(base::Version("0.9"), ping_items[0].previous_version); |
| 2612 EXPECT_EQ(base::Version("1.0"), ping_items[0].next_version); |
| 2613 EXPECT_EQ(4, ping_items[0].error_category); |
| 2614 EXPECT_EQ(2, ping_items[0].error_code); |
| 2615 EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_items[1].id); |
| 2616 EXPECT_EQ(base::Version("0.8"), ping_items[1].previous_version); |
| 2617 EXPECT_EQ(base::Version("1.0"), ping_items[1].next_version); |
| 2618 EXPECT_EQ(0, ping_items[1].error_category); |
| 2619 EXPECT_EQ(0, ping_items[1].error_code); |
| 2620 } |
| 2621 }; |
| 2622 |
| 2623 // Disables updates for the components declaring support for the group policy. |
| 2624 config()->SetEnabledComponentUpdates(false); |
| 2625 std::unique_ptr<FakePingManager> ping_manager(new FakePingManager(config())); |
| 2626 scoped_refptr<UpdateClient> update_client(new UpdateClientImpl( |
| 2627 config(), std::move(ping_manager), &FakeUpdateChecker::Create, |
| 2628 &FakeCrxDownloader::Create)); |
| 2629 |
| 2630 MockObserver observer; |
| 2631 { |
| 2632 InSequence seq; |
| 2633 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2634 "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2635 .Times(1); |
| 2636 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 2637 "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2638 .Times(1); |
| 2639 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 2640 "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2641 .Times(1); |
| 2642 } |
| 2643 { |
| 2644 InSequence seq; |
| 2645 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2646 "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 2647 .Times(1); |
| 2648 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 2649 "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 2650 .Times(1); |
| 2651 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_WAIT, |
| 2652 "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 2653 .Times(1); |
| 2654 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
| 2655 "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 2656 .Times(1); |
| 2657 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 2658 "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 2659 .Times(1); |
| 2660 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 2661 "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 2662 .Times(1); |
| 2663 } |
| 2664 |
| 2665 update_client->AddObserver(&observer); |
| 2666 |
| 2667 std::vector<std::string> ids; |
| 2668 ids.push_back(std::string("jebgalgnebhfojomionfpkfelancnnkf")); |
| 2669 ids.push_back(std::string("ihfokbkgjpifnbbojhneepfflplebdkc")); |
| 2670 |
| 2671 update_client->Update( |
| 2672 ids, base::Bind(&DataCallbackFake::Callback), |
| 2673 base::Bind(&CompletionCallbackFake::Callback, quit_closure())); |
| 2674 |
| 2675 RunThreads(); |
| 2676 |
| 2677 update_client->RemoveObserver(&observer); |
| 2678 } |
| 2679 |
| 2439 } // namespace update_client | 2680 } // namespace update_client |
| OLD | NEW |