From a18d019fc9be035baa5737b4e394722b1883b4f5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 13 Mar 2023 19:09:21 +0100 Subject: [PATCH] lwip: Fix lwiopts macro expansion and dhcps-test Minor issue in lwipopts.h in macro expansion in parameters (cosmetic change: it expands correctly but doesn't pass the expected argument) Fix lwip thread safety issue in tests: dhcp server should be started and stopped only in lwip context/thread. Without this fix, the test would fail if `CONFIG_LWIP_CHECK_THREAD_SAFETY=y` Adds CONFIG_LWIP_TCPIP_CORE_LOCKING=y to some mqtt example tests (the same configs as used in v5.1) --- components/lwip/port/esp32/include/lwipopts.h | 4 +- components/lwip/test/test_lwip_apps.c | 96 +++++++++++-------- examples/protocols/mqtt/ssl/sdkconfig.ci | 1 + examples/protocols/mqtt/tcp/sdkconfig.ci | 2 + examples/protocols/mqtt/ws/sdkconfig.ci | 1 + examples/protocols/mqtt/wss/sdkconfig.ci | 2 + 6 files changed, 64 insertions(+), 42 deletions(-) diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 21c757a6f0..cd6ab9ab00 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -1208,9 +1208,9 @@ static inline uint32_t timeout_from_offered(uint32_t lease, uint32_t min) #define LWIP_HOOK_FILENAME "lwip_default_hooks.h" #define LWIP_HOOK_IP4_ROUTE_SRC ip4_route_src_hook #if LWIP_NETCONN_FULLDUPLEX -#define LWIP_DONE_SOCK(s) done_socket(sock) +#define LWIP_DONE_SOCK(sock) done_socket(sock) #else -#define LWIP_DONE_SOCK(s) ((void)1) +#define LWIP_DONE_SOCK(sock) ((void)1) #endif /* LWIP_NETCONN_FULLDUPLEX */ #define LWIP_HOOK_SOCKETS_GETSOCKOPT(s, sock, level, optname, optval, optlen, err) \ diff --git a/components/lwip/test/test_lwip_apps.c b/components/lwip/test/test_lwip_apps.c index ec00247eeb..1b2bec278c 100644 --- a/components/lwip/test/test_lwip_apps.c +++ b/components/lwip/test/test_lwip_apps.c @@ -10,6 +10,7 @@ #include "lwip/inet.h" #include "lwip/netdb.h" #include "lwip/sockets.h" +#include "lwip/tcpip.h" #include "ping/ping_sock.h" #include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver_options.h" @@ -125,12 +126,18 @@ TEST_CASE("dhcp server init/deinit", "[lwip][leaks=0]") dhcps_delete(dhcps); } -TEST_CASE("dhcp server start/stop on localhost", "[lwip]") -{ - test_case_uses_tcpip(); - struct netif *netif; - dhcps_t *dhcps; +struct dhcps_api { + EventGroupHandle_t event; ip4_addr_t netmask; + ip4_addr_t ip; + err_t ret_start; + err_t ret_stop; +}; + +static void dhcps_test_net_classes_api(void* ctx) +{ + struct netif *netif; + struct dhcps_api *api = ctx; NETIF_FOREACH(netif) { if (netif->name[0] == 'l' && netif->name[1] == 'o') { @@ -139,40 +146,49 @@ TEST_CASE("dhcp server start/stop on localhost", "[lwip]") } TEST_ASSERT_NOT_NULL(netif); - //Class A - dhcps = dhcps_new(); - IP4_ADDR(&netmask, 255,0,0,0); - dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask)); - ip4_addr_t a_ip = { .addr = 0x7f0001 }; - IP4_ADDR(&netmask, 255,0,0,0); - TEST_ASSERT(dhcps_start(dhcps, netif, a_ip) == ERR_OK); - TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK); - dhcps_delete(dhcps); - - //Class B - dhcps = dhcps_new(); - IP4_ADDR(&netmask, 255,255,0,0); - dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask)); - ip4_addr_t b_ip = { .addr = 0x1000080 }; - TEST_ASSERT(dhcps_start(dhcps, netif, b_ip) == ERR_OK); - TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK); - dhcps_delete(dhcps); - - //Class C - dhcps = dhcps_new(); - IP4_ADDR(&netmask, 255,255,255,0); - dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask)); - ip4_addr_t c_ip = { .addr = 0x101A8C0 }; - TEST_ASSERT(dhcps_start(dhcps, netif, c_ip) == ERR_OK); - TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK); - dhcps_delete(dhcps); - - // Class A: IP: 127.0.0.1, with inaccurate Mask: 255.248.255.0 - // expect dhcps_start() to fail - dhcps = dhcps_new(); - IP4_ADDR(&netmask, 255,248,255,0); - dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask)); - TEST_ASSERT(dhcps_start(dhcps, netif, a_ip) == ERR_ARG); - TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK); + dhcps_t *dhcps = dhcps_new(); + dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&api->netmask, sizeof(api->netmask)); + api->ret_start = dhcps_start(dhcps, netif, api->ip); + api->ret_stop = dhcps_stop(dhcps, netif); dhcps_delete(dhcps); + xEventGroupSetBits(api->event, 1); +} + +static void dhcps_test_net_classes(uint32_t ip, uint32_t mask, bool pass) +{ + + struct dhcps_api api = { + .ret_start = ERR_IF, + .ret_stop = ERR_IF, + .ip = {.addr = PP_HTONL(ip)}, + .netmask = {.addr = PP_HTONL(mask)}, + .event = xEventGroupCreate() + }; + + tcpip_callback(dhcps_test_net_classes_api, &api); + xEventGroupWaitBits(api.event, 1, true, true, pdMS_TO_TICKS(5000)); + vEventGroupDelete(api.event); + err_t ret_start_expected = pass ? ERR_OK : ERR_ARG; + TEST_ASSERT(api.ret_start == ret_start_expected); + TEST_ASSERT(api.ret_stop == ERR_OK); + +} + +TEST_CASE("dhcp server start/stop on localhost", "[lwip]") +{ + test_case_uses_tcpip(); + + // Class A: IP: 127.0.0.1, Mask: 255.0.0.0 + dhcps_test_net_classes(0x7f000001, 0xFF000000, true); + + // Class B: IP: 128.1.1.1, Mask: 255.255.0.0 + dhcps_test_net_classes(0x80010101, 0xFFFF0000, true); + + // Class C: IP: 192.168.1.1, Mask: 255.255.255.0 + dhcps_test_net_classes(0xC0A80101, 0xFFFFFF00, true); + + + // Class A: IP: 127.0.0.1, with inaccurate Mask: 255.248.255.0 + // expect dhcps_start() to fail + dhcps_test_net_classes(0x7f000001, 0xFFF8FF00, false); } diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index 3271d9678e..e8d4a52f63 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -19,3 +19,4 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci index 28ce9ac93d..acf084a9e4 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -9,3 +9,5 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_TCPIP_CORE_LOCKING=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci index 2f8e425e46..eebcafdead 100644 --- a/examples/protocols/mqtt/ws/sdkconfig.ci +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -8,3 +8,4 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci index f355614e4f..c8e8ef94a4 100644 --- a/examples/protocols/mqtt/wss/sdkconfig.ci +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -9,3 +9,5 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_TCPIP_CORE_LOCKING=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y