pytest_blink.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: CC0-1.0
  3. import hashlib
  4. import logging
  5. import os
  6. import pytest
  7. from pytest_embedded_idf.dut import IdfDut
  8. def verify_elf_sha256_embedding(bin_path: str, sha256_reported: str) -> None:
  9. elf_file = os.path.join(bin_path, 'blink.elf')
  10. sha256 = hashlib.sha256()
  11. with open(elf_file, 'rb') as f:
  12. sha256.update(f.read())
  13. sha256_expected = sha256.hexdigest()
  14. logging.info(f'ELF file SHA256: {sha256_expected}')
  15. logging.info(f'ELF file SHA256 (reported by the app): {sha256_reported}')
  16. # the app reports only the first several hex characters of the SHA256, check that they match
  17. if not sha256_expected.startswith(sha256_reported):
  18. raise ValueError('ELF file SHA256 mismatch')
  19. @pytest.mark.supported_targets
  20. @pytest.mark.generic
  21. def test_blink(dut: IdfDut) -> None:
  22. # check and log bin size
  23. binary_file = os.path.join(dut.app.binary_path, 'blink.bin')
  24. bin_size = os.path.getsize(binary_file)
  25. logging.info('blink_bin_size : {}KB'.format(bin_size // 1024))
  26. sha256_reported = dut.expect(r'ELF file SHA256:\s+([a-f0-9]+)').group(1).decode('utf-8')
  27. verify_elf_sha256_embedding(dut.app.binary_path, sha256_reported)