2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - playback routines
6 m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
8 m4_dnl It is part of adns, which is
9 m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
10 m4_dnl Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
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 2, 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,
24 m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 m4_include(hmacros.i4)
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
44 static FILE *Tinputfile, *Treportfile;
47 extern void Tshutdown(void) {
48 adns__vbuf_free(&vb2);
51 static void Tensurereportfile(void) {
55 if (Treportfile) return;
57 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
59 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
62 static void Psyntax(const char *where) {
63 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
67 static void Pcheckinput(void) {
68 if (ferror(Tinputfile)) Tfailed("read test log input file");
69 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
72 void Tensurerecordfile(void) {
76 unsigned long sec, usec;
78 if (Tinputfile) return;
80 fdstr= getenv("ADNS_TEST_IN_FD");
83 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
85 setvbuf(Tinputfile,0,_IONBF,0);
87 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
88 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
90 sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
91 if (chars==-1) Psyntax("start time invalid");
92 currenttime.tv_sec= sec;
93 currenttime.tv_usec= usec;
94 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
97 static void Parg(const char *argname) {
100 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
102 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
104 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
107 static int Pstring_maybe(const char *string) {
111 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
116 static void Pstring(const char *string, const char *emsg) {
117 if (Pstring_maybe(string)) return;
121 static int Perrno(const char *stuff) {
122 const struct Terrno *te;
126 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
127 if (te->n) return te->v;
128 r= strtoul(stuff+2,&ep,10);
129 if (*ep) Psyntax("errno value not recognised, not numeric");
133 static void P_updatetime(void) {
135 unsigned long sec, usec;
137 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
138 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
140 sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
141 if (chars==-1) Psyntax("update time invalid");
142 currenttime.tv_sec+= sec;
143 currenttime.tv_usec+= usec;
144 if (currenttime.tv_usec > 1000000) {
145 currenttime.tv_sec++;
146 currenttime.tv_usec -= 1000000;
148 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
151 static void Pfdset(fd_set *set, int max) {
155 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
157 if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
159 r= strtoul(vb2.buf+vb2.used,&ep,10);
160 if (r>=max) Psyntax("fd set member > max");
161 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
163 vb2.used= ep - (char*)vb2.buf;
164 c= vb2.buf[vb2.used++];
165 if (c == hm_squote]hm_squote) break;
166 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
171 static int Ppollfdevents(void) {
174 if (Pstring_maybe("0")) return 0;
177 if (Pstring_maybe("POLLIN")) {
179 if (!Pstring_maybe("|")) return events;
182 if (Pstring_maybe("POLLOUT")) {
184 if (!Pstring_maybe("|")) return events;
187 Pstring("POLLPRI","pollfdevents PRI?");
191 static void Ppollfds(struct pollfd *fds, int nfds) {
194 const char *comma= "";
196 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
197 for (i=0; i<nfds; i++) {
198 Pstring("{fd=","{fd= in pollfds");
199 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
200 vb2.used= ep - (char*)vb2.buf;
201 Pstring(", events=",", events= in pollfds");
202 fds->events= Ppollfdevents();
203 Pstring(", revents=",", revents= in pollfds");
204 fds->revents= Ppollfdevents();
205 Pstring("}","} in pollfds");
206 Pstring(comma,"separator in pollfds");
209 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
213 static void Paddr(struct sockaddr *addr, int *lenr) {
214 struct sockaddr_in *sa= (struct sockaddr_in*)addr;
218 assert(*lenr >= sizeof(*sa));
219 p= strchr(vb2.buf+vb2.used,':');
220 if (!p) Psyntax("no port on address");
222 memset(sa,0,sizeof(*sa));
223 sa->sin_family= AF_INET;
224 if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address");
225 ul= strtoul(p,&ep,10);
226 if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)");
227 if (ul >= 65536) Psyntax("port too large");
228 sa->sin_port= htons(ul);
231 vb2.used= ep - (char*)vb2.buf;
234 static int Pbytes(byte *buf, int maxlen) {
235 static const char hexdigits[]= "0123456789abcdef";
242 c= getc(Tinputfile); Pcheckinput();
243 if (c=='\n' || c==' ' || c=='\t') continue;
245 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
246 v= (pf-hexdigits)<<4;
247 c= getc(Tinputfile); Pcheckinput();
248 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
250 if (maxlen<=0) Psyntax("buffer overflow in bytes");
255 c= getc(Tinputfile); Pcheckinput();
256 if (c=='\n') return done;
265 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
266 r= fread(vb2.buf,1,vb.used+2,Tinputfile);
267 if (feof(Tinputfile)) {
268 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
273 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
274 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
275 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
277 "adns test harness: program did unexpected:\n %.*s\n"
278 "was expecting:\n %.*s\n",
279 vb.used,vb.buf, vb.used,vb2.buf+1);
283 nl= memchr(vb.buf,'\n',vb.used);
284 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
287 m4_define(`hm_syscall', `
289 int H$1(hm_args_massage($3,void)) {
291 m4_define(`hm_rv_fd',`char *ep;')
292 m4_define(`hm_rv_any',`char *ep;')
293 m4_define(`hm_rv_len',`')
294 m4_define(`hm_rv_must',`')
295 m4_define(`hm_rv_succfail',`')
296 m4_define(`hm_rv_fcntl',`')
299 hm_create_hqcall_vars
302 hm_create_hqcall_init($1)
305 hm_create_hqcall_args
306 Q$1(hm_args_massage($3));
308 m4_define(`hm_r_offset',`m4_len(` $1=')')
309 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
310 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
313 fprintf(Treportfile,"%s",vb2.buf);
314 amtread= strlen(vb2.buf);
315 if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
316 Psyntax("badly formed line");
318 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
320 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
322 e= Perrno(vb2.buf+hm_r_offset);
328 m4_define(`hm_rv_succfail',`
329 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
330 vb2.used= hm_r_offset+2;
333 m4_define(`hm_rv_len',`hm_rv_succfail')
334 m4_define(`hm_rv_must',`hm_rv_succfail')
335 m4_define(`hm_rv_any',`
336 r= strtoul(vb2.buf+hm_r_offset,&ep,10);
337 if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
338 vb2.used= ep - (char*)vb2.buf;
340 m4_define(`hm_rv_fd',`hm_rv_any')
341 m4_define(`hm_rv_fcntl',`
343 if (cmd == F_GETFL) {
344 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
346 vb2.used= hm_r_offset+14;
347 } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
348 vb2.used= hm_r_offset+15;
350 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
352 } else if (cmd == F_SETFL) {
355 Psyntax("fcntl not F_GETFL or F_SETFL");
361 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
362 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
363 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
365 assert(vb2.used <= amtread);
366 if (vb2.used != amtread) Psyntax("junk at end of line");
369 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
377 m4_define(`hm_specsyscall', `')
379 m4_include(`hsyscalls.i4')