blob: f57b55ca9d8eb031382528f655e049eb469bc412 [file] [log] [blame]
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +09001/*
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
29namespace android {
30namespace netdutils {
31
32class MockSyscalls : public Syscalls {
33 public:
34 virtual ~MockSyscalls() = default;
35 // Use Return(ByMove(...)) to deal with movable return types.
Joel Scherpelzde937962017-06-01 13:20:21 +090036 MOCK_CONST_METHOD3(open,
37 StatusOr<UniqueFd>(const std::string& pathname, int flags, mode_t mode));
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090038 MOCK_CONST_METHOD3(socket, StatusOr<UniqueFd>(int domain, int type, int protocol));
39 MOCK_CONST_METHOD3(getsockname, Status(Fd sock, sockaddr* addr, socklen_t* addrlen));
Benedict Wongb2daefb2017-12-06 22:05:46 -080040 MOCK_CONST_METHOD5(getsockopt, Status(Fd sock, int level, int optname, void* optval,
41 socklen_t *optlen));
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090042 MOCK_CONST_METHOD5(setsockopt, Status(Fd sock, int level, int optname, const void* optval,
43 socklen_t optlen));
44
45 MOCK_CONST_METHOD3(bind, Status(Fd sock, const sockaddr* addr, socklen_t addrlen));
46 MOCK_CONST_METHOD3(connect, Status(Fd sock, const sockaddr* addr, socklen_t addrlen));
Luke Huangf7782042018-08-08 13:13:04 +080047 MOCK_CONST_METHOD3(ioctl, StatusOr<ifreq>(Fd sock, unsigned long request, ifreq* ifr));
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090048
49 // Use Return(ByMove(...)) to deal with movable return types.
50 MOCK_CONST_METHOD2(eventfd, StatusOr<UniqueFd>(unsigned int initval, int flags));
51 MOCK_CONST_METHOD3(ppoll, StatusOr<int>(pollfd* fds, nfds_t nfds, double timeout));
ludi4072ff22017-06-15 08:56:52 -070052
53 MOCK_CONST_METHOD2(writev, StatusOr<size_t>(Fd fd, const std::vector<iovec>& iov));
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090054 MOCK_CONST_METHOD2(write, StatusOr<size_t>(Fd fd, const Slice buf));
55 MOCK_CONST_METHOD2(read, StatusOr<Slice>(Fd fd, const Slice buf));
56 MOCK_CONST_METHOD5(sendto, StatusOr<size_t>(Fd sock, const Slice buf, int flags,
57 const sockaddr* dst, socklen_t dstlen));
58 MOCK_CONST_METHOD5(recvfrom, StatusOr<Slice>(Fd sock, const Slice dst, int flags, sockaddr* src,
59 socklen_t* srclen));
60 MOCK_CONST_METHOD2(shutdown, Status(Fd fd, int how));
61 MOCK_CONST_METHOD1(close, Status(Fd fd));
Joel Scherpelz01cc5492017-06-16 10:45:14 +090062
63 MOCK_CONST_METHOD2(fopen,
64 StatusOr<UniqueFile>(const std::string& path, const std::string& mode));
65 MOCK_CONST_METHOD3(vfprintf, StatusOr<int>(FILE* file, const char* format, va_list ap));
66 MOCK_CONST_METHOD3(vfscanf, StatusOr<int>(FILE* file, const char* format, va_list ap));
67 MOCK_CONST_METHOD1(fclose, Status(FILE* file));
Lorenzo Colitti2103b6b2017-08-14 11:38:18 +090068 MOCK_CONST_METHOD0(fork, StatusOr<pid_t>());
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090069};
70
71// For the lifetime of this mock, replace the contents of sSyscalls
72// with a pointer to this mock. Behavior is undefined if multiple
73// ScopedMockSyscalls instances exist concurrently.
74class ScopedMockSyscalls : public MockSyscalls {
75 public:
76 ScopedMockSyscalls() : mOld(sSyscalls.swap(*this)) { assert((mRefcount++) == 1); }
77 virtual ~ScopedMockSyscalls() {
78 sSyscalls.swap(mOld);
79 assert((mRefcount--) == 0);
80 }
81
82 private:
83 std::atomic<int> mRefcount{0};
84 Syscalls& mOld;
85};
86
87} // namespace netdutils
88} // namespace android
89
90#endif /* NETUTILS_MOCK_SYSCALLS_H */