@@ -27,10 +27,13 @@ def get_my_ip():
27
27
28
28
29
29
class WebsocketTestEcho (WebSocket ):
30
-
31
30
def handleMessage (self ):
32
- self .sendMessage (self .data )
33
- print ('\n Server sent: {}\n ' .format (self .data ))
31
+ if isinstance (self .data , bytes ):
32
+ print (f'\n Server received binary data: { self .data .hex ()} \n ' )
33
+ self .sendMessage (self .data , binary = True )
34
+ else :
35
+ print (f'\n Server received: { self .data } \n ' )
36
+ self .sendMessage (self .data )
34
37
35
38
def handleConnected (self ):
36
39
print ('Connection from: {}' .format (self .address ))
@@ -86,19 +89,26 @@ def test_examples_protocol_websocket(dut):
86
89
3. send and receive data
87
90
"""
88
91
92
+ # Test for echo functionality:
93
+ # Sends a series of simple "hello" messages to the WebSocket server and verifies that each one is echoed back correctly.
94
+ # This tests the basic responsiveness and correctness of the WebSocket connection.
89
95
def test_echo (dut ):
90
96
dut .expect ('WEBSOCKET_EVENT_CONNECTED' )
91
97
for i in range (0 , 5 ):
92
98
dut .expect (re .compile (b'Received=hello (\\ d)' ))
93
99
print ('All echos received' )
94
100
sys .stdout .flush ()
95
101
102
+ # Test for clean closure of the WebSocket connection:
103
+ # Ensures that the WebSocket can correctly receive a close frame and terminate the connection without issues.
96
104
def test_close (dut ):
97
105
code = dut .expect (
98
106
re .compile (
99
107
b'websocket: Received closed message with code=(\\ d*)' ))[0 ]
100
108
print ('Received close frame with code {}' .format (code ))
101
109
110
+ # Test for JSON message handling:
111
+ # Sends a JSON formatted string and verifies that the received message matches the expected JSON structure.
102
112
def test_json (dut , websocket ):
103
113
json_string = """
104
114
[
@@ -118,14 +128,16 @@ def test_json(dut, websocket):
118
128
match = dut .expect (
119
129
re .compile (b'Json=({[a-zA-Z0-9]*).*}' )).group (0 ).decode ()[5 :]
120
130
if match == str (data [0 ]):
121
- print ('Sent message and received message are equal \n ' )
131
+ print ('\n Sent message and received message are equal \n ' )
122
132
sys .stdout .flush ()
123
133
else :
124
134
raise ValueError (
125
135
'DUT received string do not match sent string, \n expected: {}\n with length {}\
126
136
\n received: {}\n with length {}' .format (
127
137
data [0 ], len (data [0 ]), match , len (match )))
128
138
139
+ # Test for receiving long messages:
140
+ # This sends a message with a specified length (2000 characters) to ensure the WebSocket can handle large data payloads. Repeated 3 times for reliability.
129
141
def test_recv_long_msg (dut , websocket , msg_len , repeats ):
130
142
131
143
send_msg = '' .join (
@@ -142,17 +154,58 @@ def test_recv_long_msg(dut, websocket, msg_len, repeats):
142
154
recv_msg += match
143
155
144
156
if recv_msg == send_msg :
145
- print ('Sent message and received message are equal \n ' )
157
+ print ('\n Sent message and received message are equal \n ' )
146
158
sys .stdout .flush ()
147
159
else :
148
160
raise ValueError (
149
161
'DUT received string do not match sent string, \n expected: {}\n with length {}\
150
162
\n received: {}\n with length {}' .format (
151
163
send_msg , len (send_msg ), recv_msg , len (recv_msg )))
152
164
153
- def test_fragmented_msg (dut ):
165
+ # Test for receiving the first fragment of a large message:
166
+ # Verifies the WebSocket's ability to correctly process the initial segment of a fragmented message.
167
+ def test_recv_fragmented_msg1 (dut ):
168
+ dut .expect ('websocket: Total payload length=2000, data_len=1024, current payload offset=0' )
169
+
170
+ # Test for receiving the second fragment of a large message:
171
+ # Confirms that the WebSocket can correctly handle and process the subsequent segment of a fragmented message.
172
+ def test_recv_fragmented_msg2 (dut ):
173
+ dut .expect ('websocket: Total payload length=2000, data_len=976, current payload offset=1024' )
174
+
175
+ # Test for receiving fragmented text messages:
176
+ # Checks if the WebSocket can accurately reconstruct a message sent in several smaller parts.
177
+ def test_fragmented_txt_msg (dut ):
154
178
dut .expect ('Received=' + 32 * 'a' + 32 * 'b' )
155
- print ('Fragmented data received' )
179
+ print ('\n Fragmented data received\n ' )
180
+
181
+ # Extract the hexdump portion of the log line
182
+ def parse_hexdump (line ):
183
+ match = re .search (r'\(.*\) Received binary data: ([0-9A-Fa-f ]+)' , line )
184
+ if match :
185
+ hexdump = match .group (1 ).strip ().replace (' ' , '' )
186
+ # Convert the hexdump string to a bytearray
187
+ return bytearray .fromhex (hexdump )
188
+ return bytearray ()
189
+
190
+ # Capture the binary log output from the DUT
191
+ def test_fragmented_binary_msg (dut ):
192
+ match = dut .expect (r'\(.*\) Received binary data: .*' )
193
+ if match :
194
+ line = match .group (0 ).strip ()
195
+ if isinstance (line , bytes ):
196
+ line = line .decode ('utf-8' )
197
+
198
+ # Parse the hexdump from the log line
199
+ received_data = parse_hexdump (line )
200
+
201
+ # Create the expected bytearray with the specified pattern
202
+ expected_data = bytearray ([0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 ])
203
+
204
+ # Validate the received data
205
+ assert received_data == expected_data , f'Received data does not match expected data. Received: { received_data } , Expected: { expected_data } '
206
+ print ('\n Fragmented data received\n ' )
207
+ else :
208
+ assert False , 'Log line with binary data not found'
156
209
157
210
# Starting of the test
158
211
try :
@@ -184,10 +237,12 @@ def test_fragmented_msg(dut):
184
237
dut .expect ('Please enter uri of websocket endpoint' , timeout = 30 )
185
238
dut .write (uri )
186
239
test_echo (dut )
187
- # Message length should exceed DUT's buffer size to test fragmentation, default is 1024 byte
188
240
test_recv_long_msg (dut , ws , 2000 , 3 )
189
241
test_json (dut , ws )
190
- test_fragmented_msg (dut )
242
+ test_fragmented_txt_msg (dut )
243
+ test_fragmented_binary_msg (dut )
244
+ test_recv_fragmented_msg1 (dut )
245
+ test_recv_fragmented_msg2 (dut )
191
246
test_close (dut )
192
247
else :
193
248
print ('DUT connecting to {}' .format (uri ))
0 commit comments