PageRenderTime 1732ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/components/component_updater/component_updater_service_unittest.cc

https://gitlab.com/0072016/Facebook-SDK-
C++ | 385 lines | 294 code | 81 blank | 10 comment | 3 complexity | 4489d232c2955fe4784fde3ea1c08519 MD5 | raw file
  1. // Copyright 2015 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. #include <limits>
  5. #include <string>
  6. #include <vector>
  7. #include "base/bind.h"
  8. #include "base/bind_helpers.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "base/macros.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/scoped_ptr.h"
  14. #include "base/message_loop/message_loop.h"
  15. #include "base/run_loop.h"
  16. #include "base/test/histogram_tester.h"
  17. #include "base/test/sequenced_worker_pool_owner.h"
  18. #include "base/thread_task_runner_handle.h"
  19. #include "base/values.h"
  20. #include "components/component_updater/component_updater_service.h"
  21. #include "components/component_updater/component_updater_service_internal.h"
  22. #include "components/update_client/test_configurator.h"
  23. #include "components/update_client/test_installer.h"
  24. #include "components/update_client/update_client.h"
  25. #include "testing/gmock/include/gmock/gmock.h"
  26. #include "testing/gtest/include/gtest/gtest.h"
  27. using Configurator = update_client::Configurator;
  28. using TestConfigurator = update_client::TestConfigurator;
  29. using UpdateClient = update_client::UpdateClient;
  30. using ::testing::_;
  31. using ::testing::AnyNumber;
  32. using ::testing::Invoke;
  33. using ::testing::Mock;
  34. using ::testing::Return;
  35. namespace component_updater {
  36. class MockInstaller : public CrxInstaller {
  37. public:
  38. MockInstaller();
  39. MOCK_METHOD1(OnUpdateError, void(int error));
  40. MOCK_METHOD2(Install,
  41. bool(const base::DictionaryValue& manifest,
  42. const base::FilePath& unpack_path));
  43. MOCK_METHOD2(GetInstalledFile,
  44. bool(const std::string& file, base::FilePath* installed_file));
  45. MOCK_METHOD0(Uninstall, bool());
  46. private:
  47. ~MockInstaller() override;
  48. };
  49. class MockUpdateClient : public UpdateClient {
  50. public:
  51. MockUpdateClient();
  52. MOCK_METHOD1(AddObserver, void(Observer* observer));
  53. MOCK_METHOD1(RemoveObserver, void(Observer* observer));
  54. MOCK_METHOD3(Install,
  55. void(const std::string& id,
  56. const CrxDataCallback& crx_data_callback,
  57. const CompletionCallback& completion_callback));
  58. MOCK_METHOD3(Update,
  59. void(const std::vector<std::string>& ids,
  60. const CrxDataCallback& crx_data_callback,
  61. const CompletionCallback& completion_callback));
  62. MOCK_CONST_METHOD2(GetCrxUpdateState,
  63. bool(const std::string& id, CrxUpdateItem* update_item));
  64. MOCK_CONST_METHOD1(IsUpdating, bool(const std::string& id));
  65. MOCK_METHOD0(Stop, void());
  66. MOCK_METHOD3(SendUninstallPing,
  67. void(const std::string& id, const Version& version, int reason));
  68. private:
  69. ~MockUpdateClient() override;
  70. };
  71. class MockServiceObserver : public ServiceObserver {
  72. public:
  73. MockServiceObserver();
  74. ~MockServiceObserver() override;
  75. MOCK_METHOD2(OnEvent, void(Events event, const std::string&));
  76. };
  77. class ComponentUpdaterTest : public testing::Test {
  78. public:
  79. ComponentUpdaterTest();
  80. ~ComponentUpdaterTest() override;
  81. void SetUp() override;
  82. void TearDown() override;
  83. // Makes the full path to a component updater test file.
  84. const base::FilePath test_file(const char* file);
  85. MockUpdateClient& update_client() { return *update_client_; }
  86. ComponentUpdateService& component_updater() { return *component_updater_; }
  87. scoped_refptr<TestConfigurator> configurator() const { return config_; }
  88. base::Closure quit_closure() const { return quit_closure_; }
  89. static void ReadyCallback() {}
  90. protected:
  91. void RunThreads();
  92. private:
  93. static const int kNumWorkerThreads_ = 2;
  94. base::MessageLoopForUI message_loop_;
  95. base::RunLoop runloop_;
  96. base::Closure quit_closure_;
  97. scoped_ptr<base::SequencedWorkerPoolOwner> worker_pool_;
  98. scoped_refptr<TestConfigurator> config_;
  99. scoped_refptr<MockUpdateClient> update_client_;
  100. scoped_ptr<ComponentUpdateService> component_updater_;
  101. DISALLOW_COPY_AND_ASSIGN(ComponentUpdaterTest);
  102. };
  103. class OnDemandTester {
  104. public:
  105. static bool OnDemand(ComponentUpdateService* cus, const std::string& id);
  106. };
  107. MockInstaller::MockInstaller() {
  108. }
  109. MockInstaller::~MockInstaller() {
  110. }
  111. MockUpdateClient::MockUpdateClient() {
  112. }
  113. MockUpdateClient::~MockUpdateClient() {
  114. }
  115. MockServiceObserver::MockServiceObserver() {
  116. }
  117. MockServiceObserver::~MockServiceObserver() {
  118. }
  119. bool OnDemandTester::OnDemand(ComponentUpdateService* cus,
  120. const std::string& id) {
  121. return cus->GetOnDemandUpdater().OnDemandUpdate(id);
  122. }
  123. scoped_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory(
  124. const scoped_refptr<Configurator>& config) {
  125. DCHECK(config);
  126. return scoped_ptr<ComponentUpdateService>(
  127. new CrxUpdateService(config, new MockUpdateClient()));
  128. }
  129. ComponentUpdaterTest::ComponentUpdaterTest()
  130. : worker_pool_(
  131. new base::SequencedWorkerPoolOwner(kNumWorkerThreads_, "test")) {
  132. quit_closure_ = runloop_.QuitClosure();
  133. auto pool = worker_pool_->pool();
  134. config_ = new TestConfigurator(
  135. pool->GetSequencedTaskRunner(pool->GetSequenceToken()),
  136. message_loop_.task_runner());
  137. update_client_ = new MockUpdateClient();
  138. EXPECT_CALL(update_client(), AddObserver(_)).Times(1);
  139. component_updater_.reset(new CrxUpdateService(config_, update_client_));
  140. }
  141. ComponentUpdaterTest::~ComponentUpdaterTest() {
  142. EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1);
  143. component_updater_.reset();
  144. }
  145. void ComponentUpdaterTest::SetUp() {
  146. }
  147. void ComponentUpdaterTest::TearDown() {
  148. }
  149. void ComponentUpdaterTest::RunThreads() {
  150. runloop_.Run();
  151. }
  152. TEST_F(ComponentUpdaterTest, AddObserver) {
  153. MockServiceObserver observer;
  154. EXPECT_CALL(update_client(), AddObserver(&observer)).Times(1);
  155. EXPECT_CALL(update_client(), Stop()).Times(1);
  156. component_updater().AddObserver(&observer);
  157. }
  158. TEST_F(ComponentUpdaterTest, RemoveObserver) {
  159. MockServiceObserver observer;
  160. EXPECT_CALL(update_client(), RemoveObserver(&observer)).Times(1);
  161. EXPECT_CALL(update_client(), Stop()).Times(1);
  162. component_updater().RemoveObserver(&observer);
  163. }
  164. // Tests that UpdateClient::Update is called by the timer loop when
  165. // components are registered, and the component update starts.
  166. // Also tests that Uninstall is called when a component is unregistered.
  167. TEST_F(ComponentUpdaterTest, RegisterComponent) {
  168. class LoopHandler {
  169. public:
  170. LoopHandler(int max_cnt, const base::Closure& quit_closure)
  171. : max_cnt_(max_cnt), quit_closure_(quit_closure) {}
  172. void OnUpdate(const std::vector<std::string>& ids,
  173. const UpdateClient::CrxDataCallback& crx_data_callback,
  174. const UpdateClient::CompletionCallback& completion_callback) {
  175. completion_callback.Run(0);
  176. static int cnt = 0;
  177. ++cnt;
  178. if (cnt >= max_cnt_)
  179. quit_closure_.Run();
  180. }
  181. private:
  182. const int max_cnt_;
  183. base::Closure quit_closure_;
  184. };
  185. base::HistogramTester ht;
  186. scoped_refptr<MockInstaller> installer(new MockInstaller());
  187. EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true));
  188. using update_client::jebg_hash;
  189. using update_client::abag_hash;
  190. const std::string id1 = "abagagagagagagagagagagagagagagag";
  191. const std::string id2 = "jebgalgnebhfojomionfpkfelancnnkf";
  192. std::vector<std::string> ids;
  193. ids.push_back(id1);
  194. ids.push_back(id2);
  195. CrxComponent crx_component1;
  196. crx_component1.pk_hash.assign(abag_hash, abag_hash + arraysize(abag_hash));
  197. crx_component1.version = Version("1.0");
  198. crx_component1.installer = installer;
  199. CrxComponent crx_component2;
  200. crx_component2.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
  201. crx_component2.version = Version("0.9");
  202. crx_component2.installer = installer;
  203. // Quit after two update checks have fired.
  204. LoopHandler loop_handler(2, quit_closure());
  205. EXPECT_CALL(update_client(), Update(ids, _, _))
  206. .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate));
  207. EXPECT_CALL(update_client(), IsUpdating(id1)).Times(1);
  208. EXPECT_CALL(update_client(), Stop()).Times(1);
  209. EXPECT_TRUE(component_updater().RegisterComponent(crx_component1));
  210. EXPECT_TRUE(component_updater().RegisterComponent(crx_component2));
  211. RunThreads();
  212. EXPECT_TRUE(component_updater().UnregisterComponent(id1));
  213. ht.ExpectUniqueSample("ComponentUpdater.Calls", 1, 2);
  214. ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2);
  215. ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2);
  216. }
  217. // Tests that on-demand updates invoke UpdateClient::Install.
  218. TEST_F(ComponentUpdaterTest, OnDemandUpdate) {
  219. class LoopHandler {
  220. public:
  221. LoopHandler(int max_cnt, const base::Closure& quit_closure)
  222. : max_cnt_(max_cnt), quit_closure_(quit_closure) {}
  223. void OnInstall(
  224. const std::string& ids,
  225. const UpdateClient::CrxDataCallback& crx_data_callback,
  226. const UpdateClient::CompletionCallback& completion_callback) {
  227. completion_callback.Run(0);
  228. static int cnt = 0;
  229. ++cnt;
  230. if (cnt >= max_cnt_)
  231. quit_closure_.Run();
  232. }
  233. private:
  234. const int max_cnt_;
  235. base::Closure quit_closure_;
  236. };
  237. base::HistogramTester ht;
  238. auto config = configurator();
  239. config->SetInitialDelay(3600);
  240. auto& cus = component_updater();
  241. const std::string id = "jebgalgnebhfojomionfpkfelancnnkf";
  242. EXPECT_FALSE(OnDemandTester::OnDemand(&cus, id));
  243. scoped_refptr<MockInstaller> installer(new MockInstaller());
  244. using update_client::jebg_hash;
  245. CrxComponent crx_component;
  246. crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
  247. crx_component.version = Version("0.9");
  248. crx_component.installer = installer;
  249. LoopHandler loop_handler(1, quit_closure());
  250. EXPECT_CALL(update_client(),
  251. Install("jebgalgnebhfojomionfpkfelancnnkf", _, _))
  252. .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall));
  253. EXPECT_CALL(update_client(), Stop()).Times(1);
  254. EXPECT_TRUE(cus.RegisterComponent(crx_component));
  255. EXPECT_TRUE(OnDemandTester::OnDemand(&cus, id));
  256. RunThreads();
  257. ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1);
  258. ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1);
  259. ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1);
  260. }
  261. // Tests that throttling an update invokes UpdateClient::Install.
  262. TEST_F(ComponentUpdaterTest, MaybeThrottle) {
  263. class LoopHandler {
  264. public:
  265. LoopHandler(int max_cnt, const base::Closure& quit_closure)
  266. : max_cnt_(max_cnt), quit_closure_(quit_closure) {}
  267. void OnInstall(
  268. const std::string& ids,
  269. const UpdateClient::CrxDataCallback& crx_data_callback,
  270. const UpdateClient::CompletionCallback& completion_callback) {
  271. completion_callback.Run(0);
  272. static int cnt = 0;
  273. ++cnt;
  274. if (cnt >= max_cnt_)
  275. quit_closure_.Run();
  276. }
  277. private:
  278. const int max_cnt_;
  279. base::Closure quit_closure_;
  280. };
  281. base::HistogramTester ht;
  282. auto config = configurator();
  283. config->SetInitialDelay(3600);
  284. scoped_refptr<MockInstaller> installer(new MockInstaller());
  285. using update_client::jebg_hash;
  286. CrxComponent crx_component;
  287. crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
  288. crx_component.version = Version("0.9");
  289. crx_component.installer = installer;
  290. LoopHandler loop_handler(1, quit_closure());
  291. EXPECT_CALL(update_client(),
  292. Install("jebgalgnebhfojomionfpkfelancnnkf", _, _))
  293. .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall));
  294. EXPECT_CALL(update_client(), Stop()).Times(1);
  295. EXPECT_TRUE(component_updater().RegisterComponent(crx_component));
  296. component_updater().MaybeThrottle(
  297. "jebgalgnebhfojomionfpkfelancnnkf",
  298. base::Bind(&ComponentUpdaterTest::ReadyCallback));
  299. RunThreads();
  300. ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1);
  301. ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1);
  302. ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1);
  303. }
  304. } // namespace component_updater