Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2013 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 16 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_ |
| 18 | #define UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_ |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 19 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 20 | #include "update_engine/common/test_utils.h" |
| 21 | #include "update_engine/common/utils.h" |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 22 | #include "update_engine/p2p_manager.h" |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 23 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Alex Deymo | 110e030 | 2015-10-19 20:35:21 -0700 | [diff] [blame] | 27 | #include <base/files/file_util.h> |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include <base/logging.h> |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 29 | #include <base/strings/string_number_conversions.h> |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include <base/strings/string_util.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 31 | |
| 32 | namespace chromeos_update_engine { |
| 33 | |
| 34 | // Configuration for P2PManager for use in unit tests. Instead of |
| 35 | // /var/cache/p2p, a temporary directory is used. |
| 36 | class FakeP2PManagerConfiguration : public P2PManager::Configuration { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 37 | public: |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 38 | FakeP2PManagerConfiguration() { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 39 | EXPECT_TRUE(utils::MakeTempDirectory("p2p-tc.XXXXXX", &p2p_dir_)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ~FakeP2PManagerConfiguration() { |
Alex Deymo | 110e030 | 2015-10-19 20:35:21 -0700 | [diff] [blame] | 43 | if (p2p_dir_.size() > 0 && |
| 44 | !base::DeleteFile(base::FilePath(p2p_dir_), true)) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 45 | PLOG(ERROR) << "Unable to unlink files and directory in " << p2p_dir_; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // P2PManager::Configuration override |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 50 | base::FilePath GetP2PDir() override { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 51 | return base::FilePath(p2p_dir_); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 52 | } |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 53 | |
| 54 | // P2PManager::Configuration override |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 55 | std::vector<std::string> GetInitctlArgs(bool is_start) override { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 56 | return is_start ? initctl_start_args_ : initctl_stop_args_; |
| 57 | } |
| 58 | |
| 59 | // P2PManager::Configuration override |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 60 | std::vector<std::string> GetP2PClientArgs(const std::string &file_id, |
| 61 | size_t minimum_size) override { |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 62 | std::vector<std::string> formatted_command = p2p_client_cmd_format_; |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 63 | // Replace {variable} on the passed string. |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 64 | std::string str_minimum_size = std::to_string(minimum_size); |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 65 | for (std::string& arg : formatted_command) { |
Alex Vakulenko | 0057daa | 2016-01-23 16:22:50 -0800 | [diff] [blame^] | 66 | base::ReplaceSubstringsAfterOffset(&arg, 0, "{file_id}", file_id); |
| 67 | base::ReplaceSubstringsAfterOffset(&arg, 0, "{minsize}", |
| 68 | str_minimum_size); |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 69 | } |
| 70 | return formatted_command; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Use |command_line| instead of "initctl start p2p" when attempting |
| 74 | // to start the p2p service. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 75 | void SetInitctlStartCommand(const std::vector<std::string>& command) { |
| 76 | initctl_start_args_ = command; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Use |command_line| instead of "initctl stop p2p" when attempting |
| 80 | // to stop the p2p service. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 81 | void SetInitctlStopCommand(const std::vector<std::string>& command) { |
| 82 | initctl_stop_args_ = command; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 85 | // Use |command_format| instead of "p2p-client --get-url={file_id} |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 86 | // --minimum-size={minsize}" when attempting to look up a file using |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 87 | // p2p-client(1). |
| 88 | // |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 89 | // The passed |command_format| argument can have "{file_id}" and "{minsize}" |
| 90 | // as substrings of any of its elements, that will be replaced by the |
| 91 | // corresponding values passed to GetP2PClientArgs(). |
| 92 | void SetP2PClientCommand(const std::vector<std::string>& command_format) { |
| 93 | p2p_client_cmd_format_ = command_format; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 96 | private: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 97 | // The temporary directory used for p2p. |
| 98 | std::string p2p_dir_; |
| 99 | |
| 100 | // Argument vector for starting p2p. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 101 | std::vector<std::string> initctl_start_args_{"initctl", "start", "p2p"}; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 102 | |
| 103 | // Argument vector for stopping p2p. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 104 | std::vector<std::string> initctl_stop_args_{"initctl", "stop", "p2p"}; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 105 | |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 106 | // A string for generating the p2p-client command. See the |
| 107 | // SetP2PClientCommandLine() for details. |
Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 108 | std::vector<std::string> p2p_client_cmd_format_{ |
| 109 | "p2p-client", "--get-url={file_id}", "--minimum-size={minsize}"}; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration); |
| 112 | }; |
| 113 | |
| 114 | } // namespace chromeos_update_engine |
| 115 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 116 | #endif // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_ |