chromium/net/dns/address_info_unittest.cc

// Copyright 2019 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/address_info.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <array>
#include <memory>
#include <optional>
#include <string_view>

#include "base/check_op.h"
#include "base/numerics/safe_conversions.h"
#include "base/sys_byteorder.h"
#include "build/build_config.h"
#include "net/base/address_list.h"
#include "net/base/net_errors.h"
#include "net/base/sys_addrinfo.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

class MockAddrInfoGetter : public AddrInfoGetter {};

template <size_t N>
std::unique_ptr<addrinfo, FreeAddrInfoFunc>
MockAddrInfoGetter::MakeAddrInfoList(const IpAndPort (&ipp)[N],
                                     std::string_view canonical_name) {}

std::unique_ptr<addrinfo, FreeAddrInfoFunc> MockAddrInfoGetter::MakeAddrInfo(
    IpAndPort ipp,
    std::string_view canonical_name) {}

void MockAddrInfoGetter::InitializeAddrinfo(const IpAndPort& ip_and_port,
                                            char* canonical_name,
                                            addrinfo* ai_next,
                                            sockaddr_in* addr,
                                            addrinfo* ai) {}

std::unique_ptr<addrinfo, FreeAddrInfoFunc> MockAddrInfoGetter::getaddrinfo(
    const std::string& host,
    const addrinfo* /* hints */,
    int* out_os_error,
    handles::NetworkHandle) {}

std::unique_ptr<addrinfo> MakeHints(AddressFamily address_family,
                                    HostResolverFlags host_resolver_flags) {}

TEST(AddressInfoTest, Failure) {}

#if BUILDFLAG(IS_WIN)
// Note: this test is descriptive, not prescriptive.
TEST(AddressInfoTest, FailureWin) {
  auto getter = std::make_unique<MockAddrInfoGetter>();
  auto [ai, err, os_error] = AddressInfo::Get(
      "failure.com", *MakeHints(ADDRESS_FAMILY_IPV4, HOST_RESOLVER_CANONNAME),
      std::move(getter));

  EXPECT_FALSE(ai);
  EXPECT_EQ(err, ERR_NAME_RESOLUTION_FAILED);
  EXPECT_NE(os_error, 0);
}
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_ANDROID)
// Note: this test is descriptive, not prescriptive.
TEST(AddressInfoTest, FailureAndroid) {
  auto getter = std::make_unique<MockAddrInfoGetter>();
  auto [ai, err, os_error] = AddressInfo::Get(
      "failure.com", *MakeHints(ADDRESS_FAMILY_IPV4, HOST_RESOLVER_CANONNAME),
      std::move(getter));

  EXPECT_FALSE(ai);
  EXPECT_EQ(err, ERR_NAME_NOT_RESOLVED);
  EXPECT_NE(os_error, 0);
}
#endif  // BUILDFLAG(IS_ANDROID)

TEST(AddressInfoTest, Canonical) {}

TEST(AddressInfoTest, Iteration) {}

TEST(AddressInfoTest, IsAllLocalhostOfOneFamily) {}

TEST(AddressInfoTest, IsAllLocalhostOfOneFamilyFalse) {}

TEST(AddressInfoTest, CreateAddressList) {}

}  // namespace
}  // namespace net