blob: 7ce41fa0471a51859abc847b355b7c79ad7f8849 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_FILEUTILS_MOCK_H_
12#define WEBRTC_BASE_FILEUTILS_MOCK_H_
13
14#include <string>
15#include <utility>
16#include <vector>
17
18#include "webrtc/base/fileutils.h"
19#include "webrtc/base/gunit.h"
20#include "webrtc/base/pathutils.h"
21#include "webrtc/base/stream.h"
22
23namespace rtc {
24
25class FakeFileStream : public FileStream {
26 public:
27 explicit FakeFileStream(const std::string & contents) :
28 string_stream_(contents)
29 {}
30
31 virtual StreamResult Read(void* buffer, size_t buffer_len,
32 size_t* read, int* error) {
33 return string_stream_.Read(buffer, buffer_len, read, error);
34 }
35
36 virtual void Close() {
37 return string_stream_.Close();
38 }
39 virtual bool GetSize(size_t* size) const {
40 return string_stream_.GetSize(size);
41 }
42
43 private:
44 StringStream string_stream_;
45};
46
47class FakeDirectoryIterator : public DirectoryIterator {
48 public:
49 typedef std::pair<std::string, std::string> File;
50
51 /*
52 * files should be sorted by directory
53 * put '/' at the end of file if you want it to be a directory
54 *
55 * Sample list:
56 * /var/dir/file1
57 * /var/dir/file2
58 * /var/dir/subdir1/
59 * /var/dir/subdir2/
60 * /var/dir2/file2
61 * /var/dir3/
62 *
63 * you can call Iterate for any path: /var, /var/dir, /var/dir2
64 * unrelated files will be ignored
65 */
66 explicit FakeDirectoryIterator(const std::vector<File>& all_files) :
67 all_files_(all_files) {}
68
69 virtual bool Iterate(const Pathname& path) {
70 path_iterator_ = all_files_.begin();
71 path_ = path.pathname();
72
73 // make sure path ends end with '/'
74 if (path_.rfind(Pathname::DefaultFolderDelimiter()) != path_.size() - 1)
75 path_ += Pathname::DefaultFolderDelimiter();
76
77 return FakeDirectoryIterator::Search(std::string(""));
78 }
79
80 virtual bool Next() {
81 std::string current_name = Name();
82 path_iterator_++;
83 return FakeDirectoryIterator::Search(current_name);
84 }
85
86 bool Search(const std::string& current_name) {
87 for (; path_iterator_ != all_files_.end(); path_iterator_++) {
88 if (path_iterator_->first.find(path_) == 0
89 && Name().compare(current_name) != 0) {
90 return true;
91 }
92 }
93
94 return false;
95 }
96
97 virtual bool IsDirectory() const {
98 std::string sub_path = path_iterator_->first;
99
100 return std::string::npos !=
101 sub_path.find(Pathname::DefaultFolderDelimiter(), path_.size());
102 }
103
104 virtual std::string Name() const {
105 std::string sub_path = path_iterator_->first;
106
107 // path - top level path (ex. /var/lib)
108 // sub_path - subpath under top level path (ex. /var/lib/dir/dir/file )
109 // find shortest non-trivial common path. (ex. /var/lib/dir)
110 size_t start = path_.size();
111 size_t end = sub_path.find(Pathname::DefaultFolderDelimiter(), start);
112
113 if (end != std::string::npos) {
114 return sub_path.substr(start, end - start);
115 } else {
116 return sub_path.substr(start);
117 }
118 }
119
120 private:
121 const std::vector<File> all_files_;
122
123 std::string path_;
124 std::vector<File>::const_iterator path_iterator_;
125};
126
127class FakeFileSystem : public FilesystemInterface {
128 public:
129 typedef std::pair<std::string, std::string> File;
130
131 explicit FakeFileSystem(const std::vector<File>& all_files) :
132 all_files_(all_files) {}
133
134 virtual DirectoryIterator *IterateDirectory() {
135 return new FakeDirectoryIterator(all_files_);
136 }
137
138 virtual FileStream * OpenFile(
139 const Pathname &filename,
140 const std::string &mode) {
141 std::vector<File>::const_iterator i_files = all_files_.begin();
142 std::string path = filename.pathname();
143
144 for (; i_files != all_files_.end(); i_files++) {
145 if (i_files->first.compare(path) == 0) {
146 return new FakeFileStream(i_files->second);
147 }
148 }
149
150 return NULL;
151 }
152
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153 bool DeleteFile(const Pathname &filename) {
154 EXPECT_TRUE(false) << "Unsupported operation";
155 return false;
156 }
157 bool DeleteEmptyFolder(const Pathname &folder) {
158 EXPECT_TRUE(false) << "Unsupported operation";
159 return false;
160 }
161 bool DeleteFolderContents(const Pathname &folder) {
162 EXPECT_TRUE(false) << "Unsupported operation";
163 return false;
164 }
165 bool DeleteFolderAndContents(const Pathname &folder) {
166 EXPECT_TRUE(false) << "Unsupported operation";
167 return false;
168 }
169 bool CreateFolder(const Pathname &pathname) {
170 EXPECT_TRUE(false) << "Unsupported operation";
171 return false;
172 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173 bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
174 EXPECT_TRUE(false) << "Unsupported operation";
175 return false;
176 }
177 bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
178 EXPECT_TRUE(false) << "Unsupported operation";
179 return false;
180 }
181 bool IsFolder(const Pathname &pathname) {
182 EXPECT_TRUE(false) << "Unsupported operation";
183 return false;
184 }
185 bool IsFile(const Pathname &pathname) {
186 EXPECT_TRUE(false) << "Unsupported operation";
187 return false;
188 }
189 bool IsAbsent(const Pathname &pathname) {
190 EXPECT_TRUE(false) << "Unsupported operation";
191 return false;
192 }
193 bool IsTemporaryPath(const Pathname &pathname) {
194 EXPECT_TRUE(false) << "Unsupported operation";
195 return false;
196 }
197 bool GetTemporaryFolder(Pathname &path, bool create,
198 const std::string *append) {
199 EXPECT_TRUE(false) << "Unsupported operation";
200 return false;
201 }
202 std::string TempFilename(const Pathname &dir, const std::string &prefix) {
203 EXPECT_TRUE(false) << "Unsupported operation";
204 return std::string();
205 }
206 bool GetFileSize(const Pathname &path, size_t *size) {
207 EXPECT_TRUE(false) << "Unsupported operation";
208 return false;
209 }
210 bool GetFileTime(const Pathname &path, FileTimeType which,
211 time_t* time) {
212 EXPECT_TRUE(false) << "Unsupported operation";
213 return false;
214 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215 bool GetAppDataFolder(Pathname *path, bool per_user) {
216 EXPECT_TRUE(per_user) << "Unsupported operation";
217#if defined(WEBRTC_WIN)
218 path->SetPathname("c:\\Users\\test_user", "");
219#else
220 path->SetPathname("/home/user/test_user", "");
221#endif
222 return true;
223 }
224 bool GetAppTempFolder(Pathname *path) {
225 EXPECT_TRUE(false) << "Unsupported operation";
226 return false;
227 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200228 bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229 EXPECT_TRUE(false) << "Unsupported operation";
230 return false;
231 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232
nisseda35f3e2016-10-27 04:44:39 -0700233 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234 const std::vector<File> all_files_;
235};
236} // namespace rtc
237
238#endif // WEBRTC_BASE_FILEUTILS_MOCK_H_