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