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
6 m4_dnl Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
7 m4_dnl Copyright (C) 2014 Mark Wooding
8 m4_dnl Copyright (C) 1999-2000,2003,2006 Tony Finch
9 m4_dnl Copyright (C) 1991 Massachusetts Institute of Technology
10 m4_dnl (See the file INSTALL for full details.)
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 3, or (at your option)
15 m4_dnl any later version.
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.
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.
25 m4_include(hmacros.i4)
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
43 static FILE *Tinputfile, *Treportfile;
46 extern void Tshutdown(void) {
47 adns__vbuf_free(&vb2);
50 static void Tensurereportfile(void) {
54 if (Treportfile) return;
56 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
58 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
61 static void Psyntax(const char *where) {
62 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
66 static void Pcheckinput(void) {
67 if (ferror(Tinputfile)) Tfailed("read test log input file");
68 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
71 void Tensurerecordfile(void) {
75 unsigned long sec, usec;
77 if (Tinputfile) return;
79 fdstr= getenv("ADNS_TEST_IN_FD");
82 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
84 setvbuf(Tinputfile,0,_IONBF,0);
86 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
87 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
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");
96 static void Parg(const char *argname) {
99 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
101 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
103 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
106 static int Pstring_maybe(const char *string) {
110 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
115 static void Pstring(const char *string, const char *emsg) {
116 if (Pstring_maybe(string)) return;
120 static int Perrno(const char *stuff) {
121 const struct Terrno *te;
125 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
126 if (te->n) return te->v;
127 r= strtoul(stuff+2,&ep,10);
128 if (*ep) Psyntax("errno value not recognised, not numeric");
132 static void P_updatetime(void) {
134 unsigned long sec, usec;
136 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
137 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
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;
147 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
150 static void Pfdset(fd_set *set, int max) {
155 Pstring("null","null fdset pointer");
159 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
161 if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
163 r= strtoul(vb2.buf+vb2.used,&ep,10);
164 if (r>=max) Psyntax("fd set member > max");
165 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd 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 ,");
175 static int Ppollfdevents(void) {
178 if (Pstring_maybe("0")) return 0;
181 if (Pstring_maybe("POLLIN")) {
183 if (!Pstring_maybe("|")) return events;
186 if (Pstring_maybe("POLLOUT")) {
188 if (!Pstring_maybe("|")) return events;
191 Pstring("POLLPRI","pollfdevents PRI?");
195 static void Ppollfds(struct pollfd *fds, int nfds) {
198 const char *comma= "";
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");
213 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
217 static void Paddr(struct sockaddr *addr, int *lenr) {
226 if (!q) Psyntax("missing :");
231 if (!q) Psyntax("missing ]");
233 if (*q!=':') Psyntax("expected : after ]");
236 ul= strtoul(q,&ep,10);
237 if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
238 if (ul >= 65536) Psyntax("port too large");
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");
244 assert(*lenr >= a.len);
245 memcpy(addr, &a.addr, a.len);
247 vb2.used= ep - (char*)vb2.buf;
250 static int Pbytes(byte *buf, int maxlen) {
251 static const char hexdigits[]= "0123456789abcdef";
258 c= getc(Tinputfile); Pcheckinput();
259 if (c=='\n' || c==' ' || c=='\t') continue;
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");
266 if (maxlen<=0) Psyntax("buffer overflow in bytes");
271 c= getc(Tinputfile); Pcheckinput();
272 if (c=='\n') return done;
280 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
281 fread(vb2.buf,1,vb.used+2,Tinputfile);
282 if (feof(Tinputfile)) {
283 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
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) {
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);
298 nl= memchr(vb.buf,'\n',vb.used);
299 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
302 m4_define(`hm_syscall', `
304 int H$1(hm_args_massage($3,void)) {
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',`')
311 m4_define(`hm_rv_fcntl',`')
314 hm_create_hqcall_vars
317 hm_create_hqcall_init($1)
320 hm_create_hqcall_args
321 Q$1(hm_args_massage($3));
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();
328 fprintf(Treportfile,"%s",vb2.buf);
329 amtread= strlen(vb2.buf);
330 if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
331 Psyntax("badly formed line");
333 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
335 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
337 e= Perrno(vb2.buf+hm_r_offset);
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;
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;
355 m4_define(`hm_rv_fd',`hm_rv_any')
356 m4_define(`hm_rv_fcntl',`
358 if (cmd == F_GETFL) {
359 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
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;
365 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
367 } else if (cmd == F_SETFL) {
370 Psyntax("fcntl not F_GETFL or F_SETFL");
376 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
377 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
378 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
380 assert(vb2.used <= amtread);
381 if (vb2.used != amtread) Psyntax("junk at end of line");
384 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
392 m4_define(`hm_specsyscall', `')
394 m4_include(`hsyscalls.i4')