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 <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
| 9 |
9 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <memory> |
10 #include <string> | 12 #include <string> |
11 #include <vector> | 13 #include <vector> |
12 | 14 |
13 #include "ppapi/c/dev/ppb_testing_dev.h" | 15 #include "ppapi/c/dev/ppb_testing_dev.h" |
14 #include "ppapi/c/pp_bool.h" | 16 #include "ppapi/c/pp_bool.h" |
15 #include "ppapi/c/pp_completion_callback.h" | 17 #include "ppapi/c/pp_completion_callback.h" |
16 #include "ppapi/c/pp_errors.h" | 18 #include "ppapi/c/pp_errors.h" |
17 #include "ppapi/c/pp_instance.h" | 19 #include "ppapi/c/pp_instance.h" |
18 #include "ppapi/c/pp_resource.h" | 20 #include "ppapi/c/pp_resource.h" |
19 #include "ppapi/c/pp_var.h" | 21 #include "ppapi/c/pp_var.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); | 193 RUN_TEST_WITH_REFERENCE_CHECK(Protocols, filter); |
192 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); | 194 RUN_TEST_WITH_REFERENCE_CHECK(GetURL, filter); |
193 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); | 195 RUN_TEST_WITH_REFERENCE_CHECK(ValidConnect, filter); |
194 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); | 196 RUN_TEST_WITH_REFERENCE_CHECK(InvalidClose, filter); |
195 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); | 197 RUN_TEST_WITH_REFERENCE_CHECK(ValidClose, filter); |
196 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); | 198 RUN_TEST_WITH_REFERENCE_CHECK(GetProtocol, filter); |
197 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); | 199 RUN_TEST_WITH_REFERENCE_CHECK(TextSendReceive, filter); |
198 RUN_TEST_WITH_REFERENCE_CHECK(BinarySendReceive, filter); | 200 RUN_TEST_WITH_REFERENCE_CHECK(BinarySendReceive, filter); |
199 RUN_TEST_WITH_REFERENCE_CHECK(StressedSendReceive, filter); | 201 RUN_TEST_WITH_REFERENCE_CHECK(StressedSendReceive, filter); |
200 RUN_TEST_WITH_REFERENCE_CHECK(BufferedAmount, filter); | 202 RUN_TEST_WITH_REFERENCE_CHECK(BufferedAmount, filter); |
| 203 RUN_TEST_WITH_REFERENCE_CHECK(AbortCalls, filter); |
201 | 204 |
202 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); | 205 RUN_TEST_WITH_REFERENCE_CHECK(CcInterfaces, filter); |
203 | 206 |
204 RUN_TEST_WITH_REFERENCE_CHECK(UtilityInvalidConnect, filter); | 207 RUN_TEST_WITH_REFERENCE_CHECK(UtilityInvalidConnect, filter); |
205 RUN_TEST_WITH_REFERENCE_CHECK(UtilityProtocols, filter); | 208 RUN_TEST_WITH_REFERENCE_CHECK(UtilityProtocols, filter); |
206 RUN_TEST_WITH_REFERENCE_CHECK(UtilityGetURL, filter); | 209 RUN_TEST_WITH_REFERENCE_CHECK(UtilityGetURL, filter); |
207 RUN_TEST_WITH_REFERENCE_CHECK(UtilityValidConnect, filter); | 210 RUN_TEST_WITH_REFERENCE_CHECK(UtilityValidConnect, filter); |
208 RUN_TEST_WITH_REFERENCE_CHECK(UtilityInvalidClose, filter); | 211 RUN_TEST_WITH_REFERENCE_CHECK(UtilityInvalidClose, filter); |
209 RUN_TEST_WITH_REFERENCE_CHECK(UtilityValidClose, filter); | 212 RUN_TEST_WITH_REFERENCE_CHECK(UtilityValidClose, filter); |
210 RUN_TEST_WITH_REFERENCE_CHECK(UtilityGetProtocol, filter); | 213 RUN_TEST_WITH_REFERENCE_CHECK(UtilityGetProtocol, filter); |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 ASSERT_TRUE(ws); | 645 ASSERT_TRUE(ws); |
643 ASSERT_EQ(PP_OK, connect_result); | 646 ASSERT_EQ(PP_OK, connect_result); |
644 | 647 |
645 // Prepare PP_Var objects to send. | 648 // Prepare PP_Var objects to send. |
646 const char* text = "hello pepper"; | 649 const char* text = "hello pepper"; |
647 PP_Var text_var = CreateVarString(text); | 650 PP_Var text_var = CreateVarString(text); |
648 std::vector<uint8_t> binary(256); | 651 std::vector<uint8_t> binary(256); |
649 for (uint32_t i = 0; i < binary.size(); ++i) | 652 for (uint32_t i = 0; i < binary.size(); ++i) |
650 binary[i] = i; | 653 binary[i] = i; |
651 PP_Var binary_var = CreateVarBinary(binary); | 654 PP_Var binary_var = CreateVarBinary(binary); |
| 655 // Prepare very large binary data over 64KiB. Object serializer in |
| 656 // ppapi_proxy has a limitation of 64KiB as maximum return PP_Var data size |
| 657 // to SRPC. In case received data over 64KiB exists, a specific code handles |
| 658 // this large data via asynchronous callback from main thread. This data |
| 659 // intends to test the code. |
| 660 std::vector<uint8_t> large_binary(65 * 1024); |
| 661 for (uint32_t i = 0; i < large_binary.size(); ++i) |
| 662 large_binary[i] = i & 0xff; |
| 663 PP_Var large_binary_var = CreateVarBinary(large_binary); |
652 | 664 |
653 // Send many messages. | 665 // Send many messages. |
| 666 int32_t result; |
654 for (int i = 0; i < 256; ++i) { | 667 for (int i = 0; i < 256; ++i) { |
655 int32_t result = websocket_interface_->SendMessage(ws, text_var); | 668 result = websocket_interface_->SendMessage(ws, text_var); |
656 ASSERT_EQ(PP_OK, result); | 669 ASSERT_EQ(PP_OK, result); |
657 result = websocket_interface_->SendMessage(ws, binary_var); | 670 result = websocket_interface_->SendMessage(ws, binary_var); |
658 ASSERT_EQ(PP_OK, result); | 671 ASSERT_EQ(PP_OK, result); |
659 } | 672 } |
| 673 result = websocket_interface_->SendMessage(ws, large_binary_var); |
| 674 ASSERT_EQ(PP_OK, result); |
660 ReleaseVar(text_var); | 675 ReleaseVar(text_var); |
661 ReleaseVar(binary_var); | 676 ReleaseVar(binary_var); |
| 677 ReleaseVar(large_binary_var); |
662 | 678 |
663 // Receive echoed data. | 679 // Receive echoed data. |
664 for (int i = 0; i < 512; ++i) { | 680 for (int i = 0; i <= 512; ++i) { |
665 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 681 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
666 PP_Var received_message; | 682 PP_Var received_message; |
667 int32_t result = websocket_interface_->ReceiveMessage( | 683 result = websocket_interface_->ReceiveMessage( |
668 ws, &received_message, callback.GetCallback().pp_completion_callback()); | 684 ws, &received_message, callback.GetCallback().pp_completion_callback()); |
669 ASSERT_TRUE(result == PP_OK || result == PP_OK_COMPLETIONPENDING); | 685 ASSERT_TRUE(result == PP_OK || result == PP_OK_COMPLETIONPENDING); |
670 if (result == PP_OK_COMPLETIONPENDING) | 686 if (result == PP_OK_COMPLETIONPENDING) |
671 result = callback.WaitForResult(); | 687 result = callback.WaitForResult(); |
672 ASSERT_EQ(PP_OK, result); | 688 ASSERT_EQ(PP_OK, result); |
673 if (i & 1) { | 689 if (i == 512) { |
| 690 ASSERT_TRUE(AreEqualWithBinary(received_message, large_binary)); |
| 691 } else if (i & 1) { |
674 ASSERT_TRUE(AreEqualWithBinary(received_message, binary)); | 692 ASSERT_TRUE(AreEqualWithBinary(received_message, binary)); |
675 } else { | 693 } else { |
676 ASSERT_TRUE(AreEqualWithString(received_message, text)); | 694 ASSERT_TRUE(AreEqualWithString(received_message, text)); |
677 } | 695 } |
678 ReleaseVar(received_message); | 696 ReleaseVar(received_message); |
679 } | 697 } |
680 core_interface_->ReleaseResource(ws); | 698 core_interface_->ReleaseResource(ws); |
681 | 699 |
682 PASS(); | 700 PASS(); |
683 } | 701 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 ASSERT_EQ(base_buffered_amount + reason_frame_size, buffered_amount); | 757 ASSERT_EQ(base_buffered_amount + reason_frame_size, buffered_amount); |
740 | 758 |
741 ReleaseVar(message_var); | 759 ReleaseVar(message_var); |
742 ReleaseVar(reason); | 760 ReleaseVar(reason); |
743 ReleaseVar(empty_string); | 761 ReleaseVar(empty_string); |
744 core_interface_->ReleaseResource(ws); | 762 core_interface_->ReleaseResource(ws); |
745 | 763 |
746 PASS(); | 764 PASS(); |
747 } | 765 } |
748 | 766 |
| 767 std::string TestWebSocket::TestAbortCalls() { |
| 768 // Test abort behaviors where a WebSocket PP_Resource is released while |
| 769 // each function is in-flight on the WebSocket PP_Resource. |
| 770 std::vector<uint8_t> large_binary(65 * 1024); |
| 771 PP_Var large_var = CreateVarBinary(large_binary); |
| 772 |
| 773 // Firstly, test the behavior for SendMessage(). |
| 774 // This function doesn't require a callback, but operation will be done |
| 775 // asynchronously in WebKit and browser process. |
| 776 int32_t result; |
| 777 std::string url = GetFullURL(kEchoServerURL); |
| 778 PP_Resource ws = Connect(url, &result, ""); |
| 779 ASSERT_TRUE(ws); |
| 780 ASSERT_EQ(PP_OK, result); |
| 781 result = websocket_interface_->SendMessage(ws, large_var); |
| 782 ASSERT_EQ(PP_OK, result); |
| 783 core_interface_->ReleaseResource(ws); |
| 784 |
| 785 // Following tests make sure the behavior for functions which require a |
| 786 // callback. The callback must get a PP_ERROR_ABORTED. |
| 787 // Test the behavior for Connect(). |
| 788 ws = websocket_interface_->Create(instance_->pp_instance()); |
| 789 ASSERT_TRUE(ws); |
| 790 PP_Var url_var = CreateVarString(url); |
| 791 TestCompletionCallback connect_callback( |
| 792 instance_->pp_instance(), force_async_); |
| 793 result = websocket_interface_->Connect(ws, url_var, NULL, 0, |
| 794 connect_callback.GetCallback().pp_completion_callback()); |
| 795 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 796 core_interface_->ReleaseResource(ws); |
| 797 result = connect_callback.WaitForResult(); |
| 798 ASSERT_EQ(PP_ERROR_ABORTED, result); |
| 799 ReleaseVar(url_var); |
| 800 |
| 801 // Test the behavior for Close(). |
| 802 ws = Connect(url, &result, ""); |
| 803 ASSERT_TRUE(ws); |
| 804 ASSERT_EQ(PP_OK, result); |
| 805 PP_Var reason_var = CreateVarString("abort"); |
| 806 TestCompletionCallback close_callback( |
| 807 instance_->pp_instance(), force_async_); |
| 808 result = websocket_interface_->Close(ws, |
| 809 PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE, reason_var, |
| 810 close_callback.GetCallback().pp_completion_callback()); |
| 811 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 812 core_interface_->ReleaseResource(ws); |
| 813 result = close_callback.WaitForResult(); |
| 814 ASSERT_EQ(PP_ERROR_ABORTED, result); |
| 815 ReleaseVar(reason_var); |
| 816 |
| 817 // Test the behavior for ReceiveMessage(). |
| 818 // Firstly, make sure the simplest case to wait for data which never arrives. |
| 819 ws = Connect(url, &result, ""); |
| 820 ASSERT_TRUE(ws); |
| 821 ASSERT_EQ(PP_OK, result); |
| 822 PP_Var receive_var; |
| 823 TestCompletionCallback receive_callback( |
| 824 instance_->pp_instance(), force_async_); |
| 825 result = websocket_interface_->ReceiveMessage(ws, &receive_var, |
| 826 receive_callback.GetCallback().pp_completion_callback()); |
| 827 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 828 core_interface_->ReleaseResource(ws); |
| 829 result = receive_callback.WaitForResult(); |
| 830 ASSERT_EQ(PP_ERROR_ABORTED, result); |
| 831 |
| 832 // Test the behavior where receive process might be in-flight. |
| 833 const char* text = "yukarin"; |
| 834 PP_Var text_var = CreateVarString(text); |
| 835 |
| 836 // Each trial sends 17 messages and receives just |trial| number of |
| 837 // message(s) before releasing the WebSocket. The WebSocket is released while |
| 838 // the next message is going to be received. |
| 839 for (int trial = 1; trial <= 16; trial++) { |
| 840 ws = Connect(url, &result, ""); |
| 841 ASSERT_TRUE(ws); |
| 842 ASSERT_EQ(PP_OK, result); |
| 843 for (int i = 0; i <= 16; ++i) { |
| 844 result = websocket_interface_->SendMessage(ws, text_var); |
| 845 ASSERT_EQ(PP_OK, result); |
| 846 } |
| 847 std::auto_ptr<TestCompletionCallback> callback; |
| 848 PP_Var var; |
| 849 for (int i = 0; i < trial; ++i) { |
| 850 callback.reset( |
| 851 new TestCompletionCallback(instance_->pp_instance(), force_async_)); |
| 852 result = websocket_interface_->ReceiveMessage( |
| 853 ws, &var, callback->GetCallback().pp_completion_callback()); |
| 854 if (result == PP_OK_COMPLETIONPENDING) |
| 855 result = callback->WaitForResult(); |
| 856 ASSERT_EQ(PP_OK, result); |
| 857 ASSERT_TRUE(AreEqualWithString(var, text)); |
| 858 ReleaseVar(var); |
| 859 } |
| 860 result = websocket_interface_->ReceiveMessage( |
| 861 ws, &var, callback->GetCallback().pp_completion_callback()); |
| 862 core_interface_->ReleaseResource(ws); |
| 863 if (result != PP_OK) { |
| 864 result = callback->WaitForResult(); |
| 865 ASSERT_EQ(PP_ERROR_ABORTED, result); |
| 866 } |
| 867 } |
| 868 // Same test, but the last receiving message is large message over 64KiB. |
| 869 for (int trial = 1; trial <= 16; trial++) { |
| 870 ws = Connect(url, &result, ""); |
| 871 ASSERT_TRUE(ws); |
| 872 ASSERT_EQ(PP_OK, result); |
| 873 for (int i = 0; i <= 16; ++i) { |
| 874 if (i == trial) |
| 875 result = websocket_interface_->SendMessage(ws, large_var); |
| 876 else |
| 877 result = websocket_interface_->SendMessage(ws, text_var); |
| 878 ASSERT_EQ(PP_OK, result); |
| 879 } |
| 880 std::auto_ptr<TestCompletionCallback> callback; |
| 881 PP_Var var; |
| 882 for (int i = 0; i < trial; ++i) { |
| 883 callback.reset( |
| 884 new TestCompletionCallback(instance_->pp_instance(), force_async_)); |
| 885 result = websocket_interface_->ReceiveMessage( |
| 886 ws, &var, callback->GetCallback().pp_completion_callback()); |
| 887 if (result == PP_OK_COMPLETIONPENDING) |
| 888 result = callback->WaitForResult(); |
| 889 ASSERT_EQ(PP_OK, result); |
| 890 ASSERT_TRUE(AreEqualWithString(var, text)); |
| 891 ReleaseVar(var); |
| 892 } |
| 893 result = websocket_interface_->ReceiveMessage( |
| 894 ws, &var, callback->GetCallback().pp_completion_callback()); |
| 895 core_interface_->ReleaseResource(ws); |
| 896 if (result != PP_OK) { |
| 897 result = callback->WaitForResult(); |
| 898 ASSERT_EQ(PP_ERROR_ABORTED, result); |
| 899 } |
| 900 } |
| 901 |
| 902 ReleaseVar(large_var); |
| 903 ReleaseVar(text_var); |
| 904 |
| 905 PASS(); |
| 906 } |
| 907 |
749 std::string TestWebSocket::TestCcInterfaces() { | 908 std::string TestWebSocket::TestCcInterfaces() { |
750 // C++ bindings is simple straightforward, then just verifies interfaces work | 909 // C++ bindings is simple straightforward, then just verifies interfaces work |
751 // as a interface bridge fine. | 910 // as a interface bridge fine. |
752 pp::WebSocket ws(instance_); | 911 pp::WebSocket ws(instance_); |
753 | 912 |
754 // Check uninitialized properties access. | 913 // Check uninitialized properties access. |
755 ASSERT_EQ(0, ws.GetBufferedAmount()); | 914 ASSERT_EQ(0, ws.GetBufferedAmount()); |
756 ASSERT_EQ(0, ws.GetCloseCode()); | 915 ASSERT_EQ(0, ws.GetCloseCode()); |
757 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); | 916 ASSERT_TRUE(AreEqualWithString(ws.GetCloseReason().pp_var(), "")); |
758 ASSERT_EQ(false, ws.GetCloseWasClean()); | 917 ASSERT_EQ(false, ws.GetCloseWasClean()); |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 size_t last_event = events_on_closed - 1; | 1304 size_t last_event = events_on_closed - 1; |
1146 for (uint32_t i = 1; i < last_event; ++i) { | 1305 for (uint32_t i = 1; i < last_event; ++i) { |
1147 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[i].event_type); | 1306 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[i].event_type); |
1148 ASSERT_TRUE(AreEqualWithString(events[i].var.pp_var(), message)); | 1307 ASSERT_TRUE(AreEqualWithString(events[i].var.pp_var(), message)); |
1149 } | 1308 } |
1150 ASSERT_EQ(WebSocketEvent::EVENT_CLOSE, events[last_event].event_type); | 1309 ASSERT_EQ(WebSocketEvent::EVENT_CLOSE, events[last_event].event_type); |
1151 ASSERT_TRUE(events[last_event].was_clean); | 1310 ASSERT_TRUE(events[last_event].was_clean); |
1152 | 1311 |
1153 PASS(); | 1312 PASS(); |
1154 } | 1313 } |
| 1314 |
OLD | NEW |