blob: 1ba3d54cc9f029d3895f26349070249af50c489b [file] [log] [blame]
Jason Jeremy Imana21be272020-10-21 17:53:45 +09001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "patchpanel/dns/dns_query.h"
6
7#include <tuple>
8
9#include "base/stl_util.h"
10#include "testing/gmock/include/gmock/gmock.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13#include "patchpanel/dns/dns_protocol.h"
14#include "patchpanel/dns/dns_util.h"
15#include "patchpanel/dns/io_buffer.h"
16
17namespace patchpanel {
18
19namespace {
20
21using ::testing::ElementsAreArray;
22
23bool ParseAndCreateDnsQueryFromRawPacket(const uint8_t* data,
24 size_t length,
25 std::unique_ptr<DnsQuery>* out) {
26 auto packet = base::MakeRefCounted<IOBufferWithSize>(length);
27 memcpy(packet->data(), data, length);
28 out->reset(new DnsQuery(packet));
29 return (*out)->Parse(length);
30}
31
32// This includes \0 at the end.
33const char kQNameData[] =
34 "\x03"
35 "www"
36 "\x07"
37 "example"
38 "\x03"
39 "com";
40
41TEST(DnsQueryParseTest, SingleQuestionForTypeARecord) {
42 const uint8_t query_data[] = {
43 0x12, 0x34, // ID
44 0x00, 0x00, // flags
45 0x00, 0x01, // number of questions
46 0x00, 0x00, // number of answer rr
47 0x00, 0x00, // number of name server rr
48 0x00, 0x00, // number of additional rr
49 0x03, 'w', 'w', 'w', 0x07, 'e', 'x', 'a',
50 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm',
51 0x00, // null label
52 0x00, 0x01, // type A Record
53 0x00, 0x01, // class IN
54 };
55 std::unique_ptr<DnsQuery> query;
56 EXPECT_TRUE(ParseAndCreateDnsQueryFromRawPacket(query_data,
57 sizeof(query_data), &query));
58 EXPECT_EQ(0x1234, query->id());
59 base::StringPiece qname(kQNameData, sizeof(kQNameData));
60 EXPECT_EQ(qname, query->qname());
61 EXPECT_EQ(dns_protocol::kTypeA, query->qtype());
62}
63
64TEST(DnsQueryParseTest, SingleQuestionForTypeAAAARecord) {
65 const uint8_t query_data[] = {
66 0x12, 0x34, // ID
67 0x00, 0x00, // flags
68 0x00, 0x01, // number of questions
69 0x00, 0x00, // number of answer rr
70 0x00, 0x00, // number of name server rr
71 0x00, 0x00, // number of additional rr
72 0x03, 'w', 'w', 'w', 0x07, 'e', 'x', 'a',
73 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm',
74 0x00, // null label
75 0x00, 0x1c, // type AAAA Record
76 0x00, 0x01, // class IN
77 };
78 std::unique_ptr<DnsQuery> query;
79 EXPECT_TRUE(ParseAndCreateDnsQueryFromRawPacket(query_data,
80 sizeof(query_data), &query));
81 EXPECT_EQ(0x1234, query->id());
82 base::StringPiece qname(kQNameData, sizeof(kQNameData));
83 EXPECT_EQ(qname, query->qname());
84 EXPECT_EQ(dns_protocol::kTypeAAAA, query->qtype());
85}
86
87const uint8_t kQueryTruncatedQuestion[] = {
88 0x12, 0x34, // ID
89 0x00, 0x00, // flags
90 0x00, 0x02, // number of questions
91 0x00, 0x00, // number of answer rr
92 0x00, 0x00, // number of name server rr
93 0x00, 0x00, // number of additional rr
94 0x03, 'w', 'w', 'w', 0x07, 'e', 'x', 'a',
95 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm',
96 0x00, // null label
97 0x00, 0x01, // type A Record
98 0x00, // class IN, truncated
99};
100
101const uint8_t kQueryTwoQuestions[] = {
102 0x12, 0x34, // ID
103 0x00, 0x00, // flags
104 0x00, 0x02, // number of questions
105 0x00, 0x00, // number of answer rr
106 0x00, 0x00, // number of name server rr
107 0x00, 0x00, // number of additional rr
108 0x03, 'w', 'w', 'w', 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
109 0x03, 'c', 'o', 'm',
110 0x00, // null label
111 0x00, 0x01, // type A Record
112 0x00, 0x01, // class IN
113 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'o', 'r', 'g',
114 0x00, // null label
115 0x00, 0x1c, // type AAAA Record
116 0x00, 0x01, // class IN
117};
118
119const uint8_t kQueryInvalidDNSDomainName1[] = {
120 0x12, 0x34, // ID
121 0x00, 0x00, // flags
122 0x00, 0x01, // number of questions
123 0x00, 0x00, // number of answer rr
124 0x00, 0x00, // number of name server rr
125 0x00, 0x00, // number of additional rr
126 0x02, 'w', 'w', 'w', // wrong label length
127 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm',
128 0x00, // null label
129 0x00, 0x01, // type A Record
130 0x00, 0x01, // class IN
131};
132
133const uint8_t kQueryInvalidDNSDomainName2[] = {
134 0x12, 0x34, // ID
135 0x00, 0x00, // flags
136 0x00, 0x01, // number of questions
137 0x00, 0x00, // number of answer rr
138 0x00, 0x00, // number of name server rr
139 0x00, 0x00, // number of additional rr
140 0xc0, 0x02, // illegal name pointer
141 0x00, 0x01, // type A Record
142 0x00, 0x01, // class IN
143};
144
145TEST(DnsQueryParseTest, FailsInvalidQueries) {
146 const struct TestCase {
147 const uint8_t* data;
148 size_t size;
149 } testcases[] = {
150 {kQueryTruncatedQuestion, base::size(kQueryTruncatedQuestion)},
151 {kQueryTwoQuestions, base::size(kQueryTwoQuestions)},
152 {kQueryInvalidDNSDomainName1, base::size(kQueryInvalidDNSDomainName1)},
153 {kQueryInvalidDNSDomainName2, base::size(kQueryInvalidDNSDomainName2)}};
154 std::unique_ptr<DnsQuery> query;
155 for (const auto& testcase : testcases) {
156 EXPECT_FALSE(ParseAndCreateDnsQueryFromRawPacket(testcase.data,
157 testcase.size, &query));
158 }
159}
160
161} // namespace
162
163} // namespace patchpanel