test: net: buf_simple: Add unit tests for simple buffers

This adds set of unit tests for simple network buffers.
The suite is based of the test cases that are already defined for
network buffers.
Those test_net_buf_byte_order test case have been split to
smaller tests testing one functionality at a time.

Signed-off-by: Mariusz Skamra <mariusz.skamra@codecoup.pl>
This commit is contained in:
Mariusz Skamra 2023-03-07 13:42:37 +01:00 committed by Carles Cufí
parent 5862c8263b
commit 063c039480
7 changed files with 507 additions and 0 deletions

View file

@ -0,0 +1,18 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
project(buf_simple)
find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
add_subdirectory(${ZEPHYR_BASE}/tests/net/mocks net_mocks)
target_link_libraries(testbinary PRIVATE net_mocks)
target_sources(testbinary
PRIVATE
src/main.c
${ZEPHYR_BASE}/subsys/net/buf_simple.c
)

View file

@ -0,0 +1,10 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_ASSERT=y
CONFIG_ASSERT_LEVEL=2
CONFIG_ASSERT_VERBOSE=y
CONFIG_ASSERT_ON_ERRORS=y
CONFIG_NET_BUF=y
#CONFIG_NET_BUF_LOG=y
#CONFIG_NET_BUF_LOG_LEVEL_DBG=y
#CONFIG_NET_BUF_SIMPLE_LOG=y

View file

@ -0,0 +1,406 @@
/*
* Copyright (c) 2015 Intel Corporation
* Copyright (c) 2023 Codecoup
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/fff.h>
#include <zephyr/kernel.h>
#include <zephyr/net/buf.h>
DEFINE_FFF_GLOBALS;
NET_BUF_SIMPLE_DEFINE_STATIC(buf, 16);
static const uint8_t le16[2] = { 0x02, 0x01 };
static const uint8_t be16[2] = { 0x01, 0x02 };
static const uint8_t le24[3] = { 0x03, 0x02, 0x01 };
static const uint8_t be24[3] = { 0x01, 0x02, 0x03 };
static const uint8_t le32[4] = { 0x04, 0x03, 0x02, 0x01 };
static const uint8_t be32[4] = { 0x01, 0x02, 0x03, 0x04 };
static const uint8_t le48[6] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
static const uint8_t be48[6] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
static const uint8_t le64[8] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
static const uint8_t be64[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
static const uint16_t u16 = 0x0102;
static const uint32_t u24 = 0x010203;
static const uint32_t u32 = 0x01020304;
static const uint64_t u48 = 0x010203040506;
static const uint64_t u64 = 0x0102030405060708;
static void net_buf_simple_test_suite_before(void *f)
{
net_buf_simple_reset(&buf);
}
ZTEST_SUITE(net_buf_simple_test_suite, NULL, NULL,
net_buf_simple_test_suite_before, NULL, NULL);
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_clone)
{
struct net_buf_simple clone;
net_buf_simple_clone(&buf, &clone);
zassert_equal(buf.data, clone.data, "Incorrect clone data pointer");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le16)
{
net_buf_simple_add_mem(&buf, &le16, sizeof(le16));
zassert_equal(u16, net_buf_simple_pull_le16(&buf),
"Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be16)
{
net_buf_simple_add_mem(&buf, &be16, sizeof(be16));
zassert_equal(u16, net_buf_simple_pull_be16(&buf),
"Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le16)
{
net_buf_simple_add_le16(&buf, u16);
zassert_mem_equal(le16, net_buf_simple_pull_mem(&buf, sizeof(le16)),
sizeof(le16), "Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be16)
{
net_buf_simple_add_be16(&buf, u16);
zassert_mem_equal(be16, net_buf_simple_pull_mem(&buf, sizeof(be16)),
sizeof(be16), "Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le24)
{
net_buf_simple_add_mem(&buf, &le24, sizeof(le24));
zassert_equal(u24, net_buf_simple_pull_le24(&buf),
"Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be24)
{
net_buf_simple_add_mem(&buf, &be24, sizeof(be24));
zassert_equal(u24, net_buf_simple_pull_be24(&buf),
"Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le24)
{
net_buf_simple_add_le24(&buf, u24);
zassert_mem_equal(le24, net_buf_simple_pull_mem(&buf, sizeof(le24)),
sizeof(le24), "Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be24)
{
net_buf_simple_add_be24(&buf, u24);
zassert_mem_equal(be24, net_buf_simple_pull_mem(&buf, sizeof(be24)),
sizeof(be24), "Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le32)
{
net_buf_simple_add_mem(&buf, &le32, sizeof(le32));
zassert_equal(u32, net_buf_simple_pull_le32(&buf),
"Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be32)
{
net_buf_simple_add_mem(&buf, &be32, sizeof(be32));
zassert_equal(u32, net_buf_simple_pull_be32(&buf),
"Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le32)
{
net_buf_simple_add_le32(&buf, u32);
zassert_mem_equal(le32, net_buf_simple_pull_mem(&buf, sizeof(le32)),
sizeof(le32), "Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be32)
{
net_buf_simple_add_be32(&buf, u32);
zassert_mem_equal(be32, net_buf_simple_pull_mem(&buf, sizeof(be32)),
sizeof(be32), "Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le48)
{
net_buf_simple_add_mem(&buf, &le48, sizeof(le48));
zassert_equal(u48, net_buf_simple_pull_le48(&buf),
"Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be48)
{
net_buf_simple_add_mem(&buf, &be48, sizeof(be48));
zassert_equal(u48, net_buf_simple_pull_be48(&buf),
"Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le48)
{
net_buf_simple_add_le48(&buf, u48);
zassert_mem_equal(le48, net_buf_simple_pull_mem(&buf, sizeof(le48)),
sizeof(le48), "Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be48)
{
net_buf_simple_add_be48(&buf, u48);
zassert_mem_equal(be48, net_buf_simple_pull_mem(&buf, sizeof(be48)),
sizeof(be48), "Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le64)
{
net_buf_simple_add_mem(&buf, &le64, sizeof(le64));
zassert_equal(u64, net_buf_simple_pull_le64(&buf),
"Invalid 64 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be64)
{
net_buf_simple_add_mem(&buf, &be64, sizeof(be64));
zassert_equal(u64, net_buf_simple_pull_be64(&buf),
"Invalid 64 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le64)
{
net_buf_simple_add_le64(&buf, u64);
zassert_mem_equal(le64, net_buf_simple_pull_mem(&buf, sizeof(le64)),
sizeof(le64), "Invalid 64 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be64)
{
net_buf_simple_add_be64(&buf, u64);
zassert_mem_equal(be64, net_buf_simple_pull_mem(&buf, sizeof(be64)),
sizeof(be64), "Invalid 64 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le16)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &le16, sizeof(le16));
zassert_equal(u16, net_buf_simple_remove_le16(&buf),
"Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be16)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &be16, sizeof(be16));
zassert_equal(u16, net_buf_simple_remove_be16(&buf),
"Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le16)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_le16(&buf, u16);
zassert_mem_equal(le16, net_buf_simple_remove_mem(&buf, sizeof(le16)),
sizeof(le16), "Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be16)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_be16(&buf, u16);
zassert_mem_equal(be16, net_buf_simple_remove_mem(&buf, sizeof(be16)),
sizeof(be16), "Invalid 16 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le24)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &le24, sizeof(le24));
zassert_equal(u24, net_buf_simple_remove_le24(&buf),
"Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be24)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &be24, sizeof(be24));
zassert_equal(u24, net_buf_simple_remove_be24(&buf),
"Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le24)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_le24(&buf, u24);
zassert_mem_equal(le24, net_buf_simple_remove_mem(&buf, sizeof(le24)),
sizeof(le24), "Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be24)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_be24(&buf, u24);
zassert_mem_equal(be24, net_buf_simple_remove_mem(&buf, sizeof(be24)),
sizeof(be24), "Invalid 24 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le32)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &le32, sizeof(le32));
zassert_equal(u32, net_buf_simple_remove_le32(&buf),
"Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be32)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &be32, sizeof(be32));
zassert_equal(u32, net_buf_simple_remove_be32(&buf),
"Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le32)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_le32(&buf, u32);
zassert_mem_equal(le32, net_buf_simple_remove_mem(&buf, sizeof(le32)),
sizeof(le32), "Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be32)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_be32(&buf, u32);
zassert_mem_equal(be32, net_buf_simple_remove_mem(&buf, sizeof(be32)),
sizeof(be32), "Invalid 32 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le48)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &le48, sizeof(le48));
zassert_equal(u48, net_buf_simple_remove_le48(&buf),
"Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be48)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &be48, sizeof(be48));
zassert_equal(u48, net_buf_simple_remove_be48(&buf),
"Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le48)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_le48(&buf, u48);
zassert_mem_equal(le48, net_buf_simple_remove_mem(&buf, sizeof(le48)),
sizeof(le48), "Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be48)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_be48(&buf, u48);
zassert_mem_equal(be48, net_buf_simple_remove_mem(&buf, sizeof(be48)),
sizeof(be48), "Invalid 48 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le64)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &le64, sizeof(le64));
zassert_equal(u64, net_buf_simple_remove_le64(&buf),
"Invalid 64 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be64)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_mem(&buf, &be64, sizeof(be64));
zassert_equal(u64, net_buf_simple_remove_be64(&buf),
"Invalid 64 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le64)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_le64(&buf, u64);
zassert_mem_equal(le64, net_buf_simple_remove_mem(&buf, sizeof(le64)),
sizeof(le64), "Invalid 64 bits byte order");
}
ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be64)
{
net_buf_simple_reserve(&buf, 16);
net_buf_simple_push_be64(&buf, u64);
zassert_mem_equal(be64, net_buf_simple_remove_mem(&buf, sizeof(be64)),
sizeof(be64), "Invalid 64 bits byte order");
}

View file

@ -0,0 +1,5 @@
common:
tags: net buf
tests:
net.buf_simple:
type: unit

View file

@ -0,0 +1,13 @@
# SPDX-License-Identifier: Apache-2.0
add_library(net_mocks STATIC
assert.c
)
target_include_directories(net_mocks PUBLIC
${ZEPHYR_BASE}/subsys/net
${ZEPHYR_BASE}/tests/net
)
target_link_libraries(net_mocks PRIVATE test_interface)
target_compile_options(test_interface INTERFACE -include ztest.h)

39
tests/net/mocks/assert.c Normal file
View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include "assert.h"
DEFINE_FAKE_VALUE_FUNC(bool, mock_check_if_assert_expected);
void assert_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintk(fmt, ap);
va_end(ap);
}
void assert_post_action(const char *file, unsigned int line)
{
/* ztest_test_pass()/ztest_test_fail() are used to stop the execution
* If this is an unexpected assert (i.e. not following expect_assert())
* calling mock_check_if_assert_expected() will return 'false' as
* a default return value
*/
if (mock_check_if_assert_expected() == true) {
printk("Assertion expected as part of a test case.\n");
/* Mark the test as passed and stop execution:
* Needed in the passing scenario to prevent undefined behavior after hitting the
* assert. In real builds (non-UT), the system will be halted by the assert.
*/
ztest_test_pass();
} else {
/* Mark the test as failed and stop execution */
ztest_test_fail();
}
}

16
tests/net/mocks/assert.h Normal file
View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/fff.h>
#include <zephyr/kernel.h>
/* List of fakes used by this unit tester */
#define ASSERT_FFF_FAKES_LIST(FAKE) \
FAKE(mock_check_if_assert_expected) \
DECLARE_FAKE_VALUE_FUNC(bool, mock_check_if_assert_expected);
#define expect_assert() (mock_check_if_assert_expected_fake.return_val = 1)