From 9cb0a092c2e2a95829109afbad80dfaac41af8e8 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Fri, 28 Mar 2014 12:48:10 +0100 Subject: [PATCH] test: add valgrind memory checks Copy over the "make memcheck" target from libshl. It runs the test-suite again via valgrind and fails on any valgrind warnings. This is very useful to find memory-leaks and invalid memory-accesses via the test-suite. Signed-off-by: David Herrmann --- .gitignore | 2 ++ Makefile.am | 35 +++++++++++++++++++++++++++++++++-- test.supp | 15 +++++++++++++++ test/test_valgrind.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 test.supp create mode 100644 test/test_valgrind.c diff --git a/.gitignore b/.gitignore index 1b93630..2e1a99f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.la *.lo *.log +*.memlog *.o *.swp *.tar.xz @@ -28,6 +29,7 @@ miraclectl miracled stamp-h1 test-suite.log +test_valgrind test_wpas wpa_cli wpa_supplicant diff --git a/Makefile.am b/Makefile.am index 7f1f09d..7c2eb6a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -27,6 +27,7 @@ pkgconfig_DATA = TPHONY = TESTS = +MEMTESTS = bin_PROGRAMS = check_PROGRAMS = lib_LTLIBRARIES = @@ -180,8 +181,9 @@ tests = \ test_wpas if BUILD_HAVE_CHECK -check_PROGRAMS += $(tests) -TESTS += $(tests) +check_PROGRAMS += $(tests) test_valgrind +TESTS += $(tests) test_valgrind +MEMTESTS += $(tests) endif test_sources = \ @@ -197,11 +199,40 @@ test_cflags = \ test_lflags = \ $(AM_LDFLAGS) +test_rtsp_SOURCES = test/test_rtsp.c $(test_sources) +test_rtsp_CPPFLAGS = $(test_cflags) +test_rtsp_LDADD = $(test_libs) +test_rtsp_LDFLAGS = $(test_lflags) + +test_valgrind_SOURCES = test/test_valgrind.c $(test_sources) +test_valgrind_CPPFLAGS = $(test_cflags) +test_valgrind_LDADD = $(test_libs) +test_valgrind_LDFLAGS = $(test_lflags) + test_wpas_SOURCES = test/test_wpas.c $(test_sources) test_wpas_CPPFLAGS = $(test_cflags) test_wpas_LDADD = $(test_libs) test_wpas_LDFLAGS = $(test_lflags) +VALGRIND = CK_FORK=no valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --leak-resolution=high --error-exitcode=1 --suppressions=$(top_builddir)/test.supp + +# verify that test_valgrind actually leaks data +memcheck-verify: check + $(AM_V_GEN)$(VALGRIND) --log-file=/dev/null ./test_valgrind >/dev/null ; test 1 = $$? + +TPHONY += memcheck-verify + +# run memcheck tests via valgrind +memcheck: memcheck-verify + $(AM_V_GEN)for i in $(MEMTESTS) ; do \ + $(VALGRIND) --log-file=$(top_builddir)/$$i.memlog \ + $(top_builddir)/$$i >/dev/null || (echo "memcheck failed on: $$i" ; exit 1) ; \ + done + +TPHONY += memcheck + +distcheck-hook: memcheck + # # Phony targets # diff --git a/test.supp b/test.supp new file mode 100644 index 0000000..a61a1f7 --- /dev/null +++ b/test.supp @@ -0,0 +1,15 @@ +# +# Some suppression rules for our test-suite to work correctly. +# libcheck has some weird errors, so lets ignore them. +# + +{ + libcheck timer_create warnings + Memcheck:Param + timer_create(evp) + fun:timer_create + obj:/usr/lib/libcheck.so.0.0.0 + obj:/usr/lib/libcheck.so.0.0.0 + fun:srunner_run + fun:main +} diff --git a/test/test_valgrind.c b/test/test_valgrind.c new file mode 100644 index 0000000..89a5eb1 --- /dev/null +++ b/test/test_valgrind.c @@ -0,0 +1,42 @@ +/* + * MiracleCast - Wifi-Display/Miracast Implementation + * + * Copyright (c) 2013-2014 David Herrmann + * + * MiracleCast is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * MiracleCast is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with MiracleCast; If not, see . + */ + +/* Dummy which just leaks memory. Used to verify valgrind memcheck. */ + +#include "test_common.h" + +START_TEST(test_valgrind) +{ + void *p; + + p = malloc(0x100); + ck_assert(!!p); +} +END_TEST + +TEST_DEFINE_CASE(misc) + TEST(test_valgrind) +TEST_END_CASE + +TEST_DEFINE( + TEST_SUITE(valgrind, + TEST_CASE(misc), + TEST_END + ) +)