blob: 806206c3d541a4095876c7ef9aa94243fb0d4b37 [file] [log] [blame]
David Zeuthen27a48bc2013-08-06 12:06:29 -07001// Copyright (c) 2013 The Chromium OS 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
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
6#define UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
David Zeuthen27a48bc2013-08-06 12:06:29 -07007
8#include "update_engine/p2p_manager.h"
Alex Deymo10875d92014-11-10 21:52:57 -08009#include "update_engine/test_utils.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070010#include "update_engine/utils.h"
11
Alex Vakulenkod2779df2014-06-16 13:19:00 -070012#include <string>
13#include <vector>
14
David Zeuthen27a48bc2013-08-06 12:06:29 -070015#include <base/logging.h>
Alex Deymo8ad6da92014-07-15 17:17:45 -070016#include <base/strings/string_util.h>
Alex Deymob3391552015-07-10 10:48:06 -070017#include <base/strings/string_number_conversions.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070018
19namespace chromeos_update_engine {
20
21// Configuration for P2PManager for use in unit tests. Instead of
22// /var/cache/p2p, a temporary directory is used.
23class FakeP2PManagerConfiguration : public P2PManager::Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070024 public:
Alex Deymob3391552015-07-10 10:48:06 -070025 FakeP2PManagerConfiguration() {
David Zeuthen27a48bc2013-08-06 12:06:29 -070026 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/p2p-tc.XXXXXX", &p2p_dir_));
David Zeuthen27a48bc2013-08-06 12:06:29 -070027 }
28
29 ~FakeP2PManagerConfiguration() {
Alex Deymo10875d92014-11-10 21:52:57 -080030 if (p2p_dir_.size() > 0 && !test_utils::RecursiveUnlinkDir(p2p_dir_)) {
David Zeuthen27a48bc2013-08-06 12:06:29 -070031 PLOG(ERROR) << "Unable to unlink files and directory in " << p2p_dir_;
32 }
33 }
34
35 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080036 base::FilePath GetP2PDir() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070037 return base::FilePath(p2p_dir_);
Alex Vakulenkod2779df2014-06-16 13:19:00 -070038 }
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
Alex Deymo610277e2014-11-11 21:18:11 -080046 std::vector<std::string> GetP2PClientArgs(const std::string &file_id,
47 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) {
52 ReplaceSubstringsAfterOffset(&arg, 0, "{file_id}", file_id);
53 ReplaceSubstringsAfterOffset(&arg, 0, "{minsize}", str_minimum_size);
54 }
55 return formatted_command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070056 }
57
58 // Use |command_line| instead of "initctl start p2p" when attempting
59 // to start the p2p service.
Alex Deymob3391552015-07-10 10:48:06 -070060 void SetInitctlStartCommand(const std::vector<std::string>& command) {
61 initctl_start_args_ = command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070062 }
63
64 // Use |command_line| instead of "initctl stop p2p" when attempting
65 // to stop the p2p service.
Alex Deymob3391552015-07-10 10:48:06 -070066 void SetInitctlStopCommand(const std::vector<std::string>& command) {
67 initctl_stop_args_ = command;
David Zeuthen27a48bc2013-08-06 12:06:29 -070068 }
69
Alex Deymob3391552015-07-10 10:48:06 -070070 // Use |command_format| instead of "p2p-client --get-url={file_id}
Alex Deymo8ad6da92014-07-15 17:17:45 -070071 // --minimum-size={minsize}" when attempting to look up a file using
David Zeuthen27a48bc2013-08-06 12:06:29 -070072 // p2p-client(1).
73 //
Alex Deymob3391552015-07-10 10:48:06 -070074 // The passed |command_format| argument can have "{file_id}" and "{minsize}"
75 // as substrings of any of its elements, that will be replaced by the
76 // corresponding values passed to GetP2PClientArgs().
77 void SetP2PClientCommand(const std::vector<std::string>& command_format) {
78 p2p_client_cmd_format_ = command_format;
David Zeuthen27a48bc2013-08-06 12:06:29 -070079 }
80
Alex Vakulenkod2779df2014-06-16 13:19:00 -070081 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070082 // The temporary directory used for p2p.
83 std::string p2p_dir_;
84
85 // Argument vector for starting p2p.
Alex Deymob3391552015-07-10 10:48:06 -070086 std::vector<std::string> initctl_start_args_{"initctl", "start", "p2p"};
David Zeuthen27a48bc2013-08-06 12:06:29 -070087
88 // Argument vector for stopping p2p.
Alex Deymob3391552015-07-10 10:48:06 -070089 std::vector<std::string> initctl_stop_args_{"initctl", "stop", "p2p"};
David Zeuthen27a48bc2013-08-06 12:06:29 -070090
Alex Deymo8ad6da92014-07-15 17:17:45 -070091 // A string for generating the p2p-client command. See the
92 // SetP2PClientCommandLine() for details.
Alex Deymob3391552015-07-10 10:48:06 -070093 std::vector<std::string> p2p_client_cmd_format_{
94 "p2p-client", "--get-url={file_id}", "--minimum-size={minsize}"};
David Zeuthen27a48bc2013-08-06 12:06:29 -070095
96 DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
97};
98
99} // namespace chromeos_update_engine
100
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700101#endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_