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