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