• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 using namespace OHOS;
18 using namespace OHOS::Wifi;
19 
20 static bool g_mockTag = false;
21 
GetInstance()22 MockSystemFunc &MockSystemFunc::GetInstance()
23 {
24     static MockSystemFunc gMockSystemFunc;
25     return gMockSystemFunc;
26 };
27 
MockSystemFunc()28 MockSystemFunc::MockSystemFunc()
29 {}
30 
SetMockFlag(bool flag)31 void MockSystemFunc::SetMockFlag(bool flag)
32 {
33     g_mockTag = flag;
34 }
35 
GetMockFlag(void)36 bool MockSystemFunc::GetMockFlag(void)
37 {
38     return g_mockTag;
39 }
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
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         return MockSystemFunc::GetInstance().select(nfds, readfds, writefds, exceptfds, timeout);
149     } else {
150         return __real_select(nfds, readfds, writefds, exceptfds, timeout);
151     }
152 }
153 
154 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)155 ssize_t __wrap_sendto(int fd, const void *buf, size_t count, int flags, const struct sockaddr *addr, socklen_t len)
156 {
157     if (g_mockTag) {
158         return MockSystemFunc::GetInstance().sendto(fd, buf, count, flags, addr, len);
159     } else {
160         return __real_sendto(fd, buf, count, flags, addr, len);
161     }
162 }
163 
164 pid_t __real_vfork();
__wrap_vfork()165 pid_t __wrap_vfork()
166 {
167     if (g_mockTag) {
168         return MockSystemFunc::GetInstance().vfork();
169     } else {
170         return __real_vfork();
171     }
172 }
173 
174 int __real_execv(const char *__path, char *const *__argv);
__wrap_execv(const char * __path,char * const * __argv)175 int __wrap_execv(const char *__path, char *const *__argv)
176 {
177     if (g_mockTag) {
178         return MockSystemFunc::GetInstance().execv(__path, __argv);
179     } else {
180         return __real_execv(__path, __argv);
181     }
182 }
183 
184 void __real__exit(int status);
__wrap__exit(int status)185 void __wrap__exit(int status)
186 {
187     if (g_mockTag) {
188         MockSystemFunc::GetInstance()._exit(status);
189     } else {
190         __real__exit(status);
191     }
192     return;
193 }
194 
195 pid_t __real_waitpid(pid_t pid, int *status, int options);
__wrap_waitpid(pid_t pid,int * status,int options)196 pid_t __wrap_waitpid(pid_t pid, int *status, int options)
197 {
198     if (g_mockTag) {
199         return MockSystemFunc::GetInstance().waitpid(pid, status, options);
200     } else {
201         return __real_waitpid(pid, status, options);
202     }
203 }
204 
205 int __real_kill(pid_t pid, int sig);
__wrap_kill(pid_t pid,int sig)206 int __wrap_kill(pid_t pid, int sig)
207 {
208     if (g_mockTag) {
209         return MockSystemFunc::GetInstance().kill(pid, sig);
210     } else {
211         return __real_kill(pid, sig);
212     }
213 }
214 #ifdef __cplusplus
215 }
216 #endif
217