File: system_dns_config_change_notifier_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (329 lines) | stat: -rw-r--r-- 11,253 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// 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.

#include "net/dns/system_dns_config_change_notifier.h"

#include <utility>
#include <vector>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/dns/dns_hosts.h"
#include "net/dns/test_dns_config_service.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {
const std::vector<IPEndPoint> kNameservers = {
    IPEndPoint(IPAddress(1, 2, 3, 4), 95)};
const std::vector<IPEndPoint> kNameservers2 = {
    IPEndPoint(IPAddress(2, 3, 4, 5), 195)};
const DnsConfig kConfig(kNameservers);
const DnsConfig kConfig2(kNameservers2);
}  // namespace

class SystemDnsConfigChangeNotifierTest : public TestWithTaskEnvironment {
 public:
  // Set up a change notifier, owned on a dedicated blockable task runner, with
  // a faked underlying DnsConfigService.
  SystemDnsConfigChangeNotifierTest()
      : notifier_task_runner_(
            base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})) {
    auto test_service = std::make_unique<TestDnsConfigService>();
    notifier_task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&TestDnsConfigService::OnHostsRead,
                       base::Unretained(test_service.get()), DnsHosts()));
    test_config_service_ = test_service.get();

    notifier_ = std::make_unique<SystemDnsConfigChangeNotifier>(
        notifier_task_runner_, std::move(test_service));
  }

 protected:
  // Test observer implementation that records all notifications received in a
  // vector, and also validates that all notifications are received on the
  // expected sequence.
  class TestObserver : public SystemDnsConfigChangeNotifier::Observer {
   public:
    void OnSystemDnsConfigChanged(absl::optional<DnsConfig> config) override {
      DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
      configs_received_.push_back(std::move(config));

      DCHECK_GT(notifications_remaining_, 0);
      if (--notifications_remaining_ == 0)
        run_loop_->Quit();
    }

    void WaitForNotification() { WaitForNotifications(1); }
    void WaitForNotifications(int num_notifications) {
      DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

      notifications_remaining_ = num_notifications;
      run_loop_->Run();
      run_loop_ = std::make_unique<base::RunLoop>();
    }

    void ExpectNoMoreNotifications() {
      DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
      configs_received_.clear();
      base::RunLoop().RunUntilIdle();
      EXPECT_TRUE(configs_received_.empty());
    }

    std::vector<absl::optional<DnsConfig>>& configs_received() {
      return configs_received_;
    }

   private:
    int notifications_remaining_ = 0;
    std::unique_ptr<base::RunLoop> run_loop_ =
        std::make_unique<base::RunLoop>();
    std::vector<absl::optional<DnsConfig>> configs_received_;
    SEQUENCE_CHECKER(sequence_checker_);
  };

  // Load a config and wait for it to be received by the notifier.
  void LoadConfig(const DnsConfig& config, bool already_loaded = false) {
    TestObserver observer;
    notifier_->AddObserver(&observer);

    // If |notifier_| already has a config loaded, |observer| will first get a
    // notification for that initial config.
    if (already_loaded)
      observer.WaitForNotification();

    notifier_task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&TestDnsConfigService::OnConfigRead,
                       base::Unretained(test_config_service_), config));
    observer.WaitForNotification();

    notifier_->RemoveObserver(&observer);
  }

  scoped_refptr<base::SequencedTaskRunner> notifier_task_runner_;
  std::unique_ptr<SystemDnsConfigChangeNotifier> notifier_;
  // Owned by |notifier_|.
  raw_ptr<TestDnsConfigService> test_config_service_;
};

TEST_F(SystemDnsConfigChangeNotifierTest, ReceiveNotification) {
  TestObserver observer;

  notifier_->AddObserver(&observer);
  notifier_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&TestDnsConfigService::OnConfigRead,
                     base::Unretained(test_config_service_), kConfig));
  observer.WaitForNotification();

  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig)));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

TEST_F(SystemDnsConfigChangeNotifierTest, ReceiveNotification_Multiple) {
  TestObserver observer;

  notifier_->AddObserver(&observer);
  notifier_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&TestDnsConfigService::OnConfigRead,
                     base::Unretained(test_config_service_), kConfig));
  notifier_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&TestDnsConfigService::OnConfigRead,
                     base::Unretained(test_config_service_), kConfig2));
  observer.WaitForNotifications(2);

  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig),
                                   testing::Optional(kConfig2)));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

// If the notifier already has a config loaded, a new observer should receive an
// initial notification for that config.
TEST_F(SystemDnsConfigChangeNotifierTest, ReceiveInitialNotification) {
  LoadConfig(kConfig);

  TestObserver observer;
  notifier_->AddObserver(&observer);
  observer.WaitForNotification();

  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig)));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

// If multiple configs have been read before adding an Observer, should notify
// it only of the most recent.
TEST_F(SystemDnsConfigChangeNotifierTest, ReceiveInitialNotification_Multiple) {
  LoadConfig(kConfig);
  LoadConfig(kConfig2, true /* already_loaded */);

  TestObserver observer;
  notifier_->AddObserver(&observer);
  observer.WaitForNotification();

  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig2)));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

TEST_F(SystemDnsConfigChangeNotifierTest, NotificationsStopAfterRemoval) {
  TestObserver observer;
  notifier_->AddObserver(&observer);
  notifier_->RemoveObserver(&observer);

  LoadConfig(kConfig);
  LoadConfig(kConfig2, true /* already_loaded */);

  EXPECT_TRUE(observer.configs_received().empty());
  observer.ExpectNoMoreNotifications();
}

TEST_F(SystemDnsConfigChangeNotifierTest, UnchangedConfigs) {
  LoadConfig(kConfig);

  TestObserver observer;
  notifier_->AddObserver(&observer);
  observer.WaitForNotification();

  // Expect no notifications from duplicate configs.
  notifier_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&TestDnsConfigService::OnConfigRead,
                     base::Unretained(test_config_service_), kConfig));
  notifier_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&TestDnsConfigService::OnConfigRead,
                     base::Unretained(test_config_service_), kConfig));
  observer.ExpectNoMoreNotifications();

  // Notification on new config.
  notifier_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&TestDnsConfigService::OnConfigRead,
                     base::Unretained(test_config_service_), kConfig2));
  observer.WaitForNotification();
  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig2)));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

TEST_F(SystemDnsConfigChangeNotifierTest, UnloadedConfig) {
  LoadConfig(kConfig);

  TestObserver observer;
  notifier_->AddObserver(&observer);
  // Initial config.
  observer.WaitForNotification();

  notifier_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&TestDnsConfigService::InvalidateConfig,
                                base::Unretained(test_config_service_)));
  observer.WaitForNotification();

  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig), absl::nullopt));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

// All invalid configs are considered the same for notifications, so only expect
// a single notification on multiple config invalidations.
TEST_F(SystemDnsConfigChangeNotifierTest, UnloadedConfig_Multiple) {
  LoadConfig(kConfig);

  TestObserver observer;
  notifier_->AddObserver(&observer);
  // Initial config.
  observer.WaitForNotification();

  notifier_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&TestDnsConfigService::InvalidateConfig,
                                base::Unretained(test_config_service_)));
  notifier_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&TestDnsConfigService::InvalidateConfig,
                                base::Unretained(test_config_service_)));
  observer.WaitForNotification();  // Only 1 notification expected.

  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig), absl::nullopt));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

TEST_F(SystemDnsConfigChangeNotifierTest, InitialConfigInvalid) {
  // Add and invalidate a config (using an extra observer to wait for
  // invalidation to complete).
  LoadConfig(kConfig);
  TestObserver setup_observer;
  notifier_->AddObserver(&setup_observer);
  setup_observer.WaitForNotification();
  notifier_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&TestDnsConfigService::InvalidateConfig,
                                base::Unretained(test_config_service_)));
  setup_observer.WaitForNotification();
  notifier_->RemoveObserver(&setup_observer);

  TestObserver observer;
  notifier_->AddObserver(&observer);

  // No notification expected until first valid config.
  observer.ExpectNoMoreNotifications();

  // Notification on new config.
  notifier_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&TestDnsConfigService::OnConfigRead,
                     base::Unretained(test_config_service_), kConfig));
  observer.WaitForNotification();
  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig)));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

TEST_F(SystemDnsConfigChangeNotifierTest, RefreshConfig) {
  test_config_service_->SetConfigForRefresh(kConfig);

  TestObserver observer;
  notifier_->AddObserver(&observer);

  notifier_->RefreshConfig();
  observer.WaitForNotification();

  EXPECT_THAT(observer.configs_received(),
              testing::ElementsAre(testing::Optional(kConfig)));
  observer.ExpectNoMoreNotifications();

  notifier_->RemoveObserver(&observer);
}

}  // namespace net