6 #include <sys/socket.h>
7 #include <netinet/in.h>
13 static FILE *Tinputfile, *Treportfile;
15 extern void Tshutdown(void) {
16 adns__vbuf_free(&vb2);
18 static void Tensurereportfile(void) {
21 if (Treportfile) return;
23 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
25 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
27 static void Psyntax(const char *where) {
28 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
31 static void Pcheckinput(void) {
32 if (ferror(Tinputfile)) Tfailed("read test log input file");
33 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
35 static void Tensureinputfile(void) {
39 unsigned long sec, usec;
40 if (Tinputfile) return;
42 fdstr= getenv("ADNS_TEST_IN_FD");
45 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
47 setvbuf(Tinputfile,0,_IONBF,0);
48 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
49 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
51 sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
52 if (chars==-1) Psyntax("start time invalid");
53 currenttime.tv_sec= sec;
54 currenttime.tv_usec= usec;
55 if (vb2.buf[chars] != '\n') Psyntax("not newline after start time");
57 static void Parg(const char *argname) {
59 if (vb2.buf[vb2.used++] != ' ') Psyntax("not a space before argument");
61 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
63 if (vb2.buf[vb2.used++] != '=') Psyntax("not = after argument name");
65 static int Pstring_maybe(const char *string) {
68 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
72 static void Pstring(const char *string, const char *emsg) {
73 if (Pstring_maybe(string)) return;
76 static int Perrno(const char *stuff) {
77 const struct Terrno *te;
80 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
81 if (te->n) return te->v;
82 r= strtoul(stuff+2,&ep,10);
83 if (*ep) Psyntax("errno value not recognised, not numeric");
86 static void P_updatetime(void) {
88 unsigned long sec, usec;
89 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
90 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
92 sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
93 if (chars==-1) Psyntax("update time invalid");
94 currenttime.tv_sec+= sec;
95 currenttime.tv_usec+= usec;
96 if (currenttime.tv_usec > 1000000) {
98 currenttime.tv_usec -= 1000000;
100 if (vb2.buf[chars] != '\n') Psyntax("not newline after update time");
102 static void Pfdset(fd_set *set, int max) {
105 if (vb2.buf[vb2.used++] != '[') Psyntax("fd set start not [");
107 if (vb2.buf[vb2.used] == ']') { vb2.used++; return; }
109 r= strtoul(vb2.buf+vb2.used,&ep,10);
110 if (r>=max) Psyntax("fd set member > max");
111 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
113 vb2.used= ep - (char*)vb2.buf;
114 c= vb2.buf[vb2.used++];
116 if (c != ',') Psyntax("fd set separator not ,");
120 static int Ppollfdevents(void) {
122 if (Pstring_maybe("0")) return 0;
124 if (Pstring_maybe("POLLIN")) {
126 if (!Pstring_maybe("|")) return events;
128 if (Pstring_maybe("POLLOUT")) {
130 if (!Pstring_maybe("|")) return events;
132 Pstring("POLLPRI","pollfdevents PRI?");
135 static void Ppollfds(struct pollfd *fds, int nfds) {
138 const char *comma= "";
139 if (vb2.buf[vb2.used++] != '[') Psyntax("pollfds start not [");
140 for (i=0; i<nfds; i++) {
141 Pstring("{fd=","{fd= in pollfds");
142 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
143 vb2.used= ep - (char*)vb2.buf;
144 Pstring(", events=",", events= in pollfds");
145 fds->events= Ppollfdevents();
146 Pstring(", revents=",", revents= in pollfds");
147 fds->revents= Ppollfdevents();
148 Pstring("}","} in pollfds");
149 Pstring(comma,"separator in pollfds");
152 if (vb2.buf[vb2.used++] != ']') Psyntax("pollfds end not ]");
155 static void Paddr(struct sockaddr *addr, int *lenr) {
156 struct sockaddr_in *sa= (struct sockaddr_in*)addr;
159 assert(*lenr >= sizeof(*sa));
160 p= strchr(vb2.buf+vb2.used,':');
161 if (!p) Psyntax("no port on address");
163 memset(sa,0,sizeof(*sa));
164 sa->sin_family= AF_INET;
165 if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address");
166 ul= strtoul(p,&ep,10);
167 if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
168 if (ul >= 65536) Psyntax("port too large");
169 sa->sin_port= htons(ul);
171 vb2.used= ep - (char*)vb2.buf;
173 static int Pbytes(byte *buf, int maxlen) {
174 static const char hexdigits[]= "0123456789abcdef";
179 c= getc(Tinputfile); Pcheckinput();
180 if (c=='\n' || c==' ' || c=='\t') continue;
182 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
183 v= (pf-hexdigits)<<4;
184 c= getc(Tinputfile); Pcheckinput();
185 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
187 if (maxlen<=0) Psyntax("buffer overflow in bytes");
192 c= getc(Tinputfile); Pcheckinput();
193 if (c=='\n') return done;
200 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
201 r= fread(vb2.buf,1,vb.used+2,Tinputfile);
202 if (feof(Tinputfile)) {
203 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
208 if (vb2.buf[0] != ' ') Psyntax("not space before call");
209 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
210 vb2.buf[vb.used+1] != '\n') {
212 "adns test harness: program did unexpected:\n %.*s\n"
213 "was expecting:\n %.*s\n",
214 vb.used,vb.buf, vb.used,vb2.buf+1);
218 nl= memchr(vb.buf,'\n',vb.used);
219 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
221 int Hselect( int max , fd_set *rfds , fd_set *wfds , fd_set *efds , struct timeval *to ) {
224 Qselect( max , rfds , wfds , efds , to );
225 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
226 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
228 fprintf(Treportfile,"%s",vb2.buf);
229 amtread= strlen(vb2.buf);
230 if (amtread<=0 || vb2.buf[--amtread]!='\n')
231 Psyntax("badly formed line");
233 if (memcmp(vb2.buf," select=",8)) Psyntax("syscall reply mismatch");
234 if (vb2.buf[8] == 'E') {
236 e= Perrno(vb2.buf+8);
241 r= strtoul(vb2.buf+8,&ep,10);
242 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
243 vb2.used= ep - (char*)vb2.buf;
244 Parg("rfds"); Pfdset(rfds,max);
245 Parg("wfds"); Pfdset(wfds,max);
246 Parg("efds"); Pfdset(efds,max);
247 assert(vb2.used <= amtread);
248 if (vb2.used != amtread) Psyntax("junk at end of line");
253 int Hpoll( struct pollfd *fds , int nfds , int timeout ) {
256 Qpoll( fds , nfds , timeout );
257 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
258 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
260 fprintf(Treportfile,"%s",vb2.buf);
261 amtread= strlen(vb2.buf);
262 if (amtread<=0 || vb2.buf[--amtread]!='\n')
263 Psyntax("badly formed line");
265 if (memcmp(vb2.buf," poll=",6)) Psyntax("syscall reply mismatch");
266 if (vb2.buf[6] == 'E') {
268 e= Perrno(vb2.buf+6);
273 r= strtoul(vb2.buf+6,&ep,10);
274 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
275 vb2.used= ep - (char*)vb2.buf;
276 Parg("fds"); Ppollfds(fds,nfds);
277 assert(vb2.used <= amtread);
278 if (vb2.used != amtread) Psyntax("junk at end of line");
283 int Hsocket( int domain , int type , int protocol ) {
286 Tmust("socket","domain",domain==AF_INET);
287 Tmust("socket","type",type==SOCK_STREAM || type==SOCK_DGRAM);
289 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
290 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
292 fprintf(Treportfile,"%s",vb2.buf);
293 amtread= strlen(vb2.buf);
294 if (amtread<=0 || vb2.buf[--amtread]!='\n')
295 Psyntax("badly formed line");
297 if (memcmp(vb2.buf," socket=",8)) Psyntax("syscall reply mismatch");
298 if (vb2.buf[8] == 'E') {
300 e= Perrno(vb2.buf+8);
305 r= strtoul(vb2.buf+8,&ep,10);
306 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
307 vb2.used= ep - (char*)vb2.buf;
308 assert(vb2.used <= amtread);
309 if (vb2.used != amtread) Psyntax("junk at end of line");
313 int Hfcntl( int fd , int cmd , ... ) {
315 va_list al; long arg;
316 Tmust("fcntl","cmd",cmd==F_SETFL || cmd==F_GETFL);
317 if (cmd == F_SETFL) {
318 va_start(al,cmd); arg= va_arg(al,long); va_end(al);
322 Qfcntl( fd , cmd , arg );
323 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
324 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
326 fprintf(Treportfile,"%s",vb2.buf);
327 amtread= strlen(vb2.buf);
328 if (amtread<=0 || vb2.buf[--amtread]!='\n')
329 Psyntax("badly formed line");
331 if (memcmp(vb2.buf," fcntl=",7)) Psyntax("syscall reply mismatch");
332 if (vb2.buf[7] == 'E') {
334 e= Perrno(vb2.buf+7);
340 if (cmd == F_GETFL) {
341 if (!memcmp(vb2.buf+7,"O_NONBLOCK|...",14)) {
344 } else if (!memcmp(vb2.buf+7,"~O_NONBLOCK&...",15)) {
347 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
349 } else if (cmd == F_SETFL) {
350 if (memcmp(vb2.buf+7,"OK",2)) Psyntax("success/fail not E* or OK");
354 Psyntax("fcntl not F_GETFL or F_SETFL");
356 assert(vb2.used <= amtread);
357 if (vb2.used != amtread) Psyntax("junk at end of line");
361 int Hconnect( int fd , const struct sockaddr *addr , int addrlen ) {
363 Qconnect( fd , addr , addrlen );
364 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
365 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
367 fprintf(Treportfile,"%s",vb2.buf);
368 amtread= strlen(vb2.buf);
369 if (amtread<=0 || vb2.buf[--amtread]!='\n')
370 Psyntax("badly formed line");
372 if (memcmp(vb2.buf," connect=",9)) Psyntax("syscall reply mismatch");
373 if (vb2.buf[9] == 'E') {
375 e= Perrno(vb2.buf+9);
380 if (memcmp(vb2.buf+9,"OK",2)) Psyntax("success/fail not E* or OK");
383 assert(vb2.used <= amtread);
384 if (vb2.used != amtread) Psyntax("junk at end of line");
388 int Hclose( int fd ) {
391 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
392 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
394 fprintf(Treportfile,"%s",vb2.buf);
395 amtread= strlen(vb2.buf);
396 if (amtread<=0 || vb2.buf[--amtread]!='\n')
397 Psyntax("badly formed line");
399 if (memcmp(vb2.buf," close=",7)) Psyntax("syscall reply mismatch");
400 if (vb2.buf[7] == 'E') {
402 e= Perrno(vb2.buf+7);
407 if (memcmp(vb2.buf+7,"OK",2)) Psyntax("success/fail not E* or OK");
410 assert(vb2.used <= amtread);
411 if (vb2.used != amtread) Psyntax("junk at end of line");
415 int Hsendto( int fd , const void *msg , int msglen , unsigned int flags , const struct sockaddr *addr , int addrlen ) {
418 Tmust("sendto","flags",flags==0);
419 Qsendto( fd , msg , msglen , addr , addrlen );
420 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
421 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
423 fprintf(Treportfile,"%s",vb2.buf);
424 amtread= strlen(vb2.buf);
425 if (amtread<=0 || vb2.buf[--amtread]!='\n')
426 Psyntax("badly formed line");
428 if (memcmp(vb2.buf," sendto=",8)) Psyntax("syscall reply mismatch");
429 if (vb2.buf[8] == 'E') {
431 e= Perrno(vb2.buf+8);
436 r= strtoul(vb2.buf+8,&ep,10);
437 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
438 vb2.used= ep - (char*)vb2.buf;
439 assert(vb2.used <= amtread);
440 if (vb2.used != amtread) Psyntax("junk at end of line");
444 int Hrecvfrom( int fd , void *buf , int buflen , unsigned int flags , struct sockaddr *addr , int *addrlen ) {
446 Tmust("recvfrom","flags",flags==0);
447 Tmust("recvfrom","*addrlen",*addrlen>=sizeof(struct sockaddr_in));
448 Qrecvfrom( fd , buflen , *addrlen );
449 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
450 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
452 fprintf(Treportfile,"%s",vb2.buf);
453 amtread= strlen(vb2.buf);
454 if (amtread<=0 || vb2.buf[--amtread]!='\n')
455 Psyntax("badly formed line");
457 if (memcmp(vb2.buf," recvfrom=",10)) Psyntax("syscall reply mismatch");
458 if (vb2.buf[10] == 'E') {
460 e= Perrno(vb2.buf+10);
465 if (memcmp(vb2.buf+10,"OK",2)) Psyntax("success/fail not E* or OK");
468 Parg("addr"); Paddr(addr,addrlen);
469 assert(vb2.used <= amtread);
470 if (vb2.used != amtread) Psyntax("junk at end of line");
471 r= Pbytes(buf,buflen);
475 int Hread( int fd , void *buf , size_t buflen ) {
477 Qread( fd , buflen );
478 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
479 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
481 fprintf(Treportfile,"%s",vb2.buf);
482 amtread= strlen(vb2.buf);
483 if (amtread<=0 || vb2.buf[--amtread]!='\n')
484 Psyntax("badly formed line");
486 if (memcmp(vb2.buf," read=",6)) Psyntax("syscall reply mismatch");
487 if (vb2.buf[6] == 'E') {
489 e= Perrno(vb2.buf+6);
494 if (memcmp(vb2.buf+6,"OK",2)) Psyntax("success/fail not E* or OK");
497 assert(vb2.used <= amtread);
498 if (vb2.used != amtread) Psyntax("junk at end of line");
499 r= Pbytes(buf,buflen);
503 int Hwrite( int fd , const void *buf , size_t len ) {
506 Qwrite( fd , buf , len );
507 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
508 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
510 fprintf(Treportfile,"%s",vb2.buf);
511 amtread= strlen(vb2.buf);
512 if (amtread<=0 || vb2.buf[--amtread]!='\n')
513 Psyntax("badly formed line");
515 if (memcmp(vb2.buf," write=",7)) Psyntax("syscall reply mismatch");
516 if (vb2.buf[7] == 'E') {
518 e= Perrno(vb2.buf+7);
523 r= strtoul(vb2.buf+7,&ep,10);
524 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
525 vb2.used= ep - (char*)vb2.buf;
526 assert(vb2.used <= amtread);
527 if (vb2.used != amtread) Psyntax("junk at end of line");