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