1 // Copyright (C) 2021 The Android Open Source Project 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 #pragma once 16 17 #include <gmock/gmock.h> 18 19 #include <ditto/syscall.h> 20 21 class MockSyscall : public dittosuite::SyscallInterface { 22 public: 23 static constexpr int kDefaultFileSize = 4096; 24 static constexpr int kDefaultFileDescriptor = 4; 25 26 // Set default returns for each syscall (mostly return 0 to indicate a successful call) MockSyscall()27 MockSyscall() { 28 ON_CALL(*this, Access(::testing::_, ::testing::_)).WillByDefault(::testing::Return(0)); 29 ON_CALL(*this, Close(::testing::_)).WillByDefault(::testing::Return(0)); 30 ON_CALL(*this, CloseDir(::testing::_)).WillByDefault(::testing::Return(0)); 31 ON_CALL(*this, FAdvise(::testing::_, ::testing::_, ::testing::_, ::testing::_)) 32 .WillByDefault(::testing::Return(0)); 33 ON_CALL(*this, FAllocate(::testing::_, ::testing::_, ::testing::_, ::testing::_)) 34 .WillByDefault(::testing::Return(0)); 35 ON_CALL(*this, FTruncate(::testing::_, ::testing::_)).WillByDefault(::testing::Return(0)); 36 ON_CALL(*this, FStat(::testing::_, ::testing::_)) 37 .WillByDefault(::testing::Invoke([](int, struct stat64* buf) { 38 buf->st_size = kDefaultFileSize; 39 return 0; 40 })); 41 ON_CALL(*this, FSync(::testing::_)).WillByDefault(::testing::Return(0)); 42 ON_CALL(*this, GetTid()).WillByDefault(::testing::Return(0)); 43 ON_CALL(*this, Open(::testing::_, ::testing::_, ::testing::_)) 44 .WillByDefault(::testing::Return(kDefaultFileDescriptor)); 45 ON_CALL(*this, Read(::testing::_, ::testing::_, ::testing::_, ::testing::_)) 46 .WillByDefault(::testing::ReturnArg<2>()); 47 ON_CALL(*this, ReadLink(::testing::_, ::testing::_, ::testing::_)) 48 .WillByDefault(::testing::ReturnArg<2>()); 49 ON_CALL(*this, SchedSetattr(::testing::_, ::testing::_, ::testing::_)) 50 .WillByDefault(::testing::Return(0)); 51 ON_CALL(*this, Unlink(::testing::_)).WillByDefault(::testing::Return(0)); 52 ON_CALL(*this, Write(::testing::_, ::testing::_, ::testing::_, ::testing::_)) 53 .WillByDefault(::testing::ReturnArg<2>()); 54 } 55 56 MOCK_METHOD(int, Access, (const std::string& path_name, int mode), (override)); 57 MOCK_METHOD(int, Close, (int fd), (override)); 58 MOCK_METHOD(int, CloseDir, (DIR * dirp), (override)); 59 MOCK_METHOD(int, FAdvise, (int fd, int64_t offset, int64_t len, int advice), (override)); 60 MOCK_METHOD(int, FAllocate, (int fd, int mode, int64_t offset, int64_t len), (override)); 61 MOCK_METHOD(int, FTruncate, (int fd, int64_t length), (override)); 62 MOCK_METHOD(int, FStat, (int filedes, struct stat64* buf), (override)); 63 MOCK_METHOD(int, FSync, (int fd), (override)); 64 MOCK_METHOD(pid_t, GetTid, (), (override)); 65 MOCK_METHOD(int, Open, (const std::string& path_name, int flags, int mode), (override)); 66 MOCK_METHOD(DIR*, OpenDir, (const std::string& name), (override)); 67 MOCK_METHOD(int64_t, Read, (int fd, char* buf, int64_t count, int64_t offset), (override)); 68 MOCK_METHOD(int, SchedSetattr, 69 (pid_t pid, const dittosuite::SchedAttr__& attr, unsigned int flags), (override)); 70 MOCK_METHOD(struct dirent*, ReadDir, (DIR * dirp), (override)); 71 MOCK_METHOD(int64_t, ReadLink, (const std::string& path_name, char* buf, int64_t bufsiz), 72 (override)); 73 MOCK_METHOD(void, Sync, (), (override)); 74 MOCK_METHOD(int, Unlink, (const std::string& path_name), (override)); 75 MOCK_METHOD(int64_t, Write, (int fd, char* buf, int64_t count, int64_t offset), (override)); 76 MOCK_METHOD(int, LockMutex, (pthread_mutex_t*), (override)); 77 MOCK_METHOD(int, UnlockMutex, (pthread_mutex_t*), (override)); 78 }; 79