1 // Copyright (c) 2012 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.
5 #include "net/dns/dns_hosts.h"
7 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(DnsHostsTest
, ParseHosts
) {
14 std::string contents
=
15 "127.0.0.1 localhost\tlocalhost.localdomain # standard\n"
17 "1.0.0.1 localhost # ignored, first hit above\n"
18 "fe00::x example company # ignored, malformed IPv6\n"
19 "1.0.0.300 company # ignored, malformed IPv4\n"
20 "1.0.0.1 # ignored, missing hostname\n"
21 "1.0.0.1\t CoMpANy # normalized to 'company' \n"
22 "::1\tlocalhost ip6-localhost ip6-loopback # comment # within a comment\n"
23 "\t fe00::0 ip6-localnet\r\n"
25 "2048::1 company example # ignored for 'example' \n"
27 "127.0.0.1 cache2 # should reuse parsed IP\n"
28 "256.0.0.0 cache3 # bogus IP should not clear parsed IP cache\n"
29 "127.0.0.1 cache4 # should still be reused\n"
38 { "localhost", ADDRESS_FAMILY_IPV4
, "127.0.0.1" },
39 { "localhost.localdomain", ADDRESS_FAMILY_IPV4
, "127.0.0.1" },
40 { "company", ADDRESS_FAMILY_IPV4
, "1.0.0.1" },
41 { "localhost", ADDRESS_FAMILY_IPV6
, "::1" },
42 { "ip6-localhost", ADDRESS_FAMILY_IPV6
, "::1" },
43 { "ip6-loopback", ADDRESS_FAMILY_IPV6
, "::1" },
44 { "ip6-localnet", ADDRESS_FAMILY_IPV6
, "fe00::0" },
45 { "company", ADDRESS_FAMILY_IPV6
, "2048::1" },
46 { "example", ADDRESS_FAMILY_IPV6
, "2048::2" },
47 { "cache1", ADDRESS_FAMILY_IPV4
, "127.0.0.1" },
48 { "cache2", ADDRESS_FAMILY_IPV4
, "127.0.0.1" },
49 { "cache4", ADDRESS_FAMILY_IPV4
, "127.0.0.1" },
50 { "cache5", ADDRESS_FAMILY_IPV4
, "127.0.0.2" },
54 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(entries
); ++i
) {
55 DnsHostsKey
key(entries
[i
].host
, entries
[i
].family
);
56 IPAddressNumber
& ip
= expected
[key
];
57 ASSERT_TRUE(ip
.empty());
58 ASSERT_TRUE(ParseIPLiteralToNumber(entries
[i
].ip
, &ip
));
59 ASSERT_EQ(ip
.size(), (entries
[i
].family
== ADDRESS_FAMILY_IPV4
) ? 4u : 16u);
63 ParseHosts(contents
, &hosts
);
64 ASSERT_EQ(expected
, hosts
);
67 TEST(DnsHostsTest
, HostsParser_Empty
) {
69 ParseHosts("", &hosts
);
70 EXPECT_EQ(0u, hosts
.size());
73 TEST(DnsHostsTest
, HostsParser_OnlyWhitespace
) {
75 ParseHosts(" ", &hosts
);
76 EXPECT_EQ(0u, hosts
.size());
79 TEST(DnsHostsTest
, HostsParser_EndsWithNothing
) {
81 ParseHosts("127.0.0.1 localhost", &hosts
);
82 EXPECT_EQ(1u, hosts
.size());
85 TEST(DnsHostsTest
, HostsParser_EndsWithWhitespace
) {
87 ParseHosts("127.0.0.1 localhost ", &hosts
);
88 EXPECT_EQ(1u, hosts
.size());
91 TEST(DnsHostsTest
, HostsParser_EndsWithComment
) {
93 ParseHosts("127.0.0.1 localhost # comment", &hosts
);
94 EXPECT_EQ(1u, hosts
.size());
97 TEST(DnsHostsTest
, HostsParser_EndsWithNewline
) {
99 ParseHosts("127.0.0.1 localhost\n", &hosts
);
100 EXPECT_EQ(1u, hosts
.size());
103 TEST(DnsHostsTest
, HostsParser_EndsWithTwoNewlines
) {
105 ParseHosts("127.0.0.1 localhost\n\n", &hosts
);
106 EXPECT_EQ(1u, hosts
.size());
109 TEST(DnsHostsTest
, HostsParser_EndsWithNewlineAndWhitespace
) {
111 ParseHosts("127.0.0.1 localhost\n ", &hosts
);
112 EXPECT_EQ(1u, hosts
.size());
115 TEST(DnsHostsTest
, HostsParser_EndsWithNewlineAndToken
) {
117 ParseHosts("127.0.0.1 localhost\ntoken", &hosts
);
118 EXPECT_EQ(1u, hosts
.size());