Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions tests/drivers/can/api/src/canfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,45 @@ ZTEST(canfd, test_canfd_get_capabilities)
"CAN FD loopback mode not supported");
}

/**
* @brief Test sending CAN FD frame with too big payload.
*/
ZTEST(canfd, test_send_fd_dlc_out_of_range)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already negative test that checks error code returned by can_send() when CAN frame ID is invalid:
https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/drivers/can/api/src/classic.c#L799
Here, I add similar test. It checks that can_send() returns -EINVAL when CAN frame has invalid DLC value.
Test variant for CAN FD.

{
struct can_frame frame = {
.flags = CAN_FRAME_FDF | CAN_FRAME_BRS,
.id = TEST_CAN_STD_ID_1,
.dlc = CANFD_MAX_DLC + 1U,
};
int err;

Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);

err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
zassert_equal(err, -EINVAL, "wrong error on sending invalid frame (err %d)", err);
}

/**
* @brief Test error when CAN FD Error State Indicator (ESI) is send without FD format flag (FDF).
*
* CAN FD Error State Indicator (ESI) indicates that the transmitting node is
* in error-passive state. Only valid in combination with CAN_FRAME_FDF.
*/
ZTEST(canfd, test_send_fd_incorrect_esi)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already negative test that checks error code returned by can_send() when CAN frame ID is invalid:
https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/drivers/can/api/src/classic.c#L799
Here, I add similar test. It checks that can_send() returns -EINVAL when CAN frame has invalid set of flags (explanation in lines 262-263, copied from API description).

{
struct can_frame frame = {
.flags = CAN_FRAME_ESI,
.id = TEST_CAN_STD_ID_1,
.dlc = 0,
};
int err;

Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);

err = can_send(can_dev, &frame, TEST_SEND_TIMEOUT, NULL, NULL);
zassert_equal(err, -ENOTSUP, "wrong error on sending invalid frame (err %d)", err);
}

/**
* @brief Test send/receive with standard (11-bit) CAN IDs and classic CAN frames.
*/
Expand Down Expand Up @@ -395,6 +434,31 @@ ZTEST_USER(canfd, test_set_timing_data_min)
zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
}

/**
* @brief Test setting a too low data phase bitrate.
*/
ZTEST_USER(canfd, test_set_bitrate_data_too_low)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already negative test that checks error code returned by can_set_bitrate_data() when bitrate value is too high:
https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/drivers/can/api/src/canfd.c#L401
Here, I add similar test. It checks that can_set_bitrate_data() returns -ENOTSUP when bitrate value is too low.

{
uint32_t min = can_get_bitrate_min(can_dev);
int err;

if (min == 0) {
ztest_test_skip();
}

err = can_stop(can_dev);
zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);

err = can_set_bitrate_data(can_dev, min - 1);
zassert_equal(err, -ENOTSUP, "too low data phase bitrate accepted");

err = can_set_bitrate_data(can_dev, CONFIG_CAN_DEFAULT_BITRATE_DATA);
zassert_equal(err, 0, "failed to restore default data bitrate");

err = can_start(can_dev);
zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
}

/**
* @brief Test setting a too high data phase bitrate.
*/
Expand Down
65 changes: 65 additions & 0 deletions tests/drivers/can/api/src/classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,31 @@ ZTEST_USER(can_classic, test_bitrate_limits)
zassert_true(min <= max, "min bitrate must be lower or equal to max bitrate");
}

/**
* @brief Test setting a too low bitrate.
*/
ZTEST_USER(can_classic, test_set_bitrate_too_low)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already negative test that checks error code returned by can_set_bitrate() when bitrate value is too high:
https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/drivers/can/api/src/classic.c#L492
Here, I add similar test. It checks that can_set_bitrate() returns -ENOTSUP when bitrate value is too low.

{
uint32_t min = can_get_bitrate_min(can_dev);
int err;

if (min == 0) {
ztest_test_skip();
}

err = can_stop(can_dev);
zassert_equal(err, 0, "failed to stop CAN controller (err %d)", err);

err = can_set_bitrate(can_dev, min - 1);
zassert_equal(err, -ENOTSUP, "too low bitrate accepted");

err = can_set_bitrate(can_dev, CONFIG_CAN_DEFAULT_BITRATE);
zassert_equal(err, 0, "failed to restore default bitrate");

err = can_start(can_dev);
zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);
}

/**
* @brief Test setting a too high bitrate.
*/
Expand Down Expand Up @@ -615,6 +640,19 @@ ZTEST(can_classic, test_add_filter)
can_remove_rx_filter(can_dev, filter_id);
}

/**
* @brief Test adding filter without callback.
*/
ZTEST(can_classic, test_add_filter_without_callback)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a set of negative tests which check that can_add_rx_filter() returns -EINVAL when filter is incorrect:
https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/drivers/can/api/src/classic.c#L637
Here, I check that error code is returned when user tries to set filter without callback function.

{
int err;

Z_TEST_SKIP_IFNDEF(CONFIG_RUNTIME_ERROR_CHECKS);

err = can_add_rx_filter(can_dev, NULL, NULL, &test_std_filter_1);
zassert_equal(err, -EINVAL, "added filter with NULL callback");
}

/**
* @brief Test adding an invalid CAN RX filter.
*
Expand Down Expand Up @@ -818,6 +856,33 @@ ZTEST(can_classic, test_send_ext_id_out_of_range)
send_invalid_frame(can_dev, &frame);
}

/**
* @brief Test sending standard (11-bit ID) CAN frame with too big payload.
*/
ZTEST(can_classic, test_send_std_id_dlc_of_range)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already negative test that checks error code returned by can_send() when CAN frame ID is invalid:
https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/drivers/can/api/src/classic.c#L799
Here, I add similar test. It checks that can_send() returns -EINVAL when CAN frame has invalid DLC value.
Test variant for "standard" CAN with 11-bit CAN frame ID.

{
struct can_frame frame = {
.id = TEST_CAN_STD_ID_1,
.dlc = CAN_MAX_DLC + 1U,
};

send_invalid_frame(can_dev, &frame);
}

/**
* @brief Test sending extended (29-bit ID) CAN frame with too big payload.
*/
ZTEST(can_classic, test_send_ext_id_dlc_of_range)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already negative test that checks error code returned by can_send() when CAN frame ID is invalid:
https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/drivers/can/api/src/classic.c#L799
Here, I add similar test. It checks that can_send() returns -EINVAL when CAN frame has invalid DLC value.
Test variant for "standard" CAN with 29-bit CAN frame ID.

{
struct can_frame frame = {
.flags = CAN_FRAME_IDE,
.id = TEST_CAN_EXT_ID_1,
.dlc = CAN_MAX_DLC + 1U,
};

send_invalid_frame(can_dev, &frame);
}

/**
* @brief Test send/receive with standard (11-bit) CAN IDs.
*/
Expand Down
Loading