blob: 0645d7a63f312e71f133942db83f908deaa8acbb [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"
9#include "update_engine/utils.h"
10
Alex Vakulenkod2779df2014-06-16 13:19:00 -070011#include <string>
12#include <vector>
13
David Zeuthen27a48bc2013-08-06 12:06:29 -070014#include <glib.h>
15
16#include <base/logging.h>
Alex Deymo8ad6da92014-07-15 17:17:45 -070017#include <base/strings/string_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070018#include <base/strings/stringprintf.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070019
20namespace chromeos_update_engine {
21
22// Configuration for P2PManager for use in unit tests. Instead of
23// /var/cache/p2p, a temporary directory is used.
24class FakeP2PManagerConfiguration : public P2PManager::Configuration {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070025 public:
David Zeuthen27a48bc2013-08-06 12:06:29 -070026 FakeP2PManagerConfiguration()
Alex Deymo8ad6da92014-07-15 17:17:45 -070027 : p2p_client_cmdline_format_(
28 "p2p-client --get-url={file_id} --minimum-size={minsize}") {
David Zeuthen27a48bc2013-08-06 12:06:29 -070029 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/p2p-tc.XXXXXX", &p2p_dir_));
30 SetInitctlStartCommandLine("initctl start p2p");
31 SetInitctlStopCommandLine("initctl stop p2p");
32 }
33
34 ~FakeP2PManagerConfiguration() {
35 if (p2p_dir_.size() > 0 && !utils::RecursiveUnlinkDir(p2p_dir_)) {
36 PLOG(ERROR) << "Unable to unlink files and directory in " << p2p_dir_;
37 }
38 }
39
40 // P2PManager::Configuration override
Alex Vakulenko75039d72014-03-25 12:36:28 -070041 virtual base::FilePath GetP2PDir() {
42 return base::FilePath(p2p_dir_);
Alex Vakulenkod2779df2014-06-16 13:19:00 -070043 }
David Zeuthen27a48bc2013-08-06 12:06:29 -070044
45 // P2PManager::Configuration override
46 virtual std::vector<std::string> GetInitctlArgs(bool is_start) {
47 return is_start ? initctl_start_args_ : initctl_stop_args_;
48 }
49
50 // P2PManager::Configuration override
51 virtual std::vector<std::string> GetP2PClientArgs(const std::string &file_id,
52 size_t minimum_size) {
Alex Deymo8ad6da92014-07-15 17:17:45 -070053 std::string formatted_command_line = p2p_client_cmdline_format_;
54 // Replace {variable} on the passed string.
55 ReplaceSubstringsAfterOffset(
56 &formatted_command_line, 0, "{file_id}", file_id);
57 ReplaceSubstringsAfterOffset(
58 &formatted_command_line, 0,
59 "{minsize}", base::StringPrintf("%zu", minimum_size));
60
David Zeuthen27a48bc2013-08-06 12:06:29 -070061 return ParseCommandLine(formatted_command_line);
62 }
63
64 // Use |command_line| instead of "initctl start p2p" when attempting
65 // to start the p2p service.
66 void SetInitctlStartCommandLine(const std::string &command_line) {
67 initctl_start_args_ = ParseCommandLine(command_line);
68 }
69
70 // Use |command_line| instead of "initctl stop p2p" when attempting
71 // to stop the p2p service.
72 void SetInitctlStopCommandLine(const std::string &command_line) {
73 initctl_stop_args_ = ParseCommandLine(command_line);
74 }
75
Alex Deymo8ad6da92014-07-15 17:17:45 -070076 // Use |command_line_format| instead of "p2p-client --get-url={file_id}
77 // --minimum-size={minsize}" when attempting to look up a file using
David Zeuthen27a48bc2013-08-06 12:06:29 -070078 // p2p-client(1).
79 //
Alex Deymo8ad6da92014-07-15 17:17:45 -070080 // The passed |command_line_format| argument can have "{file_id}" and
81 // "{minsize}" as substrings, that will be replaced by the corresponding
82 // values passed to GetP2PClientArgs().
David Zeuthen27a48bc2013-08-06 12:06:29 -070083 void SetP2PClientCommandLine(const std::string &command_line_format) {
84 p2p_client_cmdline_format_ = command_line_format;
85 }
86
Alex Vakulenkod2779df2014-06-16 13:19:00 -070087 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070088 // Helper for parsing and splitting |command_line| into an argument
89 // vector in much the same way a shell would except for not
90 // supporting wildcards, globs, operators etc. See
91 // g_shell_parse_argv() for more details. If an error occurs, the
92 // empty vector is returned.
93 std::vector<std::string> ParseCommandLine(const std::string &command_line) {
94 gint argc;
95 gchar **argv;
96 std::vector<std::string> ret;
97
98 if (!g_shell_parse_argv(command_line.c_str(),
99 &argc,
100 &argv,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700101 nullptr)) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700102 LOG(ERROR) << "Error splitting '" << command_line << "'";
103 return ret;
104 }
105 for (int n = 0; n < argc; n++)
106 ret.push_back(argv[n]);
107 g_strfreev(argv);
108 return ret;
109 }
110
111 // The temporary directory used for p2p.
112 std::string p2p_dir_;
113
114 // Argument vector for starting p2p.
115 std::vector<std::string> initctl_start_args_;
116
117 // Argument vector for stopping p2p.
118 std::vector<std::string> initctl_stop_args_;
119
Alex Deymo8ad6da92014-07-15 17:17:45 -0700120 // A string for generating the p2p-client command. See the
121 // SetP2PClientCommandLine() for details.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700122 std::string p2p_client_cmdline_format_;
123
124 DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
125};
126
127} // namespace chromeos_update_engine
128
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700129#endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_