Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2010 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/base/mapped_host_resolver.h"
      6 
      7 #include "net/base/mock_host_resolver.h"
      8 #include "net/base/net_errors.h"
      9 #include "net/base/net_log.h"
     10 #include "net/base/net_util.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace net {
     14 
     15 namespace {
     16 
     17 TEST(MappedHostResolverTest, Inclusion) {
     18   // Create a mock host resolver, with specific hostname to IP mappings.
     19   MockHostResolver* resolver_impl(new MockHostResolver());
     20   resolver_impl->rules()->AddSimulatedFailure("*google.com");
     21   resolver_impl->rules()->AddRule("baz.com", "192.168.1.5");
     22   resolver_impl->rules()->AddRule("foo.com", "192.168.1.8");
     23   resolver_impl->rules()->AddRule("proxy", "192.168.1.11");
     24 
     25   // Create a remapped resolver that uses |resolver_impl|.
     26   scoped_ptr<MappedHostResolver> resolver(
     27       new MappedHostResolver(resolver_impl));
     28 
     29   int rv;
     30   AddressList address_list;
     31 
     32   // Try resolving "www.google.com:80". There are no mappings yet, so this
     33   // hits |resolver_impl| and fails.
     34   rv = resolver->Resolve(HostResolver::RequestInfo(
     35                              HostPortPair("www.google.com", 80)),
     36                          &address_list, NULL, NULL, BoundNetLog());
     37   EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv);
     38 
     39   // Remap *.google.com to baz.com.
     40   EXPECT_TRUE(resolver->AddRuleFromString("map *.google.com baz.com"));
     41 
     42   // Try resolving "www.google.com:80". Should be remapped to "baz.com:80".
     43   rv = resolver->Resolve(HostResolver::RequestInfo(
     44                              HostPortPair("www.google.com", 80)),
     45                          &address_list, NULL, NULL, BoundNetLog());
     46   EXPECT_EQ(OK, rv);
     47   EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head()));
     48   EXPECT_EQ(80, address_list.GetPort());
     49 
     50   // Try resolving "foo.com:77". This will NOT be remapped, so result
     51   // is "foo.com:77".
     52   rv = resolver->Resolve(HostResolver::RequestInfo(HostPortPair("foo.com", 77)),
     53                          &address_list, NULL, NULL, BoundNetLog());
     54   EXPECT_EQ(OK, rv);
     55   EXPECT_EQ("192.168.1.8", NetAddressToString(address_list.head()));
     56   EXPECT_EQ(77, address_list.GetPort());
     57 
     58   // Remap "*.org" to "proxy:99".
     59   EXPECT_TRUE(resolver->AddRuleFromString("Map *.org proxy:99"));
     60 
     61   // Try resolving "chromium.org:61". Should be remapped to "proxy:99".
     62   rv = resolver->Resolve(HostResolver::RequestInfo
     63                              (HostPortPair("chromium.org", 61)),
     64                          &address_list, NULL, NULL, BoundNetLog());
     65   EXPECT_EQ(OK, rv);
     66   EXPECT_EQ("192.168.1.11", NetAddressToString(address_list.head()));
     67   EXPECT_EQ(99, address_list.GetPort());
     68 }
     69 
     70 // Tests that exclusions are respected.
     71 TEST(MappedHostResolverTest, Exclusion) {
     72   // Create a mock host resolver, with specific hostname to IP mappings.
     73   MockHostResolver* resolver_impl(new MockHostResolver());
     74   resolver_impl->rules()->AddRule("baz", "192.168.1.5");
     75   resolver_impl->rules()->AddRule("www.google.com", "192.168.1.3");
     76 
     77   // Create a remapped resolver that uses |resolver_impl|.
     78   scoped_ptr<MappedHostResolver> resolver(
     79       new MappedHostResolver(resolver_impl));
     80 
     81   int rv;
     82   AddressList address_list;
     83 
     84   // Remap "*.com" to "baz".
     85   EXPECT_TRUE(resolver->AddRuleFromString("map *.com baz"));
     86 
     87   // Add an exclusion for "*.google.com".
     88   EXPECT_TRUE(resolver->AddRuleFromString("EXCLUDE *.google.com"));
     89 
     90   // Try resolving "www.google.com". Should not be remapped due to exclusion).
     91   rv = resolver->Resolve(HostResolver::RequestInfo(
     92                              HostPortPair("www.google.com", 80)),
     93                          &address_list, NULL, NULL, BoundNetLog());
     94   EXPECT_EQ(OK, rv);
     95   EXPECT_EQ("192.168.1.3", NetAddressToString(address_list.head()));
     96   EXPECT_EQ(80, address_list.GetPort());
     97 
     98   // Try resolving "chrome.com:80". Should be remapped to "baz:80".
     99   rv = resolver->Resolve(HostResolver::RequestInfo(
    100                              HostPortPair("chrome.com", 80)),
    101                          &address_list, NULL, NULL, BoundNetLog());
    102   EXPECT_EQ(OK, rv);
    103   EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head()));
    104   EXPECT_EQ(80, address_list.GetPort());
    105 }
    106 
    107 TEST(MappedHostResolverTest, SetRulesFromString) {
    108   // Create a mock host resolver, with specific hostname to IP mappings.
    109   MockHostResolver* resolver_impl(new MockHostResolver());
    110   resolver_impl->rules()->AddRule("baz", "192.168.1.7");
    111   resolver_impl->rules()->AddRule("bar", "192.168.1.9");
    112 
    113   // Create a remapped resolver that uses |resolver_impl|.
    114   scoped_ptr<MappedHostResolver> resolver(
    115       new MappedHostResolver(resolver_impl));
    116 
    117   int rv;
    118   AddressList address_list;
    119 
    120   // Remap "*.com" to "baz", and *.net to "bar:60".
    121   resolver->SetRulesFromString("map *.com baz , map *.net bar:60");
    122 
    123   // Try resolving "www.google.com". Should be remapped to "baz".
    124   rv = resolver->Resolve(HostResolver::RequestInfo(
    125                              HostPortPair("www.google.com", 80)),
    126                          &address_list, NULL, NULL, BoundNetLog());
    127   EXPECT_EQ(OK, rv);
    128   EXPECT_EQ("192.168.1.7", NetAddressToString(address_list.head()));
    129   EXPECT_EQ(80, address_list.GetPort());
    130 
    131   // Try resolving "chrome.net:80". Should be remapped to "bar:60".
    132   rv = resolver->Resolve(HostResolver::RequestInfo(
    133                              HostPortPair("chrome.net", 80)),
    134                          &address_list, NULL, NULL, BoundNetLog());
    135   EXPECT_EQ(OK, rv);
    136   EXPECT_EQ("192.168.1.9", NetAddressToString(address_list.head()));
    137   EXPECT_EQ(60, address_list.GetPort());
    138 }
    139 
    140 // Parsing bad rules should silently discard the rule (and never crash).
    141 TEST(MappedHostResolverTest, ParseInvalidRules) {
    142   scoped_ptr<MappedHostResolver> resolver(new MappedHostResolver(NULL));
    143 
    144   EXPECT_FALSE(resolver->AddRuleFromString("xyz"));
    145   EXPECT_FALSE(resolver->AddRuleFromString(""));
    146   EXPECT_FALSE(resolver->AddRuleFromString(" "));
    147   EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE"));
    148   EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE foo bar"));
    149   EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE"));
    150   EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x"));
    151   EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x :10"));
    152 }
    153 
    154 }  // namespace
    155 
    156 }  // namespace net
    157