Blame SOURCES/0189-testsuite-add-simple-helper-macros.patch

2c83a8
From 83ff1e3f0b925910c0965490a26860a7c5efaa57 Mon Sep 17 00:00:00 2001
2c83a8
From: Matej Habrnal <mhabrnal@redhat.com>
2c83a8
Date: Tue, 22 Mar 2016 17:13:38 +0100
2c83a8
Subject: [PATCH] testsuite: add simple helper macros
2c83a8
2c83a8
I am tired of repeating the same constructions over and over again.
2c83a8
2c83a8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
2c83a8
---
2c83a8
 tests/Makefile.am               |   2 +-
2c83a8
 tests/atlocal.in                |   2 +-
2c83a8
 tests/helpers/testsuite.h       | 297 ++++++++++++++++++++++++++++++++++++++++
2c83a8
 tests/helpers/testsuite_tools.h |  67 +++++++++
2c83a8
 4 files changed, 366 insertions(+), 2 deletions(-)
2c83a8
 create mode 100644 tests/helpers/testsuite.h
2c83a8
 create mode 100644 tests/helpers/testsuite_tools.h
2c83a8
2c83a8
diff --git a/tests/Makefile.am b/tests/Makefile.am
2c83a8
index 9aa3a07..9bfc2b6 100644
2c83a8
--- a/tests/Makefile.am
2c83a8
+++ b/tests/Makefile.am
2c83a8
@@ -58,7 +58,7 @@ MAINTAINERCLEANFILES = Makefile.in $(TESTSUITE)
2c83a8
 check_DATA = atconfig atlocal $(TESTSUITE)
2c83a8
 DISTCLEANFILES = atconfig
2c83a8
 EXTRA_DIST += atlocal.in conf ureport ureport-rhts-credentials \
2c83a8
-              bugzilla_plugin.at.in
2c83a8
+              helpers/testsuite.h bugzilla_plugin.at.in
2c83a8
 
2c83a8
 atconfig: $(top_builddir)/config.status
2c83a8
 	(cd ${top_builddir} && ./config.status ${subdir}/atconfig)
2c83a8
diff --git a/tests/atlocal.in b/tests/atlocal.in
2c83a8
index 1a82edb..3d6b04a 100644
2c83a8
--- a/tests/atlocal.in
2c83a8
+++ b/tests/atlocal.in
2c83a8
@@ -6,7 +6,7 @@ CC='@CC@'
2c83a8
 LIBTOOL="$abs_top_builddir/libtool"
2c83a8
 
2c83a8
 # We want no optimization.
2c83a8
-CFLAGS="@O0CFLAGS@ -I$abs_top_builddir/src/include -I$abs_top_builddir/src/lib -I$abs_top_builddir/src/gtk-helpers -D_GNU_SOURCE @GLIB_CFLAGS@ @GTK_CFLAGS@ -DDEFAULT_DUMP_DIR_MODE=@DEFAULT_DUMP_DIR_MODE@"
2c83a8
+CFLAGS="@O0CFLAGS@ -I$abs_top_builddir/tests/helpers -I$abs_top_builddir/src/include -I$abs_top_builddir/src/lib -I$abs_top_builddir/src/gtk-helpers -D_GNU_SOURCE @GLIB_CFLAGS@ @GTK_CFLAGS@ -DDEFAULT_DUMP_DIR_MODE=@DEFAULT_DUMP_DIR_MODE@"
2c83a8
 
2c83a8
 # Are special link options needed?
2c83a8
 LDFLAGS="@LDFLAGS@"
2c83a8
diff --git a/tests/helpers/testsuite.h b/tests/helpers/testsuite.h
2c83a8
new file mode 100644
2c83a8
index 0000000..28bfd3e
2c83a8
--- /dev/null
2c83a8
+++ b/tests/helpers/testsuite.h
2c83a8
@@ -0,0 +1,297 @@
2c83a8
+/*
2c83a8
+    Copyright (C) 2015  ABRT team <crash-catcher@lists.fedorahosted.org>
2c83a8
+    Copyright (C) 2015  RedHat inc.
2c83a8
+
2c83a8
+    This program is free software; you can redistribute it and/or modify
2c83a8
+    it under the terms of the GNU General Public License as published by
2c83a8
+    the Free Software Foundation; either version 2 of the License, or
2c83a8
+    (at your option) any later version.
2c83a8
+
2c83a8
+    This program is distributed in the hope that it will be useful,
2c83a8
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
2c83a8
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2c83a8
+    GNU General Public License for more details.
2c83a8
+
2c83a8
+    You should have received a copy of the GNU General Public License along
2c83a8
+    with this program; if not, write to the Free Software Foundation, Inc.,
2c83a8
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2c83a8
+
2c83a8
+    ----
2c83a8
+
2c83a8
+    libreport testsuite helpers
2c83a8
+
2c83a8
+    Feel free to add whatever macro you need but please try to keep this file
2c83a8
+    short and useful.
2c83a8
+
2c83a8
+    Bare in mind usability and print as much accurate log messages as possible:
2c83a8
+
2c83a8
+        Example 1:
2c83a8
+
2c83a8
+            int actual = 0;
2c83a8
+            int expected = 1;
2c83a8
+            TS_ASSERT_SIGNED_EQ(actual, expected)
2c83a8
+
2c83a8
+            ----
2c83a8
+
2c83a8
+            [ FAILED ] 12: Assert (actual == expected)
2c83a8
+                Actual  : 0
2c83a8
+                Expected: 1
2c83a8
+
2c83a8
+
2c83a8
+        Example 2:
2c83a8
+
2c83a8
+            int get_runtime_number() {
2c83a8
+                return 0;
2c83a8
+            }
2c83a8
+
2c83a8
+            TS_ASSERT_SIGNED_OP_MESSAGE(get_runtime_number(), 1, "Custom message")
2c83a8
+
2c83a8
+            ----
2c83a8
+
2c83a8
+            [ FAILED ] 3: Custom messages (get_runtime_number() >= 1)
2c83a8
+                Actual  : 0
2c83a8
+                Expected: 1
2c83a8
+
2c83a8
+    Note: the number right behind [ FAILED ] is line number where the failed
2c83a8
+          assert is located.
2c83a8
+*/
2c83a8
+#ifndef LIBREPORT_TESTSUITE_H
2c83a8
+#define LIBREPORT_TESTSUITE_H
2c83a8
+
2c83a8
+/* For g_verbose */
2c83a8
+#include "internal_libreport.h"
2c83a8
+
2c83a8
+/* For convenience */
2c83a8
+#include <assert.h>
2c83a8
+
2c83a8
+
2c83a8
+/* Number of failed asserts and other failures. Can be used a return value of
2c83a8
+ * the main function. */
2c83a8
+long g_testsuite_fails = 0;
2c83a8
+
2c83a8
+/* Number of successful asserts. For debugging purpose. */
2c83a8
+long g_testsuite_ok = 0;
2c83a8
+
2c83a8
+/* Enables additional log messages. */
2c83a8
+int g_testsuite_debug = 0;
2c83a8
+
2c83a8
+/* Can be used to change log messages destination. */
2c83a8
+FILE *g_testsuite_output_stream = 0;
2c83a8
+
2c83a8
+
2c83a8
+/*
2c83a8
+ * Test case definition
2c83a8
+ */
2c83a8
+
2c83a8
+#define TS_MAIN \
2c83a8
+    int main(int argc, char *argv[]) { g_verbose = 3; do
2c83a8
+
2c83a8
+#define TS_RETURN_MAIN \
2c83a8
+    while (0) ;\
2c83a8
+    return g_testsuite_fails; }
2c83a8
+
2c83a8
+
2c83a8
+/*
2c83a8
+ * Logging
2c83a8
+ */
2c83a8
+
2c83a8
+#define TS_PRINTF(format, ...) \
2c83a8
+    fprintf(g_testsuite_output_stream != NULL ? g_testsuite_output_stream : stderr, format, __VA_ARGS__)
2c83a8
+
2c83a8
+#define TS_DEBUG_PRINTF(format, ...) \
2c83a8
+    do { if (g_testsuite_debug) { TS_PRINTF(format, __VA_ARGS__); } } while (0)
2c83a8
+
2c83a8
+
2c83a8
+/*
2c83a8
+ * Handling of test results
2c83a8
+ */
2c83a8
+
2c83a8
+#define TS_SUCCESS(format, ...) \
2c83a8
+    do { \
2c83a8
+        TS_DEBUG_PRINTF("[   OK   ] %d: ", __LINE__); \
2c83a8
+        TS_DEBUG_PRINTF(format, __VA_ARGS__); \
2c83a8
+        ++g_testsuite_ok; \
2c83a8
+    } while (0)
2c83a8
+
2c83a8
+#define TS_FAILURE(format, ...) \
2c83a8
+    do { \
2c83a8
+        TS_PRINTF("[ FAILED ] %d: ", __LINE__); \
2c83a8
+        TS_PRINTF(format, __VA_ARGS__); \
2c83a8
+        ++g_testsuite_fails; \
2c83a8
+    } while (0)
2c83a8
+
2c83a8
+
2c83a8
+/*
2c83a8
+ * Logical conditions
2c83a8
+ */
2c83a8
+
2c83a8
+#define _TS_ASSERT_BOOLEAN(expression, expected, message) \
2c83a8
+    do { \
2c83a8
+        const int result = (expression); \
2c83a8
+        if (result == expected) { \
2c83a8
+            TS_SUCCESS("%s ("#expression" == %s)\n", message ? message : "Assert", expected ? "TRUE" : "FALSE"); \
2c83a8
+        }\
2c83a8
+        else { \
2c83a8
+            TS_FAILURE("%s ("#expression" == %s)\n", message ? message : "Assert", expected ? "TRUE" : "FALSE"); \
2c83a8
+        }\
2c83a8
+    } while(0)
2c83a8
+
2c83a8
+
2c83a8
+#define TS_ASSERT_TRUE_MESSAGE(expression, message) \
2c83a8
+    _TS_ASSERT_BOOLEAN(expression, 1, message)
2c83a8
+
2c83a8
+#define TS_ASSERT_TRUE(expression) \
2c83a8
+    TS_ASSERT_TRUE_MESSAGE(expression, NULL)
2c83a8
+
2c83a8
+#define TS_ASSERT_FALSE_MESSAGE(expression, message) \
2c83a8
+    _TS_ASSERT_BOOLEAN(expression, 0, message)
2c83a8
+
2c83a8
+#define TS_ASSERT_FALSE(expression) \
2c83a8
+    TS_ASSERT_FALSE_MESSAGE(expression, NULL)
2c83a8
+
2c83a8
+/*
2c83a8
+ * Testing of signed numbers
2c83a8
+ */
2c83a8
+
2c83a8
+#define TS_ASSERT_SIGNED_OP_MESSAGE(actual, operator, expected, message) \
2c83a8
+    do { \
2c83a8
+        long long l_ts_lhs = (actual); \
2c83a8
+        long long l_ts_rhs = (expected); \
2c83a8
+        if (l_ts_lhs operator l_ts_rhs) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" "#operator" "#expected")\n\tActual  : %lld\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else { \
2c83a8
+            TS_FAILURE("%s ("#actual" "#operator" "#expected")\n\tActual  : %lld\n\tExpected: %lld\n", message ? message : "Assert", l_ts_lhs, l_ts_rhs); \
2c83a8
+        } \
2c83a8
+    } while(0)
2c83a8
+
2c83a8
+#define TS_ASSERT_SIGNED_EQ(actual, expected) \
2c83a8
+    TS_ASSERT_SIGNED_OP_MESSAGE(actual, ==, expected, NULL)
2c83a8
+
2c83a8
+#define TS_ASSERT_SIGNED_GE(actual, expected) \
2c83a8
+    TS_ASSERT_SIGNED_OP_MESSAGE(actual, >=, expected, NULL)
2c83a8
+
2c83a8
+
2c83a8
+/*
2c83a8
+ * Testing of chars
2c83a8
+ */
2c83a8
+
2c83a8
+#define TS_ASSERT_CHAR_OP_MESSAGE(actual, operator, expected, message) \
2c83a8
+    do { \
2c83a8
+        char l_ts_lhs = (actual); \
2c83a8
+        char l_ts_rhs = (expected); \
2c83a8
+        if (l_ts_lhs operator l_ts_rhs) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" "#operator" "#expected")\n\tActual  : %c\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else { \
2c83a8
+            TS_FAILURE("%s ("#actual" "#operator" "#expected")\n\tActual  : %c\n\tExpected: %c\n", message ? message : "Assert", l_ts_lhs, l_ts_rhs); \
2c83a8
+        } \
2c83a8
+    } while(0)
2c83a8
+
2c83a8
+#define TS_ASSERT_CHAR_EQ_MESSAGE(actual, expected, message) \
2c83a8
+    TS_ASSERT_CHAR_OP_MESSAGE(actual, ==, expected, message)
2c83a8
+
2c83a8
+#define TS_ASSERT_CHAR_EQ(actual, expected) \
2c83a8
+    TS_ASSERT_CHAR_EQ_MESSAGE(actual, ==, expected, NULL)
2c83a8
+
2c83a8
+
2c83a8
+/*
2c83a8
+ * Testing of strings
2c83a8
+ */
2c83a8
+
2c83a8
+#define TS_ASSERT_STRING_EQ(actual, expected, message) \
2c83a8
+    do { \
2c83a8
+        const char *l_ts_lhs = (actual); \
2c83a8
+        const char *l_ts_rhs = (expected); \
2c83a8
+        if (l_ts_lhs == NULL && l_ts_rhs != NULL) { \
2c83a8
+            TS_FAILURE("%s ("#actual" == "#expected")\n\tActual  : NULL\n\tExpected: %p\n", message ? message : "Assert", l_ts_rhs); \
2c83a8
+        } \
2c83a8
+        else if (l_ts_lhs != NULL && l_ts_rhs == NULL) { \
2c83a8
+            TS_FAILURE("%s ("#actual" == "#expected")\n\tActual  : %s\n\tExpected: NULL\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else if ((l_ts_rhs == NULL && l_ts_rhs == NULL)) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" == "#expected")\n\tActual  : NULL\n", message ? message : "Assert"); \
2c83a8
+        } \
2c83a8
+        else if (strcmp(l_ts_lhs, l_ts_rhs) == 0) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" == "#expected")\n\tActual  : %s\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else { \
2c83a8
+            TS_FAILURE("%s ("#actual" == "#expected")\n\tActual  : %s\n\tExpected: %s\n", message ? message : "Assert", l_ts_lhs, l_ts_rhs); \
2c83a8
+        } \
2c83a8
+    } while(0)
2c83a8
+
2c83a8
+#define TS_ASSERT_STRING_BEGINS_WITH(actual, prefix, message) \
2c83a8
+    do { \
2c83a8
+        const char *l_ts_lhs = (actual); \
2c83a8
+        const char *l_ts_rhs = (prefix); \
2c83a8
+        if (l_ts_lhs == NULL && l_ts_rhs != NULL) { \
2c83a8
+            TS_FAILURE("%s ("#actual" begins with "#prefix")\n\tActual  : NULL\n\tExpected: %p\n", message ? message : "Assert", l_ts_rhs); \
2c83a8
+        } \
2c83a8
+        else if (l_ts_lhs != NULL && l_ts_rhs == NULL) { \
2c83a8
+            TS_FAILURE("%s ("#actual" begins with "#prefix")\n\tActual  : %s\n\tExpected: NULL\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else if ((l_ts_rhs == NULL && l_ts_rhs == NULL)) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" begins with "#prefix")\n\tActual  : NULL\n", message ? message : "Assert"); \
2c83a8
+        } \
2c83a8
+        else if (strncmp(l_ts_lhs, l_ts_rhs, strlen(l_ts_rhs)) == 0) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" begins with "#prefix")\n\tActual  : %s\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else { \
2c83a8
+            TS_FAILURE("%s ("#actual" begins with "#prefix")\n\tActual  : %s\n\tExpected: %s\n", message ? message : "Assert", l_ts_lhs, l_ts_rhs); \
2c83a8
+        } \
2c83a8
+    } while(0)
2c83a8
+
2c83a8
+#define TS_ASSERT_STRING_NULL_OR_EMPTY(actual, message) \
2c83a8
+    do { \
2c83a8
+        const char *l_ts_lhs = (actual); \
2c83a8
+        if (l_ts_lhs != NULL && l_ts_lhs[0] != '\0') { \
2c83a8
+            TS_FAILURE("%s ("#actual" is NULL or empty)\n\tActual  : %s\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else if ((l_ts_lhs != NULL && l_ts_lhs[0] == '\0')) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" is NULL or empty)\n\tActual  : is empty\n", message ? message : "Assert"); \
2c83a8
+        } \
2c83a8
+        else if (l_ts_lhs == NULL) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" is NULL or empty)\n\tActual  : is NULL\n", message ? message : "Assert"); \
2c83a8
+        } \
2c83a8
+        else { \
2c83a8
+            TS_PRINTF("%s", "Invalid conditions in TS_ASSERT_STRING_NULL_OR_EMPTY"); \
2c83a8
+            abort(); \
2c83a8
+        } \
2c83a8
+    } while(0)
2c83a8
+
2c83a8
+/*
2c83a8
+ * Testing of pointers
2c83a8
+ */
2c83a8
+
2c83a8
+#define TS_ASSERT_PTR_OP_MESSAGE(actual, operator, expected, message) \
2c83a8
+    do { \
2c83a8
+        const void *l_ts_lhs = (actual); \
2c83a8
+        const void *l_ts_rhs = (expected); \
2c83a8
+        if (l_ts_lhs operator l_ts_rhs) { \
2c83a8
+            TS_SUCCESS("%s ("#actual" "#operator" "#expected")\n\tActual  : %p\n", message ? message : "Assert", l_ts_lhs); \
2c83a8
+        } \
2c83a8
+        else { \
2c83a8
+            TS_FAILURE("%s ("#actual" "#operator" "#expected")\n\tActual  : %p\n\tExpected: %p\n", message ? message : "Assert", l_ts_lhs, l_ts_rhs); \
2c83a8
+        } \
2c83a8
+    } while(0)
2c83a8
+
2c83a8
+
2c83a8
+#define TS_ASSERT_PTR_IS_NULL_MESSAGE(actual, message) \
2c83a8
+    TS_ASSERT_PTR_OP_MESSAGE(actual, ==, NULL, message);
2c83a8
+
2c83a8
+#define TS_ASSERT_PTR_IS_NULL(actual) \
2c83a8
+    TS_ASSERT_PTR_IS_NULL_MESSAGE(actual, NULL);
2c83a8
+
2c83a8
+
2c83a8
+#define TS_ASSERT_PTR_IS_NOT_NULL_MESSAGE(actual, message) \
2c83a8
+    TS_ASSERT_PTR_OP_MESSAGE(actual, !=, NULL, message);
2c83a8
+
2c83a8
+#define TS_ASSERT_PTR_IS_NOT_NULL(actual) \
2c83a8
+    TS_ASSERT_PTR_IS_NOT_NULL_MESSAGE(actual, NULL);
2c83a8
+
2c83a8
+
2c83a8
+#define TS_ASSERT_PTR_EQ(actual, expected) \
2c83a8
+    TS_ASSERT_PTR_OP_MESSAGE(actual, ==, expected, NULL);
2c83a8
+
2c83a8
+
2c83a8
+#endif/*LIBREPORT_TESTSUITE_H*/
2c83a8
diff --git a/tests/helpers/testsuite_tools.h b/tests/helpers/testsuite_tools.h
2c83a8
new file mode 100644
2c83a8
index 0000000..ed3a557
2c83a8
--- /dev/null
2c83a8
+++ b/tests/helpers/testsuite_tools.h
2c83a8
@@ -0,0 +1,67 @@
2c83a8
+/*
2c83a8
+    Copyright (C) 2015  ABRT team <crash-catcher@lists.fedorahosted.org>
2c83a8
+    Copyright (C) 2015  RedHat inc.
2c83a8
+
2c83a8
+    This program is free software; you can redistribute it and/or modify
2c83a8
+    it under the terms of the GNU General Public License as published by
2c83a8
+    the Free Software Foundation; either version 2 of the License, or
2c83a8
+    (at your option) any later version.
2c83a8
+
2c83a8
+    This program is distributed in the hope that it will be useful,
2c83a8
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
2c83a8
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2c83a8
+    GNU General Public License for more details.
2c83a8
+
2c83a8
+    You should have received a copy of the GNU General Public License along
2c83a8
+    with this program; if not, write to the Free Software Foundation, Inc.,
2c83a8
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2c83a8
+
2c83a8
+    ----
2c83a8
+
2c83a8
+    Helper functions
2c83a8
+*/
2c83a8
+
2c83a8
+#include "testsuite.h"
2c83a8
+
2c83a8
+/* Creates a new dump directory in a new temporary directory
2c83a8
+ */
2c83a8
+static struct dump_dir *testsuite_dump_dir_create(uid_t uid, mode_t mode, int ts_flags)
2c83a8
+{
2c83a8
+    char dump_dir_name[] = "/tmp/XXXXXX/dump_dir";
2c83a8
+
2c83a8
+    char *last_slash = strrchr(dump_dir_name, '/');
2c83a8
+    *last_slash = '\0';
2c83a8
+
2c83a8
+    if (mkdtemp(dump_dir_name) == NULL) {
2c83a8
+        perror("mkdtemp()");
2c83a8
+        abort();
2c83a8
+    }
2c83a8
+
2c83a8
+    fprintf(stdout, "Test temp directory: %s\n", dump_dir_name);
2c83a8
+    fflush(stdout);
2c83a8
+
2c83a8
+    *last_slash = '/';
2c83a8
+
2c83a8
+    struct dump_dir *dd = dd_create(dump_dir_name, uid, mode == (mode_t)-1 ? 0640 : mode);
2c83a8
+    assert(dd != NULL);
2c83a8
+
2c83a8
+    return dd;
2c83a8
+}
2c83a8
+
2c83a8
+/* Removes the dump directory in and the temporary directory
2c83a8
+ *
2c83a8
+ * See testsuite_dump_dir_create()
2c83a8
+ */
2c83a8
+static void testsuite_dump_dir_delete(struct dump_dir *dd)
2c83a8
+{
2c83a8
+    char *tmp_dir = xstrndup(dd->dd_dirname, strrchr(dd->dd_dirname, '/') - dd->dd_dirname);
2c83a8
+    assert(dd_delete(dd) == 0);
2c83a8
+
2c83a8
+    if(rmdir(tmp_dir) != 0)
2c83a8
+    {
2c83a8
+        perror("rmdir()");
2c83a8
+        abort();
2c83a8
+    }
2c83a8
+
2c83a8
+    free(tmp_dir);
2c83a8
+}
2c83a8
-- 
2c83a8
1.8.3.1
2c83a8