Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: net/dns/dns_hosts_unittest.cc

Issue 415153002: ParseHosts: Allow commas as separators on Mac OS X (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix nits Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/dns/dns_hosts.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/dns_hosts.h" 5 #include "net/dns/dns_hosts.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 namespace net { 9 namespace net {
10 10
11 namespace { 11 namespace {
12 12
13 struct ExpectedHostsEntry {
14 const char* host;
15 AddressFamily family;
16 const char* ip;
17 };
18
19 void PopulateExpectedHosts(const ExpectedHostsEntry* entries,
20 size_t num_entries,
21 DnsHosts* expected_hosts_out) {
22 for (size_t i = 0; i < num_entries; ++i) {
23 DnsHostsKey key(entries[i].host, entries[i].family);
24 IPAddressNumber& ip_ref = (*expected_hosts_out)[key];
25 ASSERT_TRUE(ip_ref.empty());
26 ASSERT_TRUE(ParseIPLiteralToNumber(entries[i].ip, &ip_ref));
27 ASSERT_EQ(ip_ref.size(),
28 (entries[i].family == ADDRESS_FAMILY_IPV4) ? 4u : 16u);
29 }
30 }
31
13 TEST(DnsHostsTest, ParseHosts) { 32 TEST(DnsHostsTest, ParseHosts) {
14 std::string contents = 33 const std::string kContents =
15 "127.0.0.1 localhost\tlocalhost.localdomain # standard\n" 34 "127.0.0.1 localhost\tlocalhost.localdomain # standard\n"
16 "\n" 35 "\n"
17 "1.0.0.1 localhost # ignored, first hit above\n" 36 "1.0.0.1 localhost # ignored, first hit above\n"
18 "fe00::x example company # ignored, malformed IPv6\n" 37 "fe00::x example company # ignored, malformed IPv6\n"
19 "1.0.0.300 company # ignored, malformed IPv4\n" 38 "1.0.0.300 company # ignored, malformed IPv4\n"
20 "1.0.0.1 # ignored, missing hostname\n" 39 "1.0.0.1 # ignored, missing hostname\n"
21 "1.0.0.1\t CoMpANy # normalized to 'company' \n" 40 "1.0.0.1\t CoMpANy # normalized to 'company' \n"
22 "::1\tlocalhost ip6-localhost ip6-loopback # comment # within a comment\n" 41 "::1\tlocalhost ip6-localhost ip6-loopback # comment # within a comment\n"
23 "\t fe00::0 ip6-localnet\r\n" 42 "\t fe00::0 ip6-localnet\r\n"
24 "2048::2 example\n" 43 "2048::2 example\n"
25 "2048::1 company example # ignored for 'example' \n" 44 "2048::1 company example # ignored for 'example' \n"
26 "127.0.0.1 cache1\n" 45 "127.0.0.1 cache1\n"
27 "127.0.0.1 cache2 # should reuse parsed IP\n" 46 "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" 47 "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" 48 "127.0.0.1 cache4 # should still be reused\n"
30 "127.0.0.2 cache5\n" 49 "127.0.0.2 cache5\n"
31 "gibberish"; 50 "gibberish";
32 51
33 const struct { 52 const ExpectedHostsEntry kEntries[] = {
34 const char* host;
35 AddressFamily family;
36 const char* ip;
37 } entries[] = {
38 { "localhost", ADDRESS_FAMILY_IPV4, "127.0.0.1" }, 53 { "localhost", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
39 { "localhost.localdomain", ADDRESS_FAMILY_IPV4, "127.0.0.1" }, 54 { "localhost.localdomain", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
40 { "company", ADDRESS_FAMILY_IPV4, "1.0.0.1" }, 55 { "company", ADDRESS_FAMILY_IPV4, "1.0.0.1" },
41 { "localhost", ADDRESS_FAMILY_IPV6, "::1" }, 56 { "localhost", ADDRESS_FAMILY_IPV6, "::1" },
42 { "ip6-localhost", ADDRESS_FAMILY_IPV6, "::1" }, 57 { "ip6-localhost", ADDRESS_FAMILY_IPV6, "::1" },
43 { "ip6-loopback", ADDRESS_FAMILY_IPV6, "::1" }, 58 { "ip6-loopback", ADDRESS_FAMILY_IPV6, "::1" },
44 { "ip6-localnet", ADDRESS_FAMILY_IPV6, "fe00::0" }, 59 { "ip6-localnet", ADDRESS_FAMILY_IPV6, "fe00::0" },
45 { "company", ADDRESS_FAMILY_IPV6, "2048::1" }, 60 { "company", ADDRESS_FAMILY_IPV6, "2048::1" },
46 { "example", ADDRESS_FAMILY_IPV6, "2048::2" }, 61 { "example", ADDRESS_FAMILY_IPV6, "2048::2" },
47 { "cache1", ADDRESS_FAMILY_IPV4, "127.0.0.1" }, 62 { "cache1", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
48 { "cache2", ADDRESS_FAMILY_IPV4, "127.0.0.1" }, 63 { "cache2", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
49 { "cache4", ADDRESS_FAMILY_IPV4, "127.0.0.1" }, 64 { "cache4", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
50 { "cache5", ADDRESS_FAMILY_IPV4, "127.0.0.2" }, 65 { "cache5", ADDRESS_FAMILY_IPV4, "127.0.0.2" },
51 }; 66 };
52 67
53 DnsHosts expected; 68 DnsHosts expected_hosts, actual_hosts;
54 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(entries); ++i) { 69 PopulateExpectedHosts(kEntries, ARRAYSIZE_UNSAFE(kEntries), &expected_hosts);
55 DnsHostsKey key(entries[i].host, entries[i].family); 70 ParseHosts(kContents, &actual_hosts);
56 IPAddressNumber& ip = expected[key]; 71 ASSERT_EQ(expected_hosts, actual_hosts);
57 ASSERT_TRUE(ip.empty()); 72 }
58 ASSERT_TRUE(ParseIPLiteralToNumber(entries[i].ip, &ip));
59 ASSERT_EQ(ip.size(), (entries[i].family == ADDRESS_FAMILY_IPV4) ? 4u : 16u);
60 }
61 73
62 DnsHosts hosts; 74 TEST(DnsHostsTest, ParseHosts_CommaIsToken) {
63 ParseHosts(contents, &hosts); 75 const std::string kContents = "127.0.0.1 comma1,comma2";
64 ASSERT_EQ(expected, hosts); 76
77 const ExpectedHostsEntry kEntries[] = {
78 { "comma1,comma2", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
79 };
80
81 DnsHosts expected_hosts, actual_hosts;
82 PopulateExpectedHosts(kEntries, ARRAYSIZE_UNSAFE(kEntries), &expected_hosts);
83 ParseHostsWithCommaModeForTesting(
84 kContents, &actual_hosts, PARSE_HOSTS_COMMA_IS_TOKEN);
85 ASSERT_EQ(expected_hosts, actual_hosts);
86 }
87
88 TEST(DnsHostsTest, ParseHosts_CommaIsWhitespace) {
89 std::string kContents = "127.0.0.1 comma1,comma2";
90
91 const ExpectedHostsEntry kEntries[] = {
92 { "comma1", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
93 { "comma2", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
94 };
95
96 DnsHosts expected_hosts, actual_hosts;
97 PopulateExpectedHosts(kEntries, ARRAYSIZE_UNSAFE(kEntries), &expected_hosts);
98 ParseHostsWithCommaModeForTesting(
99 kContents, &actual_hosts, PARSE_HOSTS_COMMA_IS_WHITESPACE);
100 ASSERT_EQ(expected_hosts, actual_hosts);
101 }
102
103 // Test that the right comma mode is used on each platform.
104 TEST(DnsHostsTest, ParseHosts_CommaModeByPlatform) {
105 std::string kContents = "127.0.0.1 comma1,comma2";
106
107 #if defined(OS_MACOSX)
108 const ExpectedHostsEntry kEntries[] = {
109 { "comma1", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
110 { "comma2", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
111 };
112 #else
113 const ExpectedHostsEntry kEntries[] = {
114 { "comma1,comma2", ADDRESS_FAMILY_IPV4, "127.0.0.1" },
115 };
116 #endif
117
118 DnsHosts expected_hosts, actual_hosts;
119 PopulateExpectedHosts(kEntries, ARRAYSIZE_UNSAFE(kEntries), &expected_hosts);
120 ParseHosts(kContents, &actual_hosts);
121 ASSERT_EQ(expected_hosts, actual_hosts);
65 } 122 }
66 123
67 TEST(DnsHostsTest, HostsParser_Empty) { 124 TEST(DnsHostsTest, HostsParser_Empty) {
68 DnsHosts hosts; 125 DnsHosts hosts;
69 ParseHosts("", &hosts); 126 ParseHosts("", &hosts);
70 EXPECT_EQ(0u, hosts.size()); 127 EXPECT_EQ(0u, hosts.size());
71 } 128 }
72 129
73 TEST(DnsHostsTest, HostsParser_OnlyWhitespace) { 130 TEST(DnsHostsTest, HostsParser_OnlyWhitespace) {
74 DnsHosts hosts; 131 DnsHosts hosts;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 TEST(DnsHostsTest, HostsParser_EndsWithNewlineAndToken) { 172 TEST(DnsHostsTest, HostsParser_EndsWithNewlineAndToken) {
116 DnsHosts hosts; 173 DnsHosts hosts;
117 ParseHosts("127.0.0.1 localhost\ntoken", &hosts); 174 ParseHosts("127.0.0.1 localhost\ntoken", &hosts);
118 EXPECT_EQ(1u, hosts.size()); 175 EXPECT_EQ(1u, hosts.size());
119 } 176 }
120 177
121 } // namespace 178 } // namespace
122 179
123 } // namespace net 180 } // namespace net
124 181
OLDNEW
« no previous file with comments | « net/dns/dns_hosts.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698