1/* 2 * libjingle 3 * Copyright 2004--2011, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 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. 15 * 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. 26 */ 27 28#ifndef TALK_BASE_FILEUTILS_MOCK_H_ 29#define TALK_BASE_FILEUTILS_MOCK_H_ 30 31#include <string> 32#include <utility> 33#include <vector> 34 35#include "talk/base/fileutils.h" 36#include "talk/base/gunit.h" 37#include "talk/base/pathutils.h" 38#include "talk/base/stream.h" 39 40namespace talk_base { 41 42class FakeFileStream : public FileStream { 43 public: 44 explicit FakeFileStream(const std::string & contents) : 45 string_stream_(contents) 46 {} 47 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); 51 } 52 53 virtual void Close() { 54 return string_stream_.Close(); 55 } 56 virtual bool GetSize(size_t* size) const { 57 return string_stream_.GetSize(size); 58 } 59 60 private: 61 StringStream string_stream_; 62}; 63 64class FakeDirectoryIterator : public DirectoryIterator { 65 public: 66 typedef std::pair<std::string, std::string> File; 67 68 /* 69 * files should be sorted by directory 70 * put '/' at the end of file if you want it to be a directory 71 * 72 * Sample list: 73 * /var/dir/file1 74 * /var/dir/file2 75 * /var/dir/subdir1/ 76 * /var/dir/subdir2/ 77 * /var/dir2/file2 78 * /var/dir3/ 79 * 80 * you can call Iterate for any path: /var, /var/dir, /var/dir2 81 * unrelated files will be ignored 82 */ 83 explicit FakeDirectoryIterator(const std::vector<File>& all_files) : 84 all_files_(all_files) {} 85 86 virtual bool Iterate(const Pathname& path) { 87 path_iterator_ = all_files_.begin(); 88 path_ = path.pathname(); 89 90 // make sure path ends end with '/' 91 if (path_.rfind(Pathname::DefaultFolderDelimiter()) != path_.size() - 1) 92 path_ += Pathname::DefaultFolderDelimiter(); 93 94 return FakeDirectoryIterator::Search(std::string("")); 95 } 96 97 virtual bool Next() { 98 std::string current_name = Name(); 99 path_iterator_++; 100 return FakeDirectoryIterator::Search(current_name); 101 } 102 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) { 107 return true; 108 } 109 } 110 111 return false; 112 } 113 114 virtual bool IsDirectory() const { 115 std::string sub_path = path_iterator_->first; 116 117 return std::string::npos != 118 sub_path.find(Pathname::DefaultFolderDelimiter(), path_.size()); 119 } 120 121 virtual std::string Name() const { 122 std::string sub_path = path_iterator_->first; 123 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); 129 130 if (end != std::string::npos) { 131 return sub_path.substr(start, end - start); 132 } else { 133 return sub_path.substr(start); 134 } 135 } 136 137 private: 138 const std::vector<File> all_files_; 139 140 std::string path_; 141 std::vector<File>::const_iterator path_iterator_; 142}; 143 144class FakeFileSystem : public FilesystemInterface { 145 public: 146 typedef std::pair<std::string, std::string> File; 147 148 explicit FakeFileSystem(const std::vector<File>& all_files) : 149 all_files_(all_files) {} 150 151 virtual DirectoryIterator *IterateDirectory() { 152 return new FakeDirectoryIterator(all_files_); 153 } 154 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(); 160 161 for (; i_files != all_files_.end(); i_files++) { 162 if (i_files->first.compare(path) == 0) { 163 return new FakeFileStream(i_files->second); 164 } 165 } 166 167 return NULL; 168 } 169 170 bool CreatePrivateFile(const Pathname &filename) { 171 EXPECT_TRUE(false) << "Unsupported operation"; 172 return false; 173 } 174 bool DeleteFile(const Pathname &filename) { 175 EXPECT_TRUE(false) << "Unsupported operation"; 176 return false; 177 } 178 bool DeleteEmptyFolder(const Pathname &folder) { 179 EXPECT_TRUE(false) << "Unsupported operation"; 180 return false; 181 } 182 bool DeleteFolderContents(const Pathname &folder) { 183 EXPECT_TRUE(false) << "Unsupported operation"; 184 return false; 185 } 186 bool DeleteFolderAndContents(const Pathname &folder) { 187 EXPECT_TRUE(false) << "Unsupported operation"; 188 return false; 189 } 190 bool CreateFolder(const Pathname &pathname) { 191 EXPECT_TRUE(false) << "Unsupported operation"; 192 return false; 193 } 194 bool MoveFolder(const Pathname &old_path, const Pathname &new_path) { 195 EXPECT_TRUE(false) << "Unsupported operation"; 196 return false; 197 } 198 bool MoveFile(const Pathname &old_path, const Pathname &new_path) { 199 EXPECT_TRUE(false) << "Unsupported operation"; 200 return false; 201 } 202 bool CopyFile(const Pathname &old_path, const Pathname &new_path) { 203 EXPECT_TRUE(false) << "Unsupported operation"; 204 return false; 205 } 206 bool IsFolder(const Pathname &pathname) { 207 EXPECT_TRUE(false) << "Unsupported operation"; 208 return false; 209 } 210 bool IsFile(const Pathname &pathname) { 211 EXPECT_TRUE(false) << "Unsupported operation"; 212 return false; 213 } 214 bool IsAbsent(const Pathname &pathname) { 215 EXPECT_TRUE(false) << "Unsupported operation"; 216 return false; 217 } 218 bool IsTemporaryPath(const Pathname &pathname) { 219 EXPECT_TRUE(false) << "Unsupported operation"; 220 return false; 221 } 222 bool GetTemporaryFolder(Pathname &path, bool create, 223 const std::string *append) { 224 EXPECT_TRUE(false) << "Unsupported operation"; 225 return false; 226 } 227 std::string TempFilename(const Pathname &dir, const std::string &prefix) { 228 EXPECT_TRUE(false) << "Unsupported operation"; 229 return std::string(); 230 } 231 bool GetFileSize(const Pathname &path, size_t *size) { 232 EXPECT_TRUE(false) << "Unsupported operation"; 233 return false; 234 } 235 bool GetFileTime(const Pathname &path, FileTimeType which, 236 time_t* time) { 237 EXPECT_TRUE(false) << "Unsupported operation"; 238 return false; 239 } 240 bool GetAppPathname(Pathname *path) { 241 EXPECT_TRUE(false) << "Unsupported operation"; 242 return false; 243 } 244 bool GetAppDataFolder(Pathname *path, bool per_user) { 245 EXPECT_TRUE(per_user) << "Unsupported operation"; 246#ifdef WIN32 247 path->SetPathname("c:\\Users\\test_user", ""); 248#else 249 path->SetPathname("/home/user/test_user", ""); 250#endif 251 return true; 252 } 253 bool GetAppTempFolder(Pathname *path) { 254 EXPECT_TRUE(false) << "Unsupported operation"; 255 return false; 256 } 257 bool GetDiskFreeSpace(const Pathname &path, int64 *freebytes) { 258 EXPECT_TRUE(false) << "Unsupported operation"; 259 return false; 260 } 261 Pathname GetCurrentDirectory() { 262 return Pathname(); 263 } 264 265 private: 266 const std::vector<File> all_files_; 267}; 268} // namespace talk_base 269 270#endif // TALK_BASE_FILEUTILS_MOCK_H_ 271