OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/cpp/private/tcp_socket_private.h" | 9 #include "ppapi/cpp/private/tcp_socket_private.h" |
10 #include "ppapi/tests/testing_instance.h" | 10 #include "ppapi/tests/testing_instance.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 return false; | 33 return false; |
34 | 34 |
35 // 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 |
36 // came. Grab the host and port. | 36 // came. Grab the host and port. |
37 if (!EnsureRunningOverHTTP()) | 37 if (!EnsureRunningOverHTTP()) |
38 return false; | 38 return false; |
39 | 39 |
40 if (!GetLocalHostPort(instance_->pp_instance(), &host_, &port_)) | 40 if (!GetLocalHostPort(instance_->pp_instance(), &host_, &port_)) |
41 return false; | 41 return false; |
42 | 42 |
| 43 // Get the port for the SSL server. |
| 44 ssl_port_ = instance_->ssl_server_port(); |
| 45 |
43 return true; | 46 return true; |
44 } | 47 } |
45 | 48 |
46 void TestTCPSocketPrivate::RunTests(const std::string& filter) { | 49 void TestTCPSocketPrivate::RunTests(const std::string& filter) { |
47 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); | 50 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); |
48 RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite, filter); | 51 RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite, filter); |
| 52 RUN_TEST_FORCEASYNC_AND_NOT(ReadWriteSSL, filter); |
49 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress, filter); | 53 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress, filter); |
50 } | 54 } |
51 | 55 |
52 std::string TestTCPSocketPrivate::TestBasic() { | 56 std::string TestTCPSocketPrivate::TestBasic() { |
53 pp::TCPSocketPrivate socket(instance_); | 57 pp::TCPSocketPrivate socket(instance_); |
54 TestCompletionCallback cb(instance_->pp_instance(), force_async_); | 58 TestCompletionCallback cb(instance_->pp_instance(), force_async_); |
55 | 59 |
56 int32_t rv = socket.Connect(host_.c_str(), port_, cb); | 60 int32_t rv = socket.Connect(host_.c_str(), port_, cb); |
57 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); | 61 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); |
58 if (rv == PP_OK_COMPLETIONPENDING) | 62 if (rv == PP_OK_COMPLETIONPENDING) |
(...skipping 25 matching lines...) Expand all Loading... |
84 // 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. |
85 std::string s; | 89 std::string s; |
86 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); | 90 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
87 ASSERT_TRUE(ValidateHttpResponse(s)); | 91 ASSERT_TRUE(ValidateHttpResponse(s)); |
88 | 92 |
89 socket.Disconnect(); | 93 socket.Disconnect(); |
90 | 94 |
91 PASS(); | 95 PASS(); |
92 } | 96 } |
93 | 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 |
94 std::string TestTCPSocketPrivate::TestConnectAddress() { | 126 std::string TestTCPSocketPrivate::TestConnectAddress() { |
95 PP_NetAddress_Private address; | 127 PP_NetAddress_Private address; |
96 | 128 |
97 // First, bring up a connection and grab the address. | 129 // First, bring up a connection and grab the address. |
98 { | 130 { |
99 pp::TCPSocketPrivate socket(instance_); | 131 pp::TCPSocketPrivate socket(instance_); |
100 TestCompletionCallback cb(instance_->pp_instance(), force_async_); | 132 TestCompletionCallback cb(instance_->pp_instance(), force_async_); |
101 int32_t rv = socket.Connect(host_.c_str(), port_, cb); | 133 int32_t rv = socket.Connect(host_.c_str(), port_, cb); |
102 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); | 134 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); |
103 if (rv == PP_OK_COMPLETIONPENDING) | 135 if (rv == PP_OK_COMPLETIONPENDING) |
(...skipping 17 matching lines...) Expand all Loading... |
121 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")); |
122 std::string s; | 154 std::string s; |
123 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); | 155 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); |
124 ASSERT_TRUE(ValidateHttpResponse(s)); | 156 ASSERT_TRUE(ValidateHttpResponse(s)); |
125 | 157 |
126 socket.Disconnect(); | 158 socket.Disconnect(); |
127 | 159 |
128 PASS(); | 160 PASS(); |
129 } | 161 } |
130 | 162 |
131 // TODO(viettrungluu): Try testing SSL somehow. | |
132 | |
133 int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket( | 163 int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket( |
134 pp::TCPSocketPrivate* socket, | 164 pp::TCPSocketPrivate* socket, |
135 std::string* s) { | 165 std::string* s) { |
136 char buffer[10000]; | 166 char buffer[10000]; |
137 | 167 |
138 s->clear(); | 168 s->clear(); |
139 // Make sure we don't just hang if |Read()| spews. | 169 // Make sure we don't just hang if |Read()| spews. |
140 while (s->size() < 1000000) { | 170 while (s->size() < 1000000) { |
141 TestCompletionCallback cb(instance_->pp_instance(), force_async_); | 171 TestCompletionCallback cb(instance_->pp_instance(), force_async_); |
142 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... |
172 if (rv < 0) | 202 if (rv < 0) |
173 return rv; | 203 return rv; |
174 if (rv == 0) | 204 if (rv == 0) |
175 return PP_ERROR_FAILED; | 205 return PP_ERROR_FAILED; |
176 written += rv; | 206 written += rv; |
177 } | 207 } |
178 if (written != s.size()) | 208 if (written != s.size()) |
179 return PP_ERROR_FAILED; | 209 return PP_ERROR_FAILED; |
180 return PP_OK; | 210 return PP_OK; |
181 } | 211 } |
OLD | NEW |