David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1 | // 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 Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_ |
| 6 | #define UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_ |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 7 | |
| 8 | #include "update_engine/p2p_manager.h" |
| 9 | #include "update_engine/utils.h" |
| 10 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 14 | #include <glib.h> |
| 15 | |
| 16 | #include <base/logging.h> |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 17 | #include <base/strings/string_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 18 | #include <base/strings/stringprintf.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | // Configuration for P2PManager for use in unit tests. Instead of |
| 23 | // /var/cache/p2p, a temporary directory is used. |
| 24 | class FakeP2PManagerConfiguration : public P2PManager::Configuration { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 25 | public: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 26 | FakeP2PManagerConfiguration() |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 27 | : p2p_client_cmdline_format_( |
| 28 | "p2p-client --get-url={file_id} --minimum-size={minsize}") { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 29 | 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 Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 41 | virtual base::FilePath GetP2PDir() { |
| 42 | return base::FilePath(p2p_dir_); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 43 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 44 | |
| 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 Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 53 | 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 61 | 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 Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 76 | // 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 78 | // p2p-client(1). |
| 79 | // |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 80 | // 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 83 | void SetP2PClientCommandLine(const std::string &command_line_format) { |
| 84 | p2p_client_cmdline_format_ = command_line_format; |
| 85 | } |
| 86 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 87 | private: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 88 | // 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 101 | nullptr)) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 102 | 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 Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 120 | // A string for generating the p2p-client command. See the |
| 121 | // SetP2PClientCommandLine() for details. |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 122 | std::string p2p_client_cmdline_format_; |
| 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration); |
| 125 | }; |
| 126 | |
| 127 | } // namespace chromeos_update_engine |
| 128 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 129 | #endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_ |