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