| 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> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "ppapi/c/dev/ppb_websocket_dev.h" | 7 #include "ppapi/c/dev/ppb_websocket_dev.h" |
| 11 #include "ppapi/c/pp_errors.h" | |
| 12 #include "ppapi/c/pp_var.h" | |
| 13 #include "ppapi/c/pp_completion_callback.h" | |
| 14 #include "ppapi/c/ppb_core.h" | |
| 15 #include "ppapi/c/ppb_var.h" | |
| 16 #include "ppapi/cpp/instance.h" | 8 #include "ppapi/cpp/instance.h" |
| 17 #include "ppapi/cpp/module.h" | 9 #include "ppapi/cpp/module.h" |
| 18 #include "ppapi/tests/test_utils.h" | |
| 19 #include "ppapi/tests/testing_instance.h" | 10 #include "ppapi/tests/testing_instance.h" |
| 20 | 11 |
| 21 static const char kEchoServerURL[] = | |
| 22 "ws://localhost:8880/websocket/tests/hybi/echo"; | |
| 23 | |
| 24 REGISTER_TEST_CASE(WebSocket); | 12 REGISTER_TEST_CASE(WebSocket); |
| 25 | 13 |
| 26 bool TestWebSocket::Init() { | 14 bool TestWebSocket::Init() { |
| 27 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 15 websocket_interface_ = reinterpret_cast<PPB_WebSocket_Dev const*>( |
| 28 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 16 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
| 29 var_interface_ = static_cast<const PPB_Var*>( | 17 return !!websocket_interface_; |
| 30 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | |
| 31 core_interface_ = static_cast<const PPB_Core*>( | |
| 32 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | |
| 33 if (!websocket_interface_ || !var_interface_ || !core_interface_) | |
| 34 return false; | |
| 35 | |
| 36 return true; | |
| 37 } | 18 } |
| 38 | 19 |
| 39 void TestWebSocket::RunTests(const std::string& filter) { | 20 void TestWebSocket::RunTests(const std::string& filter) { |
| 40 RUN_TEST(IsWebSocket, filter); | 21 instance_->LogTest("Create", TestCreate()); |
| 41 RUN_TEST(InvalidConnect, filter); | 22 instance_->LogTest("IsWebSocket", TestIsWebSocket()); |
| 42 RUN_TEST(ValidConnect, filter); | |
| 43 RUN_TEST(TextSendReceive, filter); | |
| 44 } | 23 } |
| 45 | 24 |
| 46 PP_Var TestWebSocket::CreateVar(const char* string) { | 25 std::string TestWebSocket::TestCreate() { |
| 47 return var_interface_->VarFromUtf8( | 26 PP_Resource rsrc = websocket_interface_->Create(instance_->pp_instance()); |
| 48 pp::Module::Get()->pp_module(), string, strlen(string)); | 27 if (!rsrc) |
| 49 } | 28 return "Could not create websocket via C interface"; |
| 50 | 29 |
| 51 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 30 PASS(); |
| 52 var_interface_->Release(var); | |
| 53 } | |
| 54 | |
| 55 bool TestWebSocket::AreEqual(const PP_Var& var, const char* string) { | |
| 56 if (var.type != PP_VARTYPE_STRING) | |
| 57 return false; | |
| 58 uint32_t utf8_length; | |
| 59 const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length); | |
| 60 uint32_t string_length = strlen(string); | |
| 61 if (utf8_length != string_length) | |
| 62 return false; | |
| 63 if (strncmp(utf8, string, utf8_length)) | |
| 64 return false; | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 PP_Resource TestWebSocket::Connect() { | |
| 69 PP_Var protocols[] = { PP_MakeUndefined() }; | |
| 70 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 71 if (!ws) | |
| 72 return 0; | |
| 73 PP_Var url = CreateVar(kEchoServerURL); | |
| 74 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 75 int32_t result = websocket_interface_->Connect( | |
| 76 ws, url, protocols, 0, | |
| 77 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 78 ReleaseVar(url); | |
| 79 if (force_async_ && result != PP_OK_COMPLETIONPENDING) { | |
| 80 core_interface_->ReleaseResource(ws); | |
| 81 return 0; | |
| 82 } | |
| 83 if (callback.WaitForResult() != PP_OK) { | |
| 84 core_interface_->ReleaseResource(ws); | |
| 85 return 0; | |
| 86 } | |
| 87 return ws; | |
| 88 } | 31 } |
| 89 | 32 |
| 90 std::string TestWebSocket::TestIsWebSocket() { | 33 std::string TestWebSocket::TestIsWebSocket() { |
| 91 // Test that a NULL resource isn't a websocket. | 34 // Test that a NULL resource isn't a websocket. |
| 92 pp::Resource null_resource; | 35 pp::Resource null_resource; |
| 93 PP_Bool result = | 36 if (websocket_interface_->IsWebSocket(null_resource.pp_resource())) |
| 94 websocket_interface_->IsWebSocket(null_resource.pp_resource()); | 37 return "Null resource was reported as a valid websocket"; |
| 95 ASSERT_FALSE(result); | |
| 96 | 38 |
| 97 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 39 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 98 ASSERT_TRUE(ws); | 40 if (!websocket_interface_->IsWebSocket(ws)) |
| 99 | 41 return "websocket was reported as an invalid websocket"; |
| 100 result = websocket_interface_->IsWebSocket(ws); | |
| 101 ASSERT_TRUE(result); | |
| 102 | |
| 103 core_interface_->ReleaseResource(ws); | |
| 104 | 42 |
| 105 PASS(); | 43 PASS(); |
| 106 } | 44 } |
| 107 | |
| 108 std::string TestWebSocket::TestInvalidConnect() { | |
| 109 PP_Var protocols[] = { PP_MakeUndefined() }; | |
| 110 | |
| 111 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 112 ASSERT_TRUE(ws); | |
| 113 | |
| 114 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 115 int32_t result = websocket_interface_->Connect( | |
| 116 ws, PP_MakeUndefined(), protocols, 1, | |
| 117 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 118 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
| 119 | |
| 120 result = websocket_interface_->Connect( | |
| 121 ws, PP_MakeUndefined(), protocols, 1, | |
| 122 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 123 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | |
| 124 | |
| 125 core_interface_->ReleaseResource(ws); | |
| 126 | |
| 127 const char* invalid_urls[] = { | |
| 128 "http://www.google.com/invalid_scheme", | |
| 129 "ws://www.google.com/invalid#fragment", | |
| 130 "ws://www.google.com:65535/invalid_port", | |
| 131 NULL | |
| 132 }; | |
| 133 for (int i = 0; invalid_urls[i]; ++i) { | |
| 134 ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 135 ASSERT_TRUE(ws); | |
| 136 PP_Var invalid_url = CreateVar(invalid_urls[i]); | |
| 137 result = websocket_interface_->Connect( | |
| 138 ws, invalid_url, protocols, 0, | |
| 139 static_cast<pp::CompletionCallback>( | |
| 140 callback).pp_completion_callback()); | |
| 141 ReleaseVar(invalid_url); | |
| 142 core_interface_->ReleaseResource(ws); | |
| 143 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | |
| 144 } | |
| 145 | |
| 146 // TODO(toyoshim): Add invalid protocols tests | |
| 147 | |
| 148 PASS(); | |
| 149 } | |
| 150 | |
| 151 | |
| 152 std::string TestWebSocket::TestValidConnect() { | |
| 153 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 154 PP_Var url = CreateVar(kEchoServerURL); | |
| 155 PP_Var protocols[] = { PP_MakeUndefined() }; | |
| 156 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 157 int32_t result = websocket_interface_->Connect( | |
| 158 ws, url, protocols, 0, | |
| 159 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 160 ReleaseVar(url); | |
| 161 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 162 result = callback.WaitForResult(); | |
| 163 ASSERT_EQ(PP_OK, result); | |
| 164 core_interface_->ReleaseResource(ws); | |
| 165 | |
| 166 PASS(); | |
| 167 } | |
| 168 | |
| 169 // TODO(toyoshim): Add tests to call various interfaces before calling connect. | |
| 170 | |
| 171 std::string TestWebSocket::TestTextSendReceive() { | |
| 172 // Connect to test echo server. | |
| 173 PP_Resource ws = Connect(); | |
| 174 ASSERT_TRUE(ws); | |
| 175 | |
| 176 // Send 'hello pepper' text message. | |
| 177 const char* message = "hello pepper"; | |
| 178 PP_Var message_var = CreateVar(message); | |
| 179 int32_t result = websocket_interface_->SendMessage(ws, message_var); | |
| 180 ReleaseVar(message_var); | |
| 181 ASSERT_EQ(PP_OK, result); | |
| 182 | |
| 183 // Receive echoed 'hello pepper'. | |
| 184 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
| 185 PP_Var received_message; | |
| 186 result = websocket_interface_->ReceiveMessage(ws, &received_message, | |
| 187 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 188 ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING); | |
| 189 if (result == PP_OK_COMPLETIONPENDING) | |
| 190 result = callback.WaitForResult(); | |
| 191 ASSERT_EQ(PP_OK, result); | |
| 192 ASSERT_TRUE(AreEqual(received_message, message)); | |
| 193 ReleaseVar(received_message); | |
| 194 core_interface_->ReleaseResource(ws); | |
| 195 | |
| 196 PASS(); | |
| 197 } | |
| 198 | |
| 199 // TODO(toyoshim): Add other function tests. | |
| OLD | NEW |