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 void Tensurerecordfile(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) {
163 if (!q) Psyntax("missing :");
168 if (!q) Psyntax("missing ]");
170 if (*q!=':') Psyntax("expected : after ]");
173 ul= strtoul(q,&ep,10);
174 if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
175 if (ul >= 65536) Psyntax("port too large");
176 a.len= sizeof(a.addr);
177 err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
178 if (err) Psyntax("invalid address");
179 assert(*lenr >= a.len);
180 memcpy(addr, &a.addr, a.len);
182 vb2.used= ep - (char*)vb2.buf;
184 static int Pbytes(byte *buf, int maxlen) {
185 static const char hexdigits[]= "0123456789abcdef";
190 c= getc(Tinputfile); Pcheckinput();
191 if (c=='\n' || c==' ' || c=='\t') continue;
193 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
194 v= (pf-hexdigits)<<4;
195 c= getc(Tinputfile); Pcheckinput();
196 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
198 if (maxlen<=0) Psyntax("buffer overflow in bytes");
203 c= getc(Tinputfile); Pcheckinput();
204 if (c=='\n') return done;
210 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
211 fread(vb2.buf,1,vb.used+2,Tinputfile);
212 if (feof(Tinputfile)) {
213 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
218 if (vb2.buf[0] != ' ') Psyntax("not space before call");
219 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
220 vb2.buf[vb.used+1] != '\n') {
222 "adns test harness: program did unexpected:\n %.*s\n"
223 "was expecting:\n %.*s\n",
224 vb.used,vb.buf, vb.used,vb2.buf+1);
228 nl= memchr(vb.buf,'\n',vb.used);
229 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
231 int Hselect( int max , fd_set *rfds , fd_set *wfds , fd_set *efds , struct timeval *to ) {
234 Qselect( max , rfds , wfds , efds , to );
235 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
236 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
238 fprintf(Treportfile,"%s",vb2.buf);
239 amtread= strlen(vb2.buf);
240 if (amtread<=0 || vb2.buf[--amtread]!='\n')
241 Psyntax("badly formed line");
243 if (memcmp(vb2.buf," select=",8)) Psyntax("syscall reply mismatch");
244 if (vb2.buf[8] == 'E') {
246 e= Perrno(vb2.buf+8);
251 r= strtoul(vb2.buf+8,&ep,10);
252 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
253 vb2.used= ep - (char*)vb2.buf;
254 Parg("rfds"); Pfdset(rfds,max);
255 Parg("wfds"); Pfdset(wfds,max);
256 Parg("efds"); Pfdset(efds,max);
257 assert(vb2.used <= amtread);
258 if (vb2.used != amtread) Psyntax("junk at end of line");
263 int Hpoll( struct pollfd *fds , int nfds , int timeout ) {
266 Qpoll( fds , nfds , timeout );
267 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
268 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
270 fprintf(Treportfile,"%s",vb2.buf);
271 amtread= strlen(vb2.buf);
272 if (amtread<=0 || vb2.buf[--amtread]!='\n')
273 Psyntax("badly formed line");
275 if (memcmp(vb2.buf," poll=",6)) Psyntax("syscall reply mismatch");
276 if (vb2.buf[6] == 'E') {
278 e= Perrno(vb2.buf+6);
283 r= strtoul(vb2.buf+6,&ep,10);
284 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
285 vb2.used= ep - (char*)vb2.buf;
286 Parg("fds"); Ppollfds(fds,nfds);
287 assert(vb2.used <= amtread);
288 if (vb2.used != amtread) Psyntax("junk at end of line");
293 int Hsocket( int domain , int type , int protocol ) {
296 Tmust("socket","domain",domain==AF_INET || domain==AF_INET6);
297 Tmust("socket","type",type==SOCK_STREAM || type==SOCK_DGRAM);
298 Qsocket( domain , type );
299 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
300 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
302 fprintf(Treportfile,"%s",vb2.buf);
303 amtread= strlen(vb2.buf);
304 if (amtread<=0 || vb2.buf[--amtread]!='\n')
305 Psyntax("badly formed line");
307 if (memcmp(vb2.buf," socket=",8)) Psyntax("syscall reply mismatch");
308 if (vb2.buf[8] == 'E') {
310 e= Perrno(vb2.buf+8);
315 r= strtoul(vb2.buf+8,&ep,10);
316 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
317 vb2.used= ep - (char*)vb2.buf;
318 assert(vb2.used <= amtread);
319 if (vb2.used != amtread) Psyntax("junk at end of line");
323 int Hfcntl( int fd , int cmd , ... ) {
325 va_list al; long arg;
326 Tmust("fcntl","cmd",cmd==F_SETFL || cmd==F_GETFL);
327 if (cmd == F_SETFL) {
328 va_start(al,cmd); arg= va_arg(al,long); va_end(al);
332 Qfcntl( fd , cmd , arg );
333 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
334 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
336 fprintf(Treportfile,"%s",vb2.buf);
337 amtread= strlen(vb2.buf);
338 if (amtread<=0 || vb2.buf[--amtread]!='\n')
339 Psyntax("badly formed line");
341 if (memcmp(vb2.buf," fcntl=",7)) Psyntax("syscall reply mismatch");
342 if (vb2.buf[7] == 'E') {
344 e= Perrno(vb2.buf+7);
350 if (cmd == F_GETFL) {
351 if (!memcmp(vb2.buf+7,"O_NONBLOCK|...",14)) {
354 } else if (!memcmp(vb2.buf+7,"~O_NONBLOCK&...",15)) {
357 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
359 } else if (cmd == F_SETFL) {
360 if (memcmp(vb2.buf+7,"OK",2)) Psyntax("success/fail not E* or OK");
364 Psyntax("fcntl not F_GETFL or F_SETFL");
366 assert(vb2.used <= amtread);
367 if (vb2.used != amtread) Psyntax("junk at end of line");
371 int Hconnect( int fd , const struct sockaddr *addr , int addrlen ) {
373 Qconnect( fd , addr , addrlen );
374 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
375 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
377 fprintf(Treportfile,"%s",vb2.buf);
378 amtread= strlen(vb2.buf);
379 if (amtread<=0 || vb2.buf[--amtread]!='\n')
380 Psyntax("badly formed line");
382 if (memcmp(vb2.buf," connect=",9)) Psyntax("syscall reply mismatch");
383 if (vb2.buf[9] == 'E') {
385 e= Perrno(vb2.buf+9);
390 if (memcmp(vb2.buf+9,"OK",2)) Psyntax("success/fail not E* or OK");
393 assert(vb2.used <= amtread);
394 if (vb2.used != amtread) Psyntax("junk at end of line");
398 int Hbind( int fd , const struct sockaddr *addr , int addrlen ) {
400 Qbind( fd , addr , addrlen );
401 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
402 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
404 fprintf(Treportfile,"%s",vb2.buf);
405 amtread= strlen(vb2.buf);
406 if (amtread<=0 || vb2.buf[--amtread]!='\n')
407 Psyntax("badly formed line");
409 if (memcmp(vb2.buf," bind=",6)) Psyntax("syscall reply mismatch");
410 if (vb2.buf[6] == 'E') {
412 e= Perrno(vb2.buf+6);
417 if (memcmp(vb2.buf+6,"OK",2)) Psyntax("success/fail not E* or OK");
420 assert(vb2.used <= amtread);
421 if (vb2.used != amtread) Psyntax("junk at end of line");
425 int Hlisten( int fd , int backlog ) {
427 Qlisten( fd , backlog );
428 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
429 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
431 fprintf(Treportfile,"%s",vb2.buf);
432 amtread= strlen(vb2.buf);
433 if (amtread<=0 || vb2.buf[--amtread]!='\n')
434 Psyntax("badly formed line");
436 if (memcmp(vb2.buf," listen=",8)) Psyntax("syscall reply mismatch");
437 if (vb2.buf[8] == 'E') {
439 e= Perrno(vb2.buf+8);
444 if (memcmp(vb2.buf+8,"OK",2)) Psyntax("success/fail not E* or OK");
447 assert(vb2.used <= amtread);
448 if (vb2.used != amtread) Psyntax("junk at end of line");
452 int Hclose( int fd ) {
455 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
456 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
458 fprintf(Treportfile,"%s",vb2.buf);
459 amtread= strlen(vb2.buf);
460 if (amtread<=0 || vb2.buf[--amtread]!='\n')
461 Psyntax("badly formed line");
463 if (memcmp(vb2.buf," close=",7)) Psyntax("syscall reply mismatch");
464 if (vb2.buf[7] == 'E') {
466 e= Perrno(vb2.buf+7);
471 if (memcmp(vb2.buf+7,"OK",2)) Psyntax("success/fail not E* or OK");
474 assert(vb2.used <= amtread);
475 if (vb2.used != amtread) Psyntax("junk at end of line");
479 int Hsendto( int fd , const void *msg , int msglen , unsigned int flags , const struct sockaddr *addr , int addrlen ) {
482 Tmust("sendto","flags",flags==0);
483 Qsendto( fd , msg , msglen , addr , addrlen );
484 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
485 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
487 fprintf(Treportfile,"%s",vb2.buf);
488 amtread= strlen(vb2.buf);
489 if (amtread<=0 || vb2.buf[--amtread]!='\n')
490 Psyntax("badly formed line");
492 if (memcmp(vb2.buf," sendto=",8)) Psyntax("syscall reply mismatch");
493 if (vb2.buf[8] == 'E') {
495 e= Perrno(vb2.buf+8);
500 r= strtoul(vb2.buf+8,&ep,10);
501 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
502 vb2.used= ep - (char*)vb2.buf;
503 assert(vb2.used <= amtread);
504 if (vb2.used != amtread) Psyntax("junk at end of line");
508 int Hrecvfrom( int fd , void *buf , int buflen , unsigned int flags , struct sockaddr *addr , int *addrlen ) {
510 Tmust("recvfrom","flags",flags==0);
511 Tmust("recvfrom","*addrlen",*addrlen>=sizeof(struct sockaddr_in));
512 Qrecvfrom( fd , buflen , *addrlen );
513 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
514 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
516 fprintf(Treportfile,"%s",vb2.buf);
517 amtread= strlen(vb2.buf);
518 if (amtread<=0 || vb2.buf[--amtread]!='\n')
519 Psyntax("badly formed line");
521 if (memcmp(vb2.buf," recvfrom=",10)) Psyntax("syscall reply mismatch");
522 if (vb2.buf[10] == 'E') {
524 e= Perrno(vb2.buf+10);
529 if (memcmp(vb2.buf+10,"OK",2)) Psyntax("success/fail not E* or OK");
532 Parg("addr"); Paddr(addr,addrlen);
533 assert(vb2.used <= amtread);
534 if (vb2.used != amtread) Psyntax("junk at end of line");
535 r= Pbytes(buf,buflen);
539 int Hread( int fd , void *buf , size_t buflen ) {
541 Qread( fd , buflen );
542 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
543 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
545 fprintf(Treportfile,"%s",vb2.buf);
546 amtread= strlen(vb2.buf);
547 if (amtread<=0 || vb2.buf[--amtread]!='\n')
548 Psyntax("badly formed line");
550 if (memcmp(vb2.buf," read=",6)) Psyntax("syscall reply mismatch");
551 if (vb2.buf[6] == 'E') {
553 e= Perrno(vb2.buf+6);
558 if (memcmp(vb2.buf+6,"OK",2)) Psyntax("success/fail not E* or OK");
561 assert(vb2.used <= amtread);
562 if (vb2.used != amtread) Psyntax("junk at end of line");
563 r= Pbytes(buf,buflen);
567 int Hwrite( int fd , const void *buf , size_t len ) {
570 Qwrite( fd , buf , len );
571 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
572 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
574 fprintf(Treportfile,"%s",vb2.buf);
575 amtread= strlen(vb2.buf);
576 if (amtread<=0 || vb2.buf[--amtread]!='\n')
577 Psyntax("badly formed line");
579 if (memcmp(vb2.buf," write=",7)) Psyntax("syscall reply mismatch");
580 if (vb2.buf[7] == 'E') {
582 e= Perrno(vb2.buf+7);
587 r= strtoul(vb2.buf+7,&ep,10);
588 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
589 vb2.used= ep - (char*)vb2.buf;
590 assert(vb2.used <= amtread);
591 if (vb2.used != amtread) Psyntax("junk at end of line");