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