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