Skip to content

net: lib: dns: dns_cache: add tests for dns_cache_remove() #92179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions subsys/net/lib/dns/dns_cache.c
Original file line number Diff line number Diff line change
@@ -75,6 +75,10 @@ int dns_cache_add(struct dns_cache *cache, char const *query, struct dns_addrinf

int dns_cache_remove(struct dns_cache *cache, char const *query)
{
if (cache == NULL || query == NULL) {
return -EINVAL;
}

NET_DBG("Remove all entries with query \"%s\"", query);
if (strlen(query) >= CONFIG_DNS_RESOLVER_MAX_QUERY_LEN) {
NET_WARN("Query string to big to be processed %u >= "
93 changes: 93 additions & 0 deletions tests/net/lib/dns_cache/src/main.c
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/fff.h>
#include <zephyr/ztest.h>
#include "dns_cache.h"
@@ -174,3 +175,95 @@ ZTEST(net_dns_cache_test, test_only_expected_type_returned)
zassert_equal(1, dns_cache_find(&test_dns_cache, query, query_type_b, &info_read, 1));
zassert_equal(AF_INET6, info_read.ai_family);
}

ZTEST(net_dns_cache_test, test_remove_single_entry)
{
struct dns_addrinfo info_write = {.ai_family = AF_INET};
struct dns_addrinfo info_read = {0};
const char *query = "example.com";
enum dns_query_type query_type = DNS_QUERY_TYPE_A;

zassert_ok(dns_cache_add(&test_dns_cache, query, &info_write, TEST_DNS_CACHE_DEFAULT_TTL),
"Cache entry adding should work.");
zassert_equal(1, dns_cache_find(&test_dns_cache, query, query_type, &info_read, 1));
zassert_equal(AF_INET, info_read.ai_family);

zassert_ok(dns_cache_remove(&test_dns_cache, query), "Cache entry removal should work.");
zassert_equal(0, dns_cache_find(&test_dns_cache, query, query_type, &info_read, 1));
}

ZTEST(net_dns_cache_test, test_remove_multiple_entries_same_query)
{
struct dns_addrinfo info_write_a = {.ai_family = AF_INET};
struct dns_addrinfo info_write_b = {.ai_family = AF_INET6};
struct dns_addrinfo info_read[2] = {0};
const char *query = "example.com";
enum dns_query_type query_type_a = DNS_QUERY_TYPE_A;
enum dns_query_type query_type_b = DNS_QUERY_TYPE_AAAA;

zassert_ok(dns_cache_add(&test_dns_cache, query, &info_write_a, TEST_DNS_CACHE_DEFAULT_TTL),
"Cache entry adding should work.");
zassert_ok(dns_cache_add(&test_dns_cache, query, &info_write_b, TEST_DNS_CACHE_DEFAULT_TTL),
"Cache entry adding should work.");
zassert_equal(1, dns_cache_find(&test_dns_cache, query, query_type_a, &info_read[0], 1));
zassert_equal(1, dns_cache_find(&test_dns_cache, query, query_type_b, &info_read[0], 1));

zassert_ok(dns_cache_remove(&test_dns_cache, query), "Cache entry removal should work.");
zassert_equal(0, dns_cache_find(&test_dns_cache, query, query_type_a, &info_read[0], 1));
zassert_equal(0, dns_cache_find(&test_dns_cache, query, query_type_b, &info_read[0], 1));
}

ZTEST(net_dns_cache_test, test_remove_specific_query_only)
{
struct dns_addrinfo info_write = {.ai_family = AF_INET};
struct dns_addrinfo info_read = {0};
const char *query1 = "example.com";
const char *query2 = "test.com";
enum dns_query_type query_type = DNS_QUERY_TYPE_A;

zassert_ok(dns_cache_add(&test_dns_cache, query1, &info_write, TEST_DNS_CACHE_DEFAULT_TTL),
"Cache entry adding should work.");
zassert_ok(dns_cache_add(&test_dns_cache, query2, &info_write, TEST_DNS_CACHE_DEFAULT_TTL),
"Cache entry adding should work.");
zassert_equal(1, dns_cache_find(&test_dns_cache, query1, query_type, &info_read, 1));
zassert_equal(1, dns_cache_find(&test_dns_cache, query2, query_type, &info_read, 1));

zassert_ok(dns_cache_remove(&test_dns_cache, query1), "Cache entry removal should work.");
zassert_equal(0, dns_cache_find(&test_dns_cache, query1, query_type, &info_read, 1));
zassert_equal(1, dns_cache_find(&test_dns_cache, query2, query_type, &info_read, 1));
zassert_equal(AF_INET, info_read.ai_family);
}

ZTEST(net_dns_cache_test, test_remove_nonexistent_query)
{
struct dns_addrinfo info_write = {.ai_family = AF_INET};
struct dns_addrinfo info_read = {0};
const char *query = "example.com";
const char *nonexistent_query = "nonexistent.com";
enum dns_query_type query_type = DNS_QUERY_TYPE_A;

zassert_ok(dns_cache_add(&test_dns_cache, query, &info_write, TEST_DNS_CACHE_DEFAULT_TTL),
"Cache entry adding should work.");
zassert_equal(1, dns_cache_find(&test_dns_cache, query, query_type, &info_read, 1));

zassert_ok(dns_cache_remove(&test_dns_cache, nonexistent_query),
"Removing nonexistent query should not fail.");
zassert_equal(1, dns_cache_find(&test_dns_cache, query, query_type, &info_read, 1));
zassert_equal(AF_INET, info_read.ai_family);
}

ZTEST(net_dns_cache_test, test_remove_empty_cache)
{
const char *query = "example.com";

zassert_ok(dns_cache_remove(&test_dns_cache, query),
"Removing from empty cache should not fail.");
}

ZTEST(net_dns_cache_test, test_remove_null_parameters)
{
zassert_equal(-EINVAL, dns_cache_remove(NULL, "example.com"),
"NULL cache should return error.");
zassert_equal(-EINVAL, dns_cache_remove(&test_dns_cache, NULL),
"NULL query should return error.");
}