1 // Copyright (C) 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_FILE_MOCK_FILESYSTEM_H_ 16 #define ICING_FILE_MOCK_FILESYSTEM_H_ 17 18 #include <cstdint> 19 20 #include "gmock/gmock.h" 21 #include "icing/file/filesystem.h" 22 23 namespace icing { 24 namespace lib { 25 using ::testing::_; 26 using ::testing::A; 27 28 class MockFilesystem : public Filesystem { 29 public: MockFilesystem()30 MockFilesystem() { 31 // For all methods, we always delegate calls to a real Filesystem instance 32 // by default. 33 34 ON_CALL(*this, DeleteFile).WillByDefault([this](const char* file_name) { 35 return real_filesystem_.DeleteFile(file_name); 36 }); 37 38 ON_CALL(*this, DeleteDirectory).WillByDefault([this](const char* dir_name) { 39 return real_filesystem_.DeleteDirectory(dir_name); 40 }); 41 42 ON_CALL(*this, DeleteDirectoryRecursively) 43 .WillByDefault([this](const char* dir_name) { 44 return real_filesystem_.DeleteDirectoryRecursively(dir_name); 45 }); 46 47 ON_CALL(*this, CopyFile) 48 .WillByDefault([this](const char* src, const char* dst) { 49 return real_filesystem_.CopyFile(src, dst); 50 }); 51 52 ON_CALL(*this, CopyDirectory) 53 .WillByDefault( 54 [this](const char* src, const char* dst, bool recursive) { 55 return real_filesystem_.CopyDirectory(src, dst, recursive); 56 }); 57 58 ON_CALL(*this, FileExists).WillByDefault([this](const char* file_name) { 59 return real_filesystem_.FileExists(file_name); 60 }); 61 62 ON_CALL(*this, DirectoryExists).WillByDefault([this](const char* dir_name) { 63 return real_filesystem_.DirectoryExists(dir_name); 64 }); 65 66 ON_CALL(*this, GetBasenameIndex) 67 .WillByDefault([this](const char* file_name) { 68 return real_filesystem_.GetBasenameIndex(file_name); 69 }); 70 71 ON_CALL(*this, GetBasename).WillByDefault([this](const char* file_name) { 72 return real_filesystem_.GetBasename(file_name); 73 }); 74 75 ON_CALL(*this, GetDirname).WillByDefault([this](const char* file_name) { 76 return real_filesystem_.GetDirname(file_name); 77 }); 78 79 ON_CALL(*this, ListDirectory(_, _)) 80 .WillByDefault( 81 [this](const char* dir_name, std::vector<std::string>* entries) { 82 return real_filesystem_.ListDirectory(dir_name, entries); 83 }); 84 85 ON_CALL(*this, ListDirectory(_, _, _, _)) 86 .WillByDefault([this](const char* dir_name, 87 const std::unordered_set<std::string>& exclude, 88 bool recursive, 89 std::vector<std::string>* entries) { 90 return real_filesystem_.ListDirectory(dir_name, exclude, recursive, 91 entries); 92 }); 93 94 ON_CALL(*this, GetMatchingFiles) 95 .WillByDefault( 96 [this](const char* glob, std::vector<std::string>* matches) { 97 return real_filesystem_.GetMatchingFiles(glob, matches); 98 }); 99 100 ON_CALL(*this, OpenForWrite).WillByDefault([this](const char* file_name) { 101 return real_filesystem_.OpenForWrite(file_name); 102 }); 103 104 ON_CALL(*this, OpenForAppend).WillByDefault([this](const char* file_name) { 105 return real_filesystem_.OpenForAppend(file_name); 106 }); 107 108 ON_CALL(*this, OpenForRead).WillByDefault([this](const char* file_name) { 109 return real_filesystem_.OpenForRead(file_name); 110 }); 111 112 ON_CALL(*this, GetFileSize(A<int>())).WillByDefault([this](int fd) { 113 return real_filesystem_.GetFileSize(fd); 114 }); 115 116 ON_CALL(*this, GetFileSize(A<const char*>())) 117 .WillByDefault([this](const char* file_name) { 118 return real_filesystem_.GetFileSize(file_name); 119 }); 120 121 ON_CALL(*this, Truncate(A<int>(), _)) 122 .WillByDefault([this](int fd, int64_t new_size) { 123 return real_filesystem_.Truncate(fd, new_size); 124 }); 125 126 ON_CALL(*this, Truncate(A<const char*>(), _)) 127 .WillByDefault([this](const char* filename, int64_t new_size) { 128 return real_filesystem_.Truncate(filename, new_size); 129 }); 130 131 ON_CALL(*this, Grow(A<int>(), _)) 132 .WillByDefault([this](int fd, int64_t new_size) { 133 return real_filesystem_.Grow(fd, new_size); 134 }); 135 136 ON_CALL(*this, Grow(A<const char*>(), _)) 137 .WillByDefault([this](const char* filename, int64_t new_size) { 138 return real_filesystem_.Grow(filename, new_size); 139 }); 140 141 ON_CALL(*this, Write(A<int>(), _, _)) 142 .WillByDefault([this](int fd, const void* data, size_t data_size) { 143 return real_filesystem_.Write(fd, data, data_size); 144 }); 145 146 ON_CALL(*this, Write(A<const char*>(), _, _)) 147 .WillByDefault( 148 [this](const char* filename, const void* data, size_t data_size) { 149 return real_filesystem_.Write(filename, data, data_size); 150 }); 151 152 ON_CALL(*this, PWrite(A<int>(), _, _, _)) 153 .WillByDefault( 154 [this](int fd, off_t offset, const void* data, size_t data_size) { 155 return real_filesystem_.PWrite(fd, offset, data, data_size); 156 }); 157 158 ON_CALL(*this, PWrite(A<const char*>(), _, _, _)) 159 .WillByDefault([this](const char* filename, off_t offset, 160 const void* data, size_t data_size) { 161 return real_filesystem_.PWrite(filename, offset, data, data_size); 162 }); 163 164 ON_CALL(*this, Read(A<int>(), _, _)) 165 .WillByDefault([this](int fd, void* buf, size_t buf_size) { 166 return real_filesystem_.Read(fd, buf, buf_size); 167 }); 168 169 ON_CALL(*this, Read(A<const char*>(), _, _)) 170 .WillByDefault( 171 [this](const char* filename, void* buf, size_t buf_size) { 172 return real_filesystem_.Read(filename, buf, buf_size); 173 }); 174 175 ON_CALL(*this, PRead(A<int>(), _, _, _)) 176 .WillByDefault( 177 [this](int fd, void* buf, size_t buf_size, off_t offset) { 178 return real_filesystem_.PRead(fd, buf, buf_size, offset); 179 }); 180 181 ON_CALL(*this, PRead(A<const char*>(), _, _, _)) 182 .WillByDefault([this](const char* filename, void* buf, size_t buf_size, 183 off_t offset) { 184 return real_filesystem_.PRead(filename, buf, buf_size, offset); 185 }); 186 187 ON_CALL(*this, DataSync).WillByDefault([this](int fd) { 188 return real_filesystem_.DataSync(fd); 189 }); 190 191 ON_CALL(*this, RenameFile) 192 .WillByDefault([this](const char* old_name, const char* new_name) { 193 return real_filesystem_.RenameFile(old_name, new_name); 194 }); 195 196 ON_CALL(*this, SwapFiles) 197 .WillByDefault([this](const char* one, const char* two) { 198 return real_filesystem_.SwapFiles(one, two); 199 }); 200 201 ON_CALL(*this, CreateDirectory).WillByDefault([this](const char* dir_name) { 202 return real_filesystem_.CreateDirectory(dir_name); 203 }); 204 205 ON_CALL(*this, CreateDirectoryRecursively) 206 .WillByDefault([this](const char* dir_name) { 207 return real_filesystem_.CreateDirectoryRecursively(dir_name); 208 }); 209 210 ON_CALL(*this, GetDiskUsage(A<int>())).WillByDefault([this](int fd) { 211 return real_filesystem_.GetDiskUsage(fd); 212 }); 213 214 ON_CALL(*this, GetFileDiskUsage).WillByDefault([this](const char* path) { 215 return real_filesystem_.GetFileDiskUsage(path); 216 }); 217 218 ON_CALL(*this, GetDiskUsage(A<const char*>())) 219 .WillByDefault([this](const char* path) { 220 return real_filesystem_.GetDiskUsage(path); 221 }); 222 223 ON_CALL(*this, GetCurrentPosition).WillByDefault([this](int fd) { 224 return real_filesystem_.GetCurrentPosition(fd); 225 }); 226 227 ON_CALL(*this, SetPosition).WillByDefault([this](int fd, int offset) { 228 return real_filesystem_.SetPosition(fd, offset); 229 }); 230 } 231 232 MOCK_METHOD(bool, DeleteFile, (const char* file_name), (const)); 233 234 MOCK_METHOD(bool, DeleteDirectory, (const char* dir_name), (const)); 235 236 MOCK_METHOD(bool, DeleteDirectoryRecursively, (const char* dir_name), 237 (const)); 238 239 MOCK_METHOD(bool, CopyFile, (const char* src, const char* dst), (const)); 240 241 MOCK_METHOD(bool, CopyDirectory, 242 (const char* src, const char* dst, bool recursive), (const)); 243 244 MOCK_METHOD(bool, FileExists, (const char* file_name), (const)); 245 246 MOCK_METHOD(bool, DirectoryExists, (const char* dir_name), (const)); 247 248 MOCK_METHOD(int, GetBasenameIndex, (const char* file_name), (const)); 249 250 MOCK_METHOD(std::string, GetBasename, (const char* file_name), (const)); 251 252 MOCK_METHOD(std::string, GetDirname, (const char* file_name), (const)); 253 254 MOCK_METHOD(bool, ListDirectory, 255 (const char* dir_name, std::vector<std::string>* entries), 256 (const)); 257 258 MOCK_METHOD(bool, ListDirectory, 259 (const char* dir_name, 260 const std::unordered_set<std::string>& exclude, bool recursive, 261 std::vector<std::string>* entries), 262 (const)); 263 264 MOCK_METHOD(bool, GetMatchingFiles, 265 (const char* glob, std::vector<std::string>* matches), (const)); 266 267 MOCK_METHOD(int, OpenForWrite, (const char* file_name), (const)); 268 269 MOCK_METHOD(int, OpenForAppend, (const char* file_name), (const)); 270 271 MOCK_METHOD(int, OpenForRead, (const char* file_name), (const)); 272 273 MOCK_METHOD(int64_t, GetFileSize, (int fd), (const)); 274 275 MOCK_METHOD(int64_t, GetFileSize, (const char* filename), (const)); 276 277 MOCK_METHOD(bool, Truncate, (int fd, int64_t new_size), (const)); 278 279 MOCK_METHOD(bool, Truncate, (const char* filename, int64_t new_size), 280 (const)); 281 282 MOCK_METHOD(bool, Grow, (int fd, int64_t new_size), (const)); 283 284 MOCK_METHOD(bool, Grow, (const char* filename, int64_t new_size), (const)); 285 286 MOCK_METHOD(bool, Write, (int fd, const void* data, size_t data_size), 287 (const)); 288 289 MOCK_METHOD(bool, Write, 290 (const char* filename, const void* data, size_t data_size), 291 (const)); 292 293 MOCK_METHOD(bool, PWrite, 294 (int fd, off_t offset, const void* data, size_t data_size), 295 (const)); 296 297 MOCK_METHOD(bool, PWrite, 298 (const char* filename, off_t offset, const void* data, 299 size_t data_size), 300 (const)); 301 302 MOCK_METHOD(bool, Read, (int fd, void* buf, size_t buf_size), (const)); 303 304 MOCK_METHOD(bool, Read, (const char* filename, void* buf, size_t buf_size), 305 (const)); 306 307 MOCK_METHOD(bool, PRead, (int fd, void* buf, size_t buf_size, off_t offset), 308 (const)); 309 310 MOCK_METHOD(bool, PRead, 311 (const char* filename, void* buf, size_t buf_size, off_t offset), 312 (const)); 313 314 MOCK_METHOD(bool, DataSync, (int fd), (const)); 315 316 MOCK_METHOD(bool, RenameFile, (const char* old_name, const char* new_name), 317 (const)); 318 319 MOCK_METHOD(bool, SwapFiles, (const char* one, const char* two), (const)); 320 321 MOCK_METHOD(bool, CreateDirectory, (const char* dir_name), (const)); 322 323 MOCK_METHOD(bool, CreateDirectoryRecursively, (const char* dir_name), 324 (const)); 325 326 MOCK_METHOD(int64_t, GetDiskUsage, (int fd), (const)); 327 328 MOCK_METHOD(int64_t, GetFileDiskUsage, (const char* path), (const)); 329 330 MOCK_METHOD(int64_t, GetDiskUsage, (const char* path), (const)); 331 332 MOCK_METHOD(int64_t, GetCurrentPosition, (int fd), (const)); 333 334 MOCK_METHOD(int64_t, SetPosition, (int fd, int offset), (const)); 335 336 private: 337 Filesystem real_filesystem_; 338 }; 339 340 } // namespace lib 341 } // namespace icing 342 343 #endif // ICING_FILE_MOCK_FILESYSTEM_H_ 344