3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_FILEUTILS_MOCK_H_
29 #define TALK_BASE_FILEUTILS_MOCK_H_
35 #include "talk/base/fileutils.h"
36 #include "talk/base/gunit.h"
37 #include "talk/base/pathutils.h"
38 #include "talk/base/stream.h"
42 class FakeFileStream : public FileStream {
44 explicit FakeFileStream(const std::string & contents) :
45 string_stream_(contents)
48 virtual StreamResult Read(void* buffer, size_t buffer_len,
49 size_t* read, int* error) {
50 return string_stream_.Read(buffer, buffer_len, read, error);
53 virtual void Close() {
54 return string_stream_.Close();
56 virtual bool GetSize(size_t* size) const {
57 return string_stream_.GetSize(size);
61 StringStream string_stream_;
64 class FakeDirectoryIterator : public DirectoryIterator {
66 typedef std::pair<std::string, std::string> File;
69 * files should be sorted by directory
70 * put '/' at the end of file if you want it to be a directory
80 * you can call Iterate for any path: /var, /var/dir, /var/dir2
81 * unrelated files will be ignored
83 explicit FakeDirectoryIterator(const std::vector<File>& all_files) :
84 all_files_(all_files) {}
86 virtual bool Iterate(const Pathname& path) {
87 path_iterator_ = all_files_.begin();
88 path_ = path.pathname();
90 // make sure path ends end with '/'
91 if (path_.rfind(Pathname::DefaultFolderDelimiter()) != path_.size() - 1)
92 path_ += Pathname::DefaultFolderDelimiter();
94 return FakeDirectoryIterator::Search(std::string(""));
98 std::string current_name = Name();
100 return FakeDirectoryIterator::Search(current_name);
103 bool Search(const std::string& current_name) {
104 for (; path_iterator_ != all_files_.end(); path_iterator_++) {
105 if (path_iterator_->first.find(path_) == 0
106 && Name().compare(current_name) != 0) {
114 virtual bool IsDirectory() const {
115 std::string sub_path = path_iterator_->first;
117 return std::string::npos !=
118 sub_path.find(Pathname::DefaultFolderDelimiter(), path_.size());
121 virtual std::string Name() const {
122 std::string sub_path = path_iterator_->first;
124 // path - top level path (ex. /var/lib)
125 // sub_path - subpath under top level path (ex. /var/lib/dir/dir/file )
126 // find shortest non-trivial common path. (ex. /var/lib/dir)
127 size_t start = path_.size();
128 size_t end = sub_path.find(Pathname::DefaultFolderDelimiter(), start);
130 if (end != std::string::npos) {
131 return sub_path.substr(start, end - start);
133 return sub_path.substr(start);
138 const std::vector<File> all_files_;
141 std::vector<File>::const_iterator path_iterator_;
144 class FakeFileSystem : public FilesystemInterface {
146 typedef std::pair<std::string, std::string> File;
148 explicit FakeFileSystem(const std::vector<File>& all_files) :
149 all_files_(all_files) {}
151 virtual DirectoryIterator *IterateDirectory() {
152 return new FakeDirectoryIterator(all_files_);
155 virtual FileStream * OpenFile(
156 const Pathname &filename,
157 const std::string &mode) {
158 std::vector<File>::const_iterator i_files = all_files_.begin();
159 std::string path = filename.pathname();
161 for (; i_files != all_files_.end(); i_files++) {
162 if (i_files->first.compare(path) == 0) {
163 return new FakeFileStream(i_files->second);
170 bool CreatePrivateFile(const Pathname &filename) {
171 EXPECT_TRUE(false) << "Unsupported operation";
174 bool DeleteFile(const Pathname &filename) {
175 EXPECT_TRUE(false) << "Unsupported operation";
178 bool DeleteEmptyFolder(const Pathname &folder) {
179 EXPECT_TRUE(false) << "Unsupported operation";
182 bool DeleteFolderContents(const Pathname &folder) {
183 EXPECT_TRUE(false) << "Unsupported operation";
186 bool DeleteFolderAndContents(const Pathname &folder) {
187 EXPECT_TRUE(false) << "Unsupported operation";
190 bool CreateFolder(const Pathname &pathname) {
191 EXPECT_TRUE(false) << "Unsupported operation";
194 bool MoveFolder(const Pathname &old_path, const Pathname &new_path) {
195 EXPECT_TRUE(false) << "Unsupported operation";
198 bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
199 EXPECT_TRUE(false) << "Unsupported operation";
202 bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
203 EXPECT_TRUE(false) << "Unsupported operation";
206 bool IsFolder(const Pathname &pathname) {
207 EXPECT_TRUE(false) << "Unsupported operation";
210 bool IsFile(const Pathname &pathname) {
211 EXPECT_TRUE(false) << "Unsupported operation";
214 bool IsAbsent(const Pathname &pathname) {
215 EXPECT_TRUE(false) << "Unsupported operation";
218 bool IsTemporaryPath(const Pathname &pathname) {
219 EXPECT_TRUE(false) << "Unsupported operation";
222 bool GetTemporaryFolder(Pathname &path, bool create,
223 const std::string *append) {
224 EXPECT_TRUE(false) << "Unsupported operation";
227 std::string TempFilename(const Pathname &dir, const std::string &prefix) {
228 EXPECT_TRUE(false) << "Unsupported operation";
229 return std::string();
231 bool GetFileSize(const Pathname &path, size_t *size) {
232 EXPECT_TRUE(false) << "Unsupported operation";
235 bool GetFileTime(const Pathname &path, FileTimeType which,
237 EXPECT_TRUE(false) << "Unsupported operation";
240 bool GetAppPathname(Pathname *path) {
241 EXPECT_TRUE(false) << "Unsupported operation";
244 bool GetAppDataFolder(Pathname *path, bool per_user) {
245 EXPECT_TRUE(per_user) << "Unsupported operation";
247 path->SetPathname("c:\\Users\\test_user", "");
249 path->SetPathname("/home/user/test_user", "");
253 bool GetAppTempFolder(Pathname *path) {
254 EXPECT_TRUE(false) << "Unsupported operation";
257 bool GetDiskFreeSpace(const Pathname &path, int64 *freebytes) {
258 EXPECT_TRUE(false) << "Unsupported operation";
261 Pathname GetCurrentDirectory() {
266 const std::vector<File> all_files_;
268 } // namespace talk_base
270 #endif // TALK_BASE_FILEUTILS_MOCK_H_