Skip to content

Commit 921b2a6

Browse files
committed
fix(esp_netif): Add unit test for loopback configuration
Split tests into common and lwip/loopback specific and adds loopback tests as separate configuration.
1 parent c6748a6 commit 921b2a6

File tree

10 files changed

+190
-47
lines changed

10 files changed

+190
-47
lines changed

components/esp_netif/loopback/esp_netif_loopback.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,8 @@ esp_err_t esp_netif_tcpip_exec(esp_netif_callback_fn fn, void*ctx)
480480
return fn(ctx);
481481
}
482482

483+
esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
484+
{
485+
return esp_netif_get_handle_from_ifkey_unsafe(if_key);
486+
}
483487
#endif /* CONFIG_ESP_NETIF_LOOPBACK */
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
idf_component_register(SRCS "esp_netif_test.c"
2-
REQUIRES test_utils
3-
INCLUDE_DIRS "."
4-
PRIV_INCLUDE_DIRS "$ENV{IDF_PATH}/components/esp_netif/private_include" "."
5-
PRIV_REQUIRES unity esp_netif nvs_flash esp_wifi)
1+
if(CONFIG_ESP_NETIF_TCPIP_LWIP)
2+
set(srcs_test_stack esp_netif_test_lwip.c)
3+
elseif(CONFIG_ESP_NETIF_LOOPBACK)
4+
set(srcs_test_stack esp_netif_test_loopback.c)
5+
endif()
6+
7+
idf_component_register(SRCS esp_netif_test.c ${srcs_test_stack}
8+
REQUIRES test_utils
9+
INCLUDE_DIRS "."
10+
PRIV_INCLUDE_DIRS "$ENV{IDF_PATH}/components/esp_netif/private_include" "."
11+
PRIV_REQUIRES unity esp_netif nvs_flash esp_wifi)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include "unity.h"
9+
#include "unity_fixture.h"
10+
#include "esp_netif.h"
11+
#include "esp_netif_sntp.h"
12+
#include "esp_netif_net_stack.h"
13+
#include "esp_wifi.h"
14+
#include "nvs_flash.h"
15+
#include "esp_wifi_netif.h"
16+
#include "sdkconfig.h"
17+
#include "test_utils.h"
18+
#include "memory_checks.h"
19+
#include "lwip/netif.h"
20+
21+
//// This is a private esp-netif API, but include here to test it
22+
bool esp_netif_is_netif_listed(esp_netif_t *esp_netif);
23+
24+
void create_delete_multiple_netifs(void)
25+
{
26+
// interface key has to be a unique identifier
27+
const char* if_keys[] = { "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9" };
28+
const int nr_of_netifs = sizeof(if_keys)/sizeof(char*);
29+
esp_netif_t *netifs[nr_of_netifs];
30+
31+
// create 10 wifi stations
32+
for (int i=0; i<nr_of_netifs; ++i) {
33+
esp_netif_inherent_config_t base_netif_config = { .if_key = if_keys[i]};
34+
esp_netif_config_t cfg = { .base = &base_netif_config, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
35+
netifs[i] = esp_netif_new(&cfg);
36+
TEST_ASSERT_NOT_NULL(netifs[i]);
37+
}
38+
39+
// there's no AP within created stations
40+
TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"));
41+
42+
// check that the created netifs are correctly found by their interface keys and globally listed
43+
for (int i=0; i<nr_of_netifs; ++i) {
44+
TEST_ASSERT_EQUAL(netifs[i], esp_netif_get_handle_from_ifkey(if_keys[i]));
45+
TEST_ASSERT_TRUE(esp_netif_is_netif_listed(netifs[i]));
46+
}
47+
48+
// destroy one by one and check it's been removed
49+
for (int i=0; i<nr_of_netifs; ++i) {
50+
esp_netif_destroy(netifs[i]);
51+
TEST_ASSERT_FALSE(esp_netif_is_netif_listed(netifs[i]));
52+
}
53+
}
54+
55+
56+
void get_from_if_key(void)
57+
{
58+
// init default netif
59+
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
60+
esp_netif_t *esp_netif = esp_netif_new(&cfg);
61+
TEST_ASSERT_NOT_NULL(esp_netif);
62+
63+
// check it's accessible by key
64+
TEST_ASSERT_EQUAL(esp_netif, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
65+
66+
// destroy it
67+
esp_netif_destroy(esp_netif);
68+
69+
// check it's also destroyed in list
70+
TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
71+
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#pragma once
7+
8+
#include "unity.h"
9+
#include "unity_fixture.h"
10+
#include "esp_netif.h"
11+
12+
// List of tests that are common for both configurations
13+
void create_delete_multiple_netifs(void);
14+
void get_from_if_key(void);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include "unity.h"
9+
#include "unity_fixture.h"
10+
#include "esp_netif.h"
11+
#include "memory_checks.h"
12+
#include "esp_netif_test.h"
13+
14+
TEST_GROUP(esp_netif);
15+
16+
TEST_SETUP(esp_netif)
17+
{
18+
test_utils_record_free_mem();
19+
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
20+
}
21+
22+
TEST_TEAR_DOWN(esp_netif)
23+
{
24+
test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL),
25+
test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL));
26+
}
27+
28+
TEST(esp_netif, create_delete_multiple_netifs)
29+
{
30+
create_delete_multiple_netifs();
31+
}
32+
33+
TEST(esp_netif, get_from_if_key)
34+
{
35+
get_from_if_key();
36+
}
37+
38+
TEST_GROUP_RUNNER(esp_netif)
39+
{
40+
RUN_TEST_CASE(esp_netif, create_delete_multiple_netifs)
41+
RUN_TEST_CASE(esp_netif, get_from_if_key)
42+
}
43+
44+
void app_main(void)
45+
{
46+
UNITY_MAIN(esp_netif);
47+
}
48+
49+
const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wifi_sta = (esp_netif_netstack_config_t*)1;

components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test_lwip.c

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "test_utils.h"
1818
#include "memory_checks.h"
1919
#include "lwip/netif.h"
20+
#include "esp_netif_test.h"
2021

2122
TEST_GROUP(esp_netif);
2223

@@ -91,62 +92,28 @@ TEST(esp_netif, convert_ip_addresses)
9192

9293
TEST(esp_netif, get_from_if_key)
9394
{
94-
// init default netif
95-
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
96-
esp_netif_t *esp_netif = esp_netif_new(&cfg);
97-
TEST_ASSERT_NOT_NULL(esp_netif);
98-
99-
// check it's accessible by key
100-
TEST_ASSERT_EQUAL(esp_netif, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
101-
102-
// destroy it
103-
esp_netif_destroy(esp_netif);
104-
105-
// check it's also destroyed in list
106-
TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
107-
95+
get_from_if_key();
10896
}
10997

11098
// This is a private esp-netif API, but include here to test it
11199
bool esp_netif_is_netif_listed(esp_netif_t *esp_netif);
112100

113101
TEST(esp_netif, create_delete_multiple_netifs)
114102
{
115-
// interface key has to be a unique identifier
116-
const char* if_keys[] = { "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9" };
117-
const int nr_of_netifs = sizeof(if_keys)/sizeof(char*);
118-
esp_netif_t *netifs[nr_of_netifs];
119-
120-
// create 10 wifi stations
121-
for (int i=0; i<nr_of_netifs; ++i) {
122-
esp_netif_inherent_config_t base_netif_config = { .if_key = if_keys[i]};
123-
esp_netif_config_t cfg = { .base = &base_netif_config, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
124-
netifs[i] = esp_netif_new(&cfg);
125-
TEST_ASSERT_NOT_NULL(netifs[i]);
126-
}
127-
128-
// there's no AP within created stations
129-
TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"));
130-
131-
// check that the created netifs are correctly found by their interface keys and globally listed
132-
for (int i=0; i<nr_of_netifs; ++i) {
133-
TEST_ASSERT_EQUAL(netifs[i], esp_netif_get_handle_from_ifkey(if_keys[i]));
134-
TEST_ASSERT_TRUE(esp_netif_is_netif_listed(netifs[i]));
135-
}
136-
137-
// destroy one by one and check it's been removed
138-
for (int i=0; i<nr_of_netifs; ++i) {
139-
esp_netif_destroy(netifs[i]);
140-
TEST_ASSERT_FALSE(esp_netif_is_netif_listed(netifs[i]));
141-
}
142-
103+
create_delete_multiple_netifs();
143104
}
144105

145106
static bool desc_matches_with(esp_netif_t *netif, void *ctx)
146107
{
147108
return strcmp(ctx, esp_netif_get_desc(netif)) == 0;
148109
}
149110

111+
/*
112+
* This test validates esp_netif_find_if() API by searching in the list of netifs
113+
* by their description using the predicate function desc_matches_with() above.
114+
* These netifs have the same key and description, so we can use esp_netif_get_handle_from_ifkey()
115+
* to validate the test.
116+
*/
150117
TEST(esp_netif, find_netifs)
151118
{
152119
// Create some interfaces
@@ -181,6 +148,10 @@ TEST(esp_netif, find_netifs)
181148
}
182149

183150
#ifdef CONFIG_ESP_WIFI_ENABLED
151+
/*
152+
* This test creates a default WiFi station and checks all possible transitions
153+
* of the DHCP client used by the station.
154+
*/
184155
TEST(esp_netif, dhcp_client_state_transitions_wifi_sta)
185156
{
186157
// init default wifi netif
@@ -230,6 +201,10 @@ TEST(esp_netif, dhcp_client_state_transitions_wifi_sta)
230201
#endif // CONFIG_ESP_WIFI_ENABLED
231202

232203
#if defined(CONFIG_ESP_WIFI_ENABLED) && defined(CONFIG_ESP_WIFI_SOFTAP_SUPPORT)
204+
/*
205+
* This test creates a default WiFi AP and checks all possible transitions
206+
* of the DHCP server used by the soft AP.
207+
*/
233208
TEST(esp_netif, dhcp_server_state_transitions_wifi_ap)
234209
{
235210
// init default wifi netif
@@ -272,6 +247,10 @@ TEST(esp_netif, dhcp_server_state_transitions_wifi_ap)
272247
nvs_flash_deinit();
273248
}
274249

250+
/*
251+
* This test creates a default mesh interfaces and checks all possible transitions
252+
* of the DHCP client and server used by these netifs.
253+
*/
275254
TEST(esp_netif, dhcp_server_state_transitions_mesh)
276255
{
277256
esp_netif_t *ap = NULL;
@@ -336,6 +315,10 @@ TEST(esp_netif, dhcp_server_state_transitions_mesh)
336315
#endif // CONFIG_ESP_WIFI_ENABLED && CONFIG_ESP_WIFI_SOFTAP_SUPPORT
337316

338317
#ifdef CONFIG_ESP_WIFI_ENABLED
318+
/*
319+
* This test validates convenience API esp_netif_create_wifi() which creates WiFi station
320+
* or API with the specified inherent network config.
321+
*/
339322
TEST(esp_netif, create_custom_wifi_interfaces)
340323
{
341324
esp_netif_t *ap = NULL;
@@ -449,6 +432,14 @@ static esp_err_t dummy_transmit(void* hd, void *buf, size_t length)
449432
return ESP_OK;
450433
}
451434

435+
/*
436+
* This test validates the route priority of multiple netifs. It checks that the default route (default netif)
437+
* is set correctly for the netifs according to their `route_prio` value and `link_up` state.
438+
* - We create 10 netifs with prios: 0, 1, 2, 3, 4, 0, 0, ...., 0 (netifs[nr_of_netifs/2] has max_prio)
439+
* - We check the default netif is correct after bringing it down/up, overriding it
440+
* - We destroy the default netif and check again
441+
* - We destroy the remaining netifs
442+
*/
452443
TEST(esp_netif, route_priority)
453444
{
454445
test_case_uses_tcpip();

components/esp_netif/test_apps/test_app_esp_netif/pytest_esp_netif.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@pytest.mark.parametrize('config', [
1111
'global_dns',
1212
'dns_per_netif',
13+
'loopback', # test config without LWIP
1314
], indirect=True)
1415
def test_esp_netif(dut: Dut) -> None:
1516
dut.expect_unity_test_output()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
CONFIG_ESP_NETIF_TCPIP_LWIP=y
2+
CONFIG_ESP_NETIF_LOOPBACK=n
13
CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=y
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
CONFIG_ESP_NETIF_TCPIP_LWIP=y
2+
CONFIG_ESP_NETIF_LOOPBACK=n
13
CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=n
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ESP_NETIF_LOOPBACK=y
2+
CONFIG_ESP_NETIF_TCPIP_LWIP=n

0 commit comments

Comments
 (0)