blob: ae7b47259bef487a6c40ceaf1b0e925318043617 [file] [log] [blame]
[email protected]8ffeeeb42012-03-22 00:49:291// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]cfff9eb82011-11-15 03:17:102// 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
avie029c4132015-12-23 06:45:227#include <stddef.h>
[email protected]cfff9eb82011-11-15 03:17:108#include <stdlib.h>
9
[email protected]8522332e2013-08-28 19:42:5910#include <new>
11
[email protected]fb575bc2011-11-16 07:06:2412#include "ppapi/cpp/private/tcp_socket_private.h"
[email protected]cfff9eb82011-11-15 03:17:1013#include "ppapi/tests/test_utils.h"
[email protected]8522332e2013-08-28 19:42:5914#include "ppapi/tests/testing_instance.h"
[email protected]cfff9eb82011-11-15 03:17:1015
16namespace {
17
18// Validates the first line of an HTTP response.
19bool 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
28REGISTER_TEST_CASE(TCPSocketPrivate);
29
30TestTCPSocketPrivate::TestTCPSocketPrivate(TestingInstance* instance)
31 : TestCase(instance) {
32}
33
34bool TestTCPSocketPrivate::Init() {
[email protected]fb575bc2011-11-16 07:06:2435 if (!pp::TCPSocketPrivate::IsAvailable())
[email protected]cfff9eb82011-11-15 03:17:1036 return false;
37
[email protected]cfff9eb82011-11-15 03:17:1038 // 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]8ffeeeb42012-03-22 00:49:2943 if (!GetLocalHostPort(instance_->pp_instance(), &host_, &port_))
[email protected]cfff9eb82011-11-15 03:17:1044 return false;
[email protected]cfff9eb82011-11-15 03:17:1045
[email protected]6c113402012-03-23 01:31:5146 // Get the port for the SSL server.
47 ssl_port_ = instance_->ssl_server_port();
48
[email protected]cfff9eb82011-11-15 03:17:1049 return true;
50}
51
[email protected]2622d6b2011-11-16 04:28:0252void TestTCPSocketPrivate::RunTests(const std::string& filter) {
[email protected]d1674cc42013-04-16 15:06:2653 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]8522332e2013-08-28 19:42:5958 RUN_CALLBACK_TEST(TestTCPSocketPrivate, LargeRead, filter);
[email protected]cfff9eb82011-11-15 03:17:1059}
60
61std::string TestTCPSocketPrivate::TestBasic() {
[email protected]fb575bc2011-11-16 07:06:2462 pp::TCPSocketPrivate socket(instance_);
[email protected]d1674cc42013-04-16 15:06:2663 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
[email protected]cfff9eb82011-11-15 03:17:1064
[email protected]d1674cc42013-04-16 15:06:2665 cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback()));
66 CHECK_CALLBACK_BEHAVIOR(cb);
67 ASSERT_EQ(PP_OK, cb.result());
[email protected]cfff9eb82011-11-15 03:17:1068
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
79std::string TestTCPSocketPrivate::TestReadWrite() {
[email protected]fb575bc2011-11-16 07:06:2480 pp::TCPSocketPrivate socket(instance_);
[email protected]d1674cc42013-04-16 15:06:2681 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
[email protected]cfff9eb82011-11-15 03:17:1082
[email protected]d1674cc42013-04-16 15:06:2683 cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback()));
84 CHECK_CALLBACK_BEHAVIOR(cb);
85 ASSERT_EQ(PP_OK, cb.result());
[email protected]cfff9eb82011-11-15 03:17:1086
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]6c113402012-03-23 01:31:5199std::string TestTCPSocketPrivate::TestReadWriteSSL() {
100 pp::TCPSocketPrivate socket(instance_);
[email protected]d1674cc42013-04-16 15:06:26101 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
[email protected]6c113402012-03-23 01:31:51102
[email protected]d1674cc42013-04-16 15:06:26103 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]6c113402012-03-23 01:31:51106
[email protected]d1674cc42013-04-16 15:06:26107 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]6c113402012-03-23 01:31:51111
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]cfff9eb82011-11-15 03:17:10124std::string TestTCPSocketPrivate::TestConnectAddress() {
125 PP_NetAddress_Private address;
126
127 // First, bring up a connection and grab the address.
128 {
[email protected]fb575bc2011-11-16 07:06:24129 pp::TCPSocketPrivate socket(instance_);
[email protected]d1674cc42013-04-16 15:06:26130 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]cfff9eb82011-11-15 03:17:10134 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]fb575bc2011-11-16 07:06:24140 pp::TCPSocketPrivate socket(instance_);
[email protected]d1674cc42013-04-16 15:06:26141 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]cfff9eb82011-11-15 03:17:10145
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]466a5832013-02-22 11:17:08157std::string TestTCPSocketPrivate::TestSetOption() {
158 pp::TCPSocketPrivate socket(instance_);
[email protected]d1674cc42013-04-16 15:06:26159 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
[email protected]466a5832013-02-22 11:17:08160
[email protected]d1674cc42013-04-16 15:06:26161 cb.WaitForResult(
[email protected]ddecdae2013-06-24 23:17:46162 socket.SetOption(PP_TCPSOCKETOPTION_PRIVATE_NO_DELAY, true,
163 cb.GetCallback()));
[email protected]d1674cc42013-04-16 15:06:26164 CHECK_CALLBACK_BEHAVIOR(cb);
165 ASSERT_EQ(PP_ERROR_FAILED, cb.result());
[email protected]466a5832013-02-22 11:17:08166
[email protected]d1674cc42013-04-16 15:06:26167 cb.WaitForResult(socket.Connect(host_.c_str(), port_, cb.GetCallback()));
168 CHECK_CALLBACK_BEHAVIOR(cb);
169 ASSERT_EQ(PP_OK, cb.result());
[email protected]466a5832013-02-22 11:17:08170
[email protected]d1674cc42013-04-16 15:06:26171 cb.WaitForResult(
[email protected]ddecdae2013-06-24 23:17:46172 socket.SetOption(PP_TCPSOCKETOPTION_PRIVATE_NO_DELAY, true,
173 cb.GetCallback()));
[email protected]d1674cc42013-04-16 15:06:26174 CHECK_CALLBACK_BEHAVIOR(cb);
175 ASSERT_EQ(PP_OK, cb.result());
[email protected]466a5832013-02-22 11:17:08176
[email protected]d1674cc42013-04-16 15:06:26177 cb.WaitForResult(
[email protected]ddecdae2013-06-24 23:17:46178 socket.SetOption(PP_TCPSOCKETOPTION_PRIVATE_INVALID, true,
179 cb.GetCallback()));
[email protected]d1674cc42013-04-16 15:06:26180 CHECK_CALLBACK_BEHAVIOR(cb);
181 ASSERT_EQ(PP_ERROR_BADARGUMENT, cb.result());
[email protected]466a5832013-02-22 11:17:08182
183 socket.Disconnect();
184
185 PASS();
186}
187
[email protected]8522332e2013-08-28 19:42:59188std::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]fb575bc2011-11-16 07:06:24217int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket(
218 pp::TCPSocketPrivate* socket,
219 std::string* s) {
[email protected]cfff9eb82011-11-15 03:17:10220 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]d1674cc42013-04-16 15:06:26225 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
[email protected]599aeffdc2013-03-15 11:06:23226 int32_t rv = socket->Read(buffer, sizeof(buffer), cb.GetCallback());
[email protected]d1674cc42013-04-16 15:06:26227 if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING)
[email protected]cfff9eb82011-11-15 03:17:10228 return PP_ERROR_FAILED;
[email protected]d1674cc42013-04-16 15:06:26229 cb.WaitForResult(rv);
230 if (cb.result() < 0)
231 return cb.result();
232 if (cb.result() == 0)
[email protected]cfff9eb82011-11-15 03:17:10233 return PP_ERROR_FAILED; // Didn't get a \n-terminated line.
[email protected]d1674cc42013-04-16 15:06:26234 s->reserve(s->size() + cb.result());
235 for (int32_t i = 0; i < cb.result(); i++) {
[email protected]cfff9eb82011-11-15 03:17:10236 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]fb575bc2011-11-16 07:06:24244int32_t TestTCPSocketPrivate::WriteStringToSocket(pp::TCPSocketPrivate* socket,
[email protected]cfff9eb82011-11-15 03:17:10245 const std::string& s) {
246 const char* buffer = s.data();
247 size_t written = 0;
248 while (written < s.size()) {
[email protected]d1674cc42013-04-16 15:06:26249 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
brettw669d47b12015-02-13 21:17:38250 int32_t rv = socket->Write(buffer + written,
251 static_cast<int32_t>(s.size() - written),
[email protected]599aeffdc2013-03-15 11:06:23252 cb.GetCallback());
[email protected]d1674cc42013-04-16 15:06:26253 if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING)
[email protected]cfff9eb82011-11-15 03:17:10254 return PP_ERROR_FAILED;
[email protected]d1674cc42013-04-16 15:06:26255 cb.WaitForResult(rv);
256 if (cb.result() < 0)
257 return cb.result();
258 if (cb.result() == 0)
[email protected]cfff9eb82011-11-15 03:17:10259 return PP_ERROR_FAILED;
[email protected]d1674cc42013-04-16 15:06:26260 written += cb.result();
[email protected]cfff9eb82011-11-15 03:17:10261 }
262 if (written != s.size())
263 return PP_ERROR_FAILED;
264 return PP_OK;
265}