bb7ba677 |
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 | |
d942707d |
5 | m4_dnl This file is |
6 | m4_dnl Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
7 | m4_dnl |
8 | m4_dnl It is part of adns, which is |
9 | m4_dnl Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
10 | m4_dnl Copyright (C) 1999 Tony Finch <dot@dotat.at> |
bb7ba677 |
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 2, 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 | m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | |
26 | m4_include(hmacros.i4) |
a4f094dd |
27 | |
7d251914 |
28 | #include <fcntl.h> |
29 | #include <string.h> |
30 | #include <errno.h> |
31 | |
32 | #include <sys/socket.h> |
33 | #include <netinet/in.h> |
34 | #include <arpa/inet.h> |
35 | |
a4f094dd |
36 | #include "harness.h" |
37 | #include "internal.h" |
38 | |
7d251914 |
39 | vbuf vb; |
40 | FILE *Toutputfile= 0; |
98b6d5b4 |
41 | struct timeval currenttime; |
7d251914 |
42 | |
43 | const struct Terrno Terrnos[]= { |
44 | { "EAGAIN", EAGAIN }, |
45 | { "EINPROGRESS", EINPROGRESS }, |
46 | { "EINTR", EINTR }, |
47 | { "EINVAL", EINVAL }, |
48 | { "EMSGSIZE", EMSGSIZE }, |
49 | { "ENOBUFS", ENOBUFS }, |
50 | { "ENOENT", ENOENT }, |
51 | { "ENOPROTOOPT", ENOPROTOOPT }, |
52 | { "ENOSPC", ENOSPC }, |
53 | { "EWOULDBLOCK", EWOULDBLOCK }, |
54 | { 0, 0 } |
55 | }; |
a4f094dd |
56 | |
70a778e5 |
57 | static vbuf vbw; |
7d251914 |
58 | |
98b6d5b4 |
59 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
60 | Tmust("gettimeofday","tz",!tz); |
61 | *tv= currenttime; |
62 | return 0; |
63 | } |
64 | |
70a778e5 |
65 | int Hwritev(int fd, const struct iovec *vector, size_t count) { |
66 | size_t i; |
67 | |
68 | vbw.used= 0; |
69 | for (i=0; i<count; i++, vector++) { |
70 | if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); |
71 | } |
72 | return Hwrite(fd,vbw.buf,vbw.used); |
73 | } |
7d251914 |
74 | |
bb7ba677 |
75 | m4_define(`hm_syscall', ` |
76 | hm_create_proto_q |
77 | void Q$1(hm_args_massage($3,void)) { |
78 | |
79 | vb.used= 0; |
80 | Tvba("$1"); |
81 | m4_define(`hm_na',`') |
82 | m4_define(`hm_arg_nullptr',`') |
83 | m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);') |
84 | m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);') |
8b1171e6 |
85 | m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);') |
bb7ba677 |
86 | m4_define(`hm_arg_timeval_in_rel_null', ` |
87 | if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec); |
88 | else Tvba(" $'`1=null");') |
89 | m4_define(`hm_arg_must', `') |
90 | m4_define(`hm_arg_socktype', ` |
91 | Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");') |
92 | m4_define(`hm_arg_ign', `') |
93 | m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);') |
94 | m4_define(`hm_arg_fcntl_cmd_arg', ` |
95 | if ($'`1 == F_SETFL) { |
73c4c523 |
96 | Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&..."); |
97 | } else if ($'`1 == F_GETFL) { |
bb7ba677 |
98 | Tvba(" $'`1=F_GETFL"); |
73c4c523 |
99 | } else { |
100 | Tmust("$'`1","F_GETFL/F_SETFL",0); |
bb7ba677 |
101 | }') |
70a778e5 |
102 | m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);') |
103 | m4_define(`hm_arg_bytes_in', `') |
bb7ba677 |
104 | m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);') |
105 | m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);') |
106 | $3 |
7d251914 |
107 | |
70a778e5 |
108 | hm_create_nothing |
109 | m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);') |
110 | $3 |
111 | |
7d251914 |
112 | Q_vb(); |
113 | } |
bb7ba677 |
114 | ') |
7d251914 |
115 | |
bb7ba677 |
116 | m4_include(`hsyscalls.i4') |
7d251914 |
117 | |
118 | |
119 | void Tvbaddr(const struct sockaddr *addr, int len) { |
120 | const struct sockaddr_in *ai= (const struct sockaddr_in*)addr; |
121 | |
122 | assert(len==sizeof(struct sockaddr_in)); |
123 | assert(ai->sin_family==AF_INET); |
bb7ba677 |
124 | Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port)); |
7d251914 |
125 | } |
126 | |
127 | void Tvbbytes(const void *buf, int len) { |
128 | const byte *bp; |
129 | int i; |
130 | |
131 | if (!len) { Tvba(" empty"); return; } |
132 | for (i=0, bp=buf; i<len; i++, bp++) { |
133 | if (!(i&31)) Tvba("\n "); |
134 | else if (!(i&3)) Tvba(" "); |
135 | Tvbf("%02x",*bp); |
136 | } |
70a778e5 |
137 | Tvba("."); |
7d251914 |
138 | } |
139 | |
140 | void Tvbfdset(int max, const fd_set *fds) { |
141 | int i; |
142 | const char *comma= ""; |
143 | |
bb7ba677 |
144 | Tvba("["); |
7d251914 |
145 | for (i=0; i<max; i++) { |
146 | if (!FD_ISSET(i,fds)) continue; |
147 | Tvba(comma); |
148 | Tvbf("%d",i); |
149 | comma= ","; |
150 | } |
151 | Tvba("]"); |
152 | } |
153 | |
8b1171e6 |
154 | static void Tvbpollevents(int events) { |
155 | const char *delim= ""; |
156 | |
157 | events &= (POLLIN|POLLOUT|POLLPRI); |
158 | if (!events) { Tvba("0"); return; } |
159 | if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; } |
160 | if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; } |
161 | if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); } |
162 | } |
163 | |
164 | void Tvbpollfds(const struct pollfd *fds, int nfds) { |
165 | const char *comma= ""; |
166 | |
167 | Tvba("["); |
168 | while (nfds>0) { |
169 | Tvba(comma); |
170 | Tvbf("{fd=%d, events=",fds->fd); |
171 | Tvbpollevents(fds->events); |
172 | Tvba(", revents="); |
173 | Tvbpollevents(fds->revents); |
174 | Tvba("}"); |
175 | comma= ", "; |
176 | nfds--; fds++; |
177 | } |
178 | Tvba("]"); |
179 | } |
180 | |
7d251914 |
181 | void Tvberrno(int e) { |
182 | const struct Terrno *te; |
a4f094dd |
183 | |
79833e62 |
184 | assert(e != EBADF); |
185 | assert(e != EFAULT); |
7d251914 |
186 | for (te= Terrnos; te->n && te->v != e; te++); |
187 | if (te->n) Tvba(te->n); |
188 | else Tvbf("E#%d",e); |
a4f094dd |
189 | } |
190 | |
7d251914 |
191 | void Tvba(const char *str) { |
192 | if (!adns__vbuf_appendstr(&vb,str)) Tnomem(); |
193 | } |
a4f094dd |
194 | |
7d251914 |
195 | void Tvbvf(const char *fmt, va_list al) { |
196 | char buf[1000]; |
197 | buf[sizeof(buf)-2]= '\t'; |
198 | vsnprintf(buf,sizeof(buf),fmt,al); |
199 | assert(buf[sizeof(buf)-2] == '\t'); |
a4f094dd |
200 | |
7d251914 |
201 | Tvba(buf); |
202 | } |
203 | |
204 | void Tvbf(const char *fmt, ...) { |
205 | va_list al; |
206 | va_start(al,fmt); |
207 | Tvbvf(fmt,al); |
208 | va_end(al); |
209 | } |
210 | |
211 | |
bb7ba677 |
212 | void Tmust(const char *call, const char *arg, int cond) { |
213 | if (cond) return; |
214 | fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg); |
215 | exit(-1); |
216 | } |
217 | |
7d251914 |
218 | void Tfailed(const char *why) { |
219 | fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno)); |
220 | exit(-1); |
221 | } |
222 | |
223 | void Tnomem(void) { |
224 | Tfailed("unable to malloc/realloc"); |
225 | } |
226 | |
227 | void Toutputerr(void) { |
228 | Tfailed("write error on test harness output"); |
229 | } |