<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <arpa/inet.h>
#include <resolv.h>

#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "net/base/ip_endpoint.h"
#include "net/dns/dns_config_service_posix.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

void CompareConfig(const struct __res_state &res, const DnsConfig& config) {
  EXPECT_EQ(config.ndots, static_cast<int>(res.ndots));
  EXPECT_EQ(config.edns0, (res.options & RES_USE_EDNS0) != 0);
  EXPECT_EQ(config.rotate, (res.options & RES_ROTATE) != 0);
  EXPECT_EQ(config.timeout.InSeconds(), res.retrans);
  EXPECT_EQ(config.attempts, res.retry);

  // Compare nameservers. IPv6 precede IPv4.
#if OS_LINUX
  size_t nscount6 = res._u._ext.nscount6;
#else
  size_t nscount6 = 0;
#endif
  size_t nscount4 = res.nscount;
  ASSERT_EQ(config.nameservers.size(), nscount6 + nscount4);
#if OS_LINUX
  for (size_t i = 0; i < nscount6; ++i) {
    IPEndPoint ipe;
    EXPECT_TRUE(ipe.FromSockAddr(
        reinterpret_cast<const struct sockaddr*>(res._u._ext.nsaddrs[i]),
        sizeof *res._u._ext.nsaddrs[i]));
    EXPECT_EQ(config.nameservers[i], ipe);
  }
#endif
  for (size_t i = 0; i < nscount4; ++i) {
    IPEndPoint ipe;
    EXPECT_TRUE(ipe.FromSockAddr(
        reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]),
        sizeof res.nsaddr_list[i]));
    EXPECT_EQ(config.nameservers[nscount6 + i], ipe);
  }

  ASSERT_TRUE(config.search.size() <= MAXDNSRCH);
  EXPECT_TRUE(res.dnsrch[config.search.size()] == NULL);
  for (size_t i = 0; i < config.search.size(); ++i) {
    EXPECT_EQ(config.search[i], res.dnsrch[i]);
  }
}

// Fills in |res| with sane configuration. Change |generation| to add diversity.
void InitializeResState(res_state res, int generation) {
  memset(res, 0, sizeof(*res));
  res->options = RES_INIT | RES_ROTATE;
  res->ndots = 2;
  res->retrans = 8;
  res->retry = 7;

  const char kDnsrch[] = "chromium.org" "\0" "example.com";
  memcpy(res->defdname, kDnsrch, sizeof(kDnsrch));
  res->dnsrch[0] = res->defdname;
  res->dnsrch[1] = res->defdname + sizeof("chromium.org");

  const char* ip4addr[3] = {
      "8.8.8.8",
      "192.168.1.1",
      "192.168.3.11",
  };

  for (int i = 0; i < 3; ++i) {
    struct sockaddr_in sa;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(NS_DEFAULTPORT + i - generation);
    inet_pton(AF_INET, ip4addr[i], &sa.sin_addr);
    res->nsaddr_list[i] = sa;
  }
  res->nscount = 3;

#if OS_LINUX
  const char* ip6addr[2] = {
      "2001:db8:0::42",
      "::FFFF:192.168.127.12",
  };

  for (int i = 0; i < 2; ++i) {
    // Must use malloc to mimick res_ninit.
    struct sockaddr_in6 *sa6;
    sa6 = (struct sockaddr_in6 *)malloc(sizeof(*sa6));
    sa6->sin6_family = AF_INET6;
    sa6->sin6_port = htons(NS_DEFAULTPORT - i);
    inet_pton(AF_INET6, ip6addr[i], &sa6->sin6_addr);
    res->_u._ext.nsaddrs[i] = sa6;
  }
  res->_u._ext.nscount6 = 2;
#endif
}

void CloseResState(res_state res) {
#if OS_LINUX
  for (int i = 0; i < res->_u._ext.nscount6; ++i) {
    ASSERT_TRUE(res->_u._ext.nsaddrs[i] != NULL);
    free(res->_u._ext.nsaddrs[i]);
  }
#endif
}

class DnsConfigServiceTest : public testing::Test,
                             public DnsConfigService::Observer {
 public:
  // Mocks

  // DnsConfigService owns the instances of ResolverLib and
  // FilePathWatcherFactory that it gets, so use simple proxies to call
  // DnsConfigServiceTest.

  // ResolverLib is owned by WatcherDelegate which is posted to WorkerPool so
  // it must be canceled before the test is over.
  class MockResolverLib : public DnsConfigServicePosix::ResolverLib {
   public:
    explicit MockResolverLib(DnsConfigServiceTest *test) : test_(test) {}
    virtual ~MockResolverLib() {
      base::AutoLock lock(lock_);
      if (test_) {
        EXPECT_TRUE(test_->IsComplete());
      }
    }
    virtual int ninit(res_state res) OVERRIDE {
      base::AutoLock lock(lock_);
      if (test_)<commit_msg>Fixing #ifdef -> #if defined()<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <arpa/inet.h>
#include <resolv.h>

#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "net/base/ip_endpoint.h"
#include "net/dns/dns_config_service_posix.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

void CompareConfig(const struct __res_state &res, const DnsConfig& config) {
  EXPECT_EQ(config.ndots, static_cast<int>(res.ndots));
  EXPECT_EQ(config.edns0, (res.options & RES_USE_EDNS0) != 0);
  EXPECT_EQ(config.rotate, (res.options & RES_ROTATE) != 0);
  EXPECT_EQ(config.timeout.InSeconds(), res.retrans);
  EXPECT_EQ(config.attempts, res.retry);

  // Compare nameservers. IPv6 precede IPv4.
#if defined(OS_LINUX)
  size_t nscount6 = res._u._ext.nscount6;
#else
  size_t nscount6 = 0;
#endif
  size_t nscount4 = res.nscount;
  ASSERT_EQ(config.nameservers.size(), nscount6 + nscount4);
#if defined(OS_LINUX)
  for (size_t i = 0; i < nscount6; ++i) {
    IPEndPoint ipe;
    EXPECT_TRUE(ipe.FromSockAddr(
        reinterpret_cast<const struct sockaddr*>(res._u._ext.nsaddrs[i]),
        sizeof *res._u._ext.nsaddrs[i]));
    EXPECT_EQ(config.nameservers[i], ipe);
  }
#endif
  for (size_t i = 0; i < nscount4; ++i) {
    IPEndPoint ipe;
    EXPECT_TRUE(ipe.FromSockAddr(
        reinterpret_cast<const struct sockaddr*>(&res.nsaddr_list[i]),
        sizeof res.nsaddr_list[i]));
    EXPECT_EQ(config.nameservers[nscount6 + i], ipe);
  }

  ASSERT_TRUE(config.search.size() <= MAXDNSRCH);
  EXPECT_TRUE(res.dnsrch[config.search.size()] == NULL);
  for (size_t i = 0; i < config.search.size(); ++i) {
    EXPECT_EQ(config.search[i], res.dnsrch[i]);
  }
}

// Fills in |res| with sane configuration. Change |generation| to add diversity.
void InitializeResState(res_state res, int generation) {
  memset(res, 0, sizeof(*res));
  res->options = RES_INIT | RES_ROTATE;
  res->ndots = 2;
  res->retrans = 8;
  res->retry = 7;

  const char kDnsrch[] = "chromium.org" "\0" "example.com";
  memcpy(res->defdname, kDnsrch, sizeof(kDnsrch));
  res->dnsrch[0] = res->defdname;
  res->dnsrch[1] = res->defdname + sizeof("chromium.org");

  const char* ip4addr[3] = {
      "8.8.8.8",
      "192.168.1.1",
      "192.168.3.11",
  };

  for (int i = 0; i < 3; ++i) {
    struct sockaddr_in sa;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(NS_DEFAULTPORT + i - generation);
    inet_pton(AF_INET, ip4addr[i], &sa.sin_addr);
    res->nsaddr_list[i] = sa;
  }
  res->nscount = 3;

#if defined(OS_LINUX)
  const char* ip6addr[2] = {
      "2001:db8:0::42",
      "::FFFF:192.168.127.12",
  };

  for (int i = 0; i < 2; ++i) {
    // Must use malloc to mimick res_ninit.
    struct sockaddr_in6 *sa6;
    sa6 = (struct sockaddr_in6 *)malloc(sizeof(*sa6));
    sa6->sin6_family = AF_INET6;
    sa6->sin6_port = htons(NS_DEFAULTPORT - i);
    inet_pton(AF_INET6, ip6addr[i], &sa6->sin6_addr);
    res->_u._ext.nsaddrs[i] = sa6;
  }
  res->_u._ext.nscount6 = 2;
#endif
}

void CloseResState(res_state res) {
#if defined(OS_LINUX)
  for (int i = 0; i < res->_u._ext.nscount6; ++i) {
    ASSERT_TRUE(res->_u._ext.nsaddrs[i] != NULL);
    free(res->_u._ext.nsaddrs[i]);
  }
#endif
}

class DnsConfigServiceTest : public testing::Test,
                             public DnsConfigService::Observer {
 public:
  // Mocks

  // DnsConfigService owns the instances of ResolverLib and
  // FilePathWatcherFactory that it gets, so use simple proxies to call
  // DnsConfigServiceTest.

  // ResolverLib is owned by WatcherDelegate which is posted to WorkerPool so
  // it must be canceled before the test is over.
  class MockResolverLib : public DnsConfigServicePosix::ResolverLib {
   public:
    explicit MockResolverLib(DnsConfigServiceTest *test) : test_(test) {}
    virtual ~MockResolverLib() {
      base::AutoLock lock(lock_);
      if (test_) {
        EXPECT_TRUE(test_->IsComplete());
      }
    }
    virtual int ninit(res_state res) OVERRIDE {
      base::AutoLock lock(lock_);
      if (test_)
{'fraction_non_alphanumeric': 0.14676761793826443, 'fraction_numerical': 0.02306348281887012, 'mean_word_length': 3.5914438502673796, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4545235', 'n_tokens_mistral': 3405, 'n_tokens_neox': 3300, 'n_words': 694}