[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 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 | |
sorin | b120440b | 2015-04-27 16:34:15 | [diff] [blame] | 5 | #include "components/update_client/test_installer.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 6 | |
[email protected] | 28ea9ac | 2014-05-03 22:07:18 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 11 | #include "base/task/post_task.h" |
| 12 | #include "base/task/task_traits.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 13 | #include "base/values.h" |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 14 | #include "components/update_client/update_client_errors.h" |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 15 | #include "components/update_client/utils.h" |
Sorin Jianu | 23f70f75 | 2017-05-30 16:21:58 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 17 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 18 | namespace update_client { |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 19 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 20 | TestInstaller::TestInstaller() : error_(0), install_count_(0) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 21 | } |
| 22 | |
Sorin Jianu | 23f70f75 | 2017-05-30 16:21:58 | [diff] [blame] | 23 | TestInstaller::~TestInstaller() { |
| 24 | // The unpack path is deleted unconditionally by the component state code, |
| 25 | // which is driving this installer. Therefore, the unpack path must not |
| 26 | // exist when this object is destroyed. |
| 27 | if (!unpack_path_.empty()) |
| 28 | EXPECT_FALSE(base::DirectoryExists(unpack_path_)); |
| 29 | } |
| 30 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 31 | void TestInstaller::OnUpdateError(int error) { |
| 32 | error_ = error; |
| 33 | } |
| 34 | |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 35 | void TestInstaller::Install(const base::FilePath& unpack_path, |
Sorin Jianu | ea5534e9 | 2017-10-27 01:40:28 | [diff] [blame] | 36 | const std::string& /*public_key*/, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 37 | Callback callback) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 38 | ++install_count_; |
Sorin Jianu | 23f70f75 | 2017-05-30 16:21:58 | [diff] [blame] | 39 | unpack_path_ = unpack_path; |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 40 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 41 | InstallComplete(std::move(callback), Result(InstallError::NONE)); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 42 | } |
| 43 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 44 | void TestInstaller::InstallComplete(Callback callback, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 45 | const Result& result) const { |
| 46 | base::PostTaskWithTraits(FROM_HERE, {base::MayBlock()}, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 47 | base::BindOnce(std::move(callback), result)); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool TestInstaller::GetInstalledFile(const std::string& file, |
| 51 | base::FilePath* installed_file) { |
| 52 | return false; |
| 53 | } |
| 54 | |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 55 | bool TestInstaller::Uninstall() { |
| 56 | return false; |
| 57 | } |
| 58 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 59 | ReadOnlyTestInstaller::ReadOnlyTestInstaller(const base::FilePath& install_dir) |
| 60 | : install_directory_(install_dir) { |
| 61 | } |
| 62 | |
| 63 | ReadOnlyTestInstaller::~ReadOnlyTestInstaller() { |
| 64 | } |
| 65 | |
| 66 | bool ReadOnlyTestInstaller::GetInstalledFile(const std::string& file, |
| 67 | base::FilePath* installed_file) { |
| 68 | *installed_file = install_directory_.AppendASCII(file); |
| 69 | return true; |
| 70 | } |
| 71 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 72 | VersionedTestInstaller::VersionedTestInstaller() { |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 73 | base::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &install_directory_); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | VersionedTestInstaller::~VersionedTestInstaller() { |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 77 | base::DeleteFile(install_directory_, true); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 78 | } |
| 79 | |
Sorin Jianu | ea5534e9 | 2017-10-27 01:40:28 | [diff] [blame] | 80 | void VersionedTestInstaller::Install(const base::FilePath& unpack_path, |
| 81 | const std::string& public_key, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 82 | Callback callback) { |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 83 | const auto manifest = update_client::ReadManifest(unpack_path); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 84 | std::string version_string; |
Sorin Jianu | 990ee14 | 2017-06-02 22:34:08 | [diff] [blame] | 85 | manifest->GetStringASCII("version", &version_string); |
Zinovy Nis | e8482f5 | 2018-05-29 06:08:44 | [diff] [blame] | 86 | const base::Version version(version_string); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 87 | |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 88 | const base::FilePath path = |
| 89 | install_directory_.AppendASCII(version.GetString()); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 90 | base::CreateDirectory(path.DirName()); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 91 | if (!base::Move(unpack_path, path)) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 92 | InstallComplete(std::move(callback), Result(InstallError::GENERIC_ERROR)); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 93 | return; |
| 94 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 95 | current_version_ = version; |
| 96 | ++install_count_; |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 97 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 98 | InstallComplete(std::move(callback), Result(InstallError::NONE)); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool VersionedTestInstaller::GetInstalledFile(const std::string& file, |
| 102 | base::FilePath* installed_file) { |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 103 | const base::FilePath path = |
| 104 | install_directory_.AppendASCII(current_version_.GetString()); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 105 | *installed_file = path.Append(base::FilePath::FromUTF8Unsafe(file)); |
| 106 | return true; |
| 107 | } |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 108 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 109 | } // namespace update_client |