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 Ian Jackson
7 m4_dnl Copyright (C) 1999-2000,2003,2006 Tony Finch
8 m4_dnl Copyright (C) 1991 Massachusetts Institute of Technology
9 m4_dnl (See the file INSTALL for full details.)
11 m4_dnl This program is free software; you can redistribute it and/or modify
12 m4_dnl it under the terms of the GNU General Public License as published by
13 m4_dnl the Free Software Foundation; either version 3, or (at your option)
14 m4_dnl any later version.
16 m4_dnl This program is distributed in the hope that it will be useful,
17 m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
18 m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 m4_dnl GNU General Public License for more details.
21 m4_dnl You should have received a copy of the GNU General Public License
22 m4_dnl along with this program; if not, write to the Free Software Foundation.
24 m4_include(hmacros.i4)
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
42 static FILE *Tinputfile, *Treportfile;
45 extern void Tshutdown(void) {
46 adns__vbuf_free(&vb2);
49 static void Tensurereportfile(void) {
53 if (Treportfile) return;
55 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
57 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
60 static void Psyntax(const char *where) {
61 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
65 static void Pcheckinput(void) {
66 if (ferror(Tinputfile)) Tfailed("read test log input file");
67 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
70 void Tensurerecordfile(void) {
74 unsigned long sec, usec;
76 if (Tinputfile) return;
78 fdstr= getenv("ADNS_TEST_IN_FD");
81 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
83 setvbuf(Tinputfile,0,_IONBF,0);
85 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
86 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
88 sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
89 if (chars==-1) Psyntax("start time invalid");
90 currenttime.tv_sec= sec;
91 currenttime.tv_usec= usec;
92 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
95 static void Parg(const char *argname) {
98 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
100 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
102 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
105 static int Pstring_maybe(const char *string) {
109 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
114 static void Pstring(const char *string, const char *emsg) {
115 if (Pstring_maybe(string)) return;
119 static int Perrno(const char *stuff) {
120 const struct Terrno *te;
124 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
125 if (te->n) return te->v;
126 r= strtoul(stuff+2,&ep,10);
127 if (*ep) Psyntax("errno value not recognised, not numeric");
131 static void P_updatetime(void) {
133 unsigned long sec, usec;
135 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
136 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
138 sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
139 if (chars==-1) Psyntax("update time invalid");
140 currenttime.tv_sec+= sec;
141 currenttime.tv_usec+= usec;
142 if (currenttime.tv_usec > 1000000) {
143 currenttime.tv_sec++;
144 currenttime.tv_usec -= 1000000;
146 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
149 static void Pfdset(fd_set *set, int max) {
153 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
155 if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
157 r= strtoul(vb2.buf+vb2.used,&ep,10);
158 if (r>=max) Psyntax("fd set member > max");
159 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
161 vb2.used= ep - (char*)vb2.buf;
162 c= vb2.buf[vb2.used++];
163 if (c == hm_squote]hm_squote) break;
164 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
169 static int Ppollfdevents(void) {
172 if (Pstring_maybe("0")) return 0;
175 if (Pstring_maybe("POLLIN")) {
177 if (!Pstring_maybe("|")) return events;
180 if (Pstring_maybe("POLLOUT")) {
182 if (!Pstring_maybe("|")) return events;
185 Pstring("POLLPRI","pollfdevents PRI?");
189 static void Ppollfds(struct pollfd *fds, int nfds) {
192 const char *comma= "";
194 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
195 for (i=0; i<nfds; i++) {
196 Pstring("{fd=","{fd= in pollfds");
197 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
198 vb2.used= ep - (char*)vb2.buf;
199 Pstring(", events=",", events= in pollfds");
200 fds->events= Ppollfdevents();
201 Pstring(", revents=",", revents= in pollfds");
202 fds->revents= Ppollfdevents();
203 Pstring("}","} in pollfds");
204 Pstring(comma,"separator in pollfds");
207 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
211 static void Paddr(struct sockaddr *addr, int *lenr) {
220 if (!q) Psyntax("missing :");
225 if (!q) Psyntax("missing ]");
227 if (*q!=':') Psyntax("expected : after ]");
230 ul= strtoul(q,&ep,10);
231 if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
232 if (ul >= 65536) Psyntax("port too large");
234 a.len= sizeof(a.addr);
235 err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
236 if (err) Psyntax("invalid address");
238 assert(*lenr >= a.len);
239 memcpy(addr, &a.addr, a.len);
241 vb2.used= ep - (char*)vb2.buf;
244 static int Pbytes(byte *buf, int maxlen) {
245 static const char hexdigits[]= "0123456789abcdef";
252 c= getc(Tinputfile); Pcheckinput();
253 if (c=='\n' || c==' ' || c=='\t') continue;
255 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
256 v= (pf-hexdigits)<<4;
257 c= getc(Tinputfile); Pcheckinput();
258 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
260 if (maxlen<=0) Psyntax("buffer overflow in bytes");
265 c= getc(Tinputfile); Pcheckinput();
266 if (c=='\n') return done;
274 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
275 fread(vb2.buf,1,vb.used+2,Tinputfile);
276 if (feof(Tinputfile)) {
277 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
282 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
283 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
284 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
286 "adns test harness: program did unexpected:\n %.*s\n"
287 "was expecting:\n %.*s\n",
288 vb.used,vb.buf, vb.used,vb2.buf+1);
292 nl= memchr(vb.buf,'\n',vb.used);
293 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
296 m4_define(`hm_syscall', `
298 int H$1(hm_args_massage($3,void)) {
300 m4_define(`hm_rv_fd',`char *ep;')
301 m4_define(`hm_rv_any',`char *ep;')
302 m4_define(`hm_rv_len',`')
303 m4_define(`hm_rv_must',`')
304 m4_define(`hm_rv_succfail',`')
305 m4_define(`hm_rv_fcntl',`')
308 hm_create_hqcall_vars
311 hm_create_hqcall_init($1)
314 hm_create_hqcall_args
315 Q$1(hm_args_massage($3));
317 m4_define(`hm_r_offset',`m4_len(` $1=')')
318 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
319 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
322 fprintf(Treportfile,"%s",vb2.buf);
323 amtread= strlen(vb2.buf);
324 if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
325 Psyntax("badly formed line");
327 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
329 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
331 e= Perrno(vb2.buf+hm_r_offset);
337 m4_define(`hm_rv_succfail',`
338 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
339 vb2.used= hm_r_offset+2;
342 m4_define(`hm_rv_len',`hm_rv_succfail')
343 m4_define(`hm_rv_must',`hm_rv_succfail')
344 m4_define(`hm_rv_any',`
345 r= strtoul(vb2.buf+hm_r_offset,&ep,10);
346 if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
347 vb2.used= ep - (char*)vb2.buf;
349 m4_define(`hm_rv_fd',`hm_rv_any')
350 m4_define(`hm_rv_fcntl',`
352 if (cmd == F_GETFL) {
353 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
355 vb2.used= hm_r_offset+14;
356 } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
357 vb2.used= hm_r_offset+15;
359 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
361 } else if (cmd == F_SETFL) {
364 Psyntax("fcntl not F_GETFL or F_SETFL");
370 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
371 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
372 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
374 assert(vb2.used <= amtread);
375 if (vb2.used != amtread) Psyntax("junk at end of line");
378 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
386 m4_define(`hm_specsyscall', `')
388 m4_include(`hsyscalls.i4')