This source file includes following definitions.
- has_outstanding_request
- Resolve
- ResolveFromCache
- CancelRequest
- TEST
- TEST
- TEST
#include "net/dns/single_request_host_resolver.h"
#include "net/base/address_list.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.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 {
class HangingHostResolver : public HostResolver {
public:
HangingHostResolver() : outstanding_request_(NULL) {}
virtual ~HangingHostResolver() {
EXPECT_TRUE(!has_outstanding_request());
}
bool has_outstanding_request() const {
return outstanding_request_ != NULL;
}
virtual int Resolve(const RequestInfo& info,
RequestPriority priority,
AddressList* addresses,
const CompletionCallback& callback,
RequestHandle* out_req,
const BoundNetLog& net_log) OVERRIDE {
EXPECT_FALSE(has_outstanding_request());
outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234);
*out_req = outstanding_request_;
return ERR_IO_PENDING;
}
virtual int ResolveFromCache(const RequestInfo& info,
AddressList* addresses,
const BoundNetLog& net_log) OVERRIDE {
NOTIMPLEMENTED();
return ERR_UNEXPECTED;
}
virtual void CancelRequest(RequestHandle req) OVERRIDE {
EXPECT_TRUE(has_outstanding_request());
EXPECT_EQ(req, outstanding_request_);
outstanding_request_ = NULL;
}
private:
RequestHandle outstanding_request_;
DISALLOW_COPY_AND_ASSIGN(HangingHostResolver);
};
TEST(SingleRequestHostResolverTest, NormalResolve) {
MockHostResolver resolver;
resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string());
SingleRequestHostResolver single_request_resolver(&resolver);
AddressList addrlist;
TestCompletionCallback callback;
HostResolver::RequestInfo request(HostPortPair("watsup", 90));
int rv = single_request_resolver.Resolve(
request, DEFAULT_PRIORITY, &addrlist, callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
ASSERT_FALSE(addrlist.empty());
EXPECT_EQ("199.188.1.166", addrlist.front().ToStringWithoutPort());
}
TEST(SingleRequestHostResolverTest, Cancel) {
HangingHostResolver resolver;
{
SingleRequestHostResolver single_request_resolver(&resolver);
AddressList addrlist;
TestCompletionCallback callback;
HostResolver::RequestInfo request(HostPortPair("watsup", 90));
int rv = single_request_resolver.Resolve(request,
DEFAULT_PRIORITY,
&addrlist,
callback.callback(),
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_TRUE(resolver.has_outstanding_request());
}
EXPECT_FALSE(resolver.has_outstanding_request());
}
TEST(SingleRequestHostResolverTest, CancelWhileNoPendingRequest) {
HangingHostResolver resolver;
SingleRequestHostResolver single_request_resolver(&resolver);
single_request_resolver.Cancel();
}
}
}