]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/testcode/unitldns.c
65170a82526fa4d3d46abf463c2c5a88cae28d32
2 * testcode/unitldns.c - unit test for ldns routines.
4 * Copyright (c) 2014, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Calls ldns unit tests. Exits with code 1 on a failure.
43 #include "testcode/unitmain.h"
44 #include "ldns/sbuffer.h"
45 #include "ldns/str2wire.h"
46 #include "ldns/wire2str.h"
48 /** verbose this unit test */
51 /** print buffer to hex into string */
53 buf_to_hex(uint8_t* b
, size_t blen
, char* s
, size_t slen
)
55 const char* h
= "0123456789ABCDEF";
57 if(slen
< blen
*2+2 && vbmp
) printf("hexstring buffer too small\n");
58 unit_assert(slen
>= blen
*2+2);
59 for(i
=0; i
<blen
; i
++) {
60 s
[i
*2] = h
[(b
[i
]&0xf0)>>4];
61 s
[i
*2+1] = h
[b
[i
]&0x0f];
68 * @param txt_in: input text format.
69 * @param wire1: output wireformat in hex (txt_in converted to wire).
70 * @param txt_out: output text format (converted from wire_out).
71 * @param wire2: output wireformat in hex, txt_out converted back to wireformat.
72 * @param bufs: size of the text buffers.
75 rr_transform(char* txt_in
, char* wire1
, char* txt_out
, char* wire2
,
83 err
= sldns_str2wire_rr_buf(txt_in
, b
, &len
, NULL
, 3600,
86 if(vbmp
) printf("sldns_str2wire_rr_buf, pos %d: %s\n",
87 LDNS_WIREPARSE_OFFSET(err
),
88 sldns_get_errorstr_parse(err
));
90 unit_assert(err
== 0);
91 buf_to_hex(b
, len
, wire1
, bufs
);
92 if(vbmp
) printf("wire1: %s", wire1
);
94 err
= sldns_wire2str_rr_buf(b
, len
, txt_out
, bufs
);
95 unit_assert(err
< (int)bufs
&& err
> 0);
96 if(vbmp
) printf("txt: %s", txt_out
);
99 err
= sldns_str2wire_rr_buf(txt_out
, b
, &len
, NULL
, 3600,
102 if(vbmp
) printf("sldns_str2wire_rr_buf-2, pos %d: %s\n",
103 LDNS_WIREPARSE_OFFSET(err
),
104 sldns_get_errorstr_parse(err
));
106 unit_assert(err
== 0);
107 buf_to_hex(b
, len
, wire2
, bufs
);
108 if(vbmp
) printf("wire2: %s", wire2
);
111 /** Check if results are correct */
113 rr_checks(char* wire_chk
, char* txt_chk
, char* txt_out
, char* wire_out
,
117 /* the wiretostr on ipv6 is weird on apple, we cannot check it.
118 * skip AAAA on OSX */
119 if(strstr(txt_out
, "IN AAAA"))
120 txt_out
= txt_chk
; /* skip this test, but test wirefmt */
121 /* so we know that txt_out back to wire is the same */
124 if(strcmp(txt_chk
, txt_out
) != 0 && vbmp
)
125 printf("txt different\n");
126 if(strcmp(wire_chk
, wire_out
) != 0 && vbmp
)
127 printf("wire1 different\n");
128 if(strcmp(wire_chk
, back
) != 0 && vbmp
)
129 printf("wire2 different\n");
131 unit_assert(strcmp(txt_chk
, txt_out
) == 0);
132 unit_assert(strcmp(wire_chk
, wire_out
) == 0);
133 unit_assert(strcmp(wire_chk
, back
) == 0);
136 /** read rrs to and from string, and wireformat
137 * Skips empty lines and comments.
138 * @param input: input file with text format.
139 * @param check: check file with hex and then textformat
142 rr_test_file(const char* input
, const char* check
)
144 size_t bufs
= 131072;
145 FILE* inf
, *chf
, *of
;
146 int lineno
= 0, chlineno
= 0;
147 char* txt_in
= (char*)malloc(bufs
);
148 char* txt_out
= (char*)malloc(bufs
);
149 char* txt_chk
= (char*)malloc(bufs
);
150 char* wire_out
= (char*)malloc(bufs
);
151 char* wire_chk
= (char*)malloc(bufs
);
152 char* back
= (char*)malloc(bufs
);
153 if(!txt_in
|| !txt_out
|| !txt_chk
|| !wire_out
|| !wire_chk
|| !back
)
154 fatal_exit("malloc failure");
155 inf
= fopen(input
, "r");
156 if(!inf
) fatal_exit("cannot open %s: %s", input
, strerror(errno
));
157 chf
= fopen(check
, "r");
158 if(!chf
) fatal_exit("cannot open %s: %s", check
, strerror(errno
));
162 /* debug: create check file */
163 of
= fopen("outputfile", "w");
164 if(!of
) fatal_exit("cannot write output: %s", strerror(errno
));
167 while(fgets(txt_in
, (int)bufs
, inf
)) {
169 if(vbmp
) printf("\n%s:%d %s", input
, lineno
, txt_in
);
170 /* skip empty lines and comments */
171 if(txt_in
[0] == 0 || txt_in
[0] == '\n' || txt_in
[0] == ';')
173 /* read check lines */
174 if(!fgets(wire_chk
, (int)bufs
, chf
))
175 printf("%s too short\n", check
);
176 if(!fgets(txt_chk
, (int)bufs
, chf
))
177 printf("%s too short\n", check
);
179 if(vbmp
) printf("%s:%d %s", check
, chlineno
-1, wire_chk
);
180 if(vbmp
) printf("%s:%d %s", check
, chlineno
, txt_chk
);
181 /* generate results */
182 rr_transform(txt_in
, wire_out
, txt_out
, back
, bufs
);
185 fprintf(of
, "%s%s", wire_out
, txt_out
);
187 rr_checks(wire_chk
, txt_chk
, txt_out
, wire_out
, back
);
202 /** read rrs to and from string, to and from wireformat */
206 rr_test_file("testdata/test_ldnsrr.1", "testdata/test_ldnsrr.c1");
207 rr_test_file("testdata/test_ldnsrr.2", "testdata/test_ldnsrr.c2");
208 rr_test_file("testdata/test_ldnsrr.3", "testdata/test_ldnsrr.c3");
209 rr_test_file("testdata/test_ldnsrr.4", "testdata/test_ldnsrr.c4");
210 rr_test_file("testdata/test_ldnsrr.5", "testdata/test_ldnsrr.c5");
216 unit_show_feature("sldns");