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_websocket.h" | 5 #include "ppapi/tests/test_websocket.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ppapi/c/dev/ppb_testing_dev.h" | 10 #include "ppapi/c/dev/ppb_testing_dev.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 "ws://www.google.com/invalid#fragment", | 35 "ws://www.google.com/invalid#fragment", |
36 "ws://www.google.com:65535/invalid_port", | 36 "ws://www.google.com:65535/invalid_port", |
37 NULL | 37 NULL |
38 }; | 38 }; |
39 | 39 |
40 // Connection close code is defined in WebSocket protocol specification. | 40 // Connection close code is defined in WebSocket protocol specification. |
41 // The magic number 1000 means gracefull closure without any error. | 41 // The magic number 1000 means gracefull closure without any error. |
42 // See section 7.4.1. of RFC 6455. | 42 // See section 7.4.1. of RFC 6455. |
43 const uint16_t kCloseCodeNormalClosure = 1000U; | 43 const uint16_t kCloseCodeNormalClosure = 1000U; |
44 | 44 |
| 45 // Internal packet sizes. |
| 46 const uint64_t kCloseFrameSize = 6; |
| 47 const uint64_t kMessageFrameOverhead = 6; |
| 48 |
45 REGISTER_TEST_CASE(WebSocket); | 49 REGISTER_TEST_CASE(WebSocket); |
46 | 50 |
47 bool TestWebSocket::Init() { | 51 bool TestWebSocket::Init() { |
48 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 52 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
49 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 53 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
50 var_interface_ = static_cast<const PPB_Var*>( | 54 var_interface_ = static_cast<const PPB_Var*>( |
51 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 55 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
52 arraybuffer_interface_ = static_cast<const PPB_VarArrayBuffer_Dev*>( | 56 arraybuffer_interface_ = static_cast<const PPB_VarArrayBuffer_Dev*>( |
53 pp::Module::Get()->GetBrowserInterface( | 57 pp::Module::Get()->GetBrowserInterface( |
54 PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE)); | 58 PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE)); |
(...skipping 11 matching lines...) Expand all Loading... |
66 RUN_TEST_WITH_REFERENCE_CHECK(UninitializedPropertiesAccess, filter); | 70 RUN_TEST_WITH_REFERENCE_CHECK(UninitializedPropertiesAccess, filter); |
67 RUN_TEST_WITH_REFERENCE_CHECK(InvalidConnect, filter); | 71 RUN_TEST_WITH_REFERENCE_CHECK(InvalidConnect, filter); |
68 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); | 72 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); |
69 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); | 73 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); |
70 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); | 74 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); |
71 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); | 75 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); |
72 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); | 76 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); |
73 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); | 77 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); |
74 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); | 78 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); |
75 RUN_TEST_WITH_REFERENCE_CHECK(BinarySendReceive, filter); | 79 RUN_TEST_WITH_REFERENCE_CHECK(BinarySendReceive, filter); |
| 80 RUN_TEST_WITH_REFERENCE_CHECK(BufferedAmount, filter); |
76 | 81 |
77 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); | 82 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); |
78 } | 83 } |
79 | 84 |
80 PP_Var TestWebSocket::CreateVarString(const char* string) { | 85 PP_Var TestWebSocket::CreateVarString(const char* string) { |
81 return var_interface_->VarFromUtf8(string, strlen(string)); | 86 return var_interface_->VarFromUtf8(string, strlen(string)); |
82 } | 87 } |
83 | 88 |
84 PP_Var TestWebSocket::CreateVarBinary(const uint8_t* data, uint32_t size) { | 89 PP_Var TestWebSocket::CreateVarBinary(const uint8_t* data, uint32_t size) { |
85 PP_Var var = arraybuffer_interface_->Create(size); | 90 PP_Var var = arraybuffer_interface_->Create(size); |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 if (result == PP_OK_COMPLETIONPENDING) | 476 if (result == PP_OK_COMPLETIONPENDING) |
472 result = callback.WaitForResult(); | 477 result = callback.WaitForResult(); |
473 ASSERT_EQ(PP_OK, result); | 478 ASSERT_EQ(PP_OK, result); |
474 ASSERT_TRUE(AreEqualWithBinary(received_message, data, len)); | 479 ASSERT_TRUE(AreEqualWithBinary(received_message, data, len)); |
475 ReleaseVar(received_message); | 480 ReleaseVar(received_message); |
476 core_interface_->ReleaseResource(ws); | 481 core_interface_->ReleaseResource(ws); |
477 | 482 |
478 PASS(); | 483 PASS(); |
479 } | 484 } |
480 | 485 |
481 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 486 std::string TestWebSocket::TestBufferedAmount() { |
482 // For now, the function doesn't work fine because update callback in WebKit is | 487 // Connect to test echo server. |
483 // not landed yet. | 488 int32_t connect_result; |
| 489 PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL); |
| 490 ASSERT_TRUE(ws); |
| 491 ASSERT_EQ(PP_OK, connect_result); |
484 | 492 |
485 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 493 // Prepare a large message that is not aligned with the internal buffer |
| 494 // sizes. |
| 495 char message[8194]; |
| 496 memset(message, 'x', 8193); |
| 497 message[8193] = 0; |
| 498 PP_Var message_var = CreateVarString(message); |
486 | 499 |
487 // TODO(toyoshim): Add other function tests. | 500 uint64_t buffered_amount = 0; |
| 501 int32_t result; |
| 502 for (int i = 0; i < 100; i++) { |
| 503 result = websocket_interface_->SendMessage(ws, message_var); |
| 504 ASSERT_EQ(PP_OK, result); |
| 505 buffered_amount = websocket_interface_->GetBufferedAmount(ws); |
| 506 // Buffered amount size 262144 is too big for the internal buffer size. |
| 507 if (buffered_amount > 262144) |
| 508 break; |
| 509 } |
| 510 |
| 511 // Close connection. |
| 512 std::string reason_str = "close while busy"; |
| 513 PP_Var reason = CreateVarString(reason_str.c_str()); |
| 514 TestCompletionCallback callback(instance_->pp_instance()); |
| 515 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
| 516 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 517 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 518 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSING_DEV, |
| 519 websocket_interface_->GetReadyState(ws)); |
| 520 |
| 521 result = callback.WaitForResult(); |
| 522 ASSERT_EQ(PP_OK, result); |
| 523 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, |
| 524 websocket_interface_->GetReadyState(ws)); |
| 525 |
| 526 uint64_t base_buffered_amount = websocket_interface_->GetBufferedAmount(ws); |
| 527 |
| 528 // After connection closure, all sending requests fail and just increase |
| 529 // the bufferedAmount property. |
| 530 PP_Var empty_string = CreateVarString(""); |
| 531 result = websocket_interface_->SendMessage(ws, empty_string); |
| 532 ASSERT_EQ(PP_ERROR_FAILED, result); |
| 533 buffered_amount = websocket_interface_->GetBufferedAmount(ws); |
| 534 ASSERT_EQ(base_buffered_amount + kMessageFrameOverhead, buffered_amount); |
| 535 base_buffered_amount = buffered_amount; |
| 536 |
| 537 result = websocket_interface_->SendMessage(ws, reason); |
| 538 ASSERT_EQ(PP_ERROR_FAILED, result); |
| 539 buffered_amount = websocket_interface_->GetBufferedAmount(ws); |
| 540 uint64_t reason_frame_size = kMessageFrameOverhead + reason_str.length(); |
| 541 ASSERT_EQ(base_buffered_amount + reason_frame_size, buffered_amount); |
| 542 |
| 543 ReleaseVar(message_var); |
| 544 ReleaseVar(reason); |
| 545 ReleaseVar(empty_string); |
| 546 core_interface_->ReleaseResource(ws); |
| 547 |
| 548 PASS(); |
| 549 } |
488 | 550 |
489 std::string TestWebSocket::TestCcInterfaces() { | 551 std::string TestWebSocket::TestCcInterfaces() { |
490 // C++ bindings is simple straightforward, then just verifies interfaces work | 552 // C++ bindings is simple straightforward, then just verifies interfaces work |
491 // as a interface bridge fine. | 553 // as a interface bridge fine. |
492 pp::WebSocket_Dev ws(instance_); | 554 pp::WebSocket_Dev ws(instance_); |
493 | 555 |
494 // Check uninitialized properties access. | 556 // Check uninitialized properties access. |
495 ASSERT_EQ(0, ws.GetBufferedAmount()); | 557 ASSERT_EQ(0, ws.GetBufferedAmount()); |
496 ASSERT_EQ(0, ws.GetCloseCode()); | 558 ASSERT_EQ(0, ws.GetCloseCode()); |
497 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); | 559 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); | 613 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); |
552 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), reason.c_str())); | 614 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), reason.c_str())); |
553 ASSERT_EQ(true, ws.GetCloseWasClean()); | 615 ASSERT_EQ(true, ws.GetCloseWasClean()); |
554 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); | 616 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); |
555 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); | 617 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); |
556 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); | 618 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); |
557 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), kCloseServerURL)); | 619 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), kCloseServerURL)); |
558 | 620 |
559 PASS(); | 621 PASS(); |
560 } | 622 } |
OLD | NEW |