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