1 // Copyright 2009 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 #include "net/dns/dns_util.h"
6
7 #include "base/test/scoped_feature_list.h"
8 #include "net/dns/dns_test_util.h"
9 #include "net/dns/public/dns_over_https_config.h"
10 #include "net/dns/public/dns_protocol.h"
11 #include "net/dns/public/doh_provider_entry.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16
17 namespace {
18 // Returns the DoH provider entry in `DohProviderEntry::GetList()` that matches
19 // `provider`. Crashes if there is no matching entry.
GetDohProviderEntry(base::StringPiece provider)20 const DohProviderEntry& GetDohProviderEntry(base::StringPiece provider) {
21 auto provider_list = DohProviderEntry::GetList();
22 auto it =
23 base::ranges::find(provider_list, provider, &DohProviderEntry::provider);
24 CHECK(it != provider_list.end());
25 return **it;
26 }
27 } // namespace
28
29 class DNSUtilTest : public testing::Test {};
30
TEST_F(DNSUtilTest,GetURLFromTemplateWithoutParameters)31 TEST_F(DNSUtilTest, GetURLFromTemplateWithoutParameters) {
32 EXPECT_EQ("https://dnsserver.example.net/dns-query",
33 GetURLFromTemplateWithoutParameters(
34 "https://dnsserver.example.net/dns-query{?dns}"));
35 }
36
TEST_F(DNSUtilTest,GetDohUpgradeServersFromDotHostname)37 TEST_F(DNSUtilTest, GetDohUpgradeServersFromDotHostname) {
38 std::vector<DnsOverHttpsServerConfig> doh_servers =
39 GetDohUpgradeServersFromDotHostname("");
40 EXPECT_EQ(0u, doh_servers.size());
41
42 doh_servers = GetDohUpgradeServersFromDotHostname("unrecognized");
43 EXPECT_EQ(0u, doh_servers.size());
44
45 doh_servers = GetDohUpgradeServersFromDotHostname(
46 "family-filter-dns.cleanbrowsing.org");
47 EXPECT_EQ(1u, doh_servers.size());
48 EXPECT_EQ("https://doh.cleanbrowsing.org/doh/family-filter{?dns}",
49 doh_servers[0].server_template());
50
51 base::test::ScopedFeatureList scoped_feature_list;
52 scoped_feature_list.InitWithFeatures(
53 /*enabled_features=*/{}, /*disabled_features=*/{
54 GetDohProviderEntry("CleanBrowsingFamily").feature});
55 doh_servers = GetDohUpgradeServersFromDotHostname(
56 "family-filter-dns.cleanbrowsing.org");
57 EXPECT_EQ(0u, doh_servers.size());
58 }
59
TEST_F(DNSUtilTest,GetDohUpgradeServersFromNameservers)60 TEST_F(DNSUtilTest, GetDohUpgradeServersFromNameservers) {
61 std::vector<IPEndPoint> nameservers;
62 // Cloudflare upgradeable IPs
63 IPAddress dns_ip0(1, 0, 0, 1);
64 IPAddress dns_ip1;
65 EXPECT_TRUE(dns_ip1.AssignFromIPLiteral("2606:4700:4700::1111"));
66 // SafeBrowsing family filter upgradeable IP
67 IPAddress dns_ip2;
68 EXPECT_TRUE(dns_ip2.AssignFromIPLiteral("2a0d:2a00:2::"));
69 // SafeBrowsing security filter upgradeable IP
70 IPAddress dns_ip3(185, 228, 169, 9);
71 // None-upgradeable IP
72 IPAddress dns_ip4(1, 2, 3, 4);
73
74 nameservers.emplace_back(dns_ip0, dns_protocol::kDefaultPort);
75 nameservers.emplace_back(dns_ip1, dns_protocol::kDefaultPort);
76 nameservers.emplace_back(dns_ip2, 54);
77 nameservers.emplace_back(dns_ip3, dns_protocol::kDefaultPort);
78 nameservers.emplace_back(dns_ip4, dns_protocol::kDefaultPort);
79
80 std::vector<DnsOverHttpsServerConfig> doh_servers =
81 GetDohUpgradeServersFromNameservers(std::vector<IPEndPoint>());
82 EXPECT_EQ(0u, doh_servers.size());
83
84 doh_servers = GetDohUpgradeServersFromNameservers(nameservers);
85 auto expected_config = *DnsOverHttpsConfig::FromTemplatesForTesting(
86 {"https://chrome.cloudflare-dns.com/dns-query",
87 "https://doh.cleanbrowsing.org/doh/family-filter{?dns}",
88 "https://doh.cleanbrowsing.org/doh/security-filter{?dns}"});
89 EXPECT_EQ(expected_config.servers(), doh_servers);
90
91 base::test::ScopedFeatureList scoped_feature_list;
92 scoped_feature_list.InitWithFeatures(
93 /*enabled_features=*/{},
94 /*disabled_features=*/{GetDohProviderEntry("CleanBrowsingSecure").feature,
95 GetDohProviderEntry("Cloudflare").feature});
96
97 doh_servers = GetDohUpgradeServersFromNameservers(nameservers);
98 EXPECT_THAT(doh_servers,
99 testing::ElementsAre(*DnsOverHttpsServerConfig::FromString(
100 "https://doh.cleanbrowsing.org/doh/family-filter{?dns}")));
101 }
102
TEST_F(DNSUtilTest,GetDohProviderIdForHistogramFromServerConfig)103 TEST_F(DNSUtilTest, GetDohProviderIdForHistogramFromServerConfig) {
104 EXPECT_EQ("Cloudflare",
105 GetDohProviderIdForHistogramFromServerConfig(
106 *DnsOverHttpsServerConfig::FromString(
107 "https://chrome.cloudflare-dns.com/dns-query")));
108 EXPECT_EQ("Other", GetDohProviderIdForHistogramFromServerConfig(
109 *DnsOverHttpsServerConfig::FromString(
110 "https://unexpected.dohserver.com/dns-query")));
111 }
112
TEST_F(DNSUtilTest,GetDohProviderIdForHistogramFromNameserver)113 TEST_F(DNSUtilTest, GetDohProviderIdForHistogramFromNameserver) {
114 EXPECT_EQ("CleanBrowsingSecure",
115 GetDohProviderIdForHistogramFromNameserver(IPEndPoint(
116 IPAddress(185, 228, 169, 9), dns_protocol::kDefaultPort)));
117 EXPECT_EQ("Other", GetDohProviderIdForHistogramFromNameserver(IPEndPoint(
118 IPAddress(1, 2, 3, 4), dns_protocol::kDefaultPort)));
119 }
120
121 } // namespace net
122