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)
This commit is contained in:
David Cermak 2023-03-13 19:09:21 +01:00
parent 285362e8d6
commit a18d019fc9
6 changed files with 64 additions and 42 deletions

View File

@ -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_FILENAME "lwip_default_hooks.h"
#define LWIP_HOOK_IP4_ROUTE_SRC ip4_route_src_hook #define LWIP_HOOK_IP4_ROUTE_SRC ip4_route_src_hook
#if LWIP_NETCONN_FULLDUPLEX #if LWIP_NETCONN_FULLDUPLEX
#define LWIP_DONE_SOCK(s) done_socket(sock) #define LWIP_DONE_SOCK(sock) done_socket(sock)
#else #else
#define LWIP_DONE_SOCK(s) ((void)1) #define LWIP_DONE_SOCK(sock) ((void)1)
#endif /* LWIP_NETCONN_FULLDUPLEX */ #endif /* LWIP_NETCONN_FULLDUPLEX */
#define LWIP_HOOK_SOCKETS_GETSOCKOPT(s, sock, level, optname, optval, optlen, err) \ #define LWIP_HOOK_SOCKETS_GETSOCKOPT(s, sock, level, optname, optval, optlen, err) \

View File

@ -10,6 +10,7 @@
#include "lwip/inet.h" #include "lwip/inet.h"
#include "lwip/netdb.h" #include "lwip/netdb.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "lwip/tcpip.h"
#include "ping/ping_sock.h" #include "ping/ping_sock.h"
#include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h" #include "dhcpserver/dhcpserver_options.h"
@ -125,12 +126,18 @@ TEST_CASE("dhcp server init/deinit", "[lwip][leaks=0]")
dhcps_delete(dhcps); dhcps_delete(dhcps);
} }
TEST_CASE("dhcp server start/stop on localhost", "[lwip]") struct dhcps_api {
{ EventGroupHandle_t event;
test_case_uses_tcpip();
struct netif *netif;
dhcps_t *dhcps;
ip4_addr_t netmask; 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) { NETIF_FOREACH(netif) {
if (netif->name[0] == 'l' && netif->name[1] == 'o') { 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); TEST_ASSERT_NOT_NULL(netif);
//Class A dhcps_t *dhcps = dhcps_new();
dhcps = dhcps_new(); dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&api->netmask, sizeof(api->netmask));
IP4_ADDR(&netmask, 255,0,0,0); api->ret_start = dhcps_start(dhcps, netif, api->ip);
dhcps_set_option_info(dhcps, SUBNET_MASK, (void*)&netmask, sizeof(netmask)); api->ret_stop = dhcps_stop(dhcps, netif);
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_delete(dhcps); 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);
} }

View File

@ -19,3 +19,4 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_ETH_PHY_ADDR=1
CONFIG_EXAMPLE_CONNECT_IPV6=y CONFIG_EXAMPLE_CONNECT_IPV6=y
CONFIG_LWIP_CHECK_THREAD_SAFETY=y

View File

@ -9,3 +9,5 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_ETH_PHY_ADDR=1
CONFIG_EXAMPLE_CONNECT_IPV6=y CONFIG_EXAMPLE_CONNECT_IPV6=y
CONFIG_LWIP_TCPIP_CORE_LOCKING=y
CONFIG_LWIP_CHECK_THREAD_SAFETY=y

View File

@ -8,3 +8,4 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_ETH_PHY_ADDR=1
CONFIG_EXAMPLE_CONNECT_IPV6=y CONFIG_EXAMPLE_CONNECT_IPV6=y
CONFIG_LWIP_CHECK_THREAD_SAFETY=y

View File

@ -9,3 +9,5 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18
CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5
CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_ETH_PHY_ADDR=1
CONFIG_EXAMPLE_CONNECT_IPV6=y CONFIG_EXAMPLE_CONNECT_IPV6=y
CONFIG_LWIP_TCPIP_CORE_LOCKING=y
CONFIG_LWIP_CHECK_THREAD_SAFETY=y