From 4e3201fa6f3408b4f20c8c4c2f600659fc69fa44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 9 Aug 2011 12:43:28 +0200
Subject: [PATCH 5/6] New DEBUG facility - unit tests

https://fedorahosted.org/sssd/ticket/925
---
 Makefile.am             |   11 +
 src/tests/debug-tests.c |  711 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 722 insertions(+), 0 deletions(-)
 create mode 100644 src/tests/debug-tests.c

diff --git a/Makefile.am b/Makefile.am
index c0267d2..7f6f2ce 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -84,6 +84,7 @@ if HAVE_CHECK
         simple_access-tests \
         crypto-tests \
         util-tests \
+        debug-tests \
         ipa_hbac-tests
 endif
 
@@ -716,6 +717,16 @@ util_tests_LDADD = \
     libsss_util.la \
     libsss_test_common.la
 
+debug_tests_SOURCES = \
+    src/tests/debug-tests.c
+debug_tests_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(CHECK_CFLAGS)
+debug_tests_LDADD = \
+    $(SSSD_LIBS) \
+    $(CHECK_LIBS) \
+    libsss_util.la
+
 crypto_tests_SOURCES = \
     $(SSS_CRYPT_SOURCES) \
     src/tests/crypto-tests.c
diff --git a/src/tests/debug-tests.c b/src/tests/debug-tests.c
new file mode 100644
index 0000000..38e6add
--- /dev/null
+++ b/src/tests/debug-tests.c
@@ -0,0 +1,711 @@
+/*
+    SSSD
+
+    debug-tests.c
+
+    Authors:
+        Pavel Březina <pbrezina@redhat.com>
+
+    Copyright (C) 2011 Red Hat
+
+    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; either version 3 of the License, or
+    (at your option) any later version.
+
+    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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <check.h>
+#include <stdio.h>
+#include <talloc.h>
+#include <errno.h>
+#include <string.h>
+#include "util/util.h"
+
+#define DEBUG_TEST_NOK      -1
+#define DEBUG_TEST_NOK_TS   -2
+
+START_TEST(test_debug_convert_old_level_old_format)
+{
+    int expected_level = 0x0000;
+    int old_level;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+
+    fail_unless(
+        debug_convert_old_level(SSS_UNRESOLVED_DEBUG_LEVEL) == SSSDBG_UNRESOLVED,
+        "Invalid conversion of SSS_UNRESOLVED_DEBUG_LEVEL"
+    );
+
+    for (old_level = 0; old_level <= 9; old_level++) {
+        expected_level |= levels[old_level];
+
+        char msg[50] = {'\0'};
+        sprintf(msg, "Invalid conversion of %d", old_level);
+        fail_unless(debug_convert_old_level(old_level) == expected_level, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_convert_old_level_new_format)
+{
+    fail_unless(
+        debug_convert_old_level(SSSDBG_UNRESOLVED) == SSSDBG_UNRESOLVED,
+        "Invalid conversion of SSSDBG_UNRESOLVED"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_FATAL_FAILURE) == SSSDBG_FATAL_FAILURE,
+        "Invalid conversion of SSSDBG_FATAL_FAILURE"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_CRIT_FAILURE) == SSSDBG_CRIT_FAILURE,
+        "Invalid conversion of SSSDBG_CRIT_FAILURE"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_OP_FAILURE) == SSSDBG_OP_FAILURE,
+        "Invalid conversion of SSSDBG_OP_FAILURE"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_MINOR_FAILURE) == SSSDBG_MINOR_FAILURE,
+        "Invalid conversion of SSSDBG_MINOR_FAILURE"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_CONF_SETTINGS) == SSSDBG_CONF_SETTINGS,
+        "Invalid conversion of SSSDBG_CONF_SETTINGS"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_FUNC_DATA) == SSSDBG_FUNC_DATA,
+        "Invalid conversion of SSSDBG_FUNC_DATA"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_TRACE_FUNC) == SSSDBG_TRACE_FUNC,
+        "Invalid conversion of SSSDBG_TRACE_FUNC"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_TRACE_LIBS) == SSSDBG_TRACE_LIBS,
+        "Invalid conversion of SSSDBG_TRACE_LIBS"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_TRACE_INTERNAL) == SSSDBG_TRACE_INTERNAL,
+        "Invalid conversion of SSSDBG_TRACE_INTERNAL"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_TRACE_ALL) == SSSDBG_TRACE_ALL,
+        "Invalid conversion of SSSDBG_TRACE_ALL"
+    );
+    fail_unless(
+        debug_convert_old_level(SSSDBG_MASK_ALL) == SSSDBG_MASK_ALL,
+        "Invalid conversion of SSSDBG_MASK_ALL"
+    );
+}
+END_TEST
+
+START_TEST(test_debug_get_level_old_format)
+{
+    fail_unless(debug_get_level(SSS_UNRESOLVED_DEBUG_LEVEL) == SSSDBG_UNRESOLVED, "Invalid conversion of -1");
+    fail_unless(debug_get_level(0) == SSSDBG_FATAL_FAILURE, "Invalid conversion of 0");
+    fail_unless(debug_get_level(1) == SSSDBG_CRIT_FAILURE, "Invalid conversion of 1");
+    fail_unless(debug_get_level(2) == SSSDBG_OP_FAILURE, "Invalid conversion of 2");
+    fail_unless(debug_get_level(3) == SSSDBG_MINOR_FAILURE, "Invalid conversion of 3");
+    fail_unless(debug_get_level(4) == SSSDBG_CONF_SETTINGS, "Invalid conversion of 4");
+    fail_unless(debug_get_level(5) == SSSDBG_FUNC_DATA, "Invalid conversion of 5");
+    fail_unless(debug_get_level(6) == SSSDBG_TRACE_FUNC, "Invalid conversion of 6");
+    fail_unless(debug_get_level(7) == SSSDBG_TRACE_LIBS, "Invalid conversion of 7");
+    fail_unless(debug_get_level(8) == SSSDBG_TRACE_INTERNAL, "Invalid conversion of 8");
+    fail_unless(debug_get_level(9) == SSSDBG_TRACE_ALL, "Invalid conversion of 9");
+}
+END_TEST
+
+START_TEST(test_debug_get_level_new_format)
+{
+    fail_unless(
+        debug_get_level(SSSDBG_UNRESOLVED) == SSSDBG_UNRESOLVED,
+        "Invalid conversion of SSSDBG_UNRESOLVED"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_FATAL_FAILURE) == SSSDBG_FATAL_FAILURE,
+        "Invalid conversion of SSSDBG_FATAL_FAILURE"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_CRIT_FAILURE) == SSSDBG_CRIT_FAILURE,
+        "Invalid conversion of SSSDBG_CRIT_FAILURE"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_OP_FAILURE) == SSSDBG_OP_FAILURE,
+        "Invalid conversion of SSSDBG_OP_FAILURE"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_MINOR_FAILURE) == SSSDBG_MINOR_FAILURE,
+        "Invalid conversion of SSSDBG_MINOR_FAILURE"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_CONF_SETTINGS) == SSSDBG_CONF_SETTINGS,
+        "Invalid conversion of SSSDBG_CONF_SETTINGS"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_FUNC_DATA) == SSSDBG_FUNC_DATA,
+        "Invalid conversion of SSSDBG_FUNC_DATA"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_TRACE_FUNC) == SSSDBG_TRACE_FUNC,
+        "Invalid conversion of SSSDBG_TRACE_FUNC"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_TRACE_LIBS) == SSSDBG_TRACE_LIBS,
+        "Invalid conversion of SSSDBG_TRACE_LIBS"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_TRACE_INTERNAL) == SSSDBG_TRACE_INTERNAL,
+        "Invalid conversion of SSSDBG_TRACE_INTERNAL"
+    );
+    fail_unless(
+        debug_get_level(SSSDBG_TRACE_ALL) == SSSDBG_TRACE_ALL,
+        "Invalid conversion of SSSDBG_TRACE_ALL"
+    );
+}
+END_TEST
+
+int test_helper_debug_check_message(int level, int msgmode)
+{
+    TALLOC_CTX *ctx = talloc_new(NULL);
+    char filename[24] = {'\0'};
+    char *msg = NULL;
+    char *compare_to = NULL;
+    const char *function = __FUNCTION__;
+    const char *body = "some error\n";
+    int filesize;
+    int fd;
+    int set_fd_result;
+    int ret;
+    FILE *file = NULL;
+
+    strncpy(filename, "sssd_debug_tests.XXXXXX", 24);
+    fd = mkstemp(filename);
+    if (fd == -1) {
+        ret = errno;
+        talloc_free(ctx);
+        return errno;
+    }
+
+    file = fdopen(fd, "r");
+    if (file == NULL) {
+        ret = errno;
+        goto done;
+    }
+
+    set_fd_result = set_debug_file_from_fd(fd);
+    if (set_fd_result != EOK) {
+        ret = set_fd_result;
+        goto done;
+    }
+
+    if (msgmode == 0) {
+        DEBUG(level, (body));
+    } else {
+        DEBUG_MSG(level, __FUNCTION__, "some error");
+    }
+
+    fseek(file, 0, SEEK_END);
+    filesize = ftell(file);
+
+    rewind(file);
+    msg = talloc_array(ctx, char, filesize);
+    if (msg == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+    fread(msg, sizeof(char), filesize, file);
+
+    compare_to = talloc_array(ctx, char, filesize);
+    if (compare_to == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    if (debug_timestamps == 1) {
+        char time_day[4] = {'\0'};
+        char time_month[4] = {'\0'};
+        int time_day_num;
+        int time_hour;
+        int time_min;
+        int time_sec;
+        int time_year;
+        if (sscanf(msg, "(%s %s %d %d:%d:%d %d)", time_day, time_month,
+            &time_day_num, &time_hour, &time_min, &time_sec, &time_year) != 7) {
+            ret = DEBUG_TEST_NOK_TS;
+            goto done;
+        }
+        sprintf(compare_to, "(%s %s %2d %.2d:%.2d:%.2d %.4d) [%s] [%s] (0x%.4x): %s",
+                time_day, time_month, time_day_num, time_hour, time_min, time_sec, time_year,
+                debug_prg_name, function, level, body);
+    } else {
+        sprintf(compare_to, "[%s] [%s] (0x%.4x): %s", debug_prg_name, function, level, body);
+    }
+    ret = strncmp(msg, compare_to, filesize) == 0 ? EOK : DEBUG_TEST_NOK;
+
+done:
+    talloc_free(ctx);
+    fclose(file);
+    remove(filename);
+    return ret;
+}
+
+int test_helper_debug_is_empty_message(int level, int msgmode)
+{
+    char filename[24] = {'\0'};
+    int fd;
+    int filesize;
+    int set_fd_result;
+    int ret;
+    FILE *file;
+
+    strncpy(filename, "sssd_debug_tests.XXXXXX", 24);
+    fd = mkstemp(filename);
+    if (fd == -1)
+        return errno;
+
+    file = fdopen(fd, "r");
+    if (file == NULL) {
+        ret = errno;
+        goto done;
+    }
+
+    set_fd_result = set_debug_file_from_fd(fd);
+    if (set_fd_result != EOK) {
+        ret = set_fd_result;
+        goto done;
+    }
+
+    if (msgmode == 0) {
+        DEBUG(level, ("some error\n"));
+    } else {
+        DEBUG_MSG(level, __FUNCTION__, "some error");
+    }
+    fseek(file, 0, SEEK_END);
+    filesize = ftell(file);
+
+    ret = filesize == 0 ? EOK : DEBUG_TEST_NOK;
+
+done:
+    fclose(file);
+    remove(filename);
+    return ret;
+}
+
+START_TEST(test_debug_is_set_single_no_timestamp)
+{
+    int i;
+    int result;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 0;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = levels[i];
+        result = test_helper_debug_check_message(levels[i], 0);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - message don't match", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_is_set_single_timestamp)
+{
+    int i;
+    int result;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 1;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = levels[i];
+        result = test_helper_debug_check_message(levels[i], 0);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+
+        sprintf(msg, "Test of level 0x%.4x failed - invalid timestamp", levels[i]);
+        fail_if(result == DEBUG_TEST_NOK_TS, msg);
+
+        sprintf(msg, "Test of level 0x%.4x failed - message don't match", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_is_notset_no_timestamp)
+{
+    int i;
+    int result;
+    int all_set = SSSDBG_MASK_ALL;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 0;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = all_set & ~levels[i];
+        result = test_helper_debug_is_empty_message(levels[i], 0);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - message has been written", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_is_notset_timestamp)
+{
+    int i;
+    int result;
+    int all_set = SSSDBG_MASK_ALL;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 0;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = all_set & ~levels[i];
+        result = test_helper_debug_is_empty_message(levels[i], 0);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - message has been written", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_msg_is_set_single_no_timestamp)
+{
+    int i;
+    int result;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 0;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = levels[i];
+        result = test_helper_debug_check_message(levels[i], 1);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - message don't match", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_msg_is_set_single_timestamp)
+{
+    int i;
+    int result;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 1;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = levels[i];
+        result = test_helper_debug_check_message(levels[i], 1);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+
+        sprintf(msg, "Test of level 0x%.4x failed - invalid timestamp", levels[i]);
+        fail_if(result == DEBUG_TEST_NOK_TS, msg);
+
+        sprintf(msg, "Test of level 0x%.4x failed - message don't match", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_msg_is_notset_no_timestamp)
+{
+    int i;
+    int result;
+    int all_set = SSSDBG_MASK_ALL;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 0;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = all_set & ~levels[i];
+        result = test_helper_debug_is_empty_message(levels[i], 1);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - message has been written", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_msg_is_notset_timestamp)
+{
+    int i;
+    int result;
+    int all_set = SSSDBG_MASK_ALL;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+    char *error_msg;
+
+    debug_timestamps = 0;
+    debug_to_file = 1;
+    debug_prg_name = "sssd";
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = all_set & ~levels[i];
+        result = test_helper_debug_is_empty_message(levels[i], 1);
+
+        error_msg = strerror(result);
+        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - message has been written", levels[i]);
+        fail_unless(result == EOK, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_is_set_true)
+{
+    int i;
+    int result;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+
+    debug_level = SSSDBG_MASK_ALL;
+
+    for (i = 0; i <= 9; i++) {
+        result = DEBUG_IS_SET(levels[i]);
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - result is 0x%.4x", levels[i], result);
+        fail_unless(result > 0, msg);
+    }
+}
+END_TEST
+
+START_TEST(test_debug_is_set_false)
+{
+    int i;
+    int result;
+    int all_set = SSSDBG_MASK_ALL;
+    int levels[] = {
+        SSSDBG_FATAL_FAILURE,
+        SSSDBG_CRIT_FAILURE,
+        SSSDBG_OP_FAILURE,
+        SSSDBG_MINOR_FAILURE,
+        SSSDBG_CONF_SETTINGS,
+        SSSDBG_FUNC_DATA,
+        SSSDBG_TRACE_FUNC,
+        SSSDBG_TRACE_LIBS,
+        SSSDBG_TRACE_INTERNAL,
+        SSSDBG_TRACE_ALL
+    };
+
+    for (i = 0; i <= 9; i++) {
+        debug_level = all_set & ~levels[i];
+
+        result = DEBUG_IS_SET(levels[i]);
+        char msg[50] = {'\0'};
+        sprintf(msg, "Test of level 0x%.4x failed - result is 0x%.4x", levels[i], result);
+        fail_unless(result == 0, msg);
+    }
+}
+END_TEST
+
+Suite *debug_suite(void)
+{
+    Suite *s = suite_create("debug");
+
+    TCase *tc_debug = tcase_create("debug");
+
+    tcase_add_test(tc_debug, test_debug_convert_old_level_old_format);
+    tcase_add_test(tc_debug, test_debug_convert_old_level_new_format);
+    tcase_add_test(tc_debug, test_debug_get_level_old_format);
+    tcase_add_test(tc_debug, test_debug_get_level_new_format);
+    tcase_add_test(tc_debug, test_debug_is_set_single_no_timestamp);
+    tcase_add_test(tc_debug, test_debug_is_set_single_timestamp);
+    tcase_add_test(tc_debug, test_debug_is_notset_no_timestamp);
+    tcase_add_test(tc_debug, test_debug_is_notset_timestamp);
+    tcase_add_test(tc_debug, test_debug_msg_is_set_single_no_timestamp);
+    tcase_add_test(tc_debug, test_debug_msg_is_set_single_timestamp);
+    tcase_add_test(tc_debug, test_debug_msg_is_notset_no_timestamp);
+    tcase_add_test(tc_debug, test_debug_msg_is_notset_timestamp);
+    tcase_add_test(tc_debug, test_debug_is_set_true);
+    tcase_add_test(tc_debug, test_debug_is_set_false);
+    tcase_set_timeout(tc_debug, 60);
+
+    suite_add_tcase(s, tc_debug);
+
+    return s;
+}
+
+int main(int argc, const char *argv[])
+{
+    int number_failed;
+
+    Suite *s = debug_suite();
+    SRunner *sr = srunner_create(s);
+
+    srunner_run_all(sr, CK_NORMAL);
+    number_failed = srunner_ntests_failed(sr);
+    srunner_free(sr);
+
+    if (number_failed == 0)
+        return EXIT_SUCCESS;
+
+    return EXIT_FAILURE;
+}
-- 
1.7.6

