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