@@ -1397,6 +1397,82 @@ def test_auto_detect(self):
13971397 self ._check_output (output )
13981398
13991399
1400+ class TestChipDetectionValidation (EsptoolTestCase ):
1401+ """Test the chip detection validation logic in ESPLoader.connect() method.
1402+
1403+ This tests the section that validates if the connected chip matches the
1404+ specified chip argument, covering scenarios with:
1405+ - Chips that use chip ID detection (ESP32-S3 and later)
1406+ - Chips that use magic value detection (ESP8266, ESP32, ESP32-S2)
1407+ - ESP32-S2 in Secure Download Mode (SDM)
1408+ - Correct chip argument vs wrong chip argument detection
1409+ """
1410+
1411+ def _find_different_chip (self , chip_name , detection_method ):
1412+ """Find a different chip from the specified chip name
1413+ based on the detection method, except ESP32-S2.
1414+
1415+ Args:
1416+ chip_name: The name of the chip to find a different chip for.
1417+ detection_method: The detection method to use.
1418+
1419+ Returns:
1420+ The name of the different chip.
1421+ """
1422+ for chip in esptool .CHIP_DEFS :
1423+ if chip != chip_name and chip != "esp32s2" :
1424+ if (
1425+ detection_method == "chip_id"
1426+ and not esptool .CHIP_DEFS [chip ].USES_MAGIC_VALUE
1427+ ):
1428+ if (
1429+ esptool .CHIP_DEFS [chip ].IMAGE_CHIP_ID
1430+ != esptool .CHIP_DEFS [chip_name ].IMAGE_CHIP_ID
1431+ ):
1432+ return chip
1433+ elif (
1434+ detection_method == "magic_value"
1435+ and esptool .CHIP_DEFS [chip ].USES_MAGIC_VALUE
1436+ ):
1437+ if (
1438+ esptool .CHIP_DEFS [chip ].MAGIC_VALUE
1439+ != esptool .CHIP_DEFS [chip_name ].MAGIC_VALUE
1440+ ):
1441+ return chip
1442+
1443+ @pytest .mark .quick_test
1444+ def test_chips_with_chip_id_detection (self ):
1445+ # First verify the correct chip works
1446+ output = self .run_esptool (f"--chip { arg_chip } flash-id" )
1447+ assert "Stub flasher running." in output
1448+
1449+ # Find a different chip with different chip ID to test mismatch detection
1450+ different_chip_id = self ._find_different_chip (arg_chip , "chip_id" )
1451+ error_output = self .run_esptool_error (f"--chip { different_chip_id } flash-id" )
1452+ assert (
1453+ f"This chip is { arg_chip .upper ()} , not { different_chip_id .upper ()} ."
1454+ in error_output
1455+ or "Wrong chip argument?" in error_output
1456+ )
1457+
1458+ # Find a different chip with different magic value to test mismatch detection
1459+ different_chip_magic = self ._find_different_chip (arg_chip , "magic_value" )
1460+ error_output = self .run_esptool_error (f"--chip { different_chip_magic } flash-id" )
1461+ assert (
1462+ f"This chip is { arg_chip .upper ()} , not { different_chip_magic .upper ()} ."
1463+ in error_output
1464+ or "Wrong chip argument?" in error_output
1465+ )
1466+
1467+ if arg_chip != "esp32s2" :
1468+ # ESP32-S2 is special case that has security info, but not chip ID
1469+ error_output = self .run_esptool_error ("--chip esp32s2 flash-id" )
1470+ assert (
1471+ f"This chip is { arg_chip .upper ()} , not ESP32-S2." in error_output
1472+ or "Wrong chip argument?" in error_output
1473+ )
1474+
1475+
14001476class TestUSBMode (EsptoolTestCase ):
14011477 @pytest .mark .quick_test
14021478 def test_usb_mode (self ):
0 commit comments