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_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 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 unfitting internal buffer sizes. |
dmichael (off chromium)
2012/01/11 05:01:57
"unfitting" -> "that is too big for the"
Takashi Toyoshima
2012/01/11 05:50:29
Actually, the internal buffer size is 32K, so 8K f
| |
492 char message[8194]; | |
493 memset(message, 'x', 8193); | |
494 message[8193] = 0; | |
495 PP_Var message_var = CreateVarString(message); | |
484 | 496 |
485 // TODO(toyoshim): Add other function tests. | 497 uint64_t buffered_amount = 0; |
498 int32_t result; | |
499 for (int i = 0; i < 100; i++) { | |
500 result = websocket_interface_->SendMessage(ws, message_var); | |
501 ASSERT_EQ(PP_OK, result); | |
502 buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
503 if (buffered_amount > 262144) | |
dmichael (off chromium)
2012/01/11 05:01:57
Can you document this magic number?
Takashi Toyoshima
2012/01/11 05:50:29
Done.
| |
504 break; | |
505 } | |
506 | |
507 // Close connection. | |
508 PP_Var reason = CreateVarString("close in busy"); | |
dmichael (off chromium)
2012/01/11 05:01:57
Did you mean "close while busy"?
Takashi Toyoshima
2012/01/11 05:50:29
Yes.
I fixed it.
| |
509 TestCompletionCallback callback(instance_->pp_instance()); | |
510 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | |
511 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
512 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
513 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSING_DEV, | |
514 websocket_interface_->GetReadyState(ws)); | |
515 | |
516 result = callback.WaitForResult(); | |
517 ASSERT_EQ(PP_OK, result); | |
518 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, | |
519 websocket_interface_->GetReadyState(ws)); | |
520 | |
521 uint64_t base_buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
522 | |
523 // After connection closure, all sending requests fail and just increase | |
524 // the bufferedAmount property. | |
525 PP_Var empty_string = CreateVarString(""); | |
526 result = websocket_interface_->SendMessage(ws, empty_string); | |
527 ASSERT_EQ(PP_ERROR_FAILED, result); | |
528 buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
529 ASSERT_EQ(base_buffered_amount + kMessageFrameOverhead, buffered_amount); | |
530 base_buffered_amount = buffered_amount; | |
531 | |
532 result = websocket_interface_->SendMessage(ws, reason); | |
533 ASSERT_EQ(PP_ERROR_FAILED, result); | |
534 buffered_amount = websocket_interface_->GetBufferedAmount(ws); | |
535 uint64_t reason_frame_size = kMessageFrameOverhead + 13; | |
dmichael (off chromium)
2012/01/11 05:01:57
Please determine the size programmatically. E.g. a
Takashi Toyoshima
2012/01/11 05:50:29
That's nice.
I fixed.
| |
536 ASSERT_EQ(base_buffered_amount + reason_frame_size, buffered_amount); | |
537 | |
538 ReleaseVar(message_var); | |
539 ReleaseVar(reason); | |
540 ReleaseVar(empty_string); | |
541 core_interface_->ReleaseResource(ws); | |
542 | |
543 PASS(); | |
544 } | |
486 | 545 |
487 std::string TestWebSocket::TestCcInterfaces() { | 546 std::string TestWebSocket::TestCcInterfaces() { |
488 // C++ bindings is simple straightforward, then just verifies interfaces work | 547 // C++ bindings is simple straightforward, then just verifies interfaces work |
489 // as a interface bridge fine. | 548 // as a interface bridge fine. |
490 pp::WebSocket_Dev ws(instance_); | 549 pp::WebSocket_Dev ws(instance_); |
491 | 550 |
492 // Check uninitialized properties access. | 551 // Check uninitialized properties access. |
493 ASSERT_EQ(0, ws.GetBufferedAmount()); | 552 ASSERT_EQ(0, ws.GetBufferedAmount()); |
494 ASSERT_EQ(0, ws.GetCloseCode()); | 553 ASSERT_EQ(0, ws.GetCloseCode()); |
495 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); | 554 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()); | 608 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); |
550 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), reason.c_str())); | 609 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), reason.c_str())); |
551 ASSERT_EQ(true, ws.GetCloseWasClean()); | 610 ASSERT_EQ(true, ws.GetCloseWasClean()); |
552 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); | 611 ASSERT_TRUE(AreEqualWithString(ws.GetExtensions().pp_var(), "")); |
553 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); | 612 ASSERT_TRUE(AreEqualWithString(ws.GetProtocol().pp_var(), "")); |
554 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); | 613 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); |
555 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), kCloseServerURL)); | 614 ASSERT_TRUE(AreEqualWithString(ws.GetURL().pp_var(), kCloseServerURL)); |
556 | 615 |
557 PASS(); | 616 PASS(); |
558 } | 617 } |
OLD | NEW |