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");
32 GetLocalHostPort(instance_
->pp_instance(), &host_
, &port_
);
34 instance_
->AppendError("Can't init host and port");
36 return host_resolver_private_is_available
&&
37 tcp_socket_private_is_available
&&
39 CheckTestingInterface() &&
40 EnsureRunningOverHTTP();
43 void TestHostResolverPrivate::RunTests(const std::string
& filter
) {
44 RUN_TEST(Empty
, filter
);
45 RUN_CALLBACK_TEST(TestHostResolverPrivate
, Resolve
, filter
);
46 RUN_CALLBACK_TEST(TestHostResolverPrivate
, ResolveIPv4
, filter
);
49 std::string
TestHostResolverPrivate::SyncConnect(pp::TCPSocketPrivate
* socket
,
50 const std::string
& host
,
52 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
53 callback
.WaitForResult(
54 socket
->Connect(host
.c_str(), port
, callback
.GetCallback()));
55 CHECK_CALLBACK_BEHAVIOR(callback
);
56 ASSERT_EQ(PP_OK
, callback
.result());
60 std::string
TestHostResolverPrivate::SyncConnect(
61 pp::TCPSocketPrivate
* socket
,
62 const PP_NetAddress_Private
& address
) {
63 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
64 callback
.WaitForResult(
65 socket
->ConnectWithNetAddress(&address
, callback
.GetCallback()));
66 CHECK_CALLBACK_BEHAVIOR(callback
);
67 ASSERT_EQ(PP_OK
, callback
.result());
71 std::string
TestHostResolverPrivate::SyncRead(pp::TCPSocketPrivate
* socket
,
74 int32_t* bytes_read
) {
75 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
76 callback
.WaitForResult(
77 socket
->Read(buffer
, num_bytes
, callback
.GetCallback()));
78 CHECK_CALLBACK_BEHAVIOR(callback
);
79 ASSERT_EQ(num_bytes
, callback
.result());
80 *bytes_read
= callback
.result();
84 std::string
TestHostResolverPrivate::SyncWrite(pp::TCPSocketPrivate
* socket
,
87 int32_t* bytes_written
) {
88 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
89 callback
.WaitForResult(
90 socket
->Write(buffer
, num_bytes
, callback
.GetCallback()));
91 CHECK_CALLBACK_BEHAVIOR(callback
);
92 ASSERT_EQ(num_bytes
, callback
.result());
93 *bytes_written
= callback
.result();
97 std::string
TestHostResolverPrivate::CheckHTTPResponse(
98 pp::TCPSocketPrivate
* socket
,
99 const std::string
& request
,
100 const std::string
& response
) {
102 ASSERT_SUBTEST_SUCCESS(
103 SyncWrite(socket
, request
.c_str(), static_cast<int32_t>(request
.size()),
105 std::vector
<char> response_buffer(response
.size());
106 ASSERT_SUBTEST_SUCCESS(
107 SyncRead(socket
, &response_buffer
[0],
108 static_cast<int32_t>(response
.size()), &rv
));
109 std::string
actual_response(&response_buffer
[0], rv
);
110 if (response
!= actual_response
) {
111 return "CheckHTTPResponse failed, expected: " + response
+
112 ", actual: " + actual_response
;
117 std::string
TestHostResolverPrivate::SyncResolve(
118 pp::HostResolverPrivate
* host_resolver
,
119 const std::string
& host
,
121 const PP_HostResolver_Private_Hint
& hint
) {
122 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
123 callback
.WaitForResult(
124 host_resolver
->Resolve(host
, port
, hint
, callback
.GetCallback()));
125 CHECK_CALLBACK_BEHAVIOR(callback
);
129 std::string
TestHostResolverPrivate::TestEmpty() {
130 pp::HostResolverPrivate
host_resolver(instance_
);
131 ASSERT_EQ(0, host_resolver
.GetSize());
132 PP_NetAddress_Private address
;
133 ASSERT_FALSE(host_resolver
.GetNetAddress(0, &address
));
138 std::string
TestHostResolverPrivate::ParametrizedTestResolve(
139 const PP_HostResolver_Private_Hint
&hint
) {
140 pp::HostResolverPrivate
host_resolver(instance_
);
142 ASSERT_SUBTEST_SUCCESS(SyncResolve(&host_resolver
, host_
, port_
, hint
));
144 const size_t size
= host_resolver
.GetSize();
145 ASSERT_TRUE(size
>= 1);
147 PP_NetAddress_Private address
;
148 for (size_t i
= 0; i
< size
; ++i
) {
149 ASSERT_TRUE(host_resolver
.GetNetAddress(
150 static_cast<uint32_t>(i
), &address
));
152 pp::TCPSocketPrivate
socket(instance_
);
153 ASSERT_SUBTEST_SUCCESS(SyncConnect(&socket
, address
));
154 ASSERT_SUBTEST_SUCCESS(CheckHTTPResponse(&socket
,
155 "GET / HTTP/1.0\r\n\r\n",
160 ASSERT_FALSE(host_resolver
.GetNetAddress(
161 static_cast<uint32_t>(size
), &address
));
162 pp::Var canonical_name
= host_resolver
.GetCanonicalName();
163 ASSERT_TRUE(canonical_name
.is_string());
164 pp::TCPSocketPrivate
socket(instance_
);
165 ASSERT_SUBTEST_SUCCESS(SyncConnect(&socket
,
166 canonical_name
.AsString(),
168 ASSERT_SUBTEST_SUCCESS(CheckHTTPResponse(&socket
,
169 "GET / HTTP/1.0\r\n\r\n",
176 std::string
TestHostResolverPrivate::TestResolve() {
177 PP_HostResolver_Private_Hint hint
;
178 hint
.family
= PP_NETADDRESSFAMILY_PRIVATE_UNSPECIFIED
;
179 hint
.flags
= PP_HOST_RESOLVER_PRIVATE_FLAGS_CANONNAME
;
180 return ParametrizedTestResolve(hint
);
183 std::string
TestHostResolverPrivate::TestResolveIPv4() {
184 PP_HostResolver_Private_Hint hint
;
185 hint
.family
= PP_NETADDRESSFAMILY_PRIVATE_IPV4
;
186 hint
.flags
= PP_HOST_RESOLVER_PRIVATE_FLAGS_CANONNAME
;
187 return ParametrizedTestResolve(hint
);