1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <inttypes.h>
5 #include <ctype.h>
6 
7 #include "test-common.h"
8 
9 #include <libmy/ubuf.h>
10 #include <wdns.h>
11 
12 #define NAME "test-str_to_name"
13 
14 typedef wdns_res (*fp)(char *, wdns_name_t *);
15 
16 struct test {
17 	char *input;
18 	fp func;
19 	const uint8_t *expected;
20 	size_t expected_len;
21 	wdns_res expected_res;
22 };
23 
24 struct test tdata[] = {
25 	{ "fsi.io", (fp)wdns_str_to_name, (const uint8_t*)"\x03""fsi\x02io\x00", 8, wdns_res_success},
26 	{ "FsI.io", (fp)wdns_str_to_name_case, (const uint8_t*)"\x03""FsI\x02io\x00", 8, wdns_res_success},
27 	{ 0 }
28 };
29 
30 
31 static size_t
test_str_to_name(void)32 test_str_to_name(void)
33 {
34 	ubuf *u;
35 	struct test *cur;
36 	size_t failures = 0;
37 
38 	u = ubuf_init(256);
39 
40 	for(cur = tdata; cur->input != NULL; cur++) {
41 		wdns_name_t name;
42 		wdns_res res;
43 
44 		ubuf_reset(u);
45 
46 		res = cur->func(cur->input, &name);
47 
48 		if (res != cur->expected_res) {
49 			ubuf_add_fmt(u, "FAIL %" PRIu64 ": input=", cur-tdata);
50 			escape(u, (uint8_t*)cur->input, strlen(cur->input));
51 			ubuf_add_fmt(u, " res=%s != %s",
52 					wdns_res_to_str(res),
53 					wdns_res_to_str(cur->expected_res));
54 
55 			if (res == wdns_res_success) {
56 				ubuf_add_cstr(u, " value=");
57 				escape(u, name.data, name.len);
58 			}
59 			failures++;
60 		} else if (name.len != cur->expected_len || memcmp(name.data, cur->expected, name.len)) {
61 			ubuf_add_fmt(u, "FAIL %" PRIu64 ": input=", cur-tdata);
62 			escape(u, (uint8_t*)cur->input, strlen(cur->input));
63 
64 			if (name.len != cur->expected_len) {
65 				ubuf_add_fmt(u, " len %d != %d",
66 						name.len, cur->expected_len);
67 			}
68 
69 			ubuf_add_fmt(u, " res=%s",
70 					wdns_res_to_str(res));
71 
72 			ubuf_add_cstr(u, " value=");
73 			escape(u, name.data, name.len);
74 			ubuf_add_cstr(u, " != ");
75 			escape(u, cur->expected, cur->expected_len);
76 
77 			failures++;
78 		} else {
79 			ubuf_add_fmt(u, "PASS %" PRIu64 ": input=", cur-tdata);
80 			escape(u, (uint8_t*)cur->input, strlen(cur->input));
81 			ubuf_add_fmt(u, " res=%s",
82 					wdns_res_to_str(res));
83 
84 			if (res == wdns_res_success) {
85 				ubuf_add_cstr(u, " value=");
86 				escape(u, name.data, name.len);
87 			}
88 		}
89 
90 		fprintf (stderr, "%s\n", ubuf_cstr(u));
91 		if (name.data != NULL) {
92 			free(name.data);
93 			name.data = NULL;
94 		}
95 	}
96 
97 	ubuf_destroy(&u);
98 	return failures;
99 }
100 
main(void)101 int main (void)
102 {
103 	int ret = 0;
104 
105 	ret |= check(test_str_to_name(), "test-str_to_name", NAME);
106 
107 	if (ret)
108 		return (EXIT_FAILURE);
109 	return (EXIT_SUCCESS);
110 }
111