This source file includes following definitions.
- FirstAddress
- TEST
- TEST
- TEST
- TEST
- TEST
#include "net/dns/mapped_host_resolver.h"
#include "net/base/address_list.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/base/net_util.h"
#include "net/base/test_completion_callback.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
std::string FirstAddress(const AddressList& address_list) {
if (address_list.empty())
return std::string();
return address_list.front().ToString();
}
TEST(MappedHostResolverTest, Inclusion) {
scoped_ptr<MockHostResolver> resolver_impl(new MockHostResolver());
resolver_impl->rules()->AddSimulatedFailure("*google.com");
resolver_impl->rules()->AddRule("baz.com", "192.168.1.5");
resolver_impl->rules()->AddRule("foo.com", "192.168.1.8");
resolver_impl->rules()->AddRule("proxy", "192.168.1.11");
scoped_ptr<MappedHostResolver> resolver(
new MappedHostResolver(resolver_impl.PassAs<HostResolver>()));
int rv;
AddressList address_list;
TestCompletionCallback callback;
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv);
EXPECT_TRUE(resolver->AddRuleFromString("map *.google.com baz.com"));
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list));
rv = resolver->Resolve(HostResolver::RequestInfo(HostPortPair("foo.com", 77)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.8:77", FirstAddress(address_list));
EXPECT_TRUE(resolver->AddRuleFromString("Map *.org proxy:99"));
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("chromium.org", 61)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.11:99", FirstAddress(address_list));
}
TEST(MappedHostResolverTest, Exclusion) {
scoped_ptr<MockHostResolver> resolver_impl(new MockHostResolver());
resolver_impl->rules()->AddRule("baz", "192.168.1.5");
resolver_impl->rules()->AddRule("www.google.com", "192.168.1.3");
scoped_ptr<MappedHostResolver> resolver(
new MappedHostResolver(resolver_impl.PassAs<HostResolver>()));
int rv;
AddressList address_list;
TestCompletionCallback callback;
EXPECT_TRUE(resolver->AddRuleFromString("map *.com baz"));
EXPECT_TRUE(resolver->AddRuleFromString("EXCLUDE *.google.com"));
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.3:80", FirstAddress(address_list));
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("chrome.com", 80)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list));
}
TEST(MappedHostResolverTest, SetRulesFromString) {
scoped_ptr<MockHostResolver> resolver_impl(new MockHostResolver());
resolver_impl->rules()->AddRule("baz", "192.168.1.7");
resolver_impl->rules()->AddRule("bar", "192.168.1.9");
scoped_ptr<MappedHostResolver> resolver(
new MappedHostResolver(resolver_impl.PassAs<HostResolver>()));
int rv;
AddressList address_list;
TestCompletionCallback callback;
resolver->SetRulesFromString("map *.com baz , map *.net bar:60");
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.7:80", FirstAddress(address_list));
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("chrome.net", 80)),
DEFAULT_PRIORITY,
&address_list,
callback.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.9:60", FirstAddress(address_list));
}
TEST(MappedHostResolverTest, ParseInvalidRules) {
scoped_ptr<MappedHostResolver> resolver(
new MappedHostResolver(scoped_ptr<HostResolver>()));
EXPECT_FALSE(resolver->AddRuleFromString("xyz"));
EXPECT_FALSE(resolver->AddRuleFromString(std::string()));
EXPECT_FALSE(resolver->AddRuleFromString(" "));
EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE"));
EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE foo bar"));
EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE"));
EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x"));
EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x :10"));
}
TEST(MappedHostResolverTest, MapToError) {
scoped_ptr<MockHostResolver> resolver_impl(new MockHostResolver());
resolver_impl->rules()->AddRule("*", "192.168.1.5");
scoped_ptr<MappedHostResolver> resolver(
new MappedHostResolver(resolver_impl.PassAs<HostResolver>()));
int rv;
AddressList address_list;
EXPECT_TRUE(resolver->AddRuleFromString("MAP *.google.com ~NOTFOUND"));
TestCompletionCallback callback1;
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
DEFAULT_PRIORITY,
&address_list,
callback1.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv);
TestCompletionCallback callback2;
rv = resolver->Resolve(
HostResolver::RequestInfo(HostPortPair("www.foo.com", 80)),
DEFAULT_PRIORITY,
&address_list,
callback2.callback(),
NULL,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback2.WaitForResult();
EXPECT_EQ(OK, rv);
EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list));
}
}
}