2515
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
2507
|
3 |
"{ Package: 'stx:goodies/regression' }"
|
|
4 |
|
|
5 |
"{ NameSpace: RegressionTests }"
|
|
6 |
|
|
7 |
TestCase subclass:#WebSocketTest
|
|
8 |
instanceVariableNames:''
|
|
9 |
classVariableNames:'ServerWebSocket ClientWebSocket'
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'tests-Regression-Web'
|
|
12 |
!
|
|
13 |
|
|
14 |
HTTPService subclass:#DemoWebSocketService
|
|
15 |
instanceVariableNames:'webSocketCreationCallback'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
privateIn:WebSocketTest
|
|
19 |
!
|
|
20 |
|
|
21 |
!WebSocketTest class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
documentation
|
|
24 |
"
|
|
25 |
documentation to be added.
|
|
26 |
|
|
27 |
class:
|
|
28 |
<a short class summary here, describing what instances represent>
|
|
29 |
|
|
30 |
responsibilities:
|
|
31 |
<describing what my main role is>
|
|
32 |
|
|
33 |
collaborators:
|
|
34 |
<describing with whom and how I talk to>
|
|
35 |
|
|
36 |
API:
|
|
37 |
<public api and main messages>
|
|
38 |
|
|
39 |
example:
|
|
40 |
<a one-line examples on how to use - can also be in a separate example method>
|
|
41 |
|
|
42 |
implementation:
|
|
43 |
<implementation points>
|
|
44 |
|
|
45 |
[author:]
|
|
46 |
Stefan Reise
|
|
47 |
|
|
48 |
[instance variables:]
|
|
49 |
|
|
50 |
[class variables:]
|
|
51 |
|
|
52 |
[see also:]
|
|
53 |
|
|
54 |
"
|
|
55 |
! !
|
|
56 |
|
|
57 |
!WebSocketTest class methodsFor:'actions'!
|
|
58 |
|
|
59 |
resetServerAndClientWebsocket
|
|
60 |
"
|
|
61 |
self resetServerAndClientWebsocket
|
|
62 |
"
|
|
63 |
|
|
64 |
ServerWebSocket := nil.
|
|
65 |
ClientWebSocket := nil.
|
|
66 |
|
|
67 |
"Created: / 17-01-2020 / 13:37:18 / Stefan Reise"
|
|
68 |
! !
|
|
69 |
|
|
70 |
!WebSocketTest methodsFor:'data'!
|
|
71 |
|
|
72 |
allData
|
|
73 |
|allData|
|
|
74 |
|
|
75 |
allData := OrderedCollection new.
|
|
76 |
self allDataWithFrameHash pairWiseDo:[:first :second |
|
|
77 |
allData add:first.
|
|
78 |
].
|
|
79 |
|
|
80 |
^ allData
|
|
81 |
|
|
82 |
"Created: / 20-01-2020 / 13:56:05 / Stefan Reise"
|
|
83 |
!
|
|
84 |
|
|
85 |
allDataWithFrameHash
|
|
86 |
^ self dataWithLength0AndFrameHash "/ zero data
|
|
87 |
, self dataWithLength11AndFrameHash "/ just hello world
|
|
88 |
, self dataWithLength125AndFrameHash "/ last fitting length for the single byte
|
|
89 |
, self dataWithLength126AndFrameHash "/ first length to put into for 2 bytes according to the protocol
|
|
90 |
, self dataWithLength127AndFrameHash "/ first length + 1 to put into 2 bytes
|
|
91 |
, self dataWithLength65535AndFrameHash "/ last fitting length for 2 bytes
|
|
92 |
, self dataWithLength65536AndFrameHash "/ first length to put into 8 bytes according to the protocol
|
|
93 |
, self dataWithLength70536AndFrameHash "/ random length to put into 8 bytes
|
|
94 |
, self dataWithLengthMaxPerFrameAndFrameHash "/ last fitting length for max frame length
|
|
95 |
, self dataWithLengthMaxPerFramePlus1AndFrameHash "/ first length to put into multiple frames
|
|
96 |
, self dataWithLengthMaxTestAndFrameHash "/ max length for testing
|
|
97 |
|
|
98 |
"Created: / 20-01-2020 / 13:54:14 / Stefan Reise"
|
|
99 |
!
|
|
100 |
|
|
101 |
dataWithLength0AndFrameHash
|
|
102 |
^ #(
|
|
103 |
''
|
|
104 |
33024
|
|
105 |
)
|
|
106 |
|
|
107 |
"Created: / 20-01-2020 / 13:24:23 / Stefan Reise"
|
|
108 |
!
|
|
109 |
|
|
110 |
dataWithLength11AndFrameHash
|
|
111 |
^ #(
|
|
112 |
'Hello World'
|
|
113 |
457351956
|
|
114 |
)
|
|
115 |
|
|
116 |
"Created: / 20-01-2020 / 13:24:42 / Stefan Reise"
|
|
117 |
!
|
|
118 |
|
|
119 |
dataWithLength125AndFrameHash
|
|
120 |
^ #(
|
|
121 |
'12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345'
|
|
122 |
854990094
|
|
123 |
)
|
|
124 |
|
|
125 |
"Created: / 20-01-2020 / 13:21:12 / Stefan Reise"
|
|
126 |
!
|
|
127 |
|
|
128 |
dataWithLength126AndFrameHash
|
|
129 |
^ #(
|
|
130 |
'123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456'
|
|
131 |
386953194
|
|
132 |
)
|
|
133 |
|
|
134 |
"Created: / 20-01-2020 / 13:21:15 / Stefan Reise"
|
|
135 |
!
|
|
136 |
|
|
137 |
dataWithLength127AndFrameHash
|
|
138 |
^ #(
|
|
139 |
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'
|
|
140 |
1015153950
|
|
141 |
)
|
|
142 |
|
|
143 |
"Created: / 20-01-2020 / 13:21:18 / Stefan Reise"
|
|
144 |
!
|
|
145 |
|
|
146 |
dataWithLength65535AndFrameHash
|
|
147 |
|string|
|
|
148 |
|
|
149 |
string := String new:65535.
|
|
150 |
|
|
151 |
^ Array
|
|
152 |
with:string
|
|
153 |
with:526675651
|
|
154 |
|
|
155 |
"Created: / 20-01-2020 / 13:21:20 / Stefan Reise"
|
|
156 |
!
|
|
157 |
|
|
158 |
dataWithLength65536AndFrameHash
|
|
159 |
|string|
|
|
160 |
|
|
161 |
string := String new:65536.
|
|
162 |
|
|
163 |
^ Array
|
|
164 |
with:string
|
|
165 |
with:894588570
|
|
166 |
|
|
167 |
"Created: / 20-01-2020 / 13:21:23 / Stefan Reise"
|
|
168 |
!
|
|
169 |
|
|
170 |
dataWithLength70536AndFrameHash
|
|
171 |
|string|
|
|
172 |
|
|
173 |
string := String new:70536.
|
|
174 |
|
|
175 |
^ Array
|
|
176 |
with:string
|
|
177 |
with:201475330
|
|
178 |
|
|
179 |
"Created: / 20-01-2020 / 13:21:25 / Stefan Reise"
|
|
180 |
!
|
|
181 |
|
|
182 |
dataWithLengthMaxPerFrameAndFrameHash
|
|
183 |
|string|
|
|
184 |
|
|
185 |
string := String new:WebSocketStream maxSizeInByterPerFrame.
|
|
186 |
|
|
187 |
^ Array
|
|
188 |
with:string
|
|
189 |
with:656112522
|
|
190 |
|
|
191 |
"Created: / 20-01-2020 / 13:59:13 / Stefan Reise"
|
|
192 |
!
|
|
193 |
|
|
194 |
dataWithLengthMaxPerFramePlus1AndFrameHash
|
|
195 |
|string|
|
|
196 |
|
|
197 |
string := String new:WebSocketStream maxSizeInByterPerFrame + 1.
|
|
198 |
|
|
199 |
^ Array
|
|
200 |
with:string
|
|
201 |
with:95386507
|
|
202 |
|
|
203 |
"Created: / 20-01-2020 / 14:03:29 / Stefan Reise"
|
|
204 |
!
|
|
205 |
|
|
206 |
dataWithLengthMaxTestAndFrameHash
|
|
207 |
|string|
|
|
208 |
|
|
209 |
string := String new:5 * 1024 * 1024. "/ 5mb
|
|
210 |
|
|
211 |
^ Array
|
|
212 |
with:string
|
|
213 |
with:888620042
|
|
214 |
|
|
215 |
"Created: / 20-01-2020 / 14:04:40 / Stefan Reise"
|
|
216 |
! !
|
|
217 |
|
|
218 |
!WebSocketTest methodsFor:'helper'!
|
|
219 |
|
|
220 |
assertEncodingPairwiseDataAndHash:dataAndHash
|
|
221 |
"encodes and decode the data for client -> server and server -> client protocol"
|
|
222 |
|
|
223 |
|serverWebSocket clientWebSocket
|
|
224 |
decodeBlock
|
|
225 |
eachFrame|
|
|
226 |
|
|
227 |
serverWebSocket := self serverWebSocket.
|
|
228 |
clientWebSocket := self clientWebSocket.
|
|
229 |
|
|
230 |
decodeBlock :=
|
|
231 |
[:eachWebSocket :eachFrame |
|
|
232 |
((eachWebSocket decodeFrameHybi17:eachFrame) payloadData ? '') asString
|
|
233 |
].
|
|
234 |
|
|
235 |
dataAndHash
|
|
236 |
pairWiseDo:[:eachData :eachExpectedHashForServer |
|
|
237 |
"server -> client"
|
|
238 |
eachFrame := serverWebSocket encodeFrameHybi17:eachData.
|
|
239 |
self assert:eachFrame hash = eachExpectedHashForServer.
|
|
240 |
self assert:eachData = (decodeBlock value:clientWebSocket value:eachFrame).
|
|
241 |
|
|
242 |
"client -> server"
|
|
243 |
eachFrame := clientWebSocket encodeFrameHybi17:eachData.
|
|
244 |
"/ not able to check against hash from client requests, because they are masked randomly
|
|
245 |
self assert:eachData = (decodeBlock value:serverWebSocket value:eachFrame).
|
|
246 |
].
|
|
247 |
|
|
248 |
"Created: / 17-01-2020 / 17:03:21 / Stefan Reise"
|
|
249 |
"Modified (comment): / 20-01-2020 / 13:04:20 / Stefan Reise"
|
|
250 |
!
|
|
251 |
|
|
252 |
setupServerAndClientWebsocket
|
|
253 |
"
|
|
254 |
self resetServerAndClientWebsocket
|
|
255 |
"
|
|
256 |
|
|
257 |
|response|
|
|
258 |
|
|
259 |
DemoWebSocketService start
|
|
260 |
webSocketCreationCallback:[:newWebSocket |
|
|
261 |
ServerWebSocket := newWebSocket.
|
|
262 |
].
|
|
263 |
|
|
264 |
response := HTTPInterface
|
|
265 |
get:('ws://localhost:%1%2'
|
|
266 |
bindWith:DemoWebSocketService defaultPort
|
|
267 |
with:DemoWebSocketService linkName).
|
|
268 |
|
|
269 |
response isErrorResponse ifTrue:[
|
|
270 |
self error:'failed to connect'.
|
|
271 |
].
|
|
272 |
|
|
273 |
ClientWebSocket := response webSocket.
|
|
274 |
|
|
275 |
"Created: / 17-01-2020 / 13:21:28 / Stefan Reise"
|
2515
|
276 |
"Modified: / 20-01-2020 / 18:08:26 / Stefan Reise"
|
2507
|
277 |
! !
|
|
278 |
|
|
279 |
!WebSocketTest methodsFor:'queries'!
|
|
280 |
|
|
281 |
clientWebSocket
|
|
282 |
ClientWebSocket isNil ifTrue:[
|
|
283 |
self setupServerAndClientWebsocket.
|
|
284 |
].
|
|
285 |
|
|
286 |
^ ClientWebSocket
|
|
287 |
|
|
288 |
"Created: / 17-01-2020 / 13:23:11 / Stefan Reise"
|
|
289 |
!
|
|
290 |
|
|
291 |
serverWebSocket
|
|
292 |
ServerWebSocket isNil ifTrue:[
|
|
293 |
self setupServerAndClientWebsocket.
|
|
294 |
].
|
|
295 |
|
|
296 |
^ ServerWebSocket
|
|
297 |
|
|
298 |
"Created: / 17-01-2020 / 13:23:15 / Stefan Reise"
|
|
299 |
! !
|
|
300 |
|
|
301 |
!WebSocketTest methodsFor:'tests'!
|
|
302 |
|
|
303 |
testCommunication
|
|
304 |
"
|
|
305 |
call the following method in case the sockets did get corrupted
|
2515
|
306 |
self resetServerAndClientWebsocket
|
|
307 |
|
|
308 |
WebSocketStream verbose:true.
|
|
309 |
WebSocketStream verboseProtocol:true.
|
|
310 |
|
|
311 |
WebSocketStream verbose:false.
|
|
312 |
WebSocketStream verboseProtocol:false.
|
|
313 |
|
|
314 |
WebSocketStream maxSizeInByterPerFrame:1024.
|
|
315 |
WebSocketStream maxSizeInByterPerFrame:nil.
|
2507
|
316 |
"
|
|
317 |
|
|
318 |
self allData do:[:data |
|
|
319 |
self clientWebSocket nextPut:data.
|
|
320 |
self assert:self serverWebSocket next = data.
|
|
321 |
|
|
322 |
self serverWebSocket nextPut:data.
|
|
323 |
self assert:self clientWebSocket next = data.
|
|
324 |
].
|
|
325 |
|
|
326 |
"Created: / 17-01-2020 / 13:24:24 / Stefan Reise"
|
|
327 |
"Modified: / 20-01-2020 / 15:21:56 / Stefan Reise"
|
2515
|
328 |
"Modified (comment): / 20-01-2020 / 18:09:02 / Stefan Reise"
|
2507
|
329 |
!
|
|
330 |
|
|
331 |
testEncoding
|
|
332 |
"
|
|
333 |
call the following method in case the sockets did get corrupted
|
2515
|
334 |
self resetServerAndClientWebsocket
|
|
335 |
|
|
336 |
WebSocketStream verbose:true.
|
|
337 |
WebSocketStream verboseProtocol:true.
|
|
338 |
|
|
339 |
WebSocketStream verbose:false.
|
|
340 |
WebSocketStream verboseProtocol:false.
|
2507
|
341 |
"
|
|
342 |
|
2515
|
343 |
"set the value, which was used to record the frame hashe"
|
|
344 |
WebSocketStream maxSizeInByterPerFrame:512 * 1024.
|
|
345 |
self assertEncodingPairwiseDataAndHash:self allDataWithFrameHash.
|
|
346 |
WebSocketStream maxSizeInByterPerFrame:nil.
|
2507
|
347 |
|
|
348 |
"Created: / 20-01-2020 / 13:13:50 / Stefan Reise"
|
2515
|
349 |
"Modified (comment): / 20-01-2020 / 18:09:08 / Stefan Reise"
|
2507
|
350 |
!
|
|
351 |
|
|
352 |
testMask
|
|
353 |
"
|
|
354 |
call the following method in case the sockets did get corrupted
|
2515
|
355 |
self resetServerAndClientWebsocket
|
|
356 |
|
|
357 |
WebSocketStream verbose:true.
|
|
358 |
WebSocketStream verboseProtocol:true.
|
|
359 |
|
|
360 |
WebSocketStream verbose:false.
|
|
361 |
WebSocketStream verboseProtocol:false.
|
2507
|
362 |
"
|
|
363 |
|
|
364 |
|startTimestamp webSocket mask masked unmasked|
|
|
365 |
|
|
366 |
startTimestamp := Timestamp now.
|
|
367 |
webSocket := WebSocketStream new.
|
|
368 |
mask := #[12 214 166 43].
|
|
369 |
|
|
370 |
self allData do:[:eachData |
|
|
371 |
masked := webSocket
|
|
372 |
maskPayload:eachData
|
|
373 |
mask:mask.
|
|
374 |
|
|
375 |
unmasked := webSocket
|
|
376 |
maskPayload:masked
|
|
377 |
mask:mask.
|
|
378 |
|
|
379 |
self assert:unmasked = eachData asByteArray.
|
|
380 |
].
|
|
381 |
|
|
382 |
Transcript showCR:(Timestamp now - startTimestamp) printString.
|
|
383 |
|
|
384 |
"Created: / 20-01-2020 / 15:08:27 / Stefan Reise"
|
2515
|
385 |
"Modified (comment): / 20-01-2020 / 18:09:11 / Stefan Reise"
|
2507
|
386 |
!
|
|
387 |
|
|
388 |
testPing
|
|
389 |
"
|
|
390 |
call the following method in case the sockets did get corrupted
|
2515
|
391 |
self resetServerAndClientWebsocket
|
|
392 |
|
|
393 |
WebSocketStream verbose:true.
|
|
394 |
WebSocketStream verboseProtocol:true.
|
|
395 |
|
|
396 |
WebSocketStream verbose:false.
|
|
397 |
WebSocketStream verboseProtocol:false.
|
2507
|
398 |
"
|
|
399 |
|
|
400 |
|pingTimeDuration|
|
|
401 |
|
|
402 |
pingTimeDuration := self clientWebSocket ping.
|
|
403 |
Transcript showCR:pingTimeDuration printString.
|
|
404 |
self assert:pingTimeDuration notNil.
|
|
405 |
|
|
406 |
pingTimeDuration := self serverWebSocket ping.
|
|
407 |
Transcript showCR:pingTimeDuration printString.
|
|
408 |
self assert:pingTimeDuration notNil.
|
|
409 |
|
|
410 |
"Created: / 17-01-2020 / 13:43:58 / Stefan Reise"
|
2515
|
411 |
"Modified (comment): / 20-01-2020 / 18:09:16 / Stefan Reise"
|
2507
|
412 |
! !
|
|
413 |
|
|
414 |
!WebSocketTest methodsFor:'zzz'!
|
|
415 |
|
|
416 |
testOnEchoServer
|
|
417 |
|rply|
|
|
418 |
|
|
419 |
rply := HTTPInterface get:'ws://echo.websocket.org'.
|
|
420 |
rply isErrorResponse ifTrue:[
|
|
421 |
self error:'failed to connect'.
|
|
422 |
].
|
|
423 |
|
|
424 |
rply webSocket
|
|
425 |
|
|
426 |
"Created: / 15-01-2020 / 15:55:27 / Stefan Reise"
|
|
427 |
! !
|
|
428 |
|
|
429 |
!WebSocketTest::DemoWebSocketService class methodsFor:'constants'!
|
|
430 |
|
|
431 |
defaultPort
|
|
432 |
^ 9095
|
|
433 |
|
|
434 |
"Created: / 18-11-2019 / 13:19:29 / Stefan Reise"
|
|
435 |
!
|
|
436 |
|
|
437 |
linkName
|
|
438 |
^ '/websocket'
|
|
439 |
|
|
440 |
"Created: / 13-11-2019 / 13:30:11 / Stefan Reise"
|
|
441 |
! !
|
|
442 |
|
|
443 |
!WebSocketTest::DemoWebSocketService class methodsFor:'documentation'!
|
|
444 |
|
|
445 |
documentation
|
|
446 |
"
|
|
447 |
[start Server with:]
|
|
448 |
HTTPServer startServerOnPort:8080
|
|
449 |
|
|
450 |
[start with:]
|
|
451 |
(self new)
|
|
452 |
registerServiceOn:(HTTPServer runningServerOnPort:8080)
|
|
453 |
|
|
454 |
class:
|
|
455 |
<a short class summary here, describing what instances represent>
|
|
456 |
|
|
457 |
responsibilities:
|
|
458 |
<describing what my main role is>
|
|
459 |
|
|
460 |
collaborators:
|
|
461 |
<describing with whom and how I talk to>
|
|
462 |
|
|
463 |
API:
|
|
464 |
<public api and main messages>
|
|
465 |
|
|
466 |
example:
|
|
467 |
<a one-line examples on how to use - can also be in a separate example method>
|
|
468 |
|
|
469 |
implementation:
|
|
470 |
<implementation points>
|
|
471 |
|
|
472 |
[author:]
|
|
473 |
Stefan Reise
|
|
474 |
|
|
475 |
[instance variables:]
|
|
476 |
|
|
477 |
[class variables:]
|
|
478 |
|
|
479 |
[see also:]
|
|
480 |
|
|
481 |
"
|
|
482 |
! !
|
|
483 |
|
|
484 |
!WebSocketTest::DemoWebSocketService class methodsFor:'examples'!
|
|
485 |
|
|
486 |
start
|
|
487 |
"
|
|
488 |
self start
|
|
489 |
"
|
|
490 |
|
|
491 |
|httpServer instance|
|
|
492 |
|
|
493 |
httpServer := HTTPServer serverOnPort:self defaultPort.
|
|
494 |
instance := self registerServiceOn:httpServer.
|
|
495 |
httpServer start.
|
|
496 |
|
|
497 |
^ instance
|
|
498 |
|
|
499 |
"Created: / 17-01-2020 / 13:19:08 / Stefan Reise"
|
|
500 |
! !
|
|
501 |
|
|
502 |
!WebSocketTest::DemoWebSocketService methodsFor:'accessing'!
|
|
503 |
|
|
504 |
webSocketCreationCallback:something
|
|
505 |
webSocketCreationCallback := something.
|
|
506 |
! !
|
|
507 |
|
|
508 |
!WebSocketTest::DemoWebSocketService methodsFor:'processing'!
|
|
509 |
|
|
510 |
acceptsWebSocket:webSocket
|
|
511 |
^ true
|
|
512 |
|
|
513 |
"Created: / 13-11-2019 / 13:57:03 / Stefan Reise"
|
|
514 |
!
|
|
515 |
|
|
516 |
startServingWebSocket:webSocket
|
|
517 |
"to be redefined in subclasses (services) which do handle webSockets;
|
|
518 |
close here, in case it was not redefined"
|
|
519 |
|
|
520 |
webSocketCreationCallback value:webSocket.
|
|
521 |
|
|
522 |
"Created: / 13-11-2019 / 14:42:43 / Stefan Reise"
|
|
523 |
"Modified: / 17-01-2020 / 13:19:52 / Stefan Reise"
|
|
524 |
! !
|
|
525 |
|
|
526 |
!WebSocketTest::DemoWebSocketService methodsFor:'queries'!
|
|
527 |
|
|
528 |
allowWebSockets
|
|
529 |
^ true
|
|
530 |
|
|
531 |
"Created: / 13-11-2019 / 13:29:48 / Stefan Reise"
|
|
532 |
! !
|
|
533 |
|
|
534 |
!WebSocketTest class methodsFor:'documentation'!
|
|
535 |
|
|
536 |
version_CVS
|
|
537 |
^ '$Header$'
|
|
538 |
! !
|
|
539 |
|