component_updater_service_unittest.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_UPDATER_SERVICE_UNITTEST_H_ 6#define CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_UPDATER_SERVICE_UNITTEST_H_ 7 8#include <list> 9#include <map> 10#include <string> 11#include <utility> 12#include "base/basictypes.h" 13#include "base/compiler_specific.h" 14#include "base/files/file_path.h" 15#include "base/memory/ref_counted.h" 16#include "base/memory/scoped_ptr.h" 17#include "base/message_loop/message_loop.h" 18#include "chrome/browser/component_updater/component_updater_service.h" 19#include "chrome/browser/component_updater/test/component_patcher_mock.h" 20#include "chrome/browser/component_updater/test/url_request_post_interceptor.h" 21#include "content/public/test/test_browser_thread.h" 22#include "content/public/test/test_notification_tracker.h" 23#include "net/url_request/url_request_test_util.h" 24#include "testing/gmock/include/gmock/gmock.h" 25#include "testing/gtest/include/gtest/gtest.h" 26 27class TestInstaller; 28 29// component 1 has extension id "jebgalgnebhfojomionfpkfelancnnkf", and 30// the RSA public key the following hash: 31const uint8 jebg_hash[] = {0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec, 32 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5, 33 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4, 34 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40}; 35// component 2 has extension id "abagagagagagagagagagagagagagagag", and 36// the RSA public key the following hash: 37const uint8 abag_hash[] = {0x01, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 38 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 39 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 40 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x01}; 41// component 3 has extension id "ihfokbkgjpifnbbojhneepfflplebdkc", and 42// the RSA public key the following hash: 43const uint8 ihfo_hash[] = {0x87, 0x5e, 0xa1, 0xa6, 0x9f, 0x85, 0xd1, 0x1e, 44 0x97, 0xd4, 0x4f, 0x55, 0xbf, 0xb4, 0x13, 0xa2, 45 0xe7, 0xc5, 0xc8, 0xf5, 0x60, 0x19, 0x78, 0x1b, 46 0x6d, 0xe9, 0x4c, 0xeb, 0x96, 0x05, 0x42, 0x17}; 47 48class TestConfigurator : public ComponentUpdateService::Configurator { 49 public: 50 explicit TestConfigurator(); 51 52 virtual ~TestConfigurator(); 53 54 virtual int InitialDelay() OVERRIDE; 55 56 typedef std::pair<CrxComponent*, int> CheckAtLoopCount; 57 58 virtual int NextCheckDelay() OVERRIDE; 59 60 virtual int StepDelay() OVERRIDE; 61 62 virtual int MinimumReCheckWait() OVERRIDE; 63 64 virtual int OnDemandDelay() OVERRIDE; 65 66 virtual GURL UpdateUrl() OVERRIDE; 67 68 virtual GURL PingUrl() OVERRIDE; 69 70 virtual const char* ExtraRequestParams() OVERRIDE; 71 72 virtual size_t UrlSizeLimit() OVERRIDE; 73 74 virtual net::URLRequestContextGetter* RequestContext() OVERRIDE; 75 76 // Don't use the utility process to decode files. 77 virtual bool InProcess() OVERRIDE; 78 79 virtual ComponentPatcher* CreateComponentPatcher() OVERRIDE; 80 81 virtual bool DeltasEnabled() const OVERRIDE; 82 83 void SetLoopCount(int times); 84 85 void SetRecheckTime(int seconds); 86 87 void SetOnDemandTime(int seconds); 88 89 void AddComponentToCheck(CrxComponent* com, int at_loop_iter); 90 91 void SetComponentUpdateService(ComponentUpdateService* cus); 92 93 private: 94 int times_; 95 int recheck_time_; 96 int ondemand_time_; 97 98 std::list<CheckAtLoopCount> components_to_check_; 99 ComponentUpdateService* cus_; 100 scoped_refptr<net::TestURLRequestContextGetter> context_; 101}; 102 103class ComponentUpdaterTest : public testing::Test { 104 public: 105 enum TestComponents { 106 kTestComponent_abag, 107 kTestComponent_jebg, 108 kTestComponent_ihfo, 109 }; 110 111 ComponentUpdaterTest(); 112 113 virtual ~ComponentUpdaterTest(); 114 115 virtual void TearDown(); 116 117 ComponentUpdateService* component_updater(); 118 119 // Makes the full path to a component updater test file. 120 const base::FilePath test_file(const char* file); 121 122 content::TestNotificationTracker& notification_tracker(); 123 124 TestConfigurator* test_configurator(); 125 126 ComponentUpdateService::Status RegisterComponent(CrxComponent* com, 127 TestComponents component, 128 const Version& version, 129 TestInstaller* installer); 130 protected: 131 base::MessageLoop message_loop_; 132 133 private: 134 TestConfigurator* test_config_; 135 base::FilePath test_data_dir_; 136 content::TestNotificationTracker notification_tracker_; 137 content::TestBrowserThread ui_thread_; 138 content::TestBrowserThread file_thread_; 139 content::TestBrowserThread io_thread_; 140 scoped_ptr<ComponentUpdateService> component_updater_; 141}; 142 143const char expected_crx_url[] = 144 "http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"; 145 146class PingChecker : public RequestCounter { 147 public: 148 explicit PingChecker(const std::map<std::string, std::string>& attributes); 149 150 virtual ~PingChecker(); 151 152 virtual void Trial(net::URLRequest* request) OVERRIDE; 153 154 int NumHits() const { 155 return num_hits_; 156 } 157 int NumMisses() const { 158 return num_misses_; 159 } 160 161 private: 162 int num_hits_; 163 int num_misses_; 164 const std::map<std::string, std::string> attributes_; 165 virtual bool Test(net::URLRequest* request); 166}; 167 168class MockComponentObserver : public ComponentObserver { 169 public: 170 MockComponentObserver(); 171 ~MockComponentObserver(); 172 MOCK_METHOD2(OnEvent, void(Events event, int extra)); 173}; 174 175#endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_UPDATER_SERVICE_UNITTEST_H_ 176