test_installer.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include "components/update_client/test_installer.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "base/strings/string_util.h"
  12. #include "base/task/task_traits.h"
  13. #include "base/task/thread_pool.h"
  14. #include "base/threading/sequenced_task_runner_handle.h"
  15. #include "base/values.h"
  16. #include "components/update_client/update_client_errors.h"
  17. #include "components/update_client/utils.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. namespace update_client {
  20. TestInstaller::TestInstaller()
  21. : error_(0),
  22. install_count_(0),
  23. task_runner_(base::SequencedTaskRunnerHandle::Get()) {}
  24. TestInstaller::~TestInstaller() {
  25. // The unpack path is deleted unconditionally by the component state code,
  26. // which is driving this installer. Therefore, the unpack path must not
  27. // exist when this object is destroyed.
  28. if (!unpack_path_.empty())
  29. EXPECT_FALSE(base::DirectoryExists(unpack_path_));
  30. }
  31. void TestInstaller::OnUpdateError(int error) {
  32. error_ = error;
  33. }
  34. void TestInstaller::Install(const base::FilePath& unpack_path,
  35. const std::string& /*public_key*/,
  36. std::unique_ptr<InstallParams> install_params,
  37. ProgressCallback progress_callback,
  38. Callback callback) {
  39. ++install_count_;
  40. unpack_path_ = unpack_path;
  41. install_params_ = std::move(install_params);
  42. InstallComplete(std::move(callback), progress_callback,
  43. Result(InstallError::NONE));
  44. }
  45. void TestInstaller::InstallComplete(Callback callback,
  46. ProgressCallback progress_callback,
  47. const Result& result) {
  48. for (auto sample : installer_progress_samples_) {
  49. progress_callback.Run(sample);
  50. }
  51. task_runner_->PostTask(FROM_HERE,
  52. base::BindOnce(std::move(callback), result));
  53. }
  54. bool TestInstaller::GetInstalledFile(const std::string& file,
  55. base::FilePath* installed_file) {
  56. return false;
  57. }
  58. bool TestInstaller::Uninstall() {
  59. return false;
  60. }
  61. ReadOnlyTestInstaller::ReadOnlyTestInstaller(const base::FilePath& install_dir)
  62. : install_directory_(install_dir) {}
  63. ReadOnlyTestInstaller::~ReadOnlyTestInstaller() = default;
  64. bool ReadOnlyTestInstaller::GetInstalledFile(const std::string& file,
  65. base::FilePath* installed_file) {
  66. *installed_file = install_directory_.AppendASCII(file);
  67. return true;
  68. }
  69. VersionedTestInstaller::VersionedTestInstaller() {
  70. base::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &install_directory_);
  71. }
  72. VersionedTestInstaller::~VersionedTestInstaller() {
  73. base::DeletePathRecursively(install_directory_);
  74. }
  75. void VersionedTestInstaller::Install(
  76. const base::FilePath& unpack_path,
  77. const std::string& public_key,
  78. std::unique_ptr<InstallParams> /*install_params*/,
  79. ProgressCallback progress_callback,
  80. Callback callback) {
  81. const base::Value manifest = update_client::ReadManifest(unpack_path);
  82. const std::string* version_string = manifest.FindStringKey("version");
  83. if (!version_string || !base::IsStringASCII(*version_string))
  84. return;
  85. const base::Version version(*version_string);
  86. const base::FilePath path =
  87. install_directory_.AppendASCII(version.GetString());
  88. base::CreateDirectory(path.DirName());
  89. if (!base::Move(unpack_path, path)) {
  90. InstallComplete(std::move(callback), progress_callback,
  91. Result(InstallError::GENERIC_ERROR));
  92. return;
  93. }
  94. current_version_ = version;
  95. ++install_count_;
  96. InstallComplete(std::move(callback), progress_callback,
  97. Result(InstallError::NONE));
  98. }
  99. bool VersionedTestInstaller::GetInstalledFile(const std::string& file,
  100. base::FilePath* installed_file) {
  101. const base::FilePath path =
  102. install_directory_.AppendASCII(current_version_.GetString());
  103. *installed_file = path.Append(base::FilePath::FromUTF8Unsafe(file));
  104. return true;
  105. }
  106. } // namespace update_client