component_updater_service_unittest.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 <vector>
13
14#include "base/basictypes.h"
15#include "base/compiler_specific.h"
16#include "base/files/file_path.h"
17#include "base/memory/ref_counted.h"
18#include "base/memory/scoped_ptr.h"
19#include "chrome/browser/component_updater/component_updater_service.h"
20#include "chrome/browser/component_updater/test/component_patcher_mock.h"
21#include "chrome/browser/component_updater/test/url_request_post_interceptor.h"
22#include "content/public/test/test_browser_thread_bundle.h"
23#include "content/test/net/url_request_prepackaged_interceptor.h"
24#include "net/url_request/url_request_test_util.h"
25#include "testing/gmock/include/gmock/gmock.h"
26#include "testing/gtest/include/gtest/gtest.h"
27
28namespace component_updater {
29
30class TestInstaller;
31
32// Intercepts HTTP GET requests sent to "localhost".
33typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
34
35// Intercepts HTTP POST requests sent to "localhost2".
36class InterceptorFactory : public URLRequestPostInterceptorFactory {
37 public:
38  InterceptorFactory();
39  ~InterceptorFactory();
40
41  URLRequestPostInterceptor* CreateInterceptor();
42
43 private:
44  DISALLOW_COPY_AND_ASSIGN(InterceptorFactory);
45};
46
47class PartialMatch : public URLRequestPostInterceptor::RequestMatcher {
48 public:
49  explicit PartialMatch(const std::string& expected) : expected_(expected) {}
50  virtual bool Match(const std::string& actual) const OVERRIDE;
51 private:
52  const std::string expected_;
53
54  DISALLOW_COPY_AND_ASSIGN(PartialMatch);
55};
56
57// component 1 has extension id "jebgalgnebhfojomionfpkfelancnnkf", and
58// the RSA public key the following hash:
59const uint8 jebg_hash[] = {0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec,
60                           0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5,
61                           0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4,
62                           0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40};
63// component 2 has extension id "abagagagagagagagagagagagagagagag", and
64// the RSA public key the following hash:
65const uint8 abag_hash[] = {0x01, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
66                           0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
67                           0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
68                           0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x01};
69// component 3 has extension id "ihfokbkgjpifnbbojhneepfflplebdkc", and
70// the RSA public key the following hash:
71const uint8 ihfo_hash[] = {0x87, 0x5e, 0xa1, 0xa6, 0x9f, 0x85, 0xd1, 0x1e,
72                           0x97, 0xd4, 0x4f, 0x55, 0xbf, 0xb4, 0x13, 0xa2,
73                           0xe7, 0xc5, 0xc8, 0xf5, 0x60, 0x19, 0x78, 0x1b,
74                           0x6d, 0xe9, 0x4c, 0xeb, 0x96, 0x05, 0x42, 0x17};
75
76class TestConfigurator : public ComponentUpdateService::Configurator {
77 public:
78  TestConfigurator();
79  virtual ~TestConfigurator();
80
81  // Overrrides for ComponentUpdateService::Configurator.
82  virtual int InitialDelay() OVERRIDE;
83  virtual int NextCheckDelay() OVERRIDE;
84  virtual int StepDelay() OVERRIDE;
85  virtual int StepDelayMedium() OVERRIDE;
86  virtual int MinimumReCheckWait() OVERRIDE;
87  virtual int OnDemandDelay() OVERRIDE;
88  virtual GURL UpdateUrl() OVERRIDE;
89  virtual GURL PingUrl() OVERRIDE;
90  virtual std::string ExtraRequestParams() OVERRIDE;
91  virtual size_t UrlSizeLimit() OVERRIDE;
92  virtual net::URLRequestContextGetter* RequestContext() OVERRIDE;
93  virtual bool InProcess() OVERRIDE;
94  virtual ComponentPatcher* CreateComponentPatcher() OVERRIDE;
95  virtual bool DeltasEnabled() const OVERRIDE;
96  virtual bool UseBackgroundDownloader() const OVERRIDE;
97
98  typedef std::pair<CrxComponent*, int> CheckAtLoopCount;
99  void SetLoopCount(int times);
100  void SetRecheckTime(int seconds);
101  void SetOnDemandTime(int seconds);
102  void SetComponentUpdateService(ComponentUpdateService* cus);
103  void SetQuitClosure(const base::Closure& quit_closure);
104  void SetInitialDelay(int seconds);
105
106 private:
107  int initial_time_;
108  int times_;
109  int recheck_time_;
110  int ondemand_time_;
111
112  ComponentUpdateService* cus_;
113  scoped_refptr<net::TestURLRequestContextGetter> context_;
114  base::Closure quit_closure_;
115};
116
117class ComponentUpdaterTest : public testing::Test {
118 public:
119  enum TestComponents {
120    kTestComponent_abag,
121    kTestComponent_jebg,
122    kTestComponent_ihfo,
123  };
124
125  ComponentUpdaterTest();
126
127  virtual ~ComponentUpdaterTest();
128
129  virtual void SetUp();
130
131  virtual void TearDown();
132
133  ComponentUpdateService* component_updater();
134
135  // Makes the full path to a component updater test file.
136  const base::FilePath test_file(const char* file);
137
138  TestConfigurator* test_configurator();
139
140  ComponentUpdateService::Status RegisterComponent(CrxComponent* com,
141                                                   TestComponents component,
142                                                   const Version& version,
143                                                   TestInstaller* installer);
144
145 protected:
146  void RunThreads();
147  void RunThreadsUntilIdle();
148
149  scoped_ptr<InterceptorFactory> interceptor_factory_;
150  URLRequestPostInterceptor* post_interceptor_;   // Owned by the factory.
151
152  scoped_ptr<GetInterceptor> get_interceptor_;
153 private:
154  TestConfigurator* test_config_;
155  base::FilePath test_data_dir_;
156  content::TestBrowserThreadBundle thread_bundle_;
157  scoped_ptr<ComponentUpdateService> component_updater_;
158};
159
160const char expected_crx_url[] =
161    "http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx";
162
163class MockComponentObserver : public ComponentObserver {
164 public:
165  MockComponentObserver();
166  ~MockComponentObserver();
167  MOCK_METHOD2(OnEvent, void(Events event, int extra));
168};
169
170class OnDemandTester {
171 public:
172  static ComponentUpdateService::Status OnDemand(
173      ComponentUpdateService* cus, const std::string& component_id);
174};
175
176}  // namespace component_updater
177
178#endif  // CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_UPDATER_SERVICE_UNITTEST_H_
179