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