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("stun1.a.google.com:31323,stun2.a.google.com:54324")), PP_OK); | |
brettw
2011/08/25 16:48:43
Does example.com also make sense for these tests?
Sergey Ulanov
2011/08/25 17:36:53
Done.
| |
178 | |
179 ASSERT_EQ(transport1_->SetProperty( | |
180 PP_TRANSPORTPROPERTY_RELAY_SERVER, | |
181 pp::Var("ralay1.a.google.com:31323,relay2.a.google.com:54324")), 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 RUN_SUBTEST(Connect()); | |
199 | |
200 // SetProperty() should fail after we've connected. | |
201 ASSERT_EQ(transport1_->SetProperty( | |
202 PP_TRANSPORTPROPERTY_STUN_SERVER, | |
203 pp::Var("stun1.a.google.com:31323")), PP_ERROR_FAILED); | |
204 | |
205 Clean(); | |
206 | |
207 PASS(); | |
208 } | |
209 | |
169 std::string TestTransport::TestConnect() { | 210 std::string TestTransport::TestConnect() { |
170 RUN_SUBTEST(InitTargets("udp")); | 211 RUN_SUBTEST(InitTargets("udp")); |
171 RUN_SUBTEST(Connect()); | 212 RUN_SUBTEST(Connect()); |
172 | 213 |
173 Clean(); | 214 Clean(); |
174 | 215 |
175 PASS(); | 216 PASS(); |
176 } | 217 } |
177 | 218 |
178 // Creating datagram connection and try sending data over it. Verify | 219 // Creating datagram connection and try sending data over it. Verify |
(...skipping 25 matching lines...) Expand all Loading... | |
204 if (force_async_) { | 245 if (force_async_) { |
205 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); | 246 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); |
206 ASSERT_EQ(send_cb.WaitForResult(), static_cast<int>(send_buffer.size())); | 247 ASSERT_EQ(send_cb.WaitForResult(), static_cast<int>(send_buffer.size())); |
207 } else { | 248 } else { |
208 ASSERT_EQ(result, static_cast<int>(send_buffer.size())); | 249 ASSERT_EQ(result, static_cast<int>(send_buffer.size())); |
209 } | 250 } |
210 sent_packets[i] = send_buffer; | 251 sent_packets[i] = send_buffer; |
211 } | 252 } |
212 | 253 |
213 // Limit waiting time. | 254 // Limit waiting time. |
214 pp::Module::Get()->core()->CallOnMainThread(kUdpWaitTimeMs, done_cb); | 255 TestCompletionCallback timeout_cb(instance_->pp_instance()); |
215 ASSERT_EQ(done_cb.WaitForResult(), PP_OK); | 256 pp::Module::Get()->core()->CallOnMainThread(kUdpWaitTimeMs, timeout_cb); |
257 ASSERT_EQ(timeout_cb.WaitForResult(), PP_OK); | |
216 | 258 |
217 ASSERT_TRUE(reader.errors().size() == 0); | 259 ASSERT_TRUE(reader.errors().size() == 0); |
218 ASSERT_TRUE(reader.received().size() > 0); | 260 ASSERT_TRUE(reader.received().size() > 0); |
219 for (std::list<std::vector<char> >::const_iterator it = | 261 for (std::list<std::vector<char> >::const_iterator it = |
220 reader.received().begin(); it != reader.received().end(); ++it) { | 262 reader.received().begin(); it != reader.received().end(); ++it) { |
221 int index; | 263 int index; |
222 memcpy(&index, &(*it)[0], sizeof(index)); | 264 memcpy(&index, &(*it)[0], sizeof(index)); |
223 ASSERT_TRUE(sent_packets[index] == *it); | 265 ASSERT_TRUE(sent_packets[index] == *it); |
224 } | 266 } |
225 | 267 |
226 Clean(); | 268 Clean(); |
227 | 269 |
228 PASS(); | 270 PASS(); |
229 } | 271 } |
230 | 272 |
231 // Creating reliable (TCP-like) connection and try sending data over | 273 // Creating reliable (TCP-like) connection and try sending data over |
232 // it. Verify that all data is received correctly. | 274 // it. Verify that all data is received correctly. |
233 std::string TestTransport::TestSendDataTcp() { | 275 std::string TestTransport::TestSendDataTcp() { |
234 RUN_SUBTEST(InitTargets("tcp")); | 276 RUN_SUBTEST(InitTargets("tcp")); |
235 RUN_SUBTEST(Connect()); | 277 RUN_SUBTEST(Connect()); |
236 | 278 |
237 const int kTcpSendSize = 100000; | 279 const int kTcpSendSize = 100000; |
238 const int kTimeoutMs = 20000; // 20 seconds. | |
239 | 280 |
240 TestCompletionCallback done_cb(instance_->pp_instance()); | 281 TestCompletionCallback done_cb(instance_->pp_instance()); |
241 StreamReader reader(transport1_.get(), kTcpSendSize, | 282 StreamReader reader(transport1_.get(), kTcpSendSize, |
242 done_cb); | 283 done_cb); |
243 | 284 |
244 std::vector<char> send_buffer(kTcpSendSize); | 285 std::vector<char> send_buffer(kTcpSendSize); |
245 for (size_t j = 0; j < send_buffer.size(); ++j) { | 286 for (size_t j = 0; j < send_buffer.size(); ++j) { |
246 send_buffer[j] = rand() % 256; | 287 send_buffer[j] = rand() % 256; |
247 } | 288 } |
248 | 289 |
249 int pos = 0; | 290 int pos = 0; |
250 while (pos < static_cast<int>(send_buffer.size())) { | 291 while (pos < static_cast<int>(send_buffer.size())) { |
251 TestCompletionCallback send_cb(instance_->pp_instance(), force_async_); | 292 TestCompletionCallback send_cb(instance_->pp_instance(), force_async_); |
252 int result = transport2_->Send( | 293 int result = transport2_->Send( |
253 &send_buffer[0] + pos, send_buffer.size() - pos, send_cb); | 294 &send_buffer[0] + pos, send_buffer.size() - pos, send_cb); |
254 if (force_async_) | 295 if (force_async_) |
255 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); | 296 ASSERT_EQ(result, PP_OK_COMPLETIONPENDING); |
256 if (result == PP_OK_COMPLETIONPENDING) | 297 if (result == PP_OK_COMPLETIONPENDING) |
257 result = send_cb.WaitForResult(); | 298 result = send_cb.WaitForResult(); |
258 ASSERT_TRUE(result > 0); | 299 ASSERT_TRUE(result > 0); |
259 pos += result; | 300 pos += result; |
260 } | 301 } |
261 | 302 |
262 // Limit waiting time. | |
263 pp::Module::Get()->core()->CallOnMainThread(kTimeoutMs, done_cb); | |
264 ASSERT_EQ(done_cb.WaitForResult(), PP_OK); | 303 ASSERT_EQ(done_cb.WaitForResult(), PP_OK); |
265 | 304 |
266 ASSERT_TRUE(reader.errors().size() == 0); | 305 ASSERT_TRUE(reader.errors().size() == 0); |
267 | 306 |
268 std::vector<char> received_data; | 307 std::vector<char> received_data; |
269 for (std::list<std::vector<char> >::const_iterator it = | 308 for (std::list<std::vector<char> >::const_iterator it = |
270 reader.received().begin(); it != reader.received().end(); ++it) { | 309 reader.received().begin(); it != reader.received().end(); ++it) { |
271 received_data.insert(received_data.end(), it->begin(), it->end()); | 310 received_data.insert(received_data.end(), it->begin(), it->end()); |
272 } | 311 } |
273 ASSERT_EQ(send_buffer, received_data); | 312 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. | 350 // Close the transport and verify that callback is aborted. |
312 ASSERT_EQ(transport1_->Close(), PP_OK); | 351 ASSERT_EQ(transport1_->Close(), PP_OK); |
313 | 352 |
314 ASSERT_EQ(recv_cb.run_count(), 1); | 353 ASSERT_EQ(recv_cb.run_count(), 1); |
315 ASSERT_EQ(recv_cb.result(), PP_ERROR_ABORTED); | 354 ASSERT_EQ(recv_cb.result(), PP_ERROR_ABORTED); |
316 | 355 |
317 Clean(); | 356 Clean(); |
318 | 357 |
319 PASS(); | 358 PASS(); |
320 } | 359 } |
OLD | NEW |