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