From 76fb4e20298c7c41efb20a8a53ae81d2ce716303 Mon Sep 17 00:00:00 2001 From: John Levon Date: Wed, 25 Nov 2020 17:33:25 +0000 Subject: add "make test"; run it in pre-commit (#127) Also rename test.c to reflect it should cover unit tests. Reviewed-by: Swapnil Ingle Signed-off-by: John Levon --- .travis.yml | 1 + Makefile | 3 +++ test/CMakeLists.txt | 7 +++++-- test/test.c | 60 ----------------------------------------------------- test/unit-tests.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 62 deletions(-) delete mode 100644 test/test.c create mode 100644 test/unit-tests.c diff --git a/.travis.yml b/.travis.yml index 6f267b2..a75abce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,4 @@ before_install: script: - make - make BUILD_TYPE=rel + - make test diff --git a/Makefile b/Makefile index 9e9f04b..872c975 100644 --- a/Makefile +++ b/Makefile @@ -53,6 +53,9 @@ PHONY_TARGETS := all realclean buildclean force_cmake export install-export tags all $(filter-out $(PHONY_TARGETS), $(MAKECMDGOALS)): $(BUILD_DIR)/Makefile +$(MAKE) -C $(BUILD_DIR) $@ +test: all + cd $(BUILD_DIR)/test; ctest --verbose + realclean: rm -rf $(BUILD_DIR_BASE) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e5fc7a0..a04e0f1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,5 +26,8 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -add_executable(test test.c ) -target_link_libraries(test cmocka json-c muser) +add_executable(unit-tests unit-tests.c) +target_link_libraries(unit-tests cmocka json-c muser) + +enable_testing() +add_test(NAME unit-tests COMMAND unit-tests) diff --git a/test/test.c b/test/test.c deleted file mode 100644 index 6ad4e3b..0000000 --- a/test/test.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2020 Nutanix Inc. All rights reserved. - * - * Authors: Thanos Makatos - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Nutanix nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "muser.h" -#include "muser_priv.h" - -static void -test_dma_map_without_fd(void **state __attribute__((unused))) -{ - size_t size = sizeof(struct vfio_user_dma_region); - lm_ctx_t lm_ctx = { 0 }; - assert_true(0 == handle_dma_map_or_unmap(&lm_ctx, size, true, NULL, 0, NULL)); - /* TODO verify that dma_controller_add_region wasn't called */ -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_dma_map_without_fd), - }; - return cmocka_run_group_tests(tests, NULL, NULL); -} - -/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/test/unit-tests.c b/test/unit-tests.c new file mode 100644 index 0000000..6ad4e3b --- /dev/null +++ b/test/unit-tests.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020 Nutanix Inc. All rights reserved. + * + * Authors: Thanos Makatos + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Nutanix nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "muser.h" +#include "muser_priv.h" + +static void +test_dma_map_without_fd(void **state __attribute__((unused))) +{ + size_t size = sizeof(struct vfio_user_dma_region); + lm_ctx_t lm_ctx = { 0 }; + assert_true(0 == handle_dma_map_or_unmap(&lm_ctx, size, true, NULL, 0, NULL)); + /* TODO verify that dma_controller_add_region wasn't called */ +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_dma_map_without_fd), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} + +/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.1