<commit_before>/*
 * Copyright (C) 2022 ML!PA Consulting GmbH
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

#include <stdint.h>
#include <string.h>
#include "net/af.h"
#include "net/ipv6.h"
#include "ztimer.h"

#include "net/dns/cache.h"

#include "tests-dns_cache.h"

static void test_dns_cache_add(void)
{
    ipv6_addr_t addr_in = IPV6_ADDR_ALL_NODES_IF_LOCAL;
    ipv6_addr_t addr_out;

    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));

    /* add DNS entry, set it to expire in 1s */
    dns_cache_add("example.com", &addr_in, sizeof(addr_in), 1);
    TEST_ASSERT_EQUAL_INT(sizeof(addr_out), dns_cache_query("example.com", &addr_out, AF_INET6));
    TEST_ASSERT_EQUAL_INT(0, memcmp(&addr_in, &addr_out, sizeof(addr_in)));

    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET));
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("alt.example.com", &addr_out, AF_INET6));
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.comm", &addr_out, AF_INET6));
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.co", &addr_out, AF_INET6));

    ztimer_sleep(ZTIMER_USEC, 2000000);
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
}

Test *tests_dns_cache_tests(void)
{
    EMB_UNIT_TESTFIXTURES(fixtures) {
        new_TestFixture(test_dns_cache_add),
    };

    EMB_UNIT_TESTCALLER(dns_cache_tests, NULL, NULL, fixtures);

    return (Test *)&dns_cache_tests;
}

void tests_dns_cache(void)
{
    TESTS_RUN(tests_dns_cache_tests());
}
<commit_msg>tests/unittests/tests-dns_cache: add test case for TTL=0<commit_after>/*
 * Copyright (C) 2022 ML!PA Consulting GmbH
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

#include <stdint.h>
#include <string.h>
#include "net/af.h"
#include "net/ipv6.h"
#include "ztimer.h"

#include "net/dns/cache.h"

#include "tests-dns_cache.h"

static void test_dns_cache_add(void)
{
    ipv6_addr_t addr_in = IPV6_ADDR_ALL_NODES_IF_LOCAL;
    ipv6_addr_t addr_out;

    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));

    /* add DNS entry, set it to expire in 1s */
    dns_cache_add("example.com", &addr_in, sizeof(addr_in), 1);
    TEST_ASSERT_EQUAL_INT(sizeof(addr_out), dns_cache_query("example.com", &addr_out, AF_INET6));
    TEST_ASSERT_EQUAL_INT(0, memcmp(&addr_in, &addr_out, sizeof(addr_in)));

    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET));
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("alt.example.com", &addr_out, AF_INET6));
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.comm", &addr_out, AF_INET6));
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.co", &addr_out, AF_INET6));

    ztimer_sleep(ZTIMER_USEC, 2000000);
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
}

static void test_dns_cache_add_ttl0(void)
{
    ipv6_addr_t addr_in = IPV6_ADDR_ALL_NODES_IF_LOCAL;
    ipv6_addr_t addr_out;

    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
    dns_cache_add("example.com", &addr_in, sizeof(addr_in), 0);
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
    dns_cache_add("example.com", &addr_in, sizeof(addr_in), 1);
    TEST_ASSERT_EQUAL_INT(sizeof(addr_out), dns_cache_query("example.com", &addr_out, AF_INET6));
    dns_cache_add("example.com", &addr_in, sizeof(addr_in), 0);
    TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
}

Test *tests_dns_cache_tests(void)
{
    EMB_UNIT_TESTFIXTURES(fixtures) {
        new_TestFixture(test_dns_cache_add),
        new_TestFixture(test_dns_cache_add_ttl0),
    };

    EMB_UNIT_TESTCALLER(dns_cache_tests, NULL, NULL, fixtures);

    return (Test *)&dns_cache_tests;
}

void tests_dns_cache(void)
{
    TESTS_RUN(tests_dns_cache_tests());
}

{'fraction_non_alphanumeric': 0.12737457379444714, 'fraction_numerical': 0.01948368241597662, 'mean_word_length': 5.560702875399361, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3948531', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1648, 'n_words': 280}