-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sockutls): Add simple unit test
- Loading branch information
1 parent
62ee719
commit e77e0d7
Showing
8 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
set(COMPONENTS main) | ||
|
||
#list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") | ||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
|
||
project(sockutls_host_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
idf_component_register(SRCS "test_sock_utils.cpp" | ||
INCLUDE_DIRS "$ENV{IDF_PATH}/tools" | ||
WHOLE_ARCHIVE) | ||
|
||
target_compile_options(${COMPONENT_LIB} PUBLIC -fsanitize=address -fconcepts) | ||
target_link_options(${COMPONENT_LIB} PUBLIC -fsanitize=address) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
dependencies: | ||
espressif/catch2: "^3.4.0" | ||
sock_utils: | ||
version: "*" | ||
override_path: '../../../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include "ifaddrs.h" | ||
#include "esp_netif.h" | ||
#include "esp_event.h" | ||
#define CATCH_CONFIG_MAIN | ||
#include "catch2/catch_test_macros.hpp" | ||
#include "catch2/catch_session.hpp" | ||
#include <catch2/matchers/catch_matchers_all.hpp> | ||
|
||
static char buffer[64]; | ||
|
||
TEST_CASE("getnameinfo() for IPv4", "[sock_utils]") | ||
{ | ||
struct sockaddr_in sock_addr = {}; | ||
sock_addr.sin_family = AF_INET; | ||
sock_addr.sin_port = 257; // (0x0101: same number for LE and BE) | ||
REQUIRE(getnameinfo((struct sockaddr *)&sock_addr, sizeof(sock_addr), buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST) == 0); | ||
CHECK(strcmp("0.0.0.0", buffer) == 0); | ||
CHECK(getnameinfo((struct sockaddr *)&sock_addr, sizeof(sock_addr), NULL, 0, buffer, sizeof(buffer), NI_NUMERICSERV) == 0); | ||
CHECK(strcmp("257", buffer) == 0); | ||
} | ||
|
||
TEST_CASE("getnameinfo() for IPv6", "[sock_utils]") | ||
{ | ||
struct sockaddr_in sock_addr = {}; | ||
sock_addr.sin_family = AF_INET6; | ||
// IPv6 not supported for now | ||
CHECK(getnameinfo((struct sockaddr *)&sock_addr, sizeof(sock_addr), buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST) != 0); | ||
} | ||
|
||
TEST_CASE("getifaddr()", "[sock_utils]") | ||
{ | ||
|
||
struct ifaddrs *addresses, *addr; | ||
REQUIRE(getifaddrs(&addresses) != -1); | ||
addr = addresses; | ||
CHECK(addr != NULL); | ||
int nr_of_addrs = 0; | ||
|
||
while (addr) { | ||
++nr_of_addrs; | ||
if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) { // look for IP4 addresses | ||
struct sockaddr_in *sock_addr = (struct sockaddr_in *) addr->ifa_addr; | ||
if (getnameinfo((struct sockaddr *)sock_addr, sizeof(*sock_addr), | ||
buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST) != 0) { | ||
printf("getnameinfo() failed\n"); | ||
} else { | ||
printf("IPv4 address of interface \"%s\": %s\n", addr->ifa_name, buffer); | ||
} | ||
} | ||
addr = addr->ifa_next; | ||
} | ||
// check that we got 1 address with exact content | ||
CHECK(nr_of_addrs == 1); | ||
CHECK(strcmp("1.2.3.4", buffer) == 0); | ||
freeifaddrs(addresses); | ||
} | ||
|
||
extern "C" void app_main(void) | ||
{ | ||
ESP_ERROR_CHECK(esp_netif_init()); | ||
ESP_ERROR_CHECK(esp_event_loop_create_default()); | ||
|
||
esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); | ||
esp_netif_ip_info_t ip = { }; | ||
ip.ip.addr = ESP_IP4TOADDR(1, 2, 3, 4); | ||
base_cfg.ip_info = &ip; | ||
esp_netif_config_t cfg = { .base = &base_cfg, .driver = NULL, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA }; | ||
esp_netif_t *esp_netif = esp_netif_new(&cfg); | ||
|
||
Catch::Session session; | ||
|
||
int failures = session.run(); | ||
if (failures > 0) { | ||
printf("TEST FAILED! number of failures=%d\n", failures); | ||
} else { | ||
printf("Test passed!\n"); | ||
} | ||
esp_netif_destroy(esp_netif); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# This file was generated using idf.py save-defconfig. It can be edited manually. | ||
# Espressif IoT Development Framework (ESP-IDF) 5.4.0 Project Minimal Configuration | ||
# | ||
CONFIG_IDF_TARGET="linux" | ||
CONFIG_COMPILER_CXX_EXCEPTIONS=y | ||
CONFIG_FREERTOS_HZ=1000 | ||
CONFIG_LWIP_ENABLE=y |