diff --git a/Makefile.am b/Makefile.am index 12decd3..a9c7dfe 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,11 +74,16 @@ 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_passthru_open +check_PROGRAMS = check_j2534 check_passthru_open check_j2534_SOURCES = $(ECUTOOLS_TEST_FILES) check_j2534_LDFLAGS = $(LD_FLAG) -lcheck -lj2534 +check_passthru_open_SOURCES = tests/check_passthru_open.c src/j2534.c +check_passthru_open_CFLAGS = $(AM_CFLAGS) -ffunction-sections -fdata-sections +check_passthru_open_LDFLAGS = -Wl,--gc-sections +check_passthru_open_LDADD = -lcheck -lsubunit + bundle-install: cd bindings/ruby && bundle install && cd - cd cli && bundle install && cd - diff --git a/src/j2534.c b/src/j2534.c index 7cc7979..3a2d34e 100755 --- a/src/j2534.c +++ b/src/j2534.c @@ -427,10 +427,13 @@ long PassThruGetNextDevice(SDEVICE *psDevice) { * STATUS_NOERROR Function call was successful */ long PassThruOpen(const char *pName, unsigned long *pDeviceID) { + j2534_current_api_call = J2534_PassThruOpen; - syslog(LOG_ERR, "PassThruOpen: pName=%s, pDeviceID=%d", pName, *pDeviceID); + if(pName == NULL || pDeviceID == NULL) { + return unless_concurrent_call(ERR_NULL_PARAMETER, J2534_PassThruOpen); + } - j2534_current_api_call = J2534_PassThruOpen; + syslog(LOG_ERR, "PassThruOpen: pName=%s, pDeviceID=%d", pName, *pDeviceID); unsigned int shadow_update_topic_len = PASSTHRU_SHADOW_UPDATE_TOPIC + strlen(pName) + 1; unsigned int shadow_update_accepted_topic_len = PASSTHRU_SHADOW_UPDATE_ACCEPTED_TOPIC + strlen(pName) + 1; @@ -438,10 +441,6 @@ long PassThruOpen(const char *pName, unsigned long *pDeviceID) { unsigned int msg_rx_topic_len = J2534_MSG_RX_TOPIC + strlen(pName) + 1; unsigned int msg_tx_topic_len = J2534_MSG_TX_TOPIC + strlen(pName) + 1; - if(pName == NULL || pDeviceID == NULL) { - return unless_concurrent_call(ERR_NULL_PARAMETER, J2534_PassThruOpen); - } - if(!j2534_initialized) { vector_init(&j2534_client_vector); vector_init(&j2534_selected_channels); diff --git a/tests/check_passthru_open.c b/tests/check_passthru_open.c new file mode 100644 index 0000000..f2084b8 --- /dev/null +++ b/tests/check_passthru_open.c @@ -0,0 +1,97 @@ +#include +#include + +#include "j2534.h" + +/* PassThruOpen dependencies that are not exercised by NULL validation. */ +void vector_init(vector *items) { (void)items; } +void vector_add(vector *items, void *item) { (void)items; (void)item; } +void *vector_get(vector *items, int index) { (void)items; (void)index; return NULL; } + +unsigned int awsiot_client_connect(awsiot_client *client) +{ + (void)client; + return 0; +} + +unsigned int awsiot_client_subscribe(awsiot_client *client, + const char *topic, + void *handler, + void *data) +{ + (void)client; + (void)topic; + (void)handler; + (void)data; + return 0; +} + +unsigned int awsiot_client_publish(awsiot_client *client, + const char *topic, + char *payload) +{ + (void)client; + (void)topic; + (void)payload; + return 0; +} + +IoT_Error_t aws_iot_mqtt_yield(AWS_IoT_Client *client, uint32_t timeout_ms) +{ + (void)client; + (void)timeout_ms; + return SUCCESS; +} + +int MYINT_LEN(int *num) +{ + (void)num; + return 1; +} + +shadow_message *passthru_shadow_parser_parse_state(const char *json) +{ + (void)json; + return NULL; +} + +void passthru_shadow_parser_free_message(shadow_message *message) +{ + (void)message; +} + +START_TEST(test_passthru_open_rejects_null_name) +{ + unsigned long device_id = 1; + + ck_assert_int_eq(PassThruOpen(NULL, &device_id), ERR_NULL_PARAMETER); +} +END_TEST + +START_TEST(test_passthru_open_rejects_null_device_id) +{ + ck_assert_int_eq( + PassThruOpen("J2534-1:ecutools", NULL), + ERR_NULL_PARAMETER + ); +} +END_TEST + +int main(void) +{ + int failed; + Suite *suite = suite_create("PassThruOpen"); + TCase *null_parameters = tcase_create("null_parameters"); + SRunner *runner; + + tcase_add_test(null_parameters, test_passthru_open_rejects_null_name); + tcase_add_test(null_parameters, test_passthru_open_rejects_null_device_id); + suite_add_tcase(suite, null_parameters); + + runner = srunner_create(suite); + srunner_run_all(runner, CK_NORMAL); + failed = srunner_ntests_failed(runner); + srunner_free(runner); + + return failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +}