From: Michael Jeanson Date: Thu, 25 Mar 2021 17:21:18 +0000 (-0400) Subject: Add unit tests for lttng_ust_strerror X-Git-Tag: v2.13.0-rc1~188 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=7ae0a9445db9a97a43f0054bd38144bfddb00ce0;p=lttng-ust.git Add unit tests for lttng_ust_strerror Change-Id: I0c85780835b41a1ace9e91f0edc104bfd272f51f Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- diff --git a/.gitignore b/.gitignore index 78ecf88f..00588540 100644 --- a/.gitignore +++ b/.gitignore @@ -85,6 +85,7 @@ tests/unit/libringbuffer/test_shm tests/unit/pthread_name/test_pthread_name tests/unit/snprintf/test_snprintf tests/unit/ust-elf/ust-elf +tests/unit/ust-error/test_ust_error # Java agent library *.class diff --git a/configure.ac b/configure.ac index 5c060fb5..5e4f8671 100644 --- a/configure.ac +++ b/configure.ac @@ -562,6 +562,7 @@ AC_CONFIG_FILES([ tests/unit/pthread_name/Makefile tests/unit/snprintf/Makefile tests/unit/ust-elf/Makefile + tests/unit/ust-error/Makefile tests/utils/Makefile lttng-ust.pc lttng-ust-ctl.pc diff --git a/tests/Makefile.am b/tests/Makefile.am index 7be53331..d3ba1f63 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -14,7 +14,8 @@ TESTS = \ unit/libmsgpack/test_msgpack \ unit/pthread_name/test_pthread_name \ unit/snprintf/test_snprintf \ - unit/ust-elf/test_ust_elf + unit/ust-elf/test_ust_elf \ + unit/ust-error/test_ust_error EXTRA_DIST = README diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index ecca28ef..c8f52c3c 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -6,4 +6,5 @@ SUBDIRS = \ libringbuffer \ pthread_name \ snprintf \ - ust-elf + ust-elf \ + ust-error diff --git a/tests/unit/ust-error/Makefile.am b/tests/unit/ust-error/Makefile.am new file mode 100644 index 00000000..549b8339 --- /dev/null +++ b/tests/unit/ust-error/Makefile.am @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: LGPL-2.1-only + +AM_CPPFLAGS += -I$(top_srcdir)/tests/utils + +noinst_PROGRAMS = test_ust_error +test_ust_error_SOURCES = ust-error.c +test_ust_error_LDADD = $(top_builddir)/liblttng-ust/liblttng-ust.la \ + $(top_builddir)/tests/utils/libtap.a diff --git a/tests/unit/ust-error/ust-error.c b/tests/unit/ust-error/ust-error.c new file mode 100644 index 00000000..9b80e26b --- /dev/null +++ b/tests/unit/ust-error/ust-error.c @@ -0,0 +1,77 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * Copyright (C) 2021 Michael Jeanson + */ + +#include +#include +#include +#include +#include + +#include + +#include "tap.h" + +/* + * Sync with liblttng-ust-comm/lttng-ust-comm.c + */ +static const char *ok_str = "Success"; +static const char *unknown_str = "Unknown error"; +static const char *noent_str = "No entry"; +static const char *peercred_str = "Peer credentials PID is invalid. Socket appears to belong to a distinct, non-nested pid namespace."; + +#define NUM_TESTS 12 + +static +void test_ust_error(void) +{ + const char *error_str = NULL; + + error_str = lttng_ust_strerror(LTTNG_UST_OK); + ok(strcmp(ok_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_OK returns '%s' (%s)", ok_str, error_str); + + error_str = lttng_ust_strerror(-LTTNG_UST_OK); + ok(strcmp(ok_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_OK returns '%s' (%s)", ok_str, error_str); + + error_str = lttng_ust_strerror(INT_MAX); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive large int returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(INT_MIN); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Negative large int returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(LTTNG_UST_ERR_NR); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_NR returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(-LTTNG_UST_ERR_NR); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_NR returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(LTTNG_UST_ERR_NR + 1); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_NR + 1 returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(-LTTNG_UST_ERR_NR - 1); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_NR - 1 returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(LTTNG_UST_ERR_NOENT); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_NOENT returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(-LTTNG_UST_ERR_NOENT); + ok(strcmp(noent_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_NOENT returns '%s' (%s)", noent_str, error_str); + + /* Last error code */ + error_str = lttng_ust_strerror(LTTNG_UST_ERR_PEERCRED_PID); + ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_PEERCRED_PID returns '%s' (%s)", unknown_str, error_str); + + error_str = lttng_ust_strerror(-LTTNG_UST_ERR_PEERCRED_PID); + ok(strcmp(peercred_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_PEERCRED_PID returns '%s' (%s)", peercred_str, error_str); +} + +int main(void) +{ + plan_tests(NUM_TESTS); + + test_ust_error(); + + return exit_status(); +}