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