Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: ppapi/tests/test_websocket.cc

Issue 8839003: WebSocket Pepper API: validate redundant protocols in Connect() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase for dcommit Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/tests/test_websocket.h ('k') | webkit/plugins/ppapi/ppb_websocket_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(string, strlen(string)); 67 return var_interface_->VarFromUtf8(string, strlen(string));
(...skipping 17 matching lines...) Expand all
84 } 85 }
85 86
86 PP_Resource TestWebSocket::Connect( 87 PP_Resource TestWebSocket::Connect(
87 const char* url, int32_t* result, const char* protocol) { 88 const char* url, int32_t* result, const char* protocol) {
88 PP_Var protocols[] = { PP_MakeUndefined() }; 89 PP_Var protocols[] = { PP_MakeUndefined() };
89 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 90 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
90 if (!ws) 91 if (!ws)
91 return 0; 92 return 0;
92 PP_Var url_var = CreateVar(url); 93 PP_Var url_var = CreateVar(url);
93 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 94 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
94 int protocol_count = 0; 95 uint32_t protocol_count = 0U;
95 if (protocol) { 96 if (protocol) {
96 protocols[0] = CreateVar(protocol); 97 protocols[0] = CreateVar(protocol);
97 protocol_count = 1; 98 protocol_count = 1U;
98 } 99 }
99 *result = websocket_interface_->Connect( 100 *result = websocket_interface_->Connect(
100 ws, url_var, protocols, protocol_count, 101 ws, url_var, protocols, protocol_count,
101 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 102 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
102 ReleaseVar(url_var); 103 ReleaseVar(url_var);
103 if (protocol) 104 if (protocol)
104 ReleaseVar(protocols[0]); 105 ReleaseVar(protocols[0]);
105 if (*result == PP_OK_COMPLETIONPENDING) 106 if (*result == PP_OK_COMPLETIONPENDING)
106 *result = callback.WaitForResult(); 107 *result = callback.WaitForResult();
107 return ws; 108 return ws;
(...skipping 15 matching lines...) Expand all
123 core_interface_->ReleaseResource(ws); 124 core_interface_->ReleaseResource(ws);
124 125
125 PASS(); 126 PASS();
126 } 127 }
127 128
128 std::string TestWebSocket::TestUninitializedPropertiesAccess() { 129 std::string TestWebSocket::TestUninitializedPropertiesAccess() {
129 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 130 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
130 ASSERT_TRUE(ws); 131 ASSERT_TRUE(ws);
131 132
132 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws); 133 uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws);
133 ASSERT_EQ(0, bufferedAmount); 134 ASSERT_EQ(0U, bufferedAmount);
134 135
135 uint16_t close_code = websocket_interface_->GetCloseCode(ws); 136 uint16_t close_code = websocket_interface_->GetCloseCode(ws);
136 ASSERT_EQ(0, close_code); 137 ASSERT_EQ(0U, close_code);
137 138
138 PP_Var close_reason = websocket_interface_->GetCloseReason(ws); 139 PP_Var close_reason = websocket_interface_->GetCloseReason(ws);
139 ASSERT_TRUE(AreEqual(close_reason, "")); 140 ASSERT_TRUE(AreEqual(close_reason, ""));
140 141
141 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws); 142 PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws);
142 ASSERT_EQ(PP_FALSE, close_was_clean); 143 ASSERT_EQ(PP_FALSE, close_was_clean);
143 144
144 PP_Var extensions = websocket_interface_->GetExtensions(ws); 145 PP_Var extensions = websocket_interface_->GetExtensions(ws);
145 ASSERT_TRUE(AreEqual(extensions, "")); 146 ASSERT_TRUE(AreEqual(extensions, ""));
146 147
(...skipping 11 matching lines...) Expand all
158 } 159 }
159 160
160 std::string TestWebSocket::TestInvalidConnect() { 161 std::string TestWebSocket::TestInvalidConnect() {
161 PP_Var protocols[] = { PP_MakeUndefined() }; 162 PP_Var protocols[] = { PP_MakeUndefined() };
162 163
163 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance()); 164 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
164 ASSERT_TRUE(ws); 165 ASSERT_TRUE(ws);
165 166
166 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 167 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
167 int32_t result = websocket_interface_->Connect( 168 int32_t result = websocket_interface_->Connect(
168 ws, PP_MakeUndefined(), protocols, 1, 169 ws, PP_MakeUndefined(), protocols, 1U,
169 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 170 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
170 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); 171 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
171 172
172 result = websocket_interface_->Connect( 173 result = websocket_interface_->Connect(
173 ws, PP_MakeUndefined(), protocols, 1, 174 ws, PP_MakeUndefined(), protocols, 1U,
174 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 175 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
175 ASSERT_EQ(PP_ERROR_INPROGRESS, result); 176 ASSERT_EQ(PP_ERROR_INPROGRESS, result);
176 177
177 core_interface_->ReleaseResource(ws); 178 core_interface_->ReleaseResource(ws);
178 179
179 for (int i = 0; kInvalidURLs[i]; ++i) { 180 for (int i = 0; kInvalidURLs[i]; ++i) {
180 ws = Connect(kInvalidURLs[i], &result, NULL); 181 ws = Connect(kInvalidURLs[i], &result, NULL);
181 ASSERT_TRUE(ws); 182 ASSERT_TRUE(ws);
182 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); 183 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
183 184
184 core_interface_->ReleaseResource(ws); 185 core_interface_->ReleaseResource(ws);
185 } 186 }
186 187
187 // TODO(toyoshim): Add invalid protocols tests 188 PASS();
189 }
190
191 std::string TestWebSocket::TestProtocols() {
192 PP_Var url = CreateVar(kEchoServerURL);
193 PP_Var bad_protocols[] = { CreateVar("x-test"), CreateVar("x-test") };
194 PP_Var good_protocols[] = { CreateVar("x-test"), CreateVar("x-yatest") };
195
196 PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
197 ASSERT_TRUE(ws);
198 int32_t result = websocket_interface_->Connect(
199 ws, url, bad_protocols, 2U, PP_BlockUntilComplete());
200 ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
201 core_interface_->ReleaseResource(ws);
202
203 ws = websocket_interface_->Create(instance_->pp_instance());
204 ASSERT_TRUE(ws);
205 result = websocket_interface_->Connect(
206 ws, url, good_protocols, 2U, PP_BlockUntilComplete());
207 ASSERT_EQ(PP_ERROR_BLOCKS_MAIN_THREAD, result);
208 core_interface_->ReleaseResource(ws);
209
210 ReleaseVar(url);
211 for (int i = 0; i < 2; ++i) {
212 ReleaseVar(bad_protocols[i]);
213 ReleaseVar(good_protocols[i]);
214 }
215 core_interface_->ReleaseResource(ws);
188 216
189 PASS(); 217 PASS();
190 } 218 }
191 219
192 std::string TestWebSocket::TestGetURL() { 220 std::string TestWebSocket::TestGetURL() {
193 for (int i = 0; kInvalidURLs[i]; ++i) { 221 for (int i = 0; kInvalidURLs[i]; ++i) {
194 int32_t result; 222 int32_t result;
195 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL); 223 PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL);
196 ASSERT_TRUE(ws); 224 ASSERT_TRUE(ws);
197 PP_Var url = websocket_interface_->GetURL(ws); 225 PP_Var url = websocket_interface_->GetURL(ws);
(...skipping 26 matching lines...) Expand all
224 int32_t result = websocket_interface_->Close( 252 int32_t result = websocket_interface_->Close(
225 ws, kCloseCodeNormalClosure, reason, 253 ws, kCloseCodeNormalClosure, reason,
226 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 254 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
227 ASSERT_EQ(PP_ERROR_FAILED, result); 255 ASSERT_EQ(PP_ERROR_FAILED, result);
228 core_interface_->ReleaseResource(ws); 256 core_interface_->ReleaseResource(ws);
229 257
230 // Close with bad arguments. 258 // Close with bad arguments.
231 ws = Connect(kEchoServerURL, &result, NULL); 259 ws = Connect(kEchoServerURL, &result, NULL);
232 ASSERT_TRUE(ws); 260 ASSERT_TRUE(ws);
233 ASSERT_EQ(PP_OK, result); 261 ASSERT_EQ(PP_OK, result);
234 result = websocket_interface_->Close(ws, 1, reason, 262 result = websocket_interface_->Close(ws, 1U, reason,
235 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 263 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
236 ASSERT_EQ(PP_ERROR_NOACCESS, result); 264 ASSERT_EQ(PP_ERROR_NOACCESS, result);
237 core_interface_->ReleaseResource(ws); 265 core_interface_->ReleaseResource(ws);
238 266
239 ReleaseVar(reason); 267 ReleaseVar(reason);
240 268
241 PASS(); 269 PASS();
242 } 270 }
243 271
244 std::string TestWebSocket::TestValidClose() { 272 std::string TestWebSocket::TestValidClose() {
(...skipping 12 matching lines...) Expand all
257 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 285 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
258 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); 286 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
259 result = callback.WaitForResult(); 287 result = callback.WaitForResult();
260 ASSERT_EQ(PP_OK, result); 288 ASSERT_EQ(PP_OK, result);
261 core_interface_->ReleaseResource(ws); 289 core_interface_->ReleaseResource(ws);
262 290
263 // Close in connecting. 291 // Close in connecting.
264 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done 292 // The ongoing connect failed with PP_ERROR_ABORTED, then the close is done
265 // successfully. 293 // successfully.
266 ws = websocket_interface_->Create(instance_->pp_instance()); 294 ws = websocket_interface_->Create(instance_->pp_instance());
267 result = websocket_interface_->Connect(ws, url, protocols, 0, 295 result = websocket_interface_->Connect(ws, url, protocols, 0U,
268 static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); 296 static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
269 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); 297 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
270 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason, 298 result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
271 static_cast<pp::CompletionCallback>( 299 static_cast<pp::CompletionCallback>(
272 another_callback).pp_completion_callback()); 300 another_callback).pp_completion_callback());
273 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result); 301 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
274 result = callback.WaitForResult(); 302 result = callback.WaitForResult();
275 ASSERT_EQ(PP_ERROR_ABORTED, result); 303 ASSERT_EQ(PP_ERROR_ABORTED, result);
276 result = another_callback.WaitForResult(); 304 result = another_callback.WaitForResult();
277 ASSERT_EQ(PP_OK, result); 305 ASSERT_EQ(PP_OK, result);
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 PASS(); 400 PASS();
373 } 401 }
374 402
375 // TODO(toyoshim): Add tests for GetBufferedAmount(). 403 // TODO(toyoshim): Add tests for GetBufferedAmount().
376 // For now, the function doesn't work fine because update callback in WebKit is 404 // For now, the function doesn't work fine because update callback in WebKit is
377 // not landed yet. 405 // not landed yet.
378 406
379 // TODO(toyoshim): Add tests for didReceiveMessageError(). 407 // TODO(toyoshim): Add tests for didReceiveMessageError().
380 408
381 // TODO(toyoshim): Add other function tests. 409 // TODO(toyoshim): Add other function tests.
OLDNEW
« no previous file with comments | « ppapi/tests/test_websocket.h ('k') | webkit/plugins/ppapi/ppb_websocket_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698