From: Antoine Busque Date: Mon, 28 Sep 2015 10:01:52 +0000 (-0400) Subject: Tests: add unit tests for bt_dwarf X-Git-Tag: v1.4.0-rc1~83 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=c3a1997ea20bfa7023fa5034530b9bd7965a6561;hp=b5a8598f01c8e40163449bff173909eff824647b Tests: add unit tests for bt_dwarf Signed-off-by: Antoine Busque Signed-off-by: Jérémie Galarneau --- diff --git a/.gitignore b/.gitignore index 29680b76..94f6f3e2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /tests/lib/test_bitfield /tests/lib/test_seek /tests/lib/test_ctf_writer +/tests/lib/test_durin *.o *.a *.la diff --git a/tests/Makefile.am b/tests/Makefile.am index 507b1ba4..56e93c11 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,9 @@ SUBDIRS = utils bin lib EXTRA_DIST = $(srcdir)/ctf-traces/** tests +if ENABLE_DEBUGINFO +EXTRA_DIST += $(srcdir)/debuginfo-data/** tests_debuginfo +endif SCRIPT_LIST = run.sh @@ -22,3 +25,6 @@ clean-local: check-am: ./run.sh $(srcdir)/tests +if ENABLE_DEBUGINFO + ./run.sh $(srcdir)/tests_debuginfo +endif diff --git a/tests/debuginfo-data/libhello.so b/tests/debuginfo-data/libhello.so new file mode 100644 index 00000000..f0a93e5a Binary files /dev/null and b/tests/debuginfo-data/libhello.so differ diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index 8c46acb5..6a58cb8d 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -30,6 +30,17 @@ SCRIPT_LIST = test_seek_big_trace \ test_seek_empty_packet \ test_ctf_writer_complete +if ENABLE_DEBUGINFO +test_dwarf_LDFLAGS = -static +test_dwarf_LDADD = $(LIBTAP) \ + $(top_builddir)/lib/libbabeltrace.la \ + $(top_builddir)/lib/libdebuginfo.la +test_dwarf_SOURCES = test_dwarf.c + +noinst_PROGRAMS += test_dwarf +SCRIPT_LIST += test_dwarf_complete +endif + dist_noinst_SCRIPTS = $(SCRIPT_LIST) all-local: diff --git a/tests/lib/test_dwarf.c b/tests/lib/test_dwarf.c new file mode 100644 index 00000000..cee2d446 --- /dev/null +++ b/tests/lib/test_dwarf.c @@ -0,0 +1,107 @@ +/* + * test_dwarf.c + * + * Babeltrace bt_dwarf (DWARF utilities) tests + * + * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation + * Copyright (c) 2015 Antoine Busque + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include +#include +#include "tap/tap.h" + +#define NR_TESTS 15 + +static +void test_bt_dwarf(const char *data_dir) +{ + int fd, ret, tag; + char path[PATH_MAX]; + char *die_name = NULL; + struct bt_dwarf_cu *cu = NULL; + struct bt_dwarf_die *die = NULL; + Dwarf *dwarf_info = NULL; + + snprintf(path, PATH_MAX, "%s/libhello.so", data_dir); + + fd = open(path, O_RDONLY); + ok(fd >= 0, "Open DWARF file %s", path); + dwarf_info = dwarf_begin(fd, DWARF_C_READ); + ok(dwarf_info != NULL, "dwarf_begin successful"); + cu = bt_dwarf_cu_create(dwarf_info); + ok(cu != NULL, "bt_dwarf_cu_create successful"); + ret = bt_dwarf_cu_next(cu); + ok(ret == 0, "bt_dwarf_cu_next successful"); + die = bt_dwarf_die_create(cu); + ok(die != NULL, "bt_dwarf_die_create successful"); + /* + * Test bt_dwarf_die_next twice, as the code path is different + * for DIEs at depth 0 (just created) and other depths. + */ + ret = bt_dwarf_die_next(die); + ok(ret == 0, "bt_dwarf_die_next from root DIE successful"); + ok(die->depth == 1, + "bt_dwarf_die_next from root DIE - correct depth value"); + ret = bt_dwarf_die_next(die); + ok(ret == 0, + "bt_dwarf_die_next from non-root DIE successful"); + ok(die->depth == 1, + "bt_dwarf_die_next from non-root DIE - correct depth value"); + + /* Reset DIE to test dwarf_child */ + bt_dwarf_die_destroy(die); + die = bt_dwarf_die_create(cu); + + ret = bt_dwarf_die_child(die); + ok(ret == 0, "bt_dwarf_die_child successful"); + ok(die->depth == 1, "bt_dwarf_die_child - correct depth value"); + + ret = bt_dwarf_die_get_tag(die, &tag); + ok(ret == 0, "bt_dwarf_die_get_tag successful"); + ok(tag == DW_TAG_typedef, "bt_dwarf_die_get_tag - correct tag value"); + ret = bt_dwarf_die_get_name(die, &die_name); + ok(ret == 0, "bt_dwarf_die_get_name successful"); + ok(strcmp(die_name, "size_t") == 0, + "bt_dwarf_die_get_name - correct name value"); + + bt_dwarf_die_destroy(die); + bt_dwarf_cu_destroy(cu); + dwarf_end(dwarf_info); + free(die_name); + close(fd); +} + +int main(int argc, char **argv) +{ + const char *data_dir; + + plan_tests(NR_TESTS); + + if (argc != 2) { + return EXIT_FAILURE; + } else { + data_dir = argv[1]; + } + + test_bt_dwarf(data_dir); + + return EXIT_SUCCESS; +} diff --git a/tests/lib/test_dwarf_complete b/tests/lib/test_dwarf_complete new file mode 100755 index 00000000..c6330160 --- /dev/null +++ b/tests/lib/test_dwarf_complete @@ -0,0 +1,22 @@ +#!/bin/sh +# +# Copyright (C) 2015 - Antoine Busque +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; only version 2 +# of the License. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +CURDIR=$(dirname $0)/ +ROOTDIR=$CURDIR../.. + +$CURDIR/test_dwarf $ROOTDIR/tests/debuginfo-data diff --git a/tests/tests_debuginfo b/tests/tests_debuginfo new file mode 100644 index 00000000..e7f774cd --- /dev/null +++ b/tests/tests_debuginfo @@ -0,0 +1 @@ +lib/test_dwarf_complete \ No newline at end of file