1 // Copyright (c) 2012 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.
5 #include "ppapi/tests/test_tcp_socket_private.h"
9 #include "ppapi/cpp/private/tcp_socket_private.h"
10 #include "ppapi/tests/testing_instance.h"
11 #include "ppapi/tests/test_utils.h"
15 // Validates the first line of an HTTP response.
16 bool ValidateHttpResponse(const std::string
& s
) {
17 // Just check that it begins with "HTTP/" and ends with a "\r\n".
18 return s
.size() >= 5 &&
19 s
.substr(0, 5) == "HTTP/" &&
20 s
.substr(s
.size() - 2) == "\r\n";
25 REGISTER_TEST_CASE(TCPSocketPrivate
);
27 TestTCPSocketPrivate::TestTCPSocketPrivate(TestingInstance
* instance
)
28 : TestCase(instance
) {
31 bool TestTCPSocketPrivate::Init() {
32 if (!pp::TCPSocketPrivate::IsAvailable())
35 // We need something to connect to, so we connect to the HTTP server whence we
36 // came. Grab the host and port.
37 if (!EnsureRunningOverHTTP())
40 if (!GetLocalHostPort(instance_
->pp_instance(), &host_
, &port_
))
43 // Get the port for the SSL server.
44 ssl_port_
= instance_
->ssl_server_port();
49 void TestTCPSocketPrivate::RunTests(const std::string
& filter
) {
50 RUN_TEST_FORCEASYNC_AND_NOT(Basic
, filter
);
51 RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite
, filter
);
52 RUN_TEST_FORCEASYNC_AND_NOT(ReadWriteSSL
, filter
);
53 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress
, filter
);
56 std::string
TestTCPSocketPrivate::TestBasic() {
57 pp::TCPSocketPrivate
socket(instance_
);
58 TestCompletionCallback
cb(instance_
->pp_instance(), force_async_
);
60 int32_t rv
= socket
.Connect(host_
.c_str(), port_
, cb
);
61 ASSERT_TRUE(!force_async_
|| rv
== PP_OK_COMPLETIONPENDING
);
62 if (rv
== PP_OK_COMPLETIONPENDING
)
63 rv
= cb
.WaitForResult();
66 PP_NetAddress_Private unused
;
67 // TODO(viettrungluu): check the values somehow.
68 ASSERT_TRUE(socket
.GetLocalAddress(&unused
));
69 ASSERT_TRUE(socket
.GetRemoteAddress(&unused
));
76 std::string
TestTCPSocketPrivate::TestReadWrite() {
77 pp::TCPSocketPrivate
socket(instance_
);
78 TestCompletionCallback
cb(instance_
->pp_instance(), force_async_
);
80 int32_t rv
= socket
.Connect(host_
.c_str(), port_
, cb
);
81 ASSERT_TRUE(!force_async_
|| rv
== PP_OK_COMPLETIONPENDING
);
82 if (rv
== PP_OK_COMPLETIONPENDING
)
83 rv
= cb
.WaitForResult();
86 ASSERT_EQ(PP_OK
, WriteStringToSocket(&socket
, "GET / HTTP/1.0\r\n\r\n"));
88 // Read up to the first \n and check that it looks like valid HTTP response.
90 ASSERT_EQ(PP_OK
, ReadFirstLineFromSocket(&socket
, &s
));
91 ASSERT_TRUE(ValidateHttpResponse(s
));
98 std::string
TestTCPSocketPrivate::TestReadWriteSSL() {
99 pp::TCPSocketPrivate
socket(instance_
);
100 TestCompletionCallback
cb(instance_
->pp_instance(), force_async_
);
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
);
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
);
114 ASSERT_EQ(PP_OK
, WriteStringToSocket(&socket
, "GET / HTTP/1.0\r\n\r\n"));
116 // Read up to the first \n and check that it looks like valid HTTP response.
118 ASSERT_EQ(PP_OK
, ReadFirstLineFromSocket(&socket
, &s
));
119 ASSERT_TRUE(ValidateHttpResponse(s
));
126 std::string
TestTCPSocketPrivate::TestConnectAddress() {
127 PP_NetAddress_Private address
;
129 // First, bring up a connection and grab the address.
131 pp::TCPSocketPrivate
socket(instance_
);
132 TestCompletionCallback
cb(instance_
->pp_instance(), force_async_
);
133 int32_t rv
= socket
.Connect(host_
.c_str(), port_
, cb
);
134 ASSERT_TRUE(!force_async_
|| rv
== PP_OK_COMPLETIONPENDING
);
135 if (rv
== PP_OK_COMPLETIONPENDING
)
136 rv
= cb
.WaitForResult();
137 ASSERT_EQ(PP_OK
, rv
);
138 ASSERT_TRUE(socket
.GetRemoteAddress(&address
));
139 // Omit the |Disconnect()| here to make sure we don't crash if we just let
140 // the resource be destroyed.
143 // Connect to that address.
144 pp::TCPSocketPrivate
socket(instance_
);
145 TestCompletionCallback
cb(instance_
->pp_instance(), force_async_
);
146 int32_t rv
= socket
.ConnectWithNetAddress(&address
, cb
);
147 ASSERT_TRUE(!force_async_
|| rv
== PP_OK_COMPLETIONPENDING
);
148 if (rv
== PP_OK_COMPLETIONPENDING
)
149 rv
= cb
.WaitForResult();
150 ASSERT_EQ(PP_OK
, rv
);
152 // Make sure we can read/write to it properly (see |TestReadWrite()|).
153 ASSERT_EQ(PP_OK
, WriteStringToSocket(&socket
, "GET / HTTP/1.0\r\n\r\n"));
155 ASSERT_EQ(PP_OK
, ReadFirstLineFromSocket(&socket
, &s
));
156 ASSERT_TRUE(ValidateHttpResponse(s
));
163 int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket(
164 pp::TCPSocketPrivate
* socket
,
169 // Make sure we don't just hang if |Read()| spews.
170 while (s
->size() < 1000000) {
171 TestCompletionCallback
cb(instance_
->pp_instance(), force_async_
);
172 int32_t rv
= socket
->Read(buffer
, sizeof(buffer
), cb
);
173 if (force_async_
&& rv
!= PP_OK_COMPLETIONPENDING
)
174 return PP_ERROR_FAILED
;
175 if (rv
== PP_OK_COMPLETIONPENDING
)
176 rv
= cb
.WaitForResult();
180 return PP_ERROR_FAILED
; // Didn't get a \n-terminated line.
181 s
->reserve(s
->size() + rv
);
182 for (int32_t i
= 0; i
< rv
; i
++) {
183 s
->push_back(buffer
[i
]);
184 if (buffer
[i
] == '\n')
188 return PP_ERROR_FAILED
;
191 int32_t TestTCPSocketPrivate::WriteStringToSocket(pp::TCPSocketPrivate
* socket
,
192 const std::string
& s
) {
193 const char* buffer
= s
.data();
195 while (written
< s
.size()) {
196 TestCompletionCallback
cb(instance_
->pp_instance(), force_async_
);
197 int32_t rv
= socket
->Write(buffer
+ written
, s
.size() - written
, cb
);
198 if (force_async_
&& rv
!= PP_OK_COMPLETIONPENDING
)
199 return PP_ERROR_FAILED
;
200 if (rv
== PP_OK_COMPLETIONPENDING
)
201 rv
= cb
.WaitForResult();
205 return PP_ERROR_FAILED
;
208 if (written
!= s
.size())
209 return PP_ERROR_FAILED
;