[email protected] | 8ffeeeb4 | 2012-03-22 00:49:29 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 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 "ppapi/tests/test_tcp_socket_private.h" |
| 6 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 10 | #include <new> |
| 11 | |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 12 | #include "ppapi/cpp/private/tcp_socket_private.h" |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 13 | #include "ppapi/tests/test_utils.h" |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 14 | #include "ppapi/tests/testing_instance.h" |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // Validates the first line of an HTTP response. |
| 19 | bool ValidateHttpResponse(const std::string& s) { |
| 20 | // Just check that it begins with "HTTP/" and ends with a "\r\n". |
| 21 | return s.size() >= 5 && |
| 22 | s.substr(0, 5) == "HTTP/" && |
| 23 | s.substr(s.size() - 2) == "\r\n"; |
| 24 | } |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | REGISTER_TEST_CASE(TCPSocketPrivate); |
| 29 | |
| 30 | TestTCPSocketPrivate::TestTCPSocketPrivate(TestingInstance* instance) |
| 31 | : TestCase(instance) { |
| 32 | } |
| 33 | |
| 34 | bool TestTCPSocketPrivate::Init() { |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 35 | if (!pp::TCPSocketPrivate::IsAvailable()) |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 36 | return false; |
| 37 | |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 38 | // We need something to connect to, so we connect to the HTTP server whence we |
| 39 | // came. Grab the host and port. |
| 40 | if (!EnsureRunningOverHTTP()) |
| 41 | return false; |
| 42 | |
[email protected] | 8ffeeeb4 | 2012-03-22 00:49:29 | [diff] [blame] | 43 | if (!GetLocalHostPort(instance_->pp_instance(), &host_, &port_)) |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 44 | return false; |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 45 | |
[email protected] | 6c11340 | 2012-03-23 01:31:51 | [diff] [blame] | 46 | // Get the port for the SSL server. |
| 47 | ssl_port_ = instance_->ssl_server_port(); |
| 48 | |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
[email protected] | 2622d6b | 2011-11-16 04:28:02 | [diff] [blame] | 52 | void TestTCPSocketPrivate::RunTests(const std::string& filter) { |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 53 | RUN_CALLBACK_TEST(TestTCPSocketPrivate, Basic, filter); |
| 54 | RUN_CALLBACK_TEST(TestTCPSocketPrivate, ReadWrite, filter); |
| 55 | RUN_CALLBACK_TEST(TestTCPSocketPrivate, ReadWriteSSL, filter); |
| 56 | RUN_CALLBACK_TEST(TestTCPSocketPrivate, ConnectAddress, filter); |
| 57 | RUN_CALLBACK_TEST(TestTCPSocketPrivate, SetOption, filter); |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 58 | RUN_CALLBACK_TEST(TestTCPSocketPrivate, LargeRead, filter); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | std::string TestTCPSocketPrivate::TestBasic() { |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 62 | pp::TCPSocketPrivate socket(instance_); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 63 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 64 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 65 | cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback())); |
| 66 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 67 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 68 | |
| 69 | PP_NetAddress_Private unused; |
| 70 | // TODO(viettrungluu): check the values somehow. |
| 71 | ASSERT_TRUE(socket.GetLocalAddress(&unused)); |
| 72 | ASSERT_TRUE(socket.GetRemoteAddress(&unused)); |
| 73 | |
| 74 | socket.Disconnect(); |
| 75 | |
| 76 | PASS(); |
| 77 | } |
| 78 | |
| 79 | std::string TestTCPSocketPrivate::TestReadWrite() { |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 80 | pp::TCPSocketPrivate socket(instance_); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 81 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 82 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 83 | cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback())); |
| 84 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 85 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 86 | |
| 87 | ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); |
| 88 | |
| 89 | // Read up to the first \n and check that it looks like valid HTTP response. |
| 90 | std::string s; |
| 91 | ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
| 92 | ASSERT_TRUE(ValidateHttpResponse(s)); |
| 93 | |
| 94 | socket.Disconnect(); |
| 95 | |
| 96 | PASS(); |
| 97 | } |
| 98 | |
[email protected] | 6c11340 | 2012-03-23 01:31:51 | [diff] [blame] | 99 | std::string TestTCPSocketPrivate::TestReadWriteSSL() { |
| 100 | pp::TCPSocketPrivate socket(instance_); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 101 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | 6c11340 | 2012-03-23 01:31:51 | [diff] [blame] | 102 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 103 | cb.WaitForResult(socket.Connect(host_.c_str(), ssl_port_, cb.GetCallback())); |
| 104 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 105 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | 6c11340 | 2012-03-23 01:31:51 | [diff] [blame] | 106 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 107 | cb.WaitForResult( |
| 108 | socket.SSLHandshake(host_.c_str(), ssl_port_, cb.GetCallback())); |
| 109 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 110 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | 6c11340 | 2012-03-23 01:31:51 | [diff] [blame] | 111 | |
| 112 | ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); |
| 113 | |
| 114 | // Read up to the first \n and check that it looks like valid HTTP response. |
| 115 | std::string s; |
| 116 | ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
| 117 | ASSERT_TRUE(ValidateHttpResponse(s)); |
| 118 | |
| 119 | socket.Disconnect(); |
| 120 | |
| 121 | PASS(); |
| 122 | } |
| 123 | |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 124 | std::string TestTCPSocketPrivate::TestConnectAddress() { |
| 125 | PP_NetAddress_Private address; |
| 126 | |
| 127 | // First, bring up a connection and grab the address. |
| 128 | { |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 129 | pp::TCPSocketPrivate socket(instance_); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 130 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 131 | cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback())); |
| 132 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 133 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 134 | ASSERT_TRUE(socket.GetRemoteAddress(&address)); |
| 135 | // Omit the |Disconnect()| here to make sure we don't crash if we just let |
| 136 | // the resource be destroyed. |
| 137 | } |
| 138 | |
| 139 | // Connect to that address. |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 140 | pp::TCPSocketPrivate socket(instance_); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 141 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 142 | cb.WaitForResult(socket.ConnectWithNetAddress(&address, cb.GetCallback())); |
| 143 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 144 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 145 | |
| 146 | // Make sure we can read/write to it properly (see |TestReadWrite()|). |
| 147 | ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); |
| 148 | std::string s; |
| 149 | ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
| 150 | ASSERT_TRUE(ValidateHttpResponse(s)); |
| 151 | |
| 152 | socket.Disconnect(); |
| 153 | |
| 154 | PASS(); |
| 155 | } |
| 156 | |
[email protected] | 466a583 | 2013-02-22 11:17:08 | [diff] [blame] | 157 | std::string TestTCPSocketPrivate::TestSetOption() { |
| 158 | pp::TCPSocketPrivate socket(instance_); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 159 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | 466a583 | 2013-02-22 11:17:08 | [diff] [blame] | 160 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 161 | cb.WaitForResult( |
[email protected] | ddecdae | 2013-06-24 23:17:46 | [diff] [blame] | 162 | socket.SetOption(PP_TCPSOCKETOPTION_PRIVATE_NO_DELAY, true, |
| 163 | cb.GetCallback())); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 164 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 165 | ASSERT_EQ(PP_ERROR_FAILED, cb.result()); |
[email protected] | 466a583 | 2013-02-22 11:17:08 | [diff] [blame] | 166 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 167 | cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback())); |
| 168 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 169 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | 466a583 | 2013-02-22 11:17:08 | [diff] [blame] | 170 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 171 | cb.WaitForResult( |
[email protected] | ddecdae | 2013-06-24 23:17:46 | [diff] [blame] | 172 | socket.SetOption(PP_TCPSOCKETOPTION_PRIVATE_NO_DELAY, true, |
| 173 | cb.GetCallback())); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 174 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 175 | ASSERT_EQ(PP_OK, cb.result()); |
[email protected] | 466a583 | 2013-02-22 11:17:08 | [diff] [blame] | 176 | |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 177 | cb.WaitForResult( |
[email protected] | ddecdae | 2013-06-24 23:17:46 | [diff] [blame] | 178 | socket.SetOption(PP_TCPSOCKETOPTION_PRIVATE_INVALID, true, |
| 179 | cb.GetCallback())); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 180 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 181 | ASSERT_EQ(PP_ERROR_BADARGUMENT, cb.result()); |
[email protected] | 466a583 | 2013-02-22 11:17:08 | [diff] [blame] | 182 | |
| 183 | socket.Disconnect(); |
| 184 | |
| 185 | PASS(); |
| 186 | } |
| 187 | |
[email protected] | 8522332e | 2013-08-28 19:42:59 | [diff] [blame] | 188 | std::string TestTCPSocketPrivate::TestLargeRead() { |
| 189 | pp::TCPSocketPrivate socket(instance_); |
| 190 | { |
| 191 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 192 | |
| 193 | cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback())); |
| 194 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 195 | ASSERT_EQ(PP_OK, cb.result()); |
| 196 | } |
| 197 | |
| 198 | ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); |
| 199 | |
| 200 | const size_t kReadSize = 1024 * 1024 + 32; |
| 201 | // Create large buffer in heap to prevent run-time errors related to |
| 202 | // limits on stack size. |
| 203 | char* buffer = new (std::nothrow) char[kReadSize]; |
| 204 | ASSERT_TRUE(buffer != NULL); |
| 205 | |
| 206 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
| 207 | cb.WaitForResult(socket.Read(buffer, kReadSize * sizeof(*buffer), |
| 208 | cb.GetCallback())); |
| 209 | CHECK_CALLBACK_BEHAVIOR(cb); |
| 210 | ASSERT_LE(0, cb.result()); |
| 211 | |
| 212 | delete [] buffer; |
| 213 | |
| 214 | PASS(); |
| 215 | } |
| 216 | |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 217 | int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket( |
| 218 | pp::TCPSocketPrivate* socket, |
| 219 | std::string* s) { |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 220 | char buffer[10000]; |
| 221 | |
| 222 | s->clear(); |
| 223 | // Make sure we don't just hang if |Read()| spews. |
| 224 | while (s->size() < 1000000) { |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 225 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
[email protected] | 599aeffdc | 2013-03-15 11:06:23 | [diff] [blame] | 226 | int32_t rv = socket->Read(buffer, sizeof(buffer), cb.GetCallback()); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 227 | if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING) |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 228 | return PP_ERROR_FAILED; |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 229 | cb.WaitForResult(rv); |
| 230 | if (cb.result() < 0) |
| 231 | return cb.result(); |
| 232 | if (cb.result() == 0) |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 233 | return PP_ERROR_FAILED; // Didn't get a \n-terminated line. |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 234 | s->reserve(s->size() + cb.result()); |
| 235 | for (int32_t i = 0; i < cb.result(); i++) { |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 236 | s->push_back(buffer[i]); |
| 237 | if (buffer[i] == '\n') |
| 238 | return PP_OK; |
| 239 | } |
| 240 | } |
| 241 | return PP_ERROR_FAILED; |
| 242 | } |
| 243 | |
[email protected] | fb575bc | 2011-11-16 07:06:24 | [diff] [blame] | 244 | int32_t TestTCPSocketPrivate::WriteStringToSocket(pp::TCPSocketPrivate* socket, |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 245 | const std::string& s) { |
| 246 | const char* buffer = s.data(); |
| 247 | size_t written = 0; |
| 248 | while (written < s.size()) { |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 249 | TestCompletionCallback cb(instance_->pp_instance(), callback_type()); |
brettw | 669d47b1 | 2015-02-13 21:17:38 | [diff] [blame] | 250 | int32_t rv = socket->Write(buffer + written, |
| 251 | static_cast<int32_t>(s.size() - written), |
[email protected] | 599aeffdc | 2013-03-15 11:06:23 | [diff] [blame] | 252 | cb.GetCallback()); |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 253 | if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING) |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 254 | return PP_ERROR_FAILED; |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 255 | cb.WaitForResult(rv); |
| 256 | if (cb.result() < 0) |
| 257 | return cb.result(); |
| 258 | if (cb.result() == 0) |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 259 | return PP_ERROR_FAILED; |
[email protected] | d1674cc4 | 2013-04-16 15:06:26 | [diff] [blame] | 260 | written += cb.result(); |
[email protected] | cfff9eb8 | 2011-11-15 03:17:10 | [diff] [blame] | 261 | } |
| 262 | if (written != s.size()) |
| 263 | return PP_ERROR_FAILED; |
| 264 | return PP_OK; |
| 265 | } |