2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - playback routines
5 m4_dnl This file is part of adns, which is Copyright Ian Jackson
6 m4_dnl and contributors (see the file INSTALL for full details).
8 m4_dnl This program is free software; you can redistribute it and/or modify
9 m4_dnl it under the terms of the GNU General Public License as published by
10 m4_dnl the Free Software Foundation; either version 3, or (at your option)
11 m4_dnl any later version.
13 m4_dnl This program is distributed in the hope that it will be useful,
14 m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
15 m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 m4_dnl GNU General Public License for more details.
18 m4_dnl You should have received a copy of the GNU General Public License
19 m4_dnl along with this program; if not, write to the Free Software Foundation.
21 m4_include(hmacros.i4)
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
41 static FILE *Tinputfile, *Tfuzzrawfile, *Treportfile;
44 static void Tensure_reportfile(void) {
48 if (Treportfile) return;
50 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
52 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
55 static void Tensure_fuzzrawfile(void) {
61 const char *fdstr= getenv("ADNS_TEST_FUZZRAW_DUMP_FD");
65 Tfuzzrawfile= fdopen(fd,"ab");
66 if (!Tfuzzrawfile) Tfailed("fdopen ADNS_TEST_FUZZRAW_DUMP_FD");
69 static void FR_write(const void *p, size_t sz) {
70 if (!Tfuzzrawfile) return;
71 ssize_t got = fwrite(p,1,sz,Tfuzzrawfile);
72 if (ferror(Tfuzzrawfile)) Tfailed("write fuzzraw output file");
76 #define FR_WRITE(x) (FR_write(&(x), sizeof((x))))
78 extern void Tshutdown(void) {
79 adns__vbuf_free(&vb2);
81 if (fclose(Tfuzzrawfile)) Tfailed("close fuzzraw output file");
85 static void Psyntax(const char *where) {
86 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
90 static void Pcheckinput(void) {
91 if (ferror(Tinputfile)) Tfailed("read test log input file");
92 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
95 void T_gettimeofday_hook(void) {
96 static struct timeval previously;
98 memset(&delta,0,sizeof(delta));
99 timersub(¤ttime, &previously, &delta);
100 previously = currenttime;
104 void Tensuresetup(void) {
107 unsigned long sec, usec;
109 Tensure_reportfile();
110 Tensure_fuzzrawfile();
112 if (Tinputfile) return;
116 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
118 setvbuf(Tinputfile,0,_IONBF,0);
120 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
121 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
123 sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
124 if (chars==-1) Psyntax("start time invalid");
125 currenttime.tv_sec= sec;
126 currenttime.tv_usec= usec;
127 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
130 static void Parg(const char *argname) {
133 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
135 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
137 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
140 static int Pstring_maybe(const char *string) {
144 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
149 static void Pstring(const char *string, const char *emsg) {
150 if (Pstring_maybe(string)) return;
154 static int Perrno(const char *stuff) {
155 const struct Terrno *te;
159 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
160 if (te->n) return te->v;
161 r= strtoul(stuff+2,&ep,10);
162 if (*ep) Psyntax("errno value not recognised, not numeric");
163 if (r==0 || r>255) Psyntax("numeric errno out of range 1..255");
167 static void P_updatetime(void) {
172 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
173 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
175 sscanf(vb2.buf," +%lu.%ld%n",&sec,&usec,&chars);
176 if (chars==-1) Psyntax("update time invalid");
177 currenttime.tv_sec+= sec;
178 usec = (long)currenttime.tv_usec + usec;
180 currenttime.tv_sec--;
183 while (usec > 1000000) {
184 currenttime.tv_sec++;
187 currenttime.tv_usec = usec;
188 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
191 static void Pfdset(fd_set *set, int max) {
197 Pstring("null","null fdset pointer");
201 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
203 if (vb2.buf[vb2.used] == hm_squote]hm_squote) {
207 ul= strtoul(vb2.buf+vb2.used,&ep,10);
208 if (ul>=max) Psyntax("fd set member > max");
209 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
211 vb2.used= ep - (char*)vb2.buf;
212 c= vb2.buf[vb2.used++];
213 if (c == hm_squote]hm_squote) break;
214 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
221 if (fd>=max || inaccum==16) {
228 accum |= !!FD_ISSET(fd,set);
234 static int Ppollfdevents(void) {
237 if (Pstring_maybe("0")) return 0;
240 if (Pstring_maybe("POLLIN")) {
242 if (!Pstring_maybe("|")) return events;
245 if (Pstring_maybe("POLLOUT")) {
247 if (!Pstring_maybe("|")) return events;
250 Pstring("POLLPRI","pollfdevents PRI?");
254 static void Ppollfds(struct pollfd *fds, int nfds) {
257 const char *comma= "";
259 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
260 for (i=0; i<nfds; i++) {
261 Pstring("{fd=","{fd= in pollfds");
262 int gotfd= strtoul(vb2.buf+vb2.used,&ep,10);
263 if (gotfd != fds->fd) Psyntax("poll fds[].fd changed");
264 vb2.used= ep - (char*)vb2.buf;
265 Pstring(", events=",", events= in pollfds");
266 int gotevents= Ppollfdevents();
267 if (gotevents != fds->events) Psyntax("poll fds[].events changed");
268 Pstring(", revents=",", revents= in pollfds");
269 fds->revents= Ppollfdevents();
270 if (gotevents) FR_WRITE(fds->revents);
271 Pstring("}","} in pollfds");
272 Pstring(comma,"separator in pollfds");
275 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
279 static void Paddr(struct sockaddr *addr, int *lenr) {
288 if (!q) Psyntax("missing :");
293 if (!q) Psyntax("missing ]");
295 if (*q!=':') Psyntax("expected : after ]");
298 ul= strtoul(q,&ep,10);
299 if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
300 if (ul >= 65536) Psyntax("port too large");
306 uint16_t port16 = ul;
310 a.len= sizeof(a.addr);
311 err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
312 if (err) Psyntax("invalid address");
314 assert(*lenr >= a.len);
315 memcpy(addr, &a.addr, a.len);
317 vb2.used= ep - (char*)vb2.buf;
320 static int Pbytes(byte *buf, int maxlen) {
321 static const char hexdigits[]= "0123456789abcdef";
328 c= getc(Tinputfile); Pcheckinput();
329 if (c=='\n' || c==' ' || c=='\t') continue;
331 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
332 v= (pf-hexdigits)<<4;
333 c= getc(Tinputfile); Pcheckinput();
334 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
336 if (maxlen<=0) Psyntax("buffer overflow in bytes");
341 c= getc(Tinputfile); Pcheckinput();
342 if (c=='\n') return done;
350 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
351 fread(vb2.buf,1,vb.used+2,Tinputfile);
352 if (feof(Tinputfile)) {
353 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
358 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
359 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
360 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
362 "adns test harness: program did unexpected:\n %.*s\n"
363 "was expecting:\n %.*s\n",
364 vb.used,vb.buf, vb.used,vb2.buf+1);
367 nl= memchr(vb.buf,'\n',vb.used);
368 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
371 m4_define(`hm_syscall', `
373 int H$1(hm_args_massage($3,void)) {
376 m4_define(`hm_rv_fd',`char *ep;')
377 m4_define(`hm_rv_any',`char *ep;')
380 hm_create_hqcall_vars
383 hm_create_hqcall_init($1)
386 hm_create_hqcall_args
387 Q$1(hm_args_massage($3));
389 m4_define(`hm_r_offset',`m4_len(` $1=')')
390 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
391 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
394 fprintf(Treportfile,"%s",vb2.buf);
395 amtread= strlen(vb2.buf);
396 if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
397 Psyntax("badly formed line");
399 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
402 hm_fr_syscall_ident($1)
403 FR_WRITE(sync_expect);
406 m4_define(`hm_rv_check_errno',`
407 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
409 e= Perrno(vb2.buf+hm_r_offset);
418 m4_define(`hm_rv_check_success',`
419 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
420 vb2.used= hm_r_offset+2;
423 m4_define(`hm_rv_any_nowrite',`
425 unsigned long ul_r= strtoul(vb2.buf+hm_r_offset,&ep,10);
426 if (ul_r < 0 || ul_r > INT_MAX ||
427 (*ep && *ep!=hm_squote hm_squote))
428 Psyntax("return value not E* or positive number");
430 vb2.used= ep - (char*)vb2.buf;
433 m4_define(`hm_rv_succfail',`
437 m4_define(`hm_rv_len',`
441 m4_define(`hm_rv_must',`
444 m4_define(`hm_rv_any',`
448 m4_define(`hm_rv_fd',`hm_rv_any')
449 m4_define(`hm_rv_select',`hm_rv_any_nowrite')
450 m4_define(`hm_rv_poll',`hm_rv_any_nowrite')
451 m4_define(`hm_rv_fcntl',`
453 if (cmd == F_GETFL) {
454 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
456 vb2.used= hm_r_offset+14;
457 } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
458 vb2.used= hm_r_offset+15;
460 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
462 } else if (cmd == F_SETFL) {
465 Psyntax("fcntl not F_GETFL or F_SETFL");
471 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
472 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
473 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
475 assert(vb2.used <= amtread);
476 if (vb2.used != amtread) Psyntax("junk at end of line");
479 m4_define(`hm_arg_bytes_out',`
480 r= Pbytes($'`2,$'`4);
491 m4_define(`hm_specsyscall', `')
493 m4_include(`hsyscalls.i4')