blob: 58a740e2fadf12121e35e2a26faceba2427ac96f [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
Alex Deymo759c2752014-03-17 21:09:36 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
6#define CHROMEOS_PLATFORM_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 Vakulenko75039d72014-03-25 12:36:28 -070017#include <base/strings/stringprintf.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:
David Zeuthen27a48bc2013-08-06 12:06:29 -070025 FakeP2PManagerConfiguration()
26 : p2p_client_cmdline_format_("p2p-client --get-url=%s --minimum-size=%zu") {
27 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/p2p-tc.XXXXXX", &p2p_dir_));
28 SetInitctlStartCommandLine("initctl start p2p");
29 SetInitctlStopCommandLine("initctl stop p2p");
30 }
31
32 ~FakeP2PManagerConfiguration() {
33 if (p2p_dir_.size() > 0 && !utils::RecursiveUnlinkDir(p2p_dir_)) {
34 PLOG(ERROR) << "Unable to unlink files and directory in " << p2p_dir_;
35 }
36 }
37
38 // P2PManager::Configuration override
Alex Vakulenko75039d72014-03-25 12:36:28 -070039 virtual base::FilePath GetP2PDir() {
40 return base::FilePath(p2p_dir_);
Alex Vakulenkod2779df2014-06-16 13:19:00 -070041 }
David Zeuthen27a48bc2013-08-06 12:06:29 -070042
43 // P2PManager::Configuration override
44 virtual std::vector<std::string> GetInitctlArgs(bool is_start) {
45 return is_start ? initctl_start_args_ : initctl_stop_args_;
46 }
47
48 // P2PManager::Configuration override
49 virtual std::vector<std::string> GetP2PClientArgs(const std::string &file_id,
50 size_t minimum_size) {
51 std::string formatted_command_line =
52 base::StringPrintf(p2p_client_cmdline_format_.c_str(),
53 file_id.c_str(), minimum_size);
54 return ParseCommandLine(formatted_command_line);
55 }
56
57 // Use |command_line| instead of "initctl start p2p" when attempting
58 // to start the p2p service.
59 void SetInitctlStartCommandLine(const std::string &command_line) {
60 initctl_start_args_ = ParseCommandLine(command_line);
61 }
62
63 // Use |command_line| instead of "initctl stop p2p" when attempting
64 // to stop the p2p service.
65 void SetInitctlStopCommandLine(const std::string &command_line) {
66 initctl_stop_args_ = ParseCommandLine(command_line);
67 }
68
69 // Use |command_line_format| instead of "p2p-client --get-url=%s
70 // --minimum-size=%zu" when attempting to look up a file using
71 // p2p-client(1).
72 //
73 // The passed |command_line_format| argument should be a
74 // printf()-style format string taking two arguments, the first
75 // being the a C string for the p2p file id (e.g. %s) and the second
76 // being a size_t with the minimum_size.
77 void SetP2PClientCommandLine(const std::string &command_line_format) {
78 p2p_client_cmdline_format_ = command_line_format;
79 }
80
Alex Vakulenkod2779df2014-06-16 13:19:00 -070081 private:
David Zeuthen27a48bc2013-08-06 12:06:29 -070082 // Helper for parsing and splitting |command_line| into an argument
83 // vector in much the same way a shell would except for not
84 // supporting wildcards, globs, operators etc. See
85 // g_shell_parse_argv() for more details. If an error occurs, the
86 // empty vector is returned.
87 std::vector<std::string> ParseCommandLine(const std::string &command_line) {
88 gint argc;
89 gchar **argv;
90 std::vector<std::string> ret;
91
92 if (!g_shell_parse_argv(command_line.c_str(),
93 &argc,
94 &argv,
95 NULL)) {
96 LOG(ERROR) << "Error splitting '" << command_line << "'";
97 return ret;
98 }
99 for (int n = 0; n < argc; n++)
100 ret.push_back(argv[n]);
101 g_strfreev(argv);
102 return ret;
103 }
104
105 // The temporary directory used for p2p.
106 std::string p2p_dir_;
107
108 // Argument vector for starting p2p.
109 std::vector<std::string> initctl_start_args_;
110
111 // Argument vector for stopping p2p.
112 std::vector<std::string> initctl_stop_args_;
113
114 // A printf()-style format string for generating the p2p-client format.
115 // See the SetP2PClientCommandLine() for details.
116 std::string p2p_client_cmdline_format_;
117
118 DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
119};
120
121} // namespace chromeos_update_engine
122
Alex Deymo759c2752014-03-17 21:09:36 -0700123#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_