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_LEGACY_INDEX_ICING_MOCK_FILESYSTEM_H_ 16 #define ICING_LEGACY_INDEX_ICING_MOCK_FILESYSTEM_H_ 17 18 #include <cstdint> 19 #include <cstdio> 20 #include <cstring> 21 #include <string> 22 #include <vector> 23 24 #include "gmock/gmock.h" 25 #include "icing/legacy/index/icing-filesystem.h" 26 27 namespace icing { 28 namespace lib { 29 using ::testing::_; 30 using ::testing::A; 31 32 class IcingMockFilesystem : public IcingFilesystem { 33 public: IcingMockFilesystem()34 IcingMockFilesystem() { 35 ON_CALL(*this, DeleteFile).WillByDefault([this](const char *file_name) { 36 return real_icing_filesystem_.DeleteFile(file_name); 37 }); 38 39 ON_CALL(*this, DeleteDirectory).WillByDefault([this](const char *dir_name) { 40 return real_icing_filesystem_.DeleteDirectory(dir_name); 41 }); 42 43 ON_CALL(*this, DeleteDirectoryRecursively) 44 .WillByDefault([this](const char *dir_name) { 45 return real_icing_filesystem_.DeleteDirectoryRecursively(dir_name); 46 }); 47 48 ON_CALL(*this, FileExists).WillByDefault([this](const char *file_name) { 49 return real_icing_filesystem_.FileExists(file_name); 50 }); 51 52 ON_CALL(*this, DirectoryExists).WillByDefault([this](const char *dir_name) { 53 return real_icing_filesystem_.DirectoryExists(dir_name); 54 }); 55 56 ON_CALL(*this, GetBasenameIndex) 57 .WillByDefault([this](const char *file_name) { 58 return real_icing_filesystem_.GetBasenameIndex(file_name); 59 }); 60 61 ON_CALL(*this, GetBasename).WillByDefault([this](const char *file_name) { 62 return real_icing_filesystem_.GetBasename(file_name); 63 }); 64 65 ON_CALL(*this, GetDirname).WillByDefault([this](const char *file_name) { 66 return real_icing_filesystem_.GetDirname(file_name); 67 }); 68 69 ON_CALL(*this, ListDirectory) 70 .WillByDefault( 71 [this](const char *dir_name, std::vector<std::string> *entries) { 72 return real_icing_filesystem_.ListDirectory(dir_name, entries); 73 }); 74 75 ON_CALL(*this, GetMatchingFiles) 76 .WillByDefault( 77 [this](const char *glob, std::vector<std::string> *matches) { 78 return real_icing_filesystem_.GetMatchingFiles(glob, matches); 79 }); 80 81 ON_CALL(*this, OpenForWrite).WillByDefault([this](const char *file_name) { 82 return real_icing_filesystem_.OpenForWrite(file_name); 83 }); 84 85 ON_CALL(*this, OpenForAppend).WillByDefault([this](const char *file_name) { 86 return real_icing_filesystem_.OpenForAppend(file_name); 87 }); 88 89 ON_CALL(*this, OpenForRead).WillByDefault([this](const char *file_name) { 90 return real_icing_filesystem_.OpenForRead(file_name); 91 }); 92 93 ON_CALL(*this, GetFileSize(A<int>())).WillByDefault([this](int fd) { 94 return real_icing_filesystem_.GetFileSize(fd); 95 }); 96 97 ON_CALL(*this, GetFileSize(A<const char *>())) 98 .WillByDefault([this](const char *filename) { 99 return real_icing_filesystem_.GetFileSize(filename); 100 }); 101 102 ON_CALL(*this, Truncate(A<int>(), _)) 103 .WillByDefault([this](int fd, uint64_t new_size) { 104 return real_icing_filesystem_.Truncate(fd, new_size); 105 }); 106 107 ON_CALL(*this, Truncate(A<const char *>(), _)) 108 .WillByDefault([this](const char *filename, uint64_t new_size) { 109 return real_icing_filesystem_.Truncate(filename, new_size); 110 }); 111 112 ON_CALL(*this, Grow).WillByDefault([this](int fd, uint64_t new_size) { 113 return real_icing_filesystem_.Grow(fd, new_size); 114 }); 115 116 ON_CALL(*this, GrowUsingPWrite) 117 .WillByDefault([this](int fd, uint64_t new_size) { 118 return real_icing_filesystem_.GrowUsingPWrite(fd, new_size); 119 }); 120 121 ON_CALL(*this, Write) 122 .WillByDefault([this](int fd, const void *data, size_t data_size) { 123 return real_icing_filesystem_.Write(fd, data, data_size); 124 }); 125 ON_CALL(*this, PWrite) 126 .WillByDefault( 127 [this](int fd, off_t offset, const void *data, size_t data_size) { 128 return real_icing_filesystem_.PWrite(fd, offset, data, data_size); 129 }); 130 131 ON_CALL(*this, DataSync).WillByDefault([this](int fd) { 132 return real_icing_filesystem_.DataSync(fd); 133 }); 134 135 ON_CALL(*this, RenameFile) 136 .WillByDefault([this](const char *old_name, const char *new_name) { 137 return real_icing_filesystem_.RenameFile(old_name, new_name); 138 }); 139 140 ON_CALL(*this, SwapFiles) 141 .WillByDefault([this](const char *one, const char *two) { 142 return real_icing_filesystem_.SwapFiles(one, two); 143 }); 144 145 ON_CALL(*this, CreateDirectory).WillByDefault([this](const char *dir_name) { 146 return real_icing_filesystem_.CreateDirectory(dir_name); 147 }); 148 149 ON_CALL(*this, CreateDirectoryRecursively) 150 .WillByDefault([this](const char *dir_name) { 151 return real_icing_filesystem_.CreateDirectoryRecursively(dir_name); 152 }); 153 154 ON_CALL(*this, CopyFile) 155 .WillByDefault([this](const char *src, const char *dst) { 156 return real_icing_filesystem_.CopyFile(src, dst); 157 }); 158 159 ON_CALL(*this, ComputeChecksum) 160 .WillByDefault([this](int fd, uint32_t *checksum, uint64_t offset, 161 uint64_t length) { 162 return real_icing_filesystem_.ComputeChecksum(fd, checksum, offset, 163 length); 164 }); 165 166 ON_CALL(*this, GetDiskUsage).WillByDefault([this](const char *path) { 167 return real_icing_filesystem_.GetDiskUsage(path); 168 }); 169 } 170 171 MOCK_METHOD(bool, DeleteFile, (const char *file_name), (const, override)); 172 173 MOCK_METHOD(bool, DeleteDirectory, (const char *dir_name), (const, override)); 174 175 MOCK_METHOD(bool, DeleteDirectoryRecursively, (const char *dir_name), 176 (const, override)); 177 178 MOCK_METHOD(bool, FileExists, (const char *file_name), (const, override)); 179 180 MOCK_METHOD(bool, DirectoryExists, (const char *dir_name), (const, override)); 181 182 MOCK_METHOD(int, GetBasenameIndex, (const char *file_name), 183 (const, override)); 184 185 MOCK_METHOD(std::string, GetBasename, (const char *file_name), 186 (const, override)); 187 188 MOCK_METHOD(std::string, GetDirname, (const char *file_name), 189 (const, override)); 190 191 MOCK_METHOD(bool, ListDirectory, 192 (const char *dir_name, std::vector<std::string> *entries), 193 (const, override)); 194 195 MOCK_METHOD(bool, GetMatchingFiles, 196 (const char *glob, std::vector<std::string> *matches), 197 (const, override)); 198 199 MOCK_METHOD(int, OpenForWrite, (const char *file_name), (const, override)); 200 201 MOCK_METHOD(int, OpenForAppend, (const char *file_name), (const, override)); 202 203 MOCK_METHOD(int, OpenForRead, (const char *file_name), (const, override)); 204 205 MOCK_METHOD(uint64_t, GetFileSize, (int fd), (const, override)); 206 207 MOCK_METHOD(uint64_t, GetFileSize, (const char *filename), (const, override)); 208 209 MOCK_METHOD(bool, Truncate, (int fd, uint64_t new_size), (const, override)); 210 211 MOCK_METHOD(bool, Truncate, (const char *filename, uint64_t new_size), 212 (const, override)); 213 214 MOCK_METHOD(bool, Grow, (int fd, uint64_t new_size), (const, override)); 215 216 MOCK_METHOD(bool, GrowUsingPWrite, (int fd, uint64_t new_size), 217 (const, override)); 218 219 MOCK_METHOD(bool, Write, (int fd, const void *data, size_t data_size), 220 (const, override)); 221 MOCK_METHOD(bool, PWrite, 222 (int fd, off_t offset, const void *data, size_t data_size), 223 (const, override)); 224 225 MOCK_METHOD(bool, DataSync, (int fd), (const, override)); 226 227 MOCK_METHOD(bool, RenameFile, (const char *old_name, const char *new_name), 228 (const, override)); 229 230 MOCK_METHOD(bool, SwapFiles, (const char *one, const char *two), 231 (const, override)); 232 233 MOCK_METHOD(bool, CreateDirectory, (const char *dir_name), (const, override)); 234 235 MOCK_METHOD(bool, CreateDirectoryRecursively, (const char *dir_name), 236 (const, override)); 237 238 MOCK_METHOD(bool, CopyFile, (const char *src, const char *dst), 239 (const, override)); 240 241 MOCK_METHOD(bool, ComputeChecksum, 242 (int fd, uint32_t *checksum, uint64_t offset, uint64_t length), 243 (const, override)); 244 245 MOCK_METHOD(uint64_t, GetDiskUsage, (const char *path), (const, override)); 246 247 private: 248 IcingFilesystem real_icing_filesystem_; 249 }; 250 251 } // namespace lib 252 } // namespace icing 253 254 #endif // ICING_LEGACY_INDEX_ICING_MOCK_FILESYSTEM_H_ 255