xref: /aosp_15_r20/external/cronet/net/dns/test_dns_config_service.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DNS_TEST_DNS_CONFIG_SERVICE_H_
6 #define NET_DNS_TEST_DNS_CONFIG_SERVICE_H_
7 
8 #include <memory>
9 #include <optional>
10 #include <utility>
11 
12 #include "base/check.h"
13 #include "base/functional/callback.h"
14 #include "net/base/net_export.h"
15 #include "net/dns/dns_config_service.h"
16 
17 namespace net {
18 
19 class DnsHostsParser;
20 
21 // Simple test implementation of DnsConfigService that will trigger
22 // notifications only on explicitly calling On...() methods.
23 class NET_EXPORT_PRIVATE TestDnsConfigService : public DnsConfigService {
24  public:
25   TestDnsConfigService();
26   ~TestDnsConfigService() override;
27 
ReadConfigNow()28   void ReadConfigNow() override {}
ReadHostsNow()29   void ReadHostsNow() override {}
30   bool StartWatching() override;
31 
32   // Expose the protected methods to this test suite.
InvalidateConfig()33   void InvalidateConfig() { DnsConfigService::InvalidateConfig(); }
34 
InvalidateHosts()35   void InvalidateHosts() { DnsConfigService::InvalidateHosts(); }
36 
OnConfigRead(const DnsConfig & config)37   void OnConfigRead(const DnsConfig& config) {
38     DnsConfigService::OnConfigRead(config);
39   }
40 
OnHostsRead(const DnsHosts & hosts)41   void OnHostsRead(const DnsHosts& hosts) {
42     DnsConfigService::OnHostsRead(hosts);
43   }
44 
45   void RefreshConfig() override;
46 
SetConfigForRefresh(DnsConfig config)47   void SetConfigForRefresh(DnsConfig config) {
48     DCHECK(!config_for_refresh_);
49     config_for_refresh_ = std::move(config);
50   }
51 
52  private:
53   std::optional<DnsConfig> config_for_refresh_;
54 };
55 
56 // Test implementation of `DnsConfigService` that exercises the
57 // `DnsConfigService::HostsReader`. Uses an injected `DnsHostsParser`. `Watcher`
58 // change notifications are simulated using `TriggerHostsChangeNotification()`.
59 class NET_EXPORT_PRIVATE HostsReadingTestDnsConfigService
60     : public TestDnsConfigService {
61  public:
62   using HostsParserFactory =
63       base::RepeatingCallback<std::unique_ptr<DnsHostsParser>(void)>;
64 
65   explicit HostsReadingTestDnsConfigService(
66       HostsParserFactory hosts_parser_factory);
67   ~HostsReadingTestDnsConfigService() override;
68 
69   // Simulate a `Watcher` change notification for the HOSTS file.
TriggerHostsChangeNotification(bool success)70   void TriggerHostsChangeNotification(bool success) {
71     watcher_->TriggerHostsChangeNotification(success);
72   }
73 
74   // DnsConfigService:
75   void ReadHostsNow() override;
76   bool StartWatching() override;
77 
78  private:
79   class HostsReader : public DnsConfigService::HostsReader {
80    public:
81     HostsReader(TestDnsConfigService& service,
82                 HostsParserFactory hosts_parser_factory);
83     ~HostsReader() override;
84 
85     // DnsConfigService::HostsReader:
86     std::unique_ptr<SerialWorker::WorkItem> CreateWorkItem() override;
87 
88    private:
89     HostsParserFactory hosts_parser_factory_;
90   };
91 
92   class NET_EXPORT_PRIVATE Watcher : public DnsConfigService::Watcher {
93    public:
94     explicit Watcher(DnsConfigService& service);
95     ~Watcher() override;
96 
97     void TriggerHostsChangeNotification(bool success);
98 
99     // DnsConfigService::Watcher:
100     bool Watch() override;
101 
102    private:
103     bool watch_started_ = false;
104   };
105 
106   std::unique_ptr<HostsReader> hosts_reader_;
107   std::unique_ptr<Watcher> watcher_ = std::make_unique<Watcher>(*this);
108 };
109 
110 }  // namespace net
111 
112 #endif  // NET_DNS_TEST_DNS_CONFIG_SERVICE_H_
113