From 17a635b23b35bfd0f2676c3ccbe883424d9e1ca2 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 19 Jun 2024 11:08:39 +0200 Subject: [PATCH] fix(esp_netif): Add unit test of getter/setter API for DNS servers --- components/esp_netif/include/esp_netif.h | 2 +- .../test_app_esp_netif/main/esp_netif_test.c | 76 +++++++++++++++++++ .../test_app_esp_netif/pytest_esp_netif.py | 7 +- .../sdkconfig.ci.dns_per_netif | 1 + .../sdkconfig.ci.global_dns | 1 + 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.dns_per_netif create mode 100644 components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.global_dns diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index fed84aa335..f5df24632b 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -772,7 +772,7 @@ esp_err_t esp_netif_dhcps_get_clients_by_mac(esp_netif_t *esp_netif, int num, es * Note that LWIP stores DNS server information globally, not per interface, so the first parameter is unused * in the default LWIP configuration. * If CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=1 this API sets internal DNS server information per - * netif. It's also possible set the global DNS server info by supplying esp_netif=NULL + * netif. It's also possible to set the global DNS server info by supplying esp_netif=NULL * * If DHCP server is enabled, the Main DNS Server setting is used by the DHCP server to provide a DNS Server option * to DHCP clients (Wi-Fi stations). diff --git a/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c b/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c index 84705bc784..080f565ec5 100644 --- a/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c +++ b/components/esp_netif/test_apps/test_app_esp_netif/main/esp_netif_test.c @@ -502,6 +502,81 @@ TEST(esp_netif, route_priority) } } +// to probe DNS server info directly in LWIP +const ip_addr_t * dns_getserver(u8_t numdns); + +TEST(esp_netif, set_get_dnsserver) +{ + // create a couple of netifs + test_case_uses_tcpip(); + const char *if_keys[] = {"if0", "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9"}; + const int nr_of_netifs = sizeof(if_keys) / sizeof(char *); + esp_netif_t *netifs[nr_of_netifs]; + esp_netif_driver_ifconfig_t driver_config = { .handle = (void*)1, .transmit = dummy_transmit }; + // create 10 netifs with different route prio + for (int i = 0; i < nr_of_netifs; ++i) { + esp_netif_inherent_config_t base_netif_config = { .if_key = if_keys[i], .route_prio = i }; + esp_netif_config_t cfg = { .base = &base_netif_config, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, + .driver = &driver_config }; + netifs[i] = esp_netif_new(&cfg); + TEST_ASSERT_NOT_NULL(netifs[i]); + // set the interface up and connected -- to enable the default netif based on route_prio + esp_netif_action_start(netifs[i], 0, 0, 0); + esp_netif_action_connected(netifs[i], 0, 0, 0); + } + + esp_netif_dns_info_t dns[2]; + esp_netif_dns_info_t get_dns; + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip4("1.2.3.4", &dns[0].ip.u_addr.ip4)); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip4("5.6.7.8", &dns[1].ip.u_addr.ip4)); + + // set DNS info to one netif + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_set_dns_info(netifs[3], ESP_NETIF_DNS_MAIN, &dns[0])); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_set_dns_info(netifs[3], ESP_NETIF_DNS_BACKUP, &dns[1])); +#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF + // check that calling setters/getters with 'esp_netif==NULL' is invalid + TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_set_dns_info(NULL, ESP_NETIF_DNS_BACKUP, &dns[1])); + TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_get_dns_info(NULL, ESP_NETIF_DNS_BACKUP, &get_dns)); + + // check that the global DNS is configured the same way + const ip_addr_t *ip = dns_getserver(0); + TEST_ASSERT_EQUAL(ip->u_addr.ip4.addr, dns[0].ip.u_addr.ip4.addr); + ip = dns_getserver(1); + TEST_ASSERT_EQUAL(ip->u_addr.ip4.addr, dns[1].ip.u_addr.ip4.addr); + + // check that we get the same DNS information for all netifs + for (int i=0; i < nr_of_netifs; ++i) { + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_dns_info(netifs[i], ESP_NETIF_DNS_MAIN, &get_dns)); + TEST_ASSERT_EQUAL(get_dns.ip.u_addr.ip4.addr, dns[0].ip.u_addr.ip4.addr); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_dns_info(netifs[i], ESP_NETIF_DNS_BACKUP, &get_dns)); + TEST_ASSERT_EQUAL(get_dns.ip.u_addr.ip4.addr, dns[1].ip.u_addr.ip4.addr); + } +#else + // check that calling setters/getters with 'esp_netif==NULL' is valid, they set/get global DNS servers + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_set_dns_info(NULL, ESP_NETIF_DNS_MAIN, &dns[0])); + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_dns_info(NULL, ESP_NETIF_DNS_BACKUP, &get_dns)); + const ip_addr_t *ip = dns_getserver(0); + TEST_ASSERT_EQUAL(ip->u_addr.ip4.addr, dns[0].ip.u_addr.ip4.addr); + ip = dns_getserver(1); + TEST_ASSERT_EQUAL(ip->u_addr.ip4.addr, get_dns.ip.u_addr.ip4.addr); // same as what we got at the esp-netif layer + TEST_ASSERT_NOT_EQUAL(ip->u_addr.ip4.addr, dns[1].ip.u_addr.ip4.addr); // but different from what we set earlier per netif + + // now we set the netif[3] as default one and check again + esp_netif_set_default_netif(netifs[3]); + ip = dns_getserver(1); + TEST_ASSERT_EQUAL(ip->u_addr.ip4.addr, dns[1].ip.u_addr.ip4.addr); // now the ESP_NETIF_DNS_BACKUP[3[ should be set globally + + // check that we get a different DNS server with another netif + TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_dns_info(netifs[5], ESP_NETIF_DNS_MAIN, &get_dns)); + TEST_ASSERT_NOT_EQUAL(dns[0].ip.u_addr.ip4.addr, get_dns.ip.u_addr.ip4.addr); +#endif + + for (int i=0; i < nr_of_netifs; ++i) { + esp_netif_destroy(netifs[i]); + TEST_ASSERT_FALSE(esp_netif_is_netif_listed(netifs[i])); + } +} TEST_GROUP_RUNNER(esp_netif) { @@ -531,6 +606,7 @@ TEST_GROUP_RUNNER(esp_netif) RUN_TEST_CASE(esp_netif, dhcp_server_state_transitions_mesh) #endif RUN_TEST_CASE(esp_netif, route_priority) + RUN_TEST_CASE(esp_netif, set_get_dnsserver) } void app_main(void) diff --git a/components/esp_netif/test_apps/test_app_esp_netif/pytest_esp_netif.py b/components/esp_netif/test_apps/test_app_esp_netif/pytest_esp_netif.py index 841d825360..43c0125637 100644 --- a/components/esp_netif/test_apps/test_app_esp_netif/pytest_esp_netif.py +++ b/components/esp_netif/test_apps/test_app_esp_netif/pytest_esp_netif.py @@ -1,6 +1,5 @@ -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: CC0-1.0 - import pytest from pytest_embedded import Dut @@ -8,5 +7,9 @@ from pytest_embedded import Dut @pytest.mark.esp32s2 @pytest.mark.esp32c3 @pytest.mark.generic +@pytest.mark.parametrize('config', [ + 'global_dns', + 'dns_per_netif', +], indirect=True) def test_esp_netif(dut: Dut) -> None: dut.expect_unity_test_output() diff --git a/components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.dns_per_netif b/components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.dns_per_netif new file mode 100644 index 0000000000..3abbf3a3bc --- /dev/null +++ b/components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.dns_per_netif @@ -0,0 +1 @@ +CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=y diff --git a/components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.global_dns b/components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.global_dns new file mode 100644 index 0000000000..ba627cbf81 --- /dev/null +++ b/components/esp_netif/test_apps/test_app_esp_netif/sdkconfig.ci.global_dns @@ -0,0 +1 @@ +CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=n