1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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 
16 #include "mock_system_func.h"
17 #include "dhcp_client_state_machine.h"
18 
19 using namespace OHOS::DHCP;
20 
21 static bool g_mockTag = false;
22 static int NUM_TWO = 2;
23 static int NUM_THREE = 3;
24 static int NUM_FOUR = 4;
25 
GetInstance()26 MockSystemFunc &MockSystemFunc::GetInstance()
27 {
28     static MockSystemFunc gMockSystemFunc;
29     return gMockSystemFunc;
30 };
31 
MockSystemFunc()32 MockSystemFunc::MockSystemFunc()
33 {}
34 
SetMockFlag(bool flag)35 void MockSystemFunc::SetMockFlag(bool flag)
36 {
37     g_mockTag = flag;
38 }
39 
GetMockFlag(void)40 bool MockSystemFunc::GetMockFlag(void)
41 {
42     return g_mockTag;
43 }
44 
45 extern "C" {
46 int __real_open(const char *__file, int __oflag);
__wrap_open(const char * __file,int __oflag)47 int __wrap_open(const char *__file, int __oflag)
48 {
49     if (g_mockTag) {
50         return MockSystemFunc::GetInstance().open(__file, __oflag);
51     } else {
52         return __real_open(__file, __oflag);
53     }
54 }
55 
56 int __real_close(int fd);
__wrap_close(int fd)57 int __wrap_close(int fd)
58 {
59     if (g_mockTag) {
60         return MockSystemFunc::GetInstance().close(fd);
61     } else {
62         return __real_close(fd);
63     }
64 }
65 
66 ssize_t __real_write(int fd, const void *buf, size_t count);
__wrap_write(int fd,const void * buf,size_t count)67 ssize_t __wrap_write(int fd, const void *buf, size_t count)
68 {
69     if (g_mockTag) {
70         return MockSystemFunc::GetInstance().write(fd, buf, count);
71     } else {
72         return __real_write(fd, buf, count);
73     }
74 }
75 
76 ssize_t __real_read(int fd, void *buf, size_t count);
__wrap_read(int fd,void * buf,size_t count)77 ssize_t __wrap_read(int fd, void *buf, size_t count)
78 {
79     if (g_mockTag) {
80         return MockSystemFunc::GetInstance().read(fd, buf, count);
81     } else {
82         return __real_read(fd, buf, count);
83     }
84 }
85 
86 int __real_socket(int __domain, int __type, int __protocol);
__wrap_socket(int __domain,int __type,int __protocol)87 int __wrap_socket(int __domain, int __type, int __protocol)
88 {
89     if (g_mockTag) {
90         return MockSystemFunc::GetInstance().socket(__domain, __type, __protocol);
91     } else {
92         return __real_socket(__domain, __type, __protocol);
93     }
94 }
95 
96 int __real_setsockopt(int __fd, int __level, int __optname, const void *__optval, socklen_t __optlen);
__wrap_setsockopt(int __fd,int __level,int __optname,const void * __optval,socklen_t __optlen)97 int __wrap_setsockopt(int __fd, int __level, int __optname, const void *__optval, socklen_t __optlen)
98 {
99     if (g_mockTag) {
100         return MockSystemFunc::GetInstance().setsockopt(__fd, __level, __optname, __optval, __optlen);
101     } else {
102         return __real_setsockopt(__fd, __level, __optname, __optval, __optlen);
103     }
104 }
105 
106 int __real_ioctl(int __fd, unsigned long __request, struct sockaddr *__ifreq);
__wrap_ioctl(int __fd,unsigned long __request,struct sockaddr * __ifreq)107 int __wrap_ioctl(int __fd, unsigned long __request, struct sockaddr *__ifreq)
108 {
109     if (g_mockTag) {
110         return MockSystemFunc::GetInstance().ioctl(__fd, __request, __ifreq);
111     } else {
112         return __real_ioctl(__fd, __request, __ifreq);
113     }
114 }
115 
116 int __real_bind(int __fd, const struct sockaddr *__addr, socklen_t __len);
__wrap_bind(int __fd,const struct sockaddr * __addr,socklen_t __len)117 int __wrap_bind(int __fd, const struct sockaddr *__addr, socklen_t __len)
118 {
119     if (g_mockTag) {
120         return MockSystemFunc::GetInstance().bind(__fd, __addr, __len);
121     } else {
122         return __real_bind(__fd, __addr, __len);
123     }
124 }
125 
126 int __real_listen(int __fd, int __n);
__wrap_listen(int __fd,int __n)127 int __wrap_listen(int __fd, int __n)
128 {
129     if (g_mockTag) {
130         return MockSystemFunc::GetInstance().listen(__fd, __n);
131     } else {
132         return __real_listen(__fd, __n);
133     }
134 }
135 
136 int __real_connect(int __fd, const struct sockaddr *__addr, socklen_t __len);
__wrap_connect(int __fd,const struct sockaddr * __addr,socklen_t __len)137 int __wrap_connect(int __fd, const struct sockaddr *__addr, socklen_t __len)
138 {
139     if (g_mockTag) {
140         return MockSystemFunc::GetInstance().connect(__fd, __addr, __len);
141     } else {
142         return __real_connect(__fd, __addr, __len);
143     }
144 }
145 
146 int __real_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
__wrap_select(int nfds,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout)147 int __wrap_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
148 {
149     std::unique_ptr<OHOS::DHCP::DhcpClientStateMachine> testMachine;
150     if (g_mockTag) {
151         int nRet = MockSystemFunc::GetInstance().select(nfds, readfds, writefds, exceptfds, timeout);
152         FD_ZERO(readfds);
153         if (nRet == 1) {
154             FD_SET(testMachine->GetPacketReadSockFd(), readfds);
155         } else if (nRet == NUM_TWO) {
156             FD_SET(testMachine->GetSigReadSockFd(), readfds);
157         } else if (nRet == NUM_FOUR) {
158             FD_SET(testMachine->GetSigReadSockFd(), readfds);
159         }
160         return nRet;
161     } else {
162         return __real_select(nfds, readfds, writefds, exceptfds, timeout);
163     }
164 }
165 
166 ssize_t __real_sendto(int fd, const void *buf, size_t count, int flags, const struct sockaddr *addr, socklen_t len);
__wrap_sendto(int fd,const void * buf,size_t count,int flags,const struct sockaddr * addr,socklen_t len)167 ssize_t __wrap_sendto(int fd, const void *buf, size_t count, int flags, const struct sockaddr *addr, socklen_t len)
168 {
169     if (g_mockTag) {
170         return MockSystemFunc::GetInstance().sendto(fd, buf, count, flags, addr, len);
171     } else {
172         return __real_sendto(fd, buf, count, flags, addr, len);
173     }
174 }
175 
176 int __real_socketpair(int address, int type, int protocol, int *socket);
__wrap_socketpair(int address,int type,int protocol,int * socket)177 int __wrap_socketpair(int address, int type, int protocol, int *socket)
178 {
179     if (g_mockTag) {
180         return MockSystemFunc::GetInstance().socketpair(address, type, protocol, socket);
181     } else {
182         return __real_socketpair(address, type, protocol, socket);
183     }
184 }
185 }
186