blob: 3d3afe431533628f2e53a32a2cda50f315a3dca1 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2013 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
David Zeuthen27a48bc2013-08-06 12:06:29 -070016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
18#define UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
David Zeuthen27a48bc2013-08-06 12:06:29 -070019
Alex Deymo39910dc2015-11-09 17:04:30 -080020#include "update_engine/common/test_utils.h"
21#include "update_engine/common/utils.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070022#include "update_engine/p2p_manager.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070023
Alex Vakulenkod2779df2014-06-16 13:19:00 -070024#include <string>
25#include <vector>
26
Alex Deymo110e0302015-10-19 20:35:21 -070027#include <base/files/file_util.h>
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include <base/logging.h>
Alex Deymob3391552015-07-10 10:48:06 -070029#include <base/strings/string_number_conversions.h>
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include <base/strings/string_util.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070031
32namespace chromeos_update_engine {
33
34// Configuration for P2PManager for use in unit tests. Instead of
35// /var/cache/p2p, a temporary directory is used.
36class FakeP2PManagerConfiguration : public P2PManager::Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070037 public:
Alex Deymob3391552015-07-10 10:48:06 -070038 FakeP2PManagerConfiguration() {
Alex Deymo5aa1c542015-09-18 01:02:33 -070039 EXPECT_TRUE(utils::MakeTempDirectory("p2p-tc.XXXXXX", &p2p_dir_));
David Zeuthen27a48bc2013-08-06 12:06:29 -070040 }
41
42 ~FakeP2PManagerConfiguration() {
Alex Deymo110e0302015-10-19 20:35:21 -070043 if (p2p_dir_.size() > 0 &&
44 !base::DeleteFile(base::FilePath(p2p_dir_), true)) {
David Zeuthen27a48bc2013-08-06 12:06:29 -070045 PLOG(ERROR) << "Unable to unlink files and directory in " << p2p_dir_;
46 }
47 }
48
49 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080050 base::FilePath GetP2PDir() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070051 return base::FilePath(p2p_dir_);
Alex Vakulenkod2779df2014-06-16 13:19:00 -070052 }
David Zeuthen27a48bc2013-08-06 12:06:29 -070053
54 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080055 std::vector<std::string> GetInitctlArgs(bool is_start) override {
David Zeuthen27a48bc2013-08-06 12:06:29 -070056 return is_start ? initctl_start_args_ : initctl_stop_args_;
57 }
58
59 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080060 std::vector<std::string> GetP2PClientArgs(const std::string &file_id,
61 size_t minimum_size) override {
Alex Deymob3391552015-07-10 10:48:06 -070062 std::vector<std::string> formatted_command = p2p_client_cmd_format_;
Alex Deymo8ad6da92014-07-15 17:17:45 -070063 // Replace {variable} on the passed string.
Alex Deymoc00c98a2015-03-17 17:38:00 -070064 std::string str_minimum_size = std::to_string(minimum_size);
Alex Deymob3391552015-07-10 10:48:06 -070065 for (std::string& arg : formatted_command) {
66 ReplaceSubstringsAfterOffset(&arg, 0, "{file_id}", file_id);
67 ReplaceSubstringsAfterOffset(&arg, 0, "{minsize}", str_minimum_size);
68 }
69 return formatted_command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070070 }
71
72 // Use |command_line| instead of "initctl start p2p" when attempting
73 // to start the p2p service.
Alex Deymob3391552015-07-10 10:48:06 -070074 void SetInitctlStartCommand(const std::vector<std::string>& command) {
75 initctl_start_args_ = command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070076 }
77
78 // Use |command_line| instead of "initctl stop p2p" when attempting
79 // to stop the p2p service.
Alex Deymob3391552015-07-10 10:48:06 -070080 void SetInitctlStopCommand(const std::vector<std::string>& command) {
81 initctl_stop_args_ = command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070082 }
83
Alex Deymob3391552015-07-10 10:48:06 -070084 // Use |command_format| instead of "p2p-client --get-url={file_id}
Alex Deymo8ad6da92014-07-15 17:17:45 -070085 // --minimum-size={minsize}" when attempting to look up a file using
David Zeuthen27a48bc2013-08-06 12:06:29 -070086 // p2p-client(1).
87 //
Alex Deymob3391552015-07-10 10:48:06 -070088 // The passed |command_format| argument can have "{file_id}" and "{minsize}"
89 // as substrings of any of its elements, that will be replaced by the
90 // corresponding values passed to GetP2PClientArgs().
91 void SetP2PClientCommand(const std::vector<std::string>& command_format) {
92 p2p_client_cmd_format_ = command_format;
David Zeuthen27a48bc2013-08-06 12:06:29 -070093 }
94
Alex Vakulenkod2779df2014-06-16 13:19:00 -070095 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070096 // The temporary directory used for p2p.
97 std::string p2p_dir_;
98
99 // Argument vector for starting p2p.
Alex Deymob3391552015-07-10 10:48:06 -0700100 std::vector<std::string> initctl_start_args_{"initctl", "start", "p2p"};
David Zeuthen27a48bc2013-08-06 12:06:29 -0700101
102 // Argument vector for stopping p2p.
Alex Deymob3391552015-07-10 10:48:06 -0700103 std::vector<std::string> initctl_stop_args_{"initctl", "stop", "p2p"};
David Zeuthen27a48bc2013-08-06 12:06:29 -0700104
Alex Deymo8ad6da92014-07-15 17:17:45 -0700105 // A string for generating the p2p-client command. See the
106 // SetP2PClientCommandLine() for details.
Alex Deymob3391552015-07-10 10:48:06 -0700107 std::vector<std::string> p2p_client_cmd_format_{
108 "p2p-client", "--get-url={file_id}", "--minimum-size={minsize}"};
David Zeuthen27a48bc2013-08-06 12:06:29 -0700109
110 DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
111};
112
113} // namespace chromeos_update_engine
114
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700115#endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_