blob: d0febe59c411dae9fe4e9248e16683869ed4c8e6 [file] [log] [blame]
[email protected]e3e696d32013-06-21 20:41:361// 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
sorinb120440b2015-04-27 16:34:155#include "components/update_client/test_installer.h"
[email protected]e3e696d32013-06-21 20:41:366
[email protected]28ea9ac2014-05-03 22:07:187#include <string>
8
[email protected]e3e696d32013-06-21 20:41:369#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0410#include "base/files/file_util.h"
Gabriel Charette44db1422018-08-06 11:19:3311#include "base/task/post_task.h"
12#include "base/task/task_traits.h"
[email protected]e3e696d32013-06-21 20:41:3613#include "base/values.h"
sorin2892f7212016-11-07 18:59:4314#include "components/update_client/update_client_errors.h"
Sorin Jianu7aa6d1f2017-10-13 20:29:2915#include "components/update_client/utils.h"
Sorin Jianu23f70f752017-05-30 16:21:5816#include "testing/gtest/include/gtest/gtest.h"
[email protected]e3e696d32013-06-21 20:41:3617
sorin52ac0882015-01-24 01:15:0018namespace update_client {
[email protected]055981f2014-01-17 20:22:3219
[email protected]d0c8b8b42014-05-06 05:11:4520TestInstaller::TestInstaller() : error_(0), install_count_(0) {
[email protected]e3e696d32013-06-21 20:41:3621}
22
Sorin Jianu23f70f752017-05-30 16:21:5823TestInstaller::~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]e3e696d32013-06-21 20:41:3631void TestInstaller::OnUpdateError(int error) {
32 error_ = error;
33}
34
Sorin Jianu7aa6d1f2017-10-13 20:29:2935void TestInstaller::Install(const base::FilePath& unpack_path,
Sorin Jianuea5534e92017-10-27 01:40:2836 const std::string& /*public_key*/,
Sorin Jianua8ef73d2017-11-02 16:55:1737 Callback callback) {
[email protected]e3e696d32013-06-21 20:41:3638 ++install_count_;
Sorin Jianu23f70f752017-05-30 16:21:5839 unpack_path_ = unpack_path;
sorin2892f7212016-11-07 18:59:4340
Sorin Jianua8ef73d2017-11-02 16:55:1741 InstallComplete(std::move(callback), Result(InstallError::NONE));
Sorin Jianuf40ab4b32017-10-06 22:53:4142}
43
Sorin Jianua8ef73d2017-11-02 16:55:1744void TestInstaller::InstallComplete(Callback callback,
Sorin Jianuf40ab4b32017-10-06 22:53:4145 const Result& result) const {
46 base::PostTaskWithTraits(FROM_HERE, {base::MayBlock()},
Sorin Jianua8ef73d2017-11-02 16:55:1747 base::BindOnce(std::move(callback), result));
[email protected]e3e696d32013-06-21 20:41:3648}
49
50bool TestInstaller::GetInstalledFile(const std::string& file,
51 base::FilePath* installed_file) {
52 return false;
53}
54
bauerb1f6657e72015-02-09 00:00:2755bool TestInstaller::Uninstall() {
56 return false;
57}
58
[email protected]e3e696d32013-06-21 20:41:3659ReadOnlyTestInstaller::ReadOnlyTestInstaller(const base::FilePath& install_dir)
60 : install_directory_(install_dir) {
61}
62
63ReadOnlyTestInstaller::~ReadOnlyTestInstaller() {
64}
65
66bool 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]e3e696d32013-06-21 20:41:3672VersionedTestInstaller::VersionedTestInstaller() {
[email protected]03d9afc02013-12-03 17:55:5273 base::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &install_directory_);
[email protected]e3e696d32013-06-21 20:41:3674}
75
76VersionedTestInstaller::~VersionedTestInstaller() {
[email protected]dd3aa792013-07-16 19:10:2377 base::DeleteFile(install_directory_, true);
[email protected]e3e696d32013-06-21 20:41:3678}
79
Sorin Jianuea5534e92017-10-27 01:40:2880void VersionedTestInstaller::Install(const base::FilePath& unpack_path,
81 const std::string& public_key,
Sorin Jianua8ef73d2017-11-02 16:55:1782 Callback callback) {
Sorin Jianu7aa6d1f2017-10-13 20:29:2983 const auto manifest = update_client::ReadManifest(unpack_path);
[email protected]e3e696d32013-06-21 20:41:3684 std::string version_string;
Sorin Jianu990ee142017-06-02 22:34:0885 manifest->GetStringASCII("version", &version_string);
Zinovy Nise8482f52018-05-29 06:08:4486 const base::Version version(version_string);
[email protected]e3e696d32013-06-21 20:41:3687
Sorin Jianu7aa6d1f2017-10-13 20:29:2988 const base::FilePath path =
89 install_directory_.AppendASCII(version.GetString());
[email protected]426d1c92013-12-03 20:08:5490 base::CreateDirectory(path.DirName());
Sorin Jianuf40ab4b32017-10-06 22:53:4191 if (!base::Move(unpack_path, path)) {
Sorin Jianua8ef73d2017-11-02 16:55:1792 InstallComplete(std::move(callback), Result(InstallError::GENERIC_ERROR));
Sorin Jianuf40ab4b32017-10-06 22:53:4193 return;
94 }
[email protected]e3e696d32013-06-21 20:41:3695 current_version_ = version;
96 ++install_count_;
Sorin Jianuf40ab4b32017-10-06 22:53:4197
Sorin Jianua8ef73d2017-11-02 16:55:1798 InstallComplete(std::move(callback), Result(InstallError::NONE));
[email protected]e3e696d32013-06-21 20:41:3699}
100
101bool VersionedTestInstaller::GetInstalledFile(const std::string& file,
102 base::FilePath* installed_file) {
Sorin Jianu7aa6d1f2017-10-13 20:29:29103 const base::FilePath path =
104 install_directory_.AppendASCII(current_version_.GetString());
[email protected]e3e696d32013-06-21 20:41:36105 *installed_file = path.Append(base::FilePath::FromUTF8Unsafe(file));
106 return true;
107}
[email protected]055981f2014-01-17 20:22:32108
sorin52ac0882015-01-24 01:15:00109} // namespace update_client