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