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