test_dns_config_service.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2019 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. #ifndef NET_DNS_TEST_DNS_CONFIG_SERVICE_H_
  5. #define NET_DNS_TEST_DNS_CONFIG_SERVICE_H_
  6. #include <memory>
  7. #include <utility>
  8. #include "base/callback.h"
  9. #include "base/check.h"
  10. #include "net/base/net_export.h"
  11. #include "net/dns/dns_config_service.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace net {
  14. class DnsHostsParser;
  15. // Simple test implementation of DnsConfigService that will trigger
  16. // notifications only on explicitly calling On...() methods.
  17. class NET_EXPORT_PRIVATE TestDnsConfigService : public DnsConfigService {
  18. public:
  19. TestDnsConfigService();
  20. ~TestDnsConfigService() override;
  21. void ReadConfigNow() override {}
  22. void ReadHostsNow() override {}
  23. bool StartWatching() override;
  24. // Expose the protected methods to this test suite.
  25. void InvalidateConfig() { DnsConfigService::InvalidateConfig(); }
  26. void InvalidateHosts() { DnsConfigService::InvalidateHosts(); }
  27. void OnConfigRead(const DnsConfig& config) {
  28. DnsConfigService::OnConfigRead(config);
  29. }
  30. void OnHostsRead(const DnsHosts& hosts) {
  31. DnsConfigService::OnHostsRead(hosts);
  32. }
  33. void RefreshConfig() override;
  34. void SetConfigForRefresh(DnsConfig config) {
  35. DCHECK(!config_for_refresh_);
  36. config_for_refresh_ = std::move(config);
  37. }
  38. private:
  39. absl::optional<DnsConfig> config_for_refresh_;
  40. };
  41. // Test implementation of `DnsConfigService` that exercises the
  42. // `DnsConfigService::HostsReader`. Uses an injected `DnsHostsParser`. `Watcher`
  43. // change notifications are simulated using `TriggerHostsChangeNotification()`.
  44. class NET_EXPORT_PRIVATE HostsReadingTestDnsConfigService
  45. : public TestDnsConfigService {
  46. public:
  47. using HostsParserFactory =
  48. base::RepeatingCallback<std::unique_ptr<DnsHostsParser>(void)>;
  49. explicit HostsReadingTestDnsConfigService(
  50. HostsParserFactory hosts_parser_factory);
  51. ~HostsReadingTestDnsConfigService() override;
  52. // Simulate a `Watcher` change notification for the HOSTS file.
  53. void TriggerHostsChangeNotification(bool success) {
  54. watcher_->TriggerHostsChangeNotification(success);
  55. }
  56. // DnsConfigService:
  57. void ReadHostsNow() override;
  58. bool StartWatching() override;
  59. private:
  60. class HostsReader : public DnsConfigService::HostsReader {
  61. public:
  62. HostsReader(TestDnsConfigService& service,
  63. HostsParserFactory hosts_parser_factory);
  64. ~HostsReader() override;
  65. // DnsConfigService::HostsReader:
  66. std::unique_ptr<SerialWorker::WorkItem> CreateWorkItem() override;
  67. private:
  68. HostsParserFactory hosts_parser_factory_;
  69. };
  70. class NET_EXPORT_PRIVATE Watcher : public DnsConfigService::Watcher {
  71. public:
  72. explicit Watcher(DnsConfigService& service);
  73. ~Watcher() override;
  74. void TriggerHostsChangeNotification(bool success);
  75. // DnsConfigService::Watcher:
  76. bool Watch() override;
  77. private:
  78. bool watch_started_ = false;
  79. };
  80. std::unique_ptr<HostsReader> hosts_reader_;
  81. std::unique_ptr<Watcher> watcher_ = std::make_unique<Watcher>(*this);
  82. };
  83. } // namespace net
  84. #endif // NET_DNS_TEST_DNS_CONFIG_SERVICE_H_