chiark / gitweb /
regress: Add Tensuresetup into Hexit; remove it from Q_vb in hfuzzraw
[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
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 const int Tnerrnos= sizeof(Terrnos)/sizeof(Terrnos[0]) - 1;
68
69 static vbuf vbw;
70
71 int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
72   Tensuresetup();
73   Tmust("gettimeofday","tz",!tz);
74   T_gettimeofday_hook();
75   *tv= currenttime;
76   return 0;
77 }
78
79 int Hwritev(int fd, const struct iovec *vector, size_t count) {
80   size_t i;
81   
82   vbw.used= 0;
83   for (i=0; i<count; i++, vector++) {
84     if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
85   }
86   return Hwrite(fd,vbw.buf,vbw.used);
87 }
88
89 m4_define(`hm_syscall', `
90  hm_create_proto_q
91 void Q$1(hm_args_massage($3,void)) {
92
93  vb.used= 0;
94  Tvba("$1");
95  m4_define(`hm_na',`')
96  m4_define(`hm_arg_nullptr',`')
97  m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
98  m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
99  m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
100  m4_define(`hm_arg_timeval_in_rel_null', `
101   if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
102   else Tvba(" $'`1=null");')
103  m4_define(`hm_arg_must', `')
104  m4_define(`hm_arg_socktype', `
105   Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
106  m4_define(`hm_arg_addrfam', `
107   Tvbf($'`1==AF_INET ? " $'`1=AF_INET" :
108           $'`1==AF_INET6 ? " $'`1=AF_INET6" :
109           " $'`1=AF_???");')
110  m4_define(`hm_arg_ign', `')
111  m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
112  m4_define(`hm_arg_fcntl_cmd_arg', `
113   if ($'`1 == F_SETFL) {
114    Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
115   } else if ($'`1 == F_GETFL) {
116    Tvba(" $'`1=F_GETFL");
117   } else {
118    Tmust("$'`1","F_GETFL/F_SETFL",0);
119   }')
120  m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
121  m4_define(`hm_arg_bytes_in', `')
122  m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
123  m4_define(`hm_arg_addr_out', `')
124   $3
125
126  hm_create_nothing
127  m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
128   $3
129
130   Q_vb();
131 }
132 ')
133
134 m4_define(`hm_specsyscall', `')
135
136 m4_include(`hsyscalls.i4')
137
138 hm_stdsyscall_close
139
140 void Tvbaddr(const struct sockaddr *addr, int len) {
141   char buf[ADNS_ADDR2TEXT_BUFLEN];
142   int err, port;
143   int sz= sizeof(buf);
144
145   err= adns_addr2text(addr, 0, buf,&sz, &port);
146   assert(!err);
147
148   Tvbf(strchr(buf, ':') ? "[%s]:%d" : "%s:%d", buf,port);
149 }
150
151 void Tvbbytes(const void *buf, int len) {
152   const byte *bp;
153   int i;
154
155   if (!len) { Tvba("\n     ."); return; }
156   for (i=0, bp=buf; i<len; i++, bp++) {
157     if (!(i&31)) Tvba("\n     ");
158     else if (!(i&3)) Tvba(" ");
159     Tvbf("%02x",*bp);
160   }
161   Tvba(".");
162 }
163
164 void Tvbfdset(int max, const fd_set *fds) {
165   int i;
166   const char *comma= "";
167   
168   if (!fds) {
169     Tvba("null");
170     return;
171   }
172
173   Tvba("[");
174   for (i=0; i<max; i++) {
175     if (!FD_ISSET(i,fds)) continue;
176     Tvba(comma);
177     Tvbf("%d",i);
178     comma= ",";
179   }
180   Tvba("]");
181 }
182
183 static void Tvbpollevents(int events) {
184   const char *delim= "";
185
186   events &= (POLLIN|POLLOUT|POLLPRI);
187   if (!events) { Tvba("0"); return; }
188   if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
189   if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
190   if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
191 }
192
193 void Tvbpollfds(const struct pollfd *fds, int nfds) {
194   const char *comma= "";
195   
196   Tvba("[");
197   while (nfds>0) {
198     Tvba(comma);
199     Tvbf("{fd=%d, events=",fds->fd);
200     Tvbpollevents(fds->events);
201     Tvba(", revents=");
202     Tvbpollevents(fds->revents);
203     Tvba("}");
204     comma= ", ";
205     nfds--; fds++;
206   }
207   Tvba("]");
208 }
209
210 void Tvberrno(int e) {
211   const struct Terrno *te;
212
213   for (te= Terrnos; te->n && te->v != e; te++);
214   assert(te->n);
215   Tvba(te->n);
216 }
217
218 void Tvba(const char *str) {
219   if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
220 }
221
222 void Tvbvf(const char *fmt, va_list al) {
223   char buf[1000];
224   buf[sizeof(buf)-2]= '\t';
225   vsnprintf(buf,sizeof(buf),fmt,al);
226   assert(buf[sizeof(buf)-2] == '\t');
227
228   Tvba(buf);
229 }
230
231 void Tvbf(const char *fmt, ...) {
232   va_list al;
233   va_start(al,fmt);
234   Tvbvf(fmt,al);
235   va_end(al);
236 }
237
238
239 void Tmust(const char *call, const char *arg, int cond) {
240   if (cond) return;
241   fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
242   exit(-1);
243 }
244
245 void Tfailed(const char *why) {
246   fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
247   exit(-1);
248 }
249
250 void Tnomem(void) {
251   Tfailed("unable to malloc/realloc");
252 }
253
254 void Toutputerr(void) {
255   Tfailed("write error on test harness output");
256 }
257
258 void Hexit(int rv) {
259   Tensuresetup();
260   vb.used= 0;
261   Tvbf("exit %d", rv);
262   Q_vb();
263   Texit(0);
264 }
265
266 pid_t Hgetpid(void) {
267   return 2264; /* just some number */
268 }
269
270 void Tcommonshutdown(void) {
271   Tshutdown();
272   adns__vbuf_free(&vb);
273   adns__vbuf_free(&vbw);
274   Tmallocshutdown();
275 }