OLD | NEW |
---|---|
1 // Copyright (c) 2011 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" |
11 #include "ppapi/c/dev/ppb_var_array_buffer_dev.h" | 11 #include "ppapi/c/dev/ppb_var_array_buffer_dev.h" |
(...skipping 23 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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 if (result == PP_OK_COMPLETIONPENDING) | 474 if (result == PP_OK_COMPLETIONPENDING) |
470 result = callback.WaitForResult(); | 475 result = callback.WaitForResult(); |
471 ASSERT_EQ(PP_OK, result); | 476 ASSERT_EQ(PP_OK, result); |
472 ASSERT_TRUE(AreEqualWithBinary(received_message, data, len)); | 477 ASSERT_TRUE(AreEqualWithBinary(received_message, data, len)); |
473 ReleaseVar(received_message); | 478 ReleaseVar(received_message); |
474 core_interface_->ReleaseResource(ws); | 479 core_interface_->ReleaseResource(ws); |
475 | 480 |
476 PASS(); | 481 PASS(); |
477 } | 482 } |
478 | 483 |
479 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 484 std::string TestWebSocket::TestBufferedAmount() { |
480 // For now, the function doesn't work fine because update callback in WebKit is | 485 // Connect to test echo server. |
481 // not landed yet. | 486 int32_t connect_result; |
487 PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL); | |
488 ASSERT_TRUE(ws); | |
489 ASSERT_EQ(PP_OK, connect_result); | |
482 | 490 |
483 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 491 // Prepare a large message that is not aligned with the internal buffer |
492 // sizes. | |
493 char message[8194]; | |
494 memset(message, 'x', 8193); | |
495 message[8193] = 0; | |
496 PP_Var message_var = CreateVarString(message); | |
484 | 497 |
485 // TODO(toyoshim): Add other function tests. | 498 uint64_t buffered_amount = 0; |
499 int32_t result; | |
500 for (int i = 0; i < 100; i++) { | |
501 result = websocket_interface_->SendMessage(ws, message_var); | |
502 ASSERT_EQ(PP_OK, result); | |
503 buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
504 // Buffered amount size 262144 is enough big for the internal buffer size. | |
dmichael (off chromium)
2012/01/12 03:46:58
enough big -> too big ?
| |
505 if (buffered_amount > 262144) | |
506 break; | |
507 } | |
508 | |
509 // Close connection. | |
510 std::string reason_str = "close while busy"; | |
511 PP_Var reason = CreateVarString(reason_str.c_str()); | |
512 TestCompletionCallback callback(instance_->pp_instance()); | |
513 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | |
514 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
515 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
516 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSING_DEV, | |
517 websocket_interface_->GetReadyState(ws)); | |
518 | |
519 result = callback.WaitForResult(); | |
520 ASSERT_EQ(PP_OK, result); | |
521 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, | |
522 websocket_interface_->GetReadyState(ws)); | |
523 | |
524 uint64_t base_buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
525 | |
526 // After connection closure, all sending requests fail and just increase | |
527 // the bufferedAmount property. | |
528 PP_Var empty_string = CreateVarString(""); | |
529 result = websocket_interface_->SendMessage(ws, empty_string); | |
530 ASSERT_EQ(PP_ERROR_FAILED, result); | |
531 buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
532 ASSERT_EQ(base_buffered_amount + kMessageFrameOverhead, buffered_amount); | |
533 base_buffered_amount = buffered_amount; | |
534 | |
535 result = websocket_interface_->SendMessage(ws, reason); | |
536 ASSERT_EQ(PP_ERROR_FAILED, result); | |
537 buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
538 uint64_t reason_frame_size = kMessageFrameOverhead + reason_str.length(); | |
539 ASSERT_EQ(base_buffered_amount + reason_frame_size, buffered_amount); | |
540 | |
541 ReleaseVar(message_var); | |
542 ReleaseVar(reason); | |
543 ReleaseVar(empty_string); | |
544 core_interface_->ReleaseResource(ws); | |
545 | |
546 PASS(); | |
547 } | |
486 | 548 |
487 std::string TestWebSocket::TestCcInterfaces() { | 549 std::string TestWebSocket::TestCcInterfaces() { |
488 // C++ bindings is simple straightforward, then just verifies interfaces work | 550 // C++ bindings is simple straightforward, then just verifies interfaces work |
489 // as a interface bridge fine. | 551 // as a interface bridge fine. |
490 pp::WebSocket_Dev ws(instance_); | 552 pp::WebSocket_Dev ws(instance_); |
491 | 553 |
492 // Check uninitialized properties access. | 554 // Check uninitialized properties access. |
493 ASSERT_EQ(0, ws.GetBufferedAmount()); | 555 ASSERT_EQ(0, ws.GetBufferedAmount()); |
494 ASSERT_EQ(0, ws.GetCloseCode()); | 556 ASSERT_EQ(0, ws.GetCloseCode()); |
495 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); | 557 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
549 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); | 611 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); |
550 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), reason.c_str())); | 612 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), reason.c_str())); |
551 ASSERT_EQ(true, ws.GetCloseWasClean()); | 613 ASSERT_EQ(true, ws.GetCloseWasClean()); |
552 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); | 614 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); |
553 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); | 615 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); |
554 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); | 616 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); |
555 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), kCloseServerURL)); | 617 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), kCloseServerURL)); |
556 | 618 |
557 PASS(); | 619 PASS(); |
558 } | 620 } |
OLD | NEW |