New m4-based test harness stuff.
[adns] / regress / hcommon.c.m4
1 m4_dnl hcommon.c
2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - routines used for both record and playback
4
5 m4_dnl This file is part of adns, which is Copyright (C) 1997, 1998 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 <fcntl.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30
31 #include "harness.h"
32 #include "internal.h"
33
34 vbuf vb;
35 FILE *Toutputfile= 0;
36
37 const struct Terrno Terrnos[]= {
38 { "EAGAIN", EAGAIN },
39 { "EINPROGRESS", EINPROGRESS },
40 { "EINTR", EINTR },
41 { "EINVAL", EINVAL },
42 { "EMSGSIZE", EMSGSIZE },
43 { "ENOBUFS", ENOBUFS },
44 { "ENOENT", ENOENT },
45 { "ENOPROTOOPT", ENOPROTOOPT },
46 { "ENOSPC", ENOSPC },
47 { "EWOULDBLOCK", EWOULDBLOCK },
48 { 0, 0 }
49 };
50
51
52
53
54 m4_define(`hm_syscall', `
55 hm_create_proto_q
56 void Q$1(hm_args_massage($3,void)) {
57
58 vb.used= 0;
59 Tvba("$1");
60 m4_define(`hm_na',`')
61 m4_define(`hm_arg_nullptr',`')
62 m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
63 m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
64 m4_define(`hm_arg_timeval_in_rel_null', `
65 if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
66 else Tvba(" $'`1=null");')
67 m4_define(`hm_arg_must', `')
68 m4_define(`hm_arg_socktype', `
69 Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
70 m4_define(`hm_arg_ign', `')
71 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
72 m4_define(`hm_arg_fcntl_cmd_arg', `
73 if ($'`1 == F_SETFL) {
74 Tvbf(" $'`1=F_SETFL %ld",arg);
75 } else {
76 Tvba(" $'`1=F_GETFL");
77 }')
78 m4_define(`hm_arg_addr_in', `Tvbaddr($'`1,$'`2);')
79 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
80 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
81 m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);')
82 $3
83
84 Q_vb();
85 }
86 ')
87
88 m4_include(`hsyscalls.i4')
89
90
91 void Tvbaddr(const struct sockaddr *addr, int len) {
92 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
93
94 assert(len==sizeof(struct sockaddr_in));
95 assert(ai->sin_family==AF_INET);
96 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
97 }
98
99 void Tvbbytes(const void *buf, int len) {
100 const byte *bp;
101 int i;
102
103 if (!len) { Tvba(" empty"); return; }
104 for (i=0, bp=buf; i<len; i++, bp++) {
105 if (!(i&31)) Tvba("\n ");
106 else if (!(i&3)) Tvba(" ");
107 Tvbf("%02x",*bp);
108 }
109 }
110
111 void Tvbfdset(int max, const fd_set *fds) {
112 int i;
113 const char *comma= "";
114
115 Tvba("[");
116 for (i=0; i<max; i++) {
117 if (!FD_ISSET(i,fds)) continue;
118 Tvba(comma);
119 Tvbf("%d",i);
120 comma= ",";
121 }
122 Tvba("]");
123 }
124
125 void Tvberrno(int e) {
126 const struct Terrno *te;
127
128 for (te= Terrnos; te->n && te->v != e; te++);
129 if (te->n) Tvba(te->n);
130 else Tvbf("E#%d",e);
131 }
132
133 void Tvba(const char *str) {
134 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
135 }
136
137 void Tvbvf(const char *fmt, va_list al) {
138 char buf[1000];
139 buf[sizeof(buf)-2]= '\t';
140 vsnprintf(buf,sizeof(buf),fmt,al);
141 assert(buf[sizeof(buf)-2] == '\t');
142
143 Tvba(buf);
144 }
145
146 void Tvbf(const char *fmt, ...) {
147 va_list al;
148 va_start(al,fmt);
149 Tvbvf(fmt,al);
150 va_end(al);
151 }
152
153
154 void Tmust(const char *call, const char *arg, int cond) {
155 if (cond) return;
156 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
157 exit(-1);
158 }
159
160 void Tfailed(const char *why) {
161 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
162 exit(-1);
163 }
164
165 void Tnomem(void) {
166 Tfailed("unable to malloc/realloc");
167 }
168
169 void Toutputerr(void) {
170 Tfailed("write error on test harness output");
171 }
172
173 void Tensureoutputfile(void) {
174 /* fixme: allow sending it elsewhere */
175 Toutputfile= stdout;
176 }