@@ -249,7 +249,7 @@ async def test_wave_hello_validates_chip_not_board_profile_token(self):
249249 import target_smoke
250250 self .backend .binding ["profile_id" ] = "waveshare-esp32-s3-lcd-147b"
251251 central = SimpleNamespace (send_cmd = mock .AsyncMock (return_value = SimpleNamespace (payload = b"\0 " )),
252- disconnect = mock .AsyncMock ())
252+ disconnect = mock .AsyncMock (), is_connected = False )
253253 with mock .patch .object (HARDWARE , "_usb_exact" ), \
254254 mock .patch .object (HARDWARE .importlib .metadata , "version" , return_value = "3.0.2" ), \
255255 mock .patch .object (_pble_central .PbleCentral , "connect" , return_value = central ), \
@@ -373,6 +373,58 @@ async def bounded(awaitable, timeout):
373373 self .assertTrue (bounded_cleanup , "BLE teardown never used a bounded wait" )
374374 self .assertTrue (all (0 < seconds <= 5 for seconds in bounded_cleanup ))
375375
376+ def rom_context (self , value , rom ):
377+ adapter = HARDWARE ._EspAdapter (value , self .root , mock .Mock ())
378+ adapter ._check = mock .Mock ()
379+ tool_chip = {"esp32-4mb" : "esp32" , "esp32-s3-n16r8" : "esp32s3" }[value ["profile_id" ]]
380+ factory = mock .Mock (return_value = rom )
381+ tool = SimpleNamespace (detect_chip = mock .Mock (side_effect = AssertionError ("autodetection is not permitted" )))
382+ self .enterContext (mock .patch .dict (sys .modules , {
383+ "esptool" : tool ,
384+ "esptool.config" : SimpleNamespace (load_config_file = lambda : (None , None )),
385+ "esptool.targets" : SimpleNamespace (CHIP_DEFS = {tool_chip : factory })}))
386+ self .enterContext (mock .patch .object (HARDWARE .importlib .metadata , "version" ,
387+ side_effect = lambda name : {"esptool" : "4.12.0" , "pyserial" : "3.5" }[name ]))
388+ self .enterContext (mock .patch .object (HARDWARE , "_usb_exact" ))
389+ return adapter , factory , tool
390+
391+ async def test_expected_rom_is_retained_before_exactly_one_connect_and_no_autodetection (self ):
392+ rom = SimpleNamespace (connect = mock .Mock (side_effect = OSError ("single connect stopped" )))
393+ value = binding ()
394+ adapter , factory , tool = self .rom_context (value , rom )
395+ with self .assertRaisesRegex (OSError , "single connect stopped" ):
396+ adapter ._connect ()
397+ self .assertIs (adapter .esp , rom )
398+ factory .assert_called_once_with (value ["loader_usb" ]["port" ], 115200 )
399+ rom .connect .assert_called_once_with ("default_reset" , attempts = 1 , detecting = True )
400+ tool .detect_chip .assert_not_called ()
401+
402+ async def test_modern_rom_actual_chip_id_must_match_not_only_selected_class_constants (self ):
403+ rom = SimpleNamespace (connect = mock .Mock (), IS_STUB = False , sync_stub_detected = False ,
404+ CHIP_NAME = "ESP32-S3" , IMAGE_CHIP_ID = 9 ,
405+ get_chip_id = mock .Mock (return_value = 5 ), read_mac = mock .Mock ())
406+ adapter , _factory , tool = self .rom_context (binding (), rom )
407+ with self .assertRaisesRegex (ValueError , "ROM chip ID" ):
408+ adapter ._connect ()
409+ rom .get_chip_id .assert_called_once_with ()
410+ rom .read_mac .assert_not_called ()
411+ tool .detect_chip .assert_not_called ()
412+
413+ async def test_classic_rom_uses_actual_magic_without_unsupported_security_info_probe (self ):
414+ value = binding ()
415+ value ["profile_id" ] = "esp32-4mb"
416+ value ["flash" ].update (chip = "ESP32" , chip_id = 0 , size_bytes = 0x400000 , jedec_id = 0x164068 )
417+ rom = SimpleNamespace (connect = mock .Mock (), IS_STUB = False , sync_stub_detected = False ,
418+ CHIP_NAME = "ESP32" , IMAGE_CHIP_ID = 0 , CHIP_DETECT_MAGIC_REG_ADDR = 0x40001000 ,
419+ read_reg = mock .Mock (return_value = 0 ), get_chip_id = mock .Mock (), read_mac = mock .Mock ())
420+ adapter , _factory , tool = self .rom_context (value , rom )
421+ with self .assertRaisesRegex (ValueError , "classic ROM magic" ):
422+ adapter ._connect ()
423+ rom .read_reg .assert_called_once_with (0x40001000 )
424+ rom .get_chip_id .assert_not_called ()
425+ rom .read_mac .assert_not_called ()
426+ tool .detect_chip .assert_not_called ()
427+
376428
377429if __name__ == "__main__" :
378430 unittest .main (verbosity = 2 )
0 commit comments