1import os
2import re
3
4import ttfw_idf
5from tiny_test_fw import Utility
6
7
8@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1')
9def test_examples_protocol_https_request(env, extra_data):
10    """
11    steps: |
12      1. join AP
13      2. establish TLS connection to www.howsmyssl.com:443 with multiple
14         certificate verification options
15      3. send http request
16    """
17    dut1 = env.get_dut('https_request', 'examples/protocols/https_request', dut_class=ttfw_idf.ESP32DUT)
18    # check and log bin size
19    binary_file = os.path.join(dut1.app.binary_path, 'https_request.bin')
20    bin_size = os.path.getsize(binary_file)
21    ttfw_idf.log_performance('https_request_bin_size', '{}KB'.format(bin_size // 1024))
22    # start tes
23    Utility.console_log('Starting https_request simple test app')
24    dut1.start_app()
25
26    # Check for connection using crt bundle
27    Utility.console_log("Testing for \"https_request using crt bundle\"")
28    try:
29        dut1.expect(re.compile('https_request using crt bundle'), timeout=30)
30        dut1.expect_all('Certificate validated',
31                        'Connection established...',
32                        'Reading HTTP response...',
33                        'HTTP/1.1 200 OK',
34                        re.compile('connection closed'))
35    except Exception:
36        Utility.console_log("Failed the test for \"https_request using crt bundle\"")
37        raise
38    Utility.console_log("Passed the test for \"https_request using crt bundle\"")
39
40    # Check for connection using cacert_buf
41    Utility.console_log("Testing for \"https_request using cacert_buf\"")
42    try:
43        dut1.expect(re.compile('https_request using cacert_buf'), timeout=20)
44        dut1.expect_all('Connection established...',
45                        'Reading HTTP response...',
46                        'HTTP/1.1 200 OK',
47                        re.compile('connection closed'))
48    except Exception:
49        Utility.console_log("Passed the test for \"https_request using cacert_buf\"")
50        raise
51    Utility.console_log("Passed the test for \"https_request using cacert_buf\"")
52
53    # Check for connection using global ca_store
54    Utility.console_log("Testing for \"https_request using global ca_store\"")
55    try:
56        dut1.expect(re.compile('https_request using global ca_store'), timeout=20)
57        dut1.expect_all('Connection established...',
58                        'Reading HTTP response...',
59                        'HTTP/1.1 200 OK',
60                        re.compile('connection closed'))
61    except Exception:
62        Utility.console_log("Failed the test for \"https_request using global ca_store\"")
63        raise
64    Utility.console_log("Passed the test for \"https_request using global ca_store\"")
65
66    # Check for connection using already saved client session
67    Utility.console_log("Testing for \"https_request using saved client session\"")
68    try:
69        dut1.expect(re.compile('https_request using saved client session'), timeout=20)
70        dut1.expect_all('Connection established...',
71                        'Reading HTTP response...',
72                        'HTTP/1.1 200 OK',
73                        re.compile('connection closed'))
74    except Exception:
75        Utility.console_log("Failed the test for \"https_request using saved client session\"")
76        raise
77    Utility.console_log("Passed the test for \"https_request using saved client session\"")
78
79    # Check for connection using crt bundle with mbedtls dynamic resource enabled
80    dut1 = env.get_dut('https_request', 'examples/protocols/https_request', dut_class=ttfw_idf.ESP32DUT, app_config_name='ssldyn')
81    # check and log bin size
82    binary_file = os.path.join(dut1.app.binary_path, 'https_request.bin')
83    bin_size = os.path.getsize(binary_file)
84    ttfw_idf.log_performance('https_request_bin_size', '{}KB'.format(bin_size // 1024))
85    # start test
86    dut1.start_app()
87    # only check if one connection is established
88    Utility.console_log("Testing for \"https_request using crt bundle\" with mbedtls dynamic resource enabled")
89    try:
90        dut1.expect(re.compile('https_request using crt bundle'), timeout=30)
91        dut1.expect_all('Connection established...',
92                        'Reading HTTP response...',
93                        'HTTP/1.1 200 OK',
94                        re.compile('connection closed'))
95    except Exception:
96        Utility.console_log("Failed the test for \"https_request using crt bundle\" when mbedtls dynamic resource was enabled")
97        raise
98    Utility.console_log("Passed the test for \"https_request using crt bundle\" when mbedtls dynamic resource was enabled")
99
100
101if __name__ == '__main__':
102    test_examples_protocol_https_request()
103