// Copyright 2011 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. #pragma allow_unsafe_buffers #endif #include "net/dns/dns_query.h" #include <cstdint> #include <memory> #include <optional> #include <string> #include <string_view> #include <tuple> #include <vector> #include "base/containers/span.h" #include "base/memory/raw_ptr.h" #include "base/memory/scoped_refptr.h" #include "net/base/io_buffer.h" #include "net/dns/dns_names_util.h" #include "net/dns/opt_record_rdata.h" #include "net/dns/public/dns_protocol.h" #include "net/dns/record_rdata.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" namespace net { namespace { ElementsAreArray; std::tuple<const char*, size_t> AsTuple(const IOBufferWithSize* buf) { … } bool ParseAndCreateDnsQueryFromRawPacket(const uint8_t* data, size_t length, std::unique_ptr<DnsQuery>* out) { … } // This includes \0 at the end. const char kQNameData[] = …; const base::span<const uint8_t> kQName = …; TEST(DnsQueryTest, Constructor) { … } TEST(DnsQueryTest, CopiesAreIndependent) { … } TEST(DnsQueryTest, Clone) { … } TEST(DnsQueryTest, EDNS0) { … } TEST(DnsQueryTest, Block128Padding) { … } TEST(DnsQueryTest, Block128Padding_LongName) { … } TEST(DnsQueryParseTest, SingleQuestionForTypeARecord) { … } TEST(DnsQueryParseTest, SingleQuestionForTypeAAAARecord) { … } const uint8_t kQueryTruncatedQuestion[] = …; const uint8_t kQueryTwoQuestions[] = …; const uint8_t kQueryInvalidDNSDomainName1[] = …; const uint8_t kQueryInvalidDNSDomainName2[] = …; TEST(DnsQueryParseTest, FailsInvalidQueries) { … } TEST(DnsQueryParseTest, ParsesLongName) { … } // Tests against incorrect name length validation, which is anti-pattern #3 from // the "NAME:WRECK" report: // https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/ TEST(DnsQueryParseTest, FailsTooLongName) { … } // Tests against incorrect name length validation, which is anti-pattern #3 from // the "NAME:WRECK" report: // https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/ TEST(DnsQueryParseTest, FailsTooLongSingleLabelName) { … } // Test that a query cannot be parsed with a name extending past the end of the // data. // Tests against incorrect name length validation, which is anti-pattern #3 from // the "NAME:WRECK" report: // https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/ TEST(DnsQueryParseTest, FailsNonendedName) { … } // Test that a query cannot be parsed with a name without final null // termination. Parsing should assume the name has not ended and find the first // byte of the TYPE field instead, making the actual type unparsable. // Tests against incorrect name null termination, which is anti-pattern #4 from // the "NAME:WRECK" report: // https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/ TEST(DnsQueryParseTest, FailsNameWithoutTerminator) { … } TEST(DnsQueryParseTest, FailsQueryWithNoQuestions) { … } TEST(DnsQueryParseTest, FailsQueryWithMultipleQuestions) { … } // Test that if more questions are at the end of the buffer than the number of // questions claimed in the query header, the extra questions are safely // ignored. TEST(DnsQueryParseTest, IgnoresExtraQuestion) { … } // Test that the query fails to parse if it does not contain the number of // questions claimed in the query header. // Tests against incorrect record count field validation, which is anti-pattern // #5 from the "NAME:WRECK" report: // https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/ TEST(DnsQueryParseTest, FailsQueryWithMissingQuestion) { … } // Test that DnsQuery parsing disallows name compression pointers (which should // never be useful when only single-question queries are parsed). // Indirectly tests against incorrect name compression pointer validation, which // is anti-pattern #6 from the "NAME:WRECK" report: // https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/ TEST(DnsQueryParseTest, FailsQueryWithNamePointer) { … } } // namespace } // namespace net