1 // RUN: %clangxx %s -o %t && %run %t %p
2
3 #include <assert.h>
4 #include <resolv.h>
5 #include <string.h>
6
7 #include "sanitizer_common/sanitizer_specific.h"
8
testWrite()9 void testWrite() {
10 char unsigned input[] = {0xff, 0xc5, 0xf7, 0xff, 0x00, 0x00, 0xff, 0x0a, 0x00,
11 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,
12 0x10, 0x01, 0x05, 0x00, 0x01, 0x0a, 0x67, 0x6f, 0x6f,
13 0x67, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x00};
14 char output[1024];
15
16 int res = dn_expand(input, input + sizeof(input), input + 23, output,
17 sizeof(output));
18
19 assert(res == 12);
20 assert(strcmp(output, "google\\.com") == 0);
21 check_mem_is_good(output, strlen(output) + 1);
22 }
23
testWriteZeroLength()24 void testWriteZeroLength() {
25 char unsigned input[] = {
26 0xff, 0xc5, 0xf7, 0xff, 0x00, 0x00, 0xff, 0x0a, 0x00, 0x00, 0x00, 0x01,
27 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x10, 0x01, 0x05, 0x00, 0x01, 0x00,
28 };
29 char output[1024];
30
31 int res = dn_expand(input, input + sizeof(input), input + 23, output,
32 sizeof(output));
33
34 assert(res == 1);
35 assert(strcmp(output, "") == 0);
36 check_mem_is_good(output, strlen(output) + 1);
37 }
38
testComp()39 void testComp() {
40 char unsigned msg[1024];
41 char unsigned *mb = msg;
42 char unsigned *me = msg + sizeof(msg);
43 char unsigned **pb = (char unsigned **)mb;
44 pb[0] = msg;
45 pb[1] = nullptr;
46 mb += 64;
47 char unsigned **pe = (char unsigned **)mb;
48
49 char unsigned *n1 = mb;
50 int res = dn_comp("llvm.org", mb, me - mb, pb, pe);
51 assert(res == 10);
52 mb += res;
53
54 char unsigned *n2 = mb;
55 res = dn_comp("lab.llvm.org", mb, me - mb, pb, pe);
56 assert(res == 6);
57 mb += res;
58
59 {
60 char output[1024];
61 res = dn_expand(msg, msg + sizeof(msg), n1, output, sizeof(output));
62
63 fprintf(stderr, "%d\n", res);
64 assert(res == 10);
65 assert(strcmp(output, "llvm.org") == 0);
66 check_mem_is_good(output, strlen(output) + 1);
67 }
68
69 {
70 char output[1024];
71 res = dn_expand(msg, msg + sizeof(msg), n2, output, sizeof(output));
72
73 assert(res == 6);
74 assert(strcmp(output, "lab.llvm.org") == 0);
75 check_mem_is_good(output, strlen(output) + 1);
76 }
77 }
78
main(int iArgc,const char * szArgv[])79 int main(int iArgc, const char *szArgv[]) {
80 testWrite();
81 testWriteZeroLength();
82 testComp();
83
84 return 0;
85 }
86