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/msan_interface.h>
8 
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 }
22 
23 void testWriteZeroLength() {
24   char unsigned input[] = {
25       0xff, 0xc5, 0xf7, 0xff, 0x00, 0x00, 0xff, 0x0a, 0x00, 0x00, 0x00, 0x01,
26       0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x10, 0x01, 0x05, 0x00, 0x01, 0x00,
27   };
28   char output[1024];
29 
30   int res = dn_expand(input, input + sizeof(input), input + 23, output,
31                       sizeof(output));
32 
33   assert(res == 1);
34   assert(strcmp(output, "") == 0);
35 }
36 
37 void testComp() {
38   char unsigned msg[1024];
39   char unsigned *mb = msg;
40   char unsigned *me = msg + sizeof(msg);
41   char unsigned **pb = (char unsigned **)mb;
42   pb[0] = msg;
43   pb[1] = nullptr;
44   mb += 64;
45   char unsigned **pe = (char unsigned **)mb;
46 
47   char unsigned *n1 = mb;
48   int res = dn_comp("llvm.org", mb, me - mb, pb, pe);
49   assert(res == 10);
50   mb += res;
51 
52   char unsigned *n2 = mb;
53   res = dn_comp("lab.llvm.org", mb, me - mb, pb, pe);
54   assert(res == 6);
55   mb += res;
56 
57   {
58     char output[1024];
59     res = dn_expand(msg, msg + sizeof(msg), n1, output, sizeof(output));
60 
61     fprintf(stderr, "%d\n", res);
62     assert(res == 10);
63     assert(strcmp(output, "llvm.org") == 0);
64   }
65 
66   {
67     char output[1024];
68     res = dn_expand(msg, msg + sizeof(msg), n2, output, sizeof(output));
69 
70     assert(res == 6);
71     assert(strcmp(output, "lab.llvm.org") == 0);
72   }
73 }
74 
75 int main(int iArgc, const char *szArgv[]) {
76   testWrite();
77   testWriteZeroLength();
78   testComp();
79 
80   return 0;
81 }
82