component_updater_service_unittest.h revision 116680a4aac90f2aa7413d9095a592090648e557
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 <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/files/file_path.h"
13#include "base/memory/scoped_ptr.h"
14#include "chrome/browser/component_updater/component_updater_service.h"
15#include "chrome/browser/component_updater/test/test_configurator.h"
16#include "chrome/browser/component_updater/test/url_request_post_interceptor.h"
17#include "content/public/test/test_browser_thread_bundle.h"
18#include "content/test/net/url_request_prepackaged_interceptor.h"
19#include "net/url_request/url_request_test_util.h"
20#include "testing/gmock/include/gmock/gmock.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23namespace component_updater {
24
25class TestInstaller;
26
27// Intercepts HTTP GET requests sent to "localhost".
28typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
29
30// Intercepts HTTP POST requests sent to "localhost2".
31class InterceptorFactory : public URLRequestPostInterceptorFactory {
32 public:
33  InterceptorFactory();
34  ~InterceptorFactory();
35
36  URLRequestPostInterceptor* CreateInterceptor();
37
38 private:
39  DISALLOW_COPY_AND_ASSIGN(InterceptorFactory);
40};
41
42class PartialMatch : public URLRequestPostInterceptor::RequestMatcher {
43 public:
44  explicit PartialMatch(const std::string& expected) : expected_(expected) {}
45  virtual bool Match(const std::string& actual) const OVERRIDE;
46
47 private:
48  const std::string expected_;
49
50  DISALLOW_COPY_AND_ASSIGN(PartialMatch);
51};
52
53// component 1 has extension id "jebgalgnebhfojomionfpkfelancnnkf", and
54// the RSA public key the following hash:
55const uint8 jebg_hash[] = {0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec,
56                           0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5,
57                           0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4,
58                           0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40};
59// component 2 has extension id "abagagagagagagagagagagagagagagag", and
60// the RSA public key the following hash:
61const uint8 abag_hash[] = {0x01, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
62                           0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
63                           0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
64                           0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x01};
65// component 3 has extension id "ihfokbkgjpifnbbojhneepfflplebdkc", and
66// the RSA public key the following hash:
67const uint8 ihfo_hash[] = {0x87, 0x5e, 0xa1, 0xa6, 0x9f, 0x85, 0xd1, 0x1e,
68                           0x97, 0xd4, 0x4f, 0x55, 0xbf, 0xb4, 0x13, 0xa2,
69                           0xe7, 0xc5, 0xc8, 0xf5, 0x60, 0x19, 0x78, 0x1b,
70                           0x6d, 0xe9, 0x4c, 0xeb, 0x96, 0x05, 0x42, 0x17};
71
72class ComponentUpdaterTest : public testing::Test {
73 public:
74  enum TestComponents {
75    kTestComponent_abag,
76    kTestComponent_jebg,
77    kTestComponent_ihfo,
78  };
79
80  ComponentUpdaterTest();
81
82  virtual ~ComponentUpdaterTest();
83
84  virtual void SetUp();
85
86  virtual void TearDown();
87
88  ComponentUpdateService* component_updater();
89
90  // Makes the full path to a component updater test file.
91  const base::FilePath test_file(const char* file);
92
93  TestConfigurator* test_configurator();
94
95  ComponentUpdateService::Status RegisterComponent(CrxComponent* com,
96                                                   TestComponents component,
97                                                   const Version& version,
98                                                   TestInstaller* installer);
99
100 protected:
101  void RunThreads();
102  void RunThreadsUntilIdle();
103
104  scoped_ptr<InterceptorFactory> interceptor_factory_;
105  URLRequestPostInterceptor* post_interceptor_;  // Owned by the factory.
106
107  scoped_ptr<GetInterceptor> get_interceptor_;
108
109 private:
110  TestConfigurator* test_config_;
111  base::FilePath test_data_dir_;
112  content::TestBrowserThreadBundle thread_bundle_;
113  scoped_ptr<ComponentUpdateService> component_updater_;
114};
115
116const char expected_crx_url[] =
117    "http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx";
118
119class MockServiceObserver : public ServiceObserver {
120 public:
121  MockServiceObserver();
122  ~MockServiceObserver();
123  MOCK_METHOD2(OnEvent, void(Events event, const std::string&));
124};
125
126class OnDemandTester {
127 public:
128  static ComponentUpdateService::Status OnDemand(
129      ComponentUpdateService* cus,
130      const std::string& component_id);
131};
132
133}  // namespace component_updater
134
135#endif  // CHROME_BROWSER_COMPONENT_UPDATER_TEST_COMPONENT_UPDATER_SERVICE_UNITTEST_H_
136