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