dns_config_change_manager_unittest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/network/dns_config_change_manager.h"
  5. #include <climits>
  6. #include <utility>
  7. #include "base/run_loop.h"
  8. #include "base/test/task_environment.h"
  9. #include "mojo/public/cpp/bindings/receiver.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace network {
  12. namespace {
  13. class TestDnsConfigChangeManagerClient
  14. : public mojom::DnsConfigChangeManagerClient {
  15. public:
  16. explicit TestDnsConfigChangeManagerClient(DnsConfigChangeManager* manager) {
  17. mojo::Remote<mojom::DnsConfigChangeManager> manager_remote;
  18. manager->AddReceiver(manager_remote.BindNewPipeAndPassReceiver());
  19. manager_remote->RequestNotifications(receiver_.BindNewPipeAndPassRemote());
  20. }
  21. TestDnsConfigChangeManagerClient(const TestDnsConfigChangeManagerClient&) =
  22. delete;
  23. TestDnsConfigChangeManagerClient& operator=(
  24. const TestDnsConfigChangeManagerClient&) = delete;
  25. void OnDnsConfigChanged() override {
  26. num_notifications_++;
  27. if (num_notifications_ >= num_notifications_expected_)
  28. run_loop_.Quit();
  29. }
  30. int num_notifications() { return num_notifications_; }
  31. void WaitForNotification(int num_notifications_expected) {
  32. num_notifications_expected_ = num_notifications_expected;
  33. if (num_notifications_ < num_notifications_expected_)
  34. run_loop_.Run();
  35. }
  36. private:
  37. int num_notifications_ = 0;
  38. int num_notifications_expected_ = INT_MAX;
  39. base::RunLoop run_loop_;
  40. mojo::Receiver<mojom::DnsConfigChangeManagerClient> receiver_{this};
  41. };
  42. class DnsConfigChangeManagerTest : public testing::Test {
  43. public:
  44. DnsConfigChangeManagerTest() {}
  45. DnsConfigChangeManagerTest(const DnsConfigChangeManagerTest&) = delete;
  46. DnsConfigChangeManagerTest& operator=(const DnsConfigChangeManagerTest&) =
  47. delete;
  48. DnsConfigChangeManager* manager() { return &manager_; }
  49. TestDnsConfigChangeManagerClient* client() { return &client_; }
  50. private:
  51. base::test::TaskEnvironment task_environment_;
  52. std::unique_ptr<net::NetworkChangeNotifier> notifier_mock_ =
  53. net::NetworkChangeNotifier::CreateMockIfNeeded();
  54. DnsConfigChangeManager manager_;
  55. TestDnsConfigChangeManagerClient client_{&manager_};
  56. };
  57. TEST_F(DnsConfigChangeManagerTest, Notification) {
  58. EXPECT_EQ(0, client()->num_notifications());
  59. net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
  60. client()->WaitForNotification(1);
  61. EXPECT_EQ(1, client()->num_notifications());
  62. base::RunLoop().RunUntilIdle();
  63. EXPECT_EQ(1, client()->num_notifications());
  64. }
  65. TEST_F(DnsConfigChangeManagerTest, MultipleNotification) {
  66. EXPECT_EQ(0, client()->num_notifications());
  67. net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
  68. net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
  69. client()->WaitForNotification(2);
  70. EXPECT_EQ(2, client()->num_notifications());
  71. base::RunLoop().RunUntilIdle();
  72. EXPECT_EQ(2, client()->num_notifications());
  73. }
  74. TEST_F(DnsConfigChangeManagerTest, MultipleClients) {
  75. TestDnsConfigChangeManagerClient client2(manager());
  76. EXPECT_EQ(0, client()->num_notifications());
  77. EXPECT_EQ(0, client2.num_notifications());
  78. net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
  79. client()->WaitForNotification(1);
  80. client2.WaitForNotification(1);
  81. EXPECT_EQ(1, client()->num_notifications());
  82. EXPECT_EQ(1, client2.num_notifications());
  83. base::RunLoop().RunUntilIdle();
  84. EXPECT_EQ(1, client()->num_notifications());
  85. EXPECT_EQ(1, client2.num_notifications());
  86. }
  87. } // namespace
  88. } // namespace network