src/: Support IPv6 reverse queries.
[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
6m4_dnl Copyright (C) 1997-2000,2003,2006 Ian Jackson
7m4_dnl Copyright (C) 1999-2000,2003,2006 Tony Finch
8m4_dnl Copyright (C) 1991 Massachusetts Institute of Technology
9m4_dnl (See the file INSTALL for full details.)
bb7ba677 10m4_dnl
11m4_dnl This program is free software; you can redistribute it and/or modify
12m4_dnl it under the terms of the GNU General Public License as published by
13m4_dnl the Free Software Foundation; either version 2, or (at your option)
14m4_dnl any later version.
15m4_dnl
16m4_dnl This program is distributed in the hope that it will be useful,
17m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
18m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19m4_dnl GNU General Public License for more details.
20m4_dnl
21m4_dnl You should have received a copy of the GNU General Public License
22m4_dnl along with this program; if not, write to the Free Software Foundation,
23m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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");')
103 m4_define(`hm_arg_ign', `')
104 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
105 m4_define(`hm_arg_fcntl_cmd_arg', `
106 if ($'`1 == F_SETFL) {
73c4c523 107 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
108 } else if ($'`1 == F_GETFL) {
bb7ba677 109 Tvba(" $'`1=F_GETFL");
73c4c523 110 } else {
111 Tmust("$'`1","F_GETFL/F_SETFL",0);
bb7ba677 112 }')
70a778e5 113 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
114 m4_define(`hm_arg_bytes_in', `')
bb7ba677 115 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
4bc76b9c 116 m4_define(`hm_arg_addr_out', `')
bb7ba677 117 $3
7d251914 118
70a778e5 119 hm_create_nothing
120 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
121 $3
122
7d251914 123 Q_vb();
124}
bb7ba677 125')
7d251914 126
0ebff22d 127m4_define(`hm_specsyscall', `')
7d251914 128
0ebff22d 129m4_include(`hsyscalls.i4')
7d251914 130
131void Tvbaddr(const struct sockaddr *addr, int len) {
132 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
133
134 assert(len==sizeof(struct sockaddr_in));
135 assert(ai->sin_family==AF_INET);
bb7ba677 136 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
7d251914 137}
138
139void Tvbbytes(const void *buf, int len) {
140 const byte *bp;
141 int i;
142
68000374 143 if (!len) { Tvba("\n ."); return; }
7d251914 144 for (i=0, bp=buf; i<len; i++, bp++) {
145 if (!(i&31)) Tvba("\n ");
146 else if (!(i&3)) Tvba(" ");
147 Tvbf("%02x",*bp);
148 }
70a778e5 149 Tvba(".");
7d251914 150}
151
152void Tvbfdset(int max, const fd_set *fds) {
153 int i;
154 const char *comma= "";
155
bb7ba677 156 Tvba("[");
7d251914 157 for (i=0; i<max; i++) {
158 if (!FD_ISSET(i,fds)) continue;
159 Tvba(comma);
160 Tvbf("%d",i);
161 comma= ",";
162 }
163 Tvba("]");
164}
165
8b1171e6 166static void Tvbpollevents(int events) {
167 const char *delim= "";
168
169 events &= (POLLIN|POLLOUT|POLLPRI);
170 if (!events) { Tvba("0"); return; }
171 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
172 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
173 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
174}
175
176void Tvbpollfds(const struct pollfd *fds, int nfds) {
177 const char *comma= "";
178
179 Tvba("[");
180 while (nfds>0) {
181 Tvba(comma);
182 Tvbf("{fd=%d, events=",fds->fd);
183 Tvbpollevents(fds->events);
184 Tvba(", revents=");
185 Tvbpollevents(fds->revents);
186 Tvba("}");
187 comma= ", ";
188 nfds--; fds++;
189 }
190 Tvba("]");
191}
192
7d251914 193void Tvberrno(int e) {
194 const struct Terrno *te;
a4f094dd 195
7d251914 196 for (te= Terrnos; te->n && te->v != e; te++);
43bb811f 197 assert(te->n);
198 Tvba(te->n);
a4f094dd 199}
200
7d251914 201void Tvba(const char *str) {
202 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
203}
a4f094dd 204
7d251914 205void Tvbvf(const char *fmt, va_list al) {
206 char buf[1000];
207 buf[sizeof(buf)-2]= '\t';
208 vsnprintf(buf,sizeof(buf),fmt,al);
209 assert(buf[sizeof(buf)-2] == '\t');
a4f094dd 210
7d251914 211 Tvba(buf);
212}
213
214void Tvbf(const char *fmt, ...) {
215 va_list al;
216 va_start(al,fmt);
217 Tvbvf(fmt,al);
218 va_end(al);
219}
220
221
bb7ba677 222void Tmust(const char *call, const char *arg, int cond) {
223 if (cond) return;
224 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
225 exit(-1);
226}
227
7d251914 228void Tfailed(const char *why) {
229 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
230 exit(-1);
231}
232
233void Tnomem(void) {
234 Tfailed("unable to malloc/realloc");
235}
236
237void Toutputerr(void) {
238 Tfailed("write error on test harness output");
239}
914a5ff5 240
241struct malloced {
242 struct malloced *next, *back;
243 size_t sz;
244 unsigned long count;
245 struct { double d; long ul; void *p; void (*fp)(void); } data;
246};
247
248static unsigned long malloccount, mallocfailat;
249static struct { struct malloced *head, *tail; } mallocedlist;
250
251#define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
252
253void *Hmalloc(size_t sz) {
254 struct malloced *newnode;
255 const char *mfavar;
256 char *ep;
257
258 assert(sz);
259
260 newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem();
261
262 LIST_LINK_TAIL(mallocedlist,newnode);
263 newnode->sz= sz;
264 newnode->count= ++malloccount;
265 if (!mallocfailat) {
266 mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
267 if (mfavar) {
268 mallocfailat= strtoul(mfavar,&ep,10);
269 if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
270 } else {
271 mallocfailat= ~0UL;
272 }
273 }
274 assert(newnode->count != mallocfailat);
275 memset(&newnode->data,0xc7,sz);
276 return &newnode->data;
277}
278
279void Hfree(void *ptr) {
280 struct malloced *oldnode;
281
282 if (!ptr) return;
283
284 oldnode= (void*)((char*)ptr - MALLOCHSZ);
285 LIST_UNLINK(mallocedlist,oldnode);
286 memset(&oldnode->data,0x38,oldnode->sz);
287 free(oldnode);
288}
289
290void *Hrealloc(void *op, size_t nsz) {
291 struct malloced *oldnode;
292 void *np;
293 size_t osz;
294
295 if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
296 np= Hmalloc(nsz);
297 memcpy(np,op, osz>nsz ? nsz : osz);
298 Hfree(op);
299 return np;
300}
301
302void Hexit(int rv) {
303 struct malloced *loopnode;
304
305 Tshutdown();
306 adns__vbuf_free(&vb);
307 adns__vbuf_free(&vbw);
308 if (mallocedlist.head) {
309 fprintf(stderr,"adns test harness: memory leaked:");
310 for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
cb0a827b 311 fprintf(stderr," %lu",loopnode->count);
914a5ff5 312 putc('\n',stderr);
313 if (ferror(stderr)) exit(-1);
314 }
315 exit(rv);
316}
0e45654b 317
318pid_t Hgetpid(void) {
319 return 2264; /* just some number */
320}
321