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