xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/Linux/dn_expand.cpp (revision 4b33ea052ab7fbceed4c62debf1145f80d66b0d7)
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   check_mem_is_good(mb, res);
53   // pb is [msg, llvm.org, nullptr]
54   check_mem_is_good(pb, sizeof(*pb) * 3);
55   mb += res;
56 
57   char unsigned *n2 = mb;
58   res = dn_comp("lab.llvm.org", mb, me - mb, pb, pe);
59   assert(res == 6);
60   check_mem_is_good(mb, res);
61   // pb is [msg, llvm.org, lab.llvm.org, nullptr]
62   check_mem_is_good(pb, sizeof(*pb) * 4);
63   mb += res;
64 
65   {
66     char output[1024];
67     res = dn_expand(msg, msg + sizeof(msg), n1, output, sizeof(output));
68 
69     fprintf(stderr, "%d\n", res);
70     assert(res == 10);
71     assert(strcmp(output, "llvm.org") == 0);
72     check_mem_is_good(output, strlen(output) + 1);
73   }
74 
75   {
76     char output[1024];
77     res = dn_expand(msg, msg + sizeof(msg), n2, output, sizeof(output));
78 
79     assert(res == 6);
80     assert(strcmp(output, "lab.llvm.org") == 0);
81     check_mem_is_good(output, strlen(output) + 1);
82   }
83 }
84 
main(int iArgc,const char * szArgv[])85 int main(int iArgc, const char *szArgv[]) {
86   testWrite();
87   testWriteZeroLength();
88   testComp();
89 
90   return 0;
91 }
92