chiark / gitweb /
regress/: Use adns_addr2text/text2addr instead of inet_aton/ntoa.
[adns.git] / regress / hcommon.c.m4
1 m4_dnl hcommon.c
2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - routines used for both record and playback
4
5 m4_dnl  This file is part of adns, which is
6 m4_dnl    Copyright (C) 1997-2000,2003,2006  Ian Jackson
7 m4_dnl    Copyright (C) 1999-2000,2003,2006  Tony Finch
8 m4_dnl    Copyright (C) 1991 Massachusetts Institute of Technology
9 m4_dnl  (See the file INSTALL for full details.)
10 m4_dnl  
11 m4_dnl  This program is free software; you can redistribute it and/or modify
12 m4_dnl  it under the terms of the GNU General Public License as published by
13 m4_dnl  the Free Software Foundation; either version 2, or (at your option)
14 m4_dnl  any later version.
15 m4_dnl  
16 m4_dnl  This program is distributed in the hope that it will be useful,
17 m4_dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 m4_dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 m4_dnl  GNU General Public License for more details.
20 m4_dnl  
21 m4_dnl  You should have received a copy of the GNU General Public License
22 m4_dnl  along with this program; if not, write to the Free Software Foundation,
23 m4_dnl  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
24
25 m4_include(hmacros.i4)
26
27 #include <string.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36
37 #include <unistd.h>
38 #include <fcntl.h>
39
40 #include "harness.h"
41 #include "internal.h"
42
43 vbuf vb;
44 FILE *Toutputfile= 0;
45 struct timeval currenttime;
46
47 const struct Terrno Terrnos[]= {
48   { "EBADF",                     EBADF                        },
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                  },
59   { "EHOSTUNREACH",              EHOSTUNREACH                 },
60   { "ECONNRESET",                ECONNRESET                   },
61   { "ECONNREFUSED",              ECONNREFUSED                 },
62   { "EPIPE",                     EPIPE                        },
63   { "ENOTSOCK",                  ENOTSOCK                     },
64   {  0,                          0                            }
65 };
66
67 static vbuf vbw;
68
69 int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
70   Tensurerecordfile();
71   Tmust("gettimeofday","tz",!tz);
72   *tv= currenttime;
73   return 0;
74 }
75
76 int 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 }
85
86 m4_define(`hm_syscall', `
87  hm_create_proto_q
88 void 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);')
96  m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
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) {
107    Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
108   } else if ($'`1 == F_GETFL) {
109    Tvba(" $'`1=F_GETFL");
110   } else {
111    Tmust("$'`1","F_GETFL/F_SETFL",0);
112   }')
113  m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
114  m4_define(`hm_arg_bytes_in', `')
115  m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
116  m4_define(`hm_arg_addr_out', `')
117   $3
118
119  hm_create_nothing
120  m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
121   $3
122
123   Q_vb();
124 }
125 ')
126
127 m4_define(`hm_specsyscall', `')
128
129 m4_include(`hsyscalls.i4')
130
131 void Tvbaddr(const struct sockaddr *addr, int len) {
132   char buf[ADNS_ADDR2TEXT_BUFLEN];
133   int err, port;
134   int sz= sizeof(buf);
135
136   err= adns_addr2text(addr, 0, buf,&sz, &port);
137   assert(!err);
138
139   Tvbf(strchr(buf, ':') ? "[%s]:%d" : "%s:%d", buf,port);
140 }
141
142 void Tvbbytes(const void *buf, int len) {
143   const byte *bp;
144   int i;
145
146   if (!len) { Tvba("\n     ."); return; }
147   for (i=0, bp=buf; i<len; i++, bp++) {
148     if (!(i&31)) Tvba("\n     ");
149     else if (!(i&3)) Tvba(" ");
150     Tvbf("%02x",*bp);
151   }
152   Tvba(".");
153 }
154
155 void Tvbfdset(int max, const fd_set *fds) {
156   int i;
157   const char *comma= "";
158   
159   Tvba("[");
160   for (i=0; i<max; i++) {
161     if (!FD_ISSET(i,fds)) continue;
162     Tvba(comma);
163     Tvbf("%d",i);
164     comma= ",";
165   }
166   Tvba("]");
167 }
168
169 static void Tvbpollevents(int events) {
170   const char *delim= "";
171
172   events &= (POLLIN|POLLOUT|POLLPRI);
173   if (!events) { Tvba("0"); return; }
174   if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
175   if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
176   if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
177 }
178
179 void Tvbpollfds(const struct pollfd *fds, int nfds) {
180   const char *comma= "";
181   
182   Tvba("[");
183   while (nfds>0) {
184     Tvba(comma);
185     Tvbf("{fd=%d, events=",fds->fd);
186     Tvbpollevents(fds->events);
187     Tvba(", revents=");
188     Tvbpollevents(fds->revents);
189     Tvba("}");
190     comma= ", ";
191     nfds--; fds++;
192   }
193   Tvba("]");
194 }
195
196 void Tvberrno(int e) {
197   const struct Terrno *te;
198
199   for (te= Terrnos; te->n && te->v != e; te++);
200   assert(te->n);
201   Tvba(te->n);
202 }
203
204 void Tvba(const char *str) {
205   if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
206 }
207
208 void Tvbvf(const char *fmt, va_list al) {
209   char buf[1000];
210   buf[sizeof(buf)-2]= '\t';
211   vsnprintf(buf,sizeof(buf),fmt,al);
212   assert(buf[sizeof(buf)-2] == '\t');
213
214   Tvba(buf);
215 }
216
217 void Tvbf(const char *fmt, ...) {
218   va_list al;
219   va_start(al,fmt);
220   Tvbvf(fmt,al);
221   va_end(al);
222 }
223
224
225 void Tmust(const char *call, const char *arg, int cond) {
226   if (cond) return;
227   fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
228   exit(-1);
229 }
230
231 void Tfailed(const char *why) {
232   fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
233   exit(-1);
234 }
235
236 void Tnomem(void) {
237   Tfailed("unable to malloc/realloc");
238 }
239
240 void Toutputerr(void) {
241   Tfailed("write error on test harness output");
242 }
243
244 struct malloced {
245   struct malloced *next, *back;
246   size_t sz;
247   unsigned long count;
248   struct { double d; long ul; void *p; void (*fp)(void); } data;
249 };
250
251 static unsigned long malloccount, mallocfailat;
252 static struct { struct malloced *head, *tail; } mallocedlist;
253
254 #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
255
256 void *Hmalloc(size_t sz) {
257   struct malloced *newnode;
258   const char *mfavar;
259   char *ep;
260
261   assert(sz);
262
263   newnode= malloc(MALLOCHSZ + sz);  if (!newnode) Tnomem();
264
265   LIST_LINK_TAIL(mallocedlist,newnode);
266   newnode->sz= sz;
267   newnode->count= ++malloccount;
268   if (!mallocfailat) {
269     mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
270     if (mfavar) {
271       mallocfailat= strtoul(mfavar,&ep,10);
272       if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
273     } else {
274       mallocfailat= ~0UL;
275     }
276   }
277   assert(newnode->count != mallocfailat);
278   memset(&newnode->data,0xc7,sz);
279   return &newnode->data;
280 }
281
282 void Hfree(void *ptr) {
283   struct malloced *oldnode;
284
285   if (!ptr) return;
286
287   oldnode= (void*)((char*)ptr - MALLOCHSZ);
288   LIST_UNLINK(mallocedlist,oldnode);
289   memset(&oldnode->data,0x38,oldnode->sz);
290   free(oldnode);
291 }
292
293 void *Hrealloc(void *op, size_t nsz) {
294   struct malloced *oldnode;
295   void *np;
296   size_t osz;
297
298   if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
299   np= Hmalloc(nsz);
300   memcpy(np,op, osz>nsz ? nsz : osz);
301   Hfree(op);
302   return np;
303 }
304
305 void Hexit(int rv) {
306   struct malloced *loopnode;
307
308   Tshutdown();
309   adns__vbuf_free(&vb);
310   adns__vbuf_free(&vbw);
311   if (mallocedlist.head) {
312     fprintf(stderr,"adns test harness: memory leaked:");
313     for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
314       fprintf(stderr," %lu",loopnode->count);
315     putc('\n',stderr);
316     if (ferror(stderr)) exit(-1);
317   }
318   exit(rv);
319 }
320
321 pid_t Hgetpid(void) {
322   return 2264; /* just some number */
323 }
324