1717import clang_native
1818import common
1919from common import BrowserCore , no_windows , create_file , test_file , read_file
20+ from common import parameterized , requires_native_clang
2021from tools import shared , config , utils
2122from tools .shared import PYTHON , EMCC , path_from_root , run_process , CLANG_CC
2223
@@ -166,58 +167,62 @@ def setUpClass(cls):
166167 print ('Setting NODE_PATH=' + path_from_root ('node_modules' ))
167168 os .environ ['NODE_PATH' ] = path_from_root ('node_modules' )
168169
169- def test_sockets_echo (self , extra_args = []):
170- # Note: in the WebsockifyServerHarness and CompiledServerHarness tests below, explicitly use consecutive server listen ports,
171- # because server teardown might not occur deterministically (python dtor time) and is a bit racy.
172- # WebsockifyServerHarness uses two port numbers, x and x-1, so increment it by two.
173- # CompiledServerHarness only uses one. Start with 49160 & 49159 as the first server port addresses. If adding new tests,
174- # increment the used port addresses below.
175-
176- # Websockify-proxied servers can't run dgram tests
177- harnesses = [
178- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=0' ], 49161 ), 0 ),
179- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=1' ], 49162 ), 1 ),
180- # The following forces non-NULL addr and addlen parameters for the accept call
181- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=0' , '-DTEST_ACCEPT_ADDR=1' ], 49163 ), 0 )
182- ]
183-
184- if not common .EMTEST_LACKS_NATIVE_CLANG :
185- harnesses += [(WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 49160 ), 0 )]
186-
187- for harness , datagram in harnesses :
188- with harness :
189- self .btest_exit (test_file ('sockets/test_sockets_echo_client.c' ), args = ['-DSOCKK=%d' % harness .listen_port , '-DTEST_DGRAM=%d' % datagram ] + extra_args )
170+ # Note: in the WebsockifyServerHarness and CompiledServerHarness tests below, explicitly use
171+ # consecutive server listen ports, because server teardown might not occur deterministically
172+ # (python dtor time) and is a bit racy.
173+ # WebsockifyServerHarness uses two port numbers, x and x-1, so increment it by two.
174+ # CompiledServerHarness only uses one. Start with 49160 & 49159 as the first server port
175+ # addresses. If adding new tests, increment the used port addresses below.
176+ @parameterized ({
177+ 'websockify' : [WebsockifyServerHarness , 49160 , ['-DTEST_DGRAM=0' ]],
178+ 'tcp' : [CompiledServerHarness , 49161 , ['-DTEST_DGRAM=0' ]],
179+ 'udp' : [CompiledServerHarness , 49162 , ['-DTEST_DGRAM=1' ]],
180+ # The following forces non-NULL addr and addlen parameters for the accept call
181+ 'accept_addr' : [CompiledServerHarness , 49163 , ['-DTEST_DGRAM=0' , '-DTEST_ACCEPT_ADDR=1' ]],
182+ })
183+ def test_sockets_echo (self , harness_class , port , args ):
184+ if harness_class == WebsockifyServerHarness and common .EMTEST_LACKS_NATIVE_CLANG :
185+ self .skipTest ('requires native clange' )
186+
187+ with harness_class (test_file ('sockets/test_sockets_echo_server.c' ), args , port ) as harness :
188+ self .btest_exit (test_file ('sockets/test_sockets_echo_client.c' ), args = ['-DSOCKK=%d' % harness .listen_port ] + args )
190189
191190 def test_sockets_echo_pthreads (self ):
192- self .test_sockets_echo (['-sUSE_PTHREADS' , '-sPROXY_TO_PTHREAD' ])
191+ with CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 49161 ) as harness :
192+ self .btest_exit (test_file ('sockets/test_sockets_echo_client.c' ), args = ['-sUSE_PTHREADS' , '-sPROXY_TO_PTHREAD' , '-DSOCKK=%d' % harness .listen_port ])
193193
194194 def test_sdl2_sockets_echo (self ):
195195 with CompiledServerHarness ('sdl2_net_server.c' , ['-sUSE_SDL=2' , '-sUSE_SDL_NET=2' ], 49164 ) as harness :
196196 self .btest_exit ('sdl2_net_client.c' , args = ['-sUSE_SDL=2' , '-sUSE_SDL_NET=2' , '-DSOCKK=%d' % harness .listen_port ])
197197
198- def test_sockets_async_echo (self ):
199- # Websockify-proxied servers can't run dgram tests
200- harnesses = [
201- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=0' , '-DTEST_ASYNC=1' ], 49167 ), 0 ),
202- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=1' , '-DTEST_ASYNC=1' ], 49168 ), 1 ),
203- # The following forces non-NULL addr and addlen parameters for the accept call
204- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=0' , '-DTEST_ACCEPT_ADDR=1' , '-DTEST_ASYNC=1' ], 49169 ), 0 )
205- ]
206-
207- if not common .EMTEST_LACKS_NATIVE_CLANG :
208- harnesses += [(WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_ASYNC=1' ], 49166 ), 0 )]
209-
210- for harness , datagram in harnesses :
211- print ('harness:' , harness )
212- with harness :
213- self .btest_exit (test_file ('sockets/test_sockets_echo_client.c' ), args = ['-DSOCKK=%d' % harness .listen_port , '-DTEST_DGRAM=%d' % datagram , '-DTEST_ASYNC=1' ])
214- return
215-
216- # Deliberately attempt a connection on a port that will fail to test the error callback and getsockopt
217- print ('expect fail' )
198+ @parameterized ({
199+ 'websockify' : [WebsockifyServerHarness , 49166 , ['-DTEST_DGRAM=0' ]],
200+ 'tcp' : [CompiledServerHarness , 49167 , ['-DTEST_DGRAM=0' ]],
201+ 'udp' : [CompiledServerHarness , 49168 , ['-DTEST_DGRAM=1' ]],
202+ # The following forces non-NULL addr and addlen parameters for the accept call
203+ 'accept_addr' : [CompiledServerHarness , 49169 , ['-DTEST_DGRAM=0' , '-DTEST_ACCEPT_ADDR=1' ]],
204+ })
205+ def test_sockets_async_echo (self , harness_class , port , args ):
206+ if harness_class == WebsockifyServerHarness and common .EMTEST_LACKS_NATIVE_CLANG :
207+ self .skipTest ('requires native clange' )
208+
209+ args .append ('-DTEST_ASYNC=1' )
210+ with harness_class (test_file ('sockets/test_sockets_echo_server.c' ), args , port ) as harness :
211+ self .btest_exit (test_file ('sockets/test_sockets_echo_client.c' ), args = ['-DSOCKK=%d' % harness .listen_port ] + args )
212+
213+ def test_sockets_async_bad_port (self ):
214+ # Deliberately attempt a connection on a port that will fail to test the error callback and
215+ # getsockopt
218216 self .btest_exit (test_file ('sockets/test_sockets_echo_client.c' ), args = ['-DSOCKK=49169' , '-DTEST_ASYNC=1' ])
219217
220- def test_sockets_echo_bigdata (self ):
218+ @parameterized ({
219+ 'websockify' : [WebsockifyServerHarness , 49171 , ['-DTEST_DGRAM=0' ]],
220+ 'tcp' : [CompiledServerHarness , 49172 , ['-DTEST_DGRAM=0' ]],
221+ 'udp' : [CompiledServerHarness , 49173 , ['-DTEST_DGRAM=1' ]],
222+ })
223+ def test_sockets_echo_bigdata (self , harness_class , port , args ):
224+ if harness_class == WebsockifyServerHarness and common .EMTEST_LACKS_NATIVE_CLANG :
225+ self .skipTest ('requires native clange' )
221226 sockets_include = '-I' + test_file ('sockets' )
222227
223228 # generate a large string literal to use as our message
@@ -229,17 +234,8 @@ def test_sockets_echo_bigdata(self):
229234 src = read_file (test_file ('sockets/test_sockets_echo_client.c' ))
230235 create_file ('test_sockets_echo_bigdata.c' , src .replace ('#define MESSAGE "pingtothepong"' , '#define MESSAGE "%s"' % message ))
231236
232- harnesses = [
233- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=0' ], 49172 ), 0 ),
234- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=1' ], 49173 ), 1 )
235- ]
236-
237- if not common .EMTEST_LACKS_NATIVE_CLANG :
238- harnesses += [(WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 49171 ), 0 )]
239-
240- for harness , datagram in harnesses :
241- with harness :
242- self .btest_exit ('test_sockets_echo_bigdata.c' , args = [sockets_include , '-DSOCKK=%d' % harness .listen_port , '-DTEST_DGRAM=%d' % datagram ])
237+ with harness_class (test_file ('sockets/test_sockets_echo_server.c' ), args , port ) as harness :
238+ self .btest_exit ('test_sockets_echo_bigdata.c' , args = [sockets_include , '-DSOCKK=%d' % harness .listen_port ] + args )
243239
244240 @no_windows ('This test is Unix-specific.' )
245241 def test_sockets_partial (self ):
@@ -281,63 +277,59 @@ def test_enet(self):
281277 with CompiledServerHarness (test_file ('sockets/test_enet_server.c' ), enet , 49210 ) as harness :
282278 self .btest_exit (test_file ('sockets/test_enet_client.c' ), args = enet + ['-DSOCKK=%d' % harness .listen_port ])
283279
284- def test_nodejs_sockets_echo (self ):
285- # This test checks that sockets work when the client code is run in Node.js
286- if config .NODE_JS not in config .JS_ENGINES :
287- self .skipTest ('node is not present' )
288-
289- harnesses = [
290- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=0' ], 59162 ), 0 ),
291- (CompiledServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), ['-DTEST_DGRAM=1' ], 59164 ), 1 )
292- ]
293-
294- if not common .EMTEST_LACKS_NATIVE_CLANG :
295- harnesses += [(WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 59160 ), 0 )]
280+ @parameterized ({
281+ 'native' : [WebsockifyServerHarness , 59160 , ['-DTEST_DGRAM=0' ]],
282+ 'tcp' : [CompiledServerHarness , 59162 , ['-DTEST_DGRAM=0' ]],
283+ 'udp' : [CompiledServerHarness , 59164 , ['-DTEST_DGRAM=1' ]],
284+ })
285+ def test_nodejs_sockets_echo (self , harness_class , port , args ):
286+ if harness_class == WebsockifyServerHarness and common .EMTEST_LACKS_NATIVE_CLANG :
287+ self .skipTest ('requires native clange' )
296288
297289 # Basic test of node client against both a Websockified and compiled echo server.
298- for harness , datagram in harnesses :
299- with harness :
300- expected = 'do_msg_read: read 14 bytes'
301- self .do_runf (test_file ('sockets/test_sockets_echo_client.c' ), expected , emcc_args = ['-DSOCKK=%d' % harness .listen_port , '-DTEST_DGRAM=%d' % datagram ])
302-
303- if not common .EMTEST_LACKS_NATIVE_CLANG :
304- # Test against a Websockified server with compile time configured WebSocket subprotocol. We use a Websockified
305- # server because as long as the subprotocol list contains binary it will configure itself to accept binary
306- # This test also checks that the connect url contains the correct subprotocols.
307- print ("\n Testing compile time WebSocket configuration.\n " )
308- with WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 59166 ):
309- self .run_process ([EMCC , '-Werror' , test_file ('sockets/test_sockets_echo_client.c' ), '-o' , 'client.js' , '-sSOCKET_DEBUG' , '-sWEBSOCKET_SUBPROTOCOL="base64, binary"' , '-DSOCKK=59166' ])
310-
311- out = self .run_js ('client.js' )
312- self .assertContained ('do_msg_read: read 14 bytes' , out )
313- self .assertContained (['connect: ws://127.0.0.1:59166, base64,binary' , 'connect: ws://127.0.0.1:59166/, base64,binary' ], out )
314-
315- # Test against a Websockified server with runtime WebSocket configuration. We specify both url and subprotocol.
316- # In this test we have *deliberately* used the wrong port '-DSOCKK=12345' to configure the echo_client.c, so
317- # the connection would fail without us specifying a valid WebSocket URL in the configuration.
318- print ("\n Testing runtime WebSocket configuration.\n " )
319- create_file ('websocket_pre.js' , '''
320- var Module = {
321- websocket: {
322- url: 'ws://localhost:59168/testA/testB',
323- subprotocol: 'text, base64, binary',
324- }
325- };
326- ''' )
327- with WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 59168 ) as harness :
328- self .run_process ([EMCC , '-Werror' , test_file ('sockets/test_sockets_echo_client.c' ), '-o' , 'client.js' , '--pre-js=websocket_pre.js' , '-sSOCKET_DEBUG' , '-DSOCKK=12345' ])
329-
330- out = self .run_js ('client.js' )
331- self .assertContained ('do_msg_read: read 14 bytes' , out )
332- self .assertContained ('connect: ws://localhost:59168/testA/testB, text,base64,binary' , out )
290+ with harness_class (test_file ('sockets/test_sockets_echo_server.c' ), args , port ) as harness :
291+ expected = 'do_msg_read: read 14 bytes'
292+ self .do_runf (test_file ('sockets/test_sockets_echo_client.c' ), expected , emcc_args = ['-DSOCKK=%d' % harness .listen_port ] + args )
293+
294+ @requires_native_clang
295+ def test_nodejs_sockets_echo_subprotocol (self ):
296+ # Test against a Websockified server with compile time configured WebSocket subprotocol. We use a Websockified
297+ # server because as long as the subprotocol list contains binary it will configure itself to accept binary
298+ # This test also checks that the connect url contains the correct subprotocols.
299+ with WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 59166 ):
300+ self .run_process ([EMCC , '-Werror' , test_file ('sockets/test_sockets_echo_client.c' ), '-o' , 'client.js' , '-sSOCKET_DEBUG' , '-sWEBSOCKET_SUBPROTOCOL="base64, binary"' , '-DSOCKK=59166' ])
301+
302+ out = self .run_js ('client.js' )
303+ self .assertContained ('do_msg_read: read 14 bytes' , out )
304+ self .assertContained (['connect: ws://127.0.0.1:59166, base64,binary' , 'connect: ws://127.0.0.1:59166/, base64,binary' ], out )
305+
306+ # Test against a Websockified server with runtime WebSocket configuration. We specify both url and subprotocol.
307+ # In this test we have *deliberately* used the wrong port '-DSOCKK=12345' to configure the echo_client.c, so
308+ # the connection would fail without us specifying a valid WebSocket URL in the configuration.
309+ print ("\n Testing runtime WebSocket configuration.\n " )
310+ create_file ('websocket_pre.js' , '''
311+ var Module = {
312+ websocket: {
313+ url: 'ws://localhost:59168/testA/testB',
314+ subprotocol: 'text, base64, binary',
315+ }
316+ };
317+ ''' )
318+ with WebsockifyServerHarness (test_file ('sockets/test_sockets_echo_server.c' ), [], 59168 ):
319+ self .run_process ([EMCC , '-Werror' , test_file ('sockets/test_sockets_echo_client.c' ), '-o' , 'client.js' , '--pre-js=websocket_pre.js' , '-sSOCKET_DEBUG' , '-DSOCKK=12345' ])
320+
321+ out = self .run_js ('client.js' )
322+ self .assertContained ('do_msg_read: read 14 bytes' , out )
323+ self .assertContained ('connect: ws://localhost:59168/testA/testB, text,base64,binary' , out )
333324
334325 # Test Emscripten WebSockets API to send and receive text and binary messages against an echo server.
335326 # N.B. running this test requires 'npm install ws' in Emscripten root directory
336327 def test_websocket_send (self ):
337328 with NodeJsWebSocketEchoServerProcess ():
338329 self .btest_exit (test_file ('websocket/test_websocket_send.c' ), args = ['-lwebsocket' , '-sNO_EXIT_RUNTIME' , '-sWEBSOCKET_DEBUG' ])
339330
340- # Test that native POSIX sockets API can be used by proxying calls to an intermediate WebSockets -> POSIX sockets bridge server
331+ # Test that native POSIX sockets API can be used by proxying calls to an intermediate WebSockets
332+ # -> POSIX sockets bridge server
341333 def test_posix_proxy_sockets (self ):
342334 # Build the websocket bridge server
343335 self .run_process (['cmake' , path_from_root ('tools/websocket_to_posix_proxy' )])
0 commit comments