 Chromium Code Reviews
 Chromium Code Reviews Issue 8821008:
  WebSocket Pepper API: Add unit test to call Close()  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 8821008:
  WebSocket Pepper API: Add unit test to call Close()  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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[] = | 
| 
Takashi Toyoshima
2011/12/06 08:26:48
Constant variables are not exported out of file by
 | |
| 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* 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 const uint16_t kCloseCodeNormalClosure = 1000; | |
| 
Takashi Toyoshima
2011/12/06 08:26:48
This value is defined in WebSocket protocol spec.
 
dmichael (off chromium)
2011/12/06 17:59:01
Can you add a comment with a reference to it?
 
Takashi Toyoshima
2011/12/06 18:27:26
Done.
 | |
| 34 | |
| 33 REGISTER_TEST_CASE(WebSocket); | 35 REGISTER_TEST_CASE(WebSocket); | 
| 34 | 36 | 
| 35 bool TestWebSocket::Init() { | 37 bool TestWebSocket::Init() { | 
| 36 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 38 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 
| 37 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 39 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 
| 38 var_interface_ = static_cast<const PPB_Var*>( | 40 var_interface_ = static_cast<const PPB_Var*>( | 
| 39 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 41 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 
| 40 core_interface_ = static_cast<const PPB_Core*>( | 42 core_interface_ = static_cast<const PPB_Core*>( | 
| 41 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 43 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 
| 42 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 44 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 
| 43 return false; | 45 return false; | 
| 44 | 46 | 
| 45 return true; | 47 return true; | 
| 46 } | 48 } | 
| 47 | 49 | 
| 48 void TestWebSocket::RunTests(const std::string& filter) { | 50 void TestWebSocket::RunTests(const std::string& filter) { | 
| 49 RUN_TEST(IsWebSocket, filter); | 51 RUN_TEST(IsWebSocket, filter); | 
| 50 RUN_TEST(InvalidConnect, filter); | 52 RUN_TEST(InvalidConnect, filter); | 
| 53 RUN_TEST(InvalidClose, filter); | |
| 51 RUN_TEST(GetURL, filter); | 54 RUN_TEST(GetURL, filter); | 
| 52 RUN_TEST(ValidConnect, filter); | 55 RUN_TEST(ValidConnect, filter); | 
| 56 RUN_TEST(ValidClose, filter); | |
| 53 RUN_TEST(GetProtocol, filter); | 57 RUN_TEST(GetProtocol, filter); | 
| 54 RUN_TEST(TextSendReceive, filter); | 58 RUN_TEST(TextSendReceive, filter); | 
| 55 } | 59 } | 
| 56 | 60 | 
| 57 PP_Var TestWebSocket::CreateVar(const char* string) { | 61 PP_Var TestWebSocket::CreateVar(const char* string) { | 
| 58 return var_interface_->VarFromUtf8( | 62 return var_interface_->VarFromUtf8( | 
| 59 pp::Module::Get()->pp_module(), string, strlen(string)); | 63 pp::Module::Get()->pp_module(), string, strlen(string)); | 
| 60 } | 64 } | 
| 61 | 65 | 
| 62 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 66 void TestWebSocket::ReleaseVar(const PP_Var& var) { | 
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 149 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 
| 146 | 150 | 
| 147 core_interface_->ReleaseResource(ws); | 151 core_interface_->ReleaseResource(ws); | 
| 148 } | 152 } | 
| 149 | 153 | 
| 150 // TODO(toyoshim): Add invalid protocols tests | 154 // TODO(toyoshim): Add invalid protocols tests | 
| 151 | 155 | 
| 152 PASS(); | 156 PASS(); | 
| 153 } | 157 } | 
| 154 | 158 | 
| 159 std::string TestWebSocket::TestInvalidClose() { | |
| 160 PP_Var reason = CreateVar("close for test"); | |
| 161 TestCompletionCallback callback(instance_->pp_instance()); | |
| 162 | |
| 163 // Close before connect. | |
| 164 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 165 int32_t result = websocket_interface_->Close( | |
| 166 ws, kCloseCodeNormalClosure, reason, | |
| 167 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 168 ASSERT_EQ(PP_ERROR_FAILED, result); | |
| 169 core_interface_->ReleaseResource(ws); | |
| 170 | |
| 171 // Close with bad arguments. | |
| 172 ws = Connect(kEchoServerURL, &result, NULL); | |
| 173 ASSERT_TRUE(ws); | |
| 174 ASSERT_EQ(PP_OK, result); | |
| 175 result = websocket_interface_->Close(ws, 1, reason, | |
| 176 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 177 ASSERT_EQ(PP_ERROR_NOACCESS, result); | |
| 178 core_interface_->ReleaseResource(ws); | |
| 179 | |
| 180 ReleaseVar(reason); | |
| 181 | |
| 182 PASS(); | |
| 183 } | |
| 184 | |
| 155 std::string TestWebSocket::TestGetURL() { | 185 std::string TestWebSocket::TestGetURL() { | 
| 156 for (int i = 0; kInvalidURLs[i]; ++i) { | 186 for (int i = 0; kInvalidURLs[i]; ++i) { | 
| 157 int32_t result; | 187 int32_t result; | 
| 158 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL); | 188 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL); | 
| 159 ASSERT_TRUE(ws); | 189 ASSERT_TRUE(ws); | 
| 160 PP_Var url = websocket_interface_->GetURL(ws); | 190 PP_Var url = websocket_interface_->GetURL(ws); | 
| 161 ASSERT_TRUE(AreEqual(url, kInvalidURLs[i])); | 191 ASSERT_TRUE(AreEqual(url, kInvalidURLs[i])); | 
| 162 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 192 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 
| 163 | 193 | 
| 164 ReleaseVar(url); | 194 ReleaseVar(url); | 
| 165 core_interface_->ReleaseResource(ws); | 195 core_interface_->ReleaseResource(ws); | 
| 166 } | 196 } | 
| 167 | 197 | 
| 168 PASS(); | 198 PASS(); | 
| 169 } | 199 } | 
| 170 | 200 | 
| 171 std::string TestWebSocket::TestValidConnect() { | 201 std::string TestWebSocket::TestValidConnect() { | 
| 172 int32_t result; | 202 int32_t result; | 
| 173 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); | 203 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); | 
| 174 ASSERT_TRUE(ws); | 204 ASSERT_TRUE(ws); | 
| 175 ASSERT_EQ(PP_OK, result); | 205 ASSERT_EQ(PP_OK, result); | 
| 176 core_interface_->ReleaseResource(ws); | 206 core_interface_->ReleaseResource(ws); | 
| 177 | 207 | 
| 178 PASS(); | 208 PASS(); | 
| 179 } | 209 } | 
| 180 | 210 | 
| 211 std::string TestWebSocket::TestValidClose() { | |
| 212 PP_Var reason = CreateVar("close for test"); | |
| 213 PP_Var url = CreateVar(kEchoServerURL); | |
| 214 PP_Var protocols[] = { PP_MakeUndefined() }; | |
| 215 TestCompletionCallback callback(instance_->pp_instance()); | |
| 216 TestCompletionCallback another_callback(instance_->pp_instance()); | |
| 217 | |
| 218 // Close. | |
| 219 int32_t result; | |
| 220 PP_Resource ws = Connect(kEchoServerURL, &result, NULL); | |
| 221 ASSERT_TRUE(ws); | |
| 222 ASSERT_EQ(PP_OK, result); | |
| 223 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | |
| 224 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 225 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 226 result = callback.WaitForResult(); | |
| 227 core_interface_->ReleaseResource(ws); | |
| 228 | |
| 229 // Close in connecting. | |
| 230 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done | |
| 231 // successfully. | |
| 232 ws = websocket_interface_->Create(instance_->pp_instance()); | |
| 233 result = websocket_interface_->Connect(ws, url, protocols, 0, | |
| 234 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 235 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 236 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | |
| 237 static_cast<pp::CompletionCallback>( | |
| 238 another_callback).pp_completion_callback()); | |
| 239 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 240 result = callback.WaitForResult(); | |
| 241 ASSERT_EQ(PP_ERROR_ABORTED, result); | |
| 242 result = another_callback.WaitForResult(); | |
| 243 ASSERT_EQ(PP_OK, result); | |
| 244 core_interface_->ReleaseResource(ws); | |
| 245 | |
| 246 // Close in closing. | |
| 247 // The first close will be done successfully, then the second one failed with | |
| 248 // with PP_ERROR_INPROGRESS immediately. | |
| 249 ws = Connect(kEchoServerURL, &result, NULL); | |
| 250 ASSERT_TRUE(ws); | |
| 251 ASSERT_EQ(PP_OK, result); | |
| 252 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | |
| 253 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | |
| 254 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | |
| 255 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | |
| 256 static_cast<pp::CompletionCallback>( | |
| 257 another_callback).pp_completion_callback()); | |
| 258 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | |
| 259 result = callback.WaitForResult(); | |
| 260 ASSERT_EQ(PP_OK, result); | |
| 261 core_interface_->ReleaseResource(ws); | |
| 262 | |
| 263 // Close with ongoing receive message. | |
| 264 ws = Connect(kEchoServerURL, &result, NULL); | |
| 265 ASSERT_TRUE(ws); | |
| 266 ASSERT_EQ(PP_OK, result); | |
| 267 PP_Var receive_message_var; | |
| 268 result = websocket_interface_->ReceiveMessage(ws, &receive_message_var, | |
| 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(); | |
| 
Takashi Toyoshima
2011/12/06 08:26:48
This |WaitForResult| found the bug.
 | |
| 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 PASS(); | |
| 282 } | |
| 283 | |
| 181 std::string TestWebSocket::TestGetProtocol() { | 284 std::string TestWebSocket::TestGetProtocol() { | 
| 182 const char* expected_protocols[] = { | 285 const char* expected_protocols[] = { | 
| 183 "x-chat", | 286 "x-chat", | 
| 184 "hoehoe", | 287 "hoehoe", | 
| 185 NULL | 288 NULL | 
| 186 }; | 289 }; | 
| 187 for (int i = 0; expected_protocols[i]; ++i) { | 290 for (int i = 0; expected_protocols[i]; ++i) { | 
| 188 std::string url(kProtocolTestServerURL); | 291 std::string url(kProtocolTestServerURL); | 
| 189 url += expected_protocols[i]; | 292 url += expected_protocols[i]; | 
| 190 int32_t result; | 293 int32_t result; | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 PASS(); | 335 PASS(); | 
| 233 } | 336 } | 
| 234 | 337 | 
| 235 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 338 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 
| 236 // For now, the function doesn't work fine because update callback in WebKit is | 339 // For now, the function doesn't work fine because update callback in WebKit is | 
| 237 // not landed yet. | 340 // not landed yet. | 
| 238 | 341 | 
| 239 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 342 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 
| 240 | 343 | 
| 241 // TODO(toyoshim): Add other function tests. | 344 // TODO(toyoshim): Add other function tests. | 
| OLD | NEW |