• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, Open(::testing::_, ::testing::_, ::testing::_))
43         .WillByDefault(::testing::Return(kDefaultFileDescriptor));
44     ON_CALL(*this, Read(::testing::_, ::testing::_, ::testing::_, ::testing::_))
45         .WillByDefault(::testing::ReturnArg<2>());
46     ON_CALL(*this, ReadLink(::testing::_, ::testing::_, ::testing::_))
47         .WillByDefault(::testing::ReturnArg<2>());
48     ON_CALL(*this, Unlink(::testing::_)).WillByDefault(::testing::Return(0));
49     ON_CALL(*this, Write(::testing::_, ::testing::_, ::testing::_, ::testing::_))
50         .WillByDefault(::testing::ReturnArg<2>());
51   }
52 
53   MOCK_METHOD(int, Access, (const std::string& path_name, int mode), (override));
54   MOCK_METHOD(int, Close, (int fd), (override));
55   MOCK_METHOD(int, CloseDir, (DIR * dirp), (override));
56   MOCK_METHOD(int, FAdvise, (int fd, int64_t offset, int64_t len, int advice), (override));
57   MOCK_METHOD(int, FAllocate, (int fd, int mode, int64_t offset, int64_t len), (override));
58   MOCK_METHOD(int, FTruncate, (int fd, int64_t length), (override));
59   MOCK_METHOD(int, FStat, (int filedes, struct stat64* buf), (override));
60   MOCK_METHOD(int, FSync, (int fd), (override));
61   MOCK_METHOD(int, Open, (const std::string& path_name, int flags, int mode), (override));
62   MOCK_METHOD(DIR*, OpenDir, (const std::string& name), (override));
63   MOCK_METHOD(int64_t, Read, (int fd, char* buf, int64_t count, int64_t offset), (override));
64   MOCK_METHOD(struct dirent*, ReadDir, (DIR * dirp), (override));
65   MOCK_METHOD(int64_t, ReadLink, (const std::string& path_name, char* buf, int64_t bufsiz),
66               (override));
67   MOCK_METHOD(void, Sync, (), (override));
68   MOCK_METHOD(int, Unlink, (const std::string& path_name), (override));
69   MOCK_METHOD(int64_t, Write, (int fd, char* buf, int64_t count, int64_t offset), (override));
70 };
71