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