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

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