Tests: add unit tests for bt_dwarf
authorAntoine Busque <abusque@efficios.com>
Mon, 28 Sep 2015 10:01:52 +0000 (06:01 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 2 May 2016 16:30:23 +0000 (12:30 -0400)
Signed-off-by: Antoine Busque <abusque@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
.gitignore
tests/Makefile.am
tests/debuginfo-data/libhello.so [new file with mode: 0644]
tests/lib/Makefile.am
tests/lib/test_dwarf.c [new file with mode: 0644]
tests/lib/test_dwarf_complete [new file with mode: 0755]
tests/tests_debuginfo [new file with mode: 0644]

index 29680b765950ea77eea72557cb3be13d65ff8380..94f6f3e293688cdb9a8a6021400be3bce4e4e7e6 100644 (file)
@@ -5,6 +5,7 @@
 /tests/lib/test_bitfield
 /tests/lib/test_seek
 /tests/lib/test_ctf_writer
+/tests/lib/test_durin
 *.o
 *.a
 *.la
index 507b1ba449807b904cfb5f6979893a939c8f3167..56e93c115ab213f74943a59059ef30a457c0f7e5 100644 (file)
@@ -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 (file)
index 0000000..f0a93e5
Binary files /dev/null and b/tests/debuginfo-data/libhello.so differ
index 8c46acb533746f7cf85ce96029ff7ccd300f12c8..6a58cb8d0dcc40f696b8bc9cf6171a1451730156 100644 (file)
@@ -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 (file)
index 0000000..cee2d44
--- /dev/null
@@ -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 <abusque@efficios.com>
+ *
+ * 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 <fcntl.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <babeltrace/dwarf.h>
+#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 (executable)
index 0000000..c633016
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/sh
+#
+# Copyright (C) 2015 - Antoine Busque <abusque@efficios.com>
+#
+# 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 (file)
index 0000000..e7f774c
--- /dev/null
@@ -0,0 +1 @@
+lib/test_dwarf_complete
\ No newline at end of file
This page took 0.028033 seconds and 4 git commands to generate.