chiark / gitweb /
74811a73e32807f1284aaeb57857ee7b637ae0c5
[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,2014-2016,2020  Ian Jackson
7 m4_dnl    Copyright (C) 2014  Mark Wooding
8 m4_dnl    Copyright (C) 1999-2000,2003,2006  Tony Finch
9 m4_dnl    Copyright (C) 1991 Massachusetts Institute of Technology
10 m4_dnl  (See the file INSTALL for full details.)
11 m4_dnl  
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 3, or (at your option)
15 m4_dnl  any later version.
16 m4_dnl  
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.
21 m4_dnl  
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
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 #include <time.h>
40
41 #include "harness.h"
42 #include "internal.h"
43
44 vbuf vb;
45 FILE *Toutputfile= 0;
46 struct timeval currenttime;
47
48 const struct Terrno Terrnos[]= {
49   { "EBADF",                     EBADF                        },
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                  },
60   { "EHOSTUNREACH",              EHOSTUNREACH                 },
61   { "ECONNRESET",                ECONNRESET                   },
62   { "ECONNREFUSED",              ECONNREFUSED                 },
63   { "EPIPE",                     EPIPE                        },
64   { "ENOTSOCK",                  ENOTSOCK                     },
65   {  0,                          0                            }
66 };
67
68 const int Tnerrnos= sizeof(Terrnos)/sizeof(Terrnos[0]) - 1;
69
70 static vbuf vbw;
71
72 int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
73   Tensuresetup();
74   Tmust("gettimeofday","tz",!tz);
75   T_gettimeofday_hook();
76   *tv= currenttime;
77   return 0;
78 }
79 int Hclock_gettime(clockid_t clk, struct timespec *ts) {
80   Tensuresetup();
81   ts->tv_sec =  currenttime.tv_sec;
82   ts->tv_nsec = currenttime.tv_usec * 1000 + 666;
83   switch (clk) {
84   case CLOCK_MONOTONIC: ts->tv_sec -= 1500000000; break;
85   case CLOCK_REALTIME:                            break;
86   default: Tmust("clock_gettime","clk",0);
87   }
88   return 0;
89 }
90
91 int Hwritev(int fd, const struct iovec *vector, size_t count) {
92   size_t i;
93   
94   vbw.used= 0;
95   for (i=0; i<count; i++, vector++) {
96     if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
97   }
98   return Hwrite(fd,vbw.buf,vbw.used);
99 }
100
101 m4_define(`hm_syscall', `
102  hm_create_proto_q
103 void Q$1(hm_args_massage($3,void)) {
104
105  vb.used= 0;
106  Tvba("$1");
107  m4_define(`hm_na',`')
108  m4_define(`hm_arg_nullptr',`')
109  m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
110  m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
111  m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
112  m4_define(`hm_arg_timeval_in_rel_null', `
113   if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
114   else Tvba(" $'`1=null");')
115  m4_define(`hm_arg_must', `')
116  m4_define(`hm_arg_socktype', `
117   Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
118  m4_define(`hm_arg_addrfam', `
119   Tvbf($'`1==AF_INET ? " $'`1=AF_INET" :
120           $'`1==AF_INET6 ? " $'`1=AF_INET6" :
121           " $'`1=AF_???");')
122  m4_define(`hm_arg_ign', `')
123  m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
124  m4_define(`hm_arg_fcntl_cmd_arg', `
125   if ($'`1 == F_SETFL) {
126    Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
127   } else if ($'`1 == F_GETFL) {
128    Tvba(" $'`1=F_GETFL");
129   } else {
130    Tmust("$'`1","F_GETFL/F_SETFL",0);
131   }')
132  m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
133  m4_define(`hm_arg_bytes_in', `')
134  m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
135  m4_define(`hm_arg_addr_out', `')
136   $3
137
138  hm_create_nothing
139  m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
140   $3
141
142   Q_vb();
143 }
144 ')
145
146 m4_define(`hm_specsyscall', `')
147
148 m4_include(`hsyscalls.i4')
149
150 hm_stdsyscall_close
151
152 void Tvbaddr(const struct sockaddr *addr, int len) {
153   char buf[ADNS_ADDR2TEXT_BUFLEN];
154   int err, port;
155   int sz= sizeof(buf);
156
157   err= adns_addr2text(addr, 0, buf,&sz, &port);
158   assert(!err);
159
160   Tvbf(strchr(buf, ':') ? "[%s]:%d" : "%s:%d", buf,port);
161 }
162
163 void Tvbbytes(const void *buf, int len) {
164   const byte *bp;
165   int i;
166
167   if (!len) { Tvba("\n     ."); return; }
168   for (i=0, bp=buf; i<len; i++, bp++) {
169     if (!(i&31)) Tvba("\n     ");
170     else if (!(i&3)) Tvba(" ");
171     Tvbf("%02x",*bp);
172   }
173   Tvba(".");
174 }
175
176 void Tvbfdset(int max, const fd_set *fds) {
177   int i;
178   const char *comma= "";
179   
180   if (!fds) {
181     Tvba("null");
182     return;
183   }
184
185   Tvba("[");
186   for (i=0; i<max; i++) {
187     if (!FD_ISSET(i,fds)) continue;
188     Tvba(comma);
189     Tvbf("%d",i);
190     comma= ",";
191   }
192   Tvba("]");
193 }
194
195 static void Tvbpollevents(int events) {
196   const char *delim= "";
197
198   events &= (POLLIN|POLLOUT|POLLPRI);
199   if (!events) { Tvba("0"); return; }
200   if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
201   if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
202   if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
203 }
204
205 void Tvbpollfds(const struct pollfd *fds, int nfds) {
206   const char *comma= "";
207   
208   Tvba("[");
209   while (nfds>0) {
210     Tvba(comma);
211     Tvbf("{fd=%d, events=",fds->fd);
212     Tvbpollevents(fds->events);
213     Tvba(", revents=");
214     Tvbpollevents(fds->revents);
215     Tvba("}");
216     comma= ", ";
217     nfds--; fds++;
218   }
219   Tvba("]");
220 }
221
222 void Tvberrno(int e) {
223   const struct Terrno *te;
224
225   for (te= Terrnos; te->n && te->v != e; te++);
226   assert(te->n);
227   Tvba(te->n);
228 }
229
230 void Tvba(const char *str) {
231   if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
232 }
233
234 void Tvbvf(const char *fmt, va_list al) {
235   char buf[1000];
236   buf[sizeof(buf)-2]= '\t';
237   vsnprintf(buf,sizeof(buf),fmt,al);
238   assert(buf[sizeof(buf)-2] == '\t');
239
240   Tvba(buf);
241 }
242
243 void Tvbf(const char *fmt, ...) {
244   va_list al;
245   va_start(al,fmt);
246   Tvbvf(fmt,al);
247   va_end(al);
248 }
249
250
251 void Tmust(const char *call, const char *arg, int cond) {
252   if (cond) return;
253   fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
254   exit(-1);
255 }
256
257 void Tfailed(const char *why) {
258   fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
259   exit(-1);
260 }
261
262 void Tnomem(void) {
263   Tfailed("unable to malloc/realloc");
264 }
265
266 void Toutputerr(void) {
267   Tfailed("write error on test harness output");
268 }
269
270 void Hexit(int rv) {
271   Tensuresetup();
272   vb.used= 0;
273   Tvbf("exit %d", rv);
274   Q_vb();
275   Texit(0);
276 }
277
278 pid_t Hgetpid(void) {
279   return 2264; /* just some number */
280 }
281
282 void Tcommonshutdown(void) {
283   Tshutdown();
284   adns__vbuf_free(&vb);
285   adns__vbuf_free(&vbw);
286   Tmallocshutdown();
287 }