chiark / gitweb /
7ca28c1952c5d2259cf07d5733bfce2d89285f4d
[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 3, 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_addrfam', `
104   Tvbf($'`1==AF_INET ? " $'`1=AF_INET" :
105           $'`1==AF_INET6 ? " $'`1=AF_INET6" :
106           " $'`1=AF_???");')
107  m4_define(`hm_arg_ign', `')
108  m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
109  m4_define(`hm_arg_fcntl_cmd_arg', `
110   if ($'`1 == F_SETFL) {
111    Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
112   } else if ($'`1 == F_GETFL) {
113    Tvba(" $'`1=F_GETFL");
114   } else {
115    Tmust("$'`1","F_GETFL/F_SETFL",0);
116   }')
117  m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
118  m4_define(`hm_arg_bytes_in', `')
119  m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
120  m4_define(`hm_arg_addr_out', `')
121   $3
122
123  hm_create_nothing
124  m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
125   $3
126
127   Q_vb();
128 }
129 ')
130
131 m4_define(`hm_specsyscall', `')
132
133 m4_include(`hsyscalls.i4')
134
135 void Tvbaddr(const struct sockaddr *addr, int len) {
136   char buf[ADNS_ADDR2TEXT_BUFLEN];
137   int err, port;
138   int sz= sizeof(buf);
139
140   err= adns_addr2text(addr, 0, buf,&sz, &port);
141   assert(!err);
142
143   Tvbf(strchr(buf, ':') ? "[%s]:%d" : "%s:%d", buf,port);
144 }
145
146 void Tvbbytes(const void *buf, int len) {
147   const byte *bp;
148   int i;
149
150   if (!len) { Tvba("\n     ."); return; }
151   for (i=0, bp=buf; i<len; i++, bp++) {
152     if (!(i&31)) Tvba("\n     ");
153     else if (!(i&3)) Tvba(" ");
154     Tvbf("%02x",*bp);
155   }
156   Tvba(".");
157 }
158
159 void Tvbfdset(int max, const fd_set *fds) {
160   int i;
161   const char *comma= "";
162   
163   Tvba("[");
164   for (i=0; i<max; i++) {
165     if (!FD_ISSET(i,fds)) continue;
166     Tvba(comma);
167     Tvbf("%d",i);
168     comma= ",";
169   }
170   Tvba("]");
171 }
172
173 static void Tvbpollevents(int events) {
174   const char *delim= "";
175
176   events &= (POLLIN|POLLOUT|POLLPRI);
177   if (!events) { Tvba("0"); return; }
178   if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
179   if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
180   if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
181 }
182
183 void Tvbpollfds(const struct pollfd *fds, int nfds) {
184   const char *comma= "";
185   
186   Tvba("[");
187   while (nfds>0) {
188     Tvba(comma);
189     Tvbf("{fd=%d, events=",fds->fd);
190     Tvbpollevents(fds->events);
191     Tvba(", revents=");
192     Tvbpollevents(fds->revents);
193     Tvba("}");
194     comma= ", ";
195     nfds--; fds++;
196   }
197   Tvba("]");
198 }
199
200 void Tvberrno(int e) {
201   const struct Terrno *te;
202
203   for (te= Terrnos; te->n && te->v != e; te++);
204   assert(te->n);
205   Tvba(te->n);
206 }
207
208 void Tvba(const char *str) {
209   if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
210 }
211
212 void Tvbvf(const char *fmt, va_list al) {
213   char buf[1000];
214   buf[sizeof(buf)-2]= '\t';
215   vsnprintf(buf,sizeof(buf),fmt,al);
216   assert(buf[sizeof(buf)-2] == '\t');
217
218   Tvba(buf);
219 }
220
221 void Tvbf(const char *fmt, ...) {
222   va_list al;
223   va_start(al,fmt);
224   Tvbvf(fmt,al);
225   va_end(al);
226 }
227
228
229 void Tmust(const char *call, const char *arg, int cond) {
230   if (cond) return;
231   fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
232   exit(-1);
233 }
234
235 void Tfailed(const char *why) {
236   fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
237   exit(-1);
238 }
239
240 void Tnomem(void) {
241   Tfailed("unable to malloc/realloc");
242 }
243
244 void Toutputerr(void) {
245   Tfailed("write error on test harness output");
246 }
247
248 struct malloced {
249   struct malloced *next, *back;
250   size_t sz;
251   unsigned long count;
252   struct { double d; long ul; void *p; void (*fp)(void); } data;
253 };
254
255 static unsigned long malloccount, mallocfailat;
256 static struct { struct malloced *head, *tail; } mallocedlist;
257
258 #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
259
260 void *Hmalloc(size_t sz) {
261   struct malloced *newnode;
262   const char *mfavar;
263   char *ep;
264
265   assert(sz);
266
267   newnode= malloc(MALLOCHSZ + sz);  if (!newnode) Tnomem();
268
269   LIST_LINK_TAIL(mallocedlist,newnode);
270   newnode->sz= sz;
271   newnode->count= ++malloccount;
272   if (!mallocfailat) {
273     mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
274     if (mfavar) {
275       mallocfailat= strtoul(mfavar,&ep,10);
276       if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
277     } else {
278       mallocfailat= ~0UL;
279     }
280   }
281   assert(newnode->count != mallocfailat);
282   memset(&newnode->data,0xc7,sz);
283   return &newnode->data;
284 }
285
286 void Hfree(void *ptr) {
287   struct malloced *oldnode;
288
289   if (!ptr) return;
290
291   oldnode= (void*)((char*)ptr - MALLOCHSZ);
292   LIST_UNLINK(mallocedlist,oldnode);
293   memset(&oldnode->data,0x38,oldnode->sz);
294   free(oldnode);
295 }
296
297 void *Hrealloc(void *op, size_t nsz) {
298   struct malloced *oldnode;
299   void *np;
300   size_t osz;
301
302   if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
303   np= Hmalloc(nsz);
304   memcpy(np,op, osz>nsz ? nsz : osz);
305   Hfree(op);
306   return np;
307 }
308
309 void Hexit(int rv) {
310   struct malloced *loopnode;
311
312   Tshutdown();
313   adns__vbuf_free(&vb);
314   adns__vbuf_free(&vbw);
315   if (mallocedlist.head) {
316     fprintf(stderr,"adns test harness: memory leaked:");
317     for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
318       fprintf(stderr," %lu",loopnode->count);
319     putc('\n',stderr);
320     if (ferror(stderr)) exit(-1);
321   }
322   exit(rv);
323 }
324
325 pid_t Hgetpid(void) {
326   return 2264; /* just some number */
327 }
328