9ba9f156 |
1 | m4_dnl hplayback.c.m4 |
2 | m4_dnl (part of complex test harness, not of the library) |
3 | m4_dnl - playback routines |
4 | |
3ff64957 |
5 | m4_dnl This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
9ba9f156 |
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 | |
e3324da1 |
35 | static FILE *Tinputfile, *Treportfile; |
9ba9f156 |
36 | static vbuf vb2; |
37 | |
e3324da1 |
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 | |
9ba9f156 |
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 | |
3c5796f7 |
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 | |
9ba9f156 |
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 Perrno(const char *stuff) { |
93 | const struct Terrno *te; |
94 | int r; |
95 | char *ep; |
96 | |
97 | for (te= Terrnos; te->n && strcmp(te->n,stuff); te++); |
98 | if (te->n) return te->v; |
99 | r= strtoul(stuff+1,&ep,10); |
100 | if (ep) Psyntax("errno value not recognised, not numeric"); |
101 | return r; |
102 | } |
103 | |
3c5796f7 |
104 | static void P_updatetime(void) { |
105 | int chars; |
9ba9f156 |
106 | unsigned long sec, usec; |
3c5796f7 |
107 | |
108 | if (!adns__vbuf_ensure(&vb2,1000)) Tnomem(); |
109 | fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput(); |
9ba9f156 |
110 | chars= -1; |
3c5796f7 |
111 | sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars); |
112 | if (chars==-1) Psyntax("update time invalid"); |
113 | currenttime.tv_sec+= sec; |
114 | currenttime.tv_usec+= usec; |
115 | if (currenttime.tv_usec > 1000000) { |
116 | currenttime.tv_sec++; |
117 | currenttime.tv_usec -= 1000000; |
9ba9f156 |
118 | } |
3c5796f7 |
119 | if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time"); |
9ba9f156 |
120 | } |
121 | |
122 | static void Pfdset(fd_set *set, int max) { |
123 | int r, c; |
124 | char *ep; |
125 | |
126 | if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not ["); |
127 | FD_ZERO(set); |
128 | for (;;) { |
129 | r= strtoul(vb2.buf+vb2.used,&ep,10); |
130 | if (r>=max) Psyntax("fd set member > max"); |
131 | FD_SET(r,set); |
132 | vb2.used= ep - (char*)vb2.buf; |
133 | c= vb2.buf[vb2.used++]; |
134 | if (c == hm_squote]hm_squote) break; |
135 | if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,"); |
136 | } |
137 | } |
138 | |
139 | static void Paddr(struct sockaddr *addr, int *lenr) { |
140 | struct sockaddr_in *sa= (struct sockaddr_in*)addr; |
141 | char *p, *ep; |
142 | long ul; |
143 | |
144 | assert(*lenr >= sizeof(*sa)); |
145 | p= strchr(vb2.buf+vb2.used,':'); |
146 | if (!p) Psyntax("no port on address"); |
147 | *p++= 0; |
148 | memset(sa,0,sizeof(*sa)); |
149 | sa->sin_family= AF_INET; |
150 | if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address"); |
151 | ul= strtoul(p,&ep,10); |
152 | if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)"); |
153 | if (ul >= 65536) Psyntax("port too large"); |
154 | sa->sin_port= htons(ul); |
155 | *lenr= sizeof(*sa); |
156 | |
157 | vb2.used= ep - (char*)vb2.buf; |
158 | } |
159 | |
160 | static int Pbytes(byte *buf, int maxlen) { |
161 | static const char hexdigits[]= "0123456789abcdef"; |
162 | |
163 | int c, v, done; |
164 | const char *pf; |
165 | |
166 | done= 0; |
167 | for (;;) { |
168 | c= getc(Tinputfile); Pcheckinput(); |
169 | if (c=='\n' || c==' ' || c=='\t') continue; |
170 | if (c=='.') break; |
171 | pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit"); |
172 | v= (pf-hexdigits)<<4; |
173 | c= getc(Tinputfile); Pcheckinput(); |
174 | pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit"); |
175 | v |= (pf-hexdigits); |
176 | if (maxlen<=0) Psyntax("buffer overflow in bytes"); |
177 | *buf++= v; |
178 | maxlen--; done++; |
179 | } |
180 | for (;;) { |
181 | c= getc(Tinputfile); Pcheckinput(); |
182 | if (c=='\n') return done; |
183 | } |
184 | } |
185 | |
186 | void Q_vb(void) { |
187 | int r; |
188 | |
189 | Tensureinputfile(); |
190 | if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem(); |
191 | r= fread(vb2.buf,1,vb.used+2,Tinputfile); |
192 | if (feof(Tinputfile)) { |
193 | fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n", |
194 | vb.used,vb.buf); |
195 | exit(-1); |
196 | } |
197 | Pcheckinput(); |
198 | if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call"); |
199 | if (memcmp(vb.buf,vb2.buf+1,vb.used) || |
200 | vb2.buf[vb.used+1] != hm_squote\nhm_squote) { |
201 | fprintf(stderr, |
202 | "adns test harness: program did unexpected:\n %.*s\n" |
203 | "was expecting:\n %.*s\n", |
204 | vb.used,vb.buf, vb.used,vb2.buf+1); |
205 | exit(1); |
206 | } |
207 | } |
208 | |
209 | m4_define(`hm_syscall', ` |
210 | hm_create_proto_h |
211 | int H$1(hm_args_massage($3,void)) { |
212 | int r; |
213 | m4_define(`hm_rv_fd',`char *ep;') |
214 | m4_define(`hm_rv_any',`char *ep;') |
215 | m4_define(`hm_rv_len',`') |
216 | m4_define(`hm_rv_must',`') |
217 | m4_define(`hm_rv_succfail',`') |
09a06b84 |
218 | m4_define(`hm_rv_fcntl',`') |
9ba9f156 |
219 | $2 |
220 | |
221 | hm_create_hqcall_vars |
222 | $3 |
223 | |
224 | hm_create_hqcall_init($1) |
225 | $3 |
226 | |
227 | hm_create_hqcall_args |
228 | Q$1(hm_args_massage($3)); |
229 | |
230 | m4_define(`hm_r_offset',`m4_len(` $1=')') |
231 | if (!adns__vbuf_ensure(&vb2,1000)) Tnomem(); |
232 | fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput(); |
e3324da1 |
233 | |
234 | Tensurereportfile(); |
235 | fprintf(Treportfile,"syscallr %s",vb2.buf); |
9ba9f156 |
236 | vb2.avail= strlen(vb2.buf); |
237 | if (vb.avail<=0 || vb2.buf[--vb2.avail]!=hm_squote\nhm_squote) |
238 | Psyntax("badly formed line"); |
239 | vb2.buf[vb2.avail]= 0; |
240 | if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch"); |
241 | |
242 | if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) { |
3c5796f7 |
243 | int e; |
244 | e= Perrno(vb2.buf+hm_r_offset); |
245 | P_updatetime(); |
246 | errno= e; |
9ba9f156 |
247 | return -1; |
248 | } |
249 | |
250 | m4_define(`hm_rv_succfail',` |
251 | if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK"); |
252 | vb2.used= hm_r_offset+2; |
253 | r= 0; |
254 | ') |
255 | m4_define(`hm_rv_len',`hm_rv_succfail') |
256 | m4_define(`hm_rv_must',`hm_rv_succfail') |
257 | m4_define(`hm_rv_any',` |
258 | r= strtoul(vb2.buf+hm_r_offset,&ep,10); |
259 | if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number"); |
260 | vb2.used= ep - (char*)vb2.buf; |
261 | ') |
262 | m4_define(`hm_rv_fd',`hm_rv_any') |
09a06b84 |
263 | m4_define(`hm_rv_fcntl',` |
264 | r= 0; |
265 | if (cmd == F_GETFL) { |
266 | if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) { |
267 | r= O_NONBLOCK; |
268 | vb2.used= hm_r_offset+14; |
269 | } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) { |
270 | vb2.used= hm_r_offset+15; |
271 | } else { |
272 | Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&..."); |
273 | } |
274 | } else if (cmd == F_SETFL) { |
275 | hm_rv_succfail |
276 | } else { |
277 | Psyntax("fcntl not F_GETFL or F_SETFL"); |
278 | } |
279 | ') |
9ba9f156 |
280 | $2 |
281 | |
282 | hm_create_nothing |
283 | m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);') |
284 | m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);') |
9ba9f156 |
285 | $3 |
286 | if (vb2.used != vb2.avail) Psyntax("junk at end of line"); |
287 | |
288 | hm_create_nothing |
289 | m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);') |
290 | $3 |
291 | |
3c5796f7 |
292 | P_updatetime(); |
9ba9f156 |
293 | return r; |
294 | } |
295 | ') |
296 | |
297 | m4_include(`hsyscalls.i4') |