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_transport.h" | 5 #include "ppapi/tests/test_transport.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <list> | 10 #include <list> |
11 #include <map> | 11 #include <map> |
12 | 12 |
13 #include "ppapi/c/dev/ppb_testing_dev.h" | 13 #include "ppapi/c/dev/ppb_testing_dev.h" |
14 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/cpp/completion_callback.h" | 15 #include "ppapi/cpp/completion_callback.h" |
| 16 #include "ppapi/cpp/dev/transport_dev.h" |
16 #include "ppapi/cpp/instance.h" | 17 #include "ppapi/cpp/instance.h" |
17 #include "ppapi/cpp/module.h" | 18 #include "ppapi/cpp/module.h" |
18 #include "ppapi/cpp/dev/transport_dev.h" | 19 #include "ppapi/cpp/var.h" |
19 #include "ppapi/tests/test_utils.h" | 20 #include "ppapi/tests/test_utils.h" |
20 #include "ppapi/tests/testing_instance.h" | 21 #include "ppapi/tests/testing_instance.h" |
21 | 22 |
22 REGISTER_TEST_CASE(Transport); | 23 REGISTER_TEST_CASE(Transport); |
23 | 24 |
24 #define RUN_SUBTEST(function) { \ | 25 #define RUN_SUBTEST(function) { \ |
25 std::string result = function; \ | 26 std::string result = function; \ |
26 if (!result.empty()) \ | 27 if (!result.empty()) \ |
27 return result; \ | 28 return result; \ |
28 } | 29 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 } // namespace | 97 } // namespace |
97 | 98 |
98 bool TestTransport::Init() { | 99 bool TestTransport::Init() { |
99 transport_interface_ = reinterpret_cast<PPB_Transport_Dev const*>( | 100 transport_interface_ = reinterpret_cast<PPB_Transport_Dev const*>( |
100 pp::Module::Get()->GetBrowserInterface(PPB_TRANSPORT_DEV_INTERFACE)); | 101 pp::Module::Get()->GetBrowserInterface(PPB_TRANSPORT_DEV_INTERFACE)); |
101 return transport_interface_ && InitTestingInterface(); | 102 return transport_interface_ && InitTestingInterface(); |
102 } | 103 } |
103 | 104 |
104 void TestTransport::RunTest() { | 105 void TestTransport::RunTest() { |
105 RUN_TEST(Create); | 106 RUN_TEST(Create); |
106 RUN_TEST(Connect); | 107 RUN_TEST_FORCEASYNC_AND_NOT(Connect); |
107 RUN_TEST_FORCEASYNC(SendDataUdp); | 108 RUN_TEST(SetProperty); |
108 RUN_TEST(SendDataTcp); | 109 RUN_TEST_FORCEASYNC_AND_NOT(SendDataUdp); |
109 RUN_TEST(ConnectAndCloseUdp); | 110 RUN_TEST_FORCEASYNC_AND_NOT(SendDataTcp); |
110 RUN_TEST(ConnectAndCloseTcp); | 111 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAndCloseUdp); |
| 112 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAndCloseTcp); |
111 } | 113 } |
112 | 114 |
113 std::string TestTransport::InitTargets(const char* proto) { | 115 std::string TestTransport::InitTargets(const char* proto) { |
114 transport1_.reset(new pp::Transport_Dev(instance_, kTestChannelName, proto)); | 116 transport1_.reset(new pp::Transport_Dev(instance_, kTestChannelName, proto)); |
115 transport2_.reset(new pp::Transport_Dev(instance_, kTestChannelName, proto)); | 117 transport2_.reset(new pp::Transport_Dev(instance_, kTestChannelName, proto)); |
116 | 118 |
117 ASSERT_TRUE(transport1_.get() != NULL); | 119 ASSERT_TRUE(transport1_.get() != NULL); |
118 ASSERT_TRUE(transport2_.get() != NULL); | 120 ASSERT_TRUE(transport2_.get() != NULL); |
119 | 121 |
120 PASS(); | 122 PASS(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 161 } |
160 | 162 |
161 std::string TestTransport::TestCreate() { | 163 std::string TestTransport::TestCreate() { |
162 RUN_SUBTEST(InitTargets("udp")); | 164 RUN_SUBTEST(InitTargets("udp")); |
163 | 165 |
164 Clean(); | 166 Clean(); |
165 | 167 |
166 PASS(); | 168 PASS(); |
167 } | 169 } |
168 | 170 |
| 171 std::string TestTransport::TestSetProperty() { |
| 172 RUN_SUBTEST(InitTargets("tcp")); |
| 173 |
| 174 // Try settings STUN and Relay properties. |
| 175 ASSERT_EQ(transport1_->SetProperty( |
| 176 PP_TRANSPORTPROPERTY_STUN_SERVER, |
| 177 pp::Var("stun.example.com:19302")), PP_OK); |
| 178 |
| 179 ASSERT_EQ(transport1_->SetProperty( |
| 180 PP_TRANSPORTPROPERTY_RELAY_SERVER, |
| 181 pp::Var("ralay.example.com:80")), PP_OK); |
| 182 |
| 183 ASSERT_EQ(transport1_->SetProperty( |
| 184 PP_TRANSPORTPROPERTY_RELAY_TOKEN, |
| 185 pp::Var("INVALID_TOKEN")), PP_OK); |
| 186 |
| 187 // Try changing TCP window size. |
| 188 ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW, |
| 189 pp::Var(65536)), PP_OK); |
| 190 ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_RECEIVE_WINDOW, |
| 191 pp::Var(10000000)), PP_ERROR_BADARGUMENT); |
| 192 |
| 193 ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW, |
| 194 pp::Var(65536)), PP_OK); |
| 195 ASSERT_EQ(transport1_->SetProperty(PP_TRANSPORTPROPERTY_TCP_SEND_WINDOW, |
| 196 pp::Var(10000000)), PP_ERROR_BADARGUMENT); |
| 197 |
| 198 TestCompletionCallback connect_cb(instance_->pp_instance()); |
| 199 ASSERT_EQ(transport1_->Connect(connect_cb), PP_OK_COMPLETIONPENDING); |
| 200 |
| 201 // SetProperty() should fail after we've connected. |
| 202 ASSERT_EQ(transport1_->SetProperty( |
| 203 PP_TRANSPORTPROPERTY_STUN_SERVER, |
| 204 pp::Var("stun.example.com:31323")), PP_ERROR_FAILED); |
| 205 |
| 206 Clean(); |
| 207 ASSERT_EQ(connect_cb.WaitForResult(), PP_ERROR_ABORTED); |
| 208 |
| 209 PASS(); |
| 210 } |
| 211 |
169 std::string TestTransport::TestConnect() { | 212 std::string TestTransport::TestConnect() { |
170 RUN_SUBTEST(InitTargets("udp")); | 213 RUN_SUBTEST(InitTargets("udp")); |
171 RUN_SUBTEST(Connect()); | 214 RUN_SUBTEST(Connect()); |
172 | 215 |
173 Clean(); | 216 Clean(); |
174 | 217 |
175 PASS(); | 218 PASS(); |
176 } | 219 } |
177 | 220 |
178 // Creating datagram connection and try sending data over it. Verify | 221 // Creating datagram connection and try sending data over it. Verify |
(...skipping 25 matching lines...) Expand all Loading... |
204 if (force_async_) { | 247 if (force_async_) { |
205 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); | 248 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); |
206 ASSERT_EQ(send_cb.WaitForResult(), static_cast<int>(send_buffer.size())); | 249 ASSERT_EQ(send_cb.WaitForResult(), static_cast<int>(send_buffer.size())); |
207 } else { | 250 } else { |
208 ASSERT_EQ(result, static_cast<int>(send_buffer.size())); | 251 ASSERT_EQ(result, static_cast<int>(send_buffer.size())); |
209 } | 252 } |
210 sent_packets[i] = send_buffer; | 253 sent_packets[i] = send_buffer; |
211 } | 254 } |
212 | 255 |
213 // Limit waiting time. | 256 // Limit waiting time. |
214 pp::Module::Get()->core()->CallOnMainThread(kUdpWaitTimeMs, done_cb); | 257 TestCompletionCallback timeout_cb(instance_->pp_instance()); |
215 ASSERT_EQ(done_cb.WaitForResult(), PP_OK); | 258 pp::Module::Get()->core()->CallOnMainThread(kUdpWaitTimeMs, timeout_cb); |
| 259 ASSERT_EQ(timeout_cb.WaitForResult(), PP_OK); |
216 | 260 |
217 ASSERT_TRUE(reader.errors().size() == 0); | 261 ASSERT_TRUE(reader.errors().size() == 0); |
218 ASSERT_TRUE(reader.received().size() > 0); | 262 ASSERT_TRUE(reader.received().size() > 0); |
219 for (std::list<std::vector<char> >::const_iterator it = | 263 for (std::list<std::vector<char> >::const_iterator it = |
220 reader.received().begin(); it != reader.received().end(); ++it) { | 264 reader.received().begin(); it != reader.received().end(); ++it) { |
221 int index; | 265 int index; |
222 memcpy(&index, &(*it)[0], sizeof(index)); | 266 memcpy(&index, &(*it)[0], sizeof(index)); |
223 ASSERT_TRUE(sent_packets[index] == *it); | 267 ASSERT_TRUE(sent_packets[index] == *it); |
224 } | 268 } |
225 | 269 |
226 Clean(); | 270 Clean(); |
227 | 271 |
228 PASS(); | 272 PASS(); |
229 } | 273 } |
230 | 274 |
231 // Creating reliable (TCP-like) connection and try sending data over | 275 // Creating reliable (TCP-like) connection and try sending data over |
232 // it. Verify that all data is received correctly. | 276 // it. Verify that all data is received correctly. |
233 std::string TestTransport::TestSendDataTcp() { | 277 std::string TestTransport::TestSendDataTcp() { |
234 RUN_SUBTEST(InitTargets("tcp")); | 278 RUN_SUBTEST(InitTargets("tcp")); |
235 RUN_SUBTEST(Connect()); | 279 RUN_SUBTEST(Connect()); |
236 | 280 |
237 const int kTcpSendSize = 100000; | 281 const int kTcpSendSize = 100000; |
238 const int kTimeoutMs = 20000; // 20 seconds. | |
239 | 282 |
240 TestCompletionCallback done_cb(instance_->pp_instance()); | 283 TestCompletionCallback done_cb(instance_->pp_instance()); |
241 StreamReader reader(transport1_.get(), kTcpSendSize, | 284 StreamReader reader(transport1_.get(), kTcpSendSize, |
242 done_cb); | 285 done_cb); |
243 | 286 |
244 std::vector<char> send_buffer(kTcpSendSize); | 287 std::vector<char> send_buffer(kTcpSendSize); |
245 for (size_t j = 0; j < send_buffer.size(); ++j) { | 288 for (size_t j = 0; j < send_buffer.size(); ++j) { |
246 send_buffer[j] = rand() % 256; | 289 send_buffer[j] = rand() % 256; |
247 } | 290 } |
248 | 291 |
249 int pos = 0; | 292 int pos = 0; |
250 while (pos < static_cast<int>(send_buffer.size())) { | 293 while (pos < static_cast<int>(send_buffer.size())) { |
251 TestCompletionCallback send_cb(instance_->pp_instance(), force_async_); | 294 TestCompletionCallback send_cb(instance_->pp_instance(), force_async_); |
252 int result = transport2_->Send( | 295 int result = transport2_->Send( |
253 &send_buffer[0] + pos, send_buffer.size() - pos, send_cb); | 296 &send_buffer[0] + pos, send_buffer.size() - pos, send_cb); |
254 if (force_async_) | 297 if (force_async_) |
255 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); | 298 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); |
256 if (result == PP_OK_COMPLETIONPENDING) | 299 if (result == PP_OK_COMPLETIONPENDING) |
257 result = send_cb.WaitForResult(); | 300 result = send_cb.WaitForResult(); |
258 ASSERT_TRUE(result > 0); | 301 ASSERT_TRUE(result > 0); |
259 pos += result; | 302 pos += result; |
260 } | 303 } |
261 | 304 |
262 // Limit waiting time. | |
263 pp::Module::Get()->core()->CallOnMainThread(kTimeoutMs, done_cb); | |
264 ASSERT_EQ(done_cb.WaitForResult(), PP_OK); | 305 ASSERT_EQ(done_cb.WaitForResult(), PP_OK); |
265 | 306 |
266 ASSERT_TRUE(reader.errors().size() == 0); | 307 ASSERT_TRUE(reader.errors().size() == 0); |
267 | 308 |
268 std::vector<char> received_data; | 309 std::vector<char> received_data; |
269 for (std::list<std::vector<char> >::const_iterator it = | 310 for (std::list<std::vector<char> >::const_iterator it = |
270 reader.received().begin(); it != reader.received().end(); ++it) { | 311 reader.received().begin(); it != reader.received().end(); ++it) { |
271 received_data.insert(received_data.end(), it->begin(), it->end()); | 312 received_data.insert(received_data.end(), it->begin(), it->end()); |
272 } | 313 } |
273 ASSERT_EQ(send_buffer, received_data); | 314 ASSERT_EQ(send_buffer, received_data); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 // Close the transport and verify that callback is aborted. | 352 // Close the transport and verify that callback is aborted. |
312 ASSERT_EQ(transport1_->Close(), PP_OK); | 353 ASSERT_EQ(transport1_->Close(), PP_OK); |
313 | 354 |
314 ASSERT_EQ(recv_cb.run_count(), 1); | 355 ASSERT_EQ(recv_cb.run_count(), 1); |
315 ASSERT_EQ(recv_cb.result(), PP_ERROR_ABORTED); | 356 ASSERT_EQ(recv_cb.result(), PP_ERROR_ABORTED); |
316 | 357 |
317 Clean(); | 358 Clean(); |
318 | 359 |
319 PASS(); | 360 PASS(); |
320 } | 361 } |
OLD | NEW |