pytest_http_server_advanced.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. from __future__ import division, print_function, unicode_literals
  6. import logging
  7. import os
  8. import sys
  9. import pytest
  10. try:
  11. from idf_http_server_test import test as client
  12. except ModuleNotFoundError:
  13. sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'tools', 'ci', 'python_packages'))
  14. from idf_http_server_test import test as client
  15. from common_test_methods import get_env_config_variable
  16. from pytest_embedded import Dut
  17. # When running on local machine execute the following before running this script
  18. # > make app bootloader
  19. # > make print_flash_cmd | tail -n 1 > build/download.config
  20. # Due to connectivity issues (between runner host and DUT) in the runner environment,
  21. # some of the `advanced_tests` are ignored. These tests are intended for verifying
  22. # the expected limits of the http_server capabilities, and implement sending and receiving
  23. # of large HTTP packets and malformed requests, running multiple parallel sessions, etc.
  24. # It is advised that all these tests be run locally, when making changes or adding new
  25. # features to this component.
  26. @pytest.mark.esp32
  27. @pytest.mark.esp32c3
  28. @pytest.mark.esp32s2
  29. @pytest.mark.esp32s3
  30. @pytest.mark.wifi_router
  31. def test_examples_protocol_http_server_advanced(dut: Dut) -> None:
  32. # Get binary file
  33. binary_file = os.path.join(dut.app.binary_path, 'tests.bin')
  34. bin_size = os.path.getsize(binary_file)
  35. logging.info('http_server_bin_size : {}KB'.format(bin_size // 1024))
  36. logging.info('Starting http_server advanced test app')
  37. # Parse IP address of STA
  38. logging.info('Waiting to connect with AP')
  39. if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
  40. dut.expect('Please input ssid password:')
  41. env_name = 'wifi_router'
  42. ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
  43. ap_password = get_env_config_variable(env_name, 'ap_password')
  44. dut.write(f'{ap_ssid} {ap_password}')
  45. got_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
  46. got_port = dut.expect(r"(?:[\s\S]*)Started HTTP server on port: '(\d+)'", timeout=30)[1].decode()
  47. result = dut.expect(r"(?:[\s\S]*)Max URI handlers: '(\d+)'(?:[\s\S]*)Max Open Sessions: " # noqa: W605
  48. r"'(\d+)'(?:[\s\S]*)Max Header Length: '(\d+)'(?:[\s\S]*)Max URI Length: "
  49. r"'(\d+)'(?:[\s\S]*)Max Stack Size: '(\d+)'", timeout=15)
  50. # max_uri_handlers = int(result[1])
  51. max_sessions = int(result[2])
  52. max_hdr_len = int(result[3])
  53. max_uri_len = int(result[4])
  54. max_stack_size = int(result[5])
  55. logging.info('Got Port : {}'.format(got_port))
  56. logging.info('Got IP : {}'.format(got_ip))
  57. # Run test script
  58. # If failed raise appropriate exception
  59. failed = False
  60. logging.info('Sessions and Context Tests...')
  61. if not client.spillover_session(got_ip, got_port, max_sessions):
  62. logging.info('Ignoring failure')
  63. if not client.parallel_sessions_adder(got_ip, got_port, max_sessions):
  64. logging.info('Ignoring failure')
  65. if not client.leftover_data_test(got_ip, got_port):
  66. failed = True
  67. if not client.async_response_test(got_ip, got_port):
  68. failed = True
  69. if not client.recv_timeout_test(got_ip, got_port):
  70. failed = True
  71. if not client.arbitrary_termination_test(got_ip, got_port):
  72. failed = True
  73. # This test fails a lot! Enable when connection is stable
  74. # test_size = 50*1024 # 50KB
  75. # if not client.packet_size_limit_test(got_ip, got_port, test_size):
  76. # logging.info("Ignoring failure")
  77. logging.info('Getting initial stack usage...')
  78. if not client.get_hello(got_ip, got_port):
  79. failed = True
  80. inital_stack = int(dut.expect(r"(?:[\s\S]*)Free Stack for server task: '(\d+)'", timeout=15)[1])
  81. if inital_stack < 0.1 * max_stack_size:
  82. logging.info('More than 90% of stack being used on server start')
  83. failed = True
  84. logging.info('Basic HTTP Client Tests...')
  85. if not client.get_hello(got_ip, got_port):
  86. failed = True
  87. if not client.post_hello(got_ip, got_port):
  88. failed = True
  89. if not client.put_hello(got_ip, got_port):
  90. failed = True
  91. if not client.post_echo(got_ip, got_port):
  92. failed = True
  93. if not client.get_echo(got_ip, got_port):
  94. failed = True
  95. if not client.put_echo(got_ip, got_port):
  96. failed = True
  97. if not client.get_hello_type(got_ip, got_port):
  98. failed = True
  99. if not client.get_hello_status(got_ip, got_port):
  100. failed = True
  101. if not client.get_false_uri(got_ip, got_port):
  102. failed = True
  103. if not client.get_test_headers(got_ip, got_port):
  104. failed = True
  105. logging.info('Error code tests...')
  106. if not client.code_500_server_error_test(got_ip, got_port):
  107. failed = True
  108. if not client.code_501_method_not_impl(got_ip, got_port):
  109. failed = True
  110. if not client.code_505_version_not_supported(got_ip, got_port):
  111. failed = True
  112. if not client.code_400_bad_request(got_ip, got_port):
  113. failed = True
  114. if not client.code_404_not_found(got_ip, got_port):
  115. failed = True
  116. if not client.code_405_method_not_allowed(got_ip, got_port):
  117. failed = True
  118. if not client.code_408_req_timeout(got_ip, got_port):
  119. failed = True
  120. if not client.code_414_uri_too_long(got_ip, got_port, max_uri_len):
  121. logging.info('Ignoring failure')
  122. if not client.code_431_hdr_too_long(got_ip, got_port, max_hdr_len):
  123. logging.info('Ignoring failure')
  124. if not client.test_upgrade_not_supported(got_ip, got_port):
  125. failed = True
  126. logging.info('Getting final stack usage...')
  127. if not client.get_hello(got_ip, got_port):
  128. failed = True
  129. final_stack = int(dut.expect(r"(?:[\s\S]*)Free Stack for server task: '(\d+)'", timeout=15)[1])
  130. if final_stack < 0.05 * max_stack_size:
  131. logging.info('More than 95% of stack got used during tests')
  132. failed = True
  133. if failed:
  134. raise RuntimeError