diff --git a/lib/libx52io/Makefile.am b/lib/libx52io/Makefile.am index 2069f0c..1d3ce7e 100644 --- a/lib/libx52io/Makefile.am +++ b/lib/libx52io/Makefile.am @@ -30,12 +30,16 @@ x52include_HEADERS = libx52io.h if HAVE_CMOCKA LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) $(top_srcdir)/tap-driver.sh -TESTS = test-axis +TESTS = test-axis test-parser check_PROGRAMS = $(TESTS) test_axis_SOURCES = test_axis.c $(libx52io_la_SOURCES) test_axis_CFLAGS = $(libx52io_la_CFLAGS) test_axis_LDFLAGS = @CMOCKA_LIBS@ @HIDAPI_LIBS@ $(WARN_LDFLAGS) + +test_parser_SOURCES = test_parser.c $(libx52io_la_SOURCES) +test_parser_CFLAGS = $(libx52io_la_CFLAGS) +test_parser_LDFLAGS = @CMOCKA_LIBS@ @HIDAPI_LIBS@ $(WARN_LDFLAGS) endif # Extra files that need to be in the distribution diff --git a/lib/libx52io/io_parser.c b/lib/libx52io/io_parser.c index 565a132..ad5f1f8 100644 --- a/lib/libx52io/io_parser.c +++ b/lib/libx52io/io_parser.c @@ -265,5 +265,6 @@ int libx52io_read_timeout(libx52io_context *ctx, libx52io_report *report, int ti return LIBX52IO_ERROR_IO; } + // rc > 0 return _x52io_parse_report(ctx, report, data, rc); } diff --git a/lib/libx52io/test_parser.c b/lib/libx52io/test_parser.c new file mode 100644 index 0000000..0653da6 --- /dev/null +++ b/lib/libx52io/test_parser.c @@ -0,0 +1,94 @@ +/* + * Saitek X52 IO driver - Parser test suite + * + * Copyright (C) 2012-2020 Nirenjan Krishnan (nirenjan@nirenjan.org) + * + * SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 + */ + +#include +#include +#include +#include + +#include "io_common.h" +#include "usb-ids.h" + +static int group_setup(void **state) +{ + libx52io_context *ctx; + int rc; + + rc = libx52io_init(&ctx); + if (rc != LIBX52IO_SUCCESS) { + return rc; + } + + *state = ctx; + return 0; +} + +#define TEST_SETUP(prodid) test_setup_ ## prodid + +#define TEST_SETUP_FUNCTION(prodid) static int TEST_SETUP(prodid)(void **state) { \ + libx52io_context *ctx = *state; \ + ctx->pid = X52_PROD_X52 ## prodid; \ + _x52io_set_report_parser(ctx); \ + return 0; \ +} + +TEST_SETUP_FUNCTION(_1); +TEST_SETUP_FUNCTION(_2); +TEST_SETUP_FUNCTION(PRO); + +#undef TEST_SETUP_FUNCTION + +static int test_teardown(void **state) +{ + libx52io_context *ctx = *state; + + libx52io_close(ctx); + return 0; +} + +static int group_teardown(void **state) +{ + libx52io_context *ctx = *state; + + libx52io_exit(ctx); + return 0; +} + +static void test_error_x52(void **state) { + /* Verify that passing a buffer of the wrong size returns IO error */ + libx52io_context *ctx = *state; + unsigned char data[15]; + int rc; + + rc = _x52io_parse_report(ctx, NULL, data, sizeof(data)); + assert_int_equal(rc, LIBX52IO_ERROR_IO); +} + +static void test_error_pro(void **state) { + /* Verify that passing a buffer of the wrong size returns IO error */ + libx52io_context *ctx = *state; + unsigned char data[14]; + int rc; + + rc = _x52io_parse_report(ctx, NULL, data, sizeof(data)); + assert_int_equal(rc, LIBX52IO_ERROR_IO); +} + +const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(test_error_x52, TEST_SETUP(_1), test_teardown), + cmocka_unit_test_setup_teardown(test_error_x52, TEST_SETUP(_2), test_teardown), + cmocka_unit_test_setup_teardown(test_error_pro, TEST_SETUP(PRO), test_teardown), +}; + +int main(void) +{ + cmocka_set_message_output(CM_OUTPUT_TAP); + cmocka_run_group_tests(tests, group_setup, group_teardown); + return 0; +} +