aboutsummaryrefslogtreecommitdiffstats
path: root/libs/isotp-c/tests
diff options
context:
space:
mode:
Diffstat (limited to 'libs/isotp-c/tests')
-rw-r--r--libs/isotp-c/tests/common.c91
-rw-r--r--libs/isotp-c/tests/test_core.c78
-rw-r--r--libs/isotp-c/tests/test_receive.c176
-rw-r--r--libs/isotp-c/tests/test_send.c103
4 files changed, 448 insertions, 0 deletions
diff --git a/libs/isotp-c/tests/common.c b/libs/isotp-c/tests/common.c
new file mode 100644
index 00000000..a9eed39e
--- /dev/null
+++ b/libs/isotp-c/tests/common.c
@@ -0,0 +1,91 @@
+#include <isotp/isotp.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+IsoTpShims SHIMS;
+IsoTpReceiveHandle RECEIVE_HANDLE;
+
+uint32_t last_can_frame_sent_arb_id;
+uint8_t last_can_payload_sent[8];
+uint8_t last_can_payload_size;
+bool can_frame_was_sent;
+
+bool message_was_received;
+uint32_t last_message_received_arb_id;
+uint8_t last_message_received_payload[OUR_MAX_ISO_TP_MESSAGE_SIZE];
+uint8_t last_message_received_payload_size;
+
+uint32_t last_message_sent_arb_id;
+bool last_message_sent_status;
+uint8_t last_message_sent_payload[OUR_MAX_ISO_TP_MESSAGE_SIZE];
+uint8_t last_message_sent_payload_size;
+
+void debug(const char* format, ...) {
+ va_list args;
+ va_start(args, format);
+ vprintf(format, args);
+ printf("\r\n");
+ va_end(args);
+}
+
+bool mock_send_can(const uint32_t arbitration_id, const uint8_t* data,
+ const uint8_t size) {
+ can_frame_was_sent = true;
+ last_can_frame_sent_arb_id = arbitration_id;
+ last_can_payload_size = size;
+ if(size > 0) {
+ memcpy(last_can_payload_sent, data, size);
+ }
+ return true;
+}
+
+void message_received(const IsoTpMessage* message) {
+ debug("Received ISO-TP message:");
+ message_was_received = true;
+ char str_message[48] = {0};
+ isotp_message_to_string(message, str_message, sizeof(str_message));
+ debug("%s", str_message);
+ last_message_received_arb_id = message->arbitration_id;
+ last_message_received_payload_size = message->size;
+ if(message->size > 0) {
+ memcpy(last_message_received_payload, message->payload, message->size);
+ }
+}
+
+void message_sent(const IsoTpMessage* message, const bool success) {
+ if(success) {
+ debug("Sent ISO-TP message:");
+ } else {
+ debug("Unable to send ISO-TP message:");
+ }
+ char str_message[48] = {0};
+ isotp_message_to_string(message, str_message, sizeof(str_message));
+ debug("%s", str_message);
+
+ last_message_sent_arb_id = message->arbitration_id;
+ last_message_sent_payload_size = message->size;
+ last_message_sent_status = success;
+ if(message->size > 0) {
+ memcpy(last_message_sent_payload, message->payload, message->size);
+ }
+}
+
+void can_frame_sent(const uint32_t arbitration_id, const uint8_t* payload,
+ const uint8_t size) {
+ debug("Sent CAN Frame with arb ID 0x%x and %d bytes", arbitration_id, size);
+}
+
+void setup() {
+ SHIMS = isotp_init_shims(debug, mock_send_can, NULL);
+ RECEIVE_HANDLE = isotp_receive(&SHIMS, 0x2a, message_received);
+ memset(last_message_sent_payload, 0, OUR_MAX_ISO_TP_MESSAGE_SIZE);
+ memset(last_message_received_payload, 0, OUR_MAX_ISO_TP_MESSAGE_SIZE);
+ memset(last_can_payload_sent, 0, sizeof(last_can_payload_sent));
+ last_message_sent_status = false;
+ message_was_received = false;
+ can_frame_was_sent = false;
+}
+
diff --git a/libs/isotp-c/tests/test_core.c b/libs/isotp-c/tests/test_core.c
new file mode 100644
index 00000000..73b47af0
--- /dev/null
+++ b/libs/isotp-c/tests/test_core.c
@@ -0,0 +1,78 @@
+#include <isotp/receive.h>
+#include <check.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+extern IsoTpShims SHIMS;
+
+extern void message_sent(const IsoTpMessage* message, const bool success);
+
+extern uint16_t last_can_frame_sent_arb_id;
+extern uint8_t last_can_payload_sent[8];
+extern uint8_t last_can_payload_size;
+extern bool can_frame_was_sent;
+
+extern bool message_was_received;
+extern uint16_t last_message_received_arb_id;
+extern uint8_t last_message_received_payload[];
+extern uint8_t last_message_received_payload_size;
+
+extern uint16_t last_message_sent_arb_id;
+extern bool last_message_sent_status;
+extern uint8_t last_message_sent_payload[];
+extern uint8_t last_message_sent_payload_size;
+
+extern void setup();
+
+START_TEST (test_default_frame_padding_on)
+{
+ ck_assert(SHIMS.frame_padding);
+ const uint8_t payload[] = {0x12, 0x34};
+ uint32_t arbitration_id = 0x2a;
+ isotp_send(&SHIMS, arbitration_id, payload, sizeof(payload), message_sent);
+ ck_assert_int_eq(last_message_sent_arb_id, arbitration_id);
+ fail_unless(last_message_sent_status);
+ ck_assert_int_eq(last_message_sent_payload_size, 2);
+ ck_assert_int_eq(last_can_payload_size, 8);
+
+}
+END_TEST
+
+START_TEST (test_disabled_frame_padding)
+{
+ SHIMS.frame_padding = false;
+ const uint8_t payload[] = {0x12, 0x34};
+ uint32_t arbitration_id = 0x2a;
+ isotp_send(&SHIMS, arbitration_id, payload, sizeof(payload), message_sent);
+ ck_assert_int_eq(last_message_sent_arb_id, arbitration_id);
+ fail_unless(last_message_sent_status);
+ ck_assert_int_eq(last_message_sent_payload_size, 2);
+ ck_assert_int_eq(last_can_payload_size, 3);
+
+}
+END_TEST
+
+Suite* testSuite(void) {
+ Suite* s = suite_create("iso15765");
+ TCase *tc_core = tcase_create("core");
+ tcase_add_checked_fixture(tc_core, setup, NULL);
+ tcase_add_test(tc_core, test_default_frame_padding_on);
+ tcase_add_test(tc_core, test_disabled_frame_padding);
+ suite_add_tcase(s, tc_core);
+
+ return s;
+}
+
+int main(void) {
+ int numberFailed;
+ Suite* s = testSuite();
+ SRunner *sr = srunner_create(s);
+ // Don't fork so we can actually use gdb
+ srunner_set_fork_status(sr, CK_NOFORK);
+ srunner_run_all(sr, CK_NORMAL);
+ numberFailed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (numberFailed == 0) ? 0 : 1;
+}
diff --git a/libs/isotp-c/tests/test_receive.c b/libs/isotp-c/tests/test_receive.c
new file mode 100644
index 00000000..607f9061
--- /dev/null
+++ b/libs/isotp-c/tests/test_receive.c
@@ -0,0 +1,176 @@
+#include <isotp/isotp.h>
+#include <check.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+extern IsoTpShims SHIMS;
+extern IsoTpReceiveHandle RECEIVE_HANDLE;
+
+extern void message_sent(const IsoTpMessage* message, const bool success);
+
+extern uint16_t last_can_frame_sent_arb_id;
+extern uint8_t last_can_payload_sent;
+extern uint8_t last_can_payload_size;
+extern bool can_frame_was_sent;
+
+extern bool message_was_received;
+extern uint16_t last_message_received_arb_id;
+extern uint8_t last_message_received_payload[];
+extern uint8_t last_message_received_payload_size;
+
+extern uint16_t last_message_sent_arb_id;
+extern bool last_message_sent_status;
+extern uint8_t last_message_sent_payload[];
+extern uint8_t last_message_sent_payload_size;
+
+extern void setup();
+
+START_TEST (test_receive_empty_can_message)
+{
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0};
+ fail_if(RECEIVE_HANDLE.completed);
+ IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x100, data, 0);
+ fail_if(message.completed);
+ fail_if(message_was_received);
+}
+END_TEST
+
+START_TEST (test_receive_wrong_id)
+{
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0};
+ fail_if(RECEIVE_HANDLE.completed);
+ IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x100, data, 1);
+ fail_if(message.completed);
+ fail_if(message_was_received);
+}
+END_TEST
+
+START_TEST (test_receive_bad_pci)
+{
+ // 4 is a reserved number for the PCI field - only 0-3 are allowed
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x40};
+ IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data, 1);
+ fail_if(message.completed);
+ fail_if(message_was_received);
+}
+END_TEST
+
+START_TEST (test_receive_single_frame_empty_payload)
+{
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x00, 0x12, 0x34};
+ fail_if(RECEIVE_HANDLE.completed);
+ IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data, 3);
+ fail_unless(RECEIVE_HANDLE.completed);
+ fail_unless(message.completed);
+ fail_unless(message_was_received);
+ ck_assert_int_eq(last_message_received_arb_id, 0x2a);
+ ck_assert_int_eq(last_message_received_payload_size, 0);
+}
+END_TEST
+
+START_TEST (test_receive_single_frame)
+{
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x02, 0x12, 0x34};
+ IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data, 3);
+ fail_unless(message.completed);
+ fail_unless(message_was_received);
+ ck_assert_int_eq(last_message_received_arb_id, 0x2a);
+ ck_assert_int_eq(last_message_received_payload_size, 2);
+ ck_assert_int_eq(last_message_received_payload[0], 0x12);
+ ck_assert_int_eq(last_message_received_payload[1], 0x34);
+}
+END_TEST
+
+START_TEST (test_receive_multi_frame)
+{
+ const uint8_t data0[CAN_MESSAGE_BYTE_SIZE] = {0x10, 0x14, 0x49, 0x02, 0x01, 0x31, 0x46, 0x4d};
+ IsoTpMessage message0 = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data0, 8);
+ fail_unless(!RECEIVE_HANDLE.completed);
+ fail_unless(!message0.completed);
+ fail_unless(!message_was_received);
+ fail_unless(message0.multi_frame);
+ //make sure flow control message has been sent.
+ ck_assert_int_eq(last_can_frame_sent_arb_id, 0x2a - 8);
+ ck_assert_int_eq(last_can_payload_sent, 0x30);
+
+ const uint8_t data1[CAN_MESSAGE_BYTE_SIZE] = {0x21, 0x43, 0x55, 0x39, 0x4a, 0x39, 0x34, 0x48};
+ IsoTpMessage message1 = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data1, 8);
+ fail_unless(!RECEIVE_HANDLE.completed);
+ fail_unless(!message1.completed);
+ fail_unless(!message_was_received);
+ fail_unless(message1.multi_frame);
+
+ const uint8_t data2[CAN_MESSAGE_BYTE_SIZE] = {0x22, 0x55, 0x41, 0x30, 0x34, 0x35, 0x32, 0x34};
+ IsoTpMessage message2 = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data2, 8);
+ fail_unless(RECEIVE_HANDLE.completed);
+ fail_unless(message2.completed);
+ fail_unless(message_was_received);
+ fail_unless(message2.multi_frame);
+
+ ck_assert_int_eq(last_message_received_arb_id, 0x2a);
+ ck_assert_int_eq(last_message_received_payload_size, 0x14);
+ ck_assert_int_eq(last_message_received_payload[0], 0x49);
+ ck_assert_int_eq(last_message_received_payload[1], 0x02);
+ ck_assert_int_eq(last_message_received_payload[2], 0x01);
+ ck_assert_int_eq(last_message_received_payload[3], 0x31);
+ ck_assert_int_eq(last_message_received_payload[4], 0x46);
+ ck_assert_int_eq(last_message_received_payload[5], 0x4d);
+ ck_assert_int_eq(last_message_received_payload[6], 0x43);
+ ck_assert_int_eq(last_message_received_payload[7], 0x55);
+ ck_assert_int_eq(last_message_received_payload[8], 0x39);
+ ck_assert_int_eq(last_message_received_payload[9], 0x4a);
+ ck_assert_int_eq(last_message_received_payload[10], 0x39);
+ ck_assert_int_eq(last_message_received_payload[11], 0x34);
+ ck_assert_int_eq(last_message_received_payload[12], 0x48);
+ ck_assert_int_eq(last_message_received_payload[13], 0x55);
+ ck_assert_int_eq(last_message_received_payload[14], 0x41);
+ ck_assert_int_eq(last_message_received_payload[15], 0x30);
+ ck_assert_int_eq(last_message_received_payload[16], 0x34);
+ ck_assert_int_eq(last_message_received_payload[17], 0x35);
+ ck_assert_int_eq(last_message_received_payload[18], 0x32);
+ ck_assert_int_eq(last_message_received_payload[19], 0x34);
+}
+END_TEST
+
+START_TEST (test_receive_large_multi_frame)
+{
+ const uint8_t data0[CAN_MESSAGE_BYTE_SIZE] = {0x10, 0x80, 0x49, 0x02, 0x01, 0x31, 0x46, 0x4d};
+ IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data0, 8);
+ //Make sure we don't try to receive messages that are too large and don't send flow control.
+ fail_unless(!can_frame_was_sent);
+ fail_unless(!RECEIVE_HANDLE.completed);
+ fail_unless(!message.completed);
+ fail_unless(!message_was_received);
+ fail_unless(!message.multi_frame);
+}
+END_TEST
+
+Suite* testSuite(void) {
+ Suite* s = suite_create("iso15765");
+ TCase *tc_core = tcase_create("receive");
+ tcase_add_checked_fixture(tc_core, setup, NULL);
+ tcase_add_test(tc_core, test_receive_wrong_id);
+ tcase_add_test(tc_core, test_receive_bad_pci);
+ tcase_add_test(tc_core, test_receive_single_frame);
+ tcase_add_test(tc_core, test_receive_single_frame_empty_payload);
+ tcase_add_test(tc_core, test_receive_empty_can_message);
+ tcase_add_test(tc_core, test_receive_multi_frame);
+ tcase_add_test(tc_core, test_receive_large_multi_frame);
+ suite_add_tcase(s, tc_core);
+
+ return s;
+}
+
+int main(void) {
+ int numberFailed;
+ Suite* s = testSuite();
+ SRunner *sr = srunner_create(s);
+ // Don't fork so we can actually use gdb
+ srunner_set_fork_status(sr, CK_NOFORK);
+ srunner_run_all(sr, CK_NORMAL);
+ numberFailed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (numberFailed == 0) ? 0 : 1;
+}
diff --git a/libs/isotp-c/tests/test_send.c b/libs/isotp-c/tests/test_send.c
new file mode 100644
index 00000000..29cf5dec
--- /dev/null
+++ b/libs/isotp-c/tests/test_send.c
@@ -0,0 +1,103 @@
+#include <isotp/receive.h>
+#include <check.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+extern IsoTpShims SHIMS;
+
+extern void message_sent(const IsoTpMessage* message, const bool success);
+
+extern uint16_t last_can_frame_sent_arb_id;
+extern uint8_t last_can_payload_sent[8];
+extern uint8_t last_can_payload_size;
+extern bool can_frame_was_sent;
+
+extern bool message_was_received;
+extern uint16_t last_message_received_arb_id;
+extern uint8_t last_message_received_payload[];
+extern uint8_t last_message_received_payload_size;
+
+extern uint16_t last_message_sent_arb_id;
+extern bool last_message_sent_status;
+extern uint8_t last_message_sent_payload[];
+extern uint8_t last_message_sent_payload_size;
+
+extern void setup();
+
+START_TEST (test_send_empty_payload)
+{
+ SHIMS.frame_padding = false;
+ uint16_t arbitration_id = 0x2a;
+ IsoTpSendHandle handle = isotp_send(&SHIMS, arbitration_id, NULL, 0, message_sent);
+ fail_unless(handle.success);
+ fail_unless(handle.completed);
+ ck_assert_int_eq(last_message_sent_arb_id, arbitration_id);
+ fail_unless(last_message_sent_status);
+ ck_assert_int_eq(last_message_sent_payload[0], '\0');
+ ck_assert_int_eq(last_message_sent_payload_size, 0);
+
+ ck_assert_int_eq(last_can_frame_sent_arb_id, arbitration_id);
+ fail_unless(can_frame_was_sent);
+ ck_assert_int_eq(last_can_payload_sent[0], 0x0);
+ ck_assert_int_eq(last_can_payload_size, 1);
+}
+END_TEST
+
+START_TEST (test_send_single_frame)
+{
+ SHIMS.frame_padding = false;
+ const uint8_t payload[] = {0x12, 0x34};
+ uint16_t arbitration_id = 0x2a;
+ isotp_send(&SHIMS, arbitration_id, payload, sizeof(payload), message_sent);
+ ck_assert_int_eq(last_message_sent_arb_id, arbitration_id);
+ fail_unless(last_message_sent_status);
+ ck_assert_int_eq(last_message_sent_payload[0], 0x12);
+ ck_assert_int_eq(last_message_sent_payload[1], 0x34);
+ ck_assert_int_eq(last_message_sent_payload_size, 2);
+
+ ck_assert_int_eq(last_can_frame_sent_arb_id, arbitration_id);
+ fail_unless(can_frame_was_sent);
+ ck_assert_int_eq(last_can_payload_sent[0], 0x2);
+ ck_assert_int_eq(last_can_payload_sent[1], 0x12);
+ ck_assert_int_eq(last_can_payload_sent[2], 0x34);
+ ck_assert_int_eq(last_can_payload_size, 3);
+}
+END_TEST
+
+START_TEST (test_send_multi_frame)
+{
+ const uint8_t payload[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0x01, 0x23,
+ 0x45, 0x67, 0x89};
+ uint16_t arbitration_id = 0x2a;
+ IsoTpSendHandle handle = isotp_send(&SHIMS, arbitration_id, payload, sizeof(payload),
+ message_sent);
+ fail_unless(handle.completed);
+ fail_if(handle.success);
+}
+END_TEST
+
+Suite* testSuite(void) {
+ Suite* s = suite_create("iso15765");
+ TCase *tc_core = tcase_create("send");
+ tcase_add_checked_fixture(tc_core, setup, NULL);
+ tcase_add_test(tc_core, test_send_empty_payload);
+ tcase_add_test(tc_core, test_send_single_frame);
+ tcase_add_test(tc_core, test_send_multi_frame);
+ suite_add_tcase(s, tc_core);
+
+ return s;
+}
+
+int main(void) {
+ int numberFailed;
+ Suite* s = testSuite();
+ SRunner *sr = srunner_create(s);
+ // Don't fork so we can actually use gdb
+ srunner_set_fork_status(sr, CK_NOFORK);
+ srunner_run_all(sr, CK_NORMAL);
+ numberFailed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (numberFailed == 0) ? 0 : 1;
+}