Chromium Code Reviews| 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 | 9 |
| 9 #include "ppapi/c/dev/ppb_websocket_dev.h" | 10 #include "ppapi/c/dev/ppb_websocket_dev.h" |
| 10 #include "ppapi/c/pp_errors.h" | 11 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
| 12 #include "ppapi/c/pp_completion_callback.h" | 13 #include "ppapi/c/pp_completion_callback.h" |
| 13 #include "ppapi/c/ppb_core.h" | 14 #include "ppapi/c/ppb_core.h" |
| 14 #include "ppapi/c/ppb_var.h" | 15 #include "ppapi/c/ppb_var.h" |
| 16 #include "ppapi/cpp/dev/websocket_dev.h" | |
| 15 #include "ppapi/cpp/instance.h" | 17 #include "ppapi/cpp/instance.h" |
| 16 #include "ppapi/cpp/module.h" | 18 #include "ppapi/cpp/module.h" |
| 17 #include "ppapi/tests/test_utils.h" | 19 #include "ppapi/tests/test_utils.h" |
| 18 #include "ppapi/tests/testing_instance.h" | 20 #include "ppapi/tests/testing_instance.h" |
| 19 | 21 |
| 20 const char kEchoServerURL[] = | 22 const char kEchoServerURL[] = |
| 21 "ws://localhost:8880/websocket/tests/hybi/echo"; | 23 "ws://localhost:8880/websocket/tests/hybi/echo"; |
| 22 | 24 |
| 23 const char kProtocolTestServerURL[] = | 25 const char kProtocolTestServerURL[] = |
| 24 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; | 26 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; |
| 25 | 27 |
| 26 const char* const kInvalidURLs[] = { | 28 const char* const kInvalidURLs[] = { |
| 27 "http://www.google.com/invalid_scheme", | 29 "http://www.google.com/invalid_scheme", |
| 28 "ws://www.google.com/invalid#fragment", | 30 "ws://www.google.com/invalid#fragment", |
| 29 "ws://www.google.com:65535/invalid_port", | 31 "ws://www.google.com:65535/invalid_port", |
| 30 NULL | 32 NULL |
| 31 }; | 33 }; |
| 32 | 34 |
| 33 // Connection close code is defined in WebSocket protocol specification. | 35 // Connection close code is defined in WebSocket protocol specification. |
| 34 // The magic number 1000 means gracefull closure without any error. | 36 // The magic number 1000 means gracefull closure without any error. |
| 35 // See section 7.4.1. of RFC 6455. | 37 // See section 7.4.1. of RFC 6455. |
| 36 const uint16_t kCloseCodeNormalClosure = 1000; | 38 const uint16_t kCloseCodeNormalClosure = 1000U; |
| 39 | |
| 40 namespace { | |
| 41 | |
| 42 struct WebSocketEvent { | |
| 43 enum EventType { | |
| 44 EVENT_OPEN, | |
| 45 EVENT_MESSAGE, | |
| 46 EVENT_ERROR, | |
| 47 EVENT_CLOSE | |
| 48 }; | |
| 49 | |
| 50 WebSocketEvent( | |
| 51 EventType type, bool was_clean, uint16_t code, const pp::Var& var) | |
| 52 : event_type(type), | |
| 53 was_clean(was_clean), | |
| 54 code(code), | |
| 55 var(var) {} | |
| 56 EventType event_type; | |
| 57 bool was_clean; | |
| 58 uint16_t code; | |
|
dmichael (off chromium)
2011/12/08 21:26:49
nit: close_code?
| |
| 59 pp::Var var; | |
| 60 }; | |
| 61 | |
| 62 class TestWebSocketClient : public pp::WebSocket_Dev { | |
| 63 public: | |
| 64 TestWebSocketClient(pp::Instance* instance) | |
|
dmichael (off chromium)
2011/12/08 21:26:49
nit: explicit
| |
| 65 : pp::WebSocket_Dev(instance), | |
| 66 connected_(false), | |
| 67 received_(false), | |
| 68 closed_(false), | |
| 69 wait_for_connected_(false), | |
| 70 wait_for_received_(false), | |
| 71 wait_for_closed_(false), | |
| 72 instance_(instance->pp_instance()) {} | |
| 73 | |
| 74 virtual void OnOpen() { | |
| 75 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_OPEN, | |
| 76 true, 0U, pp::Var())); | |
| 77 connected_ = true; | |
| 78 if (wait_for_connected_) { | |
| 79 GetTestingInterface()->QuitMessageLoop(instance_); | |
| 80 wait_for_connected_ = false; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 virtual void OnMessage(const pp::Var &message) { | |
| 85 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_MESSAGE, | |
| 86 true, 0U, message)); | |
| 87 received_ = true; | |
| 88 if (wait_for_received_) { | |
| 89 GetTestingInterface()->QuitMessageLoop(instance_); | |
| 90 wait_for_received_ = false; | |
| 91 received_ = false; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 virtual void OnError() { | |
| 96 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_ERROR, | |
| 97 true, 0U, pp::Var())); | |
| 98 } | |
| 99 | |
| 100 virtual void OnClose( | |
| 101 bool was_clean, uint16_t code, const pp::Var& reason) { | |
| 102 events_.push_back(WebSocketEvent(WebSocketEvent::EVENT_CLOSE, | |
| 103 was_clean, code, reason)); | |
| 104 connected_ = true; | |
| 105 closed_ = true; | |
| 106 if (wait_for_connected_ || wait_for_closed_) { | |
| 107 GetTestingInterface()->QuitMessageLoop(instance_); | |
| 108 wait_for_connected_ = false; | |
| 109 wait_for_closed_ = false; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void WaitForConnected() { | |
| 114 if (!connected_) { | |
| 115 wait_for_connected_ = true; | |
| 116 GetTestingInterface()->RunMessageLoop(instance_); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void WaitForReceived() { | |
| 121 if (!received_) { | |
| 122 wait_for_received_ = true; | |
| 123 GetTestingInterface()->RunMessageLoop(instance_); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void WaitForClosed() { | |
| 128 if (!closed_) { | |
| 129 wait_for_closed_ = true; | |
| 130 GetTestingInterface()->RunMessageLoop(instance_); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 const std::vector<WebSocketEvent>& GetSeenEvents() const { | |
| 135 return events_; | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 std::vector<WebSocketEvent> events_; | |
| 140 bool connected_; | |
| 141 bool received_; | |
| 142 bool closed_; | |
| 143 bool wait_for_connected_; | |
| 144 bool wait_for_received_; | |
| 145 bool wait_for_closed_; | |
| 146 PP_Instance instance_; | |
| 147 }; | |
| 148 | |
| 149 } // namespace | |
| 37 | 150 |
| 38 REGISTER_TEST_CASE(WebSocket); | 151 REGISTER_TEST_CASE(WebSocket); |
| 39 | 152 |
| 40 bool TestWebSocket::Init() { | 153 bool TestWebSocket::Init() { |
| 41 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 154 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
| 42 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 155 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
| 43 var_interface_ = static_cast<const PPB_Var*>( | 156 var_interface_ = static_cast<const PPB_Var*>( |
| 44 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 157 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
| 45 core_interface_ = static_cast<const PPB_Core*>( | 158 core_interface_ = static_cast<const PPB_Core*>( |
| 46 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 159 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); |
| 47 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 160 if (!websocket_interface_ || !var_interface_ || !core_interface_) |
| 48 return false; | 161 return false; |
| 49 | 162 |
| 50 return true; | 163 return true; |
| 51 } | 164 } |
| 52 | 165 |
| 53 void TestWebSocket::RunTests(const std::string& filter) { | 166 void TestWebSocket::RunTests(const std::string& filter) { |
| 54 RUN_TEST(IsWebSocket, filter); | 167 RUN_TEST(IsWebSocket, filter); |
| 55 RUN_TEST(UninitializedPropertiesAccess, filter); | 168 RUN_TEST(UninitializedPropertiesAccess, filter); |
| 56 RUN_TEST(InvalidConnect, filter); | 169 RUN_TEST(InvalidConnect, filter); |
| 57 RUN_TEST(GetURL, filter); | 170 RUN_TEST(GetURL, filter); |
| 58 RUN_TEST(ValidConnect, filter); | 171 RUN_TEST(ValidConnect, filter); |
| 59 RUN_TEST(InvalidClose, filter); | 172 RUN_TEST(InvalidClose, filter); |
| 60 RUN_TEST(ValidClose, filter); | 173 RUN_TEST(ValidClose, filter); |
| 61 RUN_TEST(GetProtocol, filter); | 174 RUN_TEST(GetProtocol, filter); |
| 62 RUN_TEST(TextSendReceive, filter); | 175 RUN_TEST(TextSendReceive, filter); |
| 176 | |
| 177 RUN_TEST(CcInvalidConnect, filter); | |
| 178 RUN_TEST(CcGetURL, filter); | |
| 179 RUN_TEST(CcValidConnect, filter); | |
| 180 RUN_TEST(CcGetProtocol, filter); | |
| 181 RUN_TEST(CcTextSendReceive, filter); | |
| 63 } | 182 } |
| 64 | 183 |
| 65 PP_Var TestWebSocket::CreateVar(const char* string) { | 184 PP_Var TestWebSocket::CreateVar(const char* string) { |
| 66 return var_interface_->VarFromUtf8(string, strlen(string)); | 185 return var_interface_->VarFromUtf8(string, strlen(string)); |
| 67 } | 186 } |
| 68 | 187 |
| 69 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 188 void TestWebSocket::ReleaseVar(const PP_Var& var) { |
| 70 var_interface_->Release(var); | 189 var_interface_->Release(var); |
| 71 } | 190 } |
| 72 | 191 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 87 const char* url, int32_t* result, const char* protocol) { | 206 const char* url, int32_t* result, const char* protocol) { |
| 88 PP_Var protocols[] = { PP_MakeUndefined() }; | 207 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 89 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 208 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 90 if (!ws) | 209 if (!ws) |
| 91 return 0; | 210 return 0; |
| 92 PP_Var url_var = CreateVar(url); | 211 PP_Var url_var = CreateVar(url); |
| 93 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 212 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 94 int protocol_count = 0; | 213 int protocol_count = 0; |
| 95 if (protocol) { | 214 if (protocol) { |
| 96 protocols[0] = CreateVar(protocol); | 215 protocols[0] = CreateVar(protocol); |
| 97 protocol_count = 1; | 216 protocol_count = 1U; |
| 98 } | 217 } |
| 99 *result = websocket_interface_->Connect( | 218 *result = websocket_interface_->Connect( |
| 100 ws, url_var, protocols, protocol_count, | 219 ws, url_var, protocols, protocol_count, |
| 101 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 220 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 102 ReleaseVar(url_var); | 221 ReleaseVar(url_var); |
| 103 if (protocol) | 222 if (protocol) |
| 104 ReleaseVar(protocols[0]); | 223 ReleaseVar(protocols[0]); |
| 105 if (*result == PP_OK_COMPLETIONPENDING) | 224 if (*result == PP_OK_COMPLETIONPENDING) |
| 106 *result = callback.WaitForResult(); | 225 *result = callback.WaitForResult(); |
| 107 return ws; | 226 return ws; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 } | 277 } |
| 159 | 278 |
| 160 std::string TestWebSocket::TestInvalidConnect() { | 279 std::string TestWebSocket::TestInvalidConnect() { |
| 161 PP_Var protocols[] = { PP_MakeUndefined() }; | 280 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 162 | 281 |
| 163 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 282 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 164 ASSERT_TRUE(ws); | 283 ASSERT_TRUE(ws); |
| 165 | 284 |
| 166 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 285 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 167 int32_t result = websocket_interface_->Connect( | 286 int32_t result = websocket_interface_->Connect( |
| 168 ws, PP_MakeUndefined(), protocols, 1, | 287 ws, PP_MakeUndefined(), protocols, 1U, |
| 169 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 288 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 170 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 289 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 171 | 290 |
| 172 result = websocket_interface_->Connect( | 291 result = websocket_interface_->Connect( |
| 173 ws, PP_MakeUndefined(), protocols, 1, | 292 ws, PP_MakeUndefined(), protocols, 1U, |
| 174 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 293 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 175 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | 294 ASSERT_EQ(PP_ERROR_INPROGRESS, result); |
| 176 | 295 |
| 177 core_interface_->ReleaseResource(ws); | 296 core_interface_->ReleaseResource(ws); |
| 178 | 297 |
| 179 for (int i = 0; kInvalidURLs[i]; ++i) { | 298 for (int i = 0; kInvalidURLs[i]; ++i) { |
| 180 ws = Connect(kInvalidURLs[i], &result, NULL); | 299 ws = Connect(kInvalidURLs[i], &result, NULL); |
| 181 ASSERT_TRUE(ws); | 300 ASSERT_TRUE(ws); |
| 182 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 301 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 183 | 302 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 PASS(); | 491 PASS(); |
| 373 } | 492 } |
| 374 | 493 |
| 375 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 494 // TODO(toyoshim): Add tests for GetBufferedAmount(). |
| 376 // For now, the function doesn't work fine because update callback in WebKit is | 495 // For now, the function doesn't work fine because update callback in WebKit is |
| 377 // not landed yet. | 496 // not landed yet. |
| 378 | 497 |
| 379 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 498 // TODO(toyoshim): Add tests for didReceiveMessageError(). |
| 380 | 499 |
| 381 // TODO(toyoshim): Add other function tests. | 500 // TODO(toyoshim): Add other function tests. |
| 501 | |
| 502 std::string TestWebSocket::TestCcInvalidConnect() { | |
| 503 const pp::Var protocols[] = { pp::Var() }; | |
| 504 | |
| 505 TestWebSocketClient websocket(instance_); | |
| 506 int32_t result = websocket.Connect(pp::Var(), protocols, 1U); | |
| 507 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
| 508 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
| 509 | |
| 510 result = websocket.Connect(pp::Var(), protocols, 1U); | |
| 511 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | |
| 512 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
| 513 | |
| 514 for (int i = 0; kInvalidURLs[i]; ++i) { | |
| 515 TestWebSocketClient ws(instance_); | |
| 516 result = ws.Connect(pp::Var(std::string(kInvalidURLs[i])), protocols, 0U); | |
| 517 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
| 518 ASSERT_EQ(0U, ws.GetSeenEvents().size()); | |
| 519 } | |
| 520 | |
| 521 PASS(); | |
| 522 } | |
| 523 | |
| 524 std::string TestWebSocket::TestCcGetURL() { | |
| 525 const pp::Var protocols[] = { pp::Var() }; | |
| 526 | |
| 527 for (int i = 0; kInvalidURLs[i]; ++i) { | |
| 528 TestWebSocketClient websocket(instance_); | |
| 529 int32_t result = websocket.Connect( | |
| 530 pp::Var(std::string(kInvalidURLs[i])), protocols, 0U); | |
| 531 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
| 532 pp::Var url = websocket.GetURL(); | |
| 533 ASSERT_TRUE(AreEqual(url.pp_var(), kInvalidURLs[i])); | |
| 534 ASSERT_EQ(0U, websocket.GetSeenEvents().size()); | |
| 535 } | |
| 536 | |
| 537 PASS(); | |
| 538 } | |
| 539 | |
| 540 std::string TestWebSocket::TestCcValidConnect() { | |
| 541 const pp::Var protocols[] = { pp::Var() }; | |
| 542 TestWebSocketClient websocket(instance_); | |
| 543 int32_t result = websocket.Connect( | |
| 544 pp::Var(std::string(kEchoServerURL)), protocols, 0U); | |
| 545 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 546 websocket.WaitForConnected(); | |
| 547 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
| 548 ASSERT_EQ(1U, events.size()); | |
| 549 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
| 550 | |
| 551 PASS(); | |
| 552 } | |
| 553 | |
| 554 std::string TestWebSocket::TestCcGetProtocol() { | |
| 555 const std::string protocol("x-chat"); | |
| 556 const pp::Var protocols[] = { pp::Var(protocol) }; | |
| 557 std::string url(kProtocolTestServerURL); | |
| 558 url += protocol; | |
| 559 TestWebSocketClient websocket(instance_); | |
| 560 int32_t result = websocket.Connect(pp::Var(url), protocols, 1U); | |
| 561 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 562 websocket.WaitForConnected(); | |
| 563 ASSERT_TRUE(AreEqual(websocket.GetProtocol().pp_var(), protocol.c_str())); | |
| 564 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
| 565 ASSERT_EQ(1U, events.size()); | |
| 566 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
| 567 | |
| 568 PASS(); | |
| 569 } | |
| 570 | |
| 571 std::string TestWebSocket::TestCcTextSendReceive() { | |
| 572 const pp::Var protocols[] = { pp::Var() }; | |
| 573 TestWebSocketClient websocket(instance_); | |
| 574 int32_t result = | |
| 575 websocket.Connect(pp::Var(std::string(kEchoServerURL)), protocols, 0U); | |
| 576 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 577 websocket.WaitForConnected(); | |
| 578 | |
| 579 // Send 'hello pepper'. | |
| 580 std::string message1("hello pepper"); | |
| 581 result = websocket.Send(pp::Var(std::string(message1))); | |
| 582 ASSERT_EQ(PP_OK, result); | |
| 583 | |
| 584 // Receive echoed 'hello pepper'. | |
| 585 websocket.WaitForReceived(); | |
| 586 | |
| 587 // Send 'goodbye pepper'. | |
| 588 std::string message2("goodbye pepper"); | |
| 589 result = websocket.Send(pp::Var(std::string(message2))); | |
| 590 | |
| 591 // Receive echoed 'goodbye pepper'. | |
| 592 websocket.WaitForReceived(); | |
| 593 | |
| 594 const std::vector<WebSocketEvent>& events = websocket.GetSeenEvents(); | |
| 595 ASSERT_EQ(3U, events.size()); | |
| 596 ASSERT_EQ(WebSocketEvent::EVENT_OPEN, events[0].event_type); | |
| 597 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[1].event_type); | |
| 598 ASSERT_TRUE(AreEqual(events[1].var.pp_var(), message1.c_str())); | |
| 599 ASSERT_EQ(WebSocketEvent::EVENT_MESSAGE, events[2].event_type); | |
| 600 ASSERT_TRUE(AreEqual(events[2].var.pp_var(), message2.c_str())); | |
| 601 | |
| 602 PASS(); | |
| 603 } | |
| OLD | NEW |