blob: 6272689a31de8b034dbcc33a4156e4f218ec4b74 [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 <glib.h>
16
17#include <base/logging.h>
Alex Deymo8ad6da92014-07-15 17:17:45 -070018#include <base/strings/string_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070019#include <base/strings/stringprintf.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070020
21namespace chromeos_update_engine {
22
23// Configuration for P2PManager for use in unit tests. Instead of
24// /var/cache/p2p, a temporary directory is used.
25class FakeP2PManagerConfiguration : public P2PManager::Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070026 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070027 FakeP2PManagerConfiguration()
Alex Deymo8ad6da92014-07-15 17:17:45 -070028 : p2p_client_cmdline_format_(
29 "p2p-client --get-url={file_id} --minimum-size={minsize}") {
David Zeuthen27a48bc2013-08-06 12:06:29 -070030 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/p2p-tc.XXXXXX", &p2p_dir_));
31 SetInitctlStartCommandLine("initctl start p2p");
32 SetInitctlStopCommandLine("initctl stop p2p");
33 }
34
35 ~FakeP2PManagerConfiguration() {
Alex Deymo10875d92014-11-10 21:52:57 -080036 if (p2p_dir_.size() > 0 && !test_utils::RecursiveUnlinkDir(p2p_dir_)) {
David Zeuthen27a48bc2013-08-06 12:06:29 -070037 PLOG(ERROR) << "Unable to unlink files and directory in " << p2p_dir_;
38 }
39 }
40
41 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080042 base::FilePath GetP2PDir() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070043 return base::FilePath(p2p_dir_);
Alex Vakulenkod2779df2014-06-16 13:19:00 -070044 }
David Zeuthen27a48bc2013-08-06 12:06:29 -070045
46 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080047 std::vector<std::string> GetInitctlArgs(bool is_start) override {
David Zeuthen27a48bc2013-08-06 12:06:29 -070048 return is_start ? initctl_start_args_ : initctl_stop_args_;
49 }
50
51 // P2PManager::Configuration override
Alex Deymo610277e2014-11-11 21:18:11 -080052 std::vector<std::string> GetP2PClientArgs(const std::string &file_id,
53 size_t minimum_size) override {
Alex Deymo8ad6da92014-07-15 17:17:45 -070054 std::string formatted_command_line = p2p_client_cmdline_format_;
55 // Replace {variable} on the passed string.
56 ReplaceSubstringsAfterOffset(
57 &formatted_command_line, 0, "{file_id}", file_id);
58 ReplaceSubstringsAfterOffset(
59 &formatted_command_line, 0,
60 "{minsize}", base::StringPrintf("%zu", minimum_size));
61
David Zeuthen27a48bc2013-08-06 12:06:29 -070062 return ParseCommandLine(formatted_command_line);
63 }
64
65 // Use |command_line| instead of "initctl start p2p" when attempting
66 // to start the p2p service.
67 void SetInitctlStartCommandLine(const std::string &command_line) {
68 initctl_start_args_ = ParseCommandLine(command_line);
69 }
70
71 // Use |command_line| instead of "initctl stop p2p" when attempting
72 // to stop the p2p service.
73 void SetInitctlStopCommandLine(const std::string &command_line) {
74 initctl_stop_args_ = ParseCommandLine(command_line);
75 }
76
Alex Deymo8ad6da92014-07-15 17:17:45 -070077 // Use |command_line_format| instead of "p2p-client --get-url={file_id}
78 // --minimum-size={minsize}" when attempting to look up a file using
David Zeuthen27a48bc2013-08-06 12:06:29 -070079 // p2p-client(1).
80 //
Alex Deymo8ad6da92014-07-15 17:17:45 -070081 // The passed |command_line_format| argument can have "{file_id}" and
82 // "{minsize}" as substrings, that will be replaced by the corresponding
83 // values passed to GetP2PClientArgs().
David Zeuthen27a48bc2013-08-06 12:06:29 -070084 void SetP2PClientCommandLine(const std::string &command_line_format) {
85 p2p_client_cmdline_format_ = command_line_format;
86 }
87
Alex Vakulenkod2779df2014-06-16 13:19:00 -070088 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070089 // Helper for parsing and splitting |command_line| into an argument
90 // vector in much the same way a shell would except for not
91 // supporting wildcards, globs, operators etc. See
92 // g_shell_parse_argv() for more details. If an error occurs, the
93 // empty vector is returned.
94 std::vector<std::string> ParseCommandLine(const std::string &command_line) {
95 gint argc;
96 gchar **argv;
97 std::vector<std::string> ret;
98
99 if (!g_shell_parse_argv(command_line.c_str(),
100 &argc,
101 &argv,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700102 nullptr)) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700103 LOG(ERROR) << "Error splitting '" << command_line << "'";
104 return ret;
105 }
106 for (int n = 0; n < argc; n++)
107 ret.push_back(argv[n]);
108 g_strfreev(argv);
109 return ret;
110 }
111
112 // The temporary directory used for p2p.
113 std::string p2p_dir_;
114
115 // Argument vector for starting p2p.
116 std::vector<std::string> initctl_start_args_;
117
118 // Argument vector for stopping p2p.
119 std::vector<std::string> initctl_stop_args_;
120
Alex Deymo8ad6da92014-07-15 17:17:45 -0700121 // A string for generating the p2p-client command. See the
122 // SetP2PClientCommandLine() for details.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700123 std::string p2p_client_cmdline_format_;
124
125 DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
126};
127
128} // namespace chromeos_update_engine
129
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700130#endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_