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