def test_examples_protocol_socket(env, extra_data): MESSAGE = "Data to ESP" """ steps: 1. join AP 2. have the board connect to the server 3. send and receive data """ dut1 = env.get_dut("udp_server", "examples/protocols/sockets/udp_server", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "udp_server.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("udp_server_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("udp_server_bin_size", bin_size // 1024, dut1.TARGET) # start test dut1.start_app() ipv4 = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30)[0] ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8) # expect all 8 octets from IPv6 (assumes it's printed in the long form) ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0] print("Connected with IPv4={} and IPv6={}".format(ipv4, ipv6)) # test IPv4 received = udp_client(ipv4, MESSAGE) if not received == MESSAGE: raise dut1.expect(MESSAGE) # test IPv6 received = udp_client("{}%{}".format(ipv6, INTERFACE), MESSAGE) if not received == MESSAGE: raise dut1.expect(MESSAGE)
def test_app_esp_openssl(env, extra_data): dut1 = env.get_dut("openssl_connect_test", "tools/test_apps/protocols/openssl", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "openssl_connect_test.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("openssl_connect_test_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("openssl_connect_test_bin_size_vin_size", bin_size // 1024, dut1.TARGET) dut1.start_app() esp_ip = dut1.expect( re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) print("Got IP={}".format(esp_ip[0])) ip = get_my_ip() server_port = 2222 def start_case(case, desc, negotiated_protocol, result): with TlsServer(server_port, negotiated_protocol=negotiated_protocol): print("Starting {}: {}".format(case, desc)) dut1.write("conn {} {} {}".format(ip, server_port, case)) dut1.expect(re.compile(result), timeout=10) return case
def test_examples_protocol_mqtt_wss(env, extra_data): broker_url = "" broker_port = 0 """ steps: | 1. join AP and connects to wss broker 2. Test connects a client to the same broker 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("mqtt_websocket_secure_size", bin_size // 1024, dut1.TARGET) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) broker_url = value.group(1) broker_port = int(value.group(2)) except Exception: print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') raise client = None # 1. Test connects to a broker try: client = mqtt.Client(transport="websockets") client.on_connect = on_connect client.on_message = on_message client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) print("Connecting...") client.connect(broker_url, broker_port, 60) except Exception: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) thread1.start() try: print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port)) if not event_client_connected.wait(timeout=30): raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url)) dut1.start_app() try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') raise print("Checking py-client received msg published from esp...") if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) print("Checking esp-client received msg published from py-client...") dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) finally: event_stop_client.set() thread1.join()
def test_examples_protocol_esp_http_client(env, extra_data): """ steps: | 1. join AP 2. Send HTTP request to httpbin.org """ dut1 = env.get_dut("esp_http_client", "examples/protocols/esp_http_client", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "esp-http-client-example.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("esp_http_client_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("esp_http_client_bin_size", bin_size // 1024) # start test dut1.start_app() dut1.expect("Connected to AP, begin http example", timeout=30) dut1.expect(re.compile(r"HTTP GET Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP POST Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP PUT Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP PATCH Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP DELETE Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP HEAD Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP Basic Auth Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP Basic Auth redirect Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP Digest Auth Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP Relative path redirect Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP Absolute path redirect Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTPS Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"HTTP redirect to HTTPS Status = 200, content_length = (\d)"), timeout=10) dut1.expect(re.compile(r"HTTP chunk encoding Status = 200, content_length = (-?\d)")) # content-len for chunked encoding is typically -1, could be a positive length in some cases dut1.expect(re.compile(r"HTTP Stream reader Status = 200, content_length = (\d)")) dut1.expect(re.compile(r"Last esp error code: 0x8001")) dut1.expect("Finish http example")
def test_examples_protocol_socket(env, extra_data): """ steps: 1. join AP 2. have the board connect to the server 3. send and receive data """ dut1 = env.get_dut("tcp_client", "examples/protocols/sockets/tcp_client", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "tcp_client.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("tcp_client_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("tcp_client_bin_size", bin_size // 1024, dut1.TARGET) # start test dut1.start_app() data = dut1.expect( re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) print("Connected with IPv4: {}".format(data[0])) # test IPv4 with TcpServer(PORT, socket.AF_INET): dut1.write(get_my_ip(netifaces.AF_INET)) dut1.expect(re.compile(r"OK: Message from ESP32")) # test IPv6 with TcpServer(PORT, socket.AF_INET6): dut1.write(get_my_ip(netifaces.AF_INET6)) dut1.expect(re.compile(r"OK: Message from ESP32"))
def test_examples_protocol_simple_ota_example(env, extra_data): """ steps: | 1. join AP 2. Fetch OTA image over HTTPS 3. Reboot with the new OTA image """ dut1 = env.get_dut("simple_ota_example", "examples/system/ota/simple_ota_example") # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "simple_ota.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("simple_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("simple_ota_bin_size", bin_size // 1024) # start test host_ip = get_my_ip() thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, 8000)) thread1.daemon = True thread1.start() dut1.start_app() dut1.expect("Loaded app from partition at offset 0x10000", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') thread1.close() dut1.expect("Starting OTA example", timeout=30) print("writing to device: {}".format("https://" + host_ip + ":8000/simple_ota.bin")) dut1.write("https://" + host_ip + ":8000/simple_ota.bin") dut1.expect("Loaded app from partition at offset 0x110000", timeout=60) dut1.expect("Starting OTA example", timeout=30)
def test_examples_protocol_websocket(env, extra_data): """ steps: | 1. join AP 2. connect to ws://echo.websocket.org 3. send and receive data """ dut1 = env.get_dut("websocket", "examples/protocols/websocket", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "websocket-example.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("websocket_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("websocket_bin_size", bin_size // 1024) # start test dut1.start_app() dut1.expect("Waiting for wifi ...") dut1.expect("Connection established...", timeout=30) dut1.expect("WEBSOCKET_EVENT_CONNECTED") for i in range(0, 10): dut1.expect(re.compile(r"Sending hello (\d)")) dut1.expect(re.compile(r"Received=hello (\d)")) dut1.expect("Websocket Stopped")
def test_examples_protocol_asio_tcp_server(env, extra_data): """ steps: | 1. join AP 2. Start server 3. Test connects to server and sends a test message 4. Test evaluates received test message from server 5. Test evaluates received test message on server stdout """ test_msg = b"echo message from client to server" dut1 = env.get_dut("tcp_echo_server", "examples/protocols/asio/tcp_echo_server", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "asio_tcp_echo_server.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("asio_tcp_echo_server_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("asio_tcp_echo_server_size", bin_size // 1024) # 1. start test dut1.start_app() # 2. get the server IP address data = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) # 3. create tcp client and connect to server cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) cli.settimeout(30) cli.connect((data[0], 2222)) cli.send(test_msg) data = cli.recv(1024) # 4. check the message received back from the server if (data == test_msg): print("PASS: Received correct message") pass else: print("Failure!") raise ValueError('Wrong data received from asi tcp server: {} (expected:{})'.format(data, test_msg)) # 5. check the client message appears also on server terminal dut1.expect(test_msg.decode())
def test_examples_protocol_socket_tcpclient(env, extra_data): """ steps: 1. join AP 2. have the board connect to the server 3. send and receive data """ dut1 = env.get_dut("tcp_client", "examples/protocols/sockets/tcp_client", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "tcp_client.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("tcp_client_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("tcp_client_bin_size", bin_size // 1024, dut1.TARGET) # start test dut1.start_app() ipv4 = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30)[0] ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8) # expect all 8 octets from IPv6 (assumes it's printed in the long form) ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0] print("Connected with IPv4={} and IPv6={}".format(ipv4, ipv6)) # test IPv4 with TcpServer(PORT, socket.AF_INET): server_ip = get_my_ip(netifaces.AF_INET) print("Connect tcp client to server IP={}".format(server_ip)) dut1.write(server_ip) dut1.expect(re.compile(r"OK: Message from ESP32")) # test IPv6 with TcpServer(PORT, socket.AF_INET6): server_ip = get_my_ip(netifaces.AF_INET6) print("Connect tcp client to server IP={}".format(server_ip)) dut1.write(server_ip) dut1.expect(re.compile(r"OK: Message from ESP32"))
def test_examples_protocol_advanced_https_ota_example_truncated_header( env, extra_data): """ 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 """ dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT) 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 grnerated. 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(dut1.app.binary_path, bin_name) f = open(binary_file, "rb+") fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), "wb+") fo.write(f.read(truncated_bin_size)) fo.close() f.close() binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name) bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET) # start test host_ip = get_my_ip() if (get_server_status(host_ip, server_port) is False): thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port)) thread1.daemon = True thread1.start() dut1.start_app() dut1.expect("Loaded app from partition at offset", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') dut1.expect("Starting Advanced OTA example", timeout=30) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name)) dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name) dut1.expect( "advanced_https_ota_example: esp_https_ota_read_img_desc failed", timeout=30) os.remove(binary_file)
def test_examples_protocol_socket_udpserver(env, extra_data): MESSAGE = "Data to ESP" MAX_RETRIES = 3 """ steps: 1. join AP 2. have the board connect to the server 3. send and receive data """ dut1 = env.get_dut("udp_server", "examples/protocols/sockets/udp_server", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "udp_server.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("udp_server_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("udp_server_bin_size", bin_size // 1024, dut1.TARGET) # start test dut1.start_app() ipv4 = dut1.expect( re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30)[0] ipv6_r = r':'.join( (r'[0-9a-fA-F]{4}', ) * 8 ) # expect all 8 octets from IPv6 (assumes it's printed in the long form) ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0] print("Connected with IPv4={} and IPv6={}".format(ipv4, ipv6)) dut1.expect(re.compile(r'Waiting for data'), timeout=10) # test IPv4 for _ in range(MAX_RETRIES): print('Testing UDP on IPv4...') received = udp_client(ipv4, MESSAGE) if received == MESSAGE: print('OK') break else: raise ValueError( 'IPv4: Did not receive UDP message after {} retries'.format( MAX_RETRIES)) dut1.expect(MESSAGE) # test IPv6 for _ in range(MAX_RETRIES): print('Testing UDP on IPv6...') received = udp_client('{}%{}'.format(ipv6, INTERFACE), MESSAGE) if received == MESSAGE: print('OK') break else: raise ValueError( 'IPv6: Did not receive UDP message after {} retries'.format( MAX_RETRIES)) dut1.expect(MESSAGE)
def test_examples_protocol_advanced_https_ota_example_random(env, extra_data): """ 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 """ dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT) 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(dut1.app.binary_path, random_bin_name) fo = open(binary_file, "wb+") # 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))) fo.close() bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET) # 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=(dut1.app.binary_path, host_ip, server_port)) thread1.daemon = True thread1.start() dut1.start_app() dut1.expect("Loaded app from partition at offset", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') thread1.terminate() dut1.expect('Starting Advanced OTA example', timeout=30) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name)) dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + random_bin_name) dut1.expect("esp_ota_ops: OTA image has invalid magic byte", timeout=10) os.remove(binary_file) thread1.terminate()
def test_examples_protocol_native_ota_example_truncated_bin(env, extra_data): """ 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 """ dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example", dut_class=ttfw_idf.ESP32DUT) 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(dut1.app.binary_path, bin_name) f = open(binary_file, "r+") fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), "w+") fo.write(f.read(truncated_bin_size)) fo.close() f.close() binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name) bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("native_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("native_ota_bin_size", bin_size // 1024) # start test host_ip = get_my_ip() if (get_server_status(host_ip, server_port) is False): thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port)) thread1.daemon = True thread1.start() dut1.start_app() dut1.expect("Loaded app from partition at offset", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=60) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') dut1.expect("Starting OTA example", timeout=30) print("writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name)) dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + truncated_bin_name) dut1.expect( "native_ota_example: Image validation failed, image is corrupted", timeout=20) os.remove(binary_file)
def test_examples_provisioning_ble(env, extra_data): # Acquire DUT dut1 = env.get_dut("ble_prov", "examples/provisioning/legacy/ble_prov", dut_class=ttfw_idf.ESP32DUT) # Get binary file binary_file = os.path.join(dut1.app.binary_path, "ble_prov.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("ble_prov_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("ble_prov_bin_size", bin_size // 1024, dut1.TARGET) # Upload binary and start testing dut1.start_app() # Parse BLE devname devname = dut1.expect(re.compile(r"Provisioning started with BLE devname : '(PROV_\S\S\S\S\S\S)'"), timeout=60)[0] print("BLE Device Alias for DUT :", devname) # Match additional headers sent in the request dut1.expect("BLE Provisioning started", timeout=30) print("Starting Provisioning") verbose = False protover = "V0.1" secver = 1 pop = "abcd1234" provmode = "ble" ap_ssid = "myssid" ap_password = "******" print("Getting security") security = esp_prov.get_security(secver, pop, verbose) if security is None: raise RuntimeError("Failed to get security") print("Getting transport") transport = esp_prov.get_transport(provmode, devname) if transport is None: raise RuntimeError("Failed to get transport") print("Verifying protocol version") if not esp_prov.version_match(transport, protover): raise RuntimeError("Mismatch in protocol version") print("Starting Session") if not esp_prov.establish_session(transport, security): raise RuntimeError("Failed to start session") print("Sending Wifi credential to DUT") if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password): raise RuntimeError("Failed to send Wi-Fi config") print("Applying config") if not esp_prov.apply_wifi_config(transport, security): raise RuntimeError("Failed to send apply config") if not esp_prov.wait_wifi_connected(transport, security): raise RuntimeError("Provisioning failed")
def test_wifi_throughput_basic(env, extra_data): """ steps: | 1. test TCP tx rx and UDP tx rx throughput 2. compare with the pre-defined pass standard """ pc_nic_ip = env.get_pc_nic_info('pc_nic', 'ipv4')['addr'] pc_iperf_log_file = os.path.join(env.log_path, 'pc_iperf_log.md') ap_info = { 'ssid': env.get_variable('ap_ssid'), 'password': env.get_variable('ap_password'), } # 1. get DUT dut = env.get_dut('iperf', 'examples/wifi/iperf', dut_class=ttfw_idf.ESP32DUT, app_config_name=BEST_PERFORMANCE_CONFIG) dut.start_app() dut.expect_any('iperf>', 'esp32>') # 2. preparing test_result = { 'tcp_tx': TestResult('tcp', 'tx', BEST_PERFORMANCE_CONFIG), 'tcp_rx': TestResult('tcp', 'rx', BEST_PERFORMANCE_CONFIG), 'udp_tx': TestResult('udp', 'tx', BEST_PERFORMANCE_CONFIG), 'udp_rx': TestResult('udp', 'rx', BEST_PERFORMANCE_CONFIG), } test_utility = IperfTestUtility(dut, BEST_PERFORMANCE_CONFIG, ap_info['ssid'], ap_info['password'], pc_nic_ip, pc_iperf_log_file, test_result) # 3. run test for TCP Tx, Rx and UDP Tx, Rx for _ in range(RETRY_COUNT_FOR_BEST_PERFORMANCE): test_utility.run_all_cases(0) # 4. log performance and compare with pass standard performance_items = [] for throughput_type in test_result: ttfw_idf.log_performance( '{}_throughput'.format(throughput_type), '{:.02f} Mbps'.format( test_result[throughput_type].get_best_throughput())) performance_items.append([ '{}_throughput'.format(throughput_type), '{:.02f} Mbps'.format( test_result[throughput_type].get_best_throughput()) ]) # 5. save to report TinyFW.JunitReport.update_performance(performance_items) # do check after logging, otherwise test will exit immediately if check fail, some performance can't be logged. for throughput_type in test_result: ttfw_idf.check_performance( '{}_throughput'.format(throughput_type), test_result[throughput_type].get_best_throughput(), dut.TARGET) env.close_dut('iperf')
def test_examples_protocol_advanced_https_ota_example_redirect_url( env, extra_data): """ 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 """ dut1 = env.get_dut("advanced_https_ota_example", "examples/system/ota/advanced_https_ota", dut_class=ttfw_idf.ESP32DUT) server_port = 8001 # Port to which the request should be redirecetd redirection_server_port = 8081 # File to be downloaded. This file is generated after compilation bin_name = "advanced_https_ota.bin" # check and log bin size binary_file = os.path.join(dut1.app.binary_path, bin_name) bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("advanced_https_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("advanced_https_ota_bin_size", bin_size // 1024, dut1.TARGET) # start test host_ip = get_my_ip() if (get_server_status(host_ip, server_port) is False): thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port)) thread1.daemon = True thread1.start() thread2 = Thread(target=start_redirect_server, args=(dut1.app.binary_path, host_ip, redirection_server_port, server_port)) thread2.daemon = True thread2.start() dut1.start_app() dut1.expect("Loaded app from partition at offset", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') thread1.close() thread2.close() dut1.expect("Starting Advanced OTA example", timeout=30) print("writing to device: {}".format("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name)) dut1.write("https://" + host_ip + ":" + str(redirection_server_port) + "/" + bin_name) dut1.expect("Loaded app from partition at offset", timeout=60) dut1.expect("Starting Advanced OTA example", timeout=30) dut1.reset()
def test_wifi_throughput_basic(env, extra_data): """ steps: | 1. test TCP tx rx and UDP tx rx throughput 2. compare with the pre-defined pass standard """ pc_nic_ip = env.get_pc_nic_info("pc_nic", "ipv4")["addr"] pc_iperf_log_file = os.path.join(env.log_path, "pc_iperf_log.md") ap_info = { "ssid": env.get_variable("ap_ssid"), "password": env.get_variable("ap_password"), } # 1. get DUT dut = env.get_dut("iperf", "examples/wifi/iperf", dut_class=ttfw_idf.ESP32DUT, app_config_name=BEST_PERFORMANCE_CONFIG) dut.start_app() dut.expect("esp32>") # 2. preparing test_result = { "tcp_tx": TestResult("tcp", "tx", BEST_PERFORMANCE_CONFIG), "tcp_rx": TestResult("tcp", "rx", BEST_PERFORMANCE_CONFIG), "udp_tx": TestResult("udp", "tx", BEST_PERFORMANCE_CONFIG), "udp_rx": TestResult("udp", "rx", BEST_PERFORMANCE_CONFIG), } test_utility = IperfTestUtility(dut, BEST_PERFORMANCE_CONFIG, ap_info["ssid"], ap_info["password"], pc_nic_ip, pc_iperf_log_file, test_result) # 3. run test for TCP Tx, Rx and UDP Tx, Rx for _ in range(RETRY_COUNT_FOR_BEST_PERFORMANCE): test_utility.run_all_cases(0) # 4. log performance and compare with pass standard performance_items = [] for throughput_type in test_result: ttfw_idf.log_performance( "{}_throughput".format(throughput_type), "{:.02f} Mbps".format( test_result[throughput_type].get_best_throughput())) performance_items.append([ "{}_throughput".format(throughput_type), "{:.02f} Mbps".format( test_result[throughput_type].get_best_throughput()) ]) # 5. save to report TinyFW.JunitReport.update_performance(performance_items) # do check after logging, otherwise test will exit immediately if check fail, some performance can't be logged. for throughput_type in test_result: ttfw_idf.check_performance( "{}_throughput".format(throughput_type), test_result[throughput_type].get_best_throughput()) env.close_dut("iperf")
def test_examples_blink(env, extra_data): dut = env.get_dut("blink", "examples/get-started/blink", dut_class=ttfw_idf.ESP32DUT) binary_file = os.path.join(dut.app.binary_path, "blink.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("blink_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("blink_bin_size", bin_size // 1024, dut.TARGET) dut.start_app() verify_elf_sha256_embedding(dut)
def test_example_app_ble_peripheral(env, extra_data): """ Steps: 1. Discover Bluetooth Adapter and Power On 2. Connect BLE Device 3. Read Services 4. Read Characteristics 5. Write Characteristics """ subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*']) subprocess.check_output(['hciconfig', 'hci0', 'reset']) # Acquire DUT dut = env.get_dut("bleprph", "examples/bluetooth/nimble/bleprph", dut_class=ttfw_idf.ESP32DUT) # Get binary file binary_file = os.path.join(dut.app.binary_path, "bleprph.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("bleprph_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("bleprph_bin_size", bin_size // 1024, dut.TARGET) # Upload binary and start testing Utility.console_log("Starting bleprph simple example test app") dut.start_app() dut.reset() # Get device address from dut dut_addr = dut.expect(re.compile(r"Device Address: ([a-fA-F0-9:]+)"), timeout=30)[0] exceptions_queue = Queue.Queue() # Starting a py-client in a separate thread bleprph_thread_obj = BlePrphThread(dut, dut_addr, exceptions_queue) bleprph_thread_obj.start() bleprph_thread_obj.join() exception_msg = None while True: try: exception_msg = exceptions_queue.get(block=False) except Queue.Empty: break else: Utility.console_log("\n" + exception_msg) if exception_msg: raise Exception("Thread did not run successfully") # Check dut responses dut.expect("connection established; status=0", timeout=30) dut.expect("disconnect;", timeout=30)
def test_app_protocol_mqtt_publish_connect(env, extra_data): """ steps: 1. join AP 2. connect to uri specified in the config 3. send and receive data """ dut1 = env.get_dut("mqtt_publish_connect_test", "tools/test_apps/protocols/mqtt/publish_connect_test", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish_connect_test.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_publish_connect_test_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("mqtt_publish_connect_test_bin_size_vin_size", bin_size // 1024, dut1.TARGET) # Look for test case symbolic names cases = {} try: for i in [ "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH", "CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD", "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT", "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN" ]: cases[i] = dut1.app.get_sdkconfig()[i] except Exception: print( 'ENV_TEST_FAILURE: Some mandatory test case not found in sdkconfig' ) raise dut1.start_app() esp_ip = dut1.expect( re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) print("Got IP={}".format(esp_ip[0])) # # start connection test ip = get_my_ip() set_server_cert_cn(ip) server_port = 2222 def start_case(case, desc): print("Starting {}: {}".format(case, desc)) case_id = cases[case] dut1.write("conn {} {} {}".format(ip, server_port, case_id)) dut1.expect("Test case:{} started".format(case_id)) return case_id
def test_ethernet_throughput_basic(env, _): # type: (Any, Any) -> None """ steps: | 1. test TCP tx rx and UDP tx rx throughput 2. compare with the pre-defined pass standard """ pc_nic_ip = env.get_pc_nic_info('pc_nic', 'ipv4')['addr'] pc_iperf_log_file = os.path.join(env.log_path, 'pc_iperf_log.md') # 1. get DUT dut = env.get_dut('iperf', 'examples/ethernet/iperf', dut_class=ttfw_idf.ESP32DUT) dut.start_app() dut.expect_any('iperf>', 'esp32>') # 2. preparing test_result = { 'tcp_tx': IperfUtility.TestResult('tcp', 'tx', 'ethernet'), 'tcp_rx': IperfUtility.TestResult('tcp', 'rx', 'ethernet'), 'udp_tx': IperfUtility.TestResult('udp', 'tx', 'ethernet'), 'udp_rx': IperfUtility.TestResult('udp', 'rx', 'ethernet'), } test_utility = IperfTestUtilityEth(dut, 'ethernet', pc_nic_ip, pc_iperf_log_file, test_result) # 3. run test for TCP Tx, Rx and UDP Tx, Rx test_utility.run_test('tcp', 'tx', 0, NO_BANDWIDTH_LIMIT) test_utility.run_test('tcp', 'rx', 0, NO_BANDWIDTH_LIMIT) test_utility.run_test('udp', 'tx', 0, 80) test_utility.run_test('udp', 'rx', 0, NO_BANDWIDTH_LIMIT) # 4. log performance and compare with pass standard performance_items = [] for throughput_type in test_result: ttfw_idf.log_performance( '{}_throughput'.format(throughput_type), '{:.02f} Mbps'.format( test_result[throughput_type].get_best_throughput())) performance_items.append([ '{}_throughput'.format(throughput_type), '{:.02f} Mbps'.format( test_result[throughput_type].get_best_throughput()) ]) # 5. save to report TinyFW.JunitReport.update_performance(performance_items) # do check after logging, otherwise test will exit immediately if check fail, some performance can't be logged. for throughput_type in test_result: ttfw_idf.check_performance( '{}_throughput'.format(throughput_type + '_eth'), test_result[throughput_type].get_best_throughput(), dut.TARGET) env.close_dut('iperf')
def test_examples_protocol_asio_chat_client(env, extra_data): """ steps: | 1. Test to start simple tcp server 2. `dut1` joins AP 3. Test injects server IP to `dut1`via stdin 4. Test evaluates `dut1` receives a message server placed 5. Test injects a message to `dut1` to be sent as chat_client message 6. Test evaluates received test message in host server """ global g_client_response global g_msg_to_client test_msg = "ABC" dut1 = env.get_dut("chat_client", "examples/protocols/asio/chat_client", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "asio_chat_client.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("asio_chat_client_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("asio_chat_client_size", bin_size // 1024, dut1.TARGET) # 1. start a tcp server on the host host_ip = get_my_ip() thread1 = Thread(target=chat_server_sketch, args=(host_ip, )) thread1.start() # 2. start the dut test and wait till client gets IP address dut1.start_app() dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) # 3. send host's IP to the client i.e. the `dut1` dut1.write(host_ip) # 4. client `dut1` should receive a message dut1.expect( g_msg_to_client[4:].decode() ) # Strip out the front 4 bytes of message len (see chat_message protocol) # 5. write test message from `dut1` chat_client to the server dut1.write(test_msg) while len(g_client_response) == 0: time.sleep(1) g_client_response = g_client_response.decode() print(g_client_response) # 6. evaluate host_server received this message if (g_client_response[4:7] == test_msg): print("PASS: Received correct message") pass else: print("Failure!") raise ValueError( 'Wrong data received from asi tcp server: {} (expected:{})'.format( g_client_response[4:7], test_msg)) thread1.join()
def test_examples_protocol_mdns(env, extra_data): global stop_mdns_server """ steps: | 1. join AP + init mdns example 2. get the dut host name (and IP address) 3. check the mdns name is accessible 4. check DUT output if mdns advertized host is resolved """ dut1 = env.get_dut("mdns-test", "examples/protocols/mdns", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mdns-test.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mdns-test_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("mdns-test_bin_size", bin_size // 1024, dut1.TARGET) # 1. start mdns application dut1.start_app() # 2. get the dut host name (and IP address) specific_host = dut1.expect( re.compile(r"mdns hostname set to: \[([^\]]+)\]"), timeout=30) specific_host = str(specific_host[0]) thread1 = Thread(target=mdns_server, args=(specific_host, )) thread1.start() try: dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) except DUT.ExpectTimeout: stop_mdns_server.set() thread1.join() raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') try: # 3. check the mdns name is accessible if not esp_answered.wait(timeout=30): raise ValueError( 'Test has failed: did not receive mdns answer within timeout') # 4. check DUT output if mdns advertized host is resolved dut1.expect(re.compile( r"mdns-test: Query A: tinytester.local resolved to: 127.0.0.1"), timeout=30) dut1.expect(re.compile( r"mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1" ), timeout=30) dut1.expect(re.compile( r"mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1" ), timeout=30) finally: stop_mdns_server.set() thread1.join()
def test_example_app_ble_hr(env, extra_data): """ Steps: 1. Discover Bluetooth Adapter and Power On 2. Connect BLE Device 3. Start Notifications 4. Updated value is retrieved 5. Stop Notifications """ subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*']) subprocess.check_output(['hciconfig', 'hci0', 'reset']) # Acquire DUT dut = env.get_dut("blehr", "examples/bluetooth/nimble/blehr") # Get binary file binary_file = os.path.join(dut.app.binary_path, "blehr.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("blehr_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("blehr_bin_size", bin_size // 1024) # Upload binary and start testing Utility.console_log("Starting blehr simple example test app") dut.start_app() dut.reset() # Get device address from dut dut_addr = dut.expect(re.compile(r"Device Address: ([a-fA-F0-9:]+)"), timeout=30)[0] exceptions_queue = Queue.Queue() # Starting a py-client in a separate thread blehr_thread_obj = BleHRThread(dut_addr, exceptions_queue) blehr_thread_obj.start() blehr_thread_obj.join() exception_msg = None while True: try: exception_msg = exceptions_queue.get(block=False) except Queue.Empty: break else: Utility.console_log("\n" + exception_msg) if exception_msg: raise Exception("Thread did not run successfully") # Check dut responses dut.expect("subscribe event; cur_notify=1", timeout=30) dut.expect("subscribe event; cur_notify=0", timeout=30) dut.expect("disconnect;", timeout=30)
def test_examples_protocol_native_ota_example(env, extra_data): """ 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 """ dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example", dut_class=ttfw_idf.ESP32DUT) 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" # check and log bin size binary_file = os.path.join(dut1.app.binary_path, bin_name) bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("native_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("native_ota_bin_size", bin_size // 1024, dut1.TARGET) # start test host_ip = get_my_ip() if (get_server_status(host_ip, server_port) is False): thread1 = Thread(target=start_https_server, args=(dut1.app.binary_path, host_ip, server_port)) thread1.daemon = True thread1.start() dut1.start_app() for i in range(iterations): dut1.expect("Loaded app from partition at offset", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') thread1.close() dut1.expect("Starting OTA example", timeout=30) print( "writing to device: {}".format("https://" + host_ip + ":" + str(server_port) + "/" + bin_name)) dut1.write("https://" + host_ip + ":" + str(server_port) + "/" + bin_name) dut1.expect("Loaded app from partition at offset", timeout=60) dut1.expect("Starting OTA example", timeout=30) dut1.reset()
def test_examples_protocol_native_ota_example_truncated_header( env, extra_data): """ 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 """ dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example", dut_class=ttfw_idf.ESP32DUT) # 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_header.bin" # Size of truncated file to be grnerated. 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(dut1.app.binary_path, bin_name) f = open(binary_file, "r+") fo = open(os.path.join(dut1.app.binary_path, truncated_bin_name), "w+") fo.write(f.read(truncated_bin_size)) fo.close() f.close() binary_file = os.path.join(dut1.app.binary_path, truncated_bin_name) bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("native_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("native_ota_bin_size", bin_size // 1024) # start test host_ip = get_my_ip() dut1.start_app() dut1.expect("Loaded app from partition at offset", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=60) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') dut1.expect("Starting OTA example", timeout=30) print("writing to device: {}".format("https://" + host_ip + ":8002/" + truncated_bin_name)) dut1.write("https://" + host_ip + ":8002/" + truncated_bin_name) dut1.expect("native_ota_example: received package is not fit len", timeout=20) os.remove(binary_file)
def test_examples_protocol_mqtt_qos1(env, extra_data): global msgid """ steps: (QoS1: Happy flow) 1. start the broker broker (with correctly sending ACK) 2. DUT client connects to a broker and publishes qos1 message 3. Test evaluates that qos1 message is queued and removed from queued after ACK received 4. Test the broker received the same message id evaluated in step 3 """ dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("mqtt_tcp_size", bin_size // 1024) # 1. start mqtt broker sketch host_ip = get_my_ip() thread1 = Thread(target=mqqt_server_sketch, args=(host_ip, 1883)) thread1.start() # 2. start the dut test and wait till client gets IP address dut1.start_app() # waiting for getting the IP address try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') print("writing to device: {}".format("mqtt://" + host_ip + "\n")) dut1.write("mqtt://" + host_ip + "\n") thread1.join() print("Message id received from server: {}".format(msgid)) # 3. check the message id was enqueued and then deleted msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30) msgid_deleted = dut1.expect(re.compile(r"OUTBOX: DELETED msgid=([0-9]+)"), timeout=30) # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)): print("PASS: Received correct msg id") else: print("Failure!") raise ValueError( 'Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format( msgid, msgid_enqueued, msgid_deleted))
def test_examples_protocol_mdns(env, extra_data): global g_run_server """ steps: | 1. join AP + init mdns example 2. get the dut host name (and IP address) 3. check the mdns name is accessible 4. check DUT output if mdns advertized host is resolved """ dut1 = env.get_dut("mdns-test", "examples/protocols/mdns") # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mdns-test.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mdns-test_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("mdns-test_bin_size", bin_size // 1024) # 1. start mdns application dut1.start_app() # 2. get the dut host name (and IP address) specific_host = dut1.expect( re.compile(r"mdns hostname set to: \[([^\]]+)\]"), timeout=30) specific_host = str(specific_host[0]) thread1 = Thread(target=mdns_server, args=(specific_host, )) thread1.start() try: dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) except DUT.ExpectTimeout: g_run_server = False thread1.join() raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') # 3. check the mdns name is accessible start = time.time() while (time.time() - start) <= 60: if g_done: break time.sleep(0.5) if g_done is False: raise ValueError( 'Test has failed: did not receive mdns answer within timeout') # 4. check DUT output if mdns advertized host is resolved try: dut1.expect(re.compile( r"mdns-test: Query A: tinytester.local resolved to: 127.0.0.1"), timeout=30) finally: g_run_server = False thread1.join()
def test_examples_protocol_websocket(env, extra_data): """ steps: 1. join AP 2. connect to uri specified in the config 3. send and receive data """ dut1 = env.get_dut("websocket", "examples/protocols/websocket", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "websocket-example.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("websocket_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("websocket_bin_size", bin_size // 1024, dut1.TARGET) try: if "CONFIG_WEBSOCKET_URI_FROM_STDIN" in dut1.app.get_sdkconfig(): uri_from_stdin = True else: uri = dut1.app.get_sdkconfig()["CONFIG_WEBSOCKET_URI"].strip('"') uri_from_stdin = False except Exception: print('ENV_TEST_FAILURE: Cannot find uri settings in sdkconfig') raise # start test dut1.start_app() if uri_from_stdin: server_port = 4455 with Websocket(server_port) as ws: uri = "ws://{}:{}".format(get_my_ip(), server_port) print("DUT connecting to {}".format(uri)) dut1.expect("Please enter uri of websocket endpoint", timeout=30) dut1.write(uri) test_echo(dut1) # Message length should exceed DUT's buffer size to test fragmentation, default is 1024 byte test_recv_long_msg(dut1, ws, 2000, 3) test_close(dut1) else: print("DUT connecting to {}".format(uri)) test_echo(dut1)
def test_examples_protocol_native_ota_example_random(env, extra_data): """ 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 """ dut1 = env.get_dut("native_ota_example", "examples/system/ota/native_ota_example") # 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(dut1.app.binary_path, random_bin_name) fo = open(binary_file, "w+") # 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(str(0)) for i in range(random_bin_size - 1): fo.write(str(random.randrange(0, 255, 1))) fo.close() bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("native_ota_bin_size", "{}KB".format(bin_size // 1024)) ttfw_idf.check_performance("native_ota_bin_size", bin_size // 1024) # start test host_ip = get_my_ip() dut1.start_app() dut1.expect("Loaded app from partition at offset", timeout=30) try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=60) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') dut1.expect("Starting OTA example", timeout=30) print("writing to device: {}".format("https://" + host_ip + ":8002/" + random_bin_name)) dut1.write("https://" + host_ip + ":8002/" + random_bin_name) dut1.expect("esp_ota_ops: OTA image has invalid magic byte", timeout=20) os.remove(binary_file)