<commit_before>#include <net/if.h>

#include "test/extensions/common/dynamic_forward_proxy/mocks.h"
#include "test/mocks/upstream/cluster_manager.h"

#include "gtest/gtest.h"
#include "library/common/network/configurator.h"

using testing::_;
using testing::Ref;
using testing::Return;

namespace Envoy {
namespace Network {

class ConfiguratorTest : public testing::Test {
public:
  ConfiguratorTest()
      : dns_cache_manager_(
            new NiceMock<Extensions::Common::DynamicForwardProxy::MockDnsCacheManager>()),
        dns_cache_(dns_cache_manager_->dns_cache_),
        configurator_(std::make_shared<Configurator>(cm_, dns_cache_manager_)) {
    ON_CALL(*dns_cache_manager_, lookUpCacheByName(_)).WillByDefault(Return(dns_cache_));
    // Toggle network to reset network state.
    Configurator::setPreferredNetwork(ENVOY_NET_GENERIC);
    Configurator::setPreferredNetwork(ENVOY_NET_WLAN);
  }

  std::shared_ptr<NiceMock<Extensions::Common::DynamicForwardProxy::MockDnsCacheManager>>
      dns_cache_manager_;
  std::shared_ptr<Extensions::Common::DynamicForwardProxy::MockDnsCache> dns_cache_;
  NiceMock<Upstream::MockClusterManager> cm_{};
  ConfiguratorSharedPtr configurator_;
};

TEST_F(ConfiguratorTest, SetPreferredNetworkWithNewNetworkChangesConfigurationKey) {
  envoy_netconf_t original_key = configurator_->getConfigurationKey();
  envoy_netconf_t new_key = Configurator::setPreferredNetwork(ENVOY_NET_WWAN);
  EXPECT_NE(original_key, new_key);
  EXPECT_EQ(new_key, configurator_->getConfigurationKey());
}

TEST_F(ConfiguratorTest,
       DISABLED_SetPreferredNetworkWithUnchangedNetworkReturnsStaleConfigurationKey) {
  envoy_netconf_t original_key = configurator_->getConfigurationKey();
  envoy_netconf_t stale_key = Configurator::setPreferredNetwork(ENVOY_NET_WLAN);
  EXPECT_NE(original_key, stale_key);
  EXPECT_EQ(original_key, configurator_->getConfigurationKey());
}

TEST_F(ConfiguratorTest, RefreshDnsForCurrentConfigurationTriggersDnsRefresh) {
  EXPECT_CALL(*dns_cache_, forceRefreshHosts());
  envoy_netconf_t configuration_key = configurator_->getConfigurationKey();
  configurator_->refreshDns(configuration_key, false);
}

TEST_F(ConfiguratorTest, RefreshDnsForStaleConfigurationDoesntTriggerDnsRefresh) {
  EXPECT_CALL(*dns_cache_, forceRefreshHosts()).Times(0);
  envoy_netconf_t configuration_key = configurator_->getConfigurationKey();
  configurator_->refreshDns(configuration_key - 1, false);
}

TEST_F(ConfiguratorTest, WhenDrainPostDnsRefreshEnabledDrainsPostDnsRefresh) {
  EXPECT_CALL(*dns_cache_, addUpdateCallbacks_(Ref(*configurator_)));
  configurator_->setDrainPostDnsRefreshEnabled(true);

  auto host_info = std::make_shared<Extensions::Common::DynamicForwardProxy::MockDnsHostInfo>();
  EXPECT_CALL(*dns_cache_, iterateHostMap(_))
      .WillOnce(
          Invoke([&](Extensions::Common::DynamicForwardProxy::DnsCache::IterateHostMapCb callback) {
            callback("cached.example.com", host_info);
            callback("cached2.example.com", host_info);
          }));

  EXPECT_CALL(*dns_cache_, forceRefreshHosts());
  envoy_netconf_t configuration_key = configurator_->getConfigurationKey();
  configurator_->refreshDns(configuration_key, true);

  EXPECT_CALL(cm_, drainConnections(_));
  configurator_->onDnsResolutionComplete(
      "cached.example.com",
      std::make_shared<Extensions::Common::DynamicForwardProxy::MockDnsHostInfo>(),
      Network::DnsResolver::ResolutionStatus::Success);
  configurator_->onDnsResolutionComplete(
      "not-cached.example.com",<commit_msg>test: refine connection drain test (#2245)<commit_after>#include <net/if.h>

#include "test/extensions/common/dynamic_forward_proxy/mocks.h"
#include "test/mocks/upstream/cluster_manager.h"

#include "gtest/gtest.h"
#include "library/common/network/configurator.h"

using testing::_;
using testing::Ref;
using testing::Return;

namespace Envoy {
namespace Network {

class ConfiguratorTest : public testing::Test {
public:
  ConfiguratorTest()
      : dns_cache_manager_(
            new NiceMock<Extensions::Common::DynamicForwardProxy::MockDnsCacheManager>()),
        dns_cache_(dns_cache_manager_->dns_cache_),
        configurator_(std::make_shared<Configurator>(cm_, dns_cache_manager_)) {
    ON_CALL(*dns_cache_manager_, lookUpCacheByName(_)).WillByDefault(Return(dns_cache_));
    // Toggle network to reset network state.
    Configurator::setPreferredNetwork(ENVOY_NET_GENERIC);
    Configurator::setPreferredNetwork(ENVOY_NET_WLAN);
  }

  std::shared_ptr<NiceMock<Extensions::Common::DynamicForwardProxy::MockDnsCacheManager>>
      dns_cache_manager_;
  std::shared_ptr<Extensions::Common::DynamicForwardProxy::MockDnsCache> dns_cache_;
  NiceMock<Upstream::MockClusterManager> cm_{};
  ConfiguratorSharedPtr configurator_;
};

TEST_F(ConfiguratorTest, SetPreferredNetworkWithNewNetworkChangesConfigurationKey) {
  envoy_netconf_t original_key = configurator_->getConfigurationKey();
  envoy_netconf_t new_key = Configurator::setPreferredNetwork(ENVOY_NET_WWAN);
  EXPECT_NE(original_key, new_key);
  EXPECT_EQ(new_key, configurator_->getConfigurationKey());
}

TEST_F(ConfiguratorTest,
       DISABLED_SetPreferredNetworkWithUnchangedNetworkReturnsStaleConfigurationKey) {
  envoy_netconf_t original_key = configurator_->getConfigurationKey();
  envoy_netconf_t stale_key = Configurator::setPreferredNetwork(ENVOY_NET_WLAN);
  EXPECT_NE(original_key, stale_key);
  EXPECT_EQ(original_key, configurator_->getConfigurationKey());
}

TEST_F(ConfiguratorTest, RefreshDnsForCurrentConfigurationTriggersDnsRefresh) {
  EXPECT_CALL(*dns_cache_, forceRefreshHosts());
  envoy_netconf_t configuration_key = configurator_->getConfigurationKey();
  configurator_->refreshDns(configuration_key, false);
}

TEST_F(ConfiguratorTest, RefreshDnsForStaleConfigurationDoesntTriggerDnsRefresh) {
  EXPECT_CALL(*dns_cache_, forceRefreshHosts()).Times(0);
  envoy_netconf_t configuration_key = configurator_->getConfigurationKey();
  configurator_->refreshDns(configuration_key - 1, false);
}

TEST_F(ConfiguratorTest, WhenDrainPostDnsRefreshEnabledDrainsPostDnsRefresh) {
  EXPECT_CALL(*dns_cache_, addUpdateCallbacks_(Ref(*configurator_)));
  configurator_->setDrainPostDnsRefreshEnabled(true);

  auto host_info = std::make_shared<Extensions::Common::DynamicForwardProxy::MockDnsHostInfo>();
  EXPECT_CALL(*dns_cache_, iterateHostMap(_))
      .WillOnce(
          Invoke([&](Extensions::Common::DynamicForwardProxy::DnsCache::IterateHostMapCb callback) {
            callback("cached.example.com", host_info);
            callback("cached2.example.com", host_info);
            callback("cached3.example.com", host_info);
          }));

  EXPECT_CALL(*dns_cache_, forceRefreshHosts());
  envoy_netconf_t configuration_key = configurator_->getConfigurationKey();
  configurator_->refreshDns(configuration_key, true);

  EXPECT_CALL(cm_, drainConnections(_));
  configurator_->onDnsResolutionComplete(
      "cached.example.com",
      std::make_shared<Extensions::Common::DynamicForwardProxy::MockDnsHostInfo>(),
      Network::DnsResolver::ResolutionStatus::Success);
  configurator_->onDnsResolutionComplete(
      "not-cached.example.com",
      std::make_shared<Extensions::Common::DynamicForwardProxy::MockDnsHostInfo>(),
      Network::DnsResolver::ResolutionStatus::Success);
  configurator_->onDnsResolutionComplete(
      "not-cached2.example.com",
{'fraction_non_alphanumeric': 0.11120107962213226, 'fraction_numerical': 0.0016194331983805667, 'mean_word_length': 7.498853211009174, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6600774', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2325, 'n_words': 260}