Joel Scherpelz | f3fa5cc | 2017-05-22 12:30:03 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef NETUTILS_MOCK_SYSCALLS_H |
| 18 | #define NETUTILS_MOCK_SYSCALLS_H |
| 19 | |
| 20 | #include <atomic> |
| 21 | #include <cassert> |
| 22 | #include <memory> |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include "netdutils/Syscalls.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace netdutils { |
| 31 | |
| 32 | class MockSyscalls : public Syscalls { |
| 33 | public: |
| 34 | virtual ~MockSyscalls() = default; |
| 35 | // Use Return(ByMove(...)) to deal with movable return types. |
| 36 | MOCK_CONST_METHOD3(socket, StatusOr<UniqueFd>(int domain, int type, int protocol)); |
| 37 | MOCK_CONST_METHOD3(getsockname, Status(Fd sock, sockaddr* addr, socklen_t* addrlen)); |
| 38 | MOCK_CONST_METHOD5(setsockopt, Status(Fd sock, int level, int optname, const void* optval, |
| 39 | socklen_t optlen)); |
| 40 | |
| 41 | MOCK_CONST_METHOD3(bind, Status(Fd sock, const sockaddr* addr, socklen_t addrlen)); |
| 42 | MOCK_CONST_METHOD3(connect, Status(Fd sock, const sockaddr* addr, socklen_t addrlen)); |
| 43 | |
| 44 | // Use Return(ByMove(...)) to deal with movable return types. |
| 45 | MOCK_CONST_METHOD2(eventfd, StatusOr<UniqueFd>(unsigned int initval, int flags)); |
| 46 | MOCK_CONST_METHOD3(ppoll, StatusOr<int>(pollfd* fds, nfds_t nfds, double timeout)); |
| 47 | MOCK_CONST_METHOD2(write, StatusOr<size_t>(Fd fd, const Slice buf)); |
| 48 | MOCK_CONST_METHOD2(read, StatusOr<Slice>(Fd fd, const Slice buf)); |
| 49 | MOCK_CONST_METHOD5(sendto, StatusOr<size_t>(Fd sock, const Slice buf, int flags, |
| 50 | const sockaddr* dst, socklen_t dstlen)); |
| 51 | MOCK_CONST_METHOD5(recvfrom, StatusOr<Slice>(Fd sock, const Slice dst, int flags, sockaddr* src, |
| 52 | socklen_t* srclen)); |
| 53 | MOCK_CONST_METHOD2(shutdown, Status(Fd fd, int how)); |
| 54 | MOCK_CONST_METHOD1(close, Status(Fd fd)); |
| 55 | }; |
| 56 | |
| 57 | // For the lifetime of this mock, replace the contents of sSyscalls |
| 58 | // with a pointer to this mock. Behavior is undefined if multiple |
| 59 | // ScopedMockSyscalls instances exist concurrently. |
| 60 | class ScopedMockSyscalls : public MockSyscalls { |
| 61 | public: |
| 62 | ScopedMockSyscalls() : mOld(sSyscalls.swap(*this)) { assert((mRefcount++) == 1); } |
| 63 | virtual ~ScopedMockSyscalls() { |
| 64 | sSyscalls.swap(mOld); |
| 65 | assert((mRefcount--) == 0); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | std::atomic<int> mRefcount{0}; |
| 70 | Syscalls& mOld; |
| 71 | }; |
| 72 | |
| 73 | } // namespace netdutils |
| 74 | } // namespace android |
| 75 | |
| 76 | #endif /* NETUTILS_MOCK_SYSCALLS_H */ |