pytest_http_server_simple.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import logging
  6. import os
  7. import random
  8. import socket
  9. import string
  10. import sys
  11. import threading
  12. import time
  13. import pytest
  14. try:
  15. from idf_http_server_test import client
  16. except ModuleNotFoundError:
  17. sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'tools', 'ci', 'python_packages'))
  18. from idf_http_server_test import client
  19. from common_test_methods import get_env_config_variable
  20. from pytest_embedded import Dut
  21. class http_client_thread(threading.Thread):
  22. def __init__(self, ip: str, port: int, delay: int) -> None:
  23. threading.Thread.__init__(self)
  24. self.ip = ip
  25. self.port = port
  26. self.delay = delay
  27. self.exc = 0
  28. # Thread function used to open a socket and wait for specific amount of time before returning
  29. def open_connection(self, ip: str, port: int, delay: int) -> None:
  30. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  31. s.settimeout(delay)
  32. s.connect((ip, port))
  33. time.sleep(delay)
  34. def run(self) -> None:
  35. try:
  36. self.open_connection(self.ip, self.port, self.delay)
  37. except socket.timeout:
  38. self.exc = 1
  39. def join(self, timeout=None): # type: ignore
  40. threading.Thread.join(self)
  41. if self.exc:
  42. raise socket.timeout
  43. # When running on local machine execute the following before running this script
  44. # > make app bootloader
  45. # > make print_flash_cmd | tail -n 1 > build/download.config
  46. @pytest.mark.esp32
  47. @pytest.mark.esp32c3
  48. @pytest.mark.esp32s2
  49. @pytest.mark.esp32s3
  50. @pytest.mark.wifi_router
  51. def test_examples_protocol_http_server_simple(dut: Dut) -> None:
  52. # Get binary file
  53. binary_file = os.path.join(dut.app.binary_path, 'simple.bin')
  54. bin_size = os.path.getsize(binary_file)
  55. logging.info('http_server_bin_size : {}KB'.format(bin_size // 1024))
  56. # Upload binary and start testing
  57. logging.info('Starting http_server simple test app')
  58. # Parse IP address of STA
  59. logging.info('Waiting to connect with AP')
  60. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  61. dut.expect('Please input ssid password:')
  62. env_name = 'wifi_router'
  63. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  64. ap_password = get_env_config_variable(env_name, 'ap_password')
  65. dut.write(' '.join([ap_ssid, ap_password]))
  66. got_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  67. got_port = dut.expect(r"(?:[\s\S]*)Starting server on port: '(\d+)'", timeout=30)[1].decode()
  68. logging.info('Got IP : {}'.format(got_ip))
  69. logging.info('Got Port : {}'.format(got_port))
  70. # Expected Logs
  71. dut.expect('Registering URI handlers', timeout=30)
  72. # Run test script
  73. # If failed raise appropriate exception
  74. logging.info('Test /hello GET handler')
  75. if not client.test_get_handler(got_ip, str(got_port)):
  76. raise RuntimeError
  77. # Acquire host IP. Need a way to check it
  78. dut.expect(r'(?:[\s\S]*)Found header => Host: (\d+.\d+.\d+.\d+)', timeout=30)
  79. # Match additional headers sent in the request
  80. dut.expect('Found header => Test-Header-2: Test-Value-2', timeout=30)
  81. dut.expect('Found header => Test-Header-1: Test-Value-1', timeout=30)
  82. dut.expect('Found URL query parameter => query1=value1', timeout=30)
  83. dut.expect('Found URL query parameter => query3=value3', timeout=30)
  84. dut.expect('Found URL query parameter => query2=value2', timeout=30)
  85. dut.expect('Request headers lost', timeout=30)
  86. logging.info('Test /ctrl PUT handler and realtime handler de/registration')
  87. if not client.test_put_handler(got_ip, got_port):
  88. raise RuntimeError
  89. dut.expect('Unregistering /hello and /echo URIs', timeout=30)
  90. dut.expect('Registering /hello and /echo URIs', timeout=30)
  91. # Generate random data of 10KB
  92. random_data = ''.join(string.printable[random.randint(0,len(string.printable)) - 1] for _ in range(10 * 1024))
  93. logging.info('Test /echo POST handler with random data')
  94. if not client.test_post_handler(got_ip, got_port, random_data):
  95. raise RuntimeError
  96. query = 'http://foobar'
  97. logging.info('Test /hello with custom query : {}'.format(query))
  98. if not client.test_custom_uri_query(got_ip, got_port, query):
  99. raise RuntimeError
  100. dut.expect('Found URL query => ' + query, timeout=30)
  101. query = 'abcd+1234%20xyz'
  102. logging.info('Test /hello with custom query : {}'.format(query))
  103. if not client.test_custom_uri_query(got_ip, got_port, query):
  104. raise RuntimeError
  105. dut.expect_exact('Found URL query => ' + query, timeout=30)
  106. @pytest.mark.esp32
  107. @pytest.mark.esp32c3
  108. @pytest.mark.esp32s2
  109. @pytest.mark.esp32s3
  110. @pytest.mark.wifi_router
  111. def test_examples_protocol_http_server_lru_purge_enable(dut: Dut) -> None:
  112. # Get binary file
  113. binary_file = os.path.join(dut.app.binary_path, 'simple.bin')
  114. bin_size = os.path.getsize(binary_file)
  115. logging.info('http_server_bin_size : {}KB'.format(bin_size // 1024))
  116. # Upload binary and start testing
  117. logging.info('Starting http_server simple test app')
  118. # Parse IP address of STA
  119. logging.info('Waiting to connect with AP')
  120. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  121. dut.expect('Please input ssid password:')
  122. env_name = 'wifi_router'
  123. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  124. ap_password = get_env_config_variable(env_name, 'ap_password')
  125. dut.write(f'{ap_ssid} {ap_password}')
  126. got_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  127. got_port = dut.expect(r"(?:[\s\S]*)Starting server on port: '(\d+)'", timeout=30)[1].decode()
  128. logging.info('Got IP : {}'.format(got_ip))
  129. logging.info('Got Port : {}'.format(got_port))
  130. # Expected Logs
  131. dut.expect('Registering URI handlers', timeout=30)
  132. threads = []
  133. # Open 20 sockets, one from each thread
  134. for _ in range(20):
  135. try:
  136. thread = http_client_thread(got_ip, (int(got_port)), 20)
  137. thread.start()
  138. threads.append(thread)
  139. except OSError as err:
  140. logging.info('Error: unable to start thread, {}'.format(err))
  141. for t in threads:
  142. t.join()