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_H_
18 #define UPDATE_ENGINE_FAKE_P2P_MANAGER_H_
19 
20 #include <string>
21 
22 #include "update_engine/p2p_manager.h"
23 
24 namespace chromeos_update_engine {
25 
26 // A fake implementation of P2PManager.
27 class FakeP2PManager : public P2PManager {
28  public:
FakeP2PManager()29   FakeP2PManager() :
30     is_p2p_enabled_(false),
31     ensure_p2p_running_result_(false),
32     ensure_p2p_not_running_result_(false),
33     perform_housekeeping_result_(false),
34     count_shared_files_result_(0) {}
35 
36   // P2PManager overrides.
SetDevicePolicy(const policy::DevicePolicy * device_policy)37   void SetDevicePolicy(const policy::DevicePolicy* device_policy) override {}
38 
IsP2PEnabled()39   bool IsP2PEnabled() override {
40     return is_p2p_enabled_;
41   }
42 
EnsureP2PRunning()43   bool EnsureP2PRunning() override {
44     return ensure_p2p_running_result_;
45   }
46 
EnsureP2PNotRunning()47   bool EnsureP2PNotRunning() override {
48     return ensure_p2p_not_running_result_;
49   }
50 
PerformHousekeeping()51   bool PerformHousekeeping() override {
52     return perform_housekeeping_result_;
53   }
54 
LookupUrlForFile(const std::string & file_id,size_t minimum_size,base::TimeDelta max_time_to_wait,LookupCallback callback)55   void LookupUrlForFile(const std::string& file_id,
56                         size_t minimum_size,
57                         base::TimeDelta max_time_to_wait,
58                         LookupCallback callback) override {
59     callback.Run(lookup_url_for_file_result_);
60   }
61 
FileShare(const std::string & file_id,size_t expected_size)62   bool FileShare(const std::string& file_id,
63                  size_t expected_size) override {
64     return false;
65   }
66 
FileGetPath(const std::string & file_id)67   base::FilePath FileGetPath(const std::string& file_id) override {
68     return base::FilePath();
69   }
70 
FileGetSize(const std::string & file_id)71   ssize_t FileGetSize(const std::string& file_id) override {
72     return -1;
73   }
74 
FileGetExpectedSize(const std::string & file_id)75   ssize_t FileGetExpectedSize(const std::string& file_id) override {
76     return -1;
77   }
78 
FileGetVisible(const std::string & file_id,bool * out_result)79   bool FileGetVisible(const std::string& file_id,
80                       bool *out_result) override {
81     return false;
82   }
83 
FileMakeVisible(const std::string & file_id)84   bool FileMakeVisible(const std::string& file_id) override {
85     return false;
86   }
87 
CountSharedFiles()88   int CountSharedFiles() override {
89     return count_shared_files_result_;
90   }
91 
92   // Methods for controlling what the fake returns and how it acts.
SetP2PEnabled(bool is_p2p_enabled)93   void SetP2PEnabled(bool is_p2p_enabled) {
94     is_p2p_enabled_ = is_p2p_enabled;
95   }
96 
SetEnsureP2PRunningResult(bool ensure_p2p_running_result)97   void SetEnsureP2PRunningResult(bool ensure_p2p_running_result) {
98     ensure_p2p_running_result_ = ensure_p2p_running_result;
99   }
100 
SetEnsureP2PNotRunningResult(bool ensure_p2p_not_running_result)101   void SetEnsureP2PNotRunningResult(bool ensure_p2p_not_running_result) {
102     ensure_p2p_not_running_result_ = ensure_p2p_not_running_result;
103   }
104 
SetPerformHousekeepingResult(bool perform_housekeeping_result)105   void SetPerformHousekeepingResult(bool perform_housekeeping_result) {
106     perform_housekeeping_result_ = perform_housekeeping_result;
107   }
108 
SetCountSharedFilesResult(int count_shared_files_result)109   void SetCountSharedFilesResult(int count_shared_files_result) {
110     count_shared_files_result_ = count_shared_files_result;
111   }
112 
SetLookupUrlForFileResult(const std::string & url)113   void SetLookupUrlForFileResult(const std::string& url) {
114     lookup_url_for_file_result_ = url;
115   }
116 
117  private:
118   bool is_p2p_enabled_;
119   bool ensure_p2p_running_result_;
120   bool ensure_p2p_not_running_result_;
121   bool perform_housekeeping_result_;
122   int count_shared_files_result_;
123   std::string lookup_url_for_file_result_;
124 
125   DISALLOW_COPY_AND_ASSIGN(FakeP2PManager);
126 };
127 
128 }  // namespace chromeos_update_engine
129 
130 #endif  // UPDATE_ENGINE_FAKE_P2P_MANAGER_H_
131