1 // Copyright 2017 The Chromium 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
5 #ifndef CHROMEOS_DBUS_FAKE_SMB_PROVIDER_CLIENT_H_
6 #define CHROMEOS_DBUS_FAKE_SMB_PROVIDER_CLIENT_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "chromeos/dbus/smb_provider_client.h"
13
14 namespace chromeos {
15
16 // A fake implementation of SmbProviderClient.
COMPONENT_EXPORT(CHROMEOS_DBUS)17 class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeSmbProviderClient
18 : public SmbProviderClient {
19 public:
20 FakeSmbProviderClient();
21 explicit FakeSmbProviderClient(bool should_run_synchronously);
22 ~FakeSmbProviderClient() override;
23
24 // Adds an entry in the |netbios_parse_results_| map for <packetid,
25 // hostnames>.
26 void AddNetBiosPacketParsingForTesting(uint8_t packet_id,
27 std::vector<std::string> hostnames);
28
29 // DBusClient override.
30 void Init(dbus::Bus* bus) override;
31
32 // SmbProviderClient override.
33 void Mount(const base::FilePath& share_path,
34 const MountOptions& options,
35 base::ScopedFD password_fd,
36 MountCallback callback) override;
37
38 void Unmount(int32_t mount_id,
39 bool remove_password,
40 StatusCallback callback) override;
41 void ReadDirectory(int32_t mount_id,
42 const base::FilePath& directory_path,
43 ReadDirectoryCallback callback) override;
44 void GetMetadataEntry(int32_t mount_id,
45 const base::FilePath& entry_path,
46 GetMetdataEntryCallback callback) override;
47 void OpenFile(int32_t mount_id,
48 const base::FilePath& file_path,
49 bool writeable,
50 OpenFileCallback callback) override;
51 void CloseFile(int32_t mount_id,
52 int32_t file_id,
53 StatusCallback callback) override;
54 void ReadFile(int32_t mount_id,
55 int32_t file_id,
56 int64_t offset,
57 int32_t length,
58 ReadFileCallback callback) override;
59
60 void DeleteEntry(int32_t mount_id,
61 const base::FilePath& entry_path,
62 bool recursive,
63 StatusCallback callback) override;
64
65 void CreateFile(int32_t mount_id,
66 const base::FilePath& file_path,
67 StatusCallback callback) override;
68
69 void Truncate(int32_t mount_id,
70 const base::FilePath& file_path,
71 int64_t length,
72 StatusCallback callback) override;
73
74 void WriteFile(int32_t mount_id,
75 int32_t file_id,
76 int64_t offset,
77 int32_t length,
78 base::ScopedFD temp_fd,
79 StatusCallback callback) override;
80
81 void CreateDirectory(int32_t mount_id,
82 const base::FilePath& directory_path,
83 bool recursive,
84 StatusCallback callback) override;
85
86 void MoveEntry(int32_t mount_id,
87 const base::FilePath& source_path,
88 const base::FilePath& target_path,
89 StatusCallback callback) override;
90
91 void CopyEntry(int32_t mount_id,
92 const base::FilePath& source_path,
93 const base::FilePath& target_path,
94 StatusCallback callback) override;
95
96 void GetDeleteList(int32_t mount_id,
97 const base::FilePath& entry_path,
98 GetDeleteListCallback callback) override;
99
100 void GetShares(const base::FilePath& server_url,
101 ReadDirectoryCallback callback) override;
102
103 void SetupKerberos(const std::string& account_id,
104 SetupKerberosCallback callback) override;
105
106 void ParseNetBiosPacket(const std::vector<uint8_t>& packet,
107 uint16_t transaction_id,
108 ParseNetBiosPacketCallback callback) override;
109
110 void StartCopy(int32_t mount_id,
111 const base::FilePath& source_path,
112 const base::FilePath& target_path,
113 StartCopyCallback callback) override;
114
115 void ContinueCopy(int32_t mount_id,
116 int32_t copy_token,
117 StatusCallback callback) override;
118
119 void StartReadDirectory(int32_t mount_id,
120 const base::FilePath& directory_path,
121 StartReadDirectoryCallback callback) override;
122
123 void ContinueReadDirectory(int32_t mount_id,
124 int32_t read_dir_token,
125 ReadDirectoryCallback callback) override;
126
127 void UpdateMountCredentials(int32_t mount_id,
128 std::string workgroup,
129 std::string username,
130 base::ScopedFD password_fd,
131 StatusCallback callback) override;
132
133 void UpdateSharePath(int32_t mount_id,
134 const std::string& share_path,
135 StatusCallback callback) override;
136
137 // Adds |share| to the list of shares for |server_url| in |shares_|.
138 void AddToShares(const std::string& server_url, const std::string& share);
139
140 // Adds a failure to get shares for |server_url|.
141 void AddGetSharesFailure(const std::string& server_url,
142 smbprovider::ErrorType error);
143
144 // Clears |shares_|.
145 void ClearShares();
146
147 // Runs |stored_callback_|.
148 void RunStoredReadDirCallback();
149
150 private:
151 // Result of a GetShares() call.
152 struct ShareResult {
153 ShareResult();
154 ~ShareResult();
155
156 smbprovider::ErrorType error = smbprovider::ErrorType::ERROR_OK;
157 std::vector<std::string> shares;
158 };
159
160 // Controls whether |stored_readdir_callback_| should run synchronously.
161 bool should_run_synchronously_ = true;
162
163 base::OnceClosure stored_readdir_callback_;
164
165 std::map<uint8_t, std::vector<std::string>> netbios_parse_results_;
166
167 // Mapping of a server url to its shares.
168 std::map<std::string, ShareResult> shares_;
169
170 DISALLOW_COPY_AND_ASSIGN(FakeSmbProviderClient);
171 };
172
173 } // namespace chromeos
174
175 #endif // CHROMEOS_DBUS_FAKE_SMB_PROVIDER_CLIENT_H_
176