Skip to content

Commit 87f45d1

Browse files
jukkarcarlescufi
authored andcommitted
tests: net: http_server: Add tests for the HTTP server
Tests for HTTP server support. Signed-off-by: Emna Rekik <[email protected]> Signed-off-by: Jukka Rissanen <[email protected]> Signed-off-by: Robert Lubos <[email protected]>
1 parent 4b157b9 commit 87f45d1

File tree

20 files changed

+1168
-1
lines changed

20 files changed

+1168
-1
lines changed

tests/net/lib/http_server/common/prj.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ CONFIG_TEST_RANDOM_GENERATOR=y
77
CONFIG_ZTEST_STACK_SIZE=1024
88

99
CONFIG_HTTP_SERVER=y
10+
CONFIG_EVENTFD=y
11+
CONFIG_POSIX_API=y
12+
13+
# Networking config
14+
CONFIG_NET_SOCKETS=y

tests/net/lib/http_server/common/testcase.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ common:
66
- server
77
integration_platforms:
88
- native_sim
9-
9+
platform_exclude:
10+
- native_posix
11+
- native_posix/native/64
1012
tests:
1113
net.http.server.common: {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(crime)
6+
7+
set(BASE_PATH "../../../../../subsys/net/lib/http/")
8+
include_directories(${BASE_PATH}/headers)
9+
10+
FILE(GLOB app_sources src/main.c)
11+
target_sources(app PRIVATE ${app_sources})
12+
13+
set(gen_dir ${ZEPHYR_BINARY_DIR}/include/generated/)
14+
15+
set(source_file_index src/index.html)
16+
generate_inc_file_for_target(app ${source_file_index} ${gen_dir}/index.html.inc)
17+
generate_inc_file_for_target(app ${source_file_index} ${gen_dir}/index.html.gz.inc --gzip)
18+
19+
set(source_file_not_found src/not_found_page.html)
20+
generate_inc_file_for_target(app ${source_file_not_found} ${gen_dir}/not_found_page.html.inc)
21+
generate_inc_file_for_target(app ${source_file_not_found} ${gen_dir}/not_found_page.html.gz.inc --gzip)
22+
23+
zephyr_linker_sources(SECTIONS sections-rom.ld)
24+
zephyr_iterable_section(NAME http_resource_desc_test_http_service KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_NET_TEST=y
3+
4+
# Eventfd
5+
CONFIG_EVENTFD=y
6+
CONFIG_POSIX_API=y
7+
8+
CONFIG_ENTROPY_GENERATOR=y
9+
CONFIG_TEST_RANDOM_GENERATOR=y
10+
CONFIG_ZTEST_STACK_SIZE=1024
11+
12+
CONFIG_POSIX_MAX_FDS=10
13+
CONFIG_REQUIRES_FULL_LIBC=y
14+
CONFIG_EVENTFD_MAX=10
15+
CONFIG_NET_MAX_CONTEXTS=10
16+
CONFIG_NET_MAX_CONN=10
17+
18+
# Networking config
19+
CONFIG_NETWORKING=y
20+
CONFIG_NET_IPV4=y
21+
CONFIG_NET_IPV6=y
22+
CONFIG_NET_TCP=y
23+
CONFIG_NET_SOCKETS=y
24+
CONFIG_NET_LOOPBACK=y
25+
CONFIG_NET_LOOPBACK_MTU=1280
26+
CONFIG_NET_DRIVERS=y
27+
CONFIG_NET_SOCKETS_POLL_MAX=8
28+
CONFIG_NET_BUF_RX_COUNT=32
29+
CONFIG_NET_BUF_TX_COUNT=32
30+
CONFIG_NET_PKT_RX_COUNT=16
31+
CONFIG_NET_PKT_TX_COUNT=16
32+
33+
# Reduce the retry count, so the close always finishes within a second
34+
CONFIG_NET_TCP_RETRY_COUNT=3
35+
CONFIG_NET_TCP_INIT_RETRANSMISSION_TIMEOUT=120
36+
37+
# JSON
38+
CONFIG_JSON_LIBRARY=y
39+
40+
# HTTP parser
41+
CONFIG_HTTP_PARSER_URL=y
42+
CONFIG_HTTP_PARSER=y
43+
CONFIG_HTTP_SERVER=y
44+
45+
CONFIG_HTTP_SERVER_MAX_CLIENTS=5
46+
CONFIG_HTTP_SERVER_MAX_STREAMS=5
47+
48+
# Network address config
49+
CONFIG_NET_CONFIG_SETTINGS=n
50+
51+
CONFIG_MAIN_STACK_SIZE=2048
52+
53+
# Network debug config
54+
CONFIG_NET_LOG=y
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <zephyr/linker/iterable_sections.h>
2+
3+
ITERABLE_SECTION_ROM(http_resource_desc_test_http_service, 4)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>My Simple Server</title>
5+
</head>
6+
<body>
7+
<h1>Welcome to my simple server!</h1>
8+
<p>This is a simple HTML file.</p>
9+
</body>
10+
</html>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2023, Emna Rekik
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "server_internal.h"
8+
9+
#include <string.h>
10+
11+
#include <zephyr/net/http/service.h>
12+
#include <zephyr/net/socket.h>
13+
#include <zephyr/ztest.h>
14+
15+
#define BUFFER_SIZE 256
16+
#define MY_IPV4_ADDR "127.0.0.1"
17+
#define SERVER_PORT 8080
18+
#define TIMEOUT 1000
19+
20+
static const unsigned char index_html_gz[] = {
21+
#include "index.html.gz.inc"
22+
};
23+
24+
static const unsigned char compressed_inc_file[] = {
25+
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
26+
0x02, 0xff, 0x35, 0x8e, 0xc1, 0x0a, 0xc2, 0x30,
27+
0x0c, 0x86, 0xef, 0x7d, 0x8a, 0xec, 0x05, 0x2c,
28+
0xbb, 0x87, 0x5c, 0x54, 0xf0, 0xe0, 0x50, 0x58,
29+
0x41, 0x3c, 0x4e, 0x17, 0x69, 0x21, 0xa5, 0x65,
30+
0x2d, 0x42, 0xdf, 0xde, 0xba, 0x6e, 0x21, 0x10,
31+
0xf8, 0xf9, 0xbe, 0x9f, 0x60, 0x77, 0xba, 0x1d,
32+
0xcd, 0xf3, 0x7e, 0x06, 0x9b, 0xbd, 0x90, 0xc2,
33+
0xfd, 0xf0, 0x34, 0x93, 0x82, 0x3a, 0x98, 0x5d,
34+
0x16, 0xa6, 0xa1, 0xc0, 0xe8, 0x7c, 0x14, 0x86,
35+
0x91, 0x97, 0x2f, 0x2f, 0xa8, 0x5b, 0xae, 0x50,
36+
0x37, 0x16, 0x5f, 0x61, 0x2e, 0x9b, 0x62, 0x7b,
37+
0x7a, 0xb0, 0xbc, 0x83, 0x67, 0xc8, 0x01, 0x7c,
38+
0x81, 0xd4, 0xd4, 0xb4, 0xaa, 0x5d, 0x55, 0xfa,
39+
0x8d, 0x8c, 0x64, 0xac, 0x4b, 0x50, 0x77, 0xda,
40+
0xa1, 0x8b, 0x19, 0xae, 0xf0, 0x71, 0xc2, 0x07,
41+
0xd4, 0xf1, 0xdf, 0xdf, 0x8a, 0xab, 0xb4, 0xbe,
42+
0xf6, 0x03, 0xea, 0x2d, 0x11, 0x5c, 0xb2, 0x00,
43+
0x00, 0x00,
44+
};
45+
46+
static uint16_t test_http_service_port = SERVER_PORT;
47+
HTTP_SERVICE_DEFINE(test_http_service, MY_IPV4_ADDR,
48+
&test_http_service_port, 1,
49+
10, NULL);
50+
51+
struct http_resource_detail_static index_html_gz_resource_detail = {
52+
.common = {
53+
.type = HTTP_RESOURCE_TYPE_STATIC,
54+
.bitmask_of_supported_http_methods = BIT(HTTP_GET),
55+
},
56+
.static_data = index_html_gz,
57+
.static_data_len = sizeof(index_html_gz),
58+
};
59+
60+
HTTP_RESOURCE_DEFINE(index_html_gz_resource, test_http_service, "/",
61+
&index_html_gz_resource_detail);
62+
63+
static void test_crime(void)
64+
{
65+
int ret, recv_len;
66+
int client_fd;
67+
int proto = IPPROTO_TCP;
68+
char *ptr;
69+
const char *data;
70+
size_t len;
71+
struct sockaddr_in sa;
72+
static unsigned char buf[512];
73+
74+
zassert_ok(http_server_start(), "Failed to start the server");
75+
76+
ret = zsock_socket(AF_INET, SOCK_STREAM, proto);
77+
zassert_not_equal(ret, -1, "failed to create client socket (%d)", errno);
78+
client_fd = ret;
79+
80+
sa.sin_family = AF_INET;
81+
sa.sin_port = htons(SERVER_PORT);
82+
83+
ret = zsock_inet_pton(AF_INET, MY_IPV4_ADDR, &sa.sin_addr.s_addr);
84+
zassert_not_equal(-1, ret, "inet_pton() failed (%d)", errno);
85+
zassert_not_equal(0, ret, "%s is not a valid IPv4 address", MY_IPV4_ADDR);
86+
zassert_equal(1, ret, "inet_pton() failed to convert %s", MY_IPV4_ADDR);
87+
88+
memset(buf, '\0', sizeof(buf));
89+
ptr = (char *)zsock_inet_ntop(AF_INET, &sa.sin_addr, buf, sizeof(buf));
90+
zassert_not_equal(ptr, NULL, "inet_ntop() failed (%d)", errno);
91+
92+
ret = zsock_connect(client_fd, (struct sockaddr *)&sa, sizeof(sa));
93+
zassert_not_equal(ret, -1, "failed to connect (%s/%d)", strerror(errno), errno);
94+
95+
char *http1_request = "GET / HTTP/1.1\r\n"
96+
"Host: 127.0.0.1:8080\r\n"
97+
"Accept: */*\r\n"
98+
"Accept-Encoding: deflate, gzip, br\r\n"
99+
"\r\n";
100+
101+
ret = zsock_send(client_fd, http1_request, strlen(http1_request), 0);
102+
zassert_not_equal(ret, -1, "send() failed (%d)", errno);
103+
104+
memset(buf, 0, sizeof(buf));
105+
recv_len = zsock_recv(client_fd, buf, sizeof(buf), 0);
106+
zassert_not_equal(recv_len, -1, "recv() failed (%d)", errno);
107+
108+
len = sizeof(index_html_gz);
109+
110+
while (recv_len < len) {
111+
ret = zsock_recv(client_fd, buf + recv_len, sizeof(buf) - recv_len, 0);
112+
zassert_not_equal(ret, -1, "recv() failed (%d)", errno);
113+
114+
recv_len += ret;
115+
}
116+
117+
data = strstr(buf, "\r\n\r\n");
118+
zassert_not_null(data, "Header not found");
119+
120+
data += 4;
121+
122+
zassert_equal(len, sizeof(compressed_inc_file), "Invalid compressed file size");
123+
124+
ret = memcmp(data, compressed_inc_file, len);
125+
zassert_equal(ret, 0,
126+
"inc_file and compressed_inc_file contents are not identical (%d)", ret);
127+
128+
ret = zsock_close(client_fd);
129+
zassert_not_equal(-1, ret, "close() failed on the client fd (%d)", errno);
130+
131+
zassert_ok(http_server_stop(), "Failed to stop the server");
132+
}
133+
134+
ZTEST(framework_tests_crime, test_gen_gz_inc_file)
135+
{
136+
test_crime();
137+
}
138+
139+
ZTEST_SUITE(framework_tests_crime, NULL, NULL, NULL, NULL, NULL);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>404 Not Found</title>
5+
</head>
6+
<body>
7+
<h1>404 Not Found</h1>
8+
<p>The requested resource was not found.</p>
9+
</body>
10+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
common:
2+
harness: net
3+
min_ram: 16
4+
tags:
5+
- http
6+
- net
7+
- server
8+
- socket
9+
integration_platforms:
10+
- native_sim
11+
- qemu_x86
12+
platform_exclude:
13+
- native_posix
14+
- native_posix/native/64
15+
tests:
16+
net.http.server.crime: {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(prototype)
6+
7+
set(BASE_PATH "../../../../../subsys/net/lib/http/")
8+
include_directories(${BASE_PATH}/headers)
9+
10+
FILE(GLOB app_sources src/main.c)
11+
target_sources(app PRIVATE ${app_sources})
12+
13+
set(gen_dir ${ZEPHYR_BINARY_DIR}/include/generated/)
14+
15+
target_link_libraries(app PRIVATE zephyr_interface zephyr)
16+
17+
zephyr_linker_sources(SECTIONS sections-rom.ld)
18+
zephyr_iterable_section(NAME http_resource_desc_test_http_service KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)

0 commit comments

Comments
 (0)