"""Programmer reports must reject real TD errors even with success markers.""" from contextlib import redirect_stdout import io from pathlib import Path import tempfile import unittest from unittest.mock import patch from tools import remote_flash_cpld as flash class ProgrammerErrorLogTests(unittest.TestCase): def test_actual_program_done_failure_overrides_success_marker(self): log = ('PRG-2014 : Chip validation success: EF2L45\n' 'PRG-9507 ERROR: line 2361 read = 0x00 ref = 0x01\n' 'PRG-9524 ERROR: Program done check failed!\n' 'RUN-1003 : finish command "download -mode jtag"\n' 'PROD2U_SRAM_OK\n') self.assertFalse(flash.td_result_ok(0, log, '', 'PROD2U_SRAM_OK')) self.assertEqual(len(flash.td_error_lines(log, '')), 2) def test_errors_in_either_stream_cannot_pass_burn_or_sram(self): failures = ['PRG-9505 USB device open error', 'ERROR: download failed', 'FATAL: connection lost', 'RUN-9000 ERROR: execution failed'] with tempfile.TemporaryDirectory() as directory: bit = Path(directory) / 'fixture.bit' bit.write_bytes(b'not a real firmware') bit.with_suffix('.bmk').write_bytes(b'fixture mask') for command, marker in [(flash.cmd_sram, 'PROD2U_SRAM_VERIFIED'), (flash.cmd_burn, 'PROD2U_FLASH_PROGRAMMED_VERIFIED')]: for error in failures: for stderr in (False, True): stdout = marker + '\n' + ('' if stderr else error) err = error if stderr else '' with self.subTest(command=command.__name__, error=error, stderr=stderr), \ patch.object(flash, 'run_tcl', return_value=(0, stdout, err)), \ redirect_stdout(io.StringIO()): self.assertEqual(command(str(bit)), 1) def test_error_free_completion_with_nonfatal_warning_passes(self): log = ('PRG-2014 : Chip validation success: EF2L45\n' 'PRG-1000 : Detect risk, will automatic deceleration when reading spi flash data.\n' 'RUN-1003 : finish command "download -mode jtag"\nPROD2U_SRAM_OK\n') self.assertTrue(flash.td_result_ok(0, log, '', 'PROD2U_SRAM_OK')) self.assertFalse(flash.td_result_ok(1, log, '', 'PROD2U_SRAM_OK')) self.assertFalse(flash.td_result_ok(0, 'puts "PROD2U_SRAM_OK"\n', '', 'PROD2U_SRAM_OK')) def test_device_mention_in_error_output_is_not_detection(self): class Result: stdout = 'Bus 002 Device 006: ID 336c:1002 Anlogic USB-JTAG-Cable\n' for log in ['PRG-9505 ERROR: EF2L45 device open error\n', 'DEVICE_DETECTED: EF2L45\nPRG-9507 ERROR: bad ID\n']: with patch.object(flash.subprocess, 'run', return_value=Result()), \ patch.object(flash, 'run_tcl', return_value=(0, log, '')), \ redirect_stdout(io.StringIO()): self.assertEqual(flash.cmd_check(), 1) def test_sram_requires_real_vector_generation_and_readback_at_200k(self): with tempfile.TemporaryDirectory() as directory: bit = Path(directory) / 'fixture.bit' bit.write_bytes(b'fixture') bit.with_suffix('.bmk').write_bytes(b'mask') with patch.object(flash, 'run_tcl', return_value=(0, 'PROD2U_SRAM_VERIFIED\n', '')) as run, redirect_stdout(io.StringIO()): self.assertEqual(flash.cmd_sram(str(bit)), 0) tcl = run.call_args.args[0] self.assertIn('download -mode jtag -spd 3 -cable 0', tcl) self.assertIn('download -mode x_rdbk -spd 3 -cable 0', tcl) self.assertEqual(tcl.count(' -vec '), 2) with patch.object(flash, 'run_tcl', return_value=(0, 'PROD2U_SRAM_PROGRAMMED\n', '')), redirect_stdout(io.StringIO()): self.assertEqual(flash.cmd_sram(str(bit)), 1) def test_missing_sram_mask_does_not_program(self): with tempfile.TemporaryDirectory() as directory: bit = Path(directory) / 'fixture.bit' bit.write_bytes(b'fixture') with patch.object(flash, 'run_tcl') as run, redirect_stdout(io.StringIO()): self.assertEqual(flash.cmd_sram(str(bit)), 1) run.assert_not_called() def test_flash_stages_are_real_verified_and_stop_on_error(self): markers = ['PROD2U_FLASH_PROGRAMMED_VERIFIED', 'PROD2U_FLASH_REFRESHED', 'PROD2U_FLASH_BOOT_VERIFIED'] modes = ['program_spi -v', 'refresh', 'x_rdbk'] with tempfile.TemporaryDirectory() as directory: bit = Path(directory) / 'fixture.bit' bit.write_bytes(b'fixture') with patch.object(flash, 'run_tcl') as run, redirect_stdout(io.StringIO()): self.assertEqual(flash.cmd_burn(str(bit)), 1) run.assert_not_called() bit.with_suffix('.bmk').write_bytes(b'mask') good = [(0, marker + '\n', '') for marker in markers] with patch.object(flash, 'run_tcl', side_effect=good) as run, redirect_stdout(io.StringIO()): self.assertEqual(flash.cmd_burn(str(bit)), 0) for call, mode in zip(run.call_args_list, modes): self.assertIn('download -mode ' + mode + ' -spd 3 -cable 0', call.args[0]) self.assertIn(' -vec ', call.args[0]) for failing in range(len(modes)): bad = good[:failing] + [(0, markers[failing] + '\nPRG-9507 ERROR: mismatch\n', '')] with patch.object(flash, 'run_tcl', side_effect=bad) as run, redirect_stdout(io.StringIO()): self.assertEqual(flash.cmd_burn(str(bit)), 1) self.assertEqual(run.call_count, failing + 1) if __name__ == '__main__': unittest.main()