feature/esp_http_server_idf_v3.2:Changes to make esp_http_server compatible with ESP8266.

Changes:
Lru counter in place of timestamp added.
syslimits.h definition guards for ARG_MAX, PATH_LEN.
Renamed src/port/esp32 to src/port/esp8266.
Enabled working without IPv6. Test Scripts requiring TinyFW removed
Utility.console_log replaced by print.
This commit is contained in:
Supreet Deshpande
2019-02-18 15:07:09 +05:30
parent 825a53199d
commit efc81a6649
12 changed files with 136 additions and 426 deletions

View File

@ -1,5 +1,5 @@
set(COMPONENT_ADD_INCLUDEDIRS include)
set(COMPONENT_PRIV_INCLUDEDIRS src/port/esp32 src/util)
set(COMPONENT_PRIV_INCLUDEDIRS src/port/esp8266 src/util)
set(COMPONENT_SRCS "src/httpd_main.c"
"src/httpd_parse.c"
"src/httpd_sess.c"

View File

@ -1,4 +1,5 @@
COMPONENT_SRCDIRS := src src/util
COMPONENT_ADD_INCLUDEDIRS := include
COMPONENT_PRIV_INCLUDEDIRS := src/port/esp32 src/util
COMPONENT_PRIV_INCLUDEDIRS := src/port/esp8266 src/util

View File

@ -585,7 +585,7 @@ esp_err_t httpd_sess_set_pending_override(httpd_handle_t hd, int sockfd, httpd_p
* session socket fd, from within a URI handler, ie. :
* httpd_sess_get_ctx(),
* httpd_sess_trigger_close(),
* httpd_sess_update_timestamp().
* httpd_sess_update_lru_counter().
*
* @note This API is supposed to be called only from the context of
* a URI handler where httpd_req_t* request pointer is valid.
@ -1111,15 +1111,15 @@ void *httpd_get_global_transport_ctx(httpd_handle_t handle);
esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd);
/**
* @brief Update timestamp for a given socket
* @brief Update LRU counter for a given socket
*
* Timestamps are internally associated with each session to monitor
* LRU Counters are internally associated with each session to monitor
* how recently a session exchanged traffic. When LRU purge is enabled,
* if a client is requesting for connection but maximum number of
* sockets/sessions is reached, then the session having the earliest
* timestamp is closed automatically.
* LRU counter is closed automatically.
*
* Updating the timestamp manually prevents the socket from being purged
* Updating the LRU counter manually prevents the socket from being purged
* due to the Least Recently Used (LRU) logic, even though it might not
* have received traffic for some time. This is useful when all open
* sockets/session are frequently exchanging traffic but the user specifically
@ -1130,15 +1130,15 @@ esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd);
* is enabled.
*
* @param[in] handle Handle to server returned by httpd_start
* @param[in] sockfd The socket descriptor of the session for which timestamp
* @param[in] sockfd The socket descriptor of the session for which LRU counter
* is to be updated
*
* @return
* - ESP_OK : Socket found and timestamp updated
* - ESP_OK : Socket found and LRU counter updated
* - ESP_ERR_NOT_FOUND : Socket not found
* - ESP_ERR_INVALID_ARG : Null arguments
*/
esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd);
esp_err_t httpd_sess_update_lru_counter(httpd_handle_t handle, int sockfd);
/** End of Session
* @}

View File

@ -125,7 +125,7 @@ struct sock_db {
httpd_send_func_t send_fn; /*!< Send function for this socket */
httpd_recv_func_t recv_fn; /*!< Receive function for this socket */
httpd_pending_func_t pending_fn; /*!< Pending function for this socket */
int64_t timestamp; /*!< Timestamp indicating when the socket was last used */
uint64_t lru_counter; /*!< LRU Counter indicating when the socket was last used */
char pending_data[PARSER_BLOCK_SIZE]; /*!< Buffer for pending data to be received */
size_t pending_len; /*!< Length of pending data to be received */
};

View File

@ -236,19 +236,32 @@ static void httpd_thread(void *arg)
static esp_err_t httpd_server_init(struct httpd_data *hd)
{
#ifdef CONFIG_LWIP_IPV6
int fd = socket(PF_INET6, SOCK_STREAM, 0);
#else
int fd = socket(PF_INET, SOCK_STREAM, 0);
#endif /* CONFIG_LWIP_IPV6 */
if (fd < 0) {
ESP_LOGE(TAG, LOG_FMT("error in socket (%d)"), errno);
return ESP_FAIL;
}
#ifdef CONFIG_LWIP_IPV6
struct in6_addr inaddr_any = IN6ADDR_ANY_INIT;
struct sockaddr_in6 serv_addr = {
.sin6_family = PF_INET6,
.sin6_addr = inaddr_any,
.sin6_port = htons(hd->config.server_port)
};
#else
struct sockaddr_in serv_addr = {
.sin_family = PF_INET,
.sin_addr = {
.s_addr = htonl(INADDR_ANY)
},
.sin_port = htons(hd->config.server_port)
};
#endif /* CONFIG_LWIP_IPV6 */
int ret = bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (ret < 0) {
ESP_LOGE(TAG, LOG_FMT("error in bind (%d)"), errno);

View File

@ -19,6 +19,7 @@
#include <esp_http_server.h>
#include "esp_httpd_priv.h"
#include <sys/fcntl.h>
static const char *TAG = "httpd_sess";
@ -192,7 +193,13 @@ void httpd_sess_set_descriptors(struct httpd_data *hd,
/** Check if a FD is valid */
static int fd_is_valid(int fd)
{
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
return fcntl(fd, F_GETFD, 0) != -1 || errno != EBADF;
}
static inline uint64_t httpd_sess_get_lru_counter()
{
static uint64_t lru_counter = 0;
return lru_counter++;
}
void httpd_sess_delete_invalid(struct httpd_data *hd)
@ -297,11 +304,11 @@ esp_err_t httpd_sess_process(struct httpd_data *hd, int newfd)
return ESP_FAIL;
}
ESP_LOGD(TAG, LOG_FMT("success"));
sd->timestamp = httpd_os_get_timestamp();
sd->lru_counter = httpd_sess_get_lru_counter();
return ESP_OK;
}
esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd)
esp_err_t httpd_sess_update_lru_counter(httpd_handle_t handle, int sockfd)
{
if (handle == NULL) {
return ESP_ERR_INVALID_ARG;
@ -312,7 +319,7 @@ esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd)
int i;
for (i = 0; i < hd->config.max_open_sockets; i++) {
if (hd->hd_sd[i].fd == sockfd) {
hd->hd_sd[i].timestamp = httpd_os_get_timestamp();
hd->hd_sd[i].lru_counter = httpd_sess_get_lru_counter();
return ESP_OK;
}
}
@ -321,7 +328,7 @@ esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd)
esp_err_t httpd_sess_close_lru(struct httpd_data *hd)
{
int64_t timestamp = INT64_MAX;
uint64_t lru_counter = UINT64_MAX;
int lru_fd = -1;
int i;
for (i = 0; i < hd->config.max_open_sockets; i++) {
@ -332,8 +339,8 @@ esp_err_t httpd_sess_close_lru(struct httpd_data *hd)
if (hd->hd_sd[i].fd == -1) {
return ESP_OK;
}
if (hd->hd_sd[i].timestamp < timestamp) {
timestamp = hd->hd_sd[i].timestamp;
if (hd->hd_sd[i].lru_counter < lru_counter) {
lru_counter = hd->hd_sd[i].lru_counter;
lru_fd = hd->hd_sd[i].fd;
}
}

View File

@ -52,11 +52,6 @@ static inline void httpd_os_thread_sleep(int msecs)
vTaskDelay(msecs / portTICK_RATE_MS);
}
static inline int64_t httpd_os_get_timestamp()
{
return esp_timer_get_time();
}
static inline othread_t httpd_os_thread_handle()
{
return xTaskGetCurrentTaskHandle();

View File

@ -37,7 +37,9 @@
#ifndef _SYS_SYSLIMITS_H_
#define _SYS_SYSLIMITS_H_
#ifndef ARG_MAX
#define ARG_MAX 65536 /* max bytes for an exec function */
#endif
#ifndef CHILD_MAX
#define CHILD_MAX 40 /* max simultaneous processes */
#endif
@ -49,7 +51,9 @@
#ifndef OPEN_MAX
#define OPEN_MAX 64 /* max open files per process */
#endif
#ifndef PATH_MAX
#define PATH_MAX 1024 /* max bytes in pathname */
#endif
#define PIPE_BUF 512 /* max bytes for atomic pipe writes */
#define IOV_MAX 1024 /* max elements in i/o vector */

View File

@ -1,169 +0,0 @@
#!/usr/bin/env python
#
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import imp
import re
import os
import sys
import string
import random
import socket
# This environment variable is expected on the host machine
test_fw_path = os.getenv("TEST_FW_PATH")
if test_fw_path and test_fw_path not in sys.path:
sys.path.insert(0, test_fw_path)
# When running on local machine execute the following before running this script
# > make app bootloader
# > make print_flash_cmd | tail -n 1 > build/download.config
# > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
import TinyFW
import IDF
import Utility
# Import client module
expath = os.path.dirname(os.path.realpath(__file__))
client = imp.load_source("client", expath + "/scripts/test.py")
# Due to connectivity issues (between runner host and DUT) in the runner environment,
# some of the `advanced_tests` are ignored. These tests are intended for verifying
# the expected limits of the http_server capabilities, and implement sending and receiving
# of large HTTP packets and malformed requests, running multiple parallel sessions, etc.
# It is advised that all these tests be run locally, when making changes or adding new
# features to this component.
@IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_http_server_advanced(env, extra_data):
# Acquire DUT
dut1 = env.get_dut("http_server", "examples/protocols/http_server/advanced_tests")
# Get binary file
binary_file = os.path.join(dut1.app.binary_path, "tests.bin")
bin_size = os.path.getsize(binary_file)
IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size//1024))
IDF.check_performance("http_server_bin_size", bin_size//1024)
# Upload binary and start testing
Utility.console_log("Starting http_server advanced test app")
dut1.start_app()
# Parse IP address of STA
Utility.console_log("Waiting to connect with AP")
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=30)[0]
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Started HTTP server on port: '(\d+)'"), timeout=15)[0]
result = dut1.expect(re.compile(r"(?:[\s\S]*)Max URI handlers: '(\d+)'(?:[\s\S]*)Max Open Sessions: '(\d+)'(?:[\s\S]*)Max Header Length: '(\d+)'(?:[\s\S]*)Max URI Length: '(\d+)'(?:[\s\S]*)Max Stack Size: '(\d+)'"), timeout=15)
max_uri_handlers = int(result[0])
max_sessions = int(result[1])
max_hdr_len = int(result[2])
max_uri_len = int(result[3])
max_stack_size = int(result[4])
Utility.console_log("Got IP : " + got_ip)
Utility.console_log("Got Port : " + got_port)
# Run test script
# If failed raise appropriate exception
failed = False
Utility.console_log("Sessions and Context Tests...")
if not client.spillover_session(got_ip, got_port, max_sessions):
Utility.console_log("Ignoring failure")
if not client.parallel_sessions_adder(got_ip, got_port, max_sessions):
Utility.console_log("Ignoring failure")
if not client.leftover_data_test(got_ip, got_port):
failed = True
if not client.async_response_test(got_ip, got_port):
failed = True
if not client.recv_timeout_test(got_ip, got_port):
failed = True
## This test fails a lot! Enable when connection is stable
#test_size = 50*1024 # 50KB
#if not client.packet_size_limit_test(got_ip, got_port, test_size):
# Utility.console_log("Ignoring failure")
Utility.console_log("Getting initial stack usage...")
if not client.get_hello(got_ip, got_port):
failed = True
inital_stack = int(dut1.expect(re.compile(r"(?:[\s\S]*)Free Stack for server task: '(\d+)'"), timeout=15)[0])
if inital_stack < 0.1*max_stack_size:
Utility.console_log("More than 90% of stack being used on server start")
failed = True
Utility.console_log("Basic HTTP Client Tests...")
if not client.get_hello(got_ip, got_port):
failed = True
if not client.post_hello(got_ip, got_port):
failed = True
if not client.put_hello(got_ip, got_port):
failed = True
if not client.post_echo(got_ip, got_port):
failed = True
if not client.get_echo(got_ip, got_port):
failed = True
if not client.put_echo(got_ip, got_port):
failed = True
if not client.get_hello_type(got_ip, got_port):
failed = True
if not client.get_hello_status(got_ip, got_port):
failed = True
if not client.get_false_uri(got_ip, got_port):
failed = True
Utility.console_log("Error code tests...")
if not client.code_500_server_error_test(got_ip, got_port):
failed = True
if not client.code_501_method_not_impl(got_ip, got_port):
failed = True
if not client.code_505_version_not_supported(got_ip, got_port):
failed = True
if not client.code_400_bad_request(got_ip, got_port):
failed = True
if not client.code_404_not_found(got_ip, got_port):
failed = True
if not client.code_405_method_not_allowed(got_ip, got_port):
failed = True
if not client.code_408_req_timeout(got_ip, got_port):
failed = True
if not client.code_414_uri_too_long(got_ip, got_port, max_uri_len):
Utility.console_log("Ignoring failure")
if not client.code_431_hdr_too_long(got_ip, got_port, max_hdr_len):
Utility.console_log("Ignoring failure")
if not client.test_upgrade_not_supported(got_ip, got_port):
failed = True
Utility.console_log("Getting final stack usage...")
if not client.get_hello(got_ip, got_port):
failed = True
final_stack = int(dut1.expect(re.compile(r"(?:[\s\S]*)Free Stack for server task: '(\d+)'"), timeout=15)[0])
if final_stack < 0.05*max_stack_size:
Utility.console_log("More than 95% of stack got used during tests")
failed = True
if failed:
raise RuntimeError
if __name__ == '__main__':
test_examples_protocol_http_server_advanced()

View File

@ -144,7 +144,6 @@ import http.client
import sys
import string
import random
import Utility
_verbose_ = False
@ -165,7 +164,7 @@ class Session(object):
self.client.sendall(data.encode())
except socket.error as err:
self.client.close()
Utility.console_log("Socket Error in send :", err)
print("Socket Error in send :", err)
rval = False
return rval
@ -234,7 +233,7 @@ class Session(object):
return headers
except socket.error as err:
self.client.close()
Utility.console_log("Socket Error in recv :", err)
print("Socket Error in recv :", err)
return None
def read_resp_data(self):
@ -265,7 +264,7 @@ class Session(object):
# Fetch remaining CRLF
if self.client.recv(2) != "\r\n":
# Error in packet
Utility.console_log("Error in chunked data")
print("Error in chunked data")
return None
if not chunk_len:
# If last chunk
@ -278,7 +277,7 @@ class Session(object):
return read_data
except socket.error as err:
self.client.close()
Utility.console_log("Socket Error in recv :", err)
print("Socket Error in recv :", err)
return None
def close(self):
@ -286,10 +285,10 @@ class Session(object):
def test_val(text, expected, received):
if expected != received:
Utility.console_log(" Fail!")
Utility.console_log(" [reason] " + text + ":")
Utility.console_log(" expected: " + str(expected))
Utility.console_log(" received: " + str(received))
print(" Fail!")
print(" [reason] " + text + ":")
print(" expected: " + str(expected))
print(" received: " + str(received))
return False
return True
@ -306,7 +305,7 @@ class adder_thread (threading.Thread):
# Pipeline 3 requests
if (_verbose_):
Utility.console_log(" Thread: Using adder start " + str(self.id))
print(" Thread: Using adder start " + str(self.id))
for _ in range(self.depth):
self.session.send_post('/adder', str(self.id))
@ -318,7 +317,7 @@ class adder_thread (threading.Thread):
def adder_result(self):
if len(self.response) != self.depth:
Utility.console_log("Error : missing response packets")
print("Error : missing response packets")
return False
for i in range(len(self.response)):
if not test_val("Thread" + str(self.id) + " response[" + str(i) + "]",
@ -331,7 +330,7 @@ class adder_thread (threading.Thread):
def get_hello(dut, port):
# GET /hello should return 'Hello World!'
Utility.console_log("[test] GET /hello returns 'Hello World!' =>", end=' ')
print("[test] GET /hello returns 'Hello World!' =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("GET", "/hello")
resp = conn.getresponse()
@ -344,39 +343,39 @@ def get_hello(dut, port):
if not test_val("data", "text/html", resp.getheader('Content-Type')):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def put_hello(dut, port):
# PUT /hello returns 405'
Utility.console_log("[test] PUT /hello returns 405 =>", end=' ')
print("[test] PUT /hello returns 405 =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("PUT", "/hello", "Hello")
resp = conn.getresponse()
if not test_val("status_code", 405, resp.status):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def post_hello(dut, port):
# POST /hello returns 405'
Utility.console_log("[test] POST /hello returns 404 =>", end=' ')
print("[test] POST /hello returns 404 =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("POST", "/hello", "Hello")
resp = conn.getresponse()
if not test_val("status_code", 405, resp.status):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def post_echo(dut, port):
# POST /echo echoes data'
Utility.console_log("[test] POST /echo echoes data =>", end=' ')
print("[test] POST /echo echoes data =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("POST", "/echo", "Hello")
resp = conn.getresponse()
@ -386,13 +385,13 @@ def post_echo(dut, port):
if not test_val("data", "Hello", resp.read().decode()):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def put_echo(dut, port):
# PUT /echo echoes data'
Utility.console_log("[test] PUT /echo echoes data =>", end=' ')
print("[test] PUT /echo echoes data =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("PUT", "/echo", "Hello")
resp = conn.getresponse()
@ -402,26 +401,26 @@ def put_echo(dut, port):
if not test_val("data", "Hello", resp.read().decode()):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def get_echo(dut, port):
# GET /echo returns 404'
Utility.console_log("[test] GET /echo returns 405 =>", end=' ')
print("[test] GET /echo returns 405 =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("GET", "/echo")
resp = conn.getresponse()
if not test_val("status_code", 405, resp.status):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def get_hello_type(dut, port):
# GET /hello/type_html returns text/html as Content-Type'
Utility.console_log("[test] GET /hello/type_html has Content-Type of text/html =>", end=' ')
print("[test] GET /hello/type_html has Content-Type of text/html =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("GET", "/hello/type_html")
resp = conn.getresponse()
@ -434,39 +433,39 @@ def get_hello_type(dut, port):
if not test_val("data", "text/html", resp.getheader('Content-Type')):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def get_hello_status(dut, port):
# GET /hello/status_500 returns status 500'
Utility.console_log("[test] GET /hello/status_500 returns status 500 =>", end=' ')
print("[test] GET /hello/status_500 returns status 500 =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("GET", "/hello/status_500")
resp = conn.getresponse()
if not test_val("status_code", 500, resp.status):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def get_false_uri(dut, port):
# GET /false_uri returns status 404'
Utility.console_log("[test] GET /false_uri returns status 404 =>", end=' ')
print("[test] GET /false_uri returns status 404 =>", end=' ')
conn = http.client.HTTPConnection(dut, int(port), timeout=15)
conn.request("GET", "/false_uri")
resp = conn.getresponse()
if not test_val("status_code", 404, resp.status):
conn.close()
return False
Utility.console_log("Success")
print("Success")
conn.close()
return True
def parallel_sessions_adder(dut, port, max_sessions):
# POSTs on /adder in parallel sessions
Utility.console_log("[test] POST {pipelined} on /adder in " + str(max_sessions) + " sessions =>", end=' ')
print("[test] POST {pipelined} on /adder in " + str(max_sessions) + " sessions =>", end=' ')
t = []
# Create all sessions
for i in range(max_sessions):
@ -484,13 +483,13 @@ def parallel_sessions_adder(dut, port, max_sessions):
res = False
t[i].close()
if (res):
Utility.console_log("Success")
print("Success")
return res
def async_response_test(dut, port):
# Test that an asynchronous work is executed in the HTTPD's context
# This is tested by reading two responses over the same session
Utility.console_log("[test] Test HTTPD Work Queue (Async response) =>", end=' ')
print("[test] Test HTTPD Work Queue (Async response) =>", end=' ')
s = Session(dut, port)
s.send_get('/async_data')
@ -503,12 +502,12 @@ def async_response_test(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def leftover_data_test(dut, port):
# Leftover data in POST is purged (valid and invalid URIs)
Utility.console_log("[test] Leftover data in POST is purged (valid and invalid URIs) =>", end=' ')
print("[test] Leftover data in POST is purged (valid and invalid URIs) =>", end=' ')
s = http.client.HTTPConnection(dut + ":" + port, timeout=15)
s.request("POST", url='/leftover_data', body="abcdefghijklmnopqrstuvwxyz\r\nabcdefghijklmnopqrstuvwxyz")
@ -537,17 +536,17 @@ def leftover_data_test(dut, port):
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def spillover_session(dut, port, max_sess):
# Session max_sess_sessions + 1 is rejected
Utility.console_log("[test] Session max_sess_sessions (" + str(max_sess) + ") + 1 is rejected =>", end=' ')
print("[test] Session max_sess_sessions (" + str(max_sess) + ") + 1 is rejected =>", end=' ')
s = []
_verbose_ = True
for i in range(max_sess + 1):
if (_verbose_):
Utility.console_log("Executing " + str(i))
print("Executing " + str(i))
try:
a = http.client.HTTPConnection(dut + ":" + port, timeout=15)
a.request("GET", url='/hello')
@ -558,7 +557,7 @@ def spillover_session(dut, port, max_sess):
s.append(a)
except:
if (_verbose_):
Utility.console_log("Connection " + str(i) + " rejected")
print("Connection " + str(i) + " rejected")
a.close()
break
@ -567,11 +566,11 @@ def spillover_session(dut, port, max_sess):
a.close()
# Check if number of connections is equal to max_sess
Utility.console_log(["Fail","Success"][len(s) == max_sess])
print(["Fail","Success"][len(s) == max_sess])
return (len(s) == max_sess)
def recv_timeout_test(dut, port):
Utility.console_log("[test] Timeout occurs if partial packet sent =>", end=' ')
print("[test] Timeout occurs if partial packet sent =>", end=' ')
s = Session(dut, port)
s.client.sendall(b"GE")
s.read_resp_hdrs()
@ -580,15 +579,15 @@ def recv_timeout_test(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def packet_size_limit_test(dut, port, test_size):
Utility.console_log("[test] send size limit test =>", end=' ')
print("[test] send size limit test =>", end=' ')
retry = 5
while (retry):
retry -= 1
Utility.console_log("data size = ", test_size)
print("data size = ", test_size)
s = http.client.HTTPConnection(dut + ":" + port, timeout=15)
random_data = ''.join(string.printable[random.randint(0,len(string.printable))-1] for _ in list(range(test_size)))
path = "/echo"
@ -596,28 +595,28 @@ def packet_size_limit_test(dut, port, test_size):
resp = s.getresponse()
if not test_val("Error", "200", str(resp.status)):
if test_val("Error", "500", str(resp.status)):
Utility.console_log("Data too large to be allocated")
print("Data too large to be allocated")
test_size = test_size//10
else:
Utility.console_log("Unexpected error")
print("Unexpected error")
s.close()
Utility.console_log("Retry...")
print("Retry...")
continue
resp = resp.read().decode()
result = (resp == random_data)
if not result:
test_val("Data size", str(len(random_data)), str(len(resp)))
s.close()
Utility.console_log("Retry...")
print("Retry...")
continue
s.close()
Utility.console_log("Success")
print("Success")
return True
Utility.console_log("Failed")
print("Failed")
return False
def code_500_server_error_test(dut, port):
Utility.console_log("[test] 500 Server Error test =>", end=' ')
print("[test] 500 Server Error test =>", end=' ')
s = Session(dut, port)
# Sending a very large content length will cause malloc to fail
content_len = 2**31
@ -628,11 +627,11 @@ def code_500_server_error_test(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def code_501_method_not_impl(dut, port):
Utility.console_log("[test] 501 Method Not Implemented =>", end=' ')
print("[test] 501 Method Not Implemented =>", end=' ')
s = Session(dut, port)
path = "/hello"
s.client.sendall(("ABC " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n").encode())
@ -646,11 +645,11 @@ def code_501_method_not_impl(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def code_505_version_not_supported(dut, port):
Utility.console_log("[test] 505 Version Not Supported =>", end=' ')
print("[test] 505 Version Not Supported =>", end=' ')
s = Session(dut, port)
path = "/hello"
s.client.sendall(("GET " + path + " HTTP/2.0\r\nHost: " + dut + "\r\n\r\n").encode())
@ -660,11 +659,11 @@ def code_505_version_not_supported(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def code_400_bad_request(dut, port):
Utility.console_log("[test] 400 Bad Request =>", end=' ')
print("[test] 400 Bad Request =>", end=' ')
s = Session(dut, port)
path = "/hello"
s.client.sendall(("XYZ " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n").encode())
@ -674,11 +673,11 @@ def code_400_bad_request(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def code_404_not_found(dut, port):
Utility.console_log("[test] 404 Not Found =>", end=' ')
print("[test] 404 Not Found =>", end=' ')
s = Session(dut, port)
path = "/dummy"
s.client.sendall(("GET " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n").encode())
@ -688,11 +687,11 @@ def code_404_not_found(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def code_405_method_not_allowed(dut, port):
Utility.console_log("[test] 405 Method Not Allowed =>", end=' ')
print("[test] 405 Method Not Allowed =>", end=' ')
s = Session(dut, port)
path = "/hello"
s.client.sendall(("POST " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n").encode())
@ -702,11 +701,11 @@ def code_405_method_not_allowed(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def code_408_req_timeout(dut, port):
Utility.console_log("[test] 408 Request Timeout =>", end=' ')
print("[test] 408 Request Timeout =>", end=' ')
s = Session(dut, port)
s.client.sendall(("POST /echo HTTP/1.1\r\nHost: " + dut + "\r\nContent-Length: 10\r\n\r\nABCD").encode())
s.read_resp_hdrs()
@ -715,11 +714,11 @@ def code_408_req_timeout(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def code_411_length_required(dut, port):
Utility.console_log("[test] 411 Length Required =>", end=' ')
print("[test] 411 Length Required =>", end=' ')
s = Session(dut, port)
path = "/echo"
s.client.sendall(("POST " + path + " HTTP/1.1\r\nHost: " + dut + "\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n").encode())
@ -733,7 +732,7 @@ def code_411_length_required(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
def send_getx_uri_len(dut, port, length):
@ -752,14 +751,14 @@ def send_getx_uri_len(dut, port, length):
return s.status
def code_414_uri_too_long(dut, port, max_uri_len):
Utility.console_log("[test] 414 URI Too Long =>", end=' ')
print("[test] 414 URI Too Long =>", end=' ')
status = send_getx_uri_len(dut, port, max_uri_len)
if not test_val("Client Error", "404", status):
return False
status = send_getx_uri_len(dut, port, max_uri_len + 1)
if not test_val("Client Error", "414", status):
return False
Utility.console_log("Success")
print("Success")
return True
def send_postx_hdr_len(dut, port, length):
@ -780,18 +779,18 @@ def send_postx_hdr_len(dut, port, length):
return False, s.status
def code_431_hdr_too_long(dut, port, max_hdr_len):
Utility.console_log("[test] 431 Header Too Long =>", end=' ')
print("[test] 431 Header Too Long =>", end=' ')
res, status = send_postx_hdr_len(dut, port, max_hdr_len)
if not res:
return False
res, status = send_postx_hdr_len(dut, port, max_hdr_len + 1)
if not test_val("Client Error", "431", status):
return False
Utility.console_log("Success")
print("Success")
return True
def test_upgrade_not_supported(dut, port):
Utility.console_log("[test] Upgrade Not Supported =>", end=' ')
print("[test] Upgrade Not Supported =>", end=' ')
s = Session(dut, port)
path = "/hello"
s.client.sendall(("OPTIONS * HTTP/1.1\r\nHost:" + dut + "\r\nUpgrade: TLS/1.0\r\nConnection: Upgrade\r\n\r\n").encode())
@ -801,7 +800,7 @@ def test_upgrade_not_supported(dut, port):
s.close()
return False
s.close()
Utility.console_log("Success")
print("Success")
return True
if __name__ == '__main__':
@ -825,7 +824,7 @@ if __name__ == '__main__':
_verbose_ = True
Utility.console_log("### Basic HTTP Client Tests")
print("### Basic HTTP Client Tests")
get_hello(dut, port)
post_hello(dut, port)
put_hello(dut, port)
@ -836,7 +835,7 @@ if __name__ == '__main__':
get_hello_status(dut, port)
get_false_uri(dut, port)
Utility.console_log("### Error code tests")
print("### Error code tests")
code_500_server_error_test(dut, port)
code_501_method_not_impl(dut, port)
code_505_version_not_supported(dut, port)
@ -851,7 +850,7 @@ if __name__ == '__main__':
# Not supported yet (Error on chunked request)
###code_411_length_required(dut, port)
Utility.console_log("### Sessions and Context Tests")
print("### Sessions and Context Tests")
parallel_sessions_adder(dut, port, max_sessions)
leftover_data_test(dut, port)
async_response_test(dut, port)

View File

@ -1,140 +0,0 @@
#!/usr/bin/env python
#
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import str
from builtins import range
import imp
import re
import os
import sys
import string
import random
import socket
# This environment variable is expected on the host machine
test_fw_path = os.getenv("TEST_FW_PATH")
if test_fw_path and test_fw_path not in sys.path:
sys.path.insert(0, test_fw_path)
# When running on local machine execute the following before running this script
# > make app bootloader
# > make print_flash_cmd | tail -n 1 > build/download.config
# > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
import TinyFW
import IDF
import Utility
# Import client module
expath = os.path.dirname(os.path.realpath(__file__))
client = imp.load_source("client", expath + "/scripts/adder.py")
@IDF.idf_example_test(env_tag="Example_WIFI")
def test_examples_protocol_http_server_persistence(env, extra_data):
# Acquire DUT
dut1 = env.get_dut("http_server", "examples/protocols/http_server/persistent_sockets")
# Get binary file
binary_file = os.path.join(dut1.app.binary_path, "persistent_sockets.bin")
bin_size = os.path.getsize(binary_file)
IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size//1024))
IDF.check_performance("http_server_bin_size", bin_size//1024)
# Upload binary and start testing
Utility.console_log("Starting http_server persistance test app")
dut1.start_app()
# Parse IP address of STA
Utility.console_log("Waiting to connect with AP")
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=120)[0]
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
Utility.console_log("Got IP : " + got_ip)
Utility.console_log("Got Port : " + got_port)
# Expected Logs
dut1.expect("Registering URI handlers", timeout=30)
# Run test script
conn = client.start_session(got_ip, got_port)
visitor = 0
adder = 0
# Test PUT request and initialize session context
num = random.randint(0,100)
client.putreq(conn, "/adder", str(num))
visitor += 1
dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
dut1.expect("/adder PUT handler read " + str(num), timeout=30)
dut1.expect("PUT allocating new session", timeout=30)
# Retest PUT request and change session context value
num = random.randint(0,100)
Utility.console_log("Adding: " + str(num))
client.putreq(conn, "/adder", str(num))
visitor += 1
adder += num
dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
dut1.expect("/adder PUT handler read " + str(num), timeout=30)
try:
# Re allocation shouldn't happen
dut1.expect("PUT allocating new session", timeout=30)
# Not expected
raise RuntimeError
except:
# As expected
pass
# Test POST request and session persistence
random_nums = [random.randint(0,100) for _ in range(100)]
for num in random_nums:
Utility.console_log("Adding: " + str(num))
client.postreq(conn, "/adder", str(num))
visitor += 1
adder += num
dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
dut1.expect("/adder handler read " + str(num), timeout=30)
# Test GET request and session persistence
Utility.console_log("Matching final sum: " + str(adder))
if client.getreq(conn, "/adder").decode() != str(adder):
raise RuntimeError
visitor += 1
dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
dut1.expect("/adder GET handler send " + str(adder), timeout=30)
Utility.console_log("Ending session")
# Close connection and check for invocation of context "Free" function
client.end_session(conn)
dut1.expect("/adder Free Context function called", timeout=30)
Utility.console_log("Validating user context data")
# Start another session to check user context data
conn2 = client.start_session(got_ip, got_port)
num = random.randint(0,100)
client.putreq(conn, "/adder", str(num))
visitor += 1
dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
dut1.expect("/adder PUT handler read " + str(num), timeout=30)
dut1.expect("PUT allocating new session", timeout=30)
client.end_session(conn)
dut1.expect("/adder Free Context function called", timeout=30)
if __name__ == '__main__':
test_examples_protocol_http_server_persistence()

View File

@ -34,11 +34,11 @@ def getreq (conn, path, verbose = False):
resp = conn.getresponse()
data = resp.read()
if verbose:
Utility.console_log("GET : " + path)
Utility.console_log("Status : " + resp.status)
Utility.console_log("Reason : " + resp.reason)
Utility.console_log("Data length : " + str(len(data)))
Utility.console_log("Data content : " + data)
print("GET : " + path)
print("Status : " + resp.status)
print("Reason : " + resp.reason)
print("Data length : " + str(len(data)))
print("Data content : " + data)
return data
def postreq (conn, path, data, verbose = False):
@ -46,11 +46,11 @@ def postreq (conn, path, data, verbose = False):
resp = conn.getresponse()
data = resp.read()
if verbose:
Utility.console_log("POST : " + data)
Utility.console_log("Status : " + resp.status)
Utility.console_log("Reason : " + resp.reason)
Utility.console_log("Data length : " + str(len(data)))
Utility.console_log("Data content : " + data)
print("POST : " + data)
print("Status : " + resp.status)
print("Reason : " + resp.reason)
print("Data length : " + str(len(data)))
print("Data content : " + data)
return data
def putreq (conn, path, body, verbose = False):
@ -58,11 +58,11 @@ def putreq (conn, path, body, verbose = False):
resp = conn.getresponse()
data = resp.read()
if verbose:
Utility.console_log("PUT : " + path, body)
Utility.console_log("Status : " + resp.status)
Utility.console_log("Reason : " + resp.reason)
Utility.console_log("Data length : " + str(len(data)))
Utility.console_log("Data content : " + data)
print("PUT : " + path, body)
print("Status : " + resp.status)
print("Reason : " + resp.reason)
print("Data length : " + str(len(data)))
print("Data content : " + data)
return data
if __name__ == '__main__':
@ -79,22 +79,22 @@ if __name__ == '__main__':
N = args['N']
# Establish HTTP connection
Utility.console_log("Connecting to => " + ip + ":" + port)
print("Connecting to => " + ip + ":" + port)
conn = start_session (ip, port)
# Reset adder context to specified value(0)
# -- Not needed as new connection will always
# -- have zero value of the accumulator
Utility.console_log("Reset the accumulator to 0")
print("Reset the accumulator to 0")
putreq (conn, "/adder", str(0))
# Sum numbers from 1 to specified value(N)
Utility.console_log("Summing numbers from 1 to " + str(N))
print("Summing numbers from 1 to " + str(N))
for i in range(1, N+1):
postreq (conn, "/adder", str(i))
# Fetch the result
Utility.console_log("Result :" + getreq (conn, "/adder"))
print("Result :" + getreq (conn, "/adder"))
# Close HTTP connection
end_session (conn)