|
6 | 6 | import errno
|
7 | 7 | import filecmp
|
8 | 8 | import os
|
| 9 | +import random |
9 | 10 | import re
|
10 | 11 | import socket
|
11 | 12 | import subprocess
|
@@ -267,7 +268,7 @@ def test_auto_color_advanced(self):
|
267 | 268 | @pytest.mark.skipif(os.name == 'nt', reason='Linux/MacOS only')
|
268 | 269 | def test_rfc2217(self, rfc2217: str):
|
269 | 270 | """Run monitor with RFC2217 port"""
|
270 |
| - # run with no reset because it is not supported for socker ports |
| 271 | + # run with no reset because it is not supported for socket ports |
271 | 272 | input_file = 'in1.txt'
|
272 | 273 | out, err = self.run_monitor(['--no-reset'], input_file, custom_port=rfc2217)
|
273 | 274 | with open(err, 'r') as f:
|
@@ -375,6 +376,42 @@ def test_binary_logging(self):
|
375 | 376 | log_clean
|
376 | 377 | ), "Expected address, 'app_main', and 'main.c:<line>' in the output"
|
377 | 378 |
|
| 379 | + @pytest.fixture |
| 380 | + def invalid_binary_log(self): |
| 381 | + with NamedTemporaryFile(delete=False) as f: |
| 382 | + f.write(b'I (1) main: Starting\r\n') |
| 383 | + # Binary log detection trigger |
| 384 | + f.write(b'\x01') |
| 385 | + # Corrupted/invalid binary log data that would cause stuck behavior |
| 386 | + # The max length of the binary log frame is 1023 bytes so just to be sure we write more |
| 387 | + f.write(b'\x01' + random.randbytes(1024)) |
| 388 | + # Text after binary log (should be processed normally) |
| 389 | + f.write(b'I (1000) main: Application started\r\n') |
| 390 | + # Add some valid binary log data frame from inputs/binlog |
| 391 | + # Should be decoded as "I (259) example: >>> String Formatting Tests <<<" |
| 392 | + f.write(b'\x02\x0c\x10\x00\x00\x85\x9c\x3f\x40\x09\x9c\x00\x00\x01\x03\xd6') |
| 393 | + |
| 394 | + yield f.name |
| 395 | + os.unlink(f.name) |
| 396 | + |
| 397 | + def test_binary_log_invalid_data(self, invalid_binary_log: str): |
| 398 | + """Test the binary log with invalid data to make sure it is processed normally and not stuck""" |
| 399 | + args = [os.path.join(IN_DIR, 'log.elf')] |
| 400 | + out, err = self.run_monitor(args, invalid_binary_log, timeout=15) |
| 401 | + print('Using binary log file: ', invalid_binary_log) |
| 402 | + with open(err, 'r') as f_err: |
| 403 | + stderr = f_err.read() |
| 404 | + assert 'Stopping condition has been received' in stderr |
| 405 | + |
| 406 | + # Verify that monitor didn't get stuck and processed all data; ignore errors because we are using random data |
| 407 | + with open(out, 'r', errors='ignore') as f_out: |
| 408 | + output = f_out.read() |
| 409 | + # Should contain messages from both before and after invalid binary log processing |
| 410 | + assert 'I (1) main: Starting' in output |
| 411 | + assert 'I (1000) main: Application started' in output |
| 412 | + # Valid binary log data frame should be decoded |
| 413 | + assert 'I (259) example: >>> String Formatting Tests <<<' in output |
| 414 | + |
378 | 415 |
|
379 | 416 | @pytest.mark.skipif(os.name == 'nt', reason='Linux/MacOS only')
|
380 | 417 | class TestConfig(TestBaseClass):
|
|
0 commit comments