#include "config.h"
#include "util/log.h"
#include "testcode/unitmain.h"
#include "ldns/sbuffer.h"
#include "ldns/str2wire.h"
#include "ldns/wire2str.h"
static int vbmp = 0;
static void
buf_to_hex(uint8_t* b, size_t blen, char* s, size_t slen)
{
const char* h = "0123456789ABCDEF";
size_t i;
if(slen < blen*2+2 && vbmp) printf("hexstring buffer too small\n");
unit_assert(slen >= blen*2+2);
for(i=0; i<blen; i++) {
s[i*2] = h[(b[i]&0xf0)>>4];
s[i*2+1] = h[b[i]&0x0f];
}
s[blen*2] = '\n';
s[blen*2+1] = 0;
}
static void
rr_transform(char* txt_in, char* wire1, char* txt_out, char* wire2,
size_t bufs)
{
uint8_t b[65536];
size_t len;
int err;
len = sizeof(b);
err = sldns_str2wire_rr_buf(txt_in, b, &len, NULL, 3600,
NULL, 0, NULL, 0);
if(err != 0) {
if(vbmp) printf("sldns_str2wire_rr_buf, pos %d: %s\n",
LDNS_WIREPARSE_OFFSET(err),
sldns_get_errorstr_parse(err));
}
unit_assert(err == 0);
buf_to_hex(b, len, wire1, bufs);
if(vbmp) printf("wire1: %s", wire1);
err = sldns_wire2str_rr_buf(b, len, txt_out, bufs);
unit_assert(err < (int)bufs && err > 0);
if(vbmp) printf("txt: %s", txt_out);
len = sizeof(b);
err = sldns_str2wire_rr_buf(txt_out, b, &len, NULL, 3600,
NULL, 0, NULL, 0);
if(err != 0) {
if(vbmp) printf("sldns_str2wire_rr_buf-2, pos %d: %s\n",
LDNS_WIREPARSE_OFFSET(err),
sldns_get_errorstr_parse(err));
}
unit_assert(err == 0);
buf_to_hex(b, len, wire2, bufs);
if(vbmp) printf("wire2: %s", wire2);
}
static void
rr_checks(char* wire_chk, char* txt_chk, char* txt_out, char* wire_out,
char* back)
{
#ifdef __APPLE__
if(strstr(txt_out, "IN AAAA"))
txt_out = txt_chk;
#endif
if(strcmp(txt_chk, txt_out) != 0 && vbmp)
printf("txt different\n");
if(strcmp(wire_chk, wire_out) != 0 && vbmp)
printf("wire1 different\n");
if(strcmp(wire_chk, back) != 0 && vbmp)
printf("wire2 different\n");
unit_assert(strcmp(txt_chk, txt_out) == 0);
unit_assert(strcmp(wire_chk, wire_out) == 0);
unit_assert(strcmp(wire_chk, back) == 0);
}
static void
rr_test_file(const char* input, const char* check)
{
size_t bufs = 131072;
FILE* inf, *chf, *of;
int lineno = 0, chlineno = 0;
char* txt_in = (char*)malloc(bufs);
char* txt_out = (char*)malloc(bufs);
char* txt_chk = (char*)malloc(bufs);
char* wire_out = (char*)malloc(bufs);
char* wire_chk = (char*)malloc(bufs);
char* back = (char*)malloc(bufs);
if(!txt_in || !txt_out || !txt_chk || !wire_out || !wire_chk || !back)
fatal_exit("malloc failure");
inf = fopen(input, "r");
if(!inf) fatal_exit("cannot open %s: %s", input, strerror(errno));
chf = fopen(check, "r");
if(!chf) fatal_exit("cannot open %s: %s", check, strerror(errno));
of = NULL;
if(0) {
of = fopen("outputfile", "w");
if(!of) fatal_exit("cannot write output: %s", strerror(errno));
}
while(fgets(txt_in, (int)bufs, inf)) {
lineno++;
if(vbmp) printf("\n%s:%d %s", input, lineno, txt_in);
if(txt_in[0] == 0 || txt_in[0] == '\n' || txt_in[0] == ';')
continue;
if(!fgets(wire_chk, (int)bufs, chf))
printf("%s too short\n", check);
if(!fgets(txt_chk, (int)bufs, chf))
printf("%s too short\n", check);
chlineno += 2;
if(vbmp) printf("%s:%d %s", check, chlineno-1, wire_chk);
if(vbmp) printf("%s:%d %s", check, chlineno, txt_chk);
rr_transform(txt_in, wire_out, txt_out, back, bufs);
if(of) {
fprintf(of, "%s%s", wire_out, txt_out);
} else {
rr_checks(wire_chk, txt_chk, txt_out, wire_out, back);
}
}
if(of) fclose(of);
fclose(inf);
fclose(chf);
free(txt_in);
free(txt_out);
free(txt_chk);
free(wire_out);
free(wire_chk);
free(back);
}
static void
rr_tests(void)
{
rr_test_file("testdata/test_ldnsrr.1", "testdata/test_ldnsrr.c1");
rr_test_file("testdata/test_ldnsrr.2", "testdata/test_ldnsrr.c2");
rr_test_file("testdata/test_ldnsrr.3", "testdata/test_ldnsrr.c3");
rr_test_file("testdata/test_ldnsrr.4", "testdata/test_ldnsrr.c4");
rr_test_file("testdata/test_ldnsrr.5", "testdata/test_ldnsrr.c5");
}
void
ldns_test(void)
{
unit_show_feature("sldns");
rr_tests();
}