aw_component_installer_policy_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2021 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 "android_webview/nonembedded/component_updater/aw_component_installer_policy.h"
  5. #include <stdint.h>
  6. #include <iterator>
  7. #include <memory>
  8. #include <utility>
  9. #include "android_webview/common/aw_paths.h"
  10. #include "base/android/path_utils.h"
  11. #include "base/files/file_enumerator.h"
  12. #include "base/files/file_util.h"
  13. #include "base/files/scoped_temp_dir.h"
  14. #include "base/test/scoped_path_override.h"
  15. #include "base/values.h"
  16. #include "base/version.h"
  17. #include "testing/gmock/include/gmock/gmock.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. namespace android_webview {
  20. namespace {
  21. constexpr char kComponentId[] = "jebgalgnebhfojomionfpkfelancnnkf";
  22. // This hash corresponds to kComponentId.
  23. constexpr uint8_t kSha256Hash[] = {
  24. 0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec, 0x8e, 0xd5, 0xfa,
  25. 0x54, 0xb0, 0xd2, 0xdd, 0xa5, 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47,
  26. 0xf6, 0xc4, 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40};
  27. base::Value test_manifest(const base::Version& version) {
  28. base::Value manifest(base::Value::Type::DICTIONARY);
  29. manifest.SetStringKey("version", version.GetString());
  30. return manifest;
  31. }
  32. void CreateTestFiles(const base::FilePath& install_dir) {
  33. base::CreateDirectory(install_dir);
  34. ASSERT_TRUE(base::WriteFile(install_dir.AppendASCII("file1.txt"), "1"));
  35. ASSERT_TRUE(base::WriteFile(install_dir.AppendASCII("file2.txt"), "2"));
  36. ASSERT_TRUE(base::CreateDirectory(install_dir.AppendASCII("sub_dir")));
  37. ASSERT_TRUE(base::WriteFile(
  38. install_dir.AppendASCII("sub_dir").AppendASCII("file3.txt"), "3"));
  39. }
  40. void AssertTestFiles(const base::FilePath& install_dir) {
  41. EXPECT_TRUE(base::PathExists(install_dir.AppendASCII("file1.txt")));
  42. EXPECT_TRUE(base::PathExists(install_dir.AppendASCII("file2.txt")));
  43. EXPECT_TRUE(base::DirectoryExists(install_dir.AppendASCII("sub_dir")));
  44. EXPECT_TRUE(base::PathExists(
  45. install_dir.AppendASCII("sub_dir").AppendASCII("file3.txt")));
  46. }
  47. } // namespace
  48. class TestAwComponentInstallerPolicy : public AwComponentInstallerPolicy {
  49. public:
  50. TestAwComponentInstallerPolicy() {
  51. ON_CALL(*this, GetHash).WillByDefault([](std::vector<uint8_t>* hash) {
  52. hash->assign(std::begin(kSha256Hash), std::end(kSha256Hash));
  53. });
  54. }
  55. ~TestAwComponentInstallerPolicy() override = default;
  56. TestAwComponentInstallerPolicy(const TestAwComponentInstallerPolicy&) =
  57. delete;
  58. TestAwComponentInstallerPolicy& operator=(
  59. const TestAwComponentInstallerPolicy&) = delete;
  60. MOCK_METHOD2(OnCustomInstall,
  61. update_client::CrxInstaller::Result(const base::Value&,
  62. const base::FilePath&));
  63. MOCK_CONST_METHOD2(VerifyInstallation,
  64. bool(const base::Value& manifest,
  65. const base::FilePath& dir));
  66. MOCK_CONST_METHOD0(SupportsGroupPolicyEnabledComponentUpdates, bool());
  67. MOCK_CONST_METHOD0(RequiresNetworkEncryption, bool());
  68. MOCK_CONST_METHOD0(GetRelativeInstallDir, base::FilePath());
  69. MOCK_CONST_METHOD0(GetName, std::string());
  70. MOCK_CONST_METHOD0(GetInstallerAttributes,
  71. update_client::InstallerAttributes());
  72. MOCK_CONST_METHOD1(GetHash, void(std::vector<uint8_t>*));
  73. private:
  74. void IncrementComponentsUpdatedCount() override { /* noop */
  75. }
  76. };
  77. class AwComponentInstallerPolicyTest : public testing::Test {
  78. public:
  79. AwComponentInstallerPolicyTest() = default;
  80. ~AwComponentInstallerPolicyTest() override = default;
  81. AwComponentInstallerPolicyTest(const AwComponentInstallerPolicyTest&) =
  82. delete;
  83. AwComponentInstallerPolicyTest& operator=(
  84. const AwComponentInstallerPolicyTest&) = delete;
  85. // Override from testing::Test
  86. void SetUp() override {
  87. scoped_path_override_ =
  88. std::make_unique<base::ScopedPathOverride>(DIR_COMPONENTS_TEMP);
  89. ASSERT_TRUE(base::android::GetDataDirectory(&cps_component_path_));
  90. cps_component_path_ = cps_component_path_.AppendASCII("components")
  91. .AppendASCII("cps")
  92. .AppendASCII(kComponentId);
  93. ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
  94. CreateTestFiles(GetTestInstallPath());
  95. delegate_ = std::make_unique<TestAwComponentInstallerPolicy>();
  96. }
  97. void TearDown() override {
  98. ASSERT_TRUE(base::DeletePathRecursively(cps_component_path_));
  99. }
  100. base::FilePath GetTestInstallPath() const {
  101. return scoped_temp_dir_.GetPath();
  102. }
  103. protected:
  104. base::FilePath cps_component_path_;
  105. std::unique_ptr<TestAwComponentInstallerPolicy> delegate_;
  106. private:
  107. base::ScopedTempDir scoped_temp_dir_;
  108. std::unique_ptr<base::ScopedPathOverride> scoped_path_override_;
  109. };
  110. TEST_F(AwComponentInstallerPolicyTest, TestNoExistingVersions) {
  111. const base::Version testVersion("1.2.3.4");
  112. delegate_->ComponentReady(testVersion, GetTestInstallPath(),
  113. test_manifest(testVersion));
  114. // Check that the original install path still has files.
  115. AssertTestFiles(GetTestInstallPath());
  116. AssertTestFiles(
  117. cps_component_path_.AppendASCII("1_" + testVersion.GetString()));
  118. }
  119. TEST_F(AwComponentInstallerPolicyTest, TestExistingOtherVersions) {
  120. const base::Version testVersion("1.2.3.4");
  121. CreateTestFiles(cps_component_path_.AppendASCII("1_4.3.2.1"));
  122. CreateTestFiles(cps_component_path_.AppendASCII("10_2.3.4.1"));
  123. delegate_->ComponentReady(testVersion, GetTestInstallPath(),
  124. test_manifest(testVersion));
  125. // Check that the original install path still has files.
  126. AssertTestFiles(GetTestInstallPath());
  127. AssertTestFiles(
  128. cps_component_path_.AppendASCII("11_" + testVersion.GetString()));
  129. }
  130. TEST_F(AwComponentInstallerPolicyTest, TestExistingSameVersion) {
  131. const base::Version testVersion("1.2.3.4");
  132. CreateTestFiles(
  133. cps_component_path_.AppendASCII("5_" + testVersion.GetString()));
  134. delegate_->ComponentReady(testVersion, GetTestInstallPath(),
  135. test_manifest(testVersion));
  136. // Check that the original install path still has files.
  137. AssertTestFiles(GetTestInstallPath());
  138. // Directory should only contain "<component-id>/5/1.2.3.4/", no other files
  139. // or directories should exist.
  140. base::FileEnumerator file_enumerator(
  141. cps_component_path_, /* recursive= */ false,
  142. base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES);
  143. for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
  144. path = file_enumerator.Next()) {
  145. EXPECT_EQ(path.BaseName().MaybeAsASCII(), "5_" + testVersion.GetString());
  146. AssertTestFiles(path);
  147. }
  148. }
  149. } // namespace android_webview