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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (C) 2014-2023 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 <process/daemon.h>
#include <process/logging_info.h>
#include <testutils/test_to_element.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::process;
using namespace isc::test;
using namespace isc::data;

namespace {

// Checks if two destinations can be compared for equality.
TEST(LoggingDestination, equals) {
    LoggingDestination dest1;
    LoggingDestination dest2;

    EXPECT_TRUE(dest1.equals(dest2));

    dest1.output_ = "stderr";
    EXPECT_FALSE(dest1.equals(dest2));

    dest2.output_ = "stdout";
    EXPECT_FALSE(dest1.equals(dest2));

    dest2.output_ = "stderr";
    EXPECT_TRUE(dest1.equals(dest2));

    dest1.maxver_ = 10;
    dest2.maxver_ = 5;
    EXPECT_FALSE(dest1.equals(dest2));

    dest2.maxver_ = 10;
    EXPECT_TRUE(dest1.equals(dest2));

    dest1.maxsize_ = 64;
    dest2.maxsize_ = 32;
    EXPECT_FALSE(dest1.equals(dest2));

    dest1.maxsize_ = 32;
    EXPECT_TRUE(dest1.equals(dest2));
}

/// @brief Test fixture class for testing @c LoggingInfo.
class LoggingInfoTest : public ::testing::Test {
public:

    /// @brief Constructor
    LoggingInfoTest() = default;

    /// @brief Destructor
    virtual ~LoggingInfoTest() = default;

    /// @brief Setup the test.
    virtual void SetUp() {
        Daemon::setVerbose(false);
    }

    /// @brief Clear after the test.
    virtual void TearDown() {
        Daemon::setVerbose(false);
    }
};

// Checks if default logging configuration is correct.
TEST_F(LoggingInfoTest, defaults) {<--- syntax error

    // We now need to set the default logger explicitly.
    // Otherwise leftovers from previous tests that use DController
    // would leave the default logger set to TestBin.
    Daemon::setDefaultLoggerName("kea");

    LoggingInfo info_non_verbose;

    // The DStubTest framework sets up the default binary name to TestBin
    EXPECT_EQ("kea", info_non_verbose.name_);
    EXPECT_EQ(isc::log::INFO, info_non_verbose.severity_);
    EXPECT_EQ(0, info_non_verbose.debuglevel_);

    ASSERT_EQ(1, info_non_verbose.destinations_.size());
    EXPECT_EQ("stdout", info_non_verbose.destinations_[0].output_);

    std::string header = "{\n";
    std::string begin =
        "\"name\": \"kea\",\n"
        "\"output-options\": [ {\n"
        " \"output\": \"stdout\", \"flush\": true, \"pattern\": \"\" } ],\n"
        "\"severity\": \"";
    std::string dbglvl = "\",\n\"debuglevel\": ";
    std::string trailer = "\n}\n";
    std::string expected = header + begin + "INFO" + dbglvl + "0" + trailer;
    runToElementTest<LoggingInfo>(expected, info_non_verbose);

    // Add a user context
    std::string comment = "\"comment\": \"foo\"";
    std::string user_context = "{ " + comment + " }";
    std::string user_context_nl = "{\n" + comment + "\n}";
    EXPECT_FALSE(info_non_verbose.getContext());
    info_non_verbose.setContext(Element::fromJSON(user_context));
    ASSERT_TRUE(info_non_verbose.getContext());
    EXPECT_EQ(user_context, info_non_verbose.getContext()->str());
    expected = header;
    expected += "\"user-context\": " + user_context_nl + ",\n";
    expected += begin + "INFO" + dbglvl + "0" + trailer;
    runToElementTest<LoggingInfo>(expected, info_non_verbose);

    Daemon::setVerbose(true);
    LoggingInfo info_verbose;
    EXPECT_EQ("kea", info_verbose.name_);
    EXPECT_EQ(isc::log::DEBUG, info_verbose.severity_);
    EXPECT_EQ(99, info_verbose.debuglevel_);

    ASSERT_EQ(1, info_verbose.destinations_.size());
    EXPECT_EQ("stdout", info_verbose.destinations_[0].output_);

    EXPECT_EQ(10240000, info_verbose.destinations_[0].maxsize_);
    EXPECT_EQ(1, info_verbose.destinations_[0].maxver_);

    expected = header + begin + "DEBUG" + dbglvl + "99" + trailer;
    runToElementTest<LoggingInfo>(expected, info_verbose);

    // User comment again
    EXPECT_FALSE(info_verbose.getContext());
    info_verbose.setContext(Element::fromJSON(user_context));
    ASSERT_TRUE(info_verbose.getContext());
    EXPECT_EQ(user_context, info_verbose.getContext()->str());
    expected = header;
    expected += "\"user-context\": " + user_context_nl + ",\n";
    expected += begin + "DEBUG" + dbglvl + "99" + trailer;
    runToElementTest<LoggingInfo>(expected, info_verbose);
}

// Checks if (in)equality operators work for LoggingInfo.
TEST_F(LoggingInfoTest, equalityOperators) {
    LoggingInfo info1;
    LoggingInfo info2;

    // Initially, both objects are the same.
    EXPECT_TRUE(info1 == info2);

    // Differ by name.
    info1.name_ = "foo";
    info2.name_ = "bar";
    EXPECT_FALSE(info1 == info2);
    EXPECT_TRUE(info1 != info2);

    // Names equal.
    info2.name_ = "foo";
    EXPECT_TRUE(info1 == info2);
    EXPECT_FALSE(info1 != info2);

    // Differ by severity.
    info1.severity_ = isc::log::DEBUG;
    info2.severity_ = isc::log::INFO;
    EXPECT_FALSE(info1 == info2);
    EXPECT_TRUE(info1 != info2);

    // Severities equal.
    info2.severity_ = isc::log::DEBUG;
    EXPECT_TRUE(info1 == info2);
    EXPECT_FALSE(info1 != info2);

    // Differ by debug level.
    info1.debuglevel_ = 99;
    info2.debuglevel_ = 1;
    EXPECT_FALSE(info1 == info2);
    EXPECT_TRUE(info1 != info2);

    // Debug level equal.
    info2.debuglevel_ = 99;
    EXPECT_TRUE(info1 == info2);
    EXPECT_FALSE(info1 != info2);

    // Create two different destinations.
    LoggingDestination dest1;
    LoggingDestination dest2;
    dest1.output_ = "foo";
    dest2.output_ = "bar";

    // Push destinations in some order to info1.
    info1.destinations_.push_back(dest1);
    info1.destinations_.push_back(dest2);

    // Push in reverse order to info2. Order shouldn't matter.
    info2.destinations_.push_back(dest2);
    info2.destinations_.push_back(dest1);

    EXPECT_TRUE(info1 == info2);
    EXPECT_FALSE(info1 != info2);

    // Change one of the destinations.
    LoggingDestination dest3;
    dest3.output_ = "foobar";

    info2.destinations_[2] = dest3;

    // The should now be unequal.
    EXPECT_FALSE(info1 == info2);
    EXPECT_TRUE(info1 != info2);
}

} // end of anonymous namespace