1#!/usr/bin/env python
2
3from __future__ import division, print_function, unicode_literals
4
5import hashlib
6import os
7import re
8
9import ttfw_idf
10from tiny_test_fw import Utility
11
12
13def verify_elf_sha256_embedding(dut):
14    elf_file = os.path.join(dut.app.binary_path, 'blink.elf')
15    sha256 = hashlib.sha256()
16    with open(elf_file, 'rb') as f:
17        sha256.update(f.read())
18    sha256_expected = sha256.hexdigest()
19
20    dut.reset()
21    sha256_reported = dut.expect(re.compile(r'ELF file SHA256:\s+([a-f0-9]+)'), timeout=5)[0]
22
23    Utility.console_log('ELF file SHA256: %s' % sha256_expected)
24    Utility.console_log('ELF file SHA256 (reported by the app): %s' % sha256_reported)
25    # the app reports only the first several hex characters of the SHA256, check that they match
26    if not sha256_expected.startswith(sha256_reported):
27        raise ValueError('ELF file SHA256 mismatch')
28
29
30@ttfw_idf.idf_example_test(env_tag='Example_GENERIC')
31def test_examples_blink(env, extra_data):
32    dut = env.get_dut('blink', 'examples/get-started/blink', dut_class=ttfw_idf.ESP32DUT)
33    binary_file = os.path.join(dut.app.binary_path, 'blink.bin')
34    bin_size = os.path.getsize(binary_file)
35    ttfw_idf.log_performance('blink_bin_size', '{}KB'.format(bin_size // 1024))
36
37    dut.start_app()
38
39    verify_elf_sha256_embedding(dut)
40
41
42if __name__ == '__main__':
43    test_examples_blink()
44