Blame view

3rdparty/c-ares-1.18.1/test/dns-proto-test.cc 3.7 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  #include "ares-test.h"
  #include "dns-proto.h"
  
  #include <vector>
  
  namespace ares {
  namespace test {
  
  TEST(DNSProto, EncodeQuestions) {
    DNSPacket pkt;
    pkt.set_qid(0x1234).set_response().set_aa()
      .add_question(new DNSQuestion("example.com.", T_A))
      .add_question(new DNSQuestion("www.example.com", T_AAAA, C_CHAOS));
  
    std::vector<byte> data = {
      0x12, 0x34,  // qid
      0x84, // response + query + AA + not-TC + not-RD
      0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
      0x00, 0x02,  // num questions
      0x00, 0x00,  // num answer RRs
      0x00, 0x00,  // num authority RRs
      0x00, 0x00,  // num additional RRs
      // Question 1
      0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
      0x03, 'c', 'o', 'm',
      0x00,
      0x00, 0x01,  // type A
      0x00, 0x01,  // class IN
      // Question 2
      0x03, 'w', 'w', 'w',
      0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
      0x03, 'c', 'o', 'm',
      0x00,
      0x00, 0x1C,  // type AAAA = 28
      0x00, 0x03,  // class CHAOS = 3
    };
    EXPECT_EQ(data, pkt.data());
  }
  
  TEST(DNSProto, EncodeSingleNameAnswers) {
    DNSPacket pkt;
    pkt.qid_ = 0x1234;
    pkt.response_ = true;
    pkt.aa_ = true;
    pkt.opcode_ = O_QUERY;
    pkt.add_answer(new DNSCnameRR("example.com", 0x01020304, "other.com."));
    pkt.add_auth(new DNSPtrRR("www.example.com", 0x01020304, "www.other.com"));
  
    std::vector<byte> data = {
      0x12, 0x34,  // qid
      0x84, // response + query + AA + not-TC + not-RD
      0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
      0x00, 0x00,  // num questions
      0x00, 0x01,  // num answer RRs
      0x00, 0x01,  // num authority RRs
      0x00, 0x00,  // num additional RRs
      // Answer 1
      0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
      0x03, 'c', 'o', 'm',
      0x00,
      0x00, 0x05,  // RR type
      0x00, 0x01,  // class IN
      0x01, 0x02, 0x03, 0x04, // TTL
      0x00, 0x0B,  // rdata length
      0x05, 'o', 't', 'h', 'e', 'r',
      0x03, 'c', 'o', 'm',
      0x00,
      // Authority 1
      0x03, 'w', 'w', 'w',
      0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
      0x03, 'c', 'o', 'm',
      0x00,
      0x00, 0x0c,  // RR type
      0x00, 0x01,  // class IN
      0x01, 0x02, 0x03, 0x04, // TTL
      0x00, 0x0F,  // rdata length
      0x03, 'w', 'w', 'w',
      0x05, 'o', 't', 'h', 'e', 'r',
      0x03, 'c', 'o', 'm',
      0x00,
    };
    EXPECT_EQ(data, pkt.data());
  }
  
  TEST(DNSProto, EncodeAddressAnswers) {
    DNSPacket pkt;
    pkt.qid_ = 0x1234;
    pkt.response_ = true;
    pkt.aa_ = true;
    pkt.opcode_ = O_QUERY;
    std::vector<byte> addrv4 = {0x02, 0x03, 0x04, 0x05};
    pkt.add_answer(new DNSARR("example.com", 0x01020304, addrv4));
    byte addrv6[16] = {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
                       0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04};
    pkt.add_additional(new DNSAaaaRR("www.example.com", 0x01020304, addrv6, 16));
  
    std::vector<byte> data = {
      0x12, 0x34,  // qid
      0x84, // response + query + AA + not-TC + not-RD
      0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
      0x00, 0x00,  // num questions
      0x00, 0x01,  // num answer RRs
      0x00, 0x00,  // num authority RRs
      0x00, 0x01,  // num additional RRs
      // Answer 1
      0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
      0x03, 'c', 'o', 'm',
      0x00,
      0x00, 0x01,  // RR type
      0x00, 0x01,  // class IN
      0x01, 0x02, 0x03, 0x04, // TTL
      0x00, 0x04,  // rdata length
      0x02, 0x03, 0x04, 0x05,
      // Additional 1
      0x03, 'w', 'w', 'w',
      0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
      0x03, 'c', 'o', 'm',
      0x00,
      0x00, 0x1c,  // RR type
      0x00, 0x01,  // class IN
      0x01, 0x02, 0x03, 0x04, // TTL
      0x00, 0x10,  // rdata length
      0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
      0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04
    };
    EXPECT_EQ(data, pkt.data());
  }
  
  
  }  // namespace test
  }  // namespace ares