+ Bugfixes:
[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>
d942707d 10m4_dnl Copyright (C) 1999 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>
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 },
7d251914 63 { 0, 0 }
64};
a4f094dd 65
70a778e5 66static vbuf vbw;
7d251914 67
98b6d5b4 68int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
69 Tmust("gettimeofday","tz",!tz);
70 *tv= currenttime;
71 return 0;
72}
73
70a778e5 74int Hwritev(int fd, const struct iovec *vector, size_t count) {
75 size_t i;
76
77 vbw.used= 0;
78 for (i=0; i<count; i++, vector++) {
79 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
80 }
81 return Hwrite(fd,vbw.buf,vbw.used);
82}
7d251914 83
bb7ba677 84m4_define(`hm_syscall', `
85 hm_create_proto_q
86void Q$1(hm_args_massage($3,void)) {
87
88 vb.used= 0;
89 Tvba("$1");
90 m4_define(`hm_na',`')
91 m4_define(`hm_arg_nullptr',`')
92 m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
93 m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
8b1171e6 94 m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
bb7ba677 95 m4_define(`hm_arg_timeval_in_rel_null', `
96 if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
97 else Tvba(" $'`1=null");')
98 m4_define(`hm_arg_must', `')
99 m4_define(`hm_arg_socktype', `
100 Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
101 m4_define(`hm_arg_ign', `')
102 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
103 m4_define(`hm_arg_fcntl_cmd_arg', `
104 if ($'`1 == F_SETFL) {
73c4c523 105 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
106 } else if ($'`1 == F_GETFL) {
bb7ba677 107 Tvba(" $'`1=F_GETFL");
73c4c523 108 } else {
109 Tmust("$'`1","F_GETFL/F_SETFL",0);
bb7ba677 110 }')
70a778e5 111 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
112 m4_define(`hm_arg_bytes_in', `')
bb7ba677 113 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
114 m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);')
115 $3
7d251914 116
70a778e5 117 hm_create_nothing
118 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
119 $3
120
7d251914 121 Q_vb();
122}
bb7ba677 123')
7d251914 124
bb7ba677 125m4_include(`hsyscalls.i4')
7d251914 126
127
128void Tvbaddr(const struct sockaddr *addr, int len) {
129 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
130
131 assert(len==sizeof(struct sockaddr_in));
132 assert(ai->sin_family==AF_INET);
bb7ba677 133 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
7d251914 134}
135
136void Tvbbytes(const void *buf, int len) {
137 const byte *bp;
138 int i;
139
68000374 140 if (!len) { Tvba("\n ."); return; }
7d251914 141 for (i=0, bp=buf; i<len; i++, bp++) {
142 if (!(i&31)) Tvba("\n ");
143 else if (!(i&3)) Tvba(" ");
144 Tvbf("%02x",*bp);
145 }
70a778e5 146 Tvba(".");
7d251914 147}
148
149void Tvbfdset(int max, const fd_set *fds) {
150 int i;
151 const char *comma= "";
152
bb7ba677 153 Tvba("[");
7d251914 154 for (i=0; i<max; i++) {
155 if (!FD_ISSET(i,fds)) continue;
156 Tvba(comma);
157 Tvbf("%d",i);
158 comma= ",";
159 }
160 Tvba("]");
161}
162
8b1171e6 163static void Tvbpollevents(int events) {
164 const char *delim= "";
165
166 events &= (POLLIN|POLLOUT|POLLPRI);
167 if (!events) { Tvba("0"); return; }
168 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
169 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
170 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
171}
172
173void Tvbpollfds(const struct pollfd *fds, int nfds) {
174 const char *comma= "";
175
176 Tvba("[");
177 while (nfds>0) {
178 Tvba(comma);
179 Tvbf("{fd=%d, events=",fds->fd);
180 Tvbpollevents(fds->events);
181 Tvba(", revents=");
182 Tvbpollevents(fds->revents);
183 Tvba("}");
184 comma= ", ";
185 nfds--; fds++;
186 }
187 Tvba("]");
188}
189
7d251914 190void Tvberrno(int e) {
191 const struct Terrno *te;
a4f094dd 192
7d251914 193 for (te= Terrnos; te->n && te->v != e; te++);
43bb811f 194 assert(te->n);
195 Tvba(te->n);
a4f094dd 196}
197
7d251914 198void Tvba(const char *str) {
199 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
200}
a4f094dd 201
7d251914 202void Tvbvf(const char *fmt, va_list al) {
203 char buf[1000];
204 buf[sizeof(buf)-2]= '\t';
205 vsnprintf(buf,sizeof(buf),fmt,al);
206 assert(buf[sizeof(buf)-2] == '\t');
a4f094dd 207
7d251914 208 Tvba(buf);
209}
210
211void Tvbf(const char *fmt, ...) {
212 va_list al;
213 va_start(al,fmt);
214 Tvbvf(fmt,al);
215 va_end(al);
216}
217
218
bb7ba677 219void Tmust(const char *call, const char *arg, int cond) {
220 if (cond) return;
221 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
222 exit(-1);
223}
224
7d251914 225void Tfailed(const char *why) {
226 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
227 exit(-1);
228}
229
230void Tnomem(void) {
231 Tfailed("unable to malloc/realloc");
232}
233
234void Toutputerr(void) {
235 Tfailed("write error on test harness output");
236}
914a5ff5 237
238struct malloced {
239 struct malloced *next, *back;
240 size_t sz;
241 unsigned long count;
242 struct { double d; long ul; void *p; void (*fp)(void); } data;
243};
244
245static unsigned long malloccount, mallocfailat;
246static struct { struct malloced *head, *tail; } mallocedlist;
247
248#define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
249
250void *Hmalloc(size_t sz) {
251 struct malloced *newnode;
252 const char *mfavar;
253 char *ep;
254
255 assert(sz);
256
257 newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem();
258
259 LIST_LINK_TAIL(mallocedlist,newnode);
260 newnode->sz= sz;
261 newnode->count= ++malloccount;
262 if (!mallocfailat) {
263 mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
264 if (mfavar) {
265 mallocfailat= strtoul(mfavar,&ep,10);
266 if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
267 } else {
268 mallocfailat= ~0UL;
269 }
270 }
271 assert(newnode->count != mallocfailat);
272 memset(&newnode->data,0xc7,sz);
273 return &newnode->data;
274}
275
276void Hfree(void *ptr) {
277 struct malloced *oldnode;
278
279 if (!ptr) return;
280
281 oldnode= (void*)((char*)ptr - MALLOCHSZ);
282 LIST_UNLINK(mallocedlist,oldnode);
283 memset(&oldnode->data,0x38,oldnode->sz);
284 free(oldnode);
285}
286
287void *Hrealloc(void *op, size_t nsz) {
288 struct malloced *oldnode;
289 void *np;
290 size_t osz;
291
292 if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
293 np= Hmalloc(nsz);
294 memcpy(np,op, osz>nsz ? nsz : osz);
295 Hfree(op);
296 return np;
297}
298
299void Hexit(int rv) {
300 struct malloced *loopnode;
301
302 Tshutdown();
303 adns__vbuf_free(&vb);
304 adns__vbuf_free(&vbw);
305 if (mallocedlist.head) {
306 fprintf(stderr,"adns test harness: memory leaked:");
307 for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
308 fprintf(stderr," %lu(%lu)",loopnode->count,(unsigned long)loopnode->sz);
309 putc('\n',stderr);
310 if (ferror(stderr)) exit(-1);
311 }
312 exit(rv);
313}