bb7ba677 |
1 | m4_dnl hcommon.c |
2 | m4_dnl (part of complex test harness, not of the library) |
3 | m4_dnl - routines used for both record and playback |
4 | |
d942707d |
5 | m4_dnl This file is |
3d5cde09 |
6 | m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
d942707d |
7 | m4_dnl |
8 | m4_dnl It is part of adns, which is |
3d5cde09 |
9 | m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
d942707d |
10 | m4_dnl Copyright (C) 1999 Tony Finch <dot@dotat.at> |
bb7ba677 |
11 | m4_dnl |
12 | m4_dnl This program is free software; you can redistribute it and/or modify |
13 | m4_dnl it under the terms of the GNU General Public License as published by |
14 | m4_dnl the Free Software Foundation; either version 2, or (at your option) |
15 | m4_dnl any later version. |
16 | m4_dnl |
17 | m4_dnl This program is distributed in the hope that it will be useful, |
18 | m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | m4_dnl GNU General Public License for more details. |
21 | m4_dnl |
22 | m4_dnl You should have received a copy of the GNU General Public License |
23 | m4_dnl along with this program; if not, write to the Free Software Foundation, |
24 | m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | |
26 | m4_include(hmacros.i4) |
a4f094dd |
27 | |
7d251914 |
28 | #include <fcntl.h> |
29 | #include <string.h> |
30 | #include <errno.h> |
914a5ff5 |
31 | #include <stdlib.h> |
7d251914 |
32 | |
33 | #include <sys/socket.h> |
34 | #include <netinet/in.h> |
35 | #include <arpa/inet.h> |
36 | |
a4f094dd |
37 | #include "harness.h" |
38 | #include "internal.h" |
39 | |
7d251914 |
40 | vbuf vb; |
41 | FILE *Toutputfile= 0; |
98b6d5b4 |
42 | struct timeval currenttime; |
7d251914 |
43 | |
44 | const struct Terrno Terrnos[]= { |
a80db454 |
45 | { "EBADF", EBADF }, |
7d251914 |
46 | { "EAGAIN", EAGAIN }, |
47 | { "EINPROGRESS", EINPROGRESS }, |
48 | { "EINTR", EINTR }, |
49 | { "EINVAL", EINVAL }, |
50 | { "EMSGSIZE", EMSGSIZE }, |
51 | { "ENOBUFS", ENOBUFS }, |
52 | { "ENOENT", ENOENT }, |
53 | { "ENOPROTOOPT", ENOPROTOOPT }, |
54 | { "ENOSPC", ENOSPC }, |
55 | { "EWOULDBLOCK", EWOULDBLOCK }, |
a00dbcb2 |
56 | { "EHOSTUNREACH", EHOSTUNREACH }, |
fb901bf5 |
57 | { "ECONNRESET", ECONNRESET }, |
a00dbcb2 |
58 | { "ECONNREFUSED", ECONNREFUSED }, |
59 | { "EPIPE", EPIPE }, |
7d251914 |
60 | { 0, 0 } |
61 | }; |
a4f094dd |
62 | |
70a778e5 |
63 | static vbuf vbw; |
7d251914 |
64 | |
98b6d5b4 |
65 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
66 | Tmust("gettimeofday","tz",!tz); |
67 | *tv= currenttime; |
68 | return 0; |
69 | } |
70 | |
70a778e5 |
71 | int Hwritev(int fd, const struct iovec *vector, size_t count) { |
72 | size_t i; |
73 | |
74 | vbw.used= 0; |
75 | for (i=0; i<count; i++, vector++) { |
76 | if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); |
77 | } |
78 | return Hwrite(fd,vbw.buf,vbw.used); |
79 | } |
7d251914 |
80 | |
bb7ba677 |
81 | m4_define(`hm_syscall', ` |
82 | hm_create_proto_q |
83 | void Q$1(hm_args_massage($3,void)) { |
84 | |
85 | vb.used= 0; |
86 | Tvba("$1"); |
87 | m4_define(`hm_na',`') |
88 | m4_define(`hm_arg_nullptr',`') |
89 | m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);') |
90 | m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);') |
8b1171e6 |
91 | m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);') |
bb7ba677 |
92 | m4_define(`hm_arg_timeval_in_rel_null', ` |
93 | if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec); |
94 | else Tvba(" $'`1=null");') |
95 | m4_define(`hm_arg_must', `') |
96 | m4_define(`hm_arg_socktype', ` |
97 | Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");') |
98 | m4_define(`hm_arg_ign', `') |
99 | m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);') |
100 | m4_define(`hm_arg_fcntl_cmd_arg', ` |
101 | if ($'`1 == F_SETFL) { |
73c4c523 |
102 | Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&..."); |
103 | } else if ($'`1 == F_GETFL) { |
bb7ba677 |
104 | Tvba(" $'`1=F_GETFL"); |
73c4c523 |
105 | } else { |
106 | Tmust("$'`1","F_GETFL/F_SETFL",0); |
bb7ba677 |
107 | }') |
70a778e5 |
108 | m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);') |
109 | m4_define(`hm_arg_bytes_in', `') |
bb7ba677 |
110 | m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);') |
111 | m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);') |
112 | $3 |
7d251914 |
113 | |
70a778e5 |
114 | hm_create_nothing |
115 | m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);') |
116 | $3 |
117 | |
7d251914 |
118 | Q_vb(); |
119 | } |
bb7ba677 |
120 | ') |
7d251914 |
121 | |
bb7ba677 |
122 | m4_include(`hsyscalls.i4') |
7d251914 |
123 | |
124 | |
125 | void Tvbaddr(const struct sockaddr *addr, int len) { |
126 | const struct sockaddr_in *ai= (const struct sockaddr_in*)addr; |
127 | |
128 | assert(len==sizeof(struct sockaddr_in)); |
129 | assert(ai->sin_family==AF_INET); |
bb7ba677 |
130 | Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port)); |
7d251914 |
131 | } |
132 | |
133 | void Tvbbytes(const void *buf, int len) { |
134 | const byte *bp; |
135 | int i; |
136 | |
68000374 |
137 | if (!len) { Tvba("\n ."); return; } |
7d251914 |
138 | for (i=0, bp=buf; i<len; i++, bp++) { |
139 | if (!(i&31)) Tvba("\n "); |
140 | else if (!(i&3)) Tvba(" "); |
141 | Tvbf("%02x",*bp); |
142 | } |
70a778e5 |
143 | Tvba("."); |
7d251914 |
144 | } |
145 | |
146 | void Tvbfdset(int max, const fd_set *fds) { |
147 | int i; |
148 | const char *comma= ""; |
149 | |
bb7ba677 |
150 | Tvba("["); |
7d251914 |
151 | for (i=0; i<max; i++) { |
152 | if (!FD_ISSET(i,fds)) continue; |
153 | Tvba(comma); |
154 | Tvbf("%d",i); |
155 | comma= ","; |
156 | } |
157 | Tvba("]"); |
158 | } |
159 | |
8b1171e6 |
160 | static void Tvbpollevents(int events) { |
161 | const char *delim= ""; |
162 | |
163 | events &= (POLLIN|POLLOUT|POLLPRI); |
164 | if (!events) { Tvba("0"); return; } |
165 | if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; } |
166 | if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; } |
167 | if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); } |
168 | } |
169 | |
170 | void Tvbpollfds(const struct pollfd *fds, int nfds) { |
171 | const char *comma= ""; |
172 | |
173 | Tvba("["); |
174 | while (nfds>0) { |
175 | Tvba(comma); |
176 | Tvbf("{fd=%d, events=",fds->fd); |
177 | Tvbpollevents(fds->events); |
178 | Tvba(", revents="); |
179 | Tvbpollevents(fds->revents); |
180 | Tvba("}"); |
181 | comma= ", "; |
182 | nfds--; fds++; |
183 | } |
184 | Tvba("]"); |
185 | } |
186 | |
7d251914 |
187 | void Tvberrno(int e) { |
188 | const struct Terrno *te; |
a4f094dd |
189 | |
7d251914 |
190 | for (te= Terrnos; te->n && te->v != e; te++); |
43bb811f |
191 | assert(te->n); |
192 | Tvba(te->n); |
a4f094dd |
193 | } |
194 | |
7d251914 |
195 | void Tvba(const char *str) { |
196 | if (!adns__vbuf_appendstr(&vb,str)) Tnomem(); |
197 | } |
a4f094dd |
198 | |
7d251914 |
199 | void Tvbvf(const char *fmt, va_list al) { |
200 | char buf[1000]; |
201 | buf[sizeof(buf)-2]= '\t'; |
202 | vsnprintf(buf,sizeof(buf),fmt,al); |
203 | assert(buf[sizeof(buf)-2] == '\t'); |
a4f094dd |
204 | |
7d251914 |
205 | Tvba(buf); |
206 | } |
207 | |
208 | void Tvbf(const char *fmt, ...) { |
209 | va_list al; |
210 | va_start(al,fmt); |
211 | Tvbvf(fmt,al); |
212 | va_end(al); |
213 | } |
214 | |
215 | |
bb7ba677 |
216 | void Tmust(const char *call, const char *arg, int cond) { |
217 | if (cond) return; |
218 | fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg); |
219 | exit(-1); |
220 | } |
221 | |
7d251914 |
222 | void Tfailed(const char *why) { |
223 | fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno)); |
224 | exit(-1); |
225 | } |
226 | |
227 | void Tnomem(void) { |
228 | Tfailed("unable to malloc/realloc"); |
229 | } |
230 | |
231 | void Toutputerr(void) { |
232 | Tfailed("write error on test harness output"); |
233 | } |
914a5ff5 |
234 | |
235 | struct malloced { |
236 | struct malloced *next, *back; |
237 | size_t sz; |
238 | unsigned long count; |
239 | struct { double d; long ul; void *p; void (*fp)(void); } data; |
240 | }; |
241 | |
242 | static unsigned long malloccount, mallocfailat; |
243 | static struct { struct malloced *head, *tail; } mallocedlist; |
244 | |
245 | #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head) |
246 | |
247 | void *Hmalloc(size_t sz) { |
248 | struct malloced *newnode; |
249 | const char *mfavar; |
250 | char *ep; |
251 | |
252 | assert(sz); |
253 | |
254 | newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem(); |
255 | |
256 | LIST_LINK_TAIL(mallocedlist,newnode); |
257 | newnode->sz= sz; |
258 | newnode->count= ++malloccount; |
259 | if (!mallocfailat) { |
260 | mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT"); |
261 | if (mfavar) { |
262 | mallocfailat= strtoul(mfavar,&ep,10); |
263 | if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value"); |
264 | } else { |
265 | mallocfailat= ~0UL; |
266 | } |
267 | } |
268 | assert(newnode->count != mallocfailat); |
269 | memset(&newnode->data,0xc7,sz); |
270 | return &newnode->data; |
271 | } |
272 | |
273 | void Hfree(void *ptr) { |
274 | struct malloced *oldnode; |
275 | |
276 | if (!ptr) return; |
277 | |
278 | oldnode= (void*)((char*)ptr - MALLOCHSZ); |
279 | LIST_UNLINK(mallocedlist,oldnode); |
280 | memset(&oldnode->data,0x38,oldnode->sz); |
281 | free(oldnode); |
282 | } |
283 | |
284 | void *Hrealloc(void *op, size_t nsz) { |
285 | struct malloced *oldnode; |
286 | void *np; |
287 | size_t osz; |
288 | |
289 | if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; } |
290 | np= Hmalloc(nsz); |
291 | memcpy(np,op, osz>nsz ? nsz : osz); |
292 | Hfree(op); |
293 | return np; |
294 | } |
295 | |
296 | void Hexit(int rv) { |
297 | struct malloced *loopnode; |
298 | |
299 | Tshutdown(); |
300 | adns__vbuf_free(&vb); |
301 | adns__vbuf_free(&vbw); |
302 | if (mallocedlist.head) { |
303 | fprintf(stderr,"adns test harness: memory leaked:"); |
304 | for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next) |
305 | fprintf(stderr," %lu(%lu)",loopnode->count,(unsigned long)loopnode->sz); |
306 | putc('\n',stderr); |
307 | if (ferror(stderr)) exit(-1); |
308 | } |
309 | exit(rv); |
310 | } |