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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 const char* const 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. | 33 // Connection close code is defined in WebSocket protocol specification. |
34 // The magic number 1000 means gracefull closure without any error. | 34 // The magic number 1000 means gracefull closure without any error. |
35 // See section 7.4.1. of RFC 6455. | 35 // See section 7.4.1. of RFC 6455. |
36 const uint16_t kCloseCodeNormalClosure = 1000; | 36 const uint16_t kCloseCodeNormalClosure = 1000U; |
37 | 37 |
38 REGISTER_TEST_CASE(WebSocket); | 38 REGISTER_TEST_CASE(WebSocket); |
39 | 39 |
40 bool TestWebSocket::Init() { | 40 bool TestWebSocket::Init() { |
41 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( | 41 websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>( |
42 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); | 42 pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE)); |
43 var_interface_ = static_cast<const PPB_Var*>( | 43 var_interface_ = static_cast<const PPB_Var*>( |
44 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 44 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
45 core_interface_ = static_cast<const PPB_Core*>( | 45 core_interface_ = static_cast<const PPB_Core*>( |
46 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 46 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); |
47 if (!websocket_interface_ || !var_interface_ || !core_interface_) | 47 if (!websocket_interface_ || !var_interface_ || !core_interface_) |
48 return false; | 48 return false; |
49 | 49 |
50 return true; | 50 return true; |
51 } | 51 } |
52 | 52 |
53 void TestWebSocket::RunTests(const std::string& filter) { | 53 void TestWebSocket::RunTests(const std::string& filter) { |
54 RUN_TEST(IsWebSocket, filter); | 54 RUN_TEST(IsWebSocket, filter); |
55 RUN_TEST(UninitializedPropertiesAccess, filter); | 55 RUN_TEST(UninitializedPropertiesAccess, filter); |
56 RUN_TEST(InvalidConnect, filter); | 56 RUN_TEST(InvalidConnect, filter); |
| 57 RUN_TEST(Protocols, filter); |
57 RUN_TEST(GetURL, filter); | 58 RUN_TEST(GetURL, filter); |
58 RUN_TEST(ValidConnect, filter); | 59 RUN_TEST(ValidConnect, filter); |
59 RUN_TEST(InvalidClose, filter); | 60 RUN_TEST(InvalidClose, filter); |
60 RUN_TEST(ValidClose, filter); | 61 RUN_TEST(ValidClose, filter); |
61 RUN_TEST(GetProtocol, filter); | 62 RUN_TEST(GetProtocol, filter); |
62 RUN_TEST(TextSendReceive, filter); | 63 RUN_TEST(TextSendReceive, filter); |
63 } | 64 } |
64 | 65 |
65 PP_Var TestWebSocket::CreateVar(const char* string) { | 66 PP_Var TestWebSocket::CreateVar(const char* string) { |
66 return var_interface_->VarFromUtf8( | 67 return var_interface_->VarFromUtf8( |
(...skipping 18 matching lines...) Expand all Loading... |
85 } | 86 } |
86 | 87 |
87 PP_Resource TestWebSocket::Connect( | 88 PP_Resource TestWebSocket::Connect( |
88 const char* url, int32_t* result, const char* protocol) { | 89 const char* url, int32_t* result, const char* protocol) { |
89 PP_Var protocols[] = { PP_MakeUndefined() }; | 90 PP_Var protocols[] = { PP_MakeUndefined() }; |
90 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 91 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
91 if (!ws) | 92 if (!ws) |
92 return 0; | 93 return 0; |
93 PP_Var url_var = CreateVar(url); | 94 PP_Var url_var = CreateVar(url); |
94 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 95 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
95 int protocol_count = 0; | 96 uint32_t protocol_count = 0U; |
96 if (protocol) { | 97 if (protocol) { |
97 protocols[0] = CreateVar(protocol); | 98 protocols[0] = CreateVar(protocol); |
98 protocol_count = 1; | 99 protocol_count = 1U; |
99 } | 100 } |
100 *result = websocket_interface_->Connect( | 101 *result = websocket_interface_->Connect( |
101 ws, url_var, protocols, protocol_count, | 102 ws, url_var, protocols, protocol_count, |
102 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 103 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
103 ReleaseVar(url_var); | 104 ReleaseVar(url_var); |
104 if (protocol) | 105 if (protocol) |
105 ReleaseVar(protocols[0]); | 106 ReleaseVar(protocols[0]); |
106 if (*result == PP_OK_COMPLETIONPENDING) | 107 if (*result == PP_OK_COMPLETIONPENDING) |
107 *result = callback.WaitForResult(); | 108 *result = callback.WaitForResult(); |
108 return ws; | 109 return ws; |
(...skipping 15 matching lines...) Expand all Loading... |
124 core_interface_->ReleaseResource(ws); | 125 core_interface_->ReleaseResource(ws); |
125 | 126 |
126 PASS(); | 127 PASS(); |
127 } | 128 } |
128 | 129 |
129 std::string TestWebSocket::TestUninitializedPropertiesAccess() { | 130 std::string TestWebSocket::TestUninitializedPropertiesAccess() { |
130 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 131 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
131 ASSERT_TRUE(ws); | 132 ASSERT_TRUE(ws); |
132 | 133 |
133 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws); | 134 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws); |
134 ASSERT_EQ(0, bufferedAmount); | 135 ASSERT_EQ(0U, bufferedAmount); |
135 | 136 |
136 uint16_t close_code = websocket_interface_->GetCloseCode(ws); | 137 uint16_t close_code = websocket_interface_->GetCloseCode(ws); |
137 ASSERT_EQ(0, close_code); | 138 ASSERT_EQ(0U, close_code); |
138 | 139 |
139 PP_Var close_reason = websocket_interface_->GetCloseReason(ws); | 140 PP_Var close_reason = websocket_interface_->GetCloseReason(ws); |
140 ASSERT_TRUE(AreEqual(close_reason, "")); | 141 ASSERT_TRUE(AreEqual(close_reason, "")); |
141 | 142 |
142 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws); | 143 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws); |
143 ASSERT_EQ(PP_FALSE, close_was_clean); | 144 ASSERT_EQ(PP_FALSE, close_was_clean); |
144 | 145 |
145 PP_Var extensions = websocket_interface_->GetExtensions(ws); | 146 PP_Var extensions = websocket_interface_->GetExtensions(ws); |
146 ASSERT_TRUE(AreEqual(extensions, "")); | 147 ASSERT_TRUE(AreEqual(extensions, "")); |
147 | 148 |
(...skipping 11 matching lines...) Expand all Loading... |
159 } | 160 } |
160 | 161 |
161 std::string TestWebSocket::TestInvalidConnect() { | 162 std::string TestWebSocket::TestInvalidConnect() { |
162 PP_Var protocols[] = { PP_MakeUndefined() }; | 163 PP_Var protocols[] = { PP_MakeUndefined() }; |
163 | 164 |
164 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); | 165 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
165 ASSERT_TRUE(ws); | 166 ASSERT_TRUE(ws); |
166 | 167 |
167 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 168 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
168 int32_t result = websocket_interface_->Connect( | 169 int32_t result = websocket_interface_->Connect( |
169 ws, PP_MakeUndefined(), protocols, 1, | 170 ws, PP_MakeUndefined(), protocols, 1U, |
170 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 171 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
171 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 172 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
172 | 173 |
173 result = websocket_interface_->Connect( | 174 result = websocket_interface_->Connect( |
174 ws, PP_MakeUndefined(), protocols, 1, | 175 ws, PP_MakeUndefined(), protocols, 1U, |
175 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 176 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
176 ASSERT_EQ(PP_ERROR_INPROGRESS, result); | 177 ASSERT_EQ(PP_ERROR_INPROGRESS, result); |
177 | 178 |
178 core_interface_->ReleaseResource(ws); | 179 core_interface_->ReleaseResource(ws); |
179 | 180 |
180 for (int i = 0; kInvalidURLs[i]; ++i) { | 181 for (int i = 0; kInvalidURLs[i]; ++i) { |
181 ws = Connect(kInvalidURLs[i], &result, NULL); | 182 ws = Connect(kInvalidURLs[i], &result, NULL); |
182 ASSERT_TRUE(ws); | 183 ASSERT_TRUE(ws); |
183 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 184 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
184 | 185 |
185 core_interface_->ReleaseResource(ws); | 186 core_interface_->ReleaseResource(ws); |
186 } | 187 } |
187 | 188 |
188 // TODO(toyoshim): Add invalid protocols tests | 189 PASS(); |
| 190 } |
| 191 |
| 192 std::string TestWebSocket::TestProtocols() { |
| 193 PP_Var url = CreateVar(kEchoServerURL); |
| 194 PP_Var bad_protocols[] = { CreateVar("x-test"), CreateVar("x-test") }; |
| 195 PP_Var good_protocols[] = { CreateVar("x-test"), CreateVar("x-yatest") }; |
| 196 |
| 197 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); |
| 198 ASSERT_TRUE(ws); |
| 199 int32_t result = websocket_interface_->Connect( |
| 200 ws, url, bad_protocols, 2U, PP_BlockUntilComplete()); |
| 201 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 202 core_interface_->ReleaseResource(ws); |
| 203 |
| 204 ws = websocket_interface_->Create(instance_->pp_instance()); |
| 205 ASSERT_TRUE(ws); |
| 206 result = websocket_interface_->Connect( |
| 207 ws, url, good_protocols, 2U, PP_BlockUntilComplete()); |
| 208 ASSERT_EQ(PP_ERROR_BLOCKS_MAIN_THREAD, result); |
| 209 core_interface_->ReleaseResource(ws); |
| 210 |
| 211 ReleaseVar(url); |
| 212 for (int i = 0; i < 2; ++i) { |
| 213 ReleaseVar(bad_protocols[i]); |
| 214 ReleaseVar(good_protocols[i]); |
| 215 } |
| 216 core_interface_->ReleaseResource(ws); |
189 | 217 |
190 PASS(); | 218 PASS(); |
191 } | 219 } |
192 | 220 |
193 std::string TestWebSocket::TestGetURL() { | 221 std::string TestWebSocket::TestGetURL() { |
194 for (int i = 0; kInvalidURLs[i]; ++i) { | 222 for (int i = 0; kInvalidURLs[i]; ++i) { |
195 int32_t result; | 223 int32_t result; |
196 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL); | 224 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL); |
197 ASSERT_TRUE(ws); | 225 ASSERT_TRUE(ws); |
198 PP_Var url = websocket_interface_->GetURL(ws); | 226 PP_Var url = websocket_interface_->GetURL(ws); |
(...skipping 26 matching lines...) Expand all Loading... |
225 int32_t result = websocket_interface_->Close( | 253 int32_t result = websocket_interface_->Close( |
226 ws, kCloseCodeNormalClosure, reason, | 254 ws, kCloseCodeNormalClosure, reason, |
227 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 255 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
228 ASSERT_EQ(PP_ERROR_FAILED, result); | 256 ASSERT_EQ(PP_ERROR_FAILED, result); |
229 core_interface_->ReleaseResource(ws); | 257 core_interface_->ReleaseResource(ws); |
230 | 258 |
231 // Close with bad arguments. | 259 // Close with bad arguments. |
232 ws = Connect(kEchoServerURL, &result, NULL); | 260 ws = Connect(kEchoServerURL, &result, NULL); |
233 ASSERT_TRUE(ws); | 261 ASSERT_TRUE(ws); |
234 ASSERT_EQ(PP_OK, result); | 262 ASSERT_EQ(PP_OK, result); |
235 result = websocket_interface_->Close(ws, 1, reason, | 263 result = websocket_interface_->Close(ws, 1U, reason, |
236 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 264 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
237 ASSERT_EQ(PP_ERROR_NOACCESS, result); | 265 ASSERT_EQ(PP_ERROR_NOACCESS, result); |
238 core_interface_->ReleaseResource(ws); | 266 core_interface_->ReleaseResource(ws); |
239 | 267 |
240 ReleaseVar(reason); | 268 ReleaseVar(reason); |
241 | 269 |
242 PASS(); | 270 PASS(); |
243 } | 271 } |
244 | 272 |
245 std::string TestWebSocket::TestValidClose() { | 273 std::string TestWebSocket::TestValidClose() { |
(...skipping 12 matching lines...) Expand all Loading... |
258 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 286 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
259 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | 287 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
260 result = callback.WaitForResult(); | 288 result = callback.WaitForResult(); |
261 ASSERT_EQ(PP_OK, result); | 289 ASSERT_EQ(PP_OK, result); |
262 core_interface_->ReleaseResource(ws); | 290 core_interface_->ReleaseResource(ws); |
263 | 291 |
264 // Close in connecting. | 292 // Close in connecting. |
265 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done | 293 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done |
266 // successfully. | 294 // successfully. |
267 ws = websocket_interface_->Create(instance_->pp_instance()); | 295 ws = websocket_interface_->Create(instance_->pp_instance()); |
268 result = websocket_interface_->Connect(ws, url, protocols, 0, | 296 result = websocket_interface_->Connect(ws, url, protocols, 0U, |
269 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); | 297 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); |
270 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | 298 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
271 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, | 299 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, |
272 static_cast<pp::CompletionCallback>( | 300 static_cast<pp::CompletionCallback>( |
273 another_callback).pp_completion_callback()); | 301 another_callback).pp_completion_callback()); |
274 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); | 302 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); |
275 result = callback.WaitForResult(); | 303 result = callback.WaitForResult(); |
276 ASSERT_EQ(PP_ERROR_ABORTED, result); | 304 ASSERT_EQ(PP_ERROR_ABORTED, result); |
277 result = another_callback.WaitForResult(); | 305 result = another_callback.WaitForResult(); |
278 ASSERT_EQ(PP_OK, result); | 306 ASSERT_EQ(PP_OK, result); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 PASS(); | 401 PASS(); |
374 } | 402 } |
375 | 403 |
376 // TODO(toyoshim): Add tests for GetBufferedAmount(). | 404 // TODO(toyoshim): Add tests for GetBufferedAmount(). |
377 // For now, the function doesn't work fine because update callback in WebKit is | 405 // For now, the function doesn't work fine because update callback in WebKit is |
378 // not landed yet. | 406 // not landed yet. |
379 | 407 |
380 // TODO(toyoshim): Add tests for didReceiveMessageError(). | 408 // TODO(toyoshim): Add tests for didReceiveMessageError(). |
381 | 409 |
382 // TODO(toyoshim): Add other function tests. | 410 // TODO(toyoshim): Add other function tests. |
OLD | NEW |