Skip to content

Commit 8023a58

Browse files
nordic-seglaescolar
authored andcommitted
tests: drivers: can: api: Add negative test for can_send()
Check error codes when sending invalid frames: - too big data payload; - wrong set of flags. Signed-off-by: Sebastian Głąb <[email protected]>
1 parent 2616720 commit 8023a58

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

tests/drivers/can/api/src/canfd.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,45 @@ ZTEST(canfd, test_canfd_get_capabilities)
238238
"CAN FD loopback mode not supported");
239239
}
240240

241+
/**
242+
* @brief Test sending CAN FD frame with too big payload.
243+
*/
244+
ZTEST(canfd, test_send_fd_dlc_out_of_range)
245+
{
246+
struct can_frame frame = {
247+
.flags = CAN_FRAME_FDF | CAN_FRAME_BRS,
248+
.id = TEST_CAN_STD_ID_1,
249+
.dlc = CANFD_MAX_DLC + 1U,
250+
};
251+
int err;
252+
253+
Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);
254+
255+
err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
256+
zassert_equal(err, -EINVAL, "wrong error on sending invalid frame (err %d)", err);
257+
}
258+
259+
/**
260+
* @brief Test error when CAN FD Error State Indicator (ESI) is send without FD format flag (FDF).
261+
*
262+
* CAN FD Error State Indicator (ESI) indicates that the transmitting node is
263+
* in error-passive state. Only valid in combination with CAN_FRAME_FDF.
264+
*/
265+
ZTEST(canfd, test_send_fd_incorrect_esi)
266+
{
267+
struct can_frame frame = {
268+
.flags = CAN_FRAME_ESI,
269+
.id = TEST_CAN_STD_ID_1,
270+
.dlc = 0,
271+
};
272+
int err;
273+
274+
Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);
275+
276+
err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
277+
zassert_equal(err, -ENOTSUP, "wrong error on sending invalid frame (err %d)", err);
278+
}
279+
241280
/**
242281
* @brief Test send/receive with standard (11-bit) CAN IDs and classic CAN frames.
243282
*/

tests/drivers/can/api/src/classic.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,33 @@ ZTEST(can_classic, test_send_ext_id_out_of_range)
856856
send_invalid_frame(can_dev, &frame);
857857
}
858858

859+
/**
860+
* @brief Test sending standard (11-bit ID) CAN frame with too big payload.
861+
*/
862+
ZTEST(can_classic, test_send_std_id_dlc_of_range)
863+
{
864+
struct can_frame frame = {
865+
.id = TEST_CAN_STD_ID_1,
866+
.dlc = CAN_MAX_DLC + 1U,
867+
};
868+
869+
send_invalid_frame(can_dev, &frame);
870+
}
871+
872+
/**
873+
* @brief Test sending extended (29-bit ID) CAN frame with too big payload.
874+
*/
875+
ZTEST(can_classic, test_send_ext_id_dlc_of_range)
876+
{
877+
struct can_frame frame = {
878+
.flags = CAN_FRAME_IDE,
879+
.id = TEST_CAN_EXT_ID_1,
880+
.dlc = CAN_MAX_DLC + 1U,
881+
};
882+
883+
send_invalid_frame(can_dev, &frame);
884+
}
885+
859886
/**
860887
* @brief Test send/receive with standard (11-bit) CAN IDs.
861888
*/

0 commit comments

Comments
 (0)