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