Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: components/component_updater/default_component_installer_unittest.cc

Issue 2104513003: Implement a test for DefaultComponentInstallerTest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: feedback Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #include <iterator>
6 #include <string>
7 #include <vector>
8
9 #include "base/callback.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/test/sequenced_worker_pool_owner.h"
15 #include "base/version.h"
16 #include "components/component_updater/component_updater_service.h"
17 #include "components/component_updater/component_updater_service_internal.h"
18 #include "components/component_updater/default_component_installer.h"
19 #include "components/update_client/crx_update_item.h"
20 #include "components/update_client/test_configurator.h"
21 #include "components/update_client/update_client.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using Configurator = update_client::Configurator;
26 using CrxUpdateItem = update_client::CrxUpdateItem;
27 using TestConfigurator = update_client::TestConfigurator;
28 using UpdateClient = update_client::UpdateClient;
29
30 using ::testing::_;
31 using ::testing::Invoke;
32
33 namespace component_updater {
34
35 namespace {
36
37 // This hash corresponds to jebgalgnebhfojomionfpkfelancnnkf.crx.
38 const uint8_t kSha256Hash[] = {0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec,
39 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5,
40 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4,
41 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40};
42
43 class MockUpdateClient : public UpdateClient {
44 public:
45 MockUpdateClient() {};
46 MOCK_METHOD1(AddObserver, void(Observer* observer));
47 MOCK_METHOD1(RemoveObserver, void(Observer* observer));
48 MOCK_METHOD3(Install,
49 void(const std::string& id,
50 const CrxDataCallback& crx_data_callback,
51 const CompletionCallback& completion_callback));
52 MOCK_METHOD3(Update,
53 void(const std::vector<std::string>& ids,
54 const CrxDataCallback& crx_data_callback,
55 const CompletionCallback& completion_callback));
56 MOCK_CONST_METHOD2(GetCrxUpdateState,
57 bool(const std::string& id, CrxUpdateItem* update_item));
58 MOCK_CONST_METHOD1(IsUpdating, bool(const std::string& id));
59 MOCK_METHOD0(Stop, void());
60 MOCK_METHOD3(SendUninstallPing,
61 void(const std::string& id, const Version& version, int reason));
62
63 private:
64 ~MockUpdateClient() override {};
65 };
66
67 class FakeInstallerTraits : public ComponentInstallerTraits {
68 public:
69 ~FakeInstallerTraits() override {}
70
71 bool VerifyInstallation(const base::DictionaryValue& manifest,
72 const base::FilePath& dir) const override {
73 return true;
74 }
75
76 bool CanAutoUpdate() const override { return true; }
77
78 bool RequiresNetworkEncryption() const override { return true; }
79
80 bool OnCustomInstall(const base::DictionaryValue& manifest,
81 const base::FilePath& install_dir) override {
82 return true;
83 }
84
85 void ComponentReady(
86 const base::Version& version,
87 const base::FilePath& install_dir,
88 std::unique_ptr<base::DictionaryValue> manifest) override {}
89
90 base::FilePath GetRelativeInstallDir() const override {
91 return base::FilePath(FILE_PATH_LITERAL("fake"));
92 }
93
94 void GetHash(std::vector<uint8_t>* hash) const override { GetPkHash(hash); }
95
96 std::string GetName() const override { return "fake name"; }
97
98 std::string GetAp() const override { return "fake-ap"; }
99
100 private:
101 static void GetPkHash(std::vector<uint8_t>* hash) {
102 hash->assign(std::begin(kSha256Hash), std::end(kSha256Hash));
103 }
104 };
105
106 class DefaultComponentInstallerTest : public testing::Test {
107 public:
108 DefaultComponentInstallerTest();
109 ~DefaultComponentInstallerTest() override;
110
111 MockUpdateClient& update_client() { return *update_client_; }
112 ComponentUpdateService* component_updater() {
113 return component_updater_.get();
114 }
115 scoped_refptr<TestConfigurator> configurator() const { return config_; }
116 base::Closure quit_closure() const { return quit_closure_; }
117
118 protected:
119 void RunThreads();
120
121 private:
122 static const int kNumWorkerThreads_ = 1;
123
124 base::MessageLoopForUI message_loop_;
125 base::RunLoop runloop_;
126 base::Closure quit_closure_;
127
128 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_;
129
130 scoped_refptr<TestConfigurator> config_;
131 scoped_refptr<MockUpdateClient> update_client_;
132 std::unique_ptr<ComponentUpdateService> component_updater_;
133 };
134
135 DefaultComponentInstallerTest::DefaultComponentInstallerTest()
136 : worker_pool_(
137 new base::SequencedWorkerPoolOwner(kNumWorkerThreads_, "test")) {
138 quit_closure_ = runloop_.QuitClosure();
139
140 auto pool = worker_pool_->pool();
141 config_ = new TestConfigurator(
142 pool->GetSequencedTaskRunner(pool->GetSequenceToken()),
143 message_loop_.task_runner());
144
145 update_client_ = new MockUpdateClient();
146 EXPECT_CALL(update_client(), AddObserver(_)).Times(1);
147 component_updater_.reset(new CrxUpdateService(config_, update_client_));
148 }
149
150 DefaultComponentInstallerTest::~DefaultComponentInstallerTest() {
151 EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1);
152 component_updater_.reset();
153 }
154
155 void DefaultComponentInstallerTest::RunThreads() {
156 runloop_.Run();
157 }
158
159 } // namespace
160
161 // Tests that the component metadata is propagated from the default
162 // component installer and its component traits, through the instance of the
163 // CrxComponent, to the component updater service.
164 TEST_F(DefaultComponentInstallerTest, RegisterComponent) {
165 class LoopHandler {
166 public:
167 LoopHandler(int max_cnt, const base::Closure& quit_closure)
168 : max_cnt_(max_cnt), quit_closure_(quit_closure) {}
169
170 void OnUpdate(const std::vector<std::string>& ids,
171 const UpdateClient::CrxDataCallback& crx_data_callback,
172 const UpdateClient::CompletionCallback& completion_callback) {
173 completion_callback.Run(0);
174 static int cnt = 0;
175 ++cnt;
176 if (cnt >= max_cnt_)
177 quit_closure_.Run();
178 }
179
180 private:
181 const int max_cnt_;
182 base::Closure quit_closure_;
183 };
184
185 const std::string id("jebgalgnebhfojomionfpkfelancnnkf");
186
187 // Quit after one update check has been fired.
188 LoopHandler loop_handler(1, quit_closure());
189 EXPECT_CALL(update_client(), Update(_, _, _))
190 .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate));
191
192 EXPECT_CALL(update_client(), GetCrxUpdateState(id, _)).Times(1);
193 EXPECT_CALL(update_client(), Stop()).Times(1);
194
195 std::unique_ptr<ComponentInstallerTraits> traits(new FakeInstallerTraits());
196
197 DefaultComponentInstaller* installer =
198 new DefaultComponentInstaller(std::move(traits));
199 installer->Register(component_updater(), base::Closure());
200
201 RunThreads();
202
203 CrxUpdateItem item;
204 EXPECT_TRUE(component_updater()->GetComponentDetails(id, &item));
205 const CrxComponent& component(item.component);
206 EXPECT_EQ(
207 std::vector<uint8_t>(std::begin(kSha256Hash), std::end(kSha256Hash)),
208 component.pk_hash);
209 EXPECT_EQ(base::Version("0.0.0.0"), component.version);
210 EXPECT_TRUE(component.fingerprint.empty());
211 EXPECT_STREQ("fake name", component.name.c_str());
212 EXPECT_STREQ("fake-ap", component.ap.c_str());
213 EXPECT_TRUE(component.requires_network_encryption);
214 }
215
216 } // namespace component_updater
OLDNEW
« no previous file with comments | « components/component_updater/component_updater_service.h ('k') | components/components_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698