Example #1
0
def actual_test(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('\n')

    dut.expect_exact('Enter test for running.')
    dut.write('"start_and_stop"')
    dut.expect_unity_test_output()

    dut.expect_exact("Enter next test, or 'enter' to see menu")
    dut.write('"get_set_mac"')
    dut.expect_unity_test_output()

    dut.expect_exact("Enter next test, or 'enter' to see menu")
    with configure_eth_if() as so:
        so.settimeout(30)
        dut.write('"ethernet_broadcast_transmit"')
        pkt = so.recv(1024)
        for i in range(128, 1024):
            if pkt[i] != i & 0xff:
                raise Exception('Packet content mismatch')
    dut.expect_unity_test_output()

    dut.expect_exact("Enter next test, or 'enter' to see menu")
    dut.write('"recv_pkt"')
    res = dut.expect(
        r'([\s\S]*)'
        r'DUT MAC: ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2})'
    )
    send_eth_packet(bytes.fromhex('ffffffffffff'))  # broadcast frame # pylint: disable=no-value-for-parameter
    send_eth_packet(bytes.fromhex('010000000000'))  # multicast frame # pylint: disable=no-value-for-parameter
    send_eth_packet(bytes.fromhex(res.group(2).decode('utf-8').replace(':', '')))  # unicast fram  # pylint: disable=no-value-for-parameter, line-too-long # noqa
    dut.expect_unity_test_output(extra_before=res.group(1))
Example #2
0
def test_examples_protocol_native_ota_example(dut: Dut) -> None:
    """
    This is a positive test case, which downloads complete binary file multiple number of times.
    Number of iterations can be specified in variable iterations.
    steps: |
      1. join AP
      2. Fetch OTA image over HTTPS
      3. Reboot with the new OTA image
    """
    server_port = 8002
    # No. of times working of application to be validated
    iterations = 3
    # File to be downloaded. This file is generated after compilation
    bin_name = 'native_ota.bin'
    # start test
    host_ip = get_my_ip()
    if (get_server_status(host_ip, server_port) is False):
        thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, host_ip, server_port))
        thread1.daemon = True
        thread1.start()
    for i in range(iterations):
        dut.expect('Loaded app from partition at offset', timeout=60)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' + bin_name)
    thread1.terminate()
Example #3
0
def test_examples_protocol_pre_encrypted_ota_example(dut: Dut) -> None:
    try:
        server_port = 8001
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server, args=(dut.app.binary_path, host_ip, server_port))
        thread1.daemon = True
        thread1.start()

        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting Pre Encrypted OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':' + str(server_port) + '/' + enc_bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' + enc_bin_name)
        dut.expect('Magic Verified', timeout=30)
        dut.expect('Reading RSA private key', timeout=30)
        dut.expect('upgrade successful. Rebooting', timeout=30)
    finally:
        thread1.terminate()
Example #4
0
def test_examples_protocol_simple_ota_example_ethernet_with_spiram_config(
        dut: Dut) -> None:
    """
    steps: |
      1. join AP
      2. Fetch OTA image over HTTPS
      3. Reboot with the new OTA image
    """
    try:
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                8000))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset 0x10000', timeout=30)
        try:
            ip_address = dut.expect(r' eth ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip +
                                             ':8000/simple_ota.bin'))
        dut.write('https://' + host_ip + ':8000/simple_ota.bin')
        dut.expect('Loaded app from partition at offset 0x110000', timeout=60)
        dut.expect('Starting OTA example', timeout=30)
    finally:
        thread1.terminate()
Example #5
0
def test_examples_protocol_native_ota_example_chunked(dut: Dut) -> None:
    """
    This is a positive test case, which downloads complete binary file multiple number of times.
    Number of iterations can be specified in variable iterations.
    steps: |
      1. join AP
      2. Fetch OTA image over HTTPS
      3. Reboot with the new OTA image
    """
    # File to be downloaded. This file is generated after compilation
    bin_name = 'native_ota.bin'
    # start test
    host_ip = get_my_ip()
    chunked_server = start_chunked_server(dut.app.binary_path, 8070)
    try:
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')

        dut.expect('Starting OTA example', timeout=30)
        print('writing to device: {}'.format('https://' + host_ip + ':8070/' +
                                             bin_name))
        dut.write('https://' + host_ip + ':8070/' + bin_name)
        dut.expect('Loaded app from partition at offset', timeout=60)
        dut.expect('Starting OTA example', timeout=30)
        os.remove(os.path.join(dut.app.binary_path, 'server_cert.pem'))
        os.remove(os.path.join(dut.app.binary_path, 'server_key.pem'))
    finally:
        chunked_server.kill()
Example #6
0
def test_examples_protocol_advanced_https_ota_example_redirect_url(
        dut: Dut) -> None:
    """
    This is a positive test case, which starts a server and a redirection server.
    Redirection server redirects http_request to different port
    Number of iterations can be specified in variable iterations.
    steps: |
      1. join AP
      2. Fetch OTA image over HTTPS
      3. Reboot with the new OTA image
    """
    try:
        server_port = 8001
        # Port to which the request should be redirected
        redirection_server_port = 8081
        redirection_server_port1 = 8082
        # File to be downloaded. This file is generated after compilation
        bin_name = 'advanced_https_ota.bin'
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        thread2 = multiprocessing.Process(target=start_redirect_server,
                                          args=(dut.app.binary_path, host_ip,
                                                redirection_server_port,
                                                redirection_server_port1))
        thread2.daemon = True
        thread2.start()
        thread3 = multiprocessing.Process(target=start_redirect_server,
                                          args=(dut.app.binary_path, host_ip,
                                                redirection_server_port1,
                                                server_port))
        thread3.daemon = True
        thread3.start()
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            thread2.terminate()
            thread3.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting Advanced OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':' +
                                             str(redirection_server_port) +
                                             '/' + bin_name))
        dut.write('https://' + host_ip + ':' + str(redirection_server_port) +
                  '/' + bin_name)
        dut.expect('Loaded app from partition at offset', timeout=60)
        dut.expect('Starting Advanced OTA example', timeout=30)
    finally:
        thread1.terminate()
        thread2.terminate()
        thread3.terminate()
Example #7
0
def lightsleep_test(dut: Dut, case_name: str) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write(case_name)
    if dut.target == 'esp32c3':
        # Known issue: IDF-5003
        dut.expect(r'Returned from light sleep, reason: timer', timeout=40)
    else:
        dut.expect(r'Returned from light sleep, reason: timer', timeout=10)
Example #8
0
def test_examples_protocol_advanced_https_ota_example_invalid_chip_id(
        dut: Dut) -> None:
    """
    Working of OTA if binary file have invalid chip id is validated in this test case.
    Chip id verification should fail in this case.
    steps: |
      1. join AP
      2. Generate binary image with invalid chip id
      3. Fetch OTA image over HTTPS
      4. Check working of code for random binary file
    """
    try:
        server_port = 8001
        bin_name = 'advanced_https_ota.bin'
        # Random binary file to be generated
        random_bin_name = 'random.bin'
        random_binary_file = os.path.join(dut.app.binary_path, random_bin_name)
        # Size of random binary file. 2000 is choosen, to reduce the time required to run the test-case
        random_bin_size = 2000

        binary_file = os.path.join(dut.app.binary_path, bin_name)
        with open(binary_file, 'rb+') as f:
            data = list(f.read(random_bin_size))
        # Changing Chip id
        data[13] = 0xfe
        with open(random_binary_file, 'wb+') as fo:
            fo.write(bytearray(data))

        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting Advanced OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':' +
                                             str(server_port) + '/' +
                                             random_bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  random_bin_name)
        dut.expect(r'esp_https_ota: Mismatch chip id, expected 0, found \d',
                   timeout=10)
        try:
            os.remove(random_binary_file)
        except OSError:
            pass
    finally:
        thread1.terminate()
Example #9
0
def test_examples_protocol_advanced_https_ota_example_truncated_header(
        dut: Dut) -> None:
    """
    Working of OTA if headers of binary file are truncated is vaildated in this test case.
    Application should return with error message in this case.
    steps: |
      1. join AP
      2. Generate binary file with truncated headers
      3. Fetch OTA image over HTTPS
      4. Check working of code if headers are not sent completely
    """
    try:
        server_port = 8001
        # Original binary file generated after compilation
        bin_name = 'advanced_https_ota.bin'
        # Truncated binary file to be generated from original binary file
        truncated_bin_name = 'truncated_header.bin'
        # Size of truncated file to be generated. This value should be less than 288 bytes (Image header size)
        truncated_bin_size = 180
        # check and log bin size
        binary_file = os.path.join(dut.app.binary_path, bin_name)
        with open(binary_file, 'rb+') as f:
            with open(os.path.join(dut.app.binary_path, truncated_bin_name),
                      'wb+') as fo:
                fo.write(f.read(truncated_bin_size))

        binary_file = os.path.join(dut.app.binary_path, truncated_bin_name)
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting Advanced OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':' +
                                             str(server_port) + '/' +
                                             truncated_bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  truncated_bin_name)
        dut.expect(
            'advanced_https_ota_example: esp_https_ota_read_img_desc failed',
            timeout=30)
        try:
            os.remove(binary_file)
        except OSError:
            pass
    finally:
        thread1.terminate()
Example #10
0
def test_examples_protocol_advanced_https_ota_example_random(dut: Dut) -> None:
    """
    Working of OTA if random data is added in binary file are validated in this test case.
    Magic byte verification should fail in this case.
    steps: |
      1. join AP
      2. Generate random binary image
      3. Fetch OTA image over HTTPS
      4. Check working of code for random binary file
    """
    try:
        server_port = 8001
        # Random binary file to be generated
        random_bin_name = 'random.bin'
        # Size of random binary file. 32000 is choosen, to reduce the time required to run the test-case
        random_bin_size = 32000
        # check and log bin size
        binary_file = os.path.join(dut.app.binary_path, random_bin_name)
        with open(binary_file, 'wb+') as fo:
            # First byte of binary file is always set to zero. If first byte is generated randomly,
            # in some cases it may generate 0xE9 which will result in failure of testcase.
            fo.write(struct.pack('B', 0))
            for i in range(random_bin_size - 1):
                fo.write(struct.pack('B', random.randrange(0, 255, 1)))

        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting Advanced OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':' +
                                             str(server_port) + '/' +
                                             random_bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  random_bin_name)
        dut.expect(r'esp_https_ota: Incorrect app descriptor magic',
                   timeout=10)
        try:
            os.remove(binary_file)
        except OSError:
            pass
    finally:
        thread1.terminate()
Example #11
0
def test_examples_protocol_native_ota_example_truncated_bin(dut: Dut) -> None:
    """
    Working of OTA if binary file is truncated is validated in this test case.
    Application should return with error message in this case.
    steps: |
      1. join AP
      2. Generate truncated binary file
      3. Fetch OTA image over HTTPS
      4. Check working of code if bin is truncated
    """
    try:
        server_port = 8002
        # Original binary file generated after compilation
        bin_name = 'native_ota.bin'
        # Truncated binary file to be generated from original binary file
        truncated_bin_name = 'truncated.bin'
        # Size of truncated file to be grnerated. This value can range from 288 bytes (Image header size) to size of original binary file
        # truncated_bin_size is set to 64000 to reduce consumed by the test case
        truncated_bin_size = 64000
        # check and log bin size
        binary_file = os.path.join(dut.app.binary_path, bin_name)
        f = open(binary_file, 'rb+')
        fo = open(os.path.join(dut.app.binary_path, truncated_bin_name), 'wb+')
        fo.write(f.read(truncated_bin_size))
        fo.close()
        f.close()
        binary_file = os.path.join(dut.app.binary_path, truncated_bin_name)
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':' +
                                             str(server_port) + '/' +
                                             truncated_bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  truncated_bin_name)
        dut.expect(
            'native_ota_example: Image validation failed, image is corrupted',
            timeout=20)
        os.remove(binary_file)
    finally:
        thread1.terminate()
Example #12
0
def deepsleep_test(dut: Dut, case_name: str) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write(case_name)
    reset_reason = 'DEEPSLEEP_RESET' if dut.target == 'esp32' else 'DSLEEP'
    if dut.target == 'esp32c3':
        # Known issue: IDF-5003
        dut.expect(r'rst:.*\(%s\)' % reset_reason, timeout=40)
    elif dut.target == 'esp32c2':
        # Known issue: IDF-5003
        dut.expect(r'rst:.*\(%s\)' % reset_reason, timeout=60)
    else:
        dut.expect(r'rst:.*\(%s\)' % reset_reason, timeout=10)
Example #13
0
def test_examples_protocol_simple_ota_example_with_verify_app_signature_on_update_no_secure_boot_rsa(
        dut: Dut) -> None:
    """
    steps: |
      1. join AP
      2. Fetch OTA image over HTTPS
      3. Reboot with the new OTA image
    """
    try:
        sha256_bootloader, sha256_app = calc_all_sha256(dut)
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                8000))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset 0x20000', timeout=30)
        check_sha256(
            sha256_bootloader,
            str(dut.expect(r'SHA-256 for bootloader:\s+([a-f0-9]){64}')[0]))
        check_sha256(
            sha256_app,
            str(
                dut.expect(r'SHA-256 for current firmware:\s+([a-f0-9]){64}')
                [0]))
        try:
            ip_address = dut.expect(r' eth ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip +
                                             ':8000/simple_ota.bin'))
        dut.write('https://' + host_ip + ':8000/simple_ota.bin')
        dut.expect('Writing to partition subtype 16 at offset 0x120000',
                   timeout=20)

        dut.expect('Verifying image signature...', timeout=60)
        dut.expect('#0 app key digest == #0 trusted key digest', timeout=10)
        dut.expect('Verifying with RSA-PSS...', timeout=10)
        dut.expect('Signature verified successfully!', timeout=10)

        dut.expect('Loaded app from partition at offset 0x120000', timeout=20)
        dut.expect('Starting OTA example', timeout=30)
    finally:
        thread1.terminate()
Example #14
0
def test_examples_protocol_advanced_https_ota_example_openssl_aligned_bin(
        dut: Dut) -> None:
    """
    This is a test case for esp_http_client_read with binary size multiple of 289 bytes
    steps: |
      1. join AP
      2. Fetch OTA image over HTTPS
      3. Reboot with the new OTA image
    """
    # Original binary file generated after compilation
    bin_name = 'advanced_https_ota.bin'
    # Binary file aligned to DEFAULT_OTA_BUF_SIZE(289 bytes) boundary
    aligned_bin_name = 'aligned.bin'
    # check and log bin size
    binary_file = os.path.join(dut.app.binary_path, bin_name)
    # Original binary size
    bin_size = os.path.getsize(binary_file)
    # Dummy data required to align binary size to 289 bytes boundary
    dummy_data_size = 289 - (bin_size % 289)
    with open(binary_file, 'rb+') as f:
        with open(os.path.join(dut.app.binary_path, aligned_bin_name),
                  'wb+') as fo:
            fo.write(f.read(bin_size))
            for _ in range(dummy_data_size):
                fo.write(struct.pack('B', random.randrange(0, 255, 1)))
    # start test
    host_ip = get_my_ip()
    chunked_server = start_chunked_server(dut.app.binary_path, 8070)
    try:
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')

        dut.expect('Starting Advanced OTA example', timeout=30)

        print('writing to device: {}'.format('https://' + host_ip + ':8070/' +
                                             aligned_bin_name))
        dut.write('https://' + host_ip + ':8070/' + aligned_bin_name)
        dut.expect('Loaded app from partition at offset', timeout=60)
        dut.expect('Starting Advanced OTA example', timeout=30)
        try:
            os.remove(aligned_bin_name)
        except OSError:
            pass
    finally:
        chunked_server.kill()
Example #15
0
def test_examples_protocol_advanced_https_ota_example_partial_request(
        dut: Dut) -> None:
    """
    This is a positive test case, to test OTA workflow with Range HTTP header.
    steps: |
      1. join AP
      2. Fetch OTA image over HTTPS
      3. Reboot with the new OTA image
    """
    try:
        server_port = 8001
        # Size of partial HTTP request
        request_size = 16384
        # File to be downloaded. This file is generated after compilation
        bin_name = 'advanced_https_ota.bin'
        binary_file = os.path.join(dut.app.binary_path, bin_name)
        bin_size = os.path.getsize(binary_file)
        http_requests = int((bin_size / request_size) - 1)
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            print('ENV_TEST_FAILURE: Cannot connect to AP')
            thread1.terminate()
            raise
        dut.expect('Starting Advanced OTA example', timeout=30)

        print(
            'writing to device: {}'.format('https://' + host_ip + ':' +
                                           str(server_port) + '/' + bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  bin_name)
        for _ in range(http_requests):
            dut.expect('Connection closed', timeout=60)
        dut.expect('Loaded app from partition at offset', timeout=60)
        dut.expect('Starting Advanced OTA example', timeout=30)
    finally:
        thread1.terminate()
Example #16
0
def test_examples_protocol_advanced_https_ota_example_bluedroid_gatts(
        dut: Dut) -> None:
    """
    Run an OTA image update while a BLE GATT Server is running in background. This GATT server will be using Bluedroid Host stack.
    steps: |
      1. join AP
      2. Run BLE advertise and then GATT server.
      3. Fetch OTA image over HTTPS
      4. Reboot with the new OTA image
    """
    try:
        server_port = 8001
        # File to be downloaded. This file is generated after compilation
        bin_name = 'advanced_https_ota.bin'
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' sta ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')

        dut.expect('Starting Advanced OTA example', timeout=30)
        print(
            'writing to device: {}'.format('https://' + host_ip + ':' +
                                           str(server_port) + '/' + bin_name))
        dut.expect('Started advertising.', timeout=30)
        print('Started GAP advertising.')

        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  bin_name)
        dut.expect('Loaded app from partition at offset', timeout=60)
        dut.expect('Starting Advanced OTA example', timeout=30)
    finally:
        thread1.terminate()
Example #17
0
def test_gptimer(dut: Dut) -> None:
    dut.expect('Press ENTER to see the list of tests')
    dut.write('*')
    dut.expect_unity_test_output()
Example #18
0
def test_examples_protocol_https_request_cli_session_tickets(dut: Dut) -> None:
    logging.info("Testing for \"esp_tls client session tickets\"")

    # check and log bin size
    binary_file = os.path.join(dut.app.binary_path, 'https_request.bin')
    bin_size = os.path.getsize(binary_file)
    logging.info('https_request_bin_size : {}KB'.format(bin_size // 1024))
    # start test
    host_ip = get_my_ip()
    server_port = 8070
    server_file = os.path.join(os.path.dirname(__file__), 'main',
                               'local_server_cert.pem')
    key_file = os.path.join(os.path.dirname(__file__), 'main',
                            'local_server_key.pem')
    if (get_server_status(host_ip, server_port) is False):
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(server_file, key_file, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
    logging.info('The server started on {}:{}'.format(host_ip, server_port))

    dut.expect('Loaded app from partition at offset', timeout=30)
    try:
        ip_address = dut.expect(r' (sta|eth) ip: (\d+\.\d+\.\d+\.\d+)',
                                timeout=60)[2].decode()
        print('Connected to AP with IP: {}'.format(ip_address))
    except pexpect.exceptions.TIMEOUT:
        raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')

    dut.expect('Start https_request example', timeout=30)

    print('writing to device: {}'.format('https://' + host_ip + ':' +
                                         str(server_port)))

    dut.write('https://' + host_ip + ':' + str(server_port))
    logging.info("Testing for \"https_request using saved session\"")

    # Check for connection using already saved client session
    try:
        dut.expect('https_request to local server', timeout=30)
        dut.expect([
            'Connection established...', 'Reading HTTP response...',
            'HTTP/1.1 200 OK', 'connection closed'
        ],
                   expect_all=True)
    except Exception:
        logging.info("Failed to connect to local https server\"")
        raise

    try:
        dut.expect('https_request using saved client session', timeout=20)
        dut.expect([
            'Connection established...', 'Reading HTTP response...',
            'HTTP/1.1 200 OK', 'connection closed'
        ],
                   expect_all=True)
    except Exception:
        logging.info(
            "Failed the test for \"https_request using saved client session\"")
        raise

    logging.info(
        "Passed the test for \"https_request using saved client session\"")
    thread1.terminate()
def test_cxx_noexcept_bad_alloc(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('2')
    dut.expect_exact('abort() was called')
    dut.expect_exact('Rebooting...')
Example #20
0
def test_cxx_rtti(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('*')
    dut.expect_unity_test_output()
Example #21
0
def test_psram_esp32s3_octal(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('*')
    dut.expect_unity_test_output()
Example #22
0
def test_legacy_temp_sensor_driver(dut: Dut) -> None:
    dut.expect('Press ENTER to see the list of tests')
    dut.write('*')
    dut.expect_unity_test_output(timeout=120)
Example #23
0
def test_cxx_static_init_non_pod(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('\"can use static initializers for non-POD types\"')
    dut.expect_unity_test_output()
Example #24
0
def test_cxx_stack_smash(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('\"stack smashing protection CXX\"')
    dut.expect_exact('Stack smashing protect failure!')
    dut.expect_exact('abort() was called')
    dut.expect_exact('Rebooting...')
Example #25
0
def actual_test(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('\n')

    dut.expect_exact('Enter test for running.')
    dut.write('"start_and_stop"')
    dut.expect_unity_test_output()

    dut.expect_exact("Enter next test, or 'enter' to see menu")
    dut.write('"get_set_mac"')
    dut.expect_unity_test_output()

    dut.expect_exact("Enter next test, or 'enter' to see menu")
    with configure_eth_if() as so:
        so.settimeout(30)
        dut.write('"ethernet_broadcast_transmit"')
        eth_frame = Ether(so.recv(1024))
        for i in range(0, 1010):
            if eth_frame.load[i] != i & 0xff:
                raise Exception('Packet content mismatch')
    dut.expect_unity_test_output()

    dut.expect_exact("Enter next test, or 'enter' to see menu")
    dut.write('"recv_pkt"')
    res = dut.expect(
        r'([\s\S]*)'
        r'DUT MAC: ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2})'
    )
    send_eth_packet('ff:ff:ff:ff:ff:ff')  # broadcast frame
    send_eth_packet('01:00:00:00:00:00')  # multicast frame
    send_eth_packet(res.group(2))  # unicast frame
    dut.expect_unity_test_output(extra_before=res.group(1))

    dut.expect_exact("Enter next test, or 'enter' to see menu")
    dut.write('"start_stop_stress_test"')
    res = dut.expect(
        r'([\s\S]*)'
        r'DUT MAC: ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2})'
    )
    # Start/stop under heavy Tx traffic
    for tx_i in range(10):
        recv_resp_poke(tx_i)

    # Start/stop under heavy Rx traffic
    traffic_en = 1
    thread = Thread(target=traffic_gen,
                    args=(
                        res.group(2),
                        lambda: traffic_en,
                    ))
    thread.start()
    try:
        for rx_i in range(10):
            recv_resp_poke(rx_i)
    finally:
        traffic_en = 0
        thread.join()
    dut.expect_unity_test_output()
Example #26
0
def test_examples_protocol_advanced_https_ota_example_anti_rollback(
        dut: Dut) -> None:
    """
    Working of OTA when anti_rollback is enabled and security version of new image is less than current one.
    Application should return with error message in this case.
    steps: |
      1. join AP
      2. Generate binary file with lower security version
      3. Fetch OTA image over HTTPS
      4. Check working of anti_rollback feature
    """
    try:
        dut.serial.erase_flash()
        dut.serial.flash()
        server_port = 8001
        # Original binary file generated after compilation
        bin_name = 'advanced_https_ota.bin'
        # Modified firmware image to lower security version in its header. This is to enable negative test case
        anti_rollback_bin_name = 'advanced_https_ota_lower_sec_version.bin'
        # check and log bin size
        binary_file = os.path.join(dut.app.binary_path, bin_name)
        file_size = os.path.getsize(binary_file)
        with open(binary_file, 'rb+') as f:
            with open(
                    os.path.join(dut.app.binary_path, anti_rollback_bin_name),
                    'wb+') as fo:
                fo.write(f.read(file_size))
                # Change security_version to 0 for negative test case
                fo.seek(36)
                fo.write(b'\x00')
        binary_file = os.path.join(dut.app.binary_path, anti_rollback_bin_name)
        # start test
        host_ip = get_my_ip()
        thread1 = multiprocessing.Process(target=start_https_server,
                                          args=(dut.app.binary_path, host_ip,
                                                server_port))
        thread1.daemon = True
        thread1.start()
        # Positive Case
        dut.expect('Loaded app from partition at offset', timeout=30)
        try:
            ip_address = dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
            print('Connected to AP with IP: {}'.format(ip_address))
        except pexpect.exceptions.TIMEOUT:
            thread1.terminate()
            raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP')
        dut.expect('Starting Advanced OTA example', timeout=30)

        # Use originally generated image with secure_version=1
        print(
            'writing to device: {}'.format('https://' + host_ip + ':' +
                                           str(server_port) + '/' + bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  bin_name)
        dut.expect('Loaded app from partition at offset', timeout=60)
        dut.expect(r' (sta|eth) ip: ([^,]+),', timeout=30)
        dut.expect(r'App is valid, rollback cancelled successfully',
                   timeout=30)

        # Negative Case
        dut.expect('Starting Advanced OTA example', timeout=30)
        # Use modified image with secure_version=0
        print('writing to device: {}'.format('https://' + host_ip + ':' +
                                             str(server_port) + '/' +
                                             anti_rollback_bin_name))
        dut.write('https://' + host_ip + ':' + str(server_port) + '/' +
                  anti_rollback_bin_name)
        dut.expect(
            'New firmware security version is less than eFuse programmed, 0 < 1',
            timeout=30)
        try:
            os.remove(binary_file)
        except OSError:
            pass
    finally:
        thread1.terminate()
Example #27
0
def test_legacy_pcnt(dut: Dut) -> None:
    dut.expect_exact('Press ENTER to see the list of tests')
    dut.write('*')
    dut.expect_unity_test_output(timeout=240)
Example #28
0
def test_light_sleep(dut: Dut) -> None:

    ENTERING_SLEEP_STR = 'Entering light sleep'
    EXIT_SLEEP_REGEX = r'Returned from light sleep, reason: (\w+), t=(\d+) ms, slept for (\d+) ms'
    WAITING_FOR_GPIO_STR = r'Waiting for GPIO\d to go high...'

    WAKEUP_INTERVAL_MS = 2000

    # Ensure DTR and RTS are de-asserted for proper control of GPIO0
    dut.serial.proc.setDTR(False)
    dut.serial.proc.setRTS(False)

    # enter sleep first time
    dut.expect_exact(ENTERING_SLEEP_STR, timeout=30)
    # don't check timing here, might be cache dependent
    dut.expect(EXIT_SLEEP_REGEX)
    logging.info('Got first sleep period')

    # enter sleep second time
    dut.expect_exact(ENTERING_SLEEP_STR)
    match = dut.expect(EXIT_SLEEP_REGEX)
    logging.info(
        'Got second sleep period, wakeup from {}, slept for {}'.format(
            match.group(1), match.group(3)))
    # sleep time error should be less than 1ms
    assert (match.group(1).decode('utf8') == 'timer'
            and int(match.group(3)) >= WAKEUP_INTERVAL_MS - 1
            and int(match.group(3)) <= WAKEUP_INTERVAL_MS + 1)

    # this time we'll test gpio wakeup
    dut.expect_exact(ENTERING_SLEEP_STR)
    logging.info('Pulling GPIO0 low using DTR')
    dut.serial.proc.setDTR(True)
    time.sleep(1)
    match = dut.expect(EXIT_SLEEP_REGEX)
    logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(
        match.group(1), match.group(3)))
    assert (match.group(1).decode('utf8') == 'pin'
            and int(match.group(3)) < WAKEUP_INTERVAL_MS)

    dut.expect(WAITING_FOR_GPIO_STR)
    logging.info('Is waiting for GPIO...')

    dut.serial.proc.setDTR(False)
    dut.expect_exact(ENTERING_SLEEP_STR)
    logging.info('Went to sleep again')

    # Write 'U' to uart, 'U' in ascii is 0x55 which contains 8 edges in total
    dut.write('U')
    time.sleep(1)
    match = dut.expect(EXIT_SLEEP_REGEX)
    logging.info('Got third sleep period, wakeup from {}, slept for {}'.format(
        match.group(1), match.group(3)))
    assert (match.group(1).decode('utf8') == 'uart'
            and int(match.group(3)) < WAKEUP_INTERVAL_MS)
    logging.info('Went to sleep again')

    match = dut.expect(EXIT_SLEEP_REGEX)
    assert (match.group(1).decode('utf8') == 'timer'
            and int(match.group(3)) >= WAKEUP_INTERVAL_MS - 1
            and int(match.group(3)) <= WAKEUP_INTERVAL_MS + 1)
    logging.info('Woke up from timer again')