diff --git a/Makefile.am b/Makefile.am index 12decd3..9914536 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,6 +11,8 @@ ECUTOOLS_SRC_FILES += src/canbus_logger.c src/canbus_log.c src/canbus_filelogger J2534_SRC_FILES = src/awsiot_client.c src/passthru_shadow_parser.c src/j2534.c src/j2534/apigateway.c src/vector.c src/myint.c ECUTOOLS_TEST_FILES = tests/check_j2534.c +ECUTOOLS_CANBUS_TEST_FILES = tests/check_canbus.c + # AWS IoT client directory IOT_CLIENT_DIR = src/aws_iot_src @@ -74,11 +76,15 @@ ecutuned_SOURCES = $(SRC_FILES) src/ecutuned.c ecutuned_CFLAGS = -DUSESSL -DTHREADED $(INCLUDE_ALL_DIRS) $(COMPILER_FLAGS) $(LOG_FLAGS) ecutuned_LDFLAGS = $(LD_FLAG) $(EXTERNAL_LIBS) -TESTS = check_j2534 -check_PROGRAMS = check_j2534 +TESTS = check_j2534 check_canbus +check_PROGRAMS = check_j2534 check_canbus + check_j2534_SOURCES = $(ECUTOOLS_TEST_FILES) check_j2534_LDFLAGS = $(LD_FLAG) -lcheck -lj2534 +check_canbus_SOURCES = $(ECUTOOLS_CANBUS_TEST_FILES) src/canbus.c +check_canbus_LDADD = -lcheck -lpthread -lrt -lm -lsubunit + bundle-install: cd bindings/ruby && bundle install && cd - cd cli && bundle install && cd - diff --git a/src/canbus.c b/src/canbus.c index b304cfb..b95b259 100755 --- a/src/canbus.c +++ b/src/canbus.c @@ -47,9 +47,17 @@ void canbus_framecpy(struct can_frame * frame, char *buf) { } } -unsigned int canbus_framecmp(struct can_frame *frame1, struct can_frame *frame2) { - if(frame1->can_id != frame2->can_id) return 1; - return strcmp((const char *)frame1->data, (const char *)frame2->data) == 0; +unsigned int canbus_framecmp(struct can_frame *frame1, + struct can_frame *frame2) { + if(frame1->can_id != frame2->can_id) + return 1; + + if(frame1->can_dlc != frame2->can_dlc) + return 1; + + return memcmp(frame1->data, + frame2->data, + frame1->can_dlc) != 0; } void canbus_init(canbus_client *canbus) { diff --git a/tests/check_canbus.c b/tests/check_canbus.c new file mode 100644 index 0000000..0e05daa --- /dev/null +++ b/tests/check_canbus.c @@ -0,0 +1,128 @@ +#include +#include + +#include "canbus.h" + +START_TEST(test_canbus_framecmp_identical_frames) +{ + struct can_frame lhs = { + .can_id = 0x123, + .can_dlc = 4, + .data = { 0x01, 0x00, 0xAA, 0x55 } + }; + + struct can_frame rhs = { + .can_id = 0x123, + .can_dlc = 4, + .data = { 0x01, 0x00, 0xAA, 0x55 } + }; + + ck_assert_uint_eq(canbus_framecmp(&lhs, &rhs), 0); +} +END_TEST + +START_TEST(test_canbus_framecmp_detects_different_can_id) +{ + struct can_frame lhs = { + .can_id = 0x123, + .can_dlc = 2, + .data = { 0xAA, 0x55 } + }; + + struct can_frame rhs = { + .can_id = 0x124, + .can_dlc = 2, + .data = { 0xAA, 0x55 } + }; + + ck_assert_uint_ne(canbus_framecmp(&lhs, &rhs), 0); +} +END_TEST + +START_TEST(test_canbus_framecmp_detects_different_dlc) +{ + struct can_frame lhs = { + .can_id = 0x123, + .can_dlc = 2, + .data = { 0xAA, 0x55 } + }; + + struct can_frame rhs = { + .can_id = 0x123, + .can_dlc = 3, + .data = { 0xAA, 0x55, 0x00 } + }; + + ck_assert_uint_ne(canbus_framecmp(&lhs, &rhs), 0); +} +END_TEST + +START_TEST(test_canbus_framecmp_detects_binary_difference_after_null_byte) +{ + struct can_frame lhs = { + .can_id = 0x123, + .can_dlc = 4, + .data = { 0x01, 0x00, 0xAA, 0x55 } + }; + + struct can_frame rhs = { + .can_id = 0x123, + .can_dlc = 4, + .data = { 0x01, 0x00, 0xBB, 0x55 } + }; + + ck_assert_uint_ne(canbus_framecmp(&lhs, &rhs), 0); +} +END_TEST + +START_TEST(test_canbus_framecmp_detects_different_payload) +{ + struct can_frame lhs = { + .can_id = 0x123, + .can_dlc = 2, + .data = { 0xAA, 0x55 } + }; + + struct can_frame rhs = { + .can_id = 0x123, + .can_dlc = 2, + .data = { 0xAB, 0x55 } + }; + + ck_assert_uint_ne(canbus_framecmp(&lhs, &rhs), 0); +} +END_TEST + +Suite *create_canbus_suite(void) +{ + Suite *suite = suite_create("canbus"); + TCase *tc_core = tcase_create("framecmp"); + + tcase_add_test(tc_core, test_canbus_framecmp_identical_frames); + tcase_add_test(tc_core, test_canbus_framecmp_detects_different_can_id); + tcase_add_test(tc_core, test_canbus_framecmp_detects_different_dlc); + tcase_add_test(tc_core, test_canbus_framecmp_detects_different_payload); + tcase_add_test( + tc_core, + test_canbus_framecmp_detects_binary_difference_after_null_byte + ); + + suite_add_tcase(suite, tc_core); + + return suite; +} + +int main(void) +{ + int num_fail; + Suite *suite = create_canbus_suite(); + SRunner *sr = srunner_create(suite); + + srunner_run_all(sr, CK_NORMAL); + + num_fail = srunner_ntests_failed(sr); + + srunner_free(sr); + + return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +}