fake_p2p_manager_configuration.h 3.7 KB

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