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/instance.h" | 15 #include "ppapi/cpp/instance.h" |
16 #include "ppapi/cpp/module.h" | 16 #include "ppapi/cpp/module.h" |
17 #include "ppapi/tests/test_utils.h" | 17 #include "ppapi/tests/test_utils.h" |
18 #include "ppapi/tests/testing_instance.h" | 18 #include "ppapi/tests/testing_instance.h" |
19 | 19 |
20 static const char kEchoServerURL[] = | 20 const char kEchoServerURL[] = |
21 "ws://localhost:8880/websocket/tests/hybi/echo"; | 21 "ws://localhost:8880/websocket/tests/hybi/echo"; |
22 | 22 |
23 static const char kProtocolTestServerURL[] = | 23 const char kProtocolTestServerURL[] = |
24 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; | 24 "ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol="; |
25 | 25 |
26 static const char* kInvalidURLs[] = { | 26 const char* const kInvalidURLs[] = { |
27 "http://www.google.com/invalid_scheme", | 27 "http://www.google.com/invalid_scheme", |
28 "ws://www.google.com/invalid#fragment", | 28 "ws://www.google.com/invalid#fragment", |
29 "ws://www.google.com:65535/invalid_port", | 29 "ws://www.google.com:65535/invalid_port", |
30 NULL | 30 NULL |
31 }; | 31 }; |
32 | 32 |
| 33 // Connection close code is defined in WebSocket protocol specification. |
| 34 // The magic number 1000 means gracefull closure without any error. |
| 35 // See section 7.4.1. of RFC 6455. |
| 36 const uint16_t kCloseCodeNormalClosure = 1000; |
| 37 |
33 REGISTER_TEST_CASE(WebSocket); | 38 REGISTER_TEST_CASE(WebSocket); |
34 | 39 |
35 bool TestWebSocket::Init() { | 40 bool TestWebSocket::Init() { |
36 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 41 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
37 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 42 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
38 var_interface_ = static_cast<const PPB_Var*>( | 43 var_interface_ = static_cast<const PPB_Var*>( |
39 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 44 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
40 core_interface_ = static_cast<const PPB_Core*>( | 45 core_interface_ = static_cast<const PPB_Core*>( |
41 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 46 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); |
42 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 47 if (!websocket_interface_ || !var_interface_ || !core_interface_) |
43 return false; | 48 return false; |
44 | 49 |
45 return true; | 50 return true; |
46 } | 51 } |
47 | 52 |
48 void TestWebSocket::RunTests(const std::string& filter) { | 53 void TestWebSocket::RunTests(const std::string& filter) { |
49 RUN_TEST(IsWebSocket, filter); | 54 RUN_TEST(IsWebSocket, filter); |
50 RUN_TEST(UninitializedPropertiesAccess, filter); | 55 RUN_TEST(UninitializedPropertiesAccess, filter); |
51 RUN_TEST(InvalidConnect, filter); | 56 RUN_TEST(InvalidConnect, filter); |
52 RUN_TEST(GetURL, filter); | 57 RUN_TEST(GetURL, filter); |
53 RUN_TEST(ValidConnect, filter); | 58 RUN_TEST(ValidConnect, filter); |
| 59 RUN_TEST(InvalidClose, filter); |
| 60 RUN_TEST(ValidClose, filter); |
54 RUN_TEST(GetProtocol, filter); | 61 RUN_TEST(GetProtocol, filter); |
55 RUN_TEST(TextSendReceive, filter); | 62 RUN_TEST(TextSendReceive, filter); |
56 } | 63 } |
57 | 64 |
58 PP_Var TestWebSocket::CreateVar(const char* string) { | 65 PP_Var TestWebSocket::CreateVar(const char* string) { |
59 return var_interface_->VarFromUtf8( | 66 return var_interface_->VarFromUtf8( |
60 pp::Module::Get()->pp_module(), string, strlen(string)); | 67 pp::Module::Get()->pp_module(), string, strlen(string)); |
61 } | 68 } |
62 | 69 |
63 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 70 void TestWebSocket::ReleaseVar(const PP_Var& var) { |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 std::string TestWebSocket::TestValidConnect() { | 209 std::string TestWebSocket::TestValidConnect() { |
203 int32_t result; | 210 int32_t result; |
204 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); | 211 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); |
205 ASSERT_TRUE(ws); | 212 ASSERT_TRUE(ws); |
206 ASSERT_EQ(PP_OK, result); | 213 ASSERT_EQ(PP_OK, result); |
207 core_interface_->ReleaseResource(ws); | 214 core_interface_->ReleaseResource(ws); |
208 | 215 |
209 PASS(); | 216 PASS(); |
210 } | 217 } |
211 | 218 |
| 219 std::string TestWebSocket::TestInvalidClose() { |
| 220 PP_Var reason = CreateVar("close for test"); |
| 221 TestCompletionCallback callback(instance_->pp_instance()); |
| 222 |
| 223 // Close before connect. |
| 224 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 225 int32_t result = websocket_interface_->Close( |
| 226 ws, kCloseCodeNormalClosure, reason, |
| 227 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 228 ASSERT_EQ(PP_ERROR_FAILED, result); |
| 229 core_interface_->ReleaseResource(ws); |
| 230 |
| 231 // Close with bad arguments. |
| 232 ws = Connect(kEchoServerURL, &result, NULL); |
| 233 ASSERT_TRUE(ws); |
| 234 ASSERT_EQ(PP_OK, result); |
| 235 result = websocket_interface_->Close(ws, 1, reason, |
| 236 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 237 ASSERT_EQ(PP_ERROR_NOACCESS, result); |
| 238 core_interface_->ReleaseResource(ws); |
| 239 |
| 240 ReleaseVar(reason); |
| 241 |
| 242 PASS(); |
| 243 } |
| 244 |
| 245 std::string TestWebSocket::TestValidClose() { |
| 246 PP_Var reason = CreateVar("close for test"); |
| 247 PP_Var url = CreateVar(kEchoServerURL); |
| 248 PP_Var protocols[] = { PP_MakeUndefined() }; |
| 249 TestCompletionCallback callback(instance_->pp_instance()); |
| 250 TestCompletionCallback another_callback(instance_->pp_instance()); |
| 251 |
| 252 // Close. |
| 253 int32_t result; |
| 254 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); |
| 255 ASSERT_TRUE(ws); |
| 256 ASSERT_EQ(PP_OK, result); |
| 257 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
| 258 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 259 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 260 result = callback.WaitForResult(); |
| 261 ASSERT_EQ(PP_OK, result); |
| 262 core_interface_->ReleaseResource(ws); |
| 263 |
| 264 // Close in connecting. |
| 265 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done |
| 266 // successfully. |
| 267 ws = websocket_interface_->Create(instance_->pp_instance()); |
| 268 result = websocket_interface_->Connect(ws, url, protocols, 0, |
| 269 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 270 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 271 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
| 272 static_cast<pp::CompletionCallback>( |
| 273 another_callback).pp_completion_callback()); |
| 274 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 275 result = callback.WaitForResult(); |
| 276 ASSERT_EQ(PP_ERROR_ABORTED, result); |
| 277 result = another_callback.WaitForResult(); |
| 278 ASSERT_EQ(PP_OK, result); |
| 279 core_interface_->ReleaseResource(ws); |
| 280 |
| 281 // Close in closing. |
| 282 // The first close will be done successfully, then the second one failed with |
| 283 // with PP_ERROR_INPROGRESS immediately. |
| 284 ws = Connect(kEchoServerURL, &result, NULL); |
| 285 ASSERT_TRUE(ws); |
| 286 ASSERT_EQ(PP_OK, result); |
| 287 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
| 288 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 289 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 290 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
| 291 static_cast<pp::CompletionCallback>( |
| 292 another_callback).pp_completion_callback()); |
| 293 ASSERT_EQ(PP_ERROR_INPROGRESS, result); |
| 294 result = callback.WaitForResult(); |
| 295 ASSERT_EQ(PP_OK, result); |
| 296 core_interface_->ReleaseResource(ws); |
| 297 |
| 298 // Close with ongoing receive message. |
| 299 ws = Connect(kEchoServerURL, &result, NULL); |
| 300 ASSERT_TRUE(ws); |
| 301 ASSERT_EQ(PP_OK, result); |
| 302 PP_Var receive_message_var; |
| 303 result = websocket_interface_->ReceiveMessage(ws, &receive_message_var, |
| 304 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
| 305 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 306 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
| 307 static_cast<pp::CompletionCallback>( |
| 308 another_callback).pp_completion_callback()); |
| 309 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
| 310 result = callback.WaitForResult(); |
| 311 ASSERT_EQ(PP_ERROR_ABORTED, result); |
| 312 result = another_callback.WaitForResult(); |
| 313 ASSERT_EQ(PP_OK, result); |
| 314 core_interface_->ReleaseResource(ws); |
| 315 |
| 316 ReleaseVar(reason); |
| 317 ReleaseVar(url); |
| 318 |
| 319 PASS(); |
| 320 } |
| 321 |
212 std::string TestWebSocket::TestGetProtocol() { | 322 std::string TestWebSocket::TestGetProtocol() { |
213 const char* expected_protocols[] = { | 323 const char* expected_protocols[] = { |
214 "x-chat", | 324 "x-chat", |
215 "hoehoe", | 325 "hoehoe", |
216 NULL | 326 NULL |
217 }; | 327 }; |
218 for (int i = 0; expected_protocols[i]; ++i) { | 328 for (int i = 0; expected_protocols[i]; ++i) { |
219 std::string url(kProtocolTestServerURL); | 329 std::string url(kProtocolTestServerURL); |
220 url += expected_protocols[i]; | 330 url += expected_protocols[i]; |
221 int32_t result; | 331 int32_t result; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 PASS(); | 373 PASS(); |
264 } | 374 } |
265 | 375 |
266 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 376 // TODO(toyoshim): Add tests for GetBufferedAmount(). |
267 // For now, the function doesn't work fine because update callback in WebKit is | 377 // For now, the function doesn't work fine because update callback in WebKit is |
268 // not landed yet. | 378 // not landed yet. |
269 | 379 |
270 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 380 // TODO(toyoshim): Add tests for didReceiveMessageError(). |
271 | 381 |
272 // TODO(toyoshim): Add other function tests. | 382 // TODO(toyoshim): Add other function tests. |
OLD | NEW |