chiark / gitweb /
regress: Make arrangements for fuzzraw of gettimeofday
[adns.git] / regress / hplayback.c.m4
1 m4_dnl hplayback.c.m4
2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - playback routines
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 <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdlib.h>
31
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/time.h>
37
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <limits.h>
41
42
43 #include "harness.h"
44
45 static FILE *Tinputfile, *Treportfile;
46 static vbuf vb2;
47
48 extern void Tshutdown(void) {
49   adns__vbuf_free(&vb2);
50 }
51
52 static void Tensurereportfile(void) {
53   const char *fdstr;
54   int fd;
55
56   if (Treportfile) return;
57   Treportfile= stderr;
58   fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
59   fd= atoi(fdstr);
60   Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
61 }
62
63 static void Psyntax(const char *where) {
64   fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
65   exit(-1);
66 }
67
68 static void Pcheckinput(void) {
69   if (ferror(Tinputfile)) Tfailed("read test log input file");
70   if (feof(Tinputfile)) Psyntax("eof at syscall reply");
71 }
72
73 void T_gettimeofday_hook(void) { }
74
75 void Tensurerecordfile(void) {
76   int fd;
77   int chars;
78   unsigned long sec, usec;
79
80   if (Tinputfile) return;
81   Tinputfile= stdin;
82   fd = Ttestinputfd();
83   if (fd >= 0) {
84     Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
85   }
86   setvbuf(Tinputfile,0,_IONBF,0);
87
88   if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
89   fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
90   chars= -1;
91   sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
92   if (chars==-1) Psyntax("start time invalid");
93   currenttime.tv_sec= sec;
94   currenttime.tv_usec= usec;
95   if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
96 }
97
98 static void Parg(const char *argname) {
99   int l;
100
101   if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
102   l= strlen(argname);
103   if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
104   vb2.used+= l;
105   if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
106 }
107
108 static int Pstring_maybe(const char *string) {
109   int l;
110
111   l= strlen(string);
112   if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
113   vb2.used+= l;
114   return 1;
115 }
116
117 static void Pstring(const char *string, const char *emsg) {
118   if (Pstring_maybe(string)) return;
119   Psyntax(emsg);
120 }
121
122 static int Perrno(const char *stuff) {
123   const struct Terrno *te;
124   int r;
125   char *ep;
126
127   for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
128   if (te->n) return te->v;
129   r= strtoul(stuff+2,&ep,10);
130   if (*ep) Psyntax("errno value not recognised, not numeric");
131   if (r==0 || r>255) Psyntax("numeric errno out of range 1..255");
132   return r;
133 }
134
135 static void P_updatetime(void) {
136   int chars;
137   unsigned long sec, usec;
138
139   if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
140   fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
141   chars= -1;
142   sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
143   if (chars==-1) Psyntax("update time invalid");
144   currenttime.tv_sec+= sec;
145   currenttime.tv_usec+= usec;
146   if (currenttime.tv_usec > 1000000) {
147     currenttime.tv_sec++;
148     currenttime.tv_usec -= 1000000;
149   }
150   if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
151 }
152
153 static void Pfdset(fd_set *set, int max) {
154   int c;
155   unsigned long ul;
156   char *ep;
157
158   if (!set) {
159     Pstring("null","null fdset pointer");
160     return;
161   }
162   
163   if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
164   FD_ZERO(set);
165   if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
166   for (;;) {
167     ul= strtoul(vb2.buf+vb2.used,&ep,10);
168     if (ul>=max) Psyntax("fd set member > max");
169     if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
170     FD_SET(ul,set);
171     vb2.used= ep - (char*)vb2.buf;
172     c= vb2.buf[vb2.used++];
173     if (c == hm_squote]hm_squote) break;
174     if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
175   }
176 }
177
178 #ifdef HAVE_POLL
179 static int Ppollfdevents(void) {
180   int events;
181
182   if (Pstring_maybe("0")) return 0;
183   events= 0;
184
185   if (Pstring_maybe("POLLIN")) {
186     events |= POLLIN;
187     if (!Pstring_maybe("|")) return events;
188   }
189
190   if (Pstring_maybe("POLLOUT")) {
191     events |= POLLOUT;
192     if (!Pstring_maybe("|")) return events;
193   }
194
195   Pstring("POLLPRI","pollfdevents PRI?");
196   return events;
197 }
198
199 static void Ppollfds(struct pollfd *fds, int nfds) {
200   int i;
201   char *ep;
202   const char *comma= "";
203   
204   if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
205   for (i=0; i<nfds; i++) {
206     Pstring("{fd=","{fd= in pollfds");
207     fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
208     vb2.used= ep - (char*)vb2.buf;    
209     Pstring(", events=",", events= in pollfds");
210     fds->events= Ppollfdevents();
211     Pstring(", revents=",", revents= in pollfds");
212     fds->revents= Ppollfdevents();
213     Pstring("}","} in pollfds");
214     Pstring(comma,"separator in pollfds");
215     comma= ", ";
216   }
217   if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
218 }
219 #endif
220
221 static void Paddr(struct sockaddr *addr, int *lenr) {
222   adns_rr_addr a;
223   char *p, *q, *ep;
224   int err;
225   unsigned long ul;
226
227   p= vb2.buf+vb2.used;
228   if (*p!='[') {
229     q= strchr(p,':');
230     if (!q) Psyntax("missing :");
231     *q++= 0;
232   } else {
233     p++;
234     q= strchr(p,']');
235     if (!q) Psyntax("missing ]");
236     *q++= 0;
237     if (*q!=':') Psyntax("expected : after ]");
238     q++;
239   }
240   ul= strtoul(q,&ep,10);
241   if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
242   if (ul >= 65536) Psyntax("port too large");
243
244   a.len= sizeof(a.addr);
245   err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
246   if (err) Psyntax("invalid address");
247
248   assert(*lenr >= a.len);
249   memcpy(addr, &a.addr, a.len);
250   *lenr= a.len;
251   vb2.used= ep - (char*)vb2.buf;
252 }
253
254 static int Pbytes(byte *buf, int maxlen) {
255   static const char hexdigits[]= "0123456789abcdef";
256
257   int c, v, done;
258   const char *pf;
259
260   done= 0;
261   for (;;) {
262     c= getc(Tinputfile); Pcheckinput();
263     if (c=='\n' || c==' ' || c=='\t') continue;
264     if (c=='.') break;
265     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
266     v= (pf-hexdigits)<<4;
267     c= getc(Tinputfile); Pcheckinput();
268     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
269     v |= (pf-hexdigits);
270     if (maxlen<=0) Psyntax("buffer overflow in bytes");
271     *buf++= v;
272     maxlen--; done++;
273   }
274   for (;;) {
275     c= getc(Tinputfile); Pcheckinput();
276     if (c=='\n') return done;
277   }
278 }
279   
280 void Q_vb(void) {
281   const char *nl;
282
283   Tensurerecordfile();
284   if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
285   fread(vb2.buf,1,vb.used+2,Tinputfile);
286   if (feof(Tinputfile)) {
287     fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
288            vb.used,vb.buf);
289     exit(-1);
290   }
291   Pcheckinput();
292   if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
293   if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
294       vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
295     fprintf(stderr,
296             "adns test harness: program did unexpected:\n %.*s\n"
297             "was expecting:\n %.*s\n",
298             vb.used,vb.buf, vb.used,vb2.buf+1);
299     exit(1);
300   }
301   Tensurereportfile();
302   nl= memchr(vb.buf,'\n',vb.used);
303   fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
304 }
305
306 m4_define(`hm_syscall', `
307  hm_create_proto_h
308 int H$1(hm_args_massage($3,void)) {
309  int r, amtread;
310  m4_define(`hm_rv_fd',`char *ep;')
311  m4_define(`hm_rv_any',`char *ep;')
312  m4_define(`hm_rv_len',`')
313  m4_define(`hm_rv_must',`')
314  m4_define(`hm_rv_succfail',`')
315  m4_define(`hm_rv_fcntl',`')
316  $2
317
318  hm_create_hqcall_vars
319  $3
320
321  hm_create_hqcall_init($1)
322  $3
323
324  hm_create_hqcall_args
325  Q$1(hm_args_massage($3));
326
327  m4_define(`hm_r_offset',`m4_len(` $1=')')
328  if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
329  fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
330
331  Tensurereportfile();
332  fprintf(Treportfile,"%s",vb2.buf);
333  amtread= strlen(vb2.buf);
334  if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
335   Psyntax("badly formed line");
336  vb2.buf[amtread]= 0;
337  if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
338
339  if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
340   int e;
341   e= Perrno(vb2.buf+hm_r_offset);
342   P_updatetime();
343   errno= e;
344   return -1;
345  }
346
347  m4_define(`hm_rv_succfail',`
348   if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
349   vb2.used= hm_r_offset+2;
350   r= 0;
351  ')
352  m4_define(`hm_rv_len',`hm_rv_succfail')
353  m4_define(`hm_rv_must',`hm_rv_succfail')
354  m4_define(`hm_rv_any',`
355   unsigned long ul_r= strtoul(vb2.buf+hm_r_offset,&ep,10);
356   if (ul_r < 0 || ul_r > INT_MAX ||
357       (*ep && *ep!=hm_squote hm_squote))
358     Psyntax("return value not E* or positive number");
359   r= ul_r;
360   vb2.used= ep - (char*)vb2.buf;
361  ')
362  m4_define(`hm_rv_fd',`hm_rv_any')
363  m4_define(`hm_rv_fcntl',`
364   r= 0;
365   if (cmd == F_GETFL) {
366     if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
367       r= O_NONBLOCK;
368       vb2.used= hm_r_offset+14;
369     } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
370       vb2.used= hm_r_offset+15;
371     } else {
372       Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
373     }
374   } else if (cmd == F_SETFL) {
375     hm_rv_succfail
376   } else {
377     Psyntax("fcntl not F_GETFL or F_SETFL");
378   }
379  ')
380  $2
381
382  hm_create_nothing
383  m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
384  m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
385  m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
386  $3
387  assert(vb2.used <= amtread);
388  if (vb2.used != amtread) Psyntax("junk at end of line");
389
390  hm_create_nothing
391  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
392  $3
393
394  P_updatetime();
395  return r;
396 }
397 ')
398
399 m4_define(`hm_specsyscall', `')
400
401 m4_include(`hsyscalls.i4')
402
403 hm_stdsyscall_close