regression tests: Properly handle adnshost et al exit status
[adns] / client / adnstest.c
CommitLineData
e576be50 1/*
b0f83da6 2 * adnstest.c
e576be50 3 * - simple test program, not part of the library
4 */
5/*
ae8cc977 6 * This file is part of adns, which is
26e1c3d6 7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
17cd4f48 8 * Copyright (C) 2014 Mark Wooding
ae8cc977 9 * Copyright (C) 1999-2000,2003,2006 Tony Finch
10 * Copyright (C) 1991 Massachusetts Institute of Technology
11 * (See the file INSTALL for full details.)
e576be50 12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
7f8bbe29 15 * the Free Software Foundation; either version 3, or (at your option)
e576be50 16 * any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
8c09a4c6 24 * along with this program; if not, write to the Free Software Foundation.
e576be50 25 */
964344fc 26
27#include <stdio.h>
73dba56e 28#include <sys/time.h>
350d37d9 29#include <unistd.h>
86e7b8d9 30#include <assert.h>
31#include <stdlib.h>
9da4a044 32#include <string.h>
fc6a52ae 33#include <errno.h>
16cf5367 34
138722f0 35#define ADNS_FEATURE_MANYAF
16cf5367 36#include "config.h"
5a0be244 37#include "adns.h"
964344fc 38
0ebff22d 39#ifdef ADNS_REGRESS_TEST
40# include "hredirect.h"
e7308d26
IJ
41# undef exit
42# define exit Texit
7d251914 43#endif
44
914a5ff5 45struct myctx {
46 adns_query qu;
47 int doneyet, found;
48 const char *fdom;
49};
50
51static struct myctx *mcs;
52static adns_state ads;
53static adns_rrtype *types_a;
54
55static void quitnow(int rc) NONRETURNING;
56static void quitnow(int rc) {
57 free(mcs);
58 free(types_a);
59 if (ads) adns_finish(ads);
60
61 exit(rc);
62}
63
16cf5367 64#ifndef HAVE_POLL
65#undef poll
66int poll(struct pollfd *ufds, int nfds, int timeout) {
67 fputs("poll(2) not supported on this system\n",stderr);
914a5ff5 68 quitnow(5);
16cf5367 69}
70#define adns_beforepoll(a,b,c,d,e) 0
71#define adns_afterpoll(a,b,c,d) 0
72#endif
964344fc 73
2345cc9b 74static void failure_status(const char *what, adns_status st) NONRETURNING;
9da4a044 75static void failure_status(const char *what, adns_status st) {
86e7b8d9 76 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
914a5ff5 77 quitnow(2);
86e7b8d9 78}
79
2345cc9b 80static void failure_errno(const char *what, int errnoval) NONRETURNING;
9da4a044 81static void failure_errno(const char *what, int errnoval) {
fa1b90e5 82 switch (errnoval) {
83#define CE(e) \
84 case e: fprintf(stderr,"adns failure: %s: errno=" #e "\n",what); break
85 CE(EINVAL);
86 CE(EINTR);
87 CE(ESRCH);
88 CE(EAGAIN);
89 CE(ENOSYS);
90 CE(ERANGE);
91#undef CE
92 default: fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); break;
93 }
914a5ff5 94 quitnow(2);
9da4a044 95}
96
2345cc9b 97static void usageerr(const char *why) NONRETURNING;
98static void usageerr(const char *why) {
99 fprintf(stderr,
100 "bad usage: %s\n"
101 "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
102 " [ :<typenum>,... ]\n"
103 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
104 "initflags: p use poll(2) instead of select(2)\n"
105 " s use adns_wait with specified query, instead of 0\n"
106 "queryflags: a print status abbrevs instead of strings\n"
717d4481 107 "typenum: may be 0x<hex>|<dec>, or 0x<hex> or <dec>\n"
2345cc9b 108 "exit status: 0 ok (though some queries may have failed)\n"
109 " 1 used by test harness to indicate test failed\n"
110 " 2 unable to submit or init or some such\n"
111 " 3 unexpected failure\n"
112 " 4 usage error\n"
113 " 5 operation not supported on this system\n",
114 why);
914a5ff5 115 quitnow(4);
2345cc9b 116}
86e7b8d9 117
ffbda80c 118static const adns_rrtype defaulttypes[]= {
119 adns_r_a,
120 adns_r_ns_raw,
121 adns_r_cname,
9ec44266 122 adns_r_soa_raw,
ffbda80c 123 adns_r_ptr_raw,
9ec44266 124 adns_r_hinfo,
e062dcae 125 adns_r_mx_raw,
1b644113 126 adns_r_txt,
1dfe95d8 127 adns_r_rp_raw,
9ec44266 128
828d89bd 129 adns_r_addr,
1dfe95d8 130 adns_r_ns,
a6536d8b 131 adns_r_ptr,
9ec44266 132 adns_r_mx,
133
134 adns_r_soa,
135 adns_r_rp,
136
ffbda80c 137 adns_r_none
138};
139
e062dcae 140static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
141 fprintf(stdout, "%s(%s)%s%s",
db540ace 142 (!ri && rrtn) ? rrtn : "?", ri ? "?" : fmtn ? fmtn : "-",
e062dcae 143 ri ? " " : "", ri ? adns_strerror(ri) : "");
144}
145
9da4a044 146static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
147 char *ownflags, int ownflags_l) {
5476fe64 148 int qf;
149 char *ep;
150
151 qf= strtoul(fdom,&ep,0);
9da4a044 152 if (*ep == ',' && strchr(ep,'/')) {
153 ep++;
154 while (*ep != '/') {
914a5ff5 155 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
9da4a044 156 *ownflags++= *ep++;
157 }
158 }
5476fe64 159 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
160 else { *dom_r= ep+1; *qf_r= qf; }
9da4a044 161 *ownflags= 0;
5476fe64 162}
163
fc6a52ae 164static int consistsof(const char *string, const char *accept) {
165 return strspn(string,accept) == strlen(string);
166}
167
e062dcae 168int main(int argc, char *const *argv) {
669f9df5 169 adns_query qu;
914a5ff5 170 struct myctx *mc, *mcw;
669f9df5 171 void *mcr;
4353a5c4 172 adns_answer *ans;
12c5b204 173 const char *initstring, *rrtn, *fmtn;
5476fe64 174 const char *const *fdomlist, *domain;
e062dcae 175 char *show, *cp;
6d682204 176 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
636b69b1 177 adns_status ri;
178 int r;
ffbda80c 179 const adns_rrtype *types;
73dba56e 180 struct timeval now;
9da4a044 181 char ownflags[10];
fc6a52ae 182 char *ep;
183 const char *initflags, *owninitflags;
86e7b8d9 184
fc6a52ae 185 if (argv[0] && argv[1] && argv[1][0] == '-') {
186 initflags= argv[1]+1;
187 argv++;
188 } else {
189 initflags= "";
190 }
12c5b204 191 if (argv[0] && argv[1] && argv[1][0] == '/') {
192 initstring= argv[1]+1;
193 argv++;
194 } else {
195 initstring= 0;
196 }
fc6a52ae 197
198 initflagsnum= strtoul(initflags,&ep,0);
199 if (*ep == ',') {
200 owninitflags= ep+1;
2345cc9b 201 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
fc6a52ae 202 } else if (!*ep) {
203 owninitflags= "";
204 } else {
2345cc9b 205 usageerr("bad <initflagsnum>[,<owninitflags>]");
fc6a52ae 206 }
12c5b204 207
e062dcae 208 if (argv[0] && argv[1] && argv[1][0] == ':') {
209 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
210 if (ch==',') tc++;
e3d42241 211 types_a= malloc(sizeof(*types_a)*(tc+1));
914a5ff5 212 if (!types_a) { perror("malloc types"); quitnow(3); }
717d4481
IJ
213 for (cp= argv[1]+1, ti=0; ti<tc; ) {
214 types_a[ti]= 0;
215 for (;;) {
216 types_a[ti] |= strtoul(cp,&cp,0);
217 ch= *cp;
218 if (!ch) break;
e062dcae 219 cp++;
717d4481
IJ
220 if (ch=='|') continue;
221 if (ch==',') break;
222 usageerr("unexpected char (not comma) in or between types");
e062dcae 223 }
717d4481
IJ
224 ti++;
225 if (!ch) break;
e062dcae 226 }
914a5ff5 227 types_a[ti]= adns_r_none;
e062dcae 228 types= types_a;
229 argv++;
230 } else {
914a5ff5 231 types_a= 0;
e062dcae 232 types= defaulttypes;
233 }
234
2345cc9b 235 if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
236 fdomlist= (const char *const*)argv+1;
ffbda80c 237
5476fe64 238 for (qc=0; fdomlist[qc]; qc++);
ffbda80c 239 for (tc=0; types[tc] != adns_r_none; tc++);
914a5ff5 240 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
241 if (!mcs) { perror("malloc mcs"); quitnow(3); }
964344fc 242
5a0be244 243 setvbuf(stdout,0,_IOLBF,0);
244
12c5b204 245 if (initstring) {
fc6a52ae 246 r= adns_init_strcfg(&ads,
3e2e5fab 247 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
248 ^initflagsnum,
fc6a52ae 249 stdout,initstring);
12c5b204 250 } else {
fc6a52ae 251 r= adns_init(&ads,
252 (adns_if_debug|adns_if_noautosys)^initflagsnum,
253 0);
12c5b204 254 }
9da4a044 255 if (r) failure_errno("init",r);
86e7b8d9 256
257 for (qi=0; qi<qc; qi++) {
9da4a044 258 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
2345cc9b 259 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
ffbda80c 260 for (ti=0; ti<tc; ti++) {
669f9df5 261 mc= &mcs[qi*tc+ti];
262 mc->doneyet= 0;
e42ce2b2 263 mc->fdom= fdomlist[qi];
669f9df5 264
d24b603f
MW
265 fprintf(stdout,"%s flags %d type %d",
266 domain,qflags,types[ti]&adns_rrt_reprmask);
669f9df5 267 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
636b69b1 268 if (r == ENOSYS) {
916c16ca 269 fprintf(stdout," not implemented\n");
669f9df5 270 mc->qu= 0;
271 mc->doneyet= 1;
916c16ca 272 } else if (r) {
9da4a044 273 failure_errno("submit",r);
916c16ca 274 } else {
e062dcae 275 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
7d251914 276 putc(' ',stdout);
e062dcae 277 dumptype(ri,rrtn,fmtn);
916c16ca 278 fprintf(stdout," submitted\n");
279 }
ffbda80c 280 }
86e7b8d9 281 }
350d37d9 282
e42ce2b2 283 for (;;) {
284 for (qi=0; qi<qc; qi++) {
285 for (ti=0; ti<tc; ti++) {
286 mc= &mcs[qi*tc+ti];
287 mc->found= 0;
288 }
289 }
290 for (adns_forallqueries_begin(ads);
291 (qu= adns_forallqueries_next(ads,&mcr));
292 ) {
293 mc= mcr;
294 assert(qu == mc->qu);
295 assert(!mc->doneyet);
296 mc->found= 1;
297 }
298 mcw= 0;
299 for (qi=0; qi<qc; qi++) {
300 for (ti=0; ti<tc; ti++) {
301 mc= &mcs[qi*tc+ti];
302 if (mc->doneyet) continue;
303 assert(mc->found);
304 if (!mcw) mcw= mc;
305 }
306 }
307 if (!mcw) break;
669f9df5 308
8ce38e76 309 if (strchr(owninitflags,'s')) {
310 qu= mcw->qu;
311 mc= mcw;
312 } else {
313 qu= 0;
314 mc= 0;
315 }
fc6a52ae 316
207130d4 317 if (strchr(owninitflags,'p')) {
940356bd 318 r= adns_wait_poll(ads,&qu,&ans,&mcr);
207130d4 319 } else {
320 r= adns_wait(ads,&qu,&ans,&mcr);
321 }
322 if (r) failure_errno("wait/check",r);
323
8ce38e76 324 if (mc) assert(mcr==mc);
325 else mc= mcr;
326 assert(qu==mc->qu);
327 assert(!mc->doneyet);
328
207130d4 329 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
669f9df5 330
914a5ff5 331 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
73dba56e 332
207130d4 333 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
334 fprintf(stdout, "%s flags %d type ",domain,qflags);
335 dumptype(ri,rrtn,fmtn);
336 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
337 ownflags[0] ? " ownflags=" : "", ownflags,
338 strchr(ownflags,'a')
339 ? adns_errabbrev(ans->status)
340 : adns_strerror(ans->status),
341 ans->nrrs,
342 ans->cname ? ans->cname : "$",
343 ans->owner ? ans->owner : "$",
344 (long)ans->expires - (long)now.tv_sec);
345 if (ans->nrrs) {
346 assert(!ri);
347 for (i=0; i<ans->nrrs; i++) {
636b69b1 348 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
349 if (ri) failure_status("info",ri);
207130d4 350 fprintf(stdout," %s\n",show);
351 free(show);
86e7b8d9 352 }
207130d4 353 }
354 free(ans);
e42ce2b2 355
207130d4 356 mc->doneyet= 1;
86e7b8d9 357 }
4353a5c4 358
914a5ff5 359 quitnow(0);
964344fc 360}