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-2016,2020 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>
45 static FILE *Tinputfile, *Treportfile;
48 extern void Tshutdown(void) {
49 adns__vbuf_free(&vb2);
52 static void Tensurereportfile(void) {
56 if (Treportfile) return;
58 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
60 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
63 static void Psyntax(const char *where) {
64 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
68 static void Pcheckinput(void) {
69 if (ferror(Tinputfile)) Tfailed("read test log input file");
70 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
73 void Tensurerecordfile(void) {
76 unsigned long sec, usec;
78 if (Tinputfile) return;
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");
129 if (r==0 || r>255) Psyntax("numeric errno out of range 1..255");
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) {
157 Pstring("null","null fdset pointer");
161 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
163 if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
165 ul= strtoul(vb2.buf+vb2.used,&ep,10);
166 if (ul>=max) Psyntax("fd set member > max");
167 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
169 vb2.used= ep - (char*)vb2.buf;
170 c= vb2.buf[vb2.used++];
171 if (c == hm_squote]hm_squote) break;
172 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
177 static int Ppollfdevents(void) {
180 if (Pstring_maybe("0")) return 0;
183 if (Pstring_maybe("POLLIN")) {
185 if (!Pstring_maybe("|")) return events;
188 if (Pstring_maybe("POLLOUT")) {
190 if (!Pstring_maybe("|")) return events;
193 Pstring("POLLPRI","pollfdevents PRI?");
197 static void Ppollfds(struct pollfd *fds, int nfds) {
200 const char *comma= "";
202 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
203 for (i=0; i<nfds; i++) {
204 Pstring("{fd=","{fd= in pollfds");
205 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
206 vb2.used= ep - (char*)vb2.buf;
207 Pstring(", events=",", events= in pollfds");
208 fds->events= Ppollfdevents();
209 Pstring(", revents=",", revents= in pollfds");
210 fds->revents= Ppollfdevents();
211 Pstring("}","} in pollfds");
212 Pstring(comma,"separator in pollfds");
215 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
219 static void Paddr(struct sockaddr *addr, int *lenr) {
228 if (!q) Psyntax("missing :");
233 if (!q) Psyntax("missing ]");
235 if (*q!=':') Psyntax("expected : after ]");
238 ul= strtoul(q,&ep,10);
239 if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
240 if (ul >= 65536) Psyntax("port too large");
242 a.len= sizeof(a.addr);
243 err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
244 if (err) Psyntax("invalid address");
246 assert(*lenr >= a.len);
247 memcpy(addr, &a.addr, a.len);
249 vb2.used= ep - (char*)vb2.buf;
252 static int Pbytes(byte *buf, int maxlen) {
253 static const char hexdigits[]= "0123456789abcdef";
260 c= getc(Tinputfile); Pcheckinput();
261 if (c=='\n' || c==' ' || c=='\t') continue;
263 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
264 v= (pf-hexdigits)<<4;
265 c= getc(Tinputfile); Pcheckinput();
266 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
268 if (maxlen<=0) Psyntax("buffer overflow in bytes");
273 c= getc(Tinputfile); Pcheckinput();
274 if (c=='\n') return done;
282 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
283 fread(vb2.buf,1,vb.used+2,Tinputfile);
284 if (feof(Tinputfile)) {
285 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
290 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
291 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
292 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
294 "adns test harness: program did unexpected:\n %.*s\n"
295 "was expecting:\n %.*s\n",
296 vb.used,vb.buf, vb.used,vb2.buf+1);
300 nl= memchr(vb.buf,'\n',vb.used);
301 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
304 m4_define(`hm_syscall', `
306 int H$1(hm_args_massage($3,void)) {
308 m4_define(`hm_rv_fd',`char *ep;')
309 m4_define(`hm_rv_any',`char *ep;')
310 m4_define(`hm_rv_len',`')
311 m4_define(`hm_rv_must',`')
312 m4_define(`hm_rv_succfail',`')
313 m4_define(`hm_rv_fcntl',`')
316 hm_create_hqcall_vars
319 hm_create_hqcall_init($1)
322 hm_create_hqcall_args
323 Q$1(hm_args_massage($3));
325 m4_define(`hm_r_offset',`m4_len(` $1=')')
326 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
327 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
330 fprintf(Treportfile,"%s",vb2.buf);
331 amtread= strlen(vb2.buf);
332 if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
333 Psyntax("badly formed line");
335 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
337 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
339 e= Perrno(vb2.buf+hm_r_offset);
345 m4_define(`hm_rv_succfail',`
346 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
347 vb2.used= hm_r_offset+2;
350 m4_define(`hm_rv_len',`hm_rv_succfail')
351 m4_define(`hm_rv_must',`hm_rv_succfail')
352 m4_define(`hm_rv_any',`
353 unsigned long ul_r= strtoul(vb2.buf+hm_r_offset,&ep,10);
354 if (ul_r < 0 || ul_r > INT_MAX ||
355 (*ep && *ep!=hm_squote hm_squote))
356 Psyntax("return value not E* or positive number");
358 vb2.used= ep - (char*)vb2.buf;
360 m4_define(`hm_rv_fd',`hm_rv_any')
361 m4_define(`hm_rv_fcntl',`
363 if (cmd == F_GETFL) {
364 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
366 vb2.used= hm_r_offset+14;
367 } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
368 vb2.used= hm_r_offset+15;
370 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
372 } else if (cmd == F_SETFL) {
375 Psyntax("fcntl not F_GETFL or F_SETFL");
381 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
382 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
383 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
385 assert(vb2.used <= amtread);
386 if (vb2.used != amtread) Psyntax("junk at end of line");
389 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
397 m4_define(`hm_specsyscall', `')
399 m4_include(`hsyscalls.i4')