| OLD | NEW |
| 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/mapped_host_resolver.h" | 5 #include "net/dns/mapped_host_resolver.h" |
| 6 | 6 |
| 7 #include "net/base/address_list.h" | 7 #include "net/base/address_list.h" |
| 8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 9 #include "net/base/net_log.h" | 9 #include "net/base/net_log.h" |
| 10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| 11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 12 #include "net/dns/mock_host_resolver.h" | 12 #include "net/dns/mock_host_resolver.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 std::string FirstAddress(const AddressList& address_list) { | 19 std::string FirstAddress(const AddressList& address_list) { |
| 20 if (address_list.empty()) | 20 if (address_list.empty()) |
| 21 return ""; | 21 return std::string(); |
| 22 return address_list.front().ToString(); | 22 return address_list.front().ToString(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 TEST(MappedHostResolverTest, Inclusion) { | 25 TEST(MappedHostResolverTest, Inclusion) { |
| 26 // Create a mock host resolver, with specific hostname to IP mappings. | 26 // Create a mock host resolver, with specific hostname to IP mappings. |
| 27 scoped_ptr<MockHostResolver> resolver_impl(new MockHostResolver()); | 27 scoped_ptr<MockHostResolver> resolver_impl(new MockHostResolver()); |
| 28 resolver_impl->rules()->AddSimulatedFailure("*google.com"); | 28 resolver_impl->rules()->AddSimulatedFailure("*google.com"); |
| 29 resolver_impl->rules()->AddRule("baz.com", "192.168.1.5"); | 29 resolver_impl->rules()->AddRule("baz.com", "192.168.1.5"); |
| 30 resolver_impl->rules()->AddRule("foo.com", "192.168.1.8"); | 30 resolver_impl->rules()->AddRule("foo.com", "192.168.1.8"); |
| 31 resolver_impl->rules()->AddRule("proxy", "192.168.1.11"); | 31 resolver_impl->rules()->AddRule("proxy", "192.168.1.11"); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 EXPECT_EQ(OK, rv); | 164 EXPECT_EQ(OK, rv); |
| 165 EXPECT_EQ("192.168.1.9:60", FirstAddress(address_list)); | 165 EXPECT_EQ("192.168.1.9:60", FirstAddress(address_list)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 // Parsing bad rules should silently discard the rule (and never crash). | 168 // Parsing bad rules should silently discard the rule (and never crash). |
| 169 TEST(MappedHostResolverTest, ParseInvalidRules) { | 169 TEST(MappedHostResolverTest, ParseInvalidRules) { |
| 170 scoped_ptr<MappedHostResolver> resolver( | 170 scoped_ptr<MappedHostResolver> resolver( |
| 171 new MappedHostResolver(scoped_ptr<HostResolver>())); | 171 new MappedHostResolver(scoped_ptr<HostResolver>())); |
| 172 | 172 |
| 173 EXPECT_FALSE(resolver->AddRuleFromString("xyz")); | 173 EXPECT_FALSE(resolver->AddRuleFromString("xyz")); |
| 174 EXPECT_FALSE(resolver->AddRuleFromString("")); | 174 EXPECT_FALSE(resolver->AddRuleFromString(std::string())); |
| 175 EXPECT_FALSE(resolver->AddRuleFromString(" ")); | 175 EXPECT_FALSE(resolver->AddRuleFromString(" ")); |
| 176 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE")); | 176 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE")); |
| 177 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE foo bar")); | 177 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE foo bar")); |
| 178 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE")); | 178 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE")); |
| 179 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x")); | 179 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x")); |
| 180 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x :10")); | 180 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x :10")); |
| 181 } | 181 } |
| 182 | 182 |
| 183 // Test mapping hostnames to resolving failures. | 183 // Test mapping hostnames to resolving failures. |
| 184 TEST(MappedHostResolverTest, MapToError) { | 184 TEST(MappedHostResolverTest, MapToError) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 210 BoundNetLog()); | 210 BoundNetLog()); |
| 211 EXPECT_EQ(ERR_IO_PENDING, rv); | 211 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 212 rv = callback2.WaitForResult(); | 212 rv = callback2.WaitForResult(); |
| 213 EXPECT_EQ(OK, rv); | 213 EXPECT_EQ(OK, rv); |
| 214 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); | 214 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); |
| 215 } | 215 } |
| 216 | 216 |
| 217 } // namespace | 217 } // namespace |
| 218 | 218 |
| 219 } // namespace net | 219 } // namespace net |
| OLD | NEW |