Linux-Trace-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 0/9] libtracefs: few fixes and a lot of unit tests
@ 2020-01-29  9:54 Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 1/9] trace-cmd: fix description of tracefs_get_tracing_dir() API Tzvetomir Stoyanov (VMware)
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

Few minor libtracefs fixes and a lot of new unit tests,
covering all library APIs.

Tzvetomir Stoyanov (VMware) (9):
  trace-cmd: fix description of tracefs_get_tracing_dir() API
  trace-cmd: Remove tracefs_read_page_record() API
  trace-cmd: Add sanity check of tracefs_get_tracing_file() input
    parameter.
  trace-cmd: Unit test for libtracefs
  trace-cmd: Unit tests for libtracefs APIs related to tracing file /
    directory
  trace-cmd: Unit tests for libtracefs APIs related to ftrace instances
  trace-cmd: Unit tests for libtracefs APIs related to ftrace events and
    systems
  trace-cmd: Unit test for tracefs_tracers() API
  trace-cmd: Unit tests for libtracefs APIs related to allocating a tep
    handler based on local events

 Makefile                    |  13 +-
 include/tracefs/tracefs.h   |   3 -
 lib/tracefs/tracefs-utils.c |   5 +-
 utest/Makefile              |  41 +++
 utest/README                |  15 ++
 utest/trace-utest.c         |  83 ++++++
 utest/trace-utest.h         |  11 +
 utest/tracefs-utest.c       | 491 ++++++++++++++++++++++++++++++++++++
 8 files changed, 656 insertions(+), 6 deletions(-)
 create mode 100644 utest/Makefile
 create mode 100644 utest/README
 create mode 100644 utest/trace-utest.c
 create mode 100644 utest/trace-utest.h
 create mode 100644 utest/tracefs-utest.c

-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/9] trace-cmd: fix description of tracefs_get_tracing_dir() API
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 2/9] trace-cmd: Remove tracefs_read_page_record() API Tzvetomir Stoyanov (VMware)
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

The string, returned from this API must not be freed, as it is
a pointer to a local static variable. This was not explicitly
stated in the description of the API.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 lib/tracefs/tracefs-utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/tracefs/tracefs-utils.c b/lib/tracefs/tracefs-utils.c
index 658c852..de46aab 100644
--- a/lib/tracefs/tracefs-utils.c
+++ b/lib/tracefs/tracefs-utils.c
@@ -133,7 +133,7 @@ char *tracefs_find_tracing_dir(void)
  * tracefs_get_tracing_dir - Get tracing directory
  *
  * Returns string containing the full path to the system's tracing directory.
- * Must use tracefs_put_tracing_file() to free the returned string.
+ * The returned string must *not* be freed.
  */
 const char *tracefs_get_tracing_dir(void)
 {
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 2/9] trace-cmd: Remove tracefs_read_page_record() API
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 1/9] trace-cmd: fix description of tracefs_get_tracing_dir() API Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 3/9] trace-cmd: Add sanity check of tracefs_get_tracing_file() input parameter Tzvetomir Stoyanov (VMware)
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

This function is used only internally in the library, it is
not exposed as an API. Its declaration was left by mistake in
the trasefs.h file, as official library API.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 include/tracefs/tracefs.h | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/include/tracefs/tracefs.h b/include/tracefs/tracefs.h
index bd3f732..85690b6 100644
--- a/include/tracefs/tracefs.h
+++ b/include/tracefs/tracefs.h
@@ -34,9 +34,6 @@ char *tracefs_instance_file_read(struct tracefs_instance *instance,
 				 char *file, int *psize);
 
 /* events */
-struct tep_record *
-tracefs_read_page_record(struct tep_handle *tep, void *page, int size,
-			  struct tep_record *last_record);
 void tracefs_list_free(char **list);
 char **tracefs_event_systems(const char *tracing_dir);
 char **tracefs_system_events(const char *tracing_dir, const char *system);
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 3/9] trace-cmd: Add sanity check of tracefs_get_tracing_file() input parameter.
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 1/9] trace-cmd: fix description of tracefs_get_tracing_dir() API Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 2/9] trace-cmd: Remove tracefs_read_page_record() API Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 4/9] trace-cmd: Unit test for libtracefs Tzvetomir Stoyanov (VMware)
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

The input parameter "name" of tracefs_get_tracing_file() API is mandatory,
it cannot be NULL. A check is added to verify this.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 lib/tracefs/tracefs-utils.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/tracefs/tracefs-utils.c b/lib/tracefs/tracefs-utils.c
index de46aab..227990a 100644
--- a/lib/tracefs/tracefs-utils.c
+++ b/lib/tracefs/tracefs-utils.c
@@ -161,6 +161,9 @@ char *tracefs_get_tracing_file(const char *name)
 	char *file;
 	int ret;
 
+	if (!name)
+		return NULL;
+
 	if (!tracing) {
 		tracing = tracefs_find_tracing_dir();
 		if (!tracing)
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 4/9] trace-cmd: Unit test for libtracefs
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
                   ` (2 preceding siblings ...)
  2020-01-29  9:54 ` [PATCH 3/9] trace-cmd: Add sanity check of tracefs_get_tracing_file() input parameter Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 5/9] trace-cmd: Unit tests for libtracefs APIs related to tracing file / directory Tzvetomir Stoyanov (VMware)
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

The CUnit test infrastructure is integrated in trace-cmd.
 http://cunit.sourceforge.net/
The library and its headers must be installed on the machine, in order
to build the trace-cmd unit tests. For Fedora, these packages must be installed:
   CUnit, CUnit-devel

A new directory is added:
 utest
containing unit tests implementation.

Added new target to trace-cmd top Makefile:
  make test
which builds the unit test binary:
 utest/trace-utest

The goal of this patch is not to provide full unit test coverage of
libtracefs, but to be a POC for adding test infrastructure to trace-cmd.

The first API, covered be the test is:
  tracefs_iterate_raw_events()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 Makefile              |  13 ++++-
 utest/Makefile        |  41 ++++++++++++++
 utest/README          |  15 +++++
 utest/trace-utest.c   |  83 ++++++++++++++++++++++++++++
 utest/trace-utest.h   |  11 ++++
 utest/tracefs-utest.c | 125 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 286 insertions(+), 2 deletions(-)
 create mode 100644 utest/Makefile
 create mode 100644 utest/README
 create mode 100644 utest/trace-utest.c
 create mode 100644 utest/trace-utest.h
 create mode 100644 utest/tracefs-utest.c

diff --git a/Makefile b/Makefile
index 477625f..ddf52ea 100644
--- a/Makefile
+++ b/Makefile
@@ -200,7 +200,7 @@ TRACE_LIBS = -L$(LIBTRACECMD_DIR) -ltracecmd		\
 	     -L$(LIBTRACEFS_DIR) -ltracefs
 
 export LIBS TRACE_LIBS
-export LIBTRACEEVENT_DIR LIBTRACECMD_DIR
+export LIBTRACEEVENT_DIR LIBTRACECMD_DIR LIBTRACEFS_DIR
 export LIBTRACECMD_STATIC LIBTRACECMD_SHARED
 export LIBTRACEEVENT_STATIC LIBTRACEEVENT_SHARED
 export LIBTRACEFS_STATIC LIBTRACEFS_SHARED
@@ -234,6 +234,9 @@ ifeq ($(VSOCK_DEFINED), 1)
 CFLAGS += -DVSOCK
 endif
 
+CUNIT_INSTALLED := $(shell if (echo -e "\#include <CUnit/Basic.h>\n void main(){CU_initialize_registry();}" | $(CC) -x c -lcunit - >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
+export CUNIT_INSTALLED
+
 export CFLAGS
 export INCLUDES
 
@@ -319,7 +322,6 @@ $(LIBTRACEFS_STATIC): force
 $(LIBTRACEFS_SHARED): force
 	$(Q)$(MAKE) -C $(src)/lib/tracefs $@
 
-
 libtraceevent.so: $(LIBTRACEEVENT_SHARED)
 libtraceevent.a: $(LIBTRACEEVENT_STATIC)
 libtracecmd.a: $(LIBTRACECMD_STATIC)
@@ -329,6 +331,12 @@ libtracefs.so: $(LIBTRACEFS_SHARED)
 
 libs: $(LIBTRACECMD_SHARED) $(LIBTRACEEVENT_SHARED) $(LIBTRACEFS_SHARED)
 
+test: force $(LIBTRACEEVENT_STATIC) $(LIBTRACEFS_STATIC) $(LIBTRACECMD_STATIC)
+ifneq ($(CUNIT_INSTALLED),1)
+	$(error CUnit framework not installed, cannot build unit tests))
+endif
+	$(Q)$(MAKE) -C $(src)/utest $@
+
 plugins_traceevent: force $(obj)/lib/traceevent/plugins/traceevent_plugin_dir \
 		   $(obj)/lib/traceevent/plugins/trace_python_dir
 	$(Q)$(MAKE) -C $(src)/lib/traceevent/plugins
@@ -430,6 +438,7 @@ clean:
 	$(MAKE) -C $(src)/lib/tracefs clean
 	$(MAKE) -C $(src)/lib/traceevent/plugins clean
 	$(MAKE) -C $(src)/lib/trace-cmd/plugins clean
+	$(MAKE) -C $(src)/utest clean
 	$(MAKE) -C $(src)/python clean
 	$(MAKE) -C $(src)/tracecmd clean
 	if [ -f $(kshark-dir)/build/Makefile ]; then $(MAKE) -C $(kshark-dir)/build clean; fi
diff --git a/utest/Makefile b/utest/Makefile
new file mode 100644
index 0000000..55aa46a
--- /dev/null
+++ b/utest/Makefile
@@ -0,0 +1,41 @@
+
+include $(src)/scripts/utils.mk
+
+bdir:=$(obj)/utest
+
+TARGETS = $(bdir)/trace-utest
+
+OBJS =
+OBJS += trace-utest.o
+OBJS += tracefs-utest.o
+
+LIBS += -lcunit				\
+	-L$(LIBTRACEFS_DIR) -ltracefs	\
+	-L$(LIBTRACEEVENT_DIR) -ltraceevent
+
+OBJS := $(OBJS:%.o=$(bdir)/%.o)
+DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
+
+$(bdir):
+	@mkdir -p $(bdir)
+
+$(OBJS): | $(bdir)
+$(DEPS): | $(bdir)
+
+$(bdir)/trace-utest: $(OBJS)
+	$(Q)$(do_app_build)
+
+$(bdir)/%.o: %.c
+	$(Q)$(call do_fpic_compile)
+
+$(DEPS): $(bdir)/.%.d: %.c
+	$(Q)$(CC) -M $(CPPFLAGS) $(CFLAGS) $< > $@
+
+$(OBJS): $(bdir)/%.o : $(bdir)/.%.d
+
+dep_includes := $(wildcard $(DEPS))
+
+test: $(TARGETS)
+
+clean:
+	$(RM) $(TARGETS) $(bdir)/*.o $(bdir)/.*.d
diff --git a/utest/README b/utest/README
new file mode 100644
index 0000000..f93630d
--- /dev/null
+++ b/utest/README
@@ -0,0 +1,15 @@
+
+Unit tests for trace-cmd libraries. The tests use CUnit framework:
+ http://cunit.sourceforge.net/
+which must be pre installed on the system, before building the unit tests.
+The framework can be downloaded, compiled and installed manually, or
+using a precompiled distro package:
+
+ Fedora:
+	 CUnit
+	 CUnit-devel
+
+ Ubuntu and Debian:
+	libcunit1
+	libcunit1-doc
+	libcunit1-dev
diff --git a/utest/trace-utest.c b/utest/trace-utest.c
new file mode 100644
index 0000000..58d4d4e
--- /dev/null
+++ b/utest/trace-utest.c
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
+ *
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <stdlib.h>
+
+#include <CUnit/CUnit.h>
+#include <CUnit/Basic.h>
+
+#include "trace-utest.h"
+
+enum unit_tests {
+	RUN_NONE	= 0,
+	RUN_TRACEFS	= (1 << 0),
+	RUN_ALL		= 0xFFFF
+};
+
+static void print_help(char **argv)
+{
+	printf("Usage: %s [OPTIONS]\n", basename(argv[0]));
+	printf("\t-s, --silent\tPrint test summary\n");
+	printf("\t-r, --run test\tRun specific test:\n");
+	printf("\t\t  tracefs   run libtracefs tests\n");
+	printf("\t-h, --help\tPrint usage information\n");
+	exit(0);
+}
+
+int main(int argc, char **argv)
+{
+	CU_BasicRunMode verbose = CU_BRM_VERBOSE;
+	enum unit_tests tests = RUN_NONE;
+
+	for (;;) {
+		int c;
+		int index = 0;
+		const char *opts = "+hsr:";
+		static struct option long_options[] = {
+			{"silent", no_argument, NULL, 's'},
+			{"run", required_argument, NULL, 'r'},
+			{"help", no_argument, NULL, 'h'},
+			{NULL, 0, NULL, 0}
+		};
+
+		c = getopt_long (argc, argv, opts, long_options, &index);
+		if (c == -1)
+			break;
+		switch (c) {
+		case 'r':
+			if (strcmp(optarg, "tracefs") == 0)
+				tests |= RUN_TRACEFS;
+			else
+				print_help(argv);
+			break;
+		case 's':
+			verbose = CU_BRM_SILENT;
+			break;
+		case 'h':
+		default:
+			print_help(argv);
+			break;
+		}
+	}
+
+	if (tests == RUN_NONE)
+		tests = RUN_ALL;
+
+	if (CU_initialize_registry() != CUE_SUCCESS) {
+		printf("Test registry cannot be initialized\n");
+		return -1;
+	}
+
+	if (tests & RUN_TRACEFS)
+		test_tracefs_lib();
+
+	CU_basic_set_mode(verbose);
+	CU_basic_run_tests();
+	CU_cleanup_registry();
+	return 0;
+}
diff --git a/utest/trace-utest.h b/utest/trace-utest.h
new file mode 100644
index 0000000..917c0e7
--- /dev/null
+++ b/utest/trace-utest.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+/*
+ * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
+ *
+ */
+#ifndef _TRACE_UTEST_H_
+#define _TRACE_UTEST_H_
+
+void test_tracefs_lib(void);
+
+#endif /* _TRACE_UTEST_H_ */
diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
new file mode 100644
index 0000000..d3ba213
--- /dev/null
+++ b/utest/tracefs-utest.c
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <CUnit/CUnit.h>
+#include <CUnit/Basic.h>
+
+#include "tracefs.h"
+
+#define TRACEFS_SUITE		"trasefs library"
+#define TEST_INSTANCE_NAME	"cunit_test_iter"
+#define TEST_ARRAY_SIZE		50
+
+static struct tracefs_instance *test_instance;
+static struct tep_handle *test_tep;
+static int test_array[TEST_ARRAY_SIZE];
+static int test_found;
+
+static int test_callback(struct tep_event *event, struct tep_record *record,
+			  int cpu, void *context)
+{
+	struct tep_format_field *field;
+	int val, i;
+
+	field = tep_find_field(event, "buf");
+	if (field) {
+		val = *((int *)(record->data + field->offset));
+		for (i = 0; i < TEST_ARRAY_SIZE; i++) {
+			if (test_array[i] == val) {
+				test_array[i] = 0;
+				test_found++;
+				break;
+			}
+		}
+	}
+
+	return 0;
+}
+
+static void test_iter_write(void)
+{
+	char *path;
+	int i, fd;
+	int ret;
+
+	path = tracefs_instance_get_file(test_instance, "trace_marker");
+	CU_TEST(path != NULL);
+	fd = open(path, O_WRONLY);
+	CU_TEST(fd >= 0);
+
+	for (i = 0; i < TEST_ARRAY_SIZE; i++) {
+		test_array[i] = random();
+		ret = write(fd, test_array + i, sizeof(int));
+		CU_TEST(ret == sizeof(int));
+	}
+
+	tracefs_put_tracing_file(path);
+	close(fd);
+}
+
+
+static void test_iter_raw_events(void)
+{
+	int ret;
+
+	ret = tracefs_iterate_raw_events(NULL, test_instance, test_callback, NULL);
+	CU_TEST(ret < 0);
+	ret = tracefs_iterate_raw_events(test_tep, NULL, test_callback, NULL);
+	CU_TEST(ret == 0);
+	ret = tracefs_iterate_raw_events(test_tep, test_instance, NULL, NULL);
+	CU_TEST(ret < 0);
+
+	test_found = 0;
+	test_iter_write();
+	ret = tracefs_iterate_raw_events(test_tep, test_instance,
+					 test_callback, NULL);
+	CU_TEST(ret == 0);
+	CU_TEST(test_found == TEST_ARRAY_SIZE);
+}
+
+static int test_suite_destroy(void)
+{
+	tracefs_instance_destroy(test_instance);
+	tracefs_instance_free(test_instance);
+	tep_free(test_tep);
+	return 0;
+}
+
+static int test_suite_init(void)
+{
+	const char *systems[] = {"ftrace", NULL};
+
+	test_tep = tracefs_local_events_system(NULL, systems);
+	if (test_tep == NULL)
+		return 1;
+
+	test_instance = tracefs_instance_alloc(TEST_INSTANCE_NAME);
+	if (test_instance == NULL)
+		return 1;
+
+	if (tracefs_instance_create(test_instance) < 0)
+		return 1;
+
+	return 0;
+}
+
+void test_tracefs_lib(void)
+{
+	CU_pSuite suite = NULL;
+
+	suite = CU_add_suite(TRACEFS_SUITE, test_suite_init, test_suite_destroy);
+	if (suite == NULL) {
+		fprintf(stderr, "Suite \"%s\" cannot be ceated\n", TRACEFS_SUITE);
+		return;
+	}
+	CU_add_test(suite, "tracefs_iterate_raw_events API",
+		    test_iter_raw_events);
+}
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 5/9] trace-cmd: Unit tests for libtracefs APIs related to tracing file / directory
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
                   ` (3 preceding siblings ...)
  2020-01-29  9:54 ` [PATCH 4/9] trace-cmd: Unit test for libtracefs Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 6/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace instances Tzvetomir Stoyanov (VMware)
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

Added unit tests for these APIs:
  tracefs_find_tracing_dir()
  tracefs_get_tracing_dir()
  tracefs_get_tracing_file()
  tracefs_put_tracing_file()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 utest/tracefs-utest.c | 55 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index d3ba213..2e6e96f 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -8,6 +8,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <time.h>
 
 #include <CUnit/CUnit.h>
 #include <CUnit/Basic.h>
@@ -85,6 +86,58 @@ static void test_iter_raw_events(void)
 	CU_TEST(test_found == TEST_ARRAY_SIZE);
 }
 
+#define RAND_STR_SIZE 20
+#define RAND_ASCII "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+static const char *get_rand_str()
+{
+	static char str[RAND_STR_SIZE];
+	static char sym[] = RAND_ASCII;
+	struct timespec clk;
+	int i;
+
+	clock_gettime(CLOCK_REALTIME, &clk);
+	srand(clk.tv_nsec);
+	for (i = 0; i < RAND_STR_SIZE; i++)
+		str[i] = sym[rand() % (sizeof(sym) - 1)];
+
+	str[RAND_STR_SIZE - 1] = 0;
+	return str;
+}
+
+static void test_trace_file(void)
+{
+	const char *tmp = get_rand_str();
+	const char *tdir;
+	struct stat st;
+	char *file;
+	char *dir;
+
+	dir = tracefs_find_tracing_dir();
+	CU_TEST(dir != NULL);
+	CU_TEST(stat(dir, &st) == 0);
+	CU_TEST(S_ISDIR(st.st_mode));
+
+	tdir  = tracefs_get_tracing_dir();
+	CU_TEST(tdir != NULL);
+	CU_TEST(stat(tdir, &st) == 0);
+	CU_TEST(S_ISDIR(st.st_mode));
+
+	CU_TEST(strcmp(dir, tdir) == 0);
+	free(dir);
+
+	file = tracefs_get_tracing_file(NULL);
+	CU_TEST(file == NULL);
+	file = tracefs_get_tracing_file(tmp);
+	CU_TEST(file != NULL);
+	CU_TEST(stat(file, &st) != 0);
+	tracefs_put_tracing_file(file);
+
+	file = tracefs_get_tracing_file("trace");
+	CU_TEST(file != NULL);
+	CU_TEST(stat(file, &st) == 0);
+	tracefs_put_tracing_file(file);
+}
+
 static int test_suite_destroy(void)
 {
 	tracefs_instance_destroy(test_instance);
@@ -120,6 +173,8 @@ void test_tracefs_lib(void)
 		fprintf(stderr, "Suite \"%s\" cannot be ceated\n", TRACEFS_SUITE);
 		return;
 	}
+	CU_add_test(suite, "tracing file / directory APIs",
+		    test_trace_file);
 	CU_add_test(suite, "tracefs_iterate_raw_events API",
 		    test_iter_raw_events);
 }
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 6/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace instances
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
                   ` (4 preceding siblings ...)
  2020-01-29  9:54 ` [PATCH 5/9] trace-cmd: Unit tests for libtracefs APIs related to tracing file / directory Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 7/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace events and systems Tzvetomir Stoyanov (VMware)
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

Added unit tests for these APIs:
  tracefs_instance_alloc()
  tracefs_instance_get_name()
  tracefs_instance_create()
  tracefs_instance_get_dir()
  tracefs_instance_get_file()
  tracefs_instance_file_read()
  tracefs_instance_file_write()
  tracefs_instance_free()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 utest/tracefs-utest.c | 122 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)

diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 2e6e96f..7930c64 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -138,6 +138,126 @@ static void test_trace_file(void)
 	tracefs_put_tracing_file(file);
 }
 
+static void test_instance_file_read(struct tracefs_instance *inst, char *fname)
+{
+	const char *tdir  = tracefs_get_tracing_dir();
+	char buf[BUFSIZ];
+	char *fpath;
+	char *file;
+	size_t fsize = 0;
+	int size = 0;
+	int fd;
+
+	if (inst) {
+		CU_TEST(asprintf(&fpath, "%s/instances/%s/%s",
+			tdir, tracefs_instance_get_name(inst), fname) > 0);
+	} else {
+		CU_TEST(asprintf(&fpath, "%s/%s", tdir, fname) > 0);
+	}
+
+	memset(buf, 0, BUFSIZ);
+	fd = open(fpath, O_RDONLY);
+	CU_TEST(fd >= 0);
+	fsize = read(fd, buf, BUFSIZ);
+	CU_TEST(fsize >= 0);
+	close(fd);
+	buf[BUFSIZ - 1] = 0;
+
+	file = tracefs_instance_file_read(inst, fname, &size);
+	CU_TEST(file != NULL);
+	CU_TEST(size == fsize);
+	CU_TEST(strcmp(file, buf) == 0);
+
+	free(fpath);
+	free(file);
+}
+
+#define ALL_TRACERS	"available_tracers"
+#define CUR_TRACER	"current_tracer"
+static void test_instance_file(void)
+{
+	struct tracefs_instance *instance = NULL;
+	const char *name = get_rand_str();
+	char *inst_name = NULL;
+	const char *tdir;
+	char *inst_file;
+	char *inst_dir;
+	struct stat st;
+	char *fname;
+	char *file1;
+	char *file2;
+	char *tracer;
+	int size;
+	int ret;
+
+	tdir  = tracefs_get_tracing_dir();
+	CU_TEST(tdir != NULL);
+	CU_TEST(asprintf(&inst_dir, "%s/instances/%s", tdir, name) > 0);
+	CU_TEST(stat(inst_dir, &st) != 0);
+
+	instance = tracefs_instance_alloc(name);
+	CU_TEST(instance != NULL);
+	CU_TEST(stat(inst_dir, &st) != 0);
+	inst_name = tracefs_instance_get_name(instance);
+	CU_TEST(inst_name != NULL);
+	CU_TEST(strcmp(inst_name, name) == 0);
+
+	CU_TEST(tracefs_instance_create(instance) == 0);
+	CU_TEST(stat(inst_dir, &st) == 0);
+	CU_TEST(S_ISDIR(st.st_mode));
+
+	fname = tracefs_instance_get_dir(NULL);
+	CU_TEST(fname != NULL);
+	CU_TEST(strcmp(fname, tdir) == 0);
+	free(fname);
+
+	fname = tracefs_instance_get_dir(instance);
+	CU_TEST(fname != NULL);
+	CU_TEST(strcmp(fname, inst_dir) == 0);
+	free(fname);
+
+	CU_TEST(asprintf(&fname, "%s/"ALL_TRACERS, tdir) > 0);
+	CU_TEST(fname != NULL);
+	inst_file = tracefs_instance_get_file(NULL, ALL_TRACERS);
+	CU_TEST(inst_file != NULL);
+	CU_TEST(strcmp(fname, inst_file) == 0);
+	tracefs_put_tracing_file(inst_file);
+	free(fname);
+
+	CU_TEST(asprintf(&fname, "%s/instances/%s/"ALL_TRACERS, tdir, name) > 0);
+	CU_TEST(fname != NULL);
+	CU_TEST(stat(fname, &st) == 0);
+	inst_file = tracefs_instance_get_file(instance, ALL_TRACERS);
+	CU_TEST(inst_file != NULL);
+	CU_TEST(strcmp(fname, inst_file) == 0);
+
+	test_instance_file_read(NULL, ALL_TRACERS);
+	test_instance_file_read(instance, ALL_TRACERS);
+
+	file1 = tracefs_instance_file_read(instance, ALL_TRACERS, NULL);
+	CU_TEST(file1 != NULL);
+	tracer = strtok(file1, " ");
+	CU_TEST(tracer != NULL);
+	ret = tracefs_instance_file_write(instance, CUR_TRACER, tracer);
+	CU_TEST(ret == strlen(tracer));
+	file2 = tracefs_instance_file_read(instance, CUR_TRACER, &size);
+	CU_TEST(file2 != NULL);
+	CU_TEST(size >= strlen(tracer));
+	CU_TEST(strncmp(file2, tracer, strlen(tracer)) == 0);
+	free(file1);
+	free(file2);
+
+	tracefs_put_tracing_file(inst_file);
+	free(fname);
+
+	CU_TEST(tracefs_instance_destroy(NULL) != 0);
+	CU_TEST(tracefs_instance_destroy(instance) == 0);
+	CU_TEST(tracefs_instance_destroy(instance) != 0);
+	tracefs_instance_free(instance);
+	CU_TEST(stat(inst_dir, &st) != 0);
+	free(inst_dir);
+}
+
 static int test_suite_destroy(void)
 {
 	tracefs_instance_destroy(test_instance);
@@ -175,6 +295,8 @@ void test_tracefs_lib(void)
 	}
 	CU_add_test(suite, "tracing file / directory APIs",
 		    test_trace_file);
+	CU_add_test(suite, "instance file / directory APIs",
+		    test_instance_file);
 	CU_add_test(suite, "tracefs_iterate_raw_events API",
 		    test_iter_raw_events);
 }
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 7/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace events and systems
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
                   ` (5 preceding siblings ...)
  2020-01-29  9:54 ` [PATCH 6/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace instances Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 8/9] trace-cmd: Unit test for tracefs_tracers() API Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 9/9] trace-cmd: Unit tests for libtracefs APIs related to allocating a tep handler based on local events Tzvetomir Stoyanov (VMware)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

Added unit tests for these APIs:
  tracefs_event_systems()
  tracefs_system_events()
  tracefs_list_free()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 utest/tracefs-utest.c | 66 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 7930c64..e4ab64f 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <time.h>
+#include <dirent.h>
 
 #include <CUnit/CUnit.h>
 #include <CUnit/Basic.h>
@@ -258,6 +259,69 @@ static void test_instance_file(void)
 	free(inst_dir);
 }
 
+static void exclude_string(char **strings, char *name)
+{
+	int i;
+
+	for (i = 0; strings[i]; i++) {
+		if (strcmp(strings[i], name) == 0) {
+			free(strings[i]);
+			strings[i] = strdup("/");
+			return;
+		}
+	}
+}
+
+static void test_check_files(const char *fdir, char **files)
+{
+	struct dirent *dent;
+	DIR *dir;
+	int i;
+
+	dir = opendir(fdir);
+	CU_TEST(dir != NULL);
+
+	while ((dent = readdir(dir)))
+		exclude_string(files, dent->d_name);
+
+	closedir(dir);
+
+	for (i = 0; files[i]; i++)
+		CU_TEST(files[i][0] == '/');
+}
+
+static void test_system_event(void)
+{
+	const char *tdir;
+	char **systems;
+	char **events;
+	char *sdir = NULL;
+
+	tdir  = tracefs_get_tracing_dir();
+	CU_TEST(tdir != NULL);
+
+	systems = tracefs_event_systems(tdir);
+	CU_TEST(systems != NULL);
+
+	events = tracefs_system_events(tdir, systems[0]);
+	CU_TEST(events != NULL);
+
+	asprintf(&sdir, "%s/events/%s", tdir, systems[0]);
+	CU_TEST(sdir != NULL);
+	test_check_files(sdir, events);
+	free(sdir);
+	sdir = NULL;
+
+	asprintf(&sdir, "%s/events", tdir);
+	CU_TEST(sdir != NULL);
+	test_check_files(sdir, systems);
+
+	tracefs_list_free(systems);
+	tracefs_list_free(events);
+
+	free(sdir);
+}
+
 static int test_suite_destroy(void)
 {
 	tracefs_instance_destroy(test_instance);
@@ -297,6 +361,8 @@ void test_tracefs_lib(void)
 		    test_trace_file);
 	CU_add_test(suite, "instance file / directory APIs",
 		    test_instance_file);
+	CU_add_test(suite, "systems and events APIs",
+		    test_system_event);
 	CU_add_test(suite, "tracefs_iterate_raw_events API",
 		    test_iter_raw_events);
 }
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 8/9] trace-cmd: Unit test for tracefs_tracers() API
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
                   ` (6 preceding siblings ...)
  2020-01-29  9:54 ` [PATCH 7/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace events and systems Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  2020-01-29  9:54 ` [PATCH 9/9] trace-cmd: Unit tests for libtracefs APIs related to allocating a tep handler based on local events Tzvetomir Stoyanov (VMware)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

A unit test for tracefs_tracers() API.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 utest/tracefs-utest.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index e4ab64f..1fd7f09 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -322,6 +322,35 @@ static void test_system_event(void)
 	free(sdir);
 }
 
+static void test_tracers(void)
+{
+	const char *tdir;
+	char **tracers;
+	char *tfile;
+	char *tracer;
+	int i;
+
+	tdir  = tracefs_get_tracing_dir();
+	CU_TEST(tdir != NULL);
+
+	tracers = tracefs_tracers(tdir);
+	CU_TEST(tracers != NULL);
+
+	tfile = tracefs_instance_file_read(NULL, ALL_TRACERS, NULL);
+
+	tracer = strtok(tfile, " ");
+	while (tracer) {
+		exclude_string(tracers, tracer);
+		tracer = strtok(NULL, " ");
+	}
+
+	for (i = 0; tracers[i]; i++)
+		CU_TEST(tracers[i][0] == '/');
+
+	tracefs_list_free(tracers);
+	free(tfile);
+}
+
 static int test_suite_destroy(void)
 {
 	tracefs_instance_destroy(test_instance);
@@ -365,4 +394,6 @@ void test_tracefs_lib(void)
 		    test_system_event);
 	CU_add_test(suite, "tracefs_iterate_raw_events API",
 		    test_iter_raw_events);
+	CU_add_test(suite, "tracefs_tracers API",
+		    test_tracers);
 }
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 9/9] trace-cmd: Unit tests for libtracefs APIs related to allocating a tep handler based on local events
  2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
                   ` (7 preceding siblings ...)
  2020-01-29  9:54 ` [PATCH 8/9] trace-cmd: Unit test for tracefs_tracers() API Tzvetomir Stoyanov (VMware)
@ 2020-01-29  9:54 ` Tzvetomir Stoyanov (VMware)
  8 siblings, 0 replies; 10+ messages in thread
From: Tzvetomir Stoyanov (VMware) @ 2020-01-29  9:54 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel

Added unit tests for these APIs:
  tracefs_local_events()
  tracefs_local_events_system()
  tracefs_fill_local_events()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 utest/tracefs-utest.c | 92 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 1fd7f09..2473e89 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -351,6 +351,96 @@ static void test_tracers(void)
 	free(tfile);
 }
 
+static void test_check_events(struct tep_handle *tep, char *system, bool exist)
+{
+	struct dirent *dent;
+	char file[PATH_MAX];
+	char buf[1024];
+	char *edir = NULL;
+	const char *tdir;
+	DIR *dir;
+	int fd;
+
+	tdir  = tracefs_get_tracing_dir();
+	CU_TEST(tdir != NULL);
+
+	asprintf(&edir, "%s/events/%s", tdir, system);
+	dir = opendir(edir);
+	CU_TEST(dir != NULL);
+
+	while ((dent = readdir(dir))) {
+		if (dent->d_name[0] == '.')
+			continue;
+		sprintf(file, "%s/%s/id", edir, dent->d_name);
+		fd = open(file, O_RDONLY);
+		if (fd < 0)
+			continue;
+		CU_TEST(read(fd, buf, 1024) > 0);
+		if (exist) {
+			CU_TEST(tep_find_event(tep, atoi(buf)) != NULL);
+		} else {
+			CU_TEST(tep_find_event(tep, atoi(buf)) == NULL);
+		}
+
+		close(fd);
+	}
+
+	closedir(dir);
+	free(edir);
+
+}
+
+static void test_local_events(void)
+{
+	struct tep_handle *tep;
+	const char *tdir;
+	char **systems;
+	char *lsystems[3];
+	int i;
+
+	tdir  = tracefs_get_tracing_dir();
+	CU_TEST(tdir != NULL);
+
+	tep = tracefs_local_events(tdir);
+	CU_TEST(tep != NULL);
+
+	systems = tracefs_event_systems(tdir);
+	CU_TEST(systems != NULL);
+
+	for (i = 0; systems[i]; i++)
+		test_check_events(tep, systems[i], true);
+	tep_free(tep);
+
+	memset(lsystems, 0, sizeof(lsystems));
+	for (i = 0; systems[i]; i++) {
+		if (!lsystems[0])
+			lsystems[0] = systems[i];
+		else if (!lsystems[2])
+			lsystems[2] = systems[i];
+		else
+			break;
+	}
+
+	if (lsystems[0] && lsystems[2]) {
+		tep = tracefs_local_events_system(tdir,
+						  (const char * const *)lsystems);
+		CU_TEST(tep != NULL);
+		test_check_events(tep, lsystems[0], true);
+		test_check_events(tep, lsystems[2], false);
+	}
+	tep_free(tep);
+
+	tep = tep_alloc();
+	CU_TEST(tep != NULL);
+	CU_TEST(tracefs_fill_local_events(tdir, tep, NULL) == 0);
+	for (i = 0; systems[i]; i++)
+		test_check_events(tep, systems[i], true);
+
+	tep_free(tep);
+
+	tracefs_list_free(systems);
+}
+
 static int test_suite_destroy(void)
 {
 	tracefs_instance_destroy(test_instance);
@@ -396,4 +486,6 @@ void test_tracefs_lib(void)
 		    test_iter_raw_events);
 	CU_add_test(suite, "tracefs_tracers API",
 		    test_tracers);
+	CU_add_test(suite, "tracefs_local events API",
+		    test_local_events);
 }
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, back to index

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-29  9:54 [PATCH 0/9] libtracefs: few fixes and a lot of unit tests Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 1/9] trace-cmd: fix description of tracefs_get_tracing_dir() API Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 2/9] trace-cmd: Remove tracefs_read_page_record() API Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 3/9] trace-cmd: Add sanity check of tracefs_get_tracing_file() input parameter Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 4/9] trace-cmd: Unit test for libtracefs Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 5/9] trace-cmd: Unit tests for libtracefs APIs related to tracing file / directory Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 6/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace instances Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 7/9] trace-cmd: Unit tests for libtracefs APIs related to ftrace events and systems Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 8/9] trace-cmd: Unit test for tracefs_tracers() API Tzvetomir Stoyanov (VMware)
2020-01-29  9:54 ` [PATCH 9/9] trace-cmd: Unit tests for libtracefs APIs related to allocating a tep handler based on local events Tzvetomir Stoyanov (VMware)

Linux-Trace-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-trace-devel/0 linux-trace-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-trace-devel linux-trace-devel/ https://lore.kernel.org/linux-trace-devel \
		linux-trace-devel@vger.kernel.org
	public-inbox-index linux-trace-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-trace-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git