chromium/net/dns/dns_test_util.h

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_DNS_DNS_TEST_UTIL_H_
#define NET_DNS_DNS_TEST_UTIL_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/condition_variable.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/connection_endpoint_metadata.h"
#include "net/base/ip_endpoint.h"
#include "net/dns/dns_client.h"
#include "net/dns/dns_config.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_transaction.h"
#include "net/dns/dns_util.h"
#include "net/dns/public/dns_over_https_server_config.h"
#include "net/dns/public/dns_protocol.h"
#include "net/dns/public/secure_dns_mode.h"
#include "net/socket/socket_test_util.h"
#include "url/scheme_host_port.h"

namespace net {

//-----------------------------------------------------------------------------
// Query/response set for www.google.com, ID is fixed to 0.
static const char kT0HostName[] =;
static const uint16_t kT0Qtype =;
static const char kT0DnsName[] =;
static const size_t kT0QuerySize =;
static const uint8_t kT0ResponseDatagram[] =;
static const char* const kT0IpAddresses[] =;
static const char kT0CanonName[] =;
static const base::TimeDelta kT0Ttl =;
// +1 for the CNAME record.
static const unsigned kT0RecordCount =;

//-----------------------------------------------------------------------------
// Query/response set for codereview.chromium.org, ID is fixed to 1.
static const char kT1HostName[] =;
static const uint16_t kT1Qtype =;
static const char kT1DnsName[] =;
static const size_t kT1QuerySize =;
static const uint8_t kT1ResponseDatagram[] =;
static const char* const kT1IpAddresses[] =;
static const char kT1CanonName[] =;
static const base::TimeDelta kT1Ttl =;
// +1 for the CNAME record.
static const unsigned kT1RecordCount =;

//-----------------------------------------------------------------------------
// Query/response set for www.ccs.neu.edu, ID is fixed to 2.
static const char kT2HostName[] =;
static const uint16_t kT2Qtype =;
static const char kT2DnsName[] =;
static const size_t kT2QuerySize =;
static const uint8_t kT2ResponseDatagram[] =;
static const char* const kT2IpAddresses[] =;
static const char kT2CanonName[] =;
static const base::TimeDelta kT2Ttl =;
// +1 for the CNAME record.
static const unsigned kT2RecordCount =;

//-----------------------------------------------------------------------------
// Query/response set for www.google.az, ID is fixed to 3.
static const char kT3HostName[] =;
static const uint16_t kT3Qtype =;
static const char kT3DnsName[] =;
static const size_t kT3QuerySize =;
static const uint8_t kT3ResponseDatagram[] =;
static const char* const kT3IpAddresses[] =;
static const char kT3CanonName[] =;
static const base::TimeDelta kT3Ttl =;
// +2 for the CNAME records, +1 for TXT record.
static const unsigned kT3RecordCount =;

//-----------------------------------------------------------------------------
// Query/response set for www.gstatic.com, ID is fixed to 0.
static const char kT4HostName[] =;
static const uint16_t kT4Qtype =;
static const char kT4DnsName[] =;
static const size_t kT4QuerySize =;
static const uint8_t kT4ResponseDatagram[] =;

static const char* const kT4IpAddresses[] =;
static const base::TimeDelta kT4Ttl =;
static const unsigned kT4RecordCount =;

class AddressSorter;
class DnsClient;
class DnsSession;
class IPAddress;
class ResolveContext;
class URLRequestContext;

DnsConfig CreateValidDnsConfig();

DnsResourceRecord BuildTestDnsRecord(std::string name,
                                     uint16_t type,
                                     std::string rdata,
                                     base::TimeDelta ttl = base::Days(1));

DnsResourceRecord BuildTestCnameRecord(std::string name,
                                       std::string_view canonical_name,
                                       base::TimeDelta ttl = base::Days(1));

DnsResourceRecord BuildTestAddressRecord(std::string name,
                                         const IPAddress& ip,
                                         base::TimeDelta ttl = base::Days(1));

DnsResourceRecord BuildTestTextRecord(std::string name,
                                      std::vector<std::string> text_strings,
                                      base::TimeDelta ttl = base::Days(1));

DnsResourceRecord BuildTestHttpsAliasRecord(
    std::string name,
    std::string_view alias_name,
    base::TimeDelta ttl = base::Days(1));

std::pair<uint16_t, std::string> BuildTestHttpsServiceAlpnParam(
    const std::vector<std::string>& alpns);

std::pair<uint16_t, std::string> BuildTestHttpsServiceEchConfigParam(
    base::span<const uint8_t> ech_config_list);

std::pair<uint16_t, std::string> BuildTestHttpsServiceMandatoryParam(
    std::vector<uint16_t> param_key_list);

std::pair<uint16_t, std::string> BuildTestHttpsServicePortParam(uint16_t port);

// `params` is a mapping from service param keys to a string containing the
// encoded bytes of a service param value (without the value length prefix which
// this method will automatically add).
DnsResourceRecord BuildTestHttpsServiceRecord(
    std::string name,
    uint16_t priority,
    std::string_view service_name,
    const std::map<uint16_t, std::string>& params,
    base::TimeDelta ttl = base::Days(1));

DnsResponse BuildTestDnsResponse(
    std::string name,
    uint16_t type,
    const std::vector<DnsResourceRecord>& answers,
    const std::vector<DnsResourceRecord>& authority = {};

DnsResponse BuildTestDnsAddressResponse(std::string name,
                                        const IPAddress& ip,
                                        std::string answer_name = "");
DnsResponse BuildTestDnsAddressResponseWithCname(std::string name,
                                                 const IPAddress& ip,
                                                 std::string cannonname,
                                                 std::string answer_name = "");

// If |answer_name| is empty, |name| will be used for all answer records, as is
// the normal behavior.
DnsResponse BuildTestDnsTextResponse(
    std::string name,
    std::vector<std::vector<std::string>> text_records,
    std::string answer_name = "");
DnsResponse BuildTestDnsPointerResponse(std::string name,
                                        std::vector<std::string> pointer_names,
                                        std::string answer_name = "");

struct TestServiceRecord {};

DnsResponse BuildTestDnsServiceResponse(
    std::string name,
    std::vector<TestServiceRecord> service_records,
    std::string answer_name = "");

struct MockDnsClientRule {};

MockDnsClientRuleList;

// A DnsTransactionFactory which creates MockTransaction.
class MockDnsTransactionFactory : public DnsTransactionFactory {};

// MockDnsClient provides MockDnsTransactionFactory.
class MockDnsClient : public DnsClient {};

// A HostResolverProc that pushes each host mapped into a list and allows
// waiting for a specific number of requests. Unlike RuleBasedHostResolverProc
// it never calls SystemHostResolverCall. By default resolves all hostnames to
// "127.0.0.1". After AddRule(), it resolves only names explicitly specified.
class MockHostResolverProc : public HostResolverProc {};

}  // namespace net

#endif  // NET_DNS_DNS_TEST_UTIL_H_