http_server_simple_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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
  17. from __future__ import print_function
  18. from __future__ import unicode_literals
  19. from builtins import range
  20. import re
  21. import os
  22. import string
  23. import random
  24. import threading
  25. import time
  26. import socket
  27. from tiny_test_fw import Utility
  28. import ttfw_idf
  29. from idf_http_server_test import client
  30. class http_client_thread(threading.Thread):
  31. def __init__(self, ip, port, delay):
  32. threading.Thread.__init__(self)
  33. self.ip = ip
  34. self.port = port
  35. self.delay = delay
  36. self.exc = None
  37. # Thread function used to open a socket and wait for specific amount of time before returning
  38. def open_connection(self, ip, port, delay):
  39. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  40. s.settimeout(delay)
  41. s.connect((ip, port))
  42. time.sleep(delay)
  43. def run(self):
  44. try:
  45. self.open_connection(self.ip, self.port, self.delay)
  46. except socket.timeout as e:
  47. self.exc = e
  48. def join(self, timeout=None):
  49. threading.Thread.join(self)
  50. if self.exc:
  51. raise self.exc
  52. # When running on local machine execute the following before running this script
  53. # > make app bootloader
  54. # > make print_flash_cmd | tail -n 1 > build/download.config
  55. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  56. def test_examples_protocol_http_server_simple(env, extra_data):
  57. # Acquire DUT
  58. dut1 = env.get_dut("http_server", "examples/protocols/http_server/simple", dut_class=ttfw_idf.ESP32DUT)
  59. # Get binary file
  60. binary_file = os.path.join(dut1.app.binary_path, "simple.bin")
  61. bin_size = os.path.getsize(binary_file)
  62. ttfw_idf.log_performance("http_server_bin_size", "{}KB".format(bin_size // 1024))
  63. # Upload binary and start testing
  64. Utility.console_log("Starting http_server simple test app")
  65. dut1.start_app()
  66. # Parse IP address of STA
  67. Utility.console_log("Waiting to connect with AP")
  68. got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  69. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  70. Utility.console_log("Got IP : " + got_ip)
  71. Utility.console_log("Got Port : " + got_port)
  72. # Expected Logs
  73. dut1.expect("Registering URI handlers", timeout=30)
  74. # Run test script
  75. # If failed raise appropriate exception
  76. Utility.console_log("Test /hello GET handler")
  77. if not client.test_get_handler(got_ip, got_port):
  78. raise RuntimeError
  79. # Acquire host IP. Need a way to check it
  80. dut1.expect(re.compile(r"(?:[\s\S]*)Found header => Host: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  81. # Match additional headers sent in the request
  82. dut1.expect("Found header => Test-Header-2: Test-Value-2", timeout=30)
  83. dut1.expect("Found header => Test-Header-1: Test-Value-1", timeout=30)
  84. dut1.expect("Found URL query parameter => query1=value1", timeout=30)
  85. dut1.expect("Found URL query parameter => query3=value3", timeout=30)
  86. dut1.expect("Found URL query parameter => query2=value2", timeout=30)
  87. dut1.expect("Request headers lost", timeout=30)
  88. Utility.console_log("Test /ctrl PUT handler and realtime handler de/registration")
  89. if not client.test_put_handler(got_ip, got_port):
  90. raise RuntimeError
  91. dut1.expect("Unregistering /hello and /echo URIs", timeout=30)
  92. dut1.expect("Registering /hello and /echo URIs", timeout=30)
  93. # Generate random data of 10KB
  94. random_data = ''.join(string.printable[random.randint(0,len(string.printable)) - 1] for _ in range(10 * 1024))
  95. Utility.console_log("Test /echo POST handler with random data")
  96. if not client.test_post_handler(got_ip, got_port, random_data):
  97. raise RuntimeError
  98. query = "http://foobar"
  99. Utility.console_log("Test /hello with custom query : " + query)
  100. if not client.test_custom_uri_query(got_ip, got_port, query):
  101. raise RuntimeError
  102. dut1.expect("Found URL query => " + query, timeout=30)
  103. query = "abcd+1234%20xyz"
  104. Utility.console_log("Test /hello with custom query : " + query)
  105. if not client.test_custom_uri_query(got_ip, got_port, query):
  106. raise RuntimeError
  107. dut1.expect("Found URL query => " + query, timeout=30)
  108. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  109. def test_examples_protocol_http_server_lru_purge_enable(env, extra_data):
  110. # Acquire DUT
  111. dut1 = env.get_dut("http_server", "examples/protocols/http_server/simple", dut_class=ttfw_idf.ESP32DUT)
  112. # Get binary file
  113. binary_file = os.path.join(dut1.app.binary_path, "simple.bin")
  114. bin_size = os.path.getsize(binary_file)
  115. ttfw_idf.log_performance("http_server_bin_size", "{}KB".format(bin_size // 1024))
  116. # Upload binary and start testing
  117. Utility.console_log("Starting http_server simple test app")
  118. dut1.start_app()
  119. # Parse IP address of STA
  120. Utility.console_log("Waiting to connect with AP")
  121. got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  122. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  123. Utility.console_log("Got IP : " + got_ip)
  124. Utility.console_log("Got Port : " + got_port)
  125. # Expected Logs
  126. dut1.expect("Registering URI handlers", timeout=30)
  127. threads = []
  128. # Open 20 sockets, one from each thread
  129. for _ in range(20):
  130. try:
  131. thread = http_client_thread(got_ip, (int(got_port)), 20)
  132. thread.start()
  133. threads.append(thread)
  134. except OSError as err:
  135. Utility.console_log("Error: unable to start thread, " + err)
  136. for t in threads:
  137. t.join()
  138. if __name__ == '__main__':
  139. test_examples_protocol_http_server_simple()
  140. test_examples_protocol_http_server_lru_purge_enable()