Remove extra line from unit_tests.isolate
[chromium-blink-merge.git] / ppapi / tests / test_host_resolver_private.cc
blob8de75b38041f265f15c720ed886e5036e77a8d6a
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
3 // found in the LICENSE file.
5 #include "ppapi/tests/test_host_resolver_private.h"
7 #include "ppapi/c/private/ppb_net_address_private.h"
8 #include "ppapi/cpp/module_impl.h"
9 #include "ppapi/cpp/private/host_resolver_private.h"
10 #include "ppapi/cpp/private/tcp_socket_private.h"
11 #include "ppapi/cpp/var.h"
12 #include "ppapi/tests/test_utils.h"
13 #include "ppapi/tests/testing_instance.h"
15 REGISTER_TEST_CASE(HostResolverPrivate);
17 TestHostResolverPrivate::TestHostResolverPrivate(TestingInstance* instance)
18 : TestCase(instance) {
21 bool TestHostResolverPrivate::Init() {
22 bool host_resolver_private_is_available =
23 pp::HostResolverPrivate::IsAvailable();
24 if (!host_resolver_private_is_available)
25 instance_->AppendError("PPB_HostResolver_Private interface not available");
27 bool tcp_socket_private_is_available = pp::TCPSocketPrivate::IsAvailable();
28 if (!tcp_socket_private_is_available)
29 instance_->AppendError("PPB_TCPSocket_Private interface not available");
31 bool init_host_port =
32 GetLocalHostPort(instance_->pp_instance(), &host_, &port_);
33 if (!init_host_port)
34 instance_->AppendError("Can't init host and port");
36 return host_resolver_private_is_available &&
37 tcp_socket_private_is_available &&
38 init_host_port &&
39 CheckTestingInterface() &&
40 EnsureRunningOverHTTP();
43 void TestHostResolverPrivate::RunTests(const std::string& filter) {
44 RUN_TEST(Empty, filter);
45 RUN_TEST_FORCEASYNC_AND_NOT(Resolve, filter);
46 RUN_TEST_FORCEASYNC_AND_NOT(ResolveIPv4, filter);
49 std::string TestHostResolverPrivate::SyncConnect(pp::TCPSocketPrivate* socket,
50 const std::string& host,
51 uint16_t port) {
52 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
53 int32_t rv = socket->Connect(host.c_str(), port, callback);
54 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
55 return ReportError("PPB_TCPSocket_Private::Connect force_async", rv);
56 if (rv == PP_OK_COMPLETIONPENDING)
57 rv = callback.WaitForResult();
58 ASSERT_EQ(PP_OK, rv);
59 PASS();
62 std::string TestHostResolverPrivate::SyncConnect(
63 pp::TCPSocketPrivate* socket,
64 const PP_NetAddress_Private& address) {
65 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
66 int32_t rv = socket->ConnectWithNetAddress(&address, callback);
67 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
68 return ReportError("PPB_TCPSocket_Private::Connect force_async", rv);
69 if (rv == PP_OK_COMPLETIONPENDING)
70 rv = callback.WaitForResult();
71 ASSERT_EQ(PP_OK, rv);
72 PASS();
75 std::string TestHostResolverPrivate::SyncRead(pp::TCPSocketPrivate* socket,
76 char* buffer,
77 int32_t num_bytes,
78 int32_t* bytes_read) {
79 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
80 int32_t rv = socket->Read(buffer, num_bytes, callback);
81 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
82 return ReportError("PPB_TCPSocket_Private::Read force_async", rv);
83 if (rv == PP_OK_COMPLETIONPENDING)
84 rv = callback.WaitForResult();
85 if (num_bytes != rv)
86 return ReportError("PPB_TCPSocket_Private::Read", rv);
87 *bytes_read = rv;
88 PASS();
91 std::string TestHostResolverPrivate::SyncWrite(pp::TCPSocketPrivate* socket,
92 const char* buffer,
93 int32_t num_bytes,
94 int32_t* bytes_written) {
95 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
96 int32_t rv = socket->Write(buffer, num_bytes, callback);
97 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
98 return ReportError("PPB_TCPSocket_Private::Write force_async", rv);
99 if (rv == PP_OK_COMPLETIONPENDING)
100 rv = callback.WaitForResult();
101 if (num_bytes != rv)
102 return ReportError("PPB_TCPSocket_Private::Write", rv);
103 *bytes_written = rv;
104 PASS();
107 std::string TestHostResolverPrivate::CheckHTTPResponse(
108 pp::TCPSocketPrivate* socket,
109 const std::string& request,
110 const std::string& response) {
111 int32_t rv = 0;
112 ASSERT_SUBTEST_SUCCESS(
113 SyncWrite(socket, request.c_str(), request.size(), &rv));
114 std::vector<char> response_buffer(response.size());
115 ASSERT_SUBTEST_SUCCESS(
116 SyncRead(socket, &response_buffer[0], response.size(), &rv));
117 std::string actual_response(&response_buffer[0], rv);
118 if (response != actual_response) {
119 return "CheckHTTPResponse failed, expected: " + response +
120 ", actual: " + actual_response;
122 PASS();
125 std::string TestHostResolverPrivate::SyncResolve(
126 pp::HostResolverPrivate* host_resolver,
127 const std::string& host,
128 uint16_t port,
129 const PP_HostResolver_Private_Hint& hint) {
130 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
131 int32_t rv = host_resolver->Resolve(host, port, hint, callback);
132 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
133 return ReportError("PPB_HostResolver_Private::Resolve force_async", rv);
134 if (rv == PP_OK_COMPLETIONPENDING)
135 rv = callback.WaitForResult();
136 if (rv != PP_OK)
137 return ReportError("PPB_HostResolver_Private::Resolve", rv);
138 PASS();
141 std::string TestHostResolverPrivate::TestEmpty() {
142 pp::HostResolverPrivate host_resolver(instance_);
143 ASSERT_EQ(0, host_resolver.GetSize());
144 PP_NetAddress_Private address;
145 ASSERT_FALSE(host_resolver.GetNetAddress(0, &address));
147 PASS();
150 std::string TestHostResolverPrivate::ParametrizedTestResolve(
151 const PP_HostResolver_Private_Hint &hint) {
152 pp::HostResolverPrivate host_resolver(instance_);
154 ASSERT_SUBTEST_SUCCESS(SyncResolve(&host_resolver, host_, port_, hint));
156 const size_t size = host_resolver.GetSize();
157 ASSERT_TRUE(size >= 1);
159 PP_NetAddress_Private address;
160 for (size_t i = 0; i < size; ++i) {
161 ASSERT_TRUE(host_resolver.GetNetAddress(i, &address));
163 pp::TCPSocketPrivate socket(instance_);
164 ASSERT_SUBTEST_SUCCESS(SyncConnect(&socket, address));
165 ASSERT_SUBTEST_SUCCESS(CheckHTTPResponse(&socket,
166 "GET / HTTP/1.0\r\n\r\n",
167 "HTTP"));
168 socket.Disconnect();
171 ASSERT_FALSE(host_resolver.GetNetAddress(size, &address));
172 pp::Var canonical_name = host_resolver.GetCanonicalName();
173 ASSERT_TRUE(canonical_name.is_string());
174 pp::TCPSocketPrivate socket(instance_);
175 ASSERT_SUBTEST_SUCCESS(SyncConnect(&socket,
176 canonical_name.AsString(),
177 port_));
178 ASSERT_SUBTEST_SUCCESS(CheckHTTPResponse(&socket,
179 "GET / HTTP/1.0\r\n\r\n",
180 "HTTP"));
181 socket.Disconnect();
183 PASS();
186 std::string TestHostResolverPrivate::TestResolve() {
187 PP_HostResolver_Private_Hint hint;
188 hint.family = PP_NETADDRESSFAMILY_UNSPECIFIED;
189 hint.flags = PP_HOST_RESOLVER_FLAGS_CANONNAME;
190 return ParametrizedTestResolve(hint);
193 std::string TestHostResolverPrivate::TestResolveIPv4() {
194 PP_HostResolver_Private_Hint hint;
195 hint.family = PP_NETADDRESSFAMILY_IPV4;
196 hint.flags = PP_HOST_RESOLVER_FLAGS_CANONNAME;
197 return ParametrizedTestResolve(hint);