1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* libjingle
* Copyright 2004--2011, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TALK_BASE_FILEUTILS_MOCK_H_
#define TALK_BASE_FILEUTILS_MOCK_H_
#include <string>
#include <utility>
#include <vector>
#include "talk/base/fileutils.h"
#include "talk/base/gunit.h"
#include "talk/base/pathutils.h"
#include "talk/base/stream.h"
namespace talk_base {
class FakeFileStream : public FileStream {
public:
explicit FakeFileStream(const std::string & contents) :
string_stream_(contents)
{}
virtual StreamResult Read(void* buffer, size_t buffer_len,
size_t* read, int* error) {
return string_stream_.Read(buffer, buffer_len, read, error);
}
virtual void Close() {
return string_stream_.Close();
}
virtual bool GetSize(size_t* size) const {
return string_stream_.GetSize(size);
}
private:
StringStream string_stream_;
};
class FakeDirectoryIterator : public DirectoryIterator {
public:
typedef std::pair<std::string, std::string> File;
/*
* files should be sorted by directory
* put '/' at the end of file if you want it to be a directory
*
* Sample list:
* /var/dir/file1
* /var/dir/file2
* /var/dir/subdir1/
* /var/dir/subdir2/
* /var/dir2/file2
* /var/dir3/
*
* you can call Iterate for any path: /var, /var/dir, /var/dir2
* unrelated files will be ignored
*/
explicit FakeDirectoryIterator(const std::vector<File>& all_files) :
all_files_(all_files) {}
virtual bool Iterate(const Pathname& path) {
path_iterator_ = all_files_.begin();
path_ = path.pathname();
// make sure path ends end with '/'
if (path_.rfind(Pathname::DefaultFolderDelimiter()) != path_.size() - 1)
path_ += Pathname::DefaultFolderDelimiter();
return FakeDirectoryIterator::Search(std::string(""));
}
virtual bool Next() {
std::string current_name = Name();
path_iterator_++;
return FakeDirectoryIterator::Search(current_name);
}
bool Search(const std::string& current_name) {
for (; path_iterator_ != all_files_.end(); path_iterator_++) {
if (path_iterator_->first.find(path_) == 0
&& Name().compare(current_name) != 0) {
return true;
}
}
return false;
}
virtual bool IsDirectory() const {
std::string sub_path = path_iterator_->first;
return std::string::npos !=
sub_path.find(Pathname::DefaultFolderDelimiter(), path_.size());
}
virtual std::string Name() const {
std::string sub_path = path_iterator_->first;
// path - top level path (ex. /var/lib)
// sub_path - subpath under top level path (ex. /var/lib/dir/dir/file )
// find shortest non-trivial common path. (ex. /var/lib/dir)
size_t start = path_.size();
size_t end = sub_path.find(Pathname::DefaultFolderDelimiter(), start);
if (end != std::string::npos) {
return sub_path.substr(start, end - start);
} else {
return sub_path.substr(start);
}
}
private:
const std::vector<File> all_files_;
std::string path_;
std::vector<File>::const_iterator path_iterator_;
};
class FakeFileSystem : public FilesystemInterface {
public:
typedef std::pair<std::string, std::string> File;
explicit FakeFileSystem(const std::vector<File>& all_files) :
all_files_(all_files) {}
virtual DirectoryIterator *IterateDirectory() {
return new FakeDirectoryIterator(all_files_);
}
virtual FileStream * OpenFile(
const Pathname &filename,
const std::string &mode) {
std::vector<File>::const_iterator i_files = all_files_.begin();
std::string path = filename.pathname();
for (; i_files != all_files_.end(); i_files++) {
if (i_files->first.compare(path) == 0) {
return new FakeFileStream(i_files->second);
}
}
return NULL;
}
bool CreatePrivateFile(const Pathname &filename) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool DeleteFile(const Pathname &filename) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool DeleteEmptyFolder(const Pathname &folder) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool DeleteFolderContents(const Pathname &folder) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool DeleteFolderAndContents(const Pathname &folder) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool CreateFolder(const Pathname &pathname) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool MoveFolder(const Pathname &old_path, const Pathname &new_path) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool IsFolder(const Pathname &pathname) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool IsFile(const Pathname &pathname) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool IsAbsent(const Pathname &pathname) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool IsTemporaryPath(const Pathname &pathname) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool GetTemporaryFolder(Pathname &path, bool create,
const std::string *append) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
std::string TempFilename(const Pathname &dir, const std::string &prefix) {
EXPECT_TRUE(false) << "Unsupported operation";
return std::string();
}
bool GetFileSize(const Pathname &path, size_t *size) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool GetFileTime(const Pathname &path, FileTimeType which,
time_t* time) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool GetAppPathname(Pathname *path) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool GetAppDataFolder(Pathname *path, bool per_user) {
EXPECT_TRUE(per_user) << "Unsupported operation";
#ifdef WIN32
path->SetPathname("c:\\Users\\test_user", "");
#else
path->SetPathname("/home/user/test_user", "");
#endif
return true;
}
bool GetAppTempFolder(Pathname *path) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
bool GetDiskFreeSpace(const Pathname &path, int64 *freebytes) {
EXPECT_TRUE(false) << "Unsupported operation";
return false;
}
Pathname GetCurrentDirectory() {
return Pathname();
}
private:
const std::vector<File> all_files_;
};
} // namespace talk_base
#endif // TALK_BASE_FILEUTILS_MOCK_H_
|