Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include <string.h> | |
| 6 | |
| 7 #include "ppapi/cpp/module.h" | |
| 8 #include "ppapi/tests/test_tcp_socket_private_shared.h" | |
|
polina
2011/11/29 05:20:47
this should be the first include (see style guide)
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 9 #include "ppapi/tests/test_utils.h" | |
| 10 #include "ppapi/tests/testing_instance.h" | |
| 11 | |
| 12 REGISTER_TEST_CASE(TCPSocketPrivateShared); | |
| 13 | |
| 14 static const char* const kSuccess = ""; | |
| 15 | |
| 16 bool TestTCPSocketPrivateShared::Init() { | |
| 17 if (!InitHostAndPort()) | |
| 18 return false; | |
| 19 | |
| 20 tcp_socket_private_interface_ = | |
| 21 reinterpret_cast<PPB_TCPSocket_Private const*>( | |
| 22 pp::Module::Get()->GetBrowserInterface( | |
| 23 PPB_TCPSOCKET_PRIVATE_INTERFACE)); | |
| 24 return tcp_socket_private_interface_ && InitTestingInterface(); | |
|
polina
2011/11/29 05:20:47
It would be helpful to set the error string, at le
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 25 } | |
| 26 | |
| 27 void TestTCPSocketPrivateShared::RunTests(const std::string& filter) { | |
| 28 RUN_TEST(Create, filter); | |
| 29 RUN_TEST(GetAddress, filter); | |
| 30 RUN_TEST(Connect, filter); | |
| 31 RUN_TEST(Reconnect, filter); | |
|
polina
2011/11/29 05:20:47
For tests running callbacks, you might want to tes
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 32 } | |
| 33 | |
| 34 void TestTCPSocketPrivateShared::QuitMessageLoop() { | |
| 35 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | |
| 36 } | |
| 37 | |
| 38 bool TestTCPSocketPrivateShared::InitHostAndPort() { | |
| 39 host_ = "www.google.com"; | |
| 40 port_ = 443; | |
| 41 return true; | |
|
polina
2011/11/29 05:20:47
Why does this return a bool if it always succeeds?
ygorshenin
2011/11/29 13:25:18
Fields initialization moved to constructor.
| |
| 42 } | |
| 43 | |
| 44 std::string TestTCPSocketPrivateShared::CreateSocket(PP_Resource* socket) { | |
| 45 *socket = tcp_socket_private_interface_->Create(instance_->pp_instance()); | |
| 46 ASSERT_NE(0, *socket); | |
| 47 ASSERT_TRUE(tcp_socket_private_interface_->IsTCPSocket(*socket)); | |
|
polina
2011/11/29 05:20:47
For diagnostics purposes, it would be more helpful
ygorshenin
2011/11/29 13:25:18
Good idea. But in this particular method ReportErr
| |
| 48 return kSuccess; | |
| 49 } | |
| 50 | |
| 51 std::string TestTCPSocketPrivateShared::SyncConnect(PP_Resource socket, | |
| 52 const char* host, | |
| 53 int port) { | |
| 54 TestCompletionCallback callback(instance_->pp_instance(), true); | |
|
polina
2011/11/29 05:20:47
or use force_async_setting instead of true so you
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 55 pp::CompletionCallback cb = (pp::CompletionCallback) callback; | |
|
polina
2011/11/29 05:20:47
must not use c-style casts (see style guide)
and y
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 56 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 57 tcp_socket_private_interface_->Connect( | |
| 58 socket, host, port, | |
| 59 cb.pp_completion_callback())); | |
| 60 ASSERT_EQ(PP_OK, callback.WaitForResult()); | |
| 61 return kSuccess; | |
| 62 } | |
| 63 | |
| 64 std::string TestTCPSocketPrivateShared::SyncConnectWithNetAddress( | |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 65 PP_Resource socket, const PP_NetAddress_Private& addr) { | |
| 66 TestCompletionCallback callback(instance_->pp_instance(), true); | |
| 67 pp::CompletionCallback cb = (pp::CompletionCallback) callback; | |
| 68 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 69 tcp_socket_private_interface_->ConnectWithNetAddress( | |
| 70 socket, &addr, cb.pp_completion_callback())); | |
| 71 ASSERT_EQ(PP_OK, callback.WaitForResult()); | |
| 72 return kSuccess; | |
| 73 } | |
| 74 | |
| 75 std::string TestTCPSocketPrivateShared::SyncSSLHandshake(PP_Resource socket, | |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 76 const char* host, | |
| 77 int port) { | |
| 78 TestCompletionCallback callback(instance_->pp_instance(), true); | |
| 79 pp::CompletionCallback cb = (pp::CompletionCallback) callback; | |
| 80 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 81 tcp_socket_private_interface_->SSLHandshake( | |
| 82 socket, host, port, cb.pp_completion_callback())); | |
| 83 ASSERT_EQ(PP_OK, callback.WaitForResult()); | |
| 84 return kSuccess; | |
| 85 } | |
| 86 | |
| 87 std::string TestTCPSocketPrivateShared::SyncRead(PP_Resource socket, | |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 88 char* buffer, | |
| 89 int32_t num_bytes, | |
| 90 int32_t* bytes_read) { | |
| 91 TestCompletionCallback callback(instance_->pp_instance(), true); | |
| 92 pp::CompletionCallback cb = (pp::CompletionCallback) callback; | |
| 93 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 94 tcp_socket_private_interface_->Read(socket, buffer, num_bytes, | |
| 95 cb.pp_completion_callback())); | |
| 96 *bytes_read = callback.WaitForResult(); | |
|
polina
2011/11/29 05:20:47
no bytes_read validation?
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 97 return kSuccess; | |
| 98 } | |
| 99 | |
| 100 std::string TestTCPSocketPrivateShared::SyncWrite(PP_Resource socket, | |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 101 const char* buffer, | |
| 102 int32_t num_bytes, | |
| 103 int32_t* bytes_wrote) { | |
| 104 TestCompletionCallback callback(instance_->pp_instance(), true); | |
| 105 pp::CompletionCallback cb = (pp::CompletionCallback) callback; | |
| 106 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 107 tcp_socket_private_interface_->Write(socket, buffer, num_bytes, | |
| 108 cb.pp_completion_callback())); | |
| 109 *bytes_wrote = callback.WaitForResult(); | |
|
polina
2011/11/29 05:20:47
no bytes_wrote validation?
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 110 return kSuccess; | |
| 111 } | |
| 112 | |
| 113 std::string TestTCPSocketPrivateShared::CheckHTTPResponse( | |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 114 PP_Resource socket, const char* request, const char* response) { | |
| 115 int32_t rv; | |
| 116 std::string error_message; | |
| 117 | |
| 118 error_message = SyncWrite(socket, request, strlen(request), &rv); | |
| 119 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
error.empty()
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 120 return error_message; | |
| 121 ASSERT_TRUE(0 < rv); | |
| 122 ASSERT_EQ(strlen(request), static_cast<size_t>(rv)); | |
| 123 | |
| 124 static const size_t kResponseBufferSize = 1024; | |
| 125 char response_buffer[kResponseBufferSize]; | |
|
polina
2011/11/29 05:20:47
use a small buffer, so there are multiple reads
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 126 | |
| 127 error_message = SyncRead(socket, response_buffer, kResponseBufferSize, &rv); | |
| 128 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 129 return error_message; | |
| 130 ASSERT_TRUE(0 < rv); | |
| 131 | |
| 132 ASSERT_EQ(0, strncmp(response, response_buffer, | |
|
polina
2011/11/29 05:20:47
ppapi tests usually take advantage of std::string
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 133 std::min(static_cast<size_t>(rv), | |
| 134 strlen(response)))); | |
| 135 return kSuccess; | |
| 136 } | |
| 137 | |
| 138 std::string TestTCPSocketPrivateShared::TestCreate() { | |
| 139 PP_Resource socket; | |
|
polina
2011/11/29 05:20:47
please combine lines 139 and 141
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 140 | |
| 141 socket = tcp_socket_private_interface_->Create(0); | |
| 142 ASSERT_EQ(0, socket); | |
| 143 | |
| 144 std::string error_message = CreateSocket(&socket); | |
| 145 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
just return CreateSocket(...)
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 146 return error_message; | |
| 147 PASS(); | |
| 148 } | |
| 149 | |
| 150 std::string TestTCPSocketPrivateShared::TestGetAddress() { | |
| 151 PP_Resource socket; | |
| 152 std::string error_message; | |
| 153 | |
| 154 error_message = CreateSocket(&socket); | |
| 155 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 156 return error_message; | |
| 157 | |
| 158 error_message = SyncConnect(socket, host_.c_str(), port_); | |
| 159 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
ditto
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 160 return error_message; | |
| 161 | |
| 162 PP_NetAddress_Private local_address, remote_address; | |
| 163 | |
| 164 ASSERT_EQ(PP_TRUE, tcp_socket_private_interface_->GetLocalAddress( | |
| 165 socket, &local_address)); | |
| 166 ASSERT_EQ(PP_TRUE, tcp_socket_private_interface_->GetRemoteAddress( | |
| 167 socket, &remote_address)); | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 168 | |
| 169 tcp_socket_private_interface_->Disconnect(socket); | |
| 170 | |
| 171 PASS(); | |
| 172 } | |
| 173 | |
| 174 std::string TestTCPSocketPrivateShared::TestConnect() { | |
| 175 PP_Resource socket; | |
| 176 std::string error_message; | |
| 177 | |
| 178 error_message = CreateSocket(&socket); | |
| 179 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 180 return error_message; | |
| 181 error_message = SyncConnect(socket, host_.c_str(), port_); | |
| 182 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 183 return error_message; | |
| 184 error_message = SyncSSLHandshake(socket, host_.c_str(), port_); | |
| 185 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 186 return error_message; | |
| 187 error_message = | |
| 188 CheckHTTPResponse(socket, "GET /robots.txt\r\n", "HTTP/1.0 200 OK"); | |
| 189 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 190 return error_message; | |
| 191 tcp_socket_private_interface_->Disconnect(socket); | |
| 192 | |
| 193 PASS(); | |
| 194 } | |
| 195 | |
| 196 std::string TestTCPSocketPrivateShared::TestReconnect() { | |
| 197 PP_Resource socket; | |
| 198 std::string error_message; | |
| 199 | |
| 200 error_message = CreateSocket(&socket); | |
| 201 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 202 return error_message; | |
| 203 error_message = SyncConnect(socket, host_.c_str(), port_); | |
| 204 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 205 return error_message; | |
| 206 error_message = SyncSSLHandshake(socket, host_.c_str(), port_); | |
| 207 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 208 return error_message; | |
| 209 | |
| 210 PP_NetAddress_Private remote_address; | |
| 211 ASSERT_EQ(PP_TRUE, | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 212 tcp_socket_private_interface_->GetRemoteAddress( | |
| 213 socket, &remote_address)); | |
| 214 tcp_socket_private_interface_->Disconnect(socket); | |
| 215 | |
| 216 error_message = CreateSocket(&socket); | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 217 if (error_message != kSuccess) | |
| 218 return error_message; | |
| 219 error_message = SyncConnectWithNetAddress(socket, remote_address); | |
| 220 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 221 return error_message; | |
| 222 error_message = SyncSSLHandshake(socket, host_.c_str(), port_); | |
| 223 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 224 return error_message; | |
| 225 error_message = | |
| 226 CheckHTTPResponse(socket, "GET /robots.txt\r\n", "HTTP/1.0 200 OK"); | |
| 227 if (error_message != kSuccess) | |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
| |
| 228 return error_message; | |
| 229 tcp_socket_private_interface_->Disconnect(socket); | |
| 230 | |
| 231 PASS(); | |
| 232 } | |
| OLD | NEW |