Update V8 to version 4.7.21.
[chromium-blink-merge.git] / ppapi / tests / test_host_resolver.cc
blob27ecae1339228e8ff00a9530df3533005edeee1f
1 // Copyright 2013 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.
5 #include "ppapi/tests/test_host_resolver.h"
7 #include "ppapi/cpp/host_resolver.h"
8 #include "ppapi/cpp/net_address.h"
9 #include "ppapi/cpp/tcp_socket.h"
10 #include "ppapi/cpp/var.h"
11 #include "ppapi/tests/test_utils.h"
12 #include "ppapi/tests/testing_instance.h"
14 REGISTER_TEST_CASE(HostResolver);
16 TestHostResolver::TestHostResolver(TestingInstance* instance)
17 : TestCase(instance) {
20 bool TestHostResolver::Init() {
21 bool host_resolver_is_available = pp::HostResolver::IsAvailable();
22 if (!host_resolver_is_available)
23 instance_->AppendError("PPB_HostResolver interface not available");
25 bool tcp_socket_is_available = pp::TCPSocket::IsAvailable();
26 if (!tcp_socket_is_available)
27 instance_->AppendError("PPB_TCPSocket interface not available");
29 bool init_host_port =
30 GetLocalHostPort(instance_->pp_instance(), &host_, &port_);
31 if (!init_host_port)
32 instance_->AppendError("Can't init host and port");
34 return host_resolver_is_available &&
35 tcp_socket_is_available &&
36 init_host_port &&
37 CheckTestingInterface() &&
38 EnsureRunningOverHTTP();
41 void TestHostResolver::RunTests(const std::string& filter) {
42 RUN_TEST(Empty, filter);
43 RUN_CALLBACK_TEST(TestHostResolver, Resolve, filter);
44 RUN_CALLBACK_TEST(TestHostResolver, ResolveIPv4, filter);
47 std::string TestHostResolver::SyncConnect(
48 pp::TCPSocket* socket,
49 const pp::NetAddress& address) {
50 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
51 callback.WaitForResult(socket->Connect(address, callback.GetCallback()));
52 CHECK_CALLBACK_BEHAVIOR(callback);
53 ASSERT_EQ(PP_OK, callback.result());
54 PASS();
57 std::string TestHostResolver::SyncRead(pp::TCPSocket* socket,
58 char* buffer,
59 int32_t num_bytes,
60 int32_t* bytes_read) {
61 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
62 callback.WaitForResult(
63 socket->Read(buffer, num_bytes, callback.GetCallback()));
64 CHECK_CALLBACK_BEHAVIOR(callback);
65 ASSERT_EQ(num_bytes, callback.result());
66 *bytes_read = callback.result();
67 PASS();
70 std::string TestHostResolver::SyncWrite(pp::TCPSocket* socket,
71 const char* buffer,
72 int32_t num_bytes,
73 int32_t* bytes_written) {
74 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
75 callback.WaitForResult(
76 socket->Write(buffer, num_bytes, callback.GetCallback()));
77 CHECK_CALLBACK_BEHAVIOR(callback);
78 ASSERT_EQ(num_bytes, callback.result());
79 *bytes_written = callback.result();
80 PASS();
83 std::string TestHostResolver::CheckHTTPResponse(pp::TCPSocket* socket,
84 const std::string& request,
85 const std::string& response) {
86 int32_t rv = 0;
87 ASSERT_SUBTEST_SUCCESS(
88 SyncWrite(socket, request.c_str(), static_cast<int32_t>(request.size()),
89 &rv));
90 std::vector<char> response_buffer(response.size());
91 ASSERT_SUBTEST_SUCCESS(
92 SyncRead(socket, &response_buffer[0],
93 static_cast<int32_t>(response.size()), &rv));
94 std::string actual_response(&response_buffer[0], rv);
95 if (response != actual_response) {
96 return "CheckHTTPResponse failed, expected: " + response +
97 ", actual: " + actual_response;
99 PASS();
102 std::string TestHostResolver::SyncResolve(
103 pp::HostResolver* host_resolver,
104 const std::string& host,
105 uint16_t port,
106 const PP_HostResolver_Hint& hint) {
107 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
108 callback.WaitForResult(
109 host_resolver->Resolve(host.c_str(), port, hint, callback.GetCallback()));
110 CHECK_CALLBACK_BEHAVIOR(callback);
111 ASSERT_EQ(PP_OK, callback.result());
112 PASS();
115 std::string TestHostResolver::ParameterizedTestResolve(
116 const PP_HostResolver_Hint& hint) {
117 pp::HostResolver host_resolver(instance_);
119 ASSERT_SUBTEST_SUCCESS(SyncResolve(&host_resolver, host_, port_, hint));
121 size_t size = host_resolver.GetNetAddressCount();
122 ASSERT_TRUE(size >= 1);
124 pp::NetAddress address;
125 for (size_t i = 0; i < size; ++i) {
126 address = host_resolver.GetNetAddress(static_cast<uint32_t>(i));
127 ASSERT_NE(0, address.pp_resource());
129 pp::TCPSocket socket(instance_);
130 ASSERT_SUBTEST_SUCCESS(SyncConnect(&socket, address));
131 ASSERT_SUBTEST_SUCCESS(CheckHTTPResponse(&socket,
132 "GET / HTTP/1.0\r\n\r\n",
133 "HTTP"));
134 socket.Close();
137 address = host_resolver.GetNetAddress(static_cast<uint32_t>(size));
138 ASSERT_EQ(0, address.pp_resource());
139 pp::Var canonical_name = host_resolver.GetCanonicalName();
140 ASSERT_TRUE(canonical_name.is_string());
142 ASSERT_SUBTEST_SUCCESS(SyncResolve(&host_resolver, canonical_name.AsString(),
143 port_, hint));
144 size = host_resolver.GetNetAddressCount();
145 ASSERT_TRUE(size >= 1);
147 PASS();
150 std::string TestHostResolver::TestEmpty() {
151 pp::HostResolver host_resolver(instance_);
152 ASSERT_EQ(0, host_resolver.GetNetAddressCount());
153 pp::NetAddress address = host_resolver.GetNetAddress(0);
154 ASSERT_EQ(0, address.pp_resource());
156 PASS();
159 std::string TestHostResolver::TestResolve() {
160 PP_HostResolver_Hint hint;
161 hint.family = PP_NETADDRESS_FAMILY_UNSPECIFIED;
162 hint.flags = PP_HOSTRESOLVER_FLAG_CANONNAME;
163 return ParameterizedTestResolve(hint);
166 std::string TestHostResolver::TestResolveIPv4() {
167 PP_HostResolver_Hint hint;
168 hint.family = PP_NETADDRESS_FAMILY_IPV4;
169 hint.flags = PP_HOSTRESOLVER_FLAG_CANONNAME;
170 return ParameterizedTestResolve(hint);