chiark / gitweb /
regress: hplayback: Do not tolerate hm_rv_any values out of range
[adns.git] / 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
6 m4_dnl    Copyright (C) 1997-2000,2003,2006,2014-2016,2020  Ian Jackson
7 m4_dnl    Copyright (C) 2014  Mark Wooding
8 m4_dnl    Copyright (C) 1999-2000,2003,2006  Tony Finch
9 m4_dnl    Copyright (C) 1991 Massachusetts Institute of Technology
10 m4_dnl  (See the file INSTALL for full details.)
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 3, 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
25 m4_include(hmacros.i4)
26
27 #include <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdlib.h>
31
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/time.h>
37
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <limits.h>
41
42
43 #include "harness.h"
44
45 static FILE *Tinputfile, *Treportfile;
46 static vbuf vb2;
47
48 extern void Tshutdown(void) {
49   adns__vbuf_free(&vb2);
50 }
51
52 static void Tensurereportfile(void) {
53   const char *fdstr;
54   int fd;
55
56   if (Treportfile) return;
57   Treportfile= stderr;
58   fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
59   fd= atoi(fdstr);
60   Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
61 }
62
63 static void Psyntax(const char *where) {
64   fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
65   exit(-1);
66 }
67
68 static void Pcheckinput(void) {
69   if (ferror(Tinputfile)) Tfailed("read test log input file");
70   if (feof(Tinputfile)) Psyntax("eof at syscall reply");
71 }
72
73 void Tensurerecordfile(void) {
74   int fd;
75   int chars;
76   unsigned long sec, usec;
77
78   if (Tinputfile) return;
79   Tinputfile= stdin;
80   fd = Ttestinputfd();
81   if (fd >= 0) {
82     Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
83   }
84   setvbuf(Tinputfile,0,_IONBF,0);
85
86   if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
87   fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
88   chars= -1;
89   sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
90   if (chars==-1) Psyntax("start time invalid");
91   currenttime.tv_sec= sec;
92   currenttime.tv_usec= usec;
93   if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time");
94 }
95
96 static void Parg(const char *argname) {
97   int l;
98
99   if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
100   l= strlen(argname);
101   if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
102   vb2.used+= l;
103   if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
104 }
105
106 static int Pstring_maybe(const char *string) {
107   int l;
108
109   l= strlen(string);
110   if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
111   vb2.used+= l;
112   return 1;
113 }
114
115 static void Pstring(const char *string, const char *emsg) {
116   if (Pstring_maybe(string)) return;
117   Psyntax(emsg);
118 }
119
120 static int Perrno(const char *stuff) {
121   const struct Terrno *te;
122   int r;
123   char *ep;
124
125   for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
126   if (te->n) return te->v;
127   r= strtoul(stuff+2,&ep,10);
128   if (*ep) Psyntax("errno value not recognised, not numeric");
129   if (r==0 || r>255) Psyntax("numeric errno out of range 1..255");
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 c;
153   unsigned long ul;
154   char *ep;
155
156   if (!set) {
157     Pstring("null","null fdset pointer");
158     return;
159   }
160   
161   if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
162   FD_ZERO(set);
163   if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; }
164   for (;;) {
165     ul= strtoul(vb2.buf+vb2.used,&ep,10);
166     if (ul>=max) Psyntax("fd set member > max");
167     if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
168     FD_SET(ul,set);
169     vb2.used= ep - (char*)vb2.buf;
170     c= vb2.buf[vb2.used++];
171     if (c == hm_squote]hm_squote) break;
172     if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
173   }
174 }
175
176 #ifdef HAVE_POLL
177 static int Ppollfdevents(void) {
178   int events;
179
180   if (Pstring_maybe("0")) return 0;
181   events= 0;
182
183   if (Pstring_maybe("POLLIN")) {
184     events |= POLLIN;
185     if (!Pstring_maybe("|")) return events;
186   }
187
188   if (Pstring_maybe("POLLOUT")) {
189     events |= POLLOUT;
190     if (!Pstring_maybe("|")) return events;
191   }
192
193   Pstring("POLLPRI","pollfdevents PRI?");
194   return events;
195 }
196
197 static void Ppollfds(struct pollfd *fds, int nfds) {
198   int i;
199   char *ep;
200   const char *comma= "";
201   
202   if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
203   for (i=0; i<nfds; i++) {
204     Pstring("{fd=","{fd= in pollfds");
205     fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
206     vb2.used= ep - (char*)vb2.buf;    
207     Pstring(", events=",", events= in pollfds");
208     fds->events= Ppollfdevents();
209     Pstring(", revents=",", revents= in pollfds");
210     fds->revents= Ppollfdevents();
211     Pstring("}","} in pollfds");
212     Pstring(comma,"separator in pollfds");
213     comma= ", ";
214   }
215   if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
216 }
217 #endif
218
219 static void Paddr(struct sockaddr *addr, int *lenr) {
220   adns_rr_addr a;
221   char *p, *q, *ep;
222   int err;
223   unsigned long ul;
224
225   p= vb2.buf+vb2.used;
226   if (*p!='[') {
227     q= strchr(p,':');
228     if (!q) Psyntax("missing :");
229     *q++= 0;
230   } else {
231     p++;
232     q= strchr(p,']');
233     if (!q) Psyntax("missing ]");
234     *q++= 0;
235     if (*q!=':') Psyntax("expected : after ]");
236     q++;
237   }
238   ul= strtoul(q,&ep,10);
239   if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
240   if (ul >= 65536) Psyntax("port too large");
241
242   a.len= sizeof(a.addr);
243   err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
244   if (err) Psyntax("invalid address");
245
246   assert(*lenr >= a.len);
247   memcpy(addr, &a.addr, a.len);
248   *lenr= a.len;
249   vb2.used= ep - (char*)vb2.buf;
250 }
251
252 static int Pbytes(byte *buf, int maxlen) {
253   static const char hexdigits[]= "0123456789abcdef";
254
255   int c, v, done;
256   const char *pf;
257
258   done= 0;
259   for (;;) {
260     c= getc(Tinputfile); Pcheckinput();
261     if (c=='\n' || c==' ' || c=='\t') continue;
262     if (c=='.') break;
263     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
264     v= (pf-hexdigits)<<4;
265     c= getc(Tinputfile); Pcheckinput();
266     pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
267     v |= (pf-hexdigits);
268     if (maxlen<=0) Psyntax("buffer overflow in bytes");
269     *buf++= v;
270     maxlen--; done++;
271   }
272   for (;;) {
273     c= getc(Tinputfile); Pcheckinput();
274     if (c=='\n') return done;
275   }
276 }
277   
278 void Q_vb(void) {
279   const char *nl;
280
281   Tensurerecordfile();
282   if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
283   fread(vb2.buf,1,vb.used+2,Tinputfile);
284   if (feof(Tinputfile)) {
285     fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
286            vb.used,vb.buf);
287     exit(-1);
288   }
289   Pcheckinput();
290   if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
291   if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
292       vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
293     fprintf(stderr,
294             "adns test harness: program did unexpected:\n %.*s\n"
295             "was expecting:\n %.*s\n",
296             vb.used,vb.buf, vb.used,vb2.buf+1);
297     exit(1);
298   }
299   Tensurereportfile();
300   nl= memchr(vb.buf,'\n',vb.used);
301   fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
302 }
303
304 m4_define(`hm_syscall', `
305  hm_create_proto_h
306 int H$1(hm_args_massage($3,void)) {
307  int r, amtread;
308  m4_define(`hm_rv_fd',`char *ep;')
309  m4_define(`hm_rv_any',`char *ep;')
310  m4_define(`hm_rv_len',`')
311  m4_define(`hm_rv_must',`')
312  m4_define(`hm_rv_succfail',`')
313  m4_define(`hm_rv_fcntl',`')
314  $2
315
316  hm_create_hqcall_vars
317  $3
318
319  hm_create_hqcall_init($1)
320  $3
321
322  hm_create_hqcall_args
323  Q$1(hm_args_massage($3));
324
325  m4_define(`hm_r_offset',`m4_len(` $1=')')
326  if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
327  fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
328
329  Tensurereportfile();
330  fprintf(Treportfile,"%s",vb2.buf);
331  amtread= strlen(vb2.buf);
332  if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
333   Psyntax("badly formed line");
334  vb2.buf[amtread]= 0;
335  if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
336
337  if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
338   int e;
339   e= Perrno(vb2.buf+hm_r_offset);
340   P_updatetime();
341   errno= e;
342   return -1;
343  }
344
345  m4_define(`hm_rv_succfail',`
346   if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
347   vb2.used= hm_r_offset+2;
348   r= 0;
349  ')
350  m4_define(`hm_rv_len',`hm_rv_succfail')
351  m4_define(`hm_rv_must',`hm_rv_succfail')
352  m4_define(`hm_rv_any',`
353   unsigned long ul_r= strtoul(vb2.buf+hm_r_offset,&ep,10);
354   if (ul_r < 0 || ul_r > INT_MAX ||
355       (*ep && *ep!=hm_squote hm_squote))
356     Psyntax("return value not E* or positive number");
357   r= ul_r;
358   vb2.used= ep - (char*)vb2.buf;
359  ')
360  m4_define(`hm_rv_fd',`hm_rv_any')
361  m4_define(`hm_rv_fcntl',`
362   r= 0;
363   if (cmd == F_GETFL) {
364     if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
365       r= O_NONBLOCK;
366       vb2.used= hm_r_offset+14;
367     } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
368       vb2.used= hm_r_offset+15;
369     } else {
370       Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
371     }
372   } else if (cmd == F_SETFL) {
373     hm_rv_succfail
374   } else {
375     Psyntax("fcntl not F_GETFL or F_SETFL");
376   }
377  ')
378  $2
379
380  hm_create_nothing
381  m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
382  m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
383  m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
384  $3
385  assert(vb2.used <= amtread);
386  if (vb2.used != amtread) Psyntax("junk at end of line");
387
388  hm_create_nothing
389  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
390  $3
391
392  P_updatetime();
393  return r;
394 }
395 ')
396
397 m4_define(`hm_specsyscall', `')
398
399 m4_include(`hsyscalls.i4')