• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
16 
17 #ifndef UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
18 #define UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
19 
20 #include "update_engine/p2p_manager.h"
21 
22 #include <string>
23 #include <vector>
24 
25 #include <base/files/scoped_temp_dir.h>
26 #include <base/strings/string_util.h>
27 #include <gtest/gtest.h>
28 
29 namespace chromeos_update_engine {
30 
31 // Configuration for P2PManager for use in unit tests. Instead of
32 // /var/cache/p2p, a temporary directory is used.
33 class FakeP2PManagerConfiguration : public P2PManager::Configuration {
34  public:
FakeP2PManagerConfiguration()35   FakeP2PManagerConfiguration() {
36     EXPECT_TRUE(p2p_dir_.CreateUniqueTempDir());
37   }
38 
39   // P2PManager::Configuration override
GetP2PDir()40   base::FilePath GetP2PDir() override { return p2p_dir_.GetPath(); }
41 
42   // P2PManager::Configuration override
GetInitctlArgs(bool is_start)43   std::vector<std::string> GetInitctlArgs(bool is_start) override {
44     return is_start ? initctl_start_args_ : initctl_stop_args_;
45   }
46 
47   // P2PManager::Configuration override
GetP2PClientArgs(const std::string & file_id,size_t minimum_size)48   std::vector<std::string> GetP2PClientArgs(const std::string &file_id,
49                                             size_t minimum_size) override {
50     std::vector<std::string> formatted_command = p2p_client_cmd_format_;
51     // Replace {variable} on the passed string.
52     std::string str_minimum_size = std::to_string(minimum_size);
53     for (std::string& arg : formatted_command) {
54       base::ReplaceSubstringsAfterOffset(&arg, 0, "{file_id}", file_id);
55       base::ReplaceSubstringsAfterOffset(&arg, 0, "{minsize}",
56                                          str_minimum_size);
57     }
58     return formatted_command;
59   }
60 
61   // Use |command_line| instead of "initctl start p2p" when attempting
62   // to start the p2p service.
SetInitctlStartCommand(const std::vector<std::string> & command)63   void SetInitctlStartCommand(const std::vector<std::string>& command) {
64     initctl_start_args_ = command;
65   }
66 
67   // Use |command_line| instead of "initctl stop p2p" when attempting
68   // to stop the p2p service.
SetInitctlStopCommand(const std::vector<std::string> & command)69   void SetInitctlStopCommand(const std::vector<std::string>& command) {
70     initctl_stop_args_ = command;
71   }
72 
73   // Use |command_format| instead of "p2p-client --get-url={file_id}
74   // --minimum-size={minsize}" when attempting to look up a file using
75   // p2p-client(1).
76   //
77   // The passed |command_format| argument can have "{file_id}" and "{minsize}"
78   // as substrings of any of its elements, that will be replaced by the
79   // corresponding values passed to GetP2PClientArgs().
SetP2PClientCommand(const std::vector<std::string> & command_format)80   void SetP2PClientCommand(const std::vector<std::string>& command_format) {
81     p2p_client_cmd_format_ = command_format;
82   }
83 
84  private:
85   // The temporary directory used for p2p.
86   base::ScopedTempDir p2p_dir_;
87 
88   // Argument vector for starting p2p.
89   std::vector<std::string> initctl_start_args_{"initctl", "start", "p2p"};
90 
91   // Argument vector for stopping p2p.
92   std::vector<std::string> initctl_stop_args_{"initctl", "stop", "p2p"};
93 
94   // A string for generating the p2p-client command. See the
95   // SetP2PClientCommandLine() for details.
96   std::vector<std::string> p2p_client_cmd_format_{
97       "p2p-client", "--get-url={file_id}", "--minimum-size={minsize}"};
98 
99   DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
100 };
101 
102 }  // namespace chromeos_update_engine
103 
104 #endif  // UPDATE_ENGINE_FAKE_P2P_MANAGER_CONFIGURATION_H_
105