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 
5 #include "services/network/dns_config_change_manager.h"
6 
7 #include <climits>
8 #include <utility>
9 
10 #include "base/run_loop.h"
11 #include "base/test/task_environment.h"
12 #include "mojo/public/cpp/bindings/receiver.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace network {
16 namespace {
17 
18 class TestDnsConfigChangeManagerClient
19     : public mojom::DnsConfigChangeManagerClient {
20  public:
TestDnsConfigChangeManagerClient(DnsConfigChangeManager * manager)21   explicit TestDnsConfigChangeManagerClient(DnsConfigChangeManager* manager) {
22     mojo::Remote<mojom::DnsConfigChangeManager> manager_remote;
23     manager->AddReceiver(manager_remote.BindNewPipeAndPassReceiver());
24     manager_remote->RequestNotifications(receiver_.BindNewPipeAndPassRemote());
25   }
26 
OnDnsConfigChanged()27   void OnDnsConfigChanged() override {
28     num_notifications_++;
29     if (num_notifications_ >= num_notifications_expected_)
30       run_loop_.Quit();
31   }
32 
num_notifications()33   int num_notifications() { return num_notifications_; }
34 
WaitForNotification(int num_notifications_expected)35   void WaitForNotification(int num_notifications_expected) {
36     num_notifications_expected_ = num_notifications_expected;
37     if (num_notifications_ < num_notifications_expected_)
38       run_loop_.Run();
39   }
40 
41  private:
42   int num_notifications_ = 0;
43   int num_notifications_expected_ = INT_MAX;
44   base::RunLoop run_loop_;
45   mojo::Receiver<mojom::DnsConfigChangeManagerClient> receiver_{this};
46 
47   DISALLOW_COPY_AND_ASSIGN(TestDnsConfigChangeManagerClient);
48 };
49 
50 class DnsConfigChangeManagerTest : public testing::Test {
51  public:
DnsConfigChangeManagerTest()52   DnsConfigChangeManagerTest() {}
53 
manager()54   DnsConfigChangeManager* manager() { return &manager_; }
client()55   TestDnsConfigChangeManagerClient* client() { return &client_; }
56 
57  private:
58   base::test::TaskEnvironment task_environment_;
59   std::unique_ptr<net::NetworkChangeNotifier> notifier_mock_ =
60       net::NetworkChangeNotifier::CreateMockIfNeeded();
61   DnsConfigChangeManager manager_;
62   TestDnsConfigChangeManagerClient client_{&manager_};
63 
64   DISALLOW_COPY_AND_ASSIGN(DnsConfigChangeManagerTest);
65 };
66 
TEST_F(DnsConfigChangeManagerTest,Notification)67 TEST_F(DnsConfigChangeManagerTest, Notification) {
68   EXPECT_EQ(0, client()->num_notifications());
69 
70   net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
71   client()->WaitForNotification(1);
72   EXPECT_EQ(1, client()->num_notifications());
73 
74   base::RunLoop().RunUntilIdle();
75   EXPECT_EQ(1, client()->num_notifications());
76 }
77 
TEST_F(DnsConfigChangeManagerTest,MultipleNotification)78 TEST_F(DnsConfigChangeManagerTest, MultipleNotification) {
79   EXPECT_EQ(0, client()->num_notifications());
80 
81   net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
82   net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
83   client()->WaitForNotification(2);
84   EXPECT_EQ(2, client()->num_notifications());
85 
86   base::RunLoop().RunUntilIdle();
87   EXPECT_EQ(2, client()->num_notifications());
88 }
89 
TEST_F(DnsConfigChangeManagerTest,MultipleClients)90 TEST_F(DnsConfigChangeManagerTest, MultipleClients) {
91   TestDnsConfigChangeManagerClient client2(manager());
92 
93   EXPECT_EQ(0, client()->num_notifications());
94   EXPECT_EQ(0, client2.num_notifications());
95 
96   net::NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests();
97   client()->WaitForNotification(1);
98   client2.WaitForNotification(1);
99   EXPECT_EQ(1, client()->num_notifications());
100   EXPECT_EQ(1, client2.num_notifications());
101 
102   base::RunLoop().RunUntilIdle();
103   EXPECT_EQ(1, client()->num_notifications());
104   EXPECT_EQ(1, client2.num_notifications());
105 }
106 
107 }  // namespace
108 }  // namespace network
109