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