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