fix(esp_netif): Add unit test of getter/setter API for DNS servers

This commit is contained in:
David Cermak 2024-06-19 11:08:39 +02:00
parent 6acdb384f6
commit 17a635b23b
5 changed files with 84 additions and 3 deletions

View File

@ -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).

View File

@ -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)

View File

@ -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()

View File

@ -0,0 +1 @@
CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=y

View File

@ -0,0 +1 @@
CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=n