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. 4 5 #include "net/dns/dns_hosts.h" 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 namespace net { 10 11 namespace { 12 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"; 32 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" }, 51 }; 52 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); 60 } 61 62 DnsHosts hosts; 63 ParseHosts(contents, &hosts); 64 ASSERT_EQ(expected, hosts); 65 } 66 67 TEST(DnsHostsTest, HostsParser_Empty) { 68 DnsHosts hosts; 69 ParseHosts("", &hosts); 70 EXPECT_EQ(0u, hosts.size()); 71 } 72 73 TEST(DnsHostsTest, HostsParser_OnlyWhitespace) { 74 DnsHosts hosts; 75 ParseHosts(" ", &hosts); 76 EXPECT_EQ(0u, hosts.size()); 77 } 78 79 TEST(DnsHostsTest, HostsParser_EndsWithNothing) { 80 DnsHosts hosts; 81 ParseHosts("127.0.0.1 localhost", &hosts); 82 EXPECT_EQ(1u, hosts.size()); 83 } 84 85 TEST(DnsHostsTest, HostsParser_EndsWithWhitespace) { 86 DnsHosts hosts; 87 ParseHosts("127.0.0.1 localhost ", &hosts); 88 EXPECT_EQ(1u, hosts.size()); 89 } 90 91 TEST(DnsHostsTest, HostsParser_EndsWithComment) { 92 DnsHosts hosts; 93 ParseHosts("127.0.0.1 localhost # comment", &hosts); 94 EXPECT_EQ(1u, hosts.size()); 95 } 96 97 TEST(DnsHostsTest, HostsParser_EndsWithNewline) { 98 DnsHosts hosts; 99 ParseHosts("127.0.0.1 localhost\n", &hosts); 100 EXPECT_EQ(1u, hosts.size()); 101 } 102 103 TEST(DnsHostsTest, HostsParser_EndsWithTwoNewlines) { 104 DnsHosts hosts; 105 ParseHosts("127.0.0.1 localhost\n\n", &hosts); 106 EXPECT_EQ(1u, hosts.size()); 107 } 108 109 TEST(DnsHostsTest, HostsParser_EndsWithNewlineAndWhitespace) { 110 DnsHosts hosts; 111 ParseHosts("127.0.0.1 localhost\n ", &hosts); 112 EXPECT_EQ(1u, hosts.size()); 113 } 114 115 TEST(DnsHostsTest, HostsParser_EndsWithNewlineAndToken) { 116 DnsHosts hosts; 117 ParseHosts("127.0.0.1 localhost\ntoken", &hosts); 118 EXPECT_EQ(1u, hosts.size()); 119 } 120 121 } // namespace 122 123 } // namespace net 124 125