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 (C) 1997-1999 Ian Jackson
7 m4_dnl This program is free software; you can redistribute it and/or modify
8 m4_dnl it under the terms of the GNU General Public License as published by
9 m4_dnl the Free Software Foundation; either version 2, or (at your option)
10 m4_dnl any later version.
12 m4_dnl This program is distributed in the hope that it will be useful,
13 m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
14 m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 m4_dnl GNU General Public License for more details.
17 m4_dnl You should have received a copy of the GNU General Public License
18 m4_dnl along with this program; if not, write to the Free Software Foundation,
19 m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 m4_include(hmacros.i4)
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
35 static FILE *Tinputfile, *Treportfile;
38 static void Tensurereportfile(void) {
43 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
45 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
48 static void Psyntax(const char *where) {
49 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
53 static void Pcheckinput(void) {
54 if (ferror(Tinputfile)) Tfailed("read test log input file");
55 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
58 static void Tensureinputfile(void) {
62 unsigned long sec, usec;
64 if (Tinputfile) return;
66 fdstr= getenv("ADNS_TEST_IN_FD");
69 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
72 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
73 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
75 sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
76 if (chars==-1) Psyntax("start time invalid");
77 currenttime.tv_sec= sec;
78 currenttime.tv_usec= usec;
79 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
82 static void Parg(const char *argname) {
85 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
87 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
89 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
92 static int Pstring_maybe(const char *string) {
96 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
101 static void Pstring(const char *string, const char *emsg) {
102 if (Pstring_maybe(string)) return;
106 static int Perrno(const char *stuff) {
107 const struct Terrno *te;
111 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
112 if (te->n) return te->v;
113 r= strtoul(stuff+1,&ep,10);
114 if (ep) Psyntax("errno value not recognised, not numeric");
118 static void P_updatetime(void) {
120 unsigned long sec, usec;
122 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
123 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
125 sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
126 if (chars==-1) Psyntax("update time invalid");
127 currenttime.tv_sec+= sec;
128 currenttime.tv_usec+= usec;
129 if (currenttime.tv_usec > 1000000) {
130 currenttime.tv_sec++;
131 currenttime.tv_usec -= 1000000;
133 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
136 static void Pfdset(fd_set *set, int max) {
140 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
143 r= strtoul(vb2.buf+vb2.used,&ep,10);
144 if (r>=max) Psyntax("fd set member > max");
146 vb2.used= ep - (char*)vb2.buf;
147 c= vb2.buf[vb2.used++];
148 if (c == hm_squote]hm_squote) break;
149 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
153 static int Ppollfdevents(void) {
156 if (Pstring_maybe("0")) return 0;
159 if (Pstring_maybe("POLLIN")) {
161 if (!Pstring_maybe("|")) return events;
164 if (Pstring_maybe("POLLOUT")) {
166 if (!Pstring_maybe("|")) return events;
169 Pstring("POLLPRI","pollfdevents PRI?");
173 static void Ppollfds(struct pollfd *fds, int nfds) {
176 const char *comma= "";
178 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
179 for (i=0; i<nfds; i++) {
180 Pstring("{fd=","{fd= in pollfds");
181 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
182 vb2.used= ep - (char*)vb2.buf;
183 Pstring(", events=",", events= in pollfds");
184 fds->events= Ppollfdevents();
185 Pstring(", revents=",", revents= in pollfds");
186 fds->revents= Ppollfdevents();
187 Pstring("}","} in pollfds");
188 Pstring(comma,"separator in pollfds");
191 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
194 static void Paddr(struct sockaddr *addr, int *lenr) {
195 struct sockaddr_in *sa= (struct sockaddr_in*)addr;
199 assert(*lenr >= sizeof(*sa));
200 p= strchr(vb2.buf+vb2.used,':');
201 if (!p) Psyntax("no port on address");
203 memset(sa,0,sizeof(*sa));
204 sa->sin_family= AF_INET;
205 if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address");
206 ul= strtoul(p,&ep,10);
207 if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)");
208 if (ul >= 65536) Psyntax("port too large");
209 sa->sin_port= htons(ul);
212 vb2.used= ep - (char*)vb2.buf;
215 static int Pbytes(byte *buf, int maxlen) {
216 static const char hexdigits[]= "0123456789abcdef";
223 c= getc(Tinputfile); Pcheckinput();
224 if (c=='\n' || c==' ' || c=='\t') continue;
226 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
227 v= (pf-hexdigits)<<4;
228 c= getc(Tinputfile); Pcheckinput();
229 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
231 if (maxlen<=0) Psyntax("buffer overflow in bytes");
236 c= getc(Tinputfile); Pcheckinput();
237 if (c=='\n') return done;
245 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
246 r= fread(vb2.buf,1,vb.used+2,Tinputfile);
247 if (feof(Tinputfile)) {
248 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
253 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
254 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
255 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
257 "adns test harness: program did unexpected:\n %.*s\n"
258 "was expecting:\n %.*s\n",
259 vb.used,vb.buf, vb.used,vb2.buf+1);
264 m4_define(`hm_syscall', `
266 int H$1(hm_args_massage($3,void)) {
268 m4_define(`hm_rv_fd',`char *ep;')
269 m4_define(`hm_rv_any',`char *ep;')
270 m4_define(`hm_rv_len',`')
271 m4_define(`hm_rv_must',`')
272 m4_define(`hm_rv_succfail',`')
273 m4_define(`hm_rv_fcntl',`')
276 hm_create_hqcall_vars
279 hm_create_hqcall_init($1)
282 hm_create_hqcall_args
283 Q$1(hm_args_massage($3));
285 m4_define(`hm_r_offset',`m4_len(` $1=')')
286 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
287 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
290 fprintf(Treportfile,"syscallr %s",vb2.buf);
291 vb2.avail= strlen(vb2.buf);
292 if (vb.avail<=0 || vb2.buf[--vb2.avail]!=hm_squote\nhm_squote)
293 Psyntax("badly formed line");
294 vb2.buf[vb2.avail]= 0;
295 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
297 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
299 e= Perrno(vb2.buf+hm_r_offset);
305 m4_define(`hm_rv_succfail',`
306 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
307 vb2.used= hm_r_offset+2;
310 m4_define(`hm_rv_len',`hm_rv_succfail')
311 m4_define(`hm_rv_must',`hm_rv_succfail')
312 m4_define(`hm_rv_any',`
313 r= strtoul(vb2.buf+hm_r_offset,&ep,10);
314 if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
315 vb2.used= ep - (char*)vb2.buf;
317 m4_define(`hm_rv_fd',`hm_rv_any')
318 m4_define(`hm_rv_fcntl',`
320 if (cmd == F_GETFL) {
321 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
323 vb2.used= hm_r_offset+14;
324 } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
325 vb2.used= hm_r_offset+15;
327 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
329 } else if (cmd == F_SETFL) {
332 Psyntax("fcntl not F_GETFL or F_SETFL");
338 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
339 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
340 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
342 if (vb2.used != vb2.avail) Psyntax("junk at end of line");
345 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
353 m4_include(`hsyscalls.i4')