1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/static_assert.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/lexical_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <log/logger_level_impl.h>
#include <log/logger_support.h>
#include <log4cplus/logger.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::log;
using namespace std;

class LoggerLevelImplTest : public ::testing::Test {
protected:
    LoggerLevelImplTest() {
        // Ensure logging set to default for unit tests
        setDefaultLoggingOutput();
    }

    ~LoggerLevelImplTest()
    {}
};


// Checks that the log4cplus and Kea levels convert correctly
TEST_F(LoggerLevelImplTest, DefaultConversionFromBind) {
    log4cplus::LogLevel fatal =
        LoggerLevelImpl::convertFromBindLevel(Level(FATAL));
    EXPECT_EQ(log4cplus::FATAL_LOG_LEVEL, fatal);

    log4cplus::LogLevel error =
        LoggerLevelImpl::convertFromBindLevel(Level(ERROR));
    EXPECT_EQ(log4cplus::ERROR_LOG_LEVEL, error);

    log4cplus::LogLevel warn =
        LoggerLevelImpl::convertFromBindLevel(Level(WARN));
    EXPECT_EQ(log4cplus::WARN_LOG_LEVEL, warn);

    log4cplus::LogLevel info =
        LoggerLevelImpl::convertFromBindLevel(Level(INFO));
    EXPECT_EQ(log4cplus::INFO_LOG_LEVEL, info);

    log4cplus::LogLevel debug =
        LoggerLevelImpl::convertFromBindLevel(Level(DEBUG));
    EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL, debug);
}

// Checks that the debug severity and level converts correctly
TEST_F(LoggerLevelImplTest, DebugConversionFromBind) {
    log4cplus::LogLevel debug0 =
        LoggerLevelImpl::convertFromBindLevel(Level(DEBUG, 0));
    EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL - 0, debug0);

    log4cplus::LogLevel debug1 =
        LoggerLevelImpl::convertFromBindLevel(Level(DEBUG, 1));
    EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL - 1, debug1);

    log4cplus::LogLevel debug99 =
        LoggerLevelImpl::convertFromBindLevel(Level(DEBUG, 99));
    EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL - 99, debug99);

    // Out of range should be coerced to the nearest boundary
    log4cplus::LogLevel debug_1 =
        LoggerLevelImpl::convertFromBindLevel(Level(DEBUG, MIN_DEBUG_LEVEL - 1));
    EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL, debug_1);

    log4cplus::LogLevel debug100 =
        LoggerLevelImpl::convertFromBindLevel(Level(DEBUG, MAX_DEBUG_LEVEL + 1));
    EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL - MAX_DEBUG_LEVEL, debug100);
}

// Do the checks the other way
static void
test_convert_to(const char* trace, isc::log::Severity severity, int dbglevel,
                log4cplus::LogLevel level)
{
    SCOPED_TRACE(trace);
    Level test = LoggerLevelImpl::convertToBindLevel(level);
    EXPECT_EQ(severity, test.severity);
    EXPECT_EQ(dbglevel, test.dbglevel);
}

TEST_F(LoggerLevelImplTest, ConversionToBind) {
    test_convert_to("FATAL", FATAL, MIN_DEBUG_LEVEL, log4cplus::FATAL_LOG_LEVEL);
    test_convert_to("ERROR", ERROR, MIN_DEBUG_LEVEL, log4cplus::ERROR_LOG_LEVEL);
    test_convert_to("WARN",  WARN , MIN_DEBUG_LEVEL, log4cplus::WARN_LOG_LEVEL);
    test_convert_to("INFO",  INFO , MIN_DEBUG_LEVEL, log4cplus::INFO_LOG_LEVEL);
    test_convert_to("DEBUG",  DEBUG, MIN_DEBUG_LEVEL, log4cplus::DEBUG_LOG_LEVEL);

    test_convert_to("DEBUG0",  DEBUG, MIN_DEBUG_LEVEL + 0,
            (log4cplus::DEBUG_LOG_LEVEL));
    test_convert_to("DEBUG1",  DEBUG, MIN_DEBUG_LEVEL + 1,
            (log4cplus::DEBUG_LOG_LEVEL - 1));
    test_convert_to("DEBUG2",  DEBUG, MIN_DEBUG_LEVEL + 2,
            (log4cplus::DEBUG_LOG_LEVEL - 2));
    test_convert_to("DEBUG99",  DEBUG, MIN_DEBUG_LEVEL + 99,
            (log4cplus::DEBUG_LOG_LEVEL - 99));

    // ... and some invalid valid values
    test_convert_to("DEBUG-1",  INFO, MIN_DEBUG_LEVEL,
            (log4cplus::DEBUG_LOG_LEVEL + 1));
    BOOST_STATIC_ASSERT(MAX_DEBUG_LEVEL == 99);
    test_convert_to("DEBUG+100",  DEFAULT, 0,
            (log4cplus::DEBUG_LOG_LEVEL - MAX_DEBUG_LEVEL - 1));
}

// Check that we can convert from a string to the new log4cplus levels
TEST_F(LoggerLevelImplTest, FromString) {

    // Test all valid values
    for (int i = MIN_DEBUG_LEVEL; i <= MAX_DEBUG_LEVEL; ++i) {
        std::string token = string("DEBUG") + boost::lexical_cast<std::string>(i);
        EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL - i,
                  LoggerLevelImpl::logLevelFromString(token));
    }

    // ... in lowercase too
    for (int i = MIN_DEBUG_LEVEL; i <= MAX_DEBUG_LEVEL; ++i) {
        std::string token = string("debug") + boost::lexical_cast<std::string>(i);
        EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL - i,
                  LoggerLevelImpl::logLevelFromString(token));
    }

    // A few below the minimum
    for (int i = MIN_DEBUG_LEVEL - 5; i < MIN_DEBUG_LEVEL; ++i) {
        std::string token = string("DEBUG") + boost::lexical_cast<std::string>(i);
        EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL, LoggerLevelImpl::logLevelFromString(token));
    }

    // ... and above the maximum
    for (int i = MAX_DEBUG_LEVEL + 1; i < MAX_DEBUG_LEVEL + 5; ++i) {
        std::string token = string("DEBUG") + boost::lexical_cast<std::string>(i);
        EXPECT_EQ(log4cplus::DEBUG_LOG_LEVEL - MAX_DEBUG_LEVEL,
                  LoggerLevelImpl::logLevelFromString(token));
    }

    // Invalid strings.
    EXPECT_EQ(log4cplus::NOT_SET_LOG_LEVEL,
              LoggerLevelImpl::logLevelFromString("DEBU"));
    EXPECT_EQ(log4cplus::NOT_SET_LOG_LEVEL,
              LoggerLevelImpl::logLevelFromString("unrecognized"));
}

// ... and check the conversion back again.  All levels should convert to "DEBUG".
TEST_F(LoggerLevelImplTest, ToString) {

    for (int i = MIN_DEBUG_LEVEL; i <= MAX_DEBUG_LEVEL; ++i) {
        EXPECT_EQ(std::string("DEBUG"),
                  LoggerLevelImpl::logLevelToString(log4cplus::DEBUG_LOG_LEVEL - i));
    }

    // ... and that out of range stuff returns an empty string.
    EXPECT_EQ(std::string(),
              LoggerLevelImpl::logLevelToString(log4cplus::DEBUG_LOG_LEVEL + 1));
    EXPECT_EQ(std::string(),
              LoggerLevelImpl::logLevelToString(
                               log4cplus::DEBUG_LOG_LEVEL - MAX_DEBUG_LEVEL - 100));
}