From 8b45ac0fbcf398888cb14e20b9c2ac0e775f1712 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 14 Sep 2021 16:30:46 +0200 Subject: [PATCH] lwip: Add ping test case (moved from eth component) --- components/esp_eth/test/test_emac.c | 140 +------------------------- components/lwip/test/CMakeLists.txt | 2 + components/lwip/test/component.mk | 4 + components/lwip/test/test_lwip_apps.c | 106 +++++++++++++++++++ 4 files changed, 113 insertions(+), 139 deletions(-) create mode 100644 components/lwip/test/CMakeLists.txt create mode 100644 components/lwip/test/component.mk create mode 100644 components/lwip/test/test_lwip_apps.c diff --git a/components/esp_eth/test/test_emac.c b/components/esp_eth/test/test_emac.c index b5a5f35762..cd47098b56 100644 --- a/components/esp_eth/test/test_emac.c +++ b/components/esp_eth/test/test_emac.c @@ -9,10 +9,6 @@ #include "esp_eth.h" #include "esp_log.h" #include "esp_http_client.h" -#include "lwip/inet.h" -#include "lwip/netdb.h" -#include "lwip/sockets.h" -#include "ping/ping_sock.h" #include "esp_rom_md5.h" #include "soc/soc_caps.h" @@ -24,18 +20,13 @@ static const char *TAG = "esp32_eth_test"; #define ETH_STOP_BIT BIT(1) #define ETH_CONNECT_BIT BIT(2) #define ETH_GOT_IP_BIT BIT(3) -#define ETH_PING_END_BIT BIT(4) -#define ETH_DOWNLOAD_END_BIT BIT(5) +#define ETH_DOWNLOAD_END_BIT BIT(4) #define ETH_START_TIMEOUT_MS (10000) #define ETH_CONNECT_TIMEOUT_MS (40000) #define ETH_STOP_TIMEOUT_MS (10000) #define ETH_GET_IP_TIMEOUT_MS (60000) #define ETH_DOWNLOAD_END_TIMEOUT_MS (240000) -#define ETH_PING_DURATION_MS (5000) -#define ETH_PING_END_TIMEOUT_MS (ETH_PING_DURATION_MS * 2) - -#define TEST_ICMP_DESTINATION_DOMAIN_NAME "127.0.0.1" extern const char dl_espressif_com_root_cert_pem_start[] asm("_binary_dl_espressif_com_root_cert_pem_start"); extern const char dl_espressif_com_root_cert_pem_end[] asm("_binary_dl_espressif_com_root_cert_pem_end"); @@ -86,46 +77,6 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base, xEventGroupSetBits(eth_event_group, ETH_GOT_IP_BIT); } -static void test_on_ping_success(esp_ping_handle_t hdl, void *args) -{ - uint8_t ttl; - uint16_t seqno; - uint32_t elapsed_time, recv_len; - ip_addr_t target_addr; - esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); - esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); - esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len)); - esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time)); - printf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", - recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time); -} - -static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args) -{ - uint16_t seqno; - ip_addr_t target_addr; - esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); - esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); - printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno); -} - -static void test_on_ping_end(esp_ping_handle_t hdl, void *args) -{ - EventGroupHandle_t eth_event_group = (EventGroupHandle_t)args; - uint32_t transmitted; - uint32_t received; - uint32_t total_time_ms; - - esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted)); - esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received)); - esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms)); - printf("%d packets transmitted, %d received, time %dms\n", transmitted, received, total_time_ms); - if (transmitted == received) { - xEventGroupSetBits(eth_event_group, ETH_PING_END_BIT); - } -} - static esp_err_t test_uninstall_driver(esp_eth_handle_t eth_hdl, uint32_t ms_to_wait) { int i = 0; @@ -306,95 +257,6 @@ TEST_CASE("esp32 ethernet start/stop stress test", "[ethernet][test_env=UT_T2_Et vEventGroupDelete(eth_event_group); } -TEST_CASE("esp32 ethernet icmp test", "[ethernet][test_env=UT_T2_Ethernet]") -{ - EventBits_t bits = 0; - EventGroupHandle_t eth_event_group = xEventGroupCreate(); - TEST_ASSERT(eth_event_group != NULL); - test_case_uses_tcpip(); - TEST_ESP_OK(esp_event_loop_create_default()); - // create TCP/IP netif - esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH(); - esp_netif_t *eth_netif = esp_netif_new(&netif_cfg); - - eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); - eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); - esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); - esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); - esp_eth_handle_t eth_handle = NULL; - TEST_ESP_OK(esp_eth_driver_install(ð_config, ð_handle)); - // combine driver with netif - esp_eth_netif_glue_handle_t glue = esp_eth_new_netif_glue(eth_handle); - TEST_ESP_OK(esp_netif_attach(eth_netif, glue)); - // register user defined event handers - TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_group)); - TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group)); - // start Ethernet driver - TEST_ESP_OK(esp_eth_start(eth_handle)); - /* wait for IP lease */ - bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS)); - TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT); - - // Parse IP address - ip_addr_t target_addr; - struct addrinfo hint; - struct addrinfo *res = NULL; - memset(&hint, 0, sizeof(hint)); - memset(&target_addr, 0, sizeof(target_addr)); - /* convert URL to IP */ - TEST_ASSERT(getaddrinfo(TEST_ICMP_DESTINATION_DOMAIN_NAME, NULL, &hint, &res) == 0); - struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr; - inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); - freeaddrinfo(res); - - esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG(); - ping_config.timeout_ms = 2000; - ping_config.target_addr = target_addr; - ping_config.count = 0; // ping in infinite mode - /* set callback functions */ - esp_ping_callbacks_t cbs; - cbs.on_ping_success = test_on_ping_success; - cbs.on_ping_timeout = test_on_ping_timeout; - cbs.on_ping_end = test_on_ping_end; - cbs.cb_args = eth_event_group; - - esp_ping_handle_t ping; - TEST_ESP_OK(esp_ping_new_session(&ping_config, &cbs, &ping)); - /* start ping */ - TEST_ESP_OK(esp_ping_start(ping)); - /* ping for a while */ - vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS)); - /* stop ping */ - TEST_ESP_OK(esp_ping_stop(ping)); - /* wait for end of ping */ - bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS)); - TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT); - /* restart ping */ - TEST_ESP_OK(esp_ping_start(ping)); - vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS)); - TEST_ESP_OK(esp_ping_stop(ping)); - bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS)); - TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT); - /* de-initialize ping process */ - TEST_ESP_OK(esp_ping_delete_session(ping)); - - // stop Ethernet driver - TEST_ESP_OK(esp_eth_stop(eth_handle)); - /* wait for connection stop */ - bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS)); - TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); - TEST_ESP_OK(esp_eth_del_netif_glue(glue)); - /* driver should be uninstalled within 2 seconds */ - TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000)); - TEST_ESP_OK(phy->del(phy)); - TEST_ESP_OK(mac->del(mac)); - TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler)); - esp_netif_destroy(eth_netif); - TEST_ESP_OK(esp_event_loop_delete_default()); - vEventGroupDelete(eth_event_group); -} - esp_err_t http_event_handle(esp_http_client_event_t *evt) { switch (evt->event_id) { diff --git a/components/lwip/test/CMakeLists.txt b/components/lwip/test/CMakeLists.txt new file mode 100644 index 0000000000..568494320b --- /dev/null +++ b/components/lwip/test/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRC_DIRS "." + PRIV_REQUIRES test_utils) diff --git a/components/lwip/test/component.mk b/components/lwip/test/component.mk new file mode 100644 index 0000000000..8c6eb513e4 --- /dev/null +++ b/components/lwip/test/component.mk @@ -0,0 +1,4 @@ +# +#Component Makefile +# +COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive diff --git a/components/lwip/test/test_lwip_apps.c b/components/lwip/test/test_lwip_apps.c new file mode 100644 index 0000000000..bf55b5f38a --- /dev/null +++ b/components/lwip/test/test_lwip_apps.c @@ -0,0 +1,106 @@ +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "test_utils.h" +#include "unity.h" +#include "lwip/inet.h" +#include "lwip/netdb.h" +#include "lwip/sockets.h" +#include "ping/ping_sock.h" + +#define ETH_PING_END_BIT BIT(1) +#define ETH_PING_DURATION_MS (5000) +#define ETH_PING_END_TIMEOUT_MS (ETH_PING_DURATION_MS * 2) +#define TEST_ICMP_DESTINATION_DOMAIN_NAME "127.0.0.1" + +static void test_on_ping_success(esp_ping_handle_t hdl, void *args) +{ + uint8_t ttl; + uint16_t seqno; + uint32_t elapsed_time, recv_len; + ip_addr_t target_addr; + esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); + esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl)); + esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); + esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len)); + esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time)); + printf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", + recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time); +} + +static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args) +{ + uint16_t seqno; + ip_addr_t target_addr; + esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); + esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); + printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno); +} + +static void test_on_ping_end(esp_ping_handle_t hdl, void *args) +{ + EventGroupHandle_t eth_event_group = (EventGroupHandle_t)args; + uint32_t transmitted; + uint32_t received; + uint32_t total_time_ms; + + esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted)); + esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received)); + esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms)); + printf("%d packets transmitted, %d received, time %dms\n", transmitted, received, total_time_ms); + if (transmitted == received) { + xEventGroupSetBits(eth_event_group, ETH_PING_END_BIT); + } +} + +TEST_CASE("localhost ping test", "[lwip]") +{ + EventBits_t bits; + EventGroupHandle_t eth_event_group = xEventGroupCreate(); + TEST_ASSERT(eth_event_group != NULL); + test_case_uses_tcpip(); + + // Parse IP address: Destination is a localhost address, so we don't need any interface (esp-netif/driver) + ip_addr_t target_addr; + struct addrinfo hint; + struct addrinfo *res = NULL; + memset(&hint, 0, sizeof(hint)); + memset(&target_addr, 0, sizeof(target_addr)); + /* convert URL to IP */ + TEST_ASSERT(getaddrinfo(TEST_ICMP_DESTINATION_DOMAIN_NAME, NULL, &hint, &res) == 0); + struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr; + inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4); + freeaddrinfo(res); + + esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG(); + ping_config.timeout_ms = 2000; + ping_config.target_addr = target_addr; + ping_config.count = 0; // ping in infinite mode + /* set callback functions */ + esp_ping_callbacks_t cbs; + cbs.on_ping_success = test_on_ping_success; + cbs.on_ping_timeout = test_on_ping_timeout; + cbs.on_ping_end = test_on_ping_end; + cbs.cb_args = eth_event_group; + + esp_ping_handle_t ping; + TEST_ESP_OK(esp_ping_new_session(&ping_config, &cbs, &ping)); + /* start ping */ + TEST_ESP_OK(esp_ping_start(ping)); + /* ping for a while */ + vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS)); + /* stop ping */ + TEST_ESP_OK(esp_ping_stop(ping)); + /* wait for end of ping */ + bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS)); + TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT); + /* restart ping */ + TEST_ESP_OK(esp_ping_start(ping)); + vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS)); + TEST_ESP_OK(esp_ping_stop(ping)); + bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS)); + TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT); + /* de-initialize ping process */ + TEST_ESP_OK(esp_ping_delete_session(ping)); + + vEventGroupDelete(eth_event_group); +}