Skip to content

Commit

Permalink
unittests: add test for dns_msg
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 11, 2024
1 parent 3c8e44d commit 5f5b6c7
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/unittests/tests-dns_msg/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
5 changes: 5 additions & 0 deletions tests/unittests/tests-dns_msg/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
USEMODULE += dns_msg

USEMODULE += gnrc_ipv6
USEMODULE += sock_udp
USEMODULE += posix_inet
129 changes: 129 additions & 0 deletions tests/unittests/tests-dns_msg/tests-dns_msg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (C) 2024 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/msg.h"

#include "tests-dns_msg.h"

static void test_dns_msg_valid_google(void)
{
const uint8_t dns_msg[] = {
0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x06, 0x67, 0x6f, 0x6f,
0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00,
0x00, 0x1c, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x1c,
0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x10,
0x2a, 0x00, 0x14, 0x50, 0x40, 0x05, 0x08, 0x0b,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0e,
};
const uint8_t addr[] = {
0x2a, 0x00, 0x14, 0x50, 0x40, 0x05, 0x08, 0x0b,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0e,
};
const uint32_t ttl = 300;

uint8_t addr_out[16];
uint32_t ttl_out;
int res = dns_msg_parse_reply(dns_msg, sizeof(dns_msg), AF_INET6, &addr_out, &ttl_out);
TEST_ASSERT_EQUAL_INT(16, res);
TEST_ASSERT_EQUAL_INT(ttl, ttl_out);
TEST_ASSERT_EQUAL_INT(0, memcmp(addr, addr_out, sizeof(addr)));
}

static void test_dns_msg_valid_dns64_github(void)
{
const uint8_t dns_msg[] = {
0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x06, 0x67, 0x69, 0x74,
0x68, 0x75, 0x62, 0x03, 0x63, 0x6f, 0x6d, 0x00,
0x00, 0x1c, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x1c,
0x00, 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x10,
0x20, 0x01, 0x06, 0x7c, 0x02, 0xb0, 0xdb, 0x32,
0x00, 0x00, 0x00, 0x01, 0x8c, 0x52, 0x79, 0x03,
};
const uint8_t addr[] = {
0x20, 0x01, 0x06, 0x7c, 0x02, 0xb0, 0xdb, 0x32,
0x00, 0x00, 0x00, 0x01, 0x8c, 0x52, 0x79, 0x03,
};
const uint32_t ttl = 60;

uint8_t addr_out[16];
uint32_t ttl_out;
int res = dns_msg_parse_reply(dns_msg, sizeof(dns_msg), AF_INET6, &addr_out, &ttl_out);
TEST_ASSERT_EQUAL_INT(16, res);
TEST_ASSERT_EQUAL_INT(ttl, ttl_out);
TEST_ASSERT_EQUAL_INT(0, memcmp(addr, addr_out, sizeof(addr)));
}

static void test_dns_msg_valid_dns64_azure(void)
{
const uint8_t dns_msg[] = {
0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00, 0x06, 0x67, 0x6c, 0x6f,
0x62, 0x61, 0x6c, 0x1a, 0x61, 0x7a, 0x75, 0x72,
0x65, 0x2d, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65,
0x73, 0x2d, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x73,
0x69, 0x6f, 0x6e, 0x69, 0x6e, 0x67, 0x03, 0x6e,
0x65, 0x74, 0x00, 0x00, 0x1c, 0x00, 0x01, 0xc0,
0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x02,
0x58, 0x00, 0x29, 0x17, 0x69, 0x64, 0x2d, 0x70,
0x72, 0x6f, 0x64, 0x2d, 0x67, 0x6c, 0x6f, 0x62,
0x61, 0x6c, 0x2d, 0x65, 0x6e, 0x64, 0x70, 0x6f,
0x69, 0x6e, 0x74, 0x0e, 0x74, 0x72, 0x61, 0x66,
0x66, 0x69, 0x63, 0x6d, 0x61, 0x6e, 0x61, 0x67,
0x65, 0x72, 0xc0, 0x2e, 0xc0, 0x43, 0x00, 0x05,
0x00, 0x01, 0x00, 0x00, 0x00, 0x5a, 0x00, 0x37,
0x16, 0x69, 0x64, 0x73, 0x75, 0x2d, 0x70, 0x72,
0x6f, 0x64, 0x2d, 0x64, 0x62, 0x2d, 0x30, 0x30,
0x31, 0x2d, 0x73, 0x75, 0x2d, 0x61, 0x7a, 0x0b,
0x6e, 0x6f, 0x72, 0x74, 0x68, 0x65, 0x75, 0x72,
0x6f, 0x70, 0x65, 0x08, 0x63, 0x6c, 0x6f, 0x75,
0x64, 0x61, 0x70, 0x70, 0x05, 0x61, 0x7a, 0x75,
0x72, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0xc0,
0x78, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x00,
0x0a, 0x00, 0x10, 0x20, 0x01, 0x06, 0x7c, 0x02,
0xb0, 0xdb, 0x32, 0x00, 0x00, 0x00, 0x01, 0x14,
0x32, 0x41, 0x8d,
};
const uint8_t addr[] = {
0x20, 0x01, 0x06, 0x7c, 0x02, 0xb0, 0xdb, 0x32,
0x00, 0x00, 0x00, 0x01, 0x14, 0x32, 0x41, 0x8d,
};
const uint32_t ttl = 10;

uint8_t addr_out[16];
uint32_t ttl_out;
int res = dns_msg_parse_reply(dns_msg, sizeof(dns_msg), AF_INET6, &addr_out, &ttl_out);
TEST_ASSERT_EQUAL_INT(16, res);
TEST_ASSERT_EQUAL_INT(ttl, ttl_out);
TEST_ASSERT_EQUAL_INT(0, memcmp(addr, addr_out, sizeof(addr)));
}

Test *tests_dns_msg_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_dns_msg_valid_google),
new_TestFixture(test_dns_msg_valid_dns64_github),
new_TestFixture(test_dns_msg_valid_dns64_azure),
};

EMB_UNIT_TESTCALLER(dns_msg_tests, NULL, NULL, fixtures);

return (Test *)&dns_msg_tests;
}

void tests_dns_msg(void)
{
TESTS_RUN(tests_dns_msg_tests());
}
44 changes: 44 additions & 0 deletions tests/unittests/tests-dns_msg/tests-dns_msg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2024 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.
*/

/**
* @addtogroup unittests
* @{
*
* @file
* @brief Unittests for the `dns_msg` module
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef TESTS_DNS_MSG_H
#define TESTS_DNS_MSG_H

#include "embUnit.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief The entry point of this test suite.
*/
void tests_dns_msg(void);

/**
* @brief Generates tests for dns_msg
*
* @return embUnit tests if successful, NULL if not.
*/
Test *tests_dns_msg_tests(void);

#ifdef __cplusplus
}
#endif

#endif /* TESTS_DNS_MSG_H */
/** @} */

0 comments on commit 5f5b6c7

Please sign in to comment.