chiark / gitweb /
312a04a159b064b2fccf06265ef60ace94126639
[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   if (r==0 || r>255) Psyntax("numeric errno out of range 1..255");
128   return r;
129 }
130
131 static void P_updatetime(void) {
132   int chars;
133   unsigned long sec, usec;
134
135   if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
136   fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
137   chars= -1;
138   sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
139   if (chars==-1) Psyntax("update time invalid");
140   currenttime.tv_sec+= sec;
141   currenttime.tv_usec+= usec;
142   if (currenttime.tv_usec > 1000000) {
143     currenttime.tv_sec++;
144     currenttime.tv_usec -= 1000000;
145   }
146   if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
147 }
148
149 static void Pfdset(fd_set *set, int max) {
150   int r, c;
151   char *ep;
152
153   if (!set) {
154     Pstring("null","null fdset pointer");
155     return;
156   }
157   
158   if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
159   FD_ZERO(set);
160   if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
161   for (;;) {
162     r= strtoul(vb2.buf+vb2.used,&ep,10);
163     if (r>=max) Psyntax("fd set member > max");
164     if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
165     FD_SET(r,set);
166     vb2.used= ep - (char*)vb2.buf;
167     c= vb2.buf[vb2.used++];
168     if (c == hm_squote]hm_squote) break;
169     if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
170   }
171 }
172
173 #ifdef HAVE_POLL
174 static int Ppollfdevents(void) {
175   int events;
176
177   if (Pstring_maybe("0")) return 0;
178   events= 0;
179
180   if (Pstring_maybe("POLLIN")) {
181     events |= POLLIN;
182     if (!Pstring_maybe("|")) return events;
183   }
184
185   if (Pstring_maybe("POLLOUT")) {
186     events |= POLLOUT;
187     if (!Pstring_maybe("|")) return events;
188   }
189
190   Pstring("POLLPRI","pollfdevents PRI?");
191   return events;
192 }
193
194 static void Ppollfds(struct pollfd *fds, int nfds) {
195   int i;
196   char *ep;
197   const char *comma= "";
198   
199   if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
200   for (i=0; i<nfds; i++) {
201     Pstring("{fd=","{fd= in pollfds");
202     fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
203     vb2.used= ep - (char*)vb2.buf;    
204     Pstring(", events=",", events= in pollfds");
205     fds->events= Ppollfdevents();
206     Pstring(", revents=",", revents= in pollfds");
207     fds->revents= Ppollfdevents();
208     Pstring("}","} in pollfds");
209     Pstring(comma,"separator in pollfds");
210     comma= ", ";
211   }
212   if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
213 }
214 #endif
215
216 static void Paddr(struct sockaddr *addr, int *lenr) {
217   adns_rr_addr a;
218   char *p, *q, *ep;
219   int err;
220   unsigned long ul;
221
222   p= vb2.buf+vb2.used;
223   if (*p!='[') {
224     q= strchr(p,':');
225     if (!q) Psyntax("missing :");
226     *q++= 0;
227   } else {
228     p++;
229     q= strchr(p,']');
230     if (!q) Psyntax("missing ]");
231     *q++= 0;
232     if (*q!=':') Psyntax("expected : after ]");
233     q++;
234   }
235   ul= strtoul(q,&ep,10);
236   if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
237   if (ul >= 65536) Psyntax("port too large");
238
239   a.len= sizeof(a.addr);
240   err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
241   if (err) Psyntax("invalid address");
242
243   assert(*lenr >= a.len);
244   memcpy(addr, &a.addr, a.len);
245   *lenr= a.len;
246   vb2.used= ep - (char*)vb2.buf;
247 }
248
249 static int Pbytes(byte *buf, int maxlen) {
250   static const char hexdigits[]= "0123456789abcdef";
251
252   int c, v, done;
253   const char *pf;
254
255   done= 0;
256   for (;;) {
257     c= getc(Tinputfile); Pcheckinput();
258     if (c=='\n' || c==' ' || c=='\t') continue;
259     if (c=='.') break;
260     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
261     v= (pf-hexdigits)<<4;
262     c= getc(Tinputfile); Pcheckinput();
263     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
264     v |= (pf-hexdigits);
265     if (maxlen<=0) Psyntax("buffer overflow in bytes");
266     *buf++= v;
267     maxlen--; done++;
268   }
269   for (;;) {
270     c= getc(Tinputfile); Pcheckinput();
271     if (c=='\n') return done;
272   }
273 }
274   
275 void Q_vb(void) {
276   const char *nl;
277
278   Tensurerecordfile();
279   if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
280   fread(vb2.buf,1,vb.used+2,Tinputfile);
281   if (feof(Tinputfile)) {
282     fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
283            vb.used,vb.buf);
284     exit(-1);
285   }
286   Pcheckinput();
287   if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
288   if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
289       vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
290     fprintf(stderr,
291             "adns test harness: program did unexpected:\n %.*s\n"
292             "was expecting:\n %.*s\n",
293             vb.used,vb.buf, vb.used,vb2.buf+1);
294     exit(1);
295   }
296   Tensurereportfile();
297   nl= memchr(vb.buf,'\n',vb.used);
298   fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
299 }
300
301 m4_define(`hm_syscall', `
302  hm_create_proto_h
303 int H$1(hm_args_massage($3,void)) {
304  int r, amtread;
305  m4_define(`hm_rv_fd',`char *ep;')
306  m4_define(`hm_rv_any',`char *ep;')
307  m4_define(`hm_rv_len',`')
308  m4_define(`hm_rv_must',`')
309  m4_define(`hm_rv_succfail',`')
310  m4_define(`hm_rv_fcntl',`')
311  $2
312
313  hm_create_hqcall_vars
314  $3
315
316  hm_create_hqcall_init($1)
317  $3
318
319  hm_create_hqcall_args
320  Q$1(hm_args_massage($3));
321
322  m4_define(`hm_r_offset',`m4_len(` $1=')')
323  if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
324  fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
325
326  Tensurereportfile();
327  fprintf(Treportfile,"%s",vb2.buf);
328  amtread= strlen(vb2.buf);
329  if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
330   Psyntax("badly formed line");
331  vb2.buf[amtread]= 0;
332  if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
333
334  if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
335   int e;
336   e= Perrno(vb2.buf+hm_r_offset);
337   P_updatetime();
338   errno= e;
339   return -1;
340  }
341
342  m4_define(`hm_rv_succfail',`
343   if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
344   vb2.used= hm_r_offset+2;
345   r= 0;
346  ')
347  m4_define(`hm_rv_len',`hm_rv_succfail')
348  m4_define(`hm_rv_must',`hm_rv_succfail')
349  m4_define(`hm_rv_any',`
350   r= strtoul(vb2.buf+hm_r_offset,&ep,10);
351   if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
352   vb2.used= ep - (char*)vb2.buf;
353  ')
354  m4_define(`hm_rv_fd',`hm_rv_any')
355  m4_define(`hm_rv_fcntl',`
356   r= 0;
357   if (cmd == F_GETFL) {
358     if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
359       r= O_NONBLOCK;
360       vb2.used= hm_r_offset+14;
361     } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
362       vb2.used= hm_r_offset+15;
363     } else {
364       Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
365     }
366   } else if (cmd == F_SETFL) {
367     hm_rv_succfail
368   } else {
369     Psyntax("fcntl not F_GETFL or F_SETFL");
370   }
371  ')
372  $2
373
374  hm_create_nothing
375  m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
376  m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
377  m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
378  $3
379  assert(vb2.used <= amtread);
380  if (vb2.used != amtread) Psyntax("junk at end of line");
381
382  hm_create_nothing
383  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
384  $3
385
386  P_updatetime();
387  return r;
388 }
389 ')
390
391 m4_define(`hm_specsyscall', `')
392
393 m4_include(`hsyscalls.i4')