1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/tests/test_websocket.h"
#include <string.h>
#include "ppapi/c/dev/ppb_websocket_dev.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/ppb_core.h"
#include "ppapi/c/ppb_var.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
const char kEchoServerURL[] =
"ws://localhost:8880/websocket/tests/hybi/echo";
const char kProtocolTestServerURL[] =
"ws://localhost:8880/websocket/tests/hybi/protocol-test?protocol=";
const char* const kInvalidURLs[] = {
"http://www.google.com/invalid_scheme",
"ws://www.google.com/invalid#fragment",
"ws://www.google.com:65535/invalid_port",
NULL
};
// Connection close code is defined in WebSocket protocol specification.
// The magic number 1000 means gracefull closure without any error.
// See section 7.4.1. of RFC 6455.
const uint16_t kCloseCodeNormalClosure = 1000;
REGISTER_TEST_CASE(WebSocket);
bool TestWebSocket::Init() {
websocket_interface_ = static_cast<const PPB_WebSocket_Dev*>(
pp::Module::Get()->GetBrowserInterface(PPB_WEBSOCKET_DEV_INTERFACE));
var_interface_ = static_cast<const PPB_Var*>(
pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
core_interface_ = static_cast<const PPB_Core*>(
pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
if (!websocket_interface_ || !var_interface_ || !core_interface_)
return false;
return true;
}
void TestWebSocket::RunTests(const std::string& filter) {
RUN_TEST(IsWebSocket, filter);
RUN_TEST(UninitializedPropertiesAccess, filter);
RUN_TEST(InvalidConnect, filter);
RUN_TEST(GetURL, filter);
RUN_TEST(ValidConnect, filter);
RUN_TEST(InvalidClose, filter);
RUN_TEST(ValidClose, filter);
RUN_TEST(GetProtocol, filter);
RUN_TEST(TextSendReceive, filter);
}
PP_Var TestWebSocket::CreateVar(const char* string) {
return var_interface_->VarFromUtf8(string, strlen(string));
}
void TestWebSocket::ReleaseVar(const PP_Var& var) {
var_interface_->Release(var);
}
bool TestWebSocket::AreEqual(const PP_Var& var, const char* string) {
if (var.type != PP_VARTYPE_STRING)
return false;
uint32_t utf8_length;
const char* utf8 = var_interface_->VarToUtf8(var, &utf8_length);
uint32_t string_length = strlen(string);
if (utf8_length != string_length)
return false;
if (strncmp(utf8, string, utf8_length))
return false;
return true;
}
PP_Resource TestWebSocket::Connect(
const char* url, int32_t* result, const char* protocol) {
PP_Var protocols[] = { PP_MakeUndefined() };
PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
if (!ws)
return 0;
PP_Var url_var = CreateVar(url);
TestCompletionCallback callback(instance_->pp_instance(), force_async_);
int protocol_count = 0;
if (protocol) {
protocols[0] = CreateVar(protocol);
protocol_count = 1;
}
*result = websocket_interface_->Connect(
ws, url_var, protocols, protocol_count,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ReleaseVar(url_var);
if (protocol)
ReleaseVar(protocols[0]);
if (*result == PP_OK_COMPLETIONPENDING)
*result = callback.WaitForResult();
return ws;
}
std::string TestWebSocket::TestIsWebSocket() {
// Test that a NULL resource isn't a websocket.
pp::Resource null_resource;
PP_Bool result =
websocket_interface_->IsWebSocket(null_resource.pp_resource());
ASSERT_FALSE(result);
PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
ASSERT_TRUE(ws);
result = websocket_interface_->IsWebSocket(ws);
ASSERT_TRUE(result);
core_interface_->ReleaseResource(ws);
PASS();
}
std::string TestWebSocket::TestUninitializedPropertiesAccess() {
PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
ASSERT_TRUE(ws);
uint64_t bufferedAmount = websocket_interface_->GetBufferedAmount(ws);
ASSERT_EQ(0, bufferedAmount);
uint16_t close_code = websocket_interface_->GetCloseCode(ws);
ASSERT_EQ(0, close_code);
PP_Var close_reason = websocket_interface_->GetCloseReason(ws);
ASSERT_TRUE(AreEqual(close_reason, ""));
PP_Bool close_was_clean = websocket_interface_->GetCloseWasClean(ws);
ASSERT_EQ(PP_FALSE, close_was_clean);
PP_Var extensions = websocket_interface_->GetExtensions(ws);
ASSERT_TRUE(AreEqual(extensions, ""));
PP_Var protocol = websocket_interface_->GetProtocol(ws);
ASSERT_TRUE(AreEqual(protocol, ""));
PP_WebSocketReadyState_Dev ready_state =
websocket_interface_->GetReadyState(ws);
ASSERT_EQ(PP_WEBSOCKETREADYSTATE_INVALID_DEV, ready_state);
PP_Var url = websocket_interface_->GetURL(ws);
ASSERT_TRUE(AreEqual(url, ""));
PASS();
}
std::string TestWebSocket::TestInvalidConnect() {
PP_Var protocols[] = { PP_MakeUndefined() };
PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
ASSERT_TRUE(ws);
TestCompletionCallback callback(instance_->pp_instance(), force_async_);
int32_t result = websocket_interface_->Connect(
ws, PP_MakeUndefined(), protocols, 1,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
result = websocket_interface_->Connect(
ws, PP_MakeUndefined(), protocols, 1,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_ERROR_INPROGRESS, result);
core_interface_->ReleaseResource(ws);
for (int i = 0; kInvalidURLs[i]; ++i) {
ws = Connect(kInvalidURLs[i], &result, NULL);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
core_interface_->ReleaseResource(ws);
}
// TODO(toyoshim): Add invalid protocols tests
PASS();
}
std::string TestWebSocket::TestGetURL() {
for (int i = 0; kInvalidURLs[i]; ++i) {
int32_t result;
PP_Resource ws = Connect(kInvalidURLs[i], &result, NULL);
ASSERT_TRUE(ws);
PP_Var url = websocket_interface_->GetURL(ws);
ASSERT_TRUE(AreEqual(url, kInvalidURLs[i]));
ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
ReleaseVar(url);
core_interface_->ReleaseResource(ws);
}
PASS();
}
std::string TestWebSocket::TestValidConnect() {
int32_t result;
PP_Resource ws = Connect(kEchoServerURL, &result, NULL);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
core_interface_->ReleaseResource(ws);
PASS();
}
std::string TestWebSocket::TestInvalidClose() {
PP_Var reason = CreateVar("close for test");
TestCompletionCallback callback(instance_->pp_instance());
// Close before connect.
PP_Resource ws = websocket_interface_->Create(instance_->pp_instance());
int32_t result = websocket_interface_->Close(
ws, kCloseCodeNormalClosure, reason,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_ERROR_FAILED, result);
core_interface_->ReleaseResource(ws);
// Close with bad arguments.
ws = Connect(kEchoServerURL, &result, NULL);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
result = websocket_interface_->Close(ws, 1, reason,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_ERROR_NOACCESS, result);
core_interface_->ReleaseResource(ws);
ReleaseVar(reason);
PASS();
}
std::string TestWebSocket::TestValidClose() {
PP_Var reason = CreateVar("close for test");
PP_Var url = CreateVar(kEchoServerURL);
PP_Var protocols[] = { PP_MakeUndefined() };
TestCompletionCallback callback(instance_->pp_instance());
TestCompletionCallback another_callback(instance_->pp_instance());
// Close.
int32_t result;
PP_Resource ws = Connect(kEchoServerURL, &result, NULL);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = callback.WaitForResult();
ASSERT_EQ(PP_OK, result);
core_interface_->ReleaseResource(ws);
// Close in connecting.
// The ongoing connect failed with PP_ERROR_ABORTED, then the close is done
// successfully.
ws = websocket_interface_->Create(instance_->pp_instance());
result = websocket_interface_->Connect(ws, url, protocols, 0,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
static_cast<pp::CompletionCallback>(
another_callback).pp_completion_callback());
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = callback.WaitForResult();
ASSERT_EQ(PP_ERROR_ABORTED, result);
result = another_callback.WaitForResult();
ASSERT_EQ(PP_OK, result);
core_interface_->ReleaseResource(ws);
// Close in closing.
// The first close will be done successfully, then the second one failed with
// with PP_ERROR_INPROGRESS immediately.
ws = Connect(kEchoServerURL, &result, NULL);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
static_cast<pp::CompletionCallback>(
another_callback).pp_completion_callback());
ASSERT_EQ(PP_ERROR_INPROGRESS, result);
result = callback.WaitForResult();
ASSERT_EQ(PP_OK, result);
core_interface_->ReleaseResource(ws);
// Close with ongoing receive message.
ws = Connect(kEchoServerURL, &result, NULL);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
PP_Var receive_message_var;
result = websocket_interface_->ReceiveMessage(ws, &receive_message_var,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = websocket_interface_->Close(ws, kCloseCodeNormalClosure, reason,
static_cast<pp::CompletionCallback>(
another_callback).pp_completion_callback());
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
result = callback.WaitForResult();
ASSERT_EQ(PP_ERROR_ABORTED, result);
result = another_callback.WaitForResult();
ASSERT_EQ(PP_OK, result);
core_interface_->ReleaseResource(ws);
ReleaseVar(reason);
ReleaseVar(url);
PASS();
}
std::string TestWebSocket::TestGetProtocol() {
const char* expected_protocols[] = {
"x-chat",
"hoehoe",
NULL
};
for (int i = 0; expected_protocols[i]; ++i) {
std::string url(kProtocolTestServerURL);
url += expected_protocols[i];
int32_t result;
PP_Resource ws = Connect(url.c_str(), &result, expected_protocols[i]);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, result);
PP_Var protocol = websocket_interface_->GetProtocol(ws);
ASSERT_TRUE(AreEqual(protocol, expected_protocols[i]));
ReleaseVar(protocol);
core_interface_->ReleaseResource(ws);
}
PASS();
}
std::string TestWebSocket::TestTextSendReceive() {
// Connect to test echo server.
int32_t connect_result;
PP_Resource ws = Connect(kEchoServerURL, &connect_result, NULL);
ASSERT_TRUE(ws);
ASSERT_EQ(PP_OK, connect_result);
// Send 'hello pepper' text message.
const char* message = "hello pepper";
PP_Var message_var = CreateVar(message);
int32_t result = websocket_interface_->SendMessage(ws, message_var);
ReleaseVar(message_var);
ASSERT_EQ(PP_OK, result);
// Receive echoed 'hello pepper'.
TestCompletionCallback callback(instance_->pp_instance(), force_async_);
PP_Var received_message;
result = websocket_interface_->ReceiveMessage(ws, &received_message,
static_cast<pp::CompletionCallback>(callback).pp_completion_callback());
ASSERT_FALSE(result != PP_OK && result != PP_OK_COMPLETIONPENDING);
if (result == PP_OK_COMPLETIONPENDING)
result = callback.WaitForResult();
ASSERT_EQ(PP_OK, result);
ASSERT_TRUE(AreEqual(received_message, message));
ReleaseVar(received_message);
core_interface_->ReleaseResource(ws);
PASS();
}
// TODO(toyoshim): Add tests for GetBufferedAmount().
// For now, the function doesn't work fine because update callback in WebKit is
// not landed yet.
// TODO(toyoshim): Add tests for didReceiveMessageError().
// TODO(toyoshim): Add other function tests.
|