Regression tests compile on systems without poll(2). Do not install
[adns] / regress / hplayback.c.m4
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 part of adns, which is Copyright (C) 1997-1999 Ian Jackson
6 m4_dnl
7 m4_dnl This program is free software; you can redistribute it and/or modify
8 m4_dnl it under the terms of the GNU General Public License as published by
9 m4_dnl the Free Software Foundation; either version 2, or (at your option)
10 m4_dnl any later version.
11 m4_dnl
12 m4_dnl This program is distributed in the hope that it will be useful,
13 m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
14 m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 m4_dnl GNU General Public License for more details.
16 m4_dnl
17 m4_dnl You should have received a copy of the GNU General Public License
18 m4_dnl along with this program; if not, write to the Free Software Foundation,
19 m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21 m4_include(hmacros.i4)
22
23 #include <assert.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <stdlib.h>
28
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32
33 #include "harness.h"
34
35 static FILE *Tinputfile, *Treportfile;
36 static vbuf vb2;
37
38 static void Tensurereportfile(void) {
39 const char *fdstr;
40 int fd;
41
42 Treportfile= stderr;
43 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
44 fd= atoi(fdstr);
45 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
46 }
47
48 static void Psyntax(const char *where) {
49 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
50 exit(-1);
51 }
52
53 static void Pcheckinput(void) {
54 if (ferror(Tinputfile)) Tfailed("read test log input file");
55 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
56 }
57
58 static void Tensureinputfile(void) {
59 const char *fdstr;
60 int fd;
61 int chars;
62 unsigned long sec, usec;
63
64 if (Tinputfile) return;
65 Tinputfile= stdin;
66 fdstr= getenv("ADNS_TEST_IN_FD");
67 if (fdstr) {
68 fd= atoi(fdstr);
69 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
70 }
71
72 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
73 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
74 chars= -1;
75 sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
76 if (chars==-1) Psyntax("start time invalid");
77 currenttime.tv_sec= sec;
78 currenttime.tv_usec= usec;
79 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
80 }
81
82 static void Parg(const char *argname) {
83 int l;
84
85 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
86 l= strlen(argname);
87 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
88 vb2.used+= l;
89 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
90 }
91
92 static int Pstring_maybe(const char *string) {
93 int l;
94
95 l= strlen(string);
96 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
97 vb2.used+= l;
98 return 1;
99 }
100
101 static void Pstring(const char *string, const char *emsg) {
102 if (Pstring_maybe(string)) return;
103 Psyntax(emsg);
104 }
105
106 static int Perrno(const char *stuff) {
107 const struct Terrno *te;
108 int r;
109 char *ep;
110
111 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
112 if (te->n) return te->v;
113 r= strtoul(stuff+1,&ep,10);
114 if (ep) Psyntax("errno value not recognised, not numeric");
115 return r;
116 }
117
118 static void P_updatetime(void) {
119 int chars;
120 unsigned long sec, usec;
121
122 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
123 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
124 chars= -1;
125 sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
126 if (chars==-1) Psyntax("update time invalid");
127 currenttime.tv_sec+= sec;
128 currenttime.tv_usec+= usec;
129 if (currenttime.tv_usec > 1000000) {
130 currenttime.tv_sec++;
131 currenttime.tv_usec -= 1000000;
132 }
133 if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time");
134 }
135
136 static void Pfdset(fd_set *set, int max) {
137 int r, c;
138 char *ep;
139
140 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
141 FD_ZERO(set);
142 for (;;) {
143 r= strtoul(vb2.buf+vb2.used,&ep,10);
144 if (r>=max) Psyntax("fd set member > max");
145 FD_SET(r,set);
146 vb2.used= ep - (char*)vb2.buf;
147 c= vb2.buf[vb2.used++];
148 if (c == hm_squote]hm_squote) break;
149 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
150 }
151 }
152
153 #ifdef HAVE_POLL
154 static int Ppollfdevents(void) {
155 int events;
156
157 if (Pstring_maybe("0")) return 0;
158 events= 0;
159
160 if (Pstring_maybe("POLLIN")) {
161 events |= POLLIN;
162 if (!Pstring_maybe("|")) return events;
163 }
164
165 if (Pstring_maybe("POLLOUT")) {
166 events |= POLLOUT;
167 if (!Pstring_maybe("|")) return events;
168 }
169
170 Pstring("POLLPRI","pollfdevents PRI?");
171 return events;
172 }
173
174 static void Ppollfds(struct pollfd *fds, int nfds) {
175 int i;
176 char *ep;
177 const char *comma= "";
178
179 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
180 for (i=0; i<nfds; i++) {
181 Pstring("{fd=","{fd= in pollfds");
182 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
183 vb2.used= ep - (char*)vb2.buf;
184 Pstring(", events=",", events= in pollfds");
185 fds->events= Ppollfdevents();
186 Pstring(", revents=",", revents= in pollfds");
187 fds->revents= Ppollfdevents();
188 Pstring("}","} in pollfds");
189 Pstring(comma,"separator in pollfds");
190 comma= ", ";
191 }
192 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
193 }
194 #endif
195
196 static void Paddr(struct sockaddr *addr, int *lenr) {
197 struct sockaddr_in *sa= (struct sockaddr_in*)addr;
198 char *p, *ep;
199 long ul;
200
201 assert(*lenr >= sizeof(*sa));
202 p= strchr(vb2.buf+vb2.used,':');
203 if (!p) Psyntax("no port on address");
204 *p++= 0;
205 memset(sa,0,sizeof(*sa));
206 sa->sin_family= AF_INET;
207 if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address");
208 ul= strtoul(p,&ep,10);
209 if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)");
210 if (ul >= 65536) Psyntax("port too large");
211 sa->sin_port= htons(ul);
212 *lenr= sizeof(*sa);
213
214 vb2.used= ep - (char*)vb2.buf;
215 }
216
217 static int Pbytes(byte *buf, int maxlen) {
218 static const char hexdigits[]= "0123456789abcdef";
219
220 int c, v, done;
221 const char *pf;
222
223 done= 0;
224 for (;;) {
225 c= getc(Tinputfile); Pcheckinput();
226 if (c=='\n' || c==' ' || c=='\t') continue;
227 if (c=='.') break;
228 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
229 v= (pf-hexdigits)<<4;
230 c= getc(Tinputfile); Pcheckinput();
231 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
232 v |= (pf-hexdigits);
233 if (maxlen<=0) Psyntax("buffer overflow in bytes");
234 *buf++= v;
235 maxlen--; done++;
236 }
237 for (;;) {
238 c= getc(Tinputfile); Pcheckinput();
239 if (c=='\n') return done;
240 }
241 }
242
243 void Q_vb(void) {
244 int r;
245
246 Tensureinputfile();
247 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
248 r= fread(vb2.buf,1,vb.used+2,Tinputfile);
249 if (feof(Tinputfile)) {
250 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
251 vb.used,vb.buf);
252 exit(-1);
253 }
254 Pcheckinput();
255 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
256 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
257 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
258 fprintf(stderr,
259 "adns test harness: program did unexpected:\n %.*s\n"
260 "was expecting:\n %.*s\n",
261 vb.used,vb.buf, vb.used,vb2.buf+1);
262 exit(1);
263 }
264 }
265
266 m4_define(`hm_syscall', `
267 hm_create_proto_h
268 int H$1(hm_args_massage($3,void)) {
269 int r;
270 m4_define(`hm_rv_fd',`char *ep;')
271 m4_define(`hm_rv_any',`char *ep;')
272 m4_define(`hm_rv_len',`')
273 m4_define(`hm_rv_must',`')
274 m4_define(`hm_rv_succfail',`')
275 m4_define(`hm_rv_fcntl',`')
276 $2
277
278 hm_create_hqcall_vars
279 $3
280
281 hm_create_hqcall_init($1)
282 $3
283
284 hm_create_hqcall_args
285 Q$1(hm_args_massage($3));
286
287 m4_define(`hm_r_offset',`m4_len(` $1=')')
288 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
289 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
290
291 Tensurereportfile();
292 fprintf(Treportfile,"syscallr %s",vb2.buf);
293 vb2.avail= strlen(vb2.buf);
294 if (vb.avail<=0 || vb2.buf[--vb2.avail]!=hm_squote\nhm_squote)
295 Psyntax("badly formed line");
296 vb2.buf[vb2.avail]= 0;
297 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
298
299 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
300 int e;
301 e= Perrno(vb2.buf+hm_r_offset);
302 P_updatetime();
303 errno= e;
304 return -1;
305 }
306
307 m4_define(`hm_rv_succfail',`
308 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
309 vb2.used= hm_r_offset+2;
310 r= 0;
311 ')
312 m4_define(`hm_rv_len',`hm_rv_succfail')
313 m4_define(`hm_rv_must',`hm_rv_succfail')
314 m4_define(`hm_rv_any',`
315 r= strtoul(vb2.buf+hm_r_offset,&ep,10);
316 if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
317 vb2.used= ep - (char*)vb2.buf;
318 ')
319 m4_define(`hm_rv_fd',`hm_rv_any')
320 m4_define(`hm_rv_fcntl',`
321 r= 0;
322 if (cmd == F_GETFL) {
323 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
324 r= O_NONBLOCK;
325 vb2.used= hm_r_offset+14;
326 } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
327 vb2.used= hm_r_offset+15;
328 } else {
329 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
330 }
331 } else if (cmd == F_SETFL) {
332 hm_rv_succfail
333 } else {
334 Psyntax("fcntl not F_GETFL or F_SETFL");
335 }
336 ')
337 $2
338
339 hm_create_nothing
340 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
341 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
342 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
343 $3
344 if (vb2.used != vb2.avail) Psyntax("junk at end of line");
345
346 hm_create_nothing
347 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
348 $3
349
350 P_updatetime();
351 return r;
352 }
353 ')
354
355 m4_include(`hsyscalls.i4')