tests: move the SocketCAN tests to tests/net/socket/can

Move the SocketCAN tests from tests/drivers/can/utilities to
tests/net/socket/can to match the location of the code under test.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen 2022-08-11 14:43:40 +02:00 committed by Carles Cufí
parent fe80c1676d
commit 6099e180b1
6 changed files with 162 additions and 132 deletions

View file

@ -530,6 +530,7 @@ Documentation:
- subsys/canbus/
- subsys/net/l2/canbus/
- tests/drivers/can/
- tests/net/socket/can/
- tests/subsys/canbus/
labels:
- "area: CAN"

View file

@ -8,7 +8,6 @@
#include <zephyr/drivers/can.h>
#include <zephyr/logging/log.h>
#include <zephyr/ztest.h>
#include <zephyr/net/socket_can.h>
LOG_MODULE_REGISTER(can_utilities, LOG_LEVEL_ERR);
@ -19,137 +18,6 @@ LOG_MODULE_REGISTER(can_utilities, LOG_LEVEL_ERR);
* @}
*/
/**
* @brief Test of @a can_copy_frame_to_zframe()
*/
ZTEST(can_utilities, test_can_frame_to_zcan_frame)
{
struct can_frame frame = { 0 };
struct zcan_frame expected = { 0 };
struct zcan_frame msg;
const uint8_t data[CAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08 };
frame.can_id = BIT(31) | BIT(30) | 1234;
frame.can_dlc = sizeof(data);
memcpy(frame.data, data, sizeof(frame.data));
expected.rtr = CAN_REMOTEREQUEST;
expected.id_type = CAN_EXTENDED_IDENTIFIER;
expected.id = 1234U;
expected.dlc = sizeof(data);
can_copy_frame_to_zframe(&frame, &msg);
LOG_HEXDUMP_DBG((const uint8_t *)&frame, sizeof(frame), "frame");
LOG_HEXDUMP_DBG((const uint8_t *)&msg, sizeof(msg), "msg");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(msg.rtr, expected.rtr, "RTR bit not set");
zassert_equal(msg.id_type, expected.id_type, "Id-type bit not set");
zassert_equal(msg.id, expected.id, "CAN id invalid");
zassert_equal(msg.dlc, expected.dlc, "Msg length invalid");
}
/**
* @brief Test of @a can_copy_zframe_to_frame()
*/
ZTEST(can_utilities, test_zcan_frame_to_can_frame)
{
struct can_frame frame = { 0 };
struct can_frame expected = { 0 };
struct zcan_frame msg = { 0 };
const uint8_t data[CAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08 };
expected.can_id = BIT(31) | BIT(30) | 1234;
expected.can_dlc = sizeof(data);
memcpy(expected.data, data, sizeof(expected.data));
msg.rtr = CAN_REMOTEREQUEST;
msg.id_type = CAN_EXTENDED_IDENTIFIER;
msg.id = 1234U;
msg.dlc = sizeof(data);
memcpy(msg.data, data, sizeof(data));
can_copy_zframe_to_frame(&msg, &frame);
LOG_HEXDUMP_DBG((const uint8_t *)&frame, sizeof(frame), "frame");
LOG_HEXDUMP_DBG((const uint8_t *)&msg, sizeof(msg), "msg");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(frame.can_id, expected.can_id, "CAN ID not same");
zassert_mem_equal(&frame.data, &expected.data, sizeof(frame.data),
"CAN data not same");
zassert_equal(frame.can_dlc, expected.can_dlc,
"CAN msg length not same");
}
/**
* @brief Test of @a can_copy_filter_to_zfilter()
*/
ZTEST(can_utilities, test_can_filter_to_zcan_filter)
{
struct can_filter filter = { 0 };
struct zcan_filter expected = { 0 };
struct zcan_filter msg_filter = { 0 };
filter.can_id = BIT(31) | BIT(30) | 1234;
filter.can_mask = BIT(31) | BIT(30) | 1234;
expected.rtr = CAN_REMOTEREQUEST;
expected.id_type = CAN_EXTENDED_IDENTIFIER;
expected.id = 1234U;
expected.rtr_mask = 1U;
expected.id_mask = 1234U;
can_copy_filter_to_zfilter(&filter, &msg_filter);
LOG_HEXDUMP_DBG((const uint8_t *)&msg_filter, sizeof(msg_filter),
"msg_filter");
LOG_HEXDUMP_DBG((const uint8_t *)&filter, sizeof(filter), "filter");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(msg_filter.rtr, expected.rtr, "RTR bit not set");
zassert_equal(msg_filter.id_type, expected.id_type,
"Id-type bit not set");
zassert_equal(msg_filter.id, expected.id,
"CAN id invalid");
zassert_equal(msg_filter.rtr_mask, expected.rtr_mask,
"RTR mask bit not set");
zassert_equal(msg_filter.id_mask, expected.id_mask,
"id mask not set");
}
/**
* @brief Test of @a can_copy_zfilter_to_filter()
*/
ZTEST(can_utilities, test_zcan_filter_to_can_filter)
{
struct can_filter filter = { 0 };
struct can_filter expected = { 0 };
struct zcan_filter msg_filter = { 0 };
expected.can_id = BIT(31) | BIT(30) | 1234;
expected.can_mask = BIT(31) | BIT(30) | 1234;
msg_filter.rtr = CAN_REMOTEREQUEST;
msg_filter.id_type = CAN_EXTENDED_IDENTIFIER;
msg_filter.id = 1234U;
msg_filter.rtr_mask = 1U;
msg_filter.id_mask = 1234U;
can_copy_zfilter_to_filter(&msg_filter, &filter);
LOG_HEXDUMP_DBG((const uint8_t *)&msg_filter, sizeof(msg_filter),
"msg_filter");
LOG_HEXDUMP_DBG((const uint8_t *)&filter, sizeof(filter), "filter");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(filter.can_id, expected.can_id, "CAN ID not same");
zassert_equal(filter.can_mask, expected.can_mask, "CAN mask not same");
}
/**
* @brief Test of @a can_dlc_to_bytes()
*/

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(socket_can)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y

View file

@ -0,0 +1,145 @@
/*
* Copyright (c) 2022 Vestas Wind Systems A/S
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
#include <zephyr/net/socket_can.h>
#include <zephyr/ztest.h>
LOG_MODULE_REGISTER(socket_can, LOG_LEVEL_ERR);
/**
* @brief Test of @a can_copy_frame_to_zframe()
*/
ZTEST(socket_can, test_can_frame_to_zcan_frame)
{
struct can_frame frame = { 0 };
struct zcan_frame expected = { 0 };
struct zcan_frame msg;
const uint8_t data[CAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08 };
frame.can_id = BIT(31) | BIT(30) | 1234;
frame.can_dlc = sizeof(data);
memcpy(frame.data, data, sizeof(frame.data));
expected.rtr = CAN_REMOTEREQUEST;
expected.id_type = CAN_EXTENDED_IDENTIFIER;
expected.id = 1234U;
expected.dlc = sizeof(data);
can_copy_frame_to_zframe(&frame, &msg);
LOG_HEXDUMP_DBG((const uint8_t *)&frame, sizeof(frame), "frame");
LOG_HEXDUMP_DBG((const uint8_t *)&msg, sizeof(msg), "msg");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(msg.rtr, expected.rtr, "RTR bit not set");
zassert_equal(msg.id_type, expected.id_type, "Id-type bit not set");
zassert_equal(msg.id, expected.id, "CAN id invalid");
zassert_equal(msg.dlc, expected.dlc, "Msg length invalid");
}
/**
* @brief Test of @a can_copy_zframe_to_frame()
*/
ZTEST(socket_can, test_zcan_frame_to_can_frame)
{
struct can_frame frame = { 0 };
struct can_frame expected = { 0 };
struct zcan_frame msg = { 0 };
const uint8_t data[CAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08 };
expected.can_id = BIT(31) | BIT(30) | 1234;
expected.can_dlc = sizeof(data);
memcpy(expected.data, data, sizeof(expected.data));
msg.rtr = CAN_REMOTEREQUEST;
msg.id_type = CAN_EXTENDED_IDENTIFIER;
msg.id = 1234U;
msg.dlc = sizeof(data);
memcpy(msg.data, data, sizeof(data));
can_copy_zframe_to_frame(&msg, &frame);
LOG_HEXDUMP_DBG((const uint8_t *)&frame, sizeof(frame), "frame");
LOG_HEXDUMP_DBG((const uint8_t *)&msg, sizeof(msg), "msg");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(frame.can_id, expected.can_id, "CAN ID not same");
zassert_mem_equal(&frame.data, &expected.data, sizeof(frame.data),
"CAN data not same");
zassert_equal(frame.can_dlc, expected.can_dlc,
"CAN msg length not same");
}
/**
* @brief Test of @a can_copy_filter_to_zfilter()
*/
ZTEST(socket_can, test_can_filter_to_zcan_filter)
{
struct can_filter filter = { 0 };
struct zcan_filter expected = { 0 };
struct zcan_filter msg_filter = { 0 };
filter.can_id = BIT(31) | BIT(30) | 1234;
filter.can_mask = BIT(31) | BIT(30) | 1234;
expected.rtr = CAN_REMOTEREQUEST;
expected.id_type = CAN_EXTENDED_IDENTIFIER;
expected.id = 1234U;
expected.rtr_mask = 1U;
expected.id_mask = 1234U;
can_copy_filter_to_zfilter(&filter, &msg_filter);
LOG_HEXDUMP_DBG((const uint8_t *)&msg_filter, sizeof(msg_filter),
"msg_filter");
LOG_HEXDUMP_DBG((const uint8_t *)&filter, sizeof(filter), "filter");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(msg_filter.rtr, expected.rtr, "RTR bit not set");
zassert_equal(msg_filter.id_type, expected.id_type,
"Id-type bit not set");
zassert_equal(msg_filter.id, expected.id,
"CAN id invalid");
zassert_equal(msg_filter.rtr_mask, expected.rtr_mask,
"RTR mask bit not set");
zassert_equal(msg_filter.id_mask, expected.id_mask,
"id mask not set");
}
/**
* @brief Test of @a can_copy_zfilter_to_filter()
*/
ZTEST(socket_can, test_zcan_filter_to_can_filter)
{
struct can_filter filter = { 0 };
struct can_filter expected = { 0 };
struct zcan_filter msg_filter = { 0 };
expected.can_id = BIT(31) | BIT(30) | 1234;
expected.can_mask = BIT(31) | BIT(30) | 1234;
msg_filter.rtr = CAN_REMOTEREQUEST;
msg_filter.id_type = CAN_EXTENDED_IDENTIFIER;
msg_filter.id = 1234U;
msg_filter.rtr_mask = 1U;
msg_filter.id_mask = 1234U;
can_copy_zfilter_to_filter(&msg_filter, &filter);
LOG_HEXDUMP_DBG((const uint8_t *)&msg_filter, sizeof(msg_filter),
"msg_filter");
LOG_HEXDUMP_DBG((const uint8_t *)&filter, sizeof(filter), "filter");
LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
zassert_equal(filter.can_id, expected.can_id, "CAN ID not same");
zassert_equal(filter.can_mask, expected.can_mask, "CAN mask not same");
}
ZTEST_SUITE(socket_can, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,6 @@
tests:
net.socket.can:
integration_platforms:
- native_posix
- native_posix_64
tags: net can