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" |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 9 | #include "update_engine/test_utils.h" |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 10 | #include "update_engine/utils.h" |
| 11 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 15 | #include <base/logging.h> |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 16 | #include <base/strings/string_util.h> |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 17 | #include <base/strings/string_number_conversions.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 18 | |
| 19 | namespace chromeos_update_engine { |
| 20 | |
| 21 | // Configuration for P2PManager for use in unit tests. Instead of |
| 22 | // /var/cache/p2p, a temporary directory is used. |
| 23 | class FakeP2PManagerConfiguration : public P2PManager::Configuration { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 24 | public: |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 25 | FakeP2PManagerConfiguration() { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 26 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/p2p-tc.XXXXXX", &p2p_dir_)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ~FakeP2PManagerConfiguration() { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 30 | if (p2p_dir_.size() > 0 && !test_utils::RecursiveUnlinkDir(p2p_dir_)) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 31 | PLOG(ERROR) << "Unable to unlink files and directory in " << p2p_dir_; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // P2PManager::Configuration override |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 36 | base::FilePath GetP2PDir() override { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 37 | return base::FilePath(p2p_dir_); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 38 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 39 | |
| 40 | // P2PManager::Configuration override |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 41 | std::vector<std::string> GetInitctlArgs(bool is_start) override { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 42 | return is_start ? initctl_start_args_ : initctl_stop_args_; |
| 43 | } |
| 44 | |
| 45 | // P2PManager::Configuration override |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 46 | std::vector<std::string> GetP2PClientArgs(const std::string &file_id, |
| 47 | size_t minimum_size) override { |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 48 | std::vector<std::string> formatted_command = p2p_client_cmd_format_; |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 49 | // Replace {variable} on the passed string. |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame^] | 50 | std::string str_minimum_size = std::to_string(minimum_size); |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 51 | for (std::string& arg : formatted_command) { |
| 52 | ReplaceSubstringsAfterOffset(&arg, 0, "{file_id}", file_id); |
| 53 | ReplaceSubstringsAfterOffset(&arg, 0, "{minsize}", str_minimum_size); |
| 54 | } |
| 55 | return formatted_command; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Use |command_line| instead of "initctl start p2p" when attempting |
| 59 | // to start the p2p service. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 60 | void SetInitctlStartCommand(const std::vector<std::string>& command) { |
| 61 | initctl_start_args_ = command; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Use |command_line| instead of "initctl stop p2p" when attempting |
| 65 | // to stop the p2p service. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 66 | void SetInitctlStopCommand(const std::vector<std::string>& command) { |
| 67 | initctl_stop_args_ = command; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 70 | // Use |command_format| instead of "p2p-client --get-url={file_id} |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 71 | // --minimum-size={minsize}" when attempting to look up a file using |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 72 | // p2p-client(1). |
| 73 | // |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 74 | // The passed |command_format| argument can have "{file_id}" and "{minsize}" |
| 75 | // as substrings of any of its elements, that will be replaced by the |
| 76 | // corresponding values passed to GetP2PClientArgs(). |
| 77 | void SetP2PClientCommand(const std::vector<std::string>& command_format) { |
| 78 | p2p_client_cmd_format_ = command_format; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 81 | private: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 82 | // The temporary directory used for p2p. |
| 83 | std::string p2p_dir_; |
| 84 | |
| 85 | // Argument vector for starting p2p. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 86 | std::vector<std::string> initctl_start_args_{"initctl", "start", "p2p"}; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 87 | |
| 88 | // Argument vector for stopping p2p. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 89 | std::vector<std::string> initctl_stop_args_{"initctl", "stop", "p2p"}; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 90 | |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 91 | // A string for generating the p2p-client command. See the |
| 92 | // SetP2PClientCommandLine() for details. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 93 | std::vector<std::string> p2p_client_cmd_format_{ |
| 94 | "p2p-client", "--get-url={file_id}", "--minimum-size={minsize}"}; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration); |
| 97 | }; |
| 98 | |
| 99 | } // namespace chromeos_update_engine |
| 100 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 101 | #endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_ |