1 /* 2 * testcode/unitldns.c - unit test for ldns routines. 3 * 4 * Copyright (c) 2014, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 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. 18 * 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. 22 * 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. 34 * 35 */ 36 /** 37 * \file 38 * Calls ldns unit tests. Exits with code 1 on a failure. 39 */ 40 41 #include "config.h" 42 #include "util/log.h" 43 #include "testcode/unitmain.h" 44 #include "sldns/sbuffer.h" 45 #include "sldns/str2wire.h" 46 #include "sldns/wire2str.h" 47 48 /** verbose this unit test */ 49 static int vbmp = 0; 50 51 /** print buffer to hex into string */ 52 static void 53 buf_to_hex(uint8_t* b, size_t blen, char* s, size_t slen) 54 { 55 const char* h = "0123456789ABCDEF"; 56 size_t i; 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]; 62 } 63 s[blen*2] = '\n'; 64 s[blen*2+1] = 0; 65 } 66 67 /** Transform input. 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. 73 */ 74 static void 75 rr_transform(char* txt_in, char* wire1, char* txt_out, char* wire2, 76 size_t bufs) 77 { 78 uint8_t b[65536]; 79 size_t len; 80 int err; 81 82 len = sizeof(b); 83 err = sldns_str2wire_rr_buf(txt_in, b, &len, NULL, 3600, 84 NULL, 0, NULL, 0); 85 if(err != 0) { 86 if(vbmp) printf("sldns_str2wire_rr_buf, pos %d: %s\n", 87 LDNS_WIREPARSE_OFFSET(err), 88 sldns_get_errorstr_parse(err)); 89 } 90 unit_assert(err == 0); 91 buf_to_hex(b, len, wire1, bufs); 92 if(vbmp) printf("wire1: %s", wire1); 93 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); 97 98 len = sizeof(b); 99 err = sldns_str2wire_rr_buf(txt_out, b, &len, NULL, 3600, 100 NULL, 0, NULL, 0); 101 if(err != 0) { 102 if(vbmp) printf("sldns_str2wire_rr_buf-2, pos %d: %s\n", 103 LDNS_WIREPARSE_OFFSET(err), 104 sldns_get_errorstr_parse(err)); 105 } 106 unit_assert(err == 0); 107 buf_to_hex(b, len, wire2, bufs); 108 if(vbmp) printf("wire2: %s", wire2); 109 } 110 111 /** Check if results are correct */ 112 static void 113 rr_checks(char* wire_chk, char* txt_chk, char* txt_out, char* wire_out, 114 char* back) 115 { 116 #ifdef __APPLE__ 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 */ 122 #endif 123 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"); 130 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); 134 } 135 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 140 */ 141 static void 142 rr_test_file(const char* input, const char* check) 143 { 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)); 159 160 of = NULL; 161 if(0) { 162 /* debug: create check file */ 163 of = fopen("outputfile", "w"); 164 if(!of) fatal_exit("cannot write output: %s", strerror(errno)); 165 } 166 167 while(fgets(txt_in, (int)bufs, inf)) { 168 lineno++; 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] == ';') 172 continue; 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); 178 chlineno += 2; 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); 183 /* checks */ 184 if(of) { 185 fprintf(of, "%s%s", wire_out, txt_out); 186 } else { 187 rr_checks(wire_chk, txt_chk, txt_out, wire_out, back); 188 } 189 } 190 191 if(of) fclose(of); 192 fclose(inf); 193 fclose(chf); 194 free(txt_in); 195 free(txt_out); 196 free(txt_chk); 197 free(wire_out); 198 free(wire_chk); 199 free(back); 200 } 201 202 #define xstr(s) str(s) 203 #define str(s) #s 204 205 #define SRCDIRSTR xstr(SRCDIR) 206 207 /** read rrs to and from string, to and from wireformat */ 208 static void 209 rr_tests(void) 210 { 211 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.1", 212 SRCDIRSTR "/testdata/test_ldnsrr.c1"); 213 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.2", 214 SRCDIRSTR "/testdata/test_ldnsrr.c2"); 215 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.3", 216 SRCDIRSTR "/testdata/test_ldnsrr.c3"); 217 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.4", 218 SRCDIRSTR "/testdata/test_ldnsrr.c4"); 219 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.5", 220 SRCDIRSTR "/testdata/test_ldnsrr.c5"); 221 } 222 223 void 224 ldns_test(void) 225 { 226 unit_show_feature("sldns"); 227 rr_tests(); 228 } 229