2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - routines used for both record and playback
6 m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
8 m4_dnl It is part of adns, which is
9 m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
10 m4_dnl Copyright (C) 1999 Tony Finch <dot@dotat.at>
12 m4_dnl This program is free software; you can redistribute it and/or modify
13 m4_dnl it under the terms of the GNU General Public License as published by
14 m4_dnl the Free Software Foundation; either version 2, or (at your option)
15 m4_dnl any later version.
17 m4_dnl This program is distributed in the hope that it will be useful,
18 m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
19 m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 m4_dnl GNU General Public License for more details.
22 m4_dnl You should have received a copy of the GNU General Public License
23 m4_dnl along with this program; if not, write to the Free Software Foundation,
24 m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 m4_include(hmacros.i4)
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
45 struct timeval currenttime;
47 const struct Terrno Terrnos[]= {
50 { "EINPROGRESS", EINPROGRESS },
53 { "EMSGSIZE", EMSGSIZE },
54 { "ENOBUFS", ENOBUFS },
56 { "ENOPROTOOPT", ENOPROTOOPT },
58 { "EWOULDBLOCK", EWOULDBLOCK },
59 { "EHOSTUNREACH", EHOSTUNREACH },
60 { "ECONNRESET", ECONNRESET },
61 { "ECONNREFUSED", ECONNREFUSED },
68 int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
69 Tmust("gettimeofday","tz",!tz);
74 int Hwritev(int fd, const struct iovec *vector, size_t count) {
78 for (i=0; i<count; i++, vector++) {
79 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
81 return Hwrite(fd,vbw.buf,vbw.used);
84 m4_define(`hm_syscall', `
86 void Q$1(hm_args_massage($3,void)) {
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);')
94 m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
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) {
105 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
106 } else if ($'`1 == F_GETFL) {
107 Tvba(" $'`1=F_GETFL");
109 Tmust("$'`1","F_GETFL/F_SETFL",0);
111 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
112 m4_define(`hm_arg_bytes_in', `')
113 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
114 m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);')
118 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
125 m4_define(`hm_specsyscall', `')
127 m4_include(`hsyscalls.i4')
129 void Tvbaddr(const struct sockaddr *addr, int len) {
130 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
132 assert(len==sizeof(struct sockaddr_in));
133 assert(ai->sin_family==AF_INET);
134 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
137 void Tvbbytes(const void *buf, int len) {
141 if (!len) { Tvba("\n ."); return; }
142 for (i=0, bp=buf; i<len; i++, bp++) {
143 if (!(i&31)) Tvba("\n ");
144 else if (!(i&3)) Tvba(" ");
150 void Tvbfdset(int max, const fd_set *fds) {
152 const char *comma= "";
155 for (i=0; i<max; i++) {
156 if (!FD_ISSET(i,fds)) continue;
164 static void Tvbpollevents(int events) {
165 const char *delim= "";
167 events &= (POLLIN|POLLOUT|POLLPRI);
168 if (!events) { Tvba("0"); return; }
169 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
170 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
171 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
174 void Tvbpollfds(const struct pollfd *fds, int nfds) {
175 const char *comma= "";
180 Tvbf("{fd=%d, events=",fds->fd);
181 Tvbpollevents(fds->events);
183 Tvbpollevents(fds->revents);
191 void Tvberrno(int e) {
192 const struct Terrno *te;
194 for (te= Terrnos; te->n && te->v != e; te++);
199 void Tvba(const char *str) {
200 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
203 void Tvbvf(const char *fmt, va_list al) {
205 buf[sizeof(buf)-2]= '\t';
206 vsnprintf(buf,sizeof(buf),fmt,al);
207 assert(buf[sizeof(buf)-2] == '\t');
212 void Tvbf(const char *fmt, ...) {
220 void Tmust(const char *call, const char *arg, int cond) {
222 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
226 void Tfailed(const char *why) {
227 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
232 Tfailed("unable to malloc/realloc");
235 void Toutputerr(void) {
236 Tfailed("write error on test harness output");
240 struct malloced *next, *back;
243 struct { double d; long ul; void *p; void (*fp)(void); } data;
246 static unsigned long malloccount, mallocfailat;
247 static struct { struct malloced *head, *tail; } mallocedlist;
249 #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
251 void *Hmalloc(size_t sz) {
252 struct malloced *newnode;
258 newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem();
260 LIST_LINK_TAIL(mallocedlist,newnode);
262 newnode->count= ++malloccount;
264 mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
266 mallocfailat= strtoul(mfavar,&ep,10);
267 if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
272 assert(newnode->count != mallocfailat);
273 memset(&newnode->data,0xc7,sz);
274 return &newnode->data;
277 void Hfree(void *ptr) {
278 struct malloced *oldnode;
282 oldnode= (void*)((char*)ptr - MALLOCHSZ);
283 LIST_UNLINK(mallocedlist,oldnode);
284 memset(&oldnode->data,0x38,oldnode->sz);
288 void *Hrealloc(void *op, size_t nsz) {
289 struct malloced *oldnode;
293 if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
295 memcpy(np,op, osz>nsz ? nsz : osz);
301 struct malloced *loopnode;
304 adns__vbuf_free(&vb);
305 adns__vbuf_free(&vbw);
306 if (mallocedlist.head) {
307 fprintf(stderr,"adns test harness: memory leaked:");
308 for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
309 fprintf(stderr," %lu(%lu)",loopnode->count,(unsigned long)loopnode->sz);
311 if (ferror(stderr)) exit(-1);