| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_host_resolver_private.h" | 5 #include "ppapi/tests/test_host_resolver_private.h" |
| 6 | 6 |
| 7 #include "ppapi/c/private/ppb_net_address_private.h" | 7 #include "ppapi/c/private/ppb_net_address_private.h" |
| 8 #include "ppapi/cpp/module.h" | 8 #include "ppapi/cpp/module_impl.h" |
| 9 #include "ppapi/cpp/pass_ref.h" | 9 #include "ppapi/cpp/private/host_resolver_private.h" |
| 10 #include "ppapi/cpp/private/tcp_socket_private.h" | 10 #include "ppapi/cpp/private/tcp_socket_private.h" |
| 11 #include "ppapi/cpp/var.h" | |
| 12 #include "ppapi/tests/test_utils.h" | 11 #include "ppapi/tests/test_utils.h" |
| 13 #include "ppapi/tests/testing_instance.h" | 12 #include "ppapi/tests/testing_instance.h" |
| 14 | 13 |
| 15 REGISTER_TEST_CASE(HostResolverPrivate); | 14 REGISTER_TEST_CASE(HostResolverPrivate); |
| 16 | 15 |
| 17 TestHostResolverPrivate::TestHostResolverPrivate(TestingInstance* instance) | 16 TestHostResolverPrivate::TestHostResolverPrivate(TestingInstance* instance) |
| 18 : TestCase(instance) { | 17 : TestCase(instance) { |
| 19 } | 18 } |
| 20 | 19 |
| 21 bool TestHostResolverPrivate::Init() { | 20 bool TestHostResolverPrivate::Init() { |
| 22 core_interface_ = static_cast<const PPB_Core*>( | 21 bool host_resolver_private_is_available = |
| 23 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 22 pp::HostResolverPrivate::IsAvailable(); |
| 24 if (!core_interface_) | 23 if (!host_resolver_private_is_available) |
| 25 instance_->AppendError("PPB_Core interface not available"); | 24 instance_->AppendError("PPB_HostResolver_Private interface not available"); |
| 26 host_resolver_private_interface_ = | 25 |
| 27 static_cast<const PPB_HostResolver_Private*>( | |
| 28 pp::Module::Get()->GetBrowserInterface( | |
| 29 PPB_HOSTRESOLVER_PRIVATE_INTERFACE)); | |
| 30 bool tcp_socket_private_is_available = pp::TCPSocketPrivate::IsAvailable(); | 26 bool tcp_socket_private_is_available = pp::TCPSocketPrivate::IsAvailable(); |
| 31 if (!tcp_socket_private_is_available) | 27 if (!tcp_socket_private_is_available) |
| 32 instance_->AppendError("PPB_TCPSocket_Private interface not available"); | 28 instance_->AppendError("PPB_TCPSocket_Private interface not available"); |
| 33 if (!host_resolver_private_interface_) | |
| 34 instance_->AppendError("PPB_HostResolver_Private interface not available"); | |
| 35 | 29 |
| 36 bool init_host_port = GetLocalHostPort( | 30 bool init_host_port = |
| 37 instance_->pp_instance(), &host_, &port_); | 31 GetLocalHostPort(instance_->pp_instance(), &host_, &port_); |
| 38 if (!init_host_port) | 32 if (!init_host_port) |
| 39 instance_->AppendError("Can't init host and port"); | 33 instance_->AppendError("Can't init host and port"); |
| 40 | 34 |
| 41 return core_interface_ && | 35 return host_resolver_private_is_available && |
| 42 host_resolver_private_interface_ && | |
| 43 tcp_socket_private_is_available && | 36 tcp_socket_private_is_available && |
| 44 init_host_port && | 37 init_host_port && |
| 45 CheckTestingInterface() && | 38 CheckTestingInterface() && |
| 46 EnsureRunningOverHTTP(); | 39 EnsureRunningOverHTTP(); |
| 47 } | 40 } |
| 48 | 41 |
| 49 void TestHostResolverPrivate::RunTests(const std::string& filter) { | 42 void TestHostResolverPrivate::RunTests(const std::string& filter) { |
| 50 RUN_TEST(Create, filter); | 43 RUN_TEST(Empty, filter); |
| 51 RUN_TEST_FORCEASYNC_AND_NOT(Resolve, filter); | 44 RUN_TEST_FORCEASYNC_AND_NOT(Resolve, filter); |
| 52 RUN_TEST_FORCEASYNC_AND_NOT(ResolveIPv4, filter); | 45 RUN_TEST_FORCEASYNC_AND_NOT(ResolveIPv4, filter); |
| 53 } | 46 } |
| 54 | 47 |
| 55 std::string TestHostResolverPrivate::SyncConnect(pp::TCPSocketPrivate* socket, | 48 std::string TestHostResolverPrivate::SyncConnect(pp::TCPSocketPrivate* socket, |
| 56 const std::string& host, | 49 const std::string& host, |
| 57 uint16_t port) { | 50 uint16_t port) { |
| 58 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 51 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 59 int32_t rv = socket->Connect(host.c_str(), port, callback); | 52 int32_t rv = socket->Connect(host.c_str(), port, callback); |
| 60 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | 53 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 return ReportError("PPB_TCPSocket_Private::Write", rv); | 101 return ReportError("PPB_TCPSocket_Private::Write", rv); |
| 109 *bytes_written = rv; | 102 *bytes_written = rv; |
| 110 PASS(); | 103 PASS(); |
| 111 } | 104 } |
| 112 | 105 |
| 113 std::string TestHostResolverPrivate::CheckHTTPResponse( | 106 std::string TestHostResolverPrivate::CheckHTTPResponse( |
| 114 pp::TCPSocketPrivate* socket, | 107 pp::TCPSocketPrivate* socket, |
| 115 const std::string& request, | 108 const std::string& request, |
| 116 const std::string& response) { | 109 const std::string& response) { |
| 117 int32_t rv = 0; | 110 int32_t rv = 0; |
| 118 std::string error_message; | 111 ASSERT_SUBTEST_SUCCESS( |
| 119 | 112 SyncWrite(socket, request.c_str(), request.size(), &rv)); |
| 120 error_message = SyncWrite(socket, request.c_str(), request.size(), &rv); | |
| 121 if (!error_message.empty()) | |
| 122 return error_message; | |
| 123 | |
| 124 std::vector<char> response_buffer(response.size()); | 113 std::vector<char> response_buffer(response.size()); |
| 125 error_message = SyncRead(socket, &response_buffer[0], response.size(), &rv); | 114 ASSERT_SUBTEST_SUCCESS( |
| 126 if (!error_message.empty()) | 115 SyncRead(socket, &response_buffer[0], response.size(), &rv)); |
| 127 return error_message; | |
| 128 std::string actual_response(&response_buffer[0], rv); | 116 std::string actual_response(&response_buffer[0], rv); |
| 129 if (response != actual_response) { | 117 if (response != actual_response) { |
| 130 return "CheckHTTPResponse failed, expected: " + response + | 118 return "CheckHTTPResponse failed, expected: " + response + |
| 131 ", actual: " + actual_response; | 119 ", actual: " + actual_response; |
| 132 } | 120 } |
| 133 PASS(); | 121 PASS(); |
| 134 } | 122 } |
| 135 | 123 |
| 136 std::string TestHostResolverPrivate::SyncResolve( | 124 std::string TestHostResolverPrivate::SyncResolve( |
| 137 PP_Resource host_resolver, | 125 pp::HostResolverPrivate* host_resolver, |
| 138 const std::string& host, | 126 const std::string& host, |
| 139 uint16_t port, | 127 uint16_t port, |
| 140 const PP_HostResolver_Private_Hint& hint) { | 128 const PP_HostResolver_Private_Hint& hint) { |
| 141 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 129 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 142 int32_t rv = host_resolver_private_interface_->Resolve( | 130 int32_t rv = host_resolver->Resolve(host, port, hint, callback); |
| 143 host_resolver, | |
| 144 host.c_str(), | |
| 145 port, | |
| 146 &hint, | |
| 147 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 148 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | 131 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 149 return ReportError("PPB_HostResolver_Private::Resolve force_async", rv); | 132 return ReportError("PPB_HostResolver_Private::Resolve force_async", rv); |
| 150 if (rv == PP_OK_COMPLETIONPENDING) | 133 if (rv == PP_OK_COMPLETIONPENDING) |
| 151 rv = callback.WaitForResult(); | 134 rv = callback.WaitForResult(); |
| 152 if (rv != PP_OK) | 135 if (rv != PP_OK) |
| 153 return ReportError("PPB_HostResolver_Private::Resolve", rv); | 136 return ReportError("PPB_HostResolver_Private::Resolve", rv); |
| 154 PASS(); | 137 PASS(); |
| 155 } | 138 } |
| 156 | 139 |
| 157 std::string TestHostResolverPrivate::TestCreate() { | 140 std::string TestHostResolverPrivate::TestEmpty() { |
| 158 PP_Resource host_resolver = host_resolver_private_interface_->Create(0); | 141 pp::HostResolverPrivate host_resolver(instance_); |
| 159 ASSERT_EQ(0, host_resolver); | 142 ASSERT_EQ(0, host_resolver.GetSize()); |
| 143 PP_NetAddress_Private address; |
| 144 ASSERT_FALSE(host_resolver.GetNetAddress(0, &address)); |
| 160 | 145 |
| 161 host_resolver = | |
| 162 host_resolver_private_interface_->Create(instance_->pp_instance()); | |
| 163 ASSERT_NE(0, host_resolver); | |
| 164 ASSERT_TRUE(host_resolver_private_interface_->IsHostResolver(host_resolver)); | |
| 165 ASSERT_EQ(0, host_resolver_private_interface_->GetSize(host_resolver)); | |
| 166 | |
| 167 PP_NetAddress_Private address; | |
| 168 ASSERT_FALSE(host_resolver_private_interface_->GetNetAddress(host_resolver, | |
| 169 0, | |
| 170 &address)); | |
| 171 core_interface_->ReleaseResource(host_resolver); | |
| 172 PASS(); | 146 PASS(); |
| 173 } | 147 } |
| 174 | 148 |
| 175 std::string TestHostResolverPrivate::ParametrizedTestResolve( | 149 std::string TestHostResolverPrivate::ParametrizedTestResolve( |
| 176 const PP_HostResolver_Private_Hint &hint) { | 150 const PP_HostResolver_Private_Hint &hint) { |
| 177 PP_Resource host_resolver = | 151 pp::HostResolverPrivate host_resolver(instance_); |
| 178 host_resolver_private_interface_->Create(instance_->pp_instance()); | |
| 179 ASSERT_NE(0, host_resolver); | |
| 180 ASSERT_TRUE(host_resolver_private_interface_->IsHostResolver(host_resolver)); | |
| 181 | 152 |
| 182 std::string error_message = SyncResolve(host_resolver, host_, port_, hint); | 153 ASSERT_SUBTEST_SUCCESS(SyncResolve(&host_resolver, host_, port_, hint)); |
| 183 if (!error_message.empty()) | |
| 184 return error_message; | |
| 185 | 154 |
| 186 const size_t size = host_resolver_private_interface_->GetSize(host_resolver); | 155 const size_t size = host_resolver.GetSize(); |
| 187 ASSERT_TRUE(size >= 1); | 156 ASSERT_TRUE(size >= 1); |
| 188 | 157 |
| 189 PP_NetAddress_Private address; | 158 PP_NetAddress_Private address; |
| 190 for (size_t i = 0; i < size; ++i) { | 159 for (size_t i = 0; i < size; ++i) { |
| 191 ASSERT_TRUE(host_resolver_private_interface_->GetNetAddress(host_resolver, | 160 ASSERT_TRUE(host_resolver.GetNetAddress(i, &address)); |
| 192 i, | 161 |
| 193 &address)); | |
| 194 pp::TCPSocketPrivate socket(instance_); | 162 pp::TCPSocketPrivate socket(instance_); |
| 195 error_message = SyncConnect(&socket, address); | 163 ASSERT_SUBTEST_SUCCESS(SyncConnect(&socket, address)); |
| 196 error_message = | 164 ASSERT_SUBTEST_SUCCESS(CheckHTTPResponse(&socket, |
| 197 CheckHTTPResponse(&socket, "GET / HTTP/1.0\r\n\r\n", "HTTP/1.0"); | 165 "GET / HTTP/1.0\r\n\r\n", |
| 198 if (!error_message.empty()) | 166 "HTTP")); |
| 199 return error_message; | |
| 200 socket.Disconnect(); | 167 socket.Disconnect(); |
| 201 } | 168 } |
| 202 | 169 |
| 203 ASSERT_FALSE(host_resolver_private_interface_->GetNetAddress(host_resolver, | 170 ASSERT_FALSE(host_resolver.GetNetAddress(size, &address)); |
| 204 size, | 171 std::string canonical_name; |
| 205 &address)); | 172 host_resolver.GetCanonicalName(&canonical_name); |
| 206 PP_Var pp_var = | |
| 207 host_resolver_private_interface_->GetCanonicalName(host_resolver); | |
| 208 pp::Var canonical_name(pp::PassRef(), pp_var); | |
| 209 ASSERT_TRUE(canonical_name.is_string()); | |
| 210 pp::TCPSocketPrivate socket(instance_); | 173 pp::TCPSocketPrivate socket(instance_); |
| 211 error_message = SyncConnect(&socket, | 174 ASSERT_SUBTEST_SUCCESS(SyncConnect(&socket, canonical_name, port_)); |
| 212 canonical_name.AsString().c_str(), | 175 ASSERT_SUBTEST_SUCCESS(CheckHTTPResponse(&socket, |
| 213 port_); | 176 "GET / HTTP/1.0\r\n\r\n", |
| 214 if (!error_message.empty()) | 177 "HTTP")); |
| 215 return error_message; | |
| 216 error_message = CheckHTTPResponse(&socket, | |
| 217 "GET / HTTP/1.0\r\n\r\n", "HTTP"); | |
| 218 if (!error_message.empty()) | |
| 219 return error_message; | |
| 220 socket.Disconnect(); | 178 socket.Disconnect(); |
| 221 | 179 |
| 222 core_interface_->ReleaseResource(host_resolver); | |
| 223 PASS(); | 180 PASS(); |
| 224 } | 181 } |
| 225 | 182 |
| 226 std::string TestHostResolverPrivate::TestResolve() { | 183 std::string TestHostResolverPrivate::TestResolve() { |
| 227 PP_HostResolver_Private_Hint hint; | 184 PP_HostResolver_Private_Hint hint; |
| 228 hint.family = PP_NETADDRESSFAMILY_UNSPECIFIED; | 185 hint.family = PP_NETADDRESSFAMILY_UNSPECIFIED; |
| 229 hint.flags = PP_HOST_RESOLVER_FLAGS_CANONNAME; | 186 hint.flags = PP_HOST_RESOLVER_FLAGS_CANONNAME; |
| 230 return ParametrizedTestResolve(hint); | 187 return ParametrizedTestResolve(hint); |
| 231 } | 188 } |
| 232 | 189 |
| 233 std::string TestHostResolverPrivate::TestResolveIPv4() { | 190 std::string TestHostResolverPrivate::TestResolveIPv4() { |
| 234 PP_HostResolver_Private_Hint hint; | 191 PP_HostResolver_Private_Hint hint; |
| 235 hint.family = PP_NETADDRESSFAMILY_IPV4; | 192 hint.family = PP_NETADDRESSFAMILY_IPV4; |
| 236 hint.flags = PP_HOST_RESOLVER_FLAGS_CANONNAME; | 193 hint.flags = PP_HOST_RESOLVER_FLAGS_CANONNAME; |
| 237 return ParametrizedTestResolve(hint); | 194 return ParametrizedTestResolve(hint); |
| 238 } | 195 } |
| OLD | NEW |