1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

unittests: add tests for inet_csum

This commit is contained in:
Martine Lenders 2015-03-28 14:03:29 +01:00
parent 903a9d54ce
commit 5d1ae12e8d
4 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1 @@
USEMODULE += ng_inet_csum

View File

@ -0,0 +1,128 @@
/*
* Copyright (C) 2014 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* 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.
*/
/**
* @{
*
* @file
*/
#include <errno.h>
#include <stdlib.h>
#include "embUnit.h"
#include "net/ng_inet_csum.h"
#include "unittests-constants.h"
#include "tests-inet_csum.h"
static void test_inet_csum__rfc_example(void)
{
/* source: https://tools.ietf.org/html/rfc1071#section-3 */
uint8_t data[] = {
0x00, 0x01, 0xf2, 0x03, 0xf4, 0xf5, 0xf6, 0xf7
};
TEST_ASSERT_EQUAL_INT(0xddf2, ng_inet_csum(0, data, sizeof(data)));
}
static void test_inet_csum__ipv6_pseudo_hdr(void)
{
/* source: https://www.cloudshark.org/captures/ea72fbab241b (No. 56) */
uint8_t data[] = {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 source */
0x5a, 0x6d, 0x8f, 0xff, 0xfe, 0x56, 0x30, 0x09,
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 destination */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x3a, /* payload length + next header */
0x86, 0x00, 0xab, 0x32, 0x40, 0x58, 0x07, 0x08, /* ICMPv6 payload */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x00, 0x1e,
0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00,
0x20, 0x02, 0x18, 0x3d, 0xdb, 0xa4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x58, 0x6d, 0x8f, 0x56, 0x30, 0x09
};
/* result unnormalized: take 1's-complement of 0 */
TEST_ASSERT_EQUAL_INT(0xffff, ng_inet_csum(0x0, data, sizeof(data)));
}
static void test_inet_csum__set_initial_sum(void)
{
/* source: https://www.cloudshark.org/captures/ea72fbab241b (No. 56) */
uint8_t data[] = {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 source */
0x5a, 0x6d, 0x8f, 0xff, 0xfe, 0x56, 0x30, 0x09,
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 destination */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x86, 0x00, 0xab, 0x32, 0x40, 0x58, 0x07, 0x08, /* ICMPv6 payload */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x00, 0x1e,
0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00,
0x20, 0x02, 0x18, 0x3d, 0xdb, 0xa4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x58, 0x6d, 0x8f, 0x56, 0x30, 0x09
};
/* result unnormalized: take 1's-complement of 0
* set next header and payload length as initial value */
TEST_ASSERT_EQUAL_INT(0xffff, ng_inet_csum(0x38 + 0x3a, data, sizeof(data)));
}
static void test_inet_csum__calculate_csum(void)
{
/* source: http://en.wikipedia.org/w/index.php?title=IPv4_header_checksum&oldid=645516564
* but left checksum 0 */
uint8_t data[] = {
0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40, 0x00,
0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x01,
0xc0, 0xa8, 0x00, 0xc7,
};
/* result unnormalized: take 1's-complement of 0xb861 */
TEST_ASSERT_EQUAL_INT(0x479e, ng_inet_csum(0, data, sizeof(data)));
}
static void test_inet_csum__odd_len(void)
{
/* source: https://www.cloudshark.org/captures/ea72fbab241b (No. 1) */
uint8_t data[] = {
0xc0, 0xa8, 0x01, 0x91, 0x4b, 0x4b, 0x4b, 0x4b, /* IPv4 source + dest*/
0xf6, 0xfb, 0x00, 0x35, 0x00, 0x27, 0xd1, 0xa2, /* UDP header */
0xa5, 0x6f, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, /* DNS payload */
0x00, 0x00, 0x00, 0x00, 0x09, 0x74, 0x65, 0x73,
0x74, 0x2d, 0x69, 0x70, 0x76, 0x36, 0x03, 0x63,
0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
};
/* result unnormalized: take 1's-complement of 0
* set next header and payload length as initial value */
TEST_ASSERT_EQUAL_INT(0xffff, ng_inet_csum(17 + 39, data, sizeof(data)));
}
Test *tests_inet_csum_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_inet_csum__rfc_example),
new_TestFixture(test_inet_csum__ipv6_pseudo_hdr),
new_TestFixture(test_inet_csum__set_initial_sum),
new_TestFixture(test_inet_csum__calculate_csum),
new_TestFixture(test_inet_csum__odd_len),
};
EMB_UNIT_TESTCALLER(inet_csum_tests, NULL, NULL, fixtures);
return (Test *)&inet_csum_tests;
}
void tests_inet_csum(void)
{
TESTS_RUN(tests_inet_csum_tests());
}
/** @} */

View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 Martin Lenders
*
* 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 tests-inet_csum.h
* @brief Unittests for the ``inet_csum`` module
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef TESTS_INET_CSUM_H_
#define TESTS_INET_CSUM_H_
#include "embUnit.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief The entry point of this test suite.
*/
void tests_inet_csum(void);
#ifdef __cplusplus
}
#endif
#endif /* TESTS_INET_CSUM_H_ */
/** @} */