| 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 | 8 |
| 9 #include "ppapi/c/dev/ppb_websocket_dev.h" | 9 #include "ppapi/c/dev/ppb_websocket_dev.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/c/pp_var.h" | 11 #include "ppapi/c/pp_var.h" |
| 12 #include "ppapi/c/pp_completion_callback.h" | 12 #include "ppapi/c/pp_completion_callback.h" |
| 13 #include "ppapi/c/ppb_core.h" | 13 #include "ppapi/c/ppb_core.h" |
| 14 #include "ppapi/c/ppb_var.h" | 14 #include "ppapi/c/ppb_var.h" |
| 15 #include "ppapi/cpp/dev/websocket_dev.h" |
| 15 #include "ppapi/cpp/instance.h" | 16 #include "ppapi/cpp/instance.h" |
| 16 #include "ppapi/cpp/module.h" | 17 #include "ppapi/cpp/module.h" |
| 17 #include "ppapi/tests/test_utils.h" | 18 #include "ppapi/tests/test_utils.h" |
| 18 #include "ppapi/tests/testing_instance.h" | 19 #include "ppapi/tests/testing_instance.h" |
| 19 | 20 |
| 20 const char kEchoServerURL[] = | 21 const char kEchoServerURL[] = |
| 21 "ws://localhost:8880/websocket/tests/hybi/echo"; | 22 "ws://localhost:8880/websocket/tests/hybi/echo"; |
| 23 const char kCloseServerURL[] = |
| 24 "ws://localhost:8880/websocket/tests/hybi/close"; |
| 22 | 25 |
| 23 const char kProtocolTestServerURL[] = | 26 const char kProtocolTestServerURL[] = |
| 24 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; | 27 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; |
| 25 | 28 |
| 26 const char* const kInvalidURLs[] = { | 29 const char* const kInvalidURLs[] = { |
| 27 "http://www.google.com/invalid_scheme", | 30 "http://www.google.com/invalid_scheme", |
| 28 "ws://www.google.com/invalid#fragment", | 31 "ws://www.google.com/invalid#fragment", |
| 29 "ws://www.google.com:65535/invalid_port", | 32 "ws://www.google.com:65535/invalid_port", |
| 30 NULL | 33 NULL |
| 31 }; | 34 }; |
| 32 | 35 |
| 33 // Connection close code is defined in WebSocket protocol specification. | 36 // Connection close code is defined in WebSocket protocol specification. |
| 34 // The magic number 1000 means gracefull closure without any error. | 37 // The magic number 1000 means gracefull closure without any error. |
| 35 // See section 7.4.1. of RFC 6455. | 38 // See section 7.4.1. of RFC 6455. |
| 36 const uint16_t kCloseCodeNormalClosure = 1000; | 39 const uint16_t kCloseCodeNormalClosure = 1000U; |
| 37 | 40 |
| 38 REGISTER_TEST_CASE(WebSocket); | 41 REGISTER_TEST_CASE(WebSocket); |
| 39 | 42 |
| 40 bool TestWebSocket::Init() { | 43 bool TestWebSocket::Init() { |
| 41 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 44 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
| 42 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 45 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
| 43 var_interface_ = static_cast<const PPB_Var*>( | 46 var_interface_ = static_cast<const PPB_Var*>( |
| 44 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 47 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
| 45 core_interface_ = static_cast<const PPB_Core*>( | 48 core_interface_ = static_cast<const PPB_Core*>( |
| 46 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 49 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); |
| 47 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 50 if (!websocket_interface_ || !var_interface_ || !core_interface_) |
| 48 return false; | 51 return false; |
| 49 | 52 |
| 50 return true; | 53 return true; |
| 51 } | 54 } |
| 52 | 55 |
| 53 void TestWebSocket::RunTests(const std::string& filter) { | 56 void TestWebSocket::RunTests(const std::string& filter) { |
| 54 RUN_TEST(IsWebSocket, filter); | 57 RUN_TEST(IsWebSocket, filter); |
| 55 RUN_TEST(UninitializedPropertiesAccess, filter); | 58 RUN_TEST(UninitializedPropertiesAccess, filter); |
| 56 RUN_TEST(InvalidConnect, filter); | 59 RUN_TEST(InvalidConnect, filter); |
| 57 RUN_TEST(GetURL, filter); | 60 RUN_TEST(GetURL, filter); |
| 58 RUN_TEST(ValidConnect, filter); | 61 RUN_TEST(ValidConnect, filter); |
| 59 RUN_TEST(InvalidClose, filter); | 62 RUN_TEST(InvalidClose, filter); |
| 60 RUN_TEST(ValidClose, filter); | 63 RUN_TEST(ValidClose, filter); |
| 61 RUN_TEST(GetProtocol, filter); | 64 RUN_TEST(GetProtocol, filter); |
| 62 RUN_TEST(TextSendReceive, filter); | 65 RUN_TEST(TextSendReceive, filter); |
| 66 |
| 67 RUN_TEST(CcInterfaces, filter); |
| 63 } | 68 } |
| 64 | 69 |
| 65 PP_Var TestWebSocket::CreateVar(const char* string) { | 70 PP_Var TestWebSocket::CreateVar(const char* string) { |
| 66 return var_interface_->VarFromUtf8(string, strlen(string)); | 71 return var_interface_->VarFromUtf8(string, strlen(string)); |
| 67 } | 72 } |
| 68 | 73 |
| 69 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 74 void TestWebSocket::ReleaseVar(const PP_Var& var) { |
| 70 var_interface_->Release(var); | 75 var_interface_->Release(var); |
| 71 } | 76 } |
| 72 | 77 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 87 const char* url, int32_t* result, const char* protocol) { | 92 const char* url, int32_t* result, const char* protocol) { |
| 88 PP_Var protocols[] = { PP_MakeUndefined() }; | 93 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 89 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 94 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 90 if (!ws) | 95 if (!ws) |
| 91 return 0; | 96 return 0; |
| 92 PP_Var url_var = CreateVar(url); | 97 PP_Var url_var = CreateVar(url); |
| 93 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 98 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 94 int protocol_count = 0; | 99 int protocol_count = 0; |
| 95 if (protocol) { | 100 if (protocol) { |
| 96 protocols[0] = CreateVar(protocol); | 101 protocols[0] = CreateVar(protocol); |
| 97 protocol_count = 1; | 102 protocol_count = 1U; |
| 98 } | 103 } |
| 99 *result = websocket_interface_->Connect( | 104 *result = websocket_interface_->Connect( |
| 100 ws, url_var, protocols, protocol_count, | 105 ws, url_var, protocols, protocol_count, |
| 101 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 106 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 102 ReleaseVar(url_var); | 107 ReleaseVar(url_var); |
| 103 if (protocol) | 108 if (protocol) |
| 104 ReleaseVar(protocols[0]); | 109 ReleaseVar(protocols[0]); |
| 105 if (*result == PP_OK_COMPLETIONPENDING) | 110 if (*result == PP_OK_COMPLETIONPENDING) |
| 106 *result = callback.WaitForResult(); | 111 *result = callback.WaitForResult(); |
| 107 return ws; | 112 return ws; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 130 ASSERT_TRUE(ws); | 135 ASSERT_TRUE(ws); |
| 131 | 136 |
| 132 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws); | 137 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws); |
| 133 ASSERT_EQ(0, bufferedAmount); | 138 ASSERT_EQ(0, bufferedAmount); |
| 134 | 139 |
| 135 uint16_t close_code = websocket_interface_->GetCloseCode(ws); | 140 uint16_t close_code = websocket_interface_->GetCloseCode(ws); |
| 136 ASSERT_EQ(0, close_code); | 141 ASSERT_EQ(0, close_code); |
| 137 | 142 |
| 138 PP_Var close_reason = websocket_interface_->GetCloseReason(ws); | 143 PP_Var close_reason = websocket_interface_->GetCloseReason(ws); |
| 139 ASSERT_TRUE(AreEqual(close_reason, "")); | 144 ASSERT_TRUE(AreEqual(close_reason, "")); |
| 145 ReleaseVar(close_reason); |
| 140 | 146 |
| 141 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws); | 147 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws); |
| 142 ASSERT_EQ(PP_FALSE, close_was_clean); | 148 ASSERT_EQ(PP_FALSE, close_was_clean); |
| 143 | 149 |
| 144 PP_Var extensions = websocket_interface_->GetExtensions(ws); | 150 PP_Var extensions = websocket_interface_->GetExtensions(ws); |
| 145 ASSERT_TRUE(AreEqual(extensions, "")); | 151 ASSERT_TRUE(AreEqual(extensions, "")); |
| 152 ReleaseVar(extensions); |
| 146 | 153 |
| 147 PP_Var protocol = websocket_interface_->GetProtocol(ws); | 154 PP_Var protocol = websocket_interface_->GetProtocol(ws); |
| 148 ASSERT_TRUE(AreEqual(protocol, "")); | 155 ASSERT_TRUE(AreEqual(protocol, "")); |
| 156 ReleaseVar(protocol); |
| 149 | 157 |
| 150 PP_WebSocketReadyState_Dev ready_state = | 158 PP_WebSocketReadyState_Dev ready_state = |
| 151 websocket_interface_->GetReadyState(ws); | 159 websocket_interface_->GetReadyState(ws); |
| 152 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ready_state); | 160 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ready_state); |
| 153 | 161 |
| 154 PP_Var url = websocket_interface_->GetURL(ws); | 162 PP_Var url = websocket_interface_->GetURL(ws); |
| 155 ASSERT_TRUE(AreEqual(url, "")); | 163 ASSERT_TRUE(AreEqual(url, "")); |
| 164 ReleaseVar(url); |
| 156 | 165 |
| 157 PASS(); | 166 PASS(); |
| 158 } | 167 } |
| 159 | 168 |
| 160 std::string TestWebSocket::TestInvalidConnect() { | 169 std::string TestWebSocket::TestInvalidConnect() { |
| 161 PP_Var protocols[] = { PP_MakeUndefined() }; | 170 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 162 | 171 |
| 163 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 172 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 164 ASSERT_TRUE(ws); | 173 ASSERT_TRUE(ws); |
| 165 | 174 |
| 166 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 175 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 167 int32_t result = websocket_interface_->Connect( | 176 int32_t result = websocket_interface_->Connect( |
| 168 ws, PP_MakeUndefined(), protocols, 1, | 177 ws, PP_MakeUndefined(), protocols, 1U, |
| 169 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 178 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 170 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 179 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 171 | 180 |
| 172 result = websocket_interface_->Connect( | 181 result = websocket_interface_->Connect( |
| 173 ws, PP_MakeUndefined(), protocols, 1, | 182 ws, PP_MakeUndefined(), protocols, 1U, |
| 174 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 183 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 175 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | 184 ASSERT_EQ(PP_ERROR_INPROGRESS, result); |
| 176 | 185 |
| 177 core_interface_->ReleaseResource(ws); | 186 core_interface_->ReleaseResource(ws); |
| 178 | 187 |
| 179 for (int i = 0; kInvalidURLs[i]; ++i) { | 188 for (int i = 0; kInvalidURLs[i]; ++i) { |
| 180 ws = Connect(kInvalidURLs[i], &result, NULL); | 189 ws = Connect(kInvalidURLs[i], &result, NULL); |
| 181 ASSERT_TRUE(ws); | 190 ASSERT_TRUE(ws); |
| 182 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 191 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 183 | 192 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 PASS(); | 381 PASS(); |
| 373 } | 382 } |
| 374 | 383 |
| 375 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 384 // TODO(toyoshim): Add tests for GetBufferedAmount(). |
| 376 // For now, the function doesn't work fine because update callback in WebKit is | 385 // For now, the function doesn't work fine because update callback in WebKit is |
| 377 // not landed yet. | 386 // not landed yet. |
| 378 | 387 |
| 379 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 388 // TODO(toyoshim): Add tests for didReceiveMessageError(). |
| 380 | 389 |
| 381 // TODO(toyoshim): Add other function tests. | 390 // TODO(toyoshim): Add other function tests. |
| 391 |
| 392 std::string TestWebSocket::TestCcInterfaces() { |
| 393 // C++ bindings is simple straightforward, then just verifies interfaces work |
| 394 // as a interface bridge fine. |
| 395 pp::WebSocket_Dev ws(instance_); |
| 396 |
| 397 // Check uninitialized properties access. |
| 398 ASSERT_EQ(0, ws.GetBufferedAmount()); |
| 399 ASSERT_EQ(0, ws.GetCloseCode()); |
| 400 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), "")); |
| 401 ASSERT_EQ(false, ws.GetCloseWasClean()); |
| 402 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); |
| 403 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); |
| 404 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ws.GetReadyState()); |
| 405 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), "")); |
| 406 |
| 407 // Check communication interfaces (connect, send, receive, and close). |
| 408 TestCompletionCallback connect_callback(instance_->pp_instance()); |
| 409 int32_t result = ws.Connect(pp::Var(std::string(kCloseServerURL)), NULL, 0U, |
| 410 connect_callback); |
| 411 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 412 result = connect_callback.WaitForResult(); |
| 413 ASSERT_EQ(PP_OK, result); |
| 414 |
| 415 std::string message("hello C++"); |
| 416 result = ws.SendMessage(pp::Var(message)); |
| 417 ASSERT_EQ(PP_OK, result); |
| 418 |
| 419 pp::Var receive_var; |
| 420 TestCompletionCallback receive_callback(instance_->pp_instance()); |
| 421 result = ws.ReceiveMessage(&receive_var, receive_callback); |
| 422 if (result == PP_OK_COMPLETIONPENDING) |
| 423 result = receive_callback.WaitForResult(); |
| 424 ASSERT_EQ(PP_OK, result); |
| 425 ASSERT_TRUE(AreEqual(receive_var.pp_var(), message.c_str())); |
| 426 |
| 427 TestCompletionCallback close_callback(instance_->pp_instance()); |
| 428 std::string reason("bye"); |
| 429 result = ws.Close(kCloseCodeNormalClosure, pp::Var(reason), close_callback); |
| 430 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 431 result = close_callback.WaitForResult(); |
| 432 ASSERT_EQ(PP_OK, result); |
| 433 |
| 434 // Check initialized properties access. |
| 435 ASSERT_EQ(0, ws.GetBufferedAmount()); |
| 436 ASSERT_EQ(kCloseCodeNormalClosure, ws.GetCloseCode()); |
| 437 ASSERT_TRUE(AreEqual(ws.GetCloseReason().pp_var(), reason.c_str())); |
| 438 ASSERT_EQ(true, ws.GetCloseWasClean()); |
| 439 ASSERT_TRUE(AreEqual(ws.GetExtensions().pp_var(), "")); |
| 440 ASSERT_TRUE(AreEqual(ws.GetProtocol().pp_var(), "")); |
| 441 ASSERT_EQ(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, ws.GetReadyState()); |
| 442 ASSERT_TRUE(AreEqual(ws.GetURL().pp_var(), kCloseServerURL)); |
| 443 |
| 444 PASS(); |
| 445 } |
| OLD | NEW |