| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_tcp_socket_private.h" | 5 #include "ppapi/tests/test_tcp_socket_private.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include "ppapi/c/dev/ppb_url_util_dev.h" | |
| 10 #include "ppapi/cpp/dev/url_util_dev.h" | |
| 11 #include "ppapi/cpp/private/tcp_socket_private.h" | 9 #include "ppapi/cpp/private/tcp_socket_private.h" |
| 12 #include "ppapi/cpp/var.h" | |
| 13 #include "ppapi/tests/testing_instance.h" | 10 #include "ppapi/tests/testing_instance.h" |
| 14 #include "ppapi/tests/test_utils.h" | 11 #include "ppapi/tests/test_utils.h" |
| 15 | 12 |
| 16 namespace { | 13 namespace { |
| 17 | 14 |
| 18 // Validates the first line of an HTTP response. | 15 // Validates the first line of an HTTP response. |
| 19 bool ValidateHttpResponse(const std::string& s) { | 16 bool ValidateHttpResponse(const std::string& s) { |
| 20 // Just check that it begins with "HTTP/" and ends with a "\r\n". | 17 // Just check that it begins with "HTTP/" and ends with a "\r\n". |
| 21 return s.size() >= 5 && | 18 return s.size() >= 5 && |
| 22 s.substr(0, 5) == "HTTP/" && | 19 s.substr(0, 5) == "HTTP/" && |
| 23 s.substr(s.size() - 2) == "\r\n"; | 20 s.substr(s.size() - 2) == "\r\n"; |
| 24 } | 21 } |
| 25 | 22 |
| 26 } // namespace | 23 } // namespace |
| 27 | 24 |
| 28 REGISTER_TEST_CASE(TCPSocketPrivate); | 25 REGISTER_TEST_CASE(TCPSocketPrivate); |
| 29 | 26 |
| 30 TestTCPSocketPrivate::TestTCPSocketPrivate(TestingInstance* instance) | 27 TestTCPSocketPrivate::TestTCPSocketPrivate(TestingInstance* instance) |
| 31 : TestCase(instance) { | 28 : TestCase(instance) { |
| 32 } | 29 } |
| 33 | 30 |
| 34 bool TestTCPSocketPrivate::Init() { | 31 bool TestTCPSocketPrivate::Init() { |
| 35 if (!pp::TCPSocketPrivate::IsAvailable()) | 32 if (!pp::TCPSocketPrivate::IsAvailable()) |
| 36 return false; | 33 return false; |
| 37 | 34 |
| 38 // This test currently only works out-of-process (since the API is really only | |
| 39 // implemented in that case). | |
| 40 const PPB_Testing_Dev* testing = GetTestingInterface(); | |
| 41 if (!testing) | |
| 42 return false; | |
| 43 if (!testing->IsOutOfProcess()) | |
| 44 return false; | |
| 45 | |
| 46 // We need something to connect to, so we connect to the HTTP server whence we | 35 // We need something to connect to, so we connect to the HTTP server whence we |
| 47 // came. Grab the host and port. | 36 // came. Grab the host and port. |
| 48 if (!EnsureRunningOverHTTP()) | 37 if (!EnsureRunningOverHTTP()) |
| 49 return false; | 38 return false; |
| 50 | 39 |
| 51 PP_URLComponents_Dev components; | 40 if (!GetLocalHostPort(instance_->pp_instance(), &host_, &port_)) |
| 52 pp::Var pp_url = pp::URLUtil_Dev::Get()->GetDocumentURL(instance_, | |
| 53 &components); | |
| 54 if (!pp_url.is_string()) | |
| 55 return false; | |
| 56 std::string url = pp_url.AsString(); | |
| 57 | |
| 58 // Double-check that we're running on HTTP. | |
| 59 if (components.scheme.len < 0) | |
| 60 return false; | |
| 61 if (url.substr(components.scheme.begin, components.scheme.len) != "http") | |
| 62 return false; | 41 return false; |
| 63 | 42 |
| 64 // Get host. | 43 // Get the port for the SSL server. |
| 65 if (components.host.len < 0) | 44 ssl_port_ = instance_->ssl_server_port(); |
| 66 return false; | |
| 67 host_ = url.substr(components.host.begin, components.host.len); | |
| 68 | |
| 69 // Get port (it's optional). | |
| 70 port_ = 80; // Default value. | |
| 71 if (components.port.len > 0) { | |
| 72 int i = atoi(url.substr(components.port.begin, | |
| 73 components.port.len).c_str()); | |
| 74 if (i < 0 || i > 65535) | |
| 75 return false; | |
| 76 port_ = static_cast<uint16_t>(i); | |
| 77 } | |
| 78 | 45 |
| 79 return true; | 46 return true; |
| 80 } | 47 } |
| 81 | 48 |
| 82 void TestTCPSocketPrivate::RunTests(const std::string& filter) { | 49 void TestTCPSocketPrivate::RunTests(const std::string& filter) { |
| 83 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); | 50 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); |
| 84 RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite, filter); | 51 RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite, filter); |
| 52 RUN_TEST_FORCEASYNC_AND_NOT(ReadWriteSSL, filter); |
| 85 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress, filter); | 53 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress, filter); |
| 86 } | 54 } |
| 87 | 55 |
| 88 std::string TestTCPSocketPrivate::TestBasic() { | 56 std::string TestTCPSocketPrivate::TestBasic() { |
| 89 pp::TCPSocketPrivate socket(instance_); | 57 pp::TCPSocketPrivate socket(instance_); |
| 90 TestCompletionCallback cb(instance_->pp_instance(), force_async_); | 58 TestCompletionCallback cb(instance_->pp_instance(), force_async_); |
| 91 | 59 |
| 92 int32_t rv = socket.Connect(host_.c_str(), port_, cb); | 60 int32_t rv = socket.Connect(host_.c_str(), port_, cb); |
| 93 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); | 61 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); |
| 94 if (rv == PP_OK_COMPLETIONPENDING) | 62 if (rv == PP_OK_COMPLETIONPENDING) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 120 // Read up to the first \n and check that it looks like valid HTTP response. | 88 // Read up to the first \n and check that it looks like valid HTTP response. |
| 121 std::string s; | 89 std::string s; |
| 122 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); | 90 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
| 123 ASSERT_TRUE(ValidateHttpResponse(s)); | 91 ASSERT_TRUE(ValidateHttpResponse(s)); |
| 124 | 92 |
| 125 socket.Disconnect(); | 93 socket.Disconnect(); |
| 126 | 94 |
| 127 PASS(); | 95 PASS(); |
| 128 } | 96 } |
| 129 | 97 |
| 98 std::string TestTCPSocketPrivate::TestReadWriteSSL() { |
| 99 pp::TCPSocketPrivate socket(instance_); |
| 100 TestCompletionCallback cb(instance_->pp_instance(), force_async_); |
| 101 |
| 102 int32_t rv = socket.Connect(host_.c_str(), ssl_port_, cb); |
| 103 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); |
| 104 if (rv == PP_OK_COMPLETIONPENDING) |
| 105 rv = cb.WaitForResult(); |
| 106 ASSERT_EQ(PP_OK, rv); |
| 107 |
| 108 rv = socket.SSLHandshake(host_.c_str(), ssl_port_, cb); |
| 109 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); |
| 110 if (rv == PP_OK_COMPLETIONPENDING) |
| 111 rv = cb.WaitForResult(); |
| 112 ASSERT_EQ(PP_OK, rv); |
| 113 |
| 114 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); |
| 115 |
| 116 // Read up to the first \n and check that it looks like valid HTTP response. |
| 117 std::string s; |
| 118 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
| 119 ASSERT_TRUE(ValidateHttpResponse(s)); |
| 120 |
| 121 socket.Disconnect(); |
| 122 |
| 123 PASS(); |
| 124 } |
| 125 |
| 130 std::string TestTCPSocketPrivate::TestConnectAddress() { | 126 std::string TestTCPSocketPrivate::TestConnectAddress() { |
| 131 PP_NetAddress_Private address; | 127 PP_NetAddress_Private address; |
| 132 | 128 |
| 133 // First, bring up a connection and grab the address. | 129 // First, bring up a connection and grab the address. |
| 134 { | 130 { |
| 135 pp::TCPSocketPrivate socket(instance_); | 131 pp::TCPSocketPrivate socket(instance_); |
| 136 TestCompletionCallback cb(instance_->pp_instance(), force_async_); | 132 TestCompletionCallback cb(instance_->pp_instance(), force_async_); |
| 137 int32_t rv = socket.Connect(host_.c_str(), port_, cb); | 133 int32_t rv = socket.Connect(host_.c_str(), port_, cb); |
| 138 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); | 134 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); |
| 139 if (rv == PP_OK_COMPLETIONPENDING) | 135 if (rv == PP_OK_COMPLETIONPENDING) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 157 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); | 153 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); |
| 158 std::string s; | 154 std::string s; |
| 159 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); | 155 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
| 160 ASSERT_TRUE(ValidateHttpResponse(s)); | 156 ASSERT_TRUE(ValidateHttpResponse(s)); |
| 161 | 157 |
| 162 socket.Disconnect(); | 158 socket.Disconnect(); |
| 163 | 159 |
| 164 PASS(); | 160 PASS(); |
| 165 } | 161 } |
| 166 | 162 |
| 167 // TODO(viettrungluu): Try testing SSL somehow. | |
| 168 | |
| 169 int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket( | 163 int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket( |
| 170 pp::TCPSocketPrivate* socket, | 164 pp::TCPSocketPrivate* socket, |
| 171 std::string* s) { | 165 std::string* s) { |
| 172 char buffer[10000]; | 166 char buffer[10000]; |
| 173 | 167 |
| 174 s->clear(); | 168 s->clear(); |
| 175 // Make sure we don't just hang if |Read()| spews. | 169 // Make sure we don't just hang if |Read()| spews. |
| 176 while (s->size() < 1000000) { | 170 while (s->size() < 1000000) { |
| 177 TestCompletionCallback cb(instance_->pp_instance(), force_async_); | 171 TestCompletionCallback cb(instance_->pp_instance(), force_async_); |
| 178 int32_t rv = socket->Read(buffer, sizeof(buffer), cb); | 172 int32_t rv = socket->Read(buffer, sizeof(buffer), cb); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 208 if (rv < 0) | 202 if (rv < 0) |
| 209 return rv; | 203 return rv; |
| 210 if (rv == 0) | 204 if (rv == 0) |
| 211 return PP_ERROR_FAILED; | 205 return PP_ERROR_FAILED; |
| 212 written += rv; | 206 written += rv; |
| 213 } | 207 } |
| 214 if (written != s.size()) | 208 if (written != s.size()) |
| 215 return PP_ERROR_FAILED; | 209 return PP_ERROR_FAILED; |
| 216 return PP_OK; | 210 return PP_OK; |
| 217 } | 211 } |
| OLD | NEW |