88
99
1010def test_message ():
11- # Messages are built from a message_type (bytes) a payload (bytes) an a optional list of TLVRecords
11+ # Messages are built from a message_type (bytes) a payload (bytes), and an optional list of TLVRecords
1212 mtype = b"\x00 "
1313 payload = b"\x00 \x01 \x02 "
1414 extension = []
1515 m = Message (mtype , payload , extension )
1616 assert isinstance (m , Message )
17- assert m .type == mtype and m .payload == payload and m .extension is None
17+ assert m .type == mtype and m .payload == payload and m .extension == []
1818
1919 # Same with some tlvs
2020 extension = [TLVRecord (), NetworksTLV ()]
@@ -43,7 +43,7 @@ def test_message_wrong_types():
4343
4444def test_message_from_bytes ():
4545 # From bytes builds an instance of a children class as long as the type is known, raises ValueError otherwise
46- # Not testing particular cases for the children since they will be covered in their own tests
46+ # Not testing particular cases for the child classes since they will be covered in their own tests
4747
4848 # Init
4949 m = b"\x00 \x10 \x00 \x00 \x00 \x00 "
@@ -77,7 +77,7 @@ def test_message_from_bytes_wrong():
7777
7878
7979def test_message_serialize ():
80- # Serialize returns the concatenation opf the byte representation of each field:
80+ # Serialize returns the concatenation of the byte representation of each field:
8181 # type + payload + [extension]
8282
8383 # No extension
@@ -162,7 +162,7 @@ def test_init_message_from_bytes_wrong():
162162
163163 # Message is not long enough < 6
164164 with pytest .raises (ValueError , match = "message be must at least 6-byte long" ):
165- InitMessage .from_bytes (b"\x00 \x10 \x00 " )
165+ InitMessage .from_bytes (b"\x00 \x10 \x00 \x01 \x02 " )
166166
167167 # Type is not init
168168 with pytest .raises (ValueError , match = "Wrong message format. types do not match" ):
@@ -179,7 +179,7 @@ def test_error_message():
179179 em = ErrorMessage (cid )
180180 assert isinstance (em , ErrorMessage )
181181 assert em .channel_id == cid
182- assert em .data is None
182+ assert not em .data
183183
184184 # Same with associated data
185185 data = "error message data"
@@ -191,7 +191,7 @@ def test_error_message():
191191
192192def test_error_message_wrong ():
193193 # Channel id must be a 32-byte hex str
194- # Data must be string if set and no longer than the message cap size when encoded pow(2, 16)
194+ # Data must be string if set and no longer than the message cap size when encoded pow(2, 16) - 1
195195
196196 # Wrong channel id
197197 with pytest .raises (ValueError , match = "channel_id must be a 256-bit hex string" ):
@@ -205,8 +205,8 @@ def test_error_message_wrong():
205205 ErrorMessage (get_random_value_hex (32 ), b"message" )
206206
207207 # Data too long
208- with pytest .raises (ValueError , match = f"Encoded data length cannot be bigger than { pow (2 , 16 )} " ):
209- ErrorMessage (get_random_value_hex (32 ), "A" * (pow (2 , 16 ) + 1 ))
208+ with pytest .raises (ValueError , match = f"Encoded data length cannot be bigger than { pow (2 , 16 ) - 1 } " ):
209+ ErrorMessage (get_random_value_hex (32 ), "A" * (pow (2 , 16 )))
210210
211211
212212def test_error_from_bytes ():
@@ -218,7 +218,7 @@ def test_error_from_bytes():
218218 em = ErrorMessage .from_bytes (mtype + cid + data_len )
219219 assert isinstance (em , ErrorMessage )
220220 assert em .channel_id == cid .hex ()
221- assert em .data is None
221+ assert not em .data
222222
223223 # Same with associated data
224224 data = "message"
@@ -236,7 +236,7 @@ def test_error_from_bytes_wrong():
236236
237237 # Message is not long enough < 36
238238 with pytest .raises (ValueError , match = "message be must at least 36-byte long" ):
239- ErrorMessage .from_bytes (b"\x00 \x11 \x00 \x01 " )
239+ ErrorMessage .from_bytes (b"\x00 \x11 " + bytes ( 33 ) )
240240
241241 # Type is not error
242242 with pytest .raises (ValueError , match = "Wrong message format. types do not match" ):
@@ -252,7 +252,7 @@ def test_error_from_bytes_wrong():
252252
253253
254254def test_ping_message ():
255- # Ping expects a number of pong bytes and optionally a some ignored data (bytes)
255+ # Ping expects a number of pong bytes and optionally some ignored data (bytes)
256256 num_pong_bytes = 10
257257 pm = PingMessage (num_pong_bytes )
258258 assert isinstance (pm , PingMessage )
@@ -291,7 +291,7 @@ def test_ping_message_from_bytes():
291291 pm = PingMessage .from_bytes (mtype + num_pong_bytes + bytes_len )
292292 assert isinstance (pm , PingMessage )
293293 assert pm .num_pong_bytes == int .from_bytes (num_pong_bytes , "big" )
294- assert pm .ignored_bytes is None
294+ assert not pm .ignored_bytes
295295
296296 # Same with some ignored data
297297 ignored_data = b"\x00 \x01 \x02 \x03 "
@@ -309,7 +309,7 @@ def test_ping_message_from_bytes_wrong():
309309
310310 # Message is not long enough < 6
311311 with pytest .raises (ValueError , match = "message be must at least 6-byte long" ):
312- PingMessage .from_bytes (b"\x00 \x12 \x00 \x01 " )
312+ PingMessage .from_bytes (b"\x00 \x12 \x00 \x01 \x02 " )
313313
314314 # Type is not ping
315315 with pytest .raises (ValueError , match = "Wrong message format. types do not match" ):
@@ -325,10 +325,10 @@ def test_ping_message_from_bytes_wrong():
325325
326326
327327def test_pong_message ():
328- # Pong can be empty, and optionally can receive so ignored bytes
328+ # Pong can be empty, and optionally can receive some ignored bytes
329329 pm = PongMessage ()
330330 assert isinstance (pm , PongMessage )
331- assert pm .ignored_bytes is None
331+ assert not pm .ignored_bytes
332332
333333 # With some ignored_bytes
334334 ignored_bytes = b"\x00 \x02 \x06 "
@@ -353,7 +353,7 @@ def test_pong_message_from_bytes():
353353 bytes_len = b"\x00 \x00 "
354354 pm = PongMessage .from_bytes (mtype + bytes_len )
355355 assert isinstance (pm , PongMessage )
356- assert pm .ignored_bytes is None
356+ assert not pm .ignored_bytes
357357
358358 # Add some ignored data
359359 ignored_data = b"\x03 \xfd \xef "
0 commit comments