http_server_simple_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import division, print_function, unicode_literals
  17. import os
  18. import random
  19. import re
  20. import socket
  21. import string
  22. import threading
  23. import time
  24. from builtins import range
  25. import ttfw_idf
  26. from idf_http_server_test import client
  27. from tiny_test_fw import Utility
  28. class http_client_thread(threading.Thread):
  29. def __init__(self, ip, port, delay):
  30. threading.Thread.__init__(self)
  31. self.ip = ip
  32. self.port = port
  33. self.delay = delay
  34. self.exc = None
  35. # Thread function used to open a socket and wait for specific amount of time before returning
  36. def open_connection(self, ip, port, delay):
  37. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  38. s.settimeout(delay)
  39. s.connect((ip, port))
  40. time.sleep(delay)
  41. def run(self):
  42. try:
  43. self.open_connection(self.ip, self.port, self.delay)
  44. except socket.timeout as e:
  45. self.exc = e
  46. def join(self, timeout=None):
  47. threading.Thread.join(self)
  48. if self.exc:
  49. raise self.exc
  50. # When running on local machine execute the following before running this script
  51. # > make app bootloader
  52. # > make print_flash_cmd | tail -n 1 > build/download.config
  53. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols')
  54. def test_examples_protocol_http_server_simple(env, extra_data):
  55. # Acquire DUT
  56. dut1 = env.get_dut('http_server', 'examples/protocols/http_server/simple', dut_class=ttfw_idf.ESP32DUT)
  57. # Get binary file
  58. binary_file = os.path.join(dut1.app.binary_path, 'simple.bin')
  59. bin_size = os.path.getsize(binary_file)
  60. ttfw_idf.log_performance('http_server_bin_size', '{}KB'.format(bin_size // 1024))
  61. # Upload binary and start testing
  62. Utility.console_log('Starting http_server simple test app')
  63. dut1.start_app()
  64. # Parse IP address of STA
  65. Utility.console_log('Waiting to connect with AP')
  66. got_ip = dut1.expect(re.compile(r'(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)'), timeout=30)[0]
  67. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  68. Utility.console_log('Got IP : ' + got_ip)
  69. Utility.console_log('Got Port : ' + got_port)
  70. # Expected Logs
  71. dut1.expect('Registering URI handlers', timeout=30)
  72. # Run test script
  73. # If failed raise appropriate exception
  74. Utility.console_log('Test /hello GET handler')
  75. if not client.test_get_handler(got_ip, got_port):
  76. raise RuntimeError
  77. # Acquire host IP. Need a way to check it
  78. dut1.expect(re.compile(r'(?:[\s\S]*)Found header => Host: (\d+.\d+.\d+.\d+)'), timeout=30)[0]
  79. # Match additional headers sent in the request
  80. dut1.expect('Found header => Test-Header-2: Test-Value-2', timeout=30)
  81. dut1.expect('Found header => Test-Header-1: Test-Value-1', timeout=30)
  82. dut1.expect('Found URL query parameter => query1=value1', timeout=30)
  83. dut1.expect('Found URL query parameter => query3=value3', timeout=30)
  84. dut1.expect('Found URL query parameter => query2=value2', timeout=30)
  85. dut1.expect('Request headers lost', timeout=30)
  86. Utility.console_log('Test /ctrl PUT handler and realtime handler de/registration')
  87. if not client.test_put_handler(got_ip, got_port):
  88. raise RuntimeError
  89. dut1.expect('Unregistering /hello and /echo URIs', timeout=30)
  90. dut1.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. Utility.console_log('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. Utility.console_log('Test /hello with custom query : ' + query)
  98. if not client.test_custom_uri_query(got_ip, got_port, query):
  99. raise RuntimeError
  100. dut1.expect('Found URL query => ' + query, timeout=30)
  101. query = 'abcd+1234%20xyz'
  102. Utility.console_log('Test /hello with custom query : ' + query)
  103. if not client.test_custom_uri_query(got_ip, got_port, query):
  104. raise RuntimeError
  105. dut1.expect('Found URL query => ' + query, timeout=30)
  106. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols')
  107. def test_examples_protocol_http_server_lru_purge_enable(env, extra_data):
  108. # Acquire DUT
  109. dut1 = env.get_dut('http_server', 'examples/protocols/http_server/simple', dut_class=ttfw_idf.ESP32DUT)
  110. # Get binary file
  111. binary_file = os.path.join(dut1.app.binary_path, 'simple.bin')
  112. bin_size = os.path.getsize(binary_file)
  113. ttfw_idf.log_performance('http_server_bin_size', '{}KB'.format(bin_size // 1024))
  114. # Upload binary and start testing
  115. Utility.console_log('Starting http_server simple test app')
  116. dut1.start_app()
  117. # Parse IP address of STA
  118. Utility.console_log('Waiting to connect with AP')
  119. got_ip = dut1.expect(re.compile(r'(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)'), timeout=30)[0]
  120. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  121. Utility.console_log('Got IP : ' + got_ip)
  122. Utility.console_log('Got Port : ' + got_port)
  123. # Expected Logs
  124. dut1.expect('Registering URI handlers', timeout=30)
  125. threads = []
  126. # Open 20 sockets, one from each thread
  127. for _ in range(20):
  128. try:
  129. thread = http_client_thread(got_ip, (int(got_port)), 20)
  130. thread.start()
  131. threads.append(thread)
  132. except OSError as err:
  133. Utility.console_log('Error: unable to start thread, ' + err)
  134. for t in threads:
  135. t.join()
  136. if __name__ == '__main__':
  137. test_examples_protocol_http_server_simple()
  138. test_examples_protocol_http_server_lru_purge_enable()