chiark / gitweb /
@@ -11,6 +11,11 @@
[adns] / regress / hplayback.c.m4
CommitLineData
9ba9f156 1m4_dnl hplayback.c.m4
2m4_dnl (part of complex test harness, not of the library)
3m4_dnl - playback routines
4
a79ac5ba 5m4_dnl This file is
89435c42 6m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
a79ac5ba 7m4_dnl
8m4_dnl It is part of adns, which is
89435c42 9m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
c6826df6 10m4_dnl Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
9ba9f156 11m4_dnl
12m4_dnl This program is free software; you can redistribute it and/or modify
13m4_dnl it under the terms of the GNU General Public License as published by
14m4_dnl the Free Software Foundation; either version 2, or (at your option)
15m4_dnl any later version.
16m4_dnl
17m4_dnl This program is distributed in the hope that it will be useful,
18m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
19m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20m4_dnl GNU General Public License for more details.
21m4_dnl
22m4_dnl You should have received a copy of the GNU General Public License
23m4_dnl along with this program; if not, write to the Free Software Foundation,
24m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26m4_include(hmacros.i4)
27
28#include <assert.h>
29#include <string.h>
9ba9f156 30#include <errno.h>
31#include <stdlib.h>
32
93b54ac1 33#include <sys/types.h>
9ba9f156 34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
4b30caad 37#include <sys/time.h>
9ba9f156 38
93b54ac1 39#include <unistd.h>
40#include <fcntl.h>
41
9ba9f156 42#include "harness.h"
43
e3324da1 44static FILE *Tinputfile, *Treportfile;
9ba9f156 45static vbuf vb2;
46
fc86e61f 47extern void Tshutdown(void) {
48 adns__vbuf_free(&vb2);
49}
50
e3324da1 51static void Tensurereportfile(void) {
52 const char *fdstr;
53 int fd;
54
f63b3158 55 if (Treportfile) return;
e3324da1 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
9ba9f156 62static 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
67static void Pcheckinput(void) {
68 if (ferror(Tinputfile)) Tfailed("read test log input file");
69 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
70}
71
0366bd10 72void Tensurerecordfile(void) {
3c5796f7 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 }
8744cce8 85 setvbuf(Tinputfile,0,_IONBF,0);
3c5796f7 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
9ba9f156 97static 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
2d5cce62 107static 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
116static void Pstring(const char *string, const char *emsg) {
117 if (Pstring_maybe(string)) return;
118 Psyntax(emsg);
119}
120
9ba9f156 121static 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;
4a3ca4ce 128 r= strtoul(stuff+2,&ep,10);
129 if (*ep) Psyntax("errno value not recognised, not numeric");
9ba9f156 130 return r;
131}
132
3c5796f7 133static void P_updatetime(void) {
134 int chars;
9ba9f156 135 unsigned long sec, usec;
3c5796f7 136
137 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
138 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
9ba9f156 139 chars= -1;
3c5796f7 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;
9ba9f156 147 }
3c5796f7 148 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
9ba9f156 149}
150
151static 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);
a067c9e9 157 if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
9ba9f156 158 for (;;) {
159 r= strtoul(vb2.buf+vb2.used,&ep,10);
160 if (r>=max) Psyntax("fd set member > max");
a067c9e9 161 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
9ba9f156 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
125de2a9 170#ifdef HAVE_POLL
2d5cce62 171static 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
191static 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}
125de2a9 211#endif
2d5cce62 212
9ba9f156 213static 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
234static 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
260void Q_vb(void) {
261 int r;
c0bc6aa8 262 const char *nl;
9ba9f156 263
0366bd10 264 Tensurerecordfile();
9ba9f156 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 }
c0bc6aa8 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);
9ba9f156 285}
286
287m4_define(`hm_syscall', `
288 hm_create_proto_h
289int H$1(hm_args_massage($3,void)) {
f63b3158 290 int r, amtread;
9ba9f156 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',`')
09a06b84 296 m4_define(`hm_rv_fcntl',`')
9ba9f156 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();
e3324da1 311
312 Tensurereportfile();
c0bc6aa8 313 fprintf(Treportfile,"%s",vb2.buf);
f63b3158 314 amtread= strlen(vb2.buf);
315 if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
9ba9f156 316 Psyntax("badly formed line");
f63b3158 317 vb2.buf[amtread]= 0;
9ba9f156 318 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
319
320 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
3c5796f7 321 int e;
322 e= Perrno(vb2.buf+hm_r_offset);
323 P_updatetime();
324 errno= e;
9ba9f156 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')
09a06b84 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 ')
9ba9f156 358 $2
359
360 hm_create_nothing
361 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
2d5cce62 362 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
9ba9f156 363 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
9ba9f156 364 $3
f63b3158 365 assert(vb2.used <= amtread);
366 if (vb2.used != amtread) Psyntax("junk at end of line");
9ba9f156 367
368 hm_create_nothing
369 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
370 $3
371
3c5796f7 372 P_updatetime();
9ba9f156 373 return r;
374}
375')
376
09aee00b 377m4_define(`hm_specsyscall', `')
378
9ba9f156 379m4_include(`hsyscalls.i4')