| 1 | m4_dnl hplayback.c.m4 |
| 2 | m4_dnl (part of complex test harness, not of the library) |
| 3 | m4_dnl - playback routines |
| 4 | |
| 5 | m4_dnl This file is |
| 6 | m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
| 7 | m4_dnl |
| 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> |
| 11 | m4_dnl |
| 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. |
| 16 | m4_dnl |
| 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. |
| 21 | m4_dnl |
| 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. |
| 25 | |
| 26 | m4_include(hmacros.i4) |
| 27 | |
| 28 | #include <assert.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <arpa/inet.h> |
| 37 | #include <sys/time.h> |
| 38 | |
| 39 | #include <unistd.h> |
| 40 | #include <fcntl.h> |
| 41 | |
| 42 | #include "harness.h" |
| 43 | |
| 44 | static FILE *Tinputfile, *Treportfile; |
| 45 | static vbuf vb2; |
| 46 | |
| 47 | extern void Tshutdown(void) { |
| 48 | adns__vbuf_free(&vb2); |
| 49 | } |
| 50 | |
| 51 | static void Tensurereportfile(void) { |
| 52 | const char *fdstr; |
| 53 | int fd; |
| 54 | |
| 55 | if (Treportfile) return; |
| 56 | Treportfile= stderr; |
| 57 | fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return; |
| 58 | fd= atoi(fdstr); |
| 59 | Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD"); |
| 60 | } |
| 61 | |
| 62 | static void Psyntax(const char *where) { |
| 63 | fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where); |
| 64 | exit(-1); |
| 65 | } |
| 66 | |
| 67 | static void Pcheckinput(void) { |
| 68 | if (ferror(Tinputfile)) Tfailed("read test log input file"); |
| 69 | if (feof(Tinputfile)) Psyntax("eof at syscall reply"); |
| 70 | } |
| 71 | |
| 72 | void Tensurerecordfile(void) { |
| 73 | const char *fdstr; |
| 74 | int fd; |
| 75 | int chars; |
| 76 | unsigned long sec, usec; |
| 77 | |
| 78 | if (Tinputfile) return; |
| 79 | Tinputfile= stdin; |
| 80 | fdstr= getenv("ADNS_TEST_IN_FD"); |
| 81 | if (fdstr) { |
| 82 | fd= atoi(fdstr); |
| 83 | Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD"); |
| 84 | } |
| 85 | setvbuf(Tinputfile,0,_IONBF,0); |
| 86 | |
| 87 | if (!adns__vbuf_ensure(&vb2,1000)) Tnomem(); |
| 88 | fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput(); |
| 89 | chars= -1; |
| 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"); |
| 95 | } |
| 96 | |
| 97 | static void Parg(const char *argname) { |
| 98 | int l; |
| 99 | |
| 100 | if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument"); |
| 101 | l= strlen(argname); |
| 102 | if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong"); |
| 103 | vb2.used+= l; |
| 104 | if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name"); |
| 105 | } |
| 106 | |
| 107 | static int Pstring_maybe(const char *string) { |
| 108 | int l; |
| 109 | |
| 110 | l= strlen(string); |
| 111 | if (memcmp(vb2.buf+vb2.used,string,l)) return 0; |
| 112 | vb2.used+= l; |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | static void Pstring(const char *string, const char *emsg) { |
| 117 | if (Pstring_maybe(string)) return; |
| 118 | Psyntax(emsg); |
| 119 | } |
| 120 | |
| 121 | static int Perrno(const char *stuff) { |
| 122 | const struct Terrno *te; |
| 123 | int r; |
| 124 | char *ep; |
| 125 | |
| 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"); |
| 130 | return r; |
| 131 | } |
| 132 | |
| 133 | static void P_updatetime(void) { |
| 134 | int chars; |
| 135 | unsigned long sec, usec; |
| 136 | |
| 137 | if (!adns__vbuf_ensure(&vb2,1000)) Tnomem(); |
| 138 | fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput(); |
| 139 | chars= -1; |
| 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; |
| 147 | } |
| 148 | if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time"); |
| 149 | } |
| 150 | |
| 151 | static void Pfdset(fd_set *set, int max) { |
| 152 | int r, c; |
| 153 | char *ep; |
| 154 | |
| 155 | if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not ["); |
| 156 | FD_ZERO(set); |
| 157 | if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; } |
| 158 | for (;;) { |
| 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"); |
| 162 | FD_SET(r,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 ,"); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #ifdef HAVE_POLL |
| 171 | static int Ppollfdevents(void) { |
| 172 | int events; |
| 173 | |
| 174 | if (Pstring_maybe("0")) return 0; |
| 175 | events= 0; |
| 176 | |
| 177 | if (Pstring_maybe("POLLIN")) { |
| 178 | events |= POLLIN; |
| 179 | if (!Pstring_maybe("|")) return events; |
| 180 | } |
| 181 | |
| 182 | if (Pstring_maybe("POLLOUT")) { |
| 183 | events |= POLLOUT; |
| 184 | if (!Pstring_maybe("|")) return events; |
| 185 | } |
| 186 | |
| 187 | Pstring("POLLPRI","pollfdevents PRI?"); |
| 188 | return events; |
| 189 | } |
| 190 | |
| 191 | static void Ppollfds(struct pollfd *fds, int nfds) { |
| 192 | int i; |
| 193 | char *ep; |
| 194 | const char *comma= ""; |
| 195 | |
| 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"); |
| 207 | comma= ", "; |
| 208 | } |
| 209 | if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]"); |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | static void Paddr(struct sockaddr *addr, int *lenr) { |
| 214 | struct sockaddr_in *sa= (struct sockaddr_in*)addr; |
| 215 | char *p, *ep; |
| 216 | long ul; |
| 217 | |
| 218 | assert(*lenr >= sizeof(*sa)); |
| 219 | p= strchr(vb2.buf+vb2.used,':'); |
| 220 | if (!p) Psyntax("no port on address"); |
| 221 | *p++= 0; |
| 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); |
| 229 | *lenr= sizeof(*sa); |
| 230 | |
| 231 | vb2.used= ep - (char*)vb2.buf; |
| 232 | } |
| 233 | |
| 234 | static int Pbytes(byte *buf, int maxlen) { |
| 235 | static const char hexdigits[]= "0123456789abcdef"; |
| 236 | |
| 237 | int c, v, done; |
| 238 | const char *pf; |
| 239 | |
| 240 | done= 0; |
| 241 | for (;;) { |
| 242 | c= getc(Tinputfile); Pcheckinput(); |
| 243 | if (c=='\n' || c==' ' || c=='\t') continue; |
| 244 | if (c=='.') break; |
| 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"); |
| 249 | v |= (pf-hexdigits); |
| 250 | if (maxlen<=0) Psyntax("buffer overflow in bytes"); |
| 251 | *buf++= v; |
| 252 | maxlen--; done++; |
| 253 | } |
| 254 | for (;;) { |
| 255 | c= getc(Tinputfile); Pcheckinput(); |
| 256 | if (c=='\n') return done; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void Q_vb(void) { |
| 261 | int r; |
| 262 | const char *nl; |
| 263 | |
| 264 | Tensurerecordfile(); |
| 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", |
| 269 | vb.used,vb.buf); |
| 270 | exit(-1); |
| 271 | } |
| 272 | Pcheckinput(); |
| 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) { |
| 276 | fprintf(stderr, |
| 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); |
| 280 | exit(1); |
| 281 | } |
| 282 | Tensurereportfile(); |
| 283 | nl= memchr(vb.buf,'\n',vb.used); |
| 284 | fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf); |
| 285 | } |
| 286 | |
| 287 | m4_define(`hm_syscall', ` |
| 288 | hm_create_proto_h |
| 289 | int H$1(hm_args_massage($3,void)) { |
| 290 | int r, amtread; |
| 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',`') |
| 297 | $2 |
| 298 | |
| 299 | hm_create_hqcall_vars |
| 300 | $3 |
| 301 | |
| 302 | hm_create_hqcall_init($1) |
| 303 | $3 |
| 304 | |
| 305 | hm_create_hqcall_args |
| 306 | Q$1(hm_args_massage($3)); |
| 307 | |
| 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(); |
| 311 | |
| 312 | Tensurereportfile(); |
| 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"); |
| 317 | vb2.buf[amtread]= 0; |
| 318 | if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch"); |
| 319 | |
| 320 | if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) { |
| 321 | int e; |
| 322 | e= Perrno(vb2.buf+hm_r_offset); |
| 323 | P_updatetime(); |
| 324 | errno= e; |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 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; |
| 331 | r= 0; |
| 332 | ') |
| 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; |
| 339 | ') |
| 340 | m4_define(`hm_rv_fd',`hm_rv_any') |
| 341 | m4_define(`hm_rv_fcntl',` |
| 342 | r= 0; |
| 343 | if (cmd == F_GETFL) { |
| 344 | if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) { |
| 345 | r= O_NONBLOCK; |
| 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; |
| 349 | } else { |
| 350 | Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&..."); |
| 351 | } |
| 352 | } else if (cmd == F_SETFL) { |
| 353 | hm_rv_succfail |
| 354 | } else { |
| 355 | Psyntax("fcntl not F_GETFL or F_SETFL"); |
| 356 | } |
| 357 | ') |
| 358 | $2 |
| 359 | |
| 360 | hm_create_nothing |
| 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);') |
| 364 | $3 |
| 365 | assert(vb2.used <= amtread); |
| 366 | if (vb2.used != amtread) Psyntax("junk at end of line"); |
| 367 | |
| 368 | hm_create_nothing |
| 369 | m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);') |
| 370 | $3 |
| 371 | |
| 372 | P_updatetime(); |
| 373 | return r; |
| 374 | } |
| 375 | ') |
| 376 | |
| 377 | m4_define(`hm_specsyscall', `') |
| 378 | |
| 379 | m4_include(`hsyscalls.i4') |