Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / dns / dns_hosts_unittest.cc
blobc0e8805fc47dfdf97de79e6c48746650fb27e811
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"
9 namespace net {
11 namespace {
13 TEST(DnsHostsTest, ParseHosts) {
14 std::string contents =
15 "127.0.0.1 localhost\tlocalhost.localdomain # standard\n"
16 "\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"
24 "2048::2 example\n"
25 "2048::1 company example # ignored for 'example' \n"
26 "127.0.0.1 cache1\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"
30 "127.0.0.2 cache5\n"
31 "gibberish";
33 const struct {
34 const char* host;
35 AddressFamily family;
36 const char* ip;
37 } entries[] = {
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" },
53 DnsHosts expected;
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);
62 DnsHosts hosts;
63 ParseHosts(contents, &hosts);
64 ASSERT_EQ(expected, hosts);
67 TEST(DnsHostsTest, HostsParser_Empty) {
68 DnsHosts hosts;
69 ParseHosts("", &hosts);
70 EXPECT_EQ(0u, hosts.size());
73 TEST(DnsHostsTest, HostsParser_OnlyWhitespace) {
74 DnsHosts hosts;
75 ParseHosts(" ", &hosts);
76 EXPECT_EQ(0u, hosts.size());
79 TEST(DnsHostsTest, HostsParser_EndsWithNothing) {
80 DnsHosts hosts;
81 ParseHosts("127.0.0.1 localhost", &hosts);
82 EXPECT_EQ(1u, hosts.size());
85 TEST(DnsHostsTest, HostsParser_EndsWithWhitespace) {
86 DnsHosts hosts;
87 ParseHosts("127.0.0.1 localhost ", &hosts);
88 EXPECT_EQ(1u, hosts.size());
91 TEST(DnsHostsTest, HostsParser_EndsWithComment) {
92 DnsHosts hosts;
93 ParseHosts("127.0.0.1 localhost # comment", &hosts);
94 EXPECT_EQ(1u, hosts.size());
97 TEST(DnsHostsTest, HostsParser_EndsWithNewline) {
98 DnsHosts hosts;
99 ParseHosts("127.0.0.1 localhost\n", &hosts);
100 EXPECT_EQ(1u, hosts.size());
103 TEST(DnsHostsTest, HostsParser_EndsWithTwoNewlines) {
104 DnsHosts hosts;
105 ParseHosts("127.0.0.1 localhost\n\n", &hosts);
106 EXPECT_EQ(1u, hosts.size());
109 TEST(DnsHostsTest, HostsParser_EndsWithNewlineAndWhitespace) {
110 DnsHosts hosts;
111 ParseHosts("127.0.0.1 localhost\n ", &hosts);
112 EXPECT_EQ(1u, hosts.size());
115 TEST(DnsHostsTest, HostsParser_EndsWithNewlineAndToken) {
116 DnsHosts hosts;
117 ParseHosts("127.0.0.1 localhost\ntoken", &hosts);
118 EXPECT_EQ(1u, hosts.size());
121 } // namespace
123 } // namespace net