chiark / gitweb /
Licensing: Delete FSF street address
[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  Ian Jackson
7 m4_dnl    Copyright (C) 1999-2000,2003,2006  Tony Finch
8 m4_dnl    Copyright (C) 1991 Massachusetts Institute of Technology
9 m4_dnl  (See the file INSTALL for full details.)
10 m4_dnl  
11 m4_dnl  This program is free software; you can redistribute it and/or modify
12 m4_dnl  it under the terms of the GNU General Public License as published by
13 m4_dnl  the Free Software Foundation; either version 3, or (at your option)
14 m4_dnl  any later version.
15 m4_dnl  
16 m4_dnl  This program is distributed in the hope that it will be useful,
17 m4_dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 m4_dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 m4_dnl  GNU General Public License for more details.
20 m4_dnl  
21 m4_dnl  You should have received a copy of the GNU General Public License
22 m4_dnl  along with this program; if not, write to the Free Software Foundation.
23
24 m4_include(hmacros.i4)
25
26 #include <assert.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdlib.h>
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <sys/time.h>
36
37 #include <unistd.h>
38 #include <fcntl.h>
39
40 #include "harness.h"
41
42 static FILE *Tinputfile, *Treportfile;
43 static vbuf vb2;
44
45 extern void Tshutdown(void) {
46   adns__vbuf_free(&vb2);
47 }
48
49 static void Tensurereportfile(void) {
50   const char *fdstr;
51   int fd;
52
53   if (Treportfile) return;
54   Treportfile= stderr;
55   fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
56   fd= atoi(fdstr);
57   Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
58 }
59
60 static void Psyntax(const char *where) {
61   fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
62   exit(-1);
63 }
64
65 static void Pcheckinput(void) {
66   if (ferror(Tinputfile)) Tfailed("read test log input file");
67   if (feof(Tinputfile)) Psyntax("eof at syscall reply");
68 }
69
70 void Tensurerecordfile(void) {
71   const char *fdstr;
72   int fd;
73   int chars;
74   unsigned long sec, usec;
75
76   if (Tinputfile) return;
77   Tinputfile= stdin;
78   fdstr= getenv("ADNS_TEST_IN_FD");
79   if (fdstr) {
80     fd= atoi(fdstr);
81     Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
82   }
83   setvbuf(Tinputfile,0,_IONBF,0);
84
85   if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
86   fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
87   chars= -1;
88   sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
89   if (chars==-1) Psyntax("start time invalid");
90   currenttime.tv_sec= sec;
91   currenttime.tv_usec= usec;
92   if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
93 }
94
95 static void Parg(const char *argname) {
96   int l;
97
98   if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
99   l= strlen(argname);
100   if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
101   vb2.used+= l;
102   if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
103 }
104
105 static int Pstring_maybe(const char *string) {
106   int l;
107
108   l= strlen(string);
109   if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
110   vb2.used+= l;
111   return 1;
112 }
113
114 static void Pstring(const char *string, const char *emsg) {
115   if (Pstring_maybe(string)) return;
116   Psyntax(emsg);
117 }
118
119 static int Perrno(const char *stuff) {
120   const struct Terrno *te;
121   int r;
122   char *ep;
123
124   for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
125   if (te->n) return te->v;
126   r= strtoul(stuff+2,&ep,10);
127   if (*ep) Psyntax("errno value not recognised, not numeric");
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 (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
154   FD_ZERO(set);
155   if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
156   for (;;) {
157     r= strtoul(vb2.buf+vb2.used,&ep,10);
158     if (r>=max) Psyntax("fd set member > max");
159     if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
160     FD_SET(r,set);
161     vb2.used= ep - (char*)vb2.buf;
162     c= vb2.buf[vb2.used++];
163     if (c == hm_squote]hm_squote) break;
164     if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
165   }
166 }
167
168 #ifdef HAVE_POLL
169 static int Ppollfdevents(void) {
170   int events;
171
172   if (Pstring_maybe("0")) return 0;
173   events= 0;
174
175   if (Pstring_maybe("POLLIN")) {
176     events |= POLLIN;
177     if (!Pstring_maybe("|")) return events;
178   }
179
180   if (Pstring_maybe("POLLOUT")) {
181     events |= POLLOUT;
182     if (!Pstring_maybe("|")) return events;
183   }
184
185   Pstring("POLLPRI","pollfdevents PRI?");
186   return events;
187 }
188
189 static void Ppollfds(struct pollfd *fds, int nfds) {
190   int i;
191   char *ep;
192   const char *comma= "";
193   
194   if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
195   for (i=0; i<nfds; i++) {
196     Pstring("{fd=","{fd= in pollfds");
197     fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
198     vb2.used= ep - (char*)vb2.buf;    
199     Pstring(", events=",", events= in pollfds");
200     fds->events= Ppollfdevents();
201     Pstring(", revents=",", revents= in pollfds");
202     fds->revents= Ppollfdevents();
203     Pstring("}","} in pollfds");
204     Pstring(comma,"separator in pollfds");
205     comma= ", ";
206   }
207   if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
208 }
209 #endif
210
211 static void Paddr(struct sockaddr *addr, int *lenr) {
212   adns_rr_addr a;
213   char *p, *q, *ep;
214   int err;
215   unsigned long ul;
216
217   p= vb2.buf+vb2.used;
218   if (*p!='[') {
219     q= strchr(p,':');
220     if (!q) Psyntax("missing :");
221     *q++= 0;
222   } else {
223     p++;
224     q= strchr(p,']');
225     if (!q) Psyntax("missing ]");
226     *q++= 0;
227     if (*q!=':') Psyntax("expected : after ]");
228     q++;
229   }
230   ul= strtoul(q,&ep,10);
231   if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
232   if (ul >= 65536) Psyntax("port too large");
233
234   a.len= sizeof(a.addr);
235   err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
236   if (err) Psyntax("invalid address");
237
238   assert(*lenr >= a.len);
239   memcpy(addr, &a.addr, a.len);
240   *lenr= a.len;
241   vb2.used= ep - (char*)vb2.buf;
242 }
243
244 static int Pbytes(byte *buf, int maxlen) {
245   static const char hexdigits[]= "0123456789abcdef";
246
247   int c, v, done;
248   const char *pf;
249
250   done= 0;
251   for (;;) {
252     c= getc(Tinputfile); Pcheckinput();
253     if (c=='\n' || c==' ' || c=='\t') continue;
254     if (c=='.') break;
255     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
256     v= (pf-hexdigits)<<4;
257     c= getc(Tinputfile); Pcheckinput();
258     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
259     v |= (pf-hexdigits);
260     if (maxlen<=0) Psyntax("buffer overflow in bytes");
261     *buf++= v;
262     maxlen--; done++;
263   }
264   for (;;) {
265     c= getc(Tinputfile); Pcheckinput();
266     if (c=='\n') return done;
267   }
268 }
269   
270 void Q_vb(void) {
271   const char *nl;
272
273   Tensurerecordfile();
274   if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
275   fread(vb2.buf,1,vb.used+2,Tinputfile);
276   if (feof(Tinputfile)) {
277     fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
278            vb.used,vb.buf);
279     exit(-1);
280   }
281   Pcheckinput();
282   if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
283   if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
284       vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
285     fprintf(stderr,
286             "adns test harness: program did unexpected:\n %.*s\n"
287             "was expecting:\n %.*s\n",
288             vb.used,vb.buf, vb.used,vb2.buf+1);
289     exit(1);
290   }
291   Tensurereportfile();
292   nl= memchr(vb.buf,'\n',vb.used);
293   fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
294 }
295
296 m4_define(`hm_syscall', `
297  hm_create_proto_h
298 int H$1(hm_args_massage($3,void)) {
299  int r, amtread;
300  m4_define(`hm_rv_fd',`char *ep;')
301  m4_define(`hm_rv_any',`char *ep;')
302  m4_define(`hm_rv_len',`')
303  m4_define(`hm_rv_must',`')
304  m4_define(`hm_rv_succfail',`')
305  m4_define(`hm_rv_fcntl',`')
306  $2
307
308  hm_create_hqcall_vars
309  $3
310
311  hm_create_hqcall_init($1)
312  $3
313
314  hm_create_hqcall_args
315  Q$1(hm_args_massage($3));
316
317  m4_define(`hm_r_offset',`m4_len(` $1=')')
318  if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
319  fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
320
321  Tensurereportfile();
322  fprintf(Treportfile,"%s",vb2.buf);
323  amtread= strlen(vb2.buf);
324  if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
325   Psyntax("badly formed line");
326  vb2.buf[amtread]= 0;
327  if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
328
329  if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
330   int e;
331   e= Perrno(vb2.buf+hm_r_offset);
332   P_updatetime();
333   errno= e;
334   return -1;
335  }
336
337  m4_define(`hm_rv_succfail',`
338   if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
339   vb2.used= hm_r_offset+2;
340   r= 0;
341  ')
342  m4_define(`hm_rv_len',`hm_rv_succfail')
343  m4_define(`hm_rv_must',`hm_rv_succfail')
344  m4_define(`hm_rv_any',`
345   r= strtoul(vb2.buf+hm_r_offset,&ep,10);
346   if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
347   vb2.used= ep - (char*)vb2.buf;
348  ')
349  m4_define(`hm_rv_fd',`hm_rv_any')
350  m4_define(`hm_rv_fcntl',`
351   r= 0;
352   if (cmd == F_GETFL) {
353     if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
354       r= O_NONBLOCK;
355       vb2.used= hm_r_offset+14;
356     } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
357       vb2.used= hm_r_offset+15;
358     } else {
359       Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
360     }
361   } else if (cmd == F_SETFL) {
362     hm_rv_succfail
363   } else {
364     Psyntax("fcntl not F_GETFL or F_SETFL");
365   }
366  ')
367  $2
368
369  hm_create_nothing
370  m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
371  m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
372  m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
373  $3
374  assert(vb2.used <= amtread);
375  if (vb2.used != amtread) Psyntax("junk at end of line");
376
377  hm_create_nothing
378  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
379  $3
380
381  P_updatetime();
382  return r;
383 }
384 ')
385
386 m4_define(`hm_specsyscall', `')
387
388 m4_include(`hsyscalls.i4')