blob: f5b0e80d6f0fab88a727eda7497c73e10b57ae58 [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
20#include "update_engine/p2p_manager.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070021
Alex Vakulenkod2779df2014-06-16 13:19:00 -070022#include <string>
23#include <vector>
24
Sen Jiang371615b2016-04-13 15:54:29 -070025#include <base/files/scoped_temp_dir.h>
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include <base/strings/string_util.h>
Sen Jiang371615b2016-04-13 15:54:29 -070027#include <gtest/gtest.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070028
29namespace chromeos_update_engine {
30
31// Configuration for P2PManager for use in unit tests. Instead of
32// /var/cache/p2p, a temporary directory is used.
33class FakeP2PManagerConfiguration : public P2PManager::Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070034 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080035 FakeP2PManagerConfiguration() { EXPECT_TRUE(p2p_dir_.CreateUniqueTempDir()); }
David Zeuthen27a48bc2013-08-06 12:06:29 -070036
37 // P2PManager::Configuration override
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090038 base::FilePath GetP2PDir() override { return p2p_dir_.GetPath(); }
David Zeuthen27a48bc2013-08-06 12:06:29 -070039
40 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080041 std::vector<std::string> GetInitctlArgs(bool is_start) override {
David Zeuthen27a48bc2013-08-06 12:06:29 -070042 return is_start ? initctl_start_args_ : initctl_stop_args_;
43 }
44
45 // P2PManager::Configuration override
Amin Hassani7cc8bb02019-01-14 16:29:47 -080046 std::vector<std::string> GetP2PClientArgs(const std::string& file_id,
Alex Deymo610277e2014-11-11 21:18:11 -080047 size_t minimum_size) override {
Alex Deymob3391552015-07-10 10:48:06 -070048 std::vector<std::string> formatted_command = p2p_client_cmd_format_;
Alex Deymo8ad6da92014-07-15 17:17:45 -070049 // Replace {variable} on the passed string.
Alex Deymoc00c98a2015-03-17 17:38:00 -070050 std::string str_minimum_size = std::to_string(minimum_size);
Alex Deymob3391552015-07-10 10:48:06 -070051 for (std::string& arg : formatted_command) {
Alex Vakulenko0057daa2016-01-23 16:22:50 -080052 base::ReplaceSubstringsAfterOffset(&arg, 0, "{file_id}", file_id);
Amin Hassani7cc8bb02019-01-14 16:29:47 -080053 base::ReplaceSubstringsAfterOffset(
54 &arg, 0, "{minsize}", str_minimum_size);
Alex Deymob3391552015-07-10 10:48:06 -070055 }
56 return formatted_command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070057 }
58
59 // Use |command_line| instead of "initctl start p2p" when attempting
60 // to start the p2p service.
Alex Deymob3391552015-07-10 10:48:06 -070061 void SetInitctlStartCommand(const std::vector<std::string>& command) {
62 initctl_start_args_ = command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070063 }
64
65 // Use |command_line| instead of "initctl stop p2p" when attempting
66 // to stop the p2p service.
Alex Deymob3391552015-07-10 10:48:06 -070067 void SetInitctlStopCommand(const std::vector<std::string>& command) {
68 initctl_stop_args_ = command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070069 }
70
Alex Deymob3391552015-07-10 10:48:06 -070071 // Use |command_format| instead of "p2p-client --get-url={file_id}
Alex Deymo8ad6da92014-07-15 17:17:45 -070072 // --minimum-size={minsize}" when attempting to look up a file using
David Zeuthen27a48bc2013-08-06 12:06:29 -070073 // p2p-client(1).
74 //
Alex Deymob3391552015-07-10 10:48:06 -070075 // The passed |command_format| argument can have "{file_id}" and "{minsize}"
76 // as substrings of any of its elements, that will be replaced by the
77 // corresponding values passed to GetP2PClientArgs().
78 void SetP2PClientCommand(const std::vector<std::string>& command_format) {
79 p2p_client_cmd_format_ = command_format;
David Zeuthen27a48bc2013-08-06 12:06:29 -070080 }
81
Alex Vakulenkod2779df2014-06-16 13:19:00 -070082 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070083 // The temporary directory used for p2p.
Sen Jiang371615b2016-04-13 15:54:29 -070084 base::ScopedTempDir p2p_dir_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070085
86 // Argument vector for starting p2p.
Alex Deymob3391552015-07-10 10:48:06 -070087 std::vector<std::string> initctl_start_args_{"initctl", "start", "p2p"};
David Zeuthen27a48bc2013-08-06 12:06:29 -070088
89 // Argument vector for stopping p2p.
Alex Deymob3391552015-07-10 10:48:06 -070090 std::vector<std::string> initctl_stop_args_{"initctl", "stop", "p2p"};
David Zeuthen27a48bc2013-08-06 12:06:29 -070091
Alex Deymo8ad6da92014-07-15 17:17:45 -070092 // A string for generating the p2p-client command. See the
93 // SetP2PClientCommandLine() for details.
Alex Deymob3391552015-07-10 10:48:06 -070094 std::vector<std::string> p2p_client_cmd_format_{
95 "p2p-client", "--get-url={file_id}", "--minimum-size={minsize}"};
David Zeuthen27a48bc2013-08-06 12:06:29 -070096
97 DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
98};
99
100} // namespace chromeos_update_engine
101
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700102#endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_