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
// Copyright (C) 2016-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

/// @file Contains tests that exercise the basic functionality of the
// class RotatingFile.  These tests are independent of the hooks framework.

#include <config.h>

#include <exceptions/exceptions.h>
#include <cc/data.h>
#include <test_utils.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
#include <legal_log_db_log.h><--- 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/date_time/posix_time/posix_time.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::data;
using namespace isc::db;
using namespace isc::legal_log;

namespace {

/// @brief Test fixture
struct BackendStoreTest : ::testing::Test {
    /// @brief Called before each test
    void SetUp() final override {
        // Clean up from past tests.
        BackendStore::instance().reset();
    }
};

// Verifies output of genDurationString()
TEST_F(BackendStoreTest, genDurationString) {<--- syntax error
    EXPECT_EQ("0 hrs 0 mins 0 secs", BackendStore::genDurationString(0));
    EXPECT_EQ("0 hrs 0 mins 1 secs", BackendStore::genDurationString(1));
    EXPECT_EQ("0 hrs 1 mins 1 secs", BackendStore::genDurationString(61));
    EXPECT_EQ("1 hrs 1 mins 1 secs", BackendStore::genDurationString(3661));
    EXPECT_EQ("1 days 0 hrs 0 mins 0 secs",
              BackendStore::genDurationString(24*60*60));
    EXPECT_EQ("1 days 1 hrs 1 mins 1 secs",
              BackendStore::genDurationString(24*60*60 + 3661));
    EXPECT_EQ("182 days 17 hrs 21 mins 11 secs",
              BackendStore::genDurationString(15783671 + 3600));
    EXPECT_EQ("infinite duration",
              BackendStore::genDurationString(0xFFFFFFFF));
}

// Verifies the LegalLogDbLogger class works as expected
TEST_F(BackendStoreTest, legalLogDbLogger) {
    EXPECT_EQ(1, db_logger_stack.size());

    // Open a block
    {
        EXPECT_EQ(1, db_logger_stack.size());

        // Push local logger
        LegalLogDbLogger pushed;

        // Check now we have a second logger
        EXPECT_EQ(2, db_logger_stack.size());
    }

    // Logger was popped when existing the block
    EXPECT_EQ(1, db_logger_stack.size());

    // Open a try block to check it works with it
    try {
        EXPECT_EQ(1, db_logger_stack.size());
        LegalLogDbLogger pushed;
        EXPECT_EQ(2, db_logger_stack.size());
    } catch (const std::exception&) {
        ADD_FAILURE() << "no exception was raised";
    }
    EXPECT_EQ(1, db_logger_stack.size());

    // Another check with an exception now
    try {
        EXPECT_EQ(1, db_logger_stack.size());
        LegalLogDbLogger pushed;
        EXPECT_EQ(2, db_logger_stack.size());
        isc_throw(Unexpected, "for testing");
        ADD_FAILURE() << "an exception was raised";
    } catch (const std::exception&) {
        // The handler is outside the block
        EXPECT_EQ(1, db_logger_stack.size());
    }
    ASSERT_EQ(1, db_logger_stack.size());
}

// Verifies that vectorDump handles empty content (for static analyzers)
TEST_F(BackendStoreTest, emptyVectorDump) {
    std::vector<uint8_t> bytes;
    EXPECT_TRUE(BackendStore::vectorDump(bytes).empty());
    EXPECT_TRUE(BackendStore::vectorHexDump(bytes).empty());
}

// Verify that parsing extra parameters for rotate file works
TEST_F(BackendStoreTest, parseExtraRotatingFileParameters) {
    ElementPtr params = Element::createMap();
    params->set("path", Element::create("path"));
    params->set("base-name", Element::create("name"));

    params->set("time-unit", Element::create(0));
    EXPECT_THROW(BackendStore::parseFile(params), TypeError);
    EXPECT_FALSE(BackendStore::instance());

    params->set("time-unit", Element::create("nothing"));
    EXPECT_THROW(BackendStore::parseFile(params), BadValue);
    EXPECT_FALSE(BackendStore::instance());

    params->set("time-unit", Element::create("second"));
    params->set("count", Element::create(""));
    EXPECT_THROW(BackendStore::parseFile(params), TypeError);
    EXPECT_FALSE(BackendStore::instance());

    params->set("time-unit", Element::create("day"));
    params->set("count", Element::create(-1));
    EXPECT_THROW(BackendStore::parseFile(params), OutOfRange);
    EXPECT_FALSE(BackendStore::instance());

    params->set("time-unit", Element::create("month"));
    params->set("count", Element::create(static_cast<int64_t>(1) << 32));
    EXPECT_THROW(BackendStore::parseFile(params), OutOfRange);
    EXPECT_FALSE(BackendStore::instance());

    params->set("time-unit", Element::create("year"));
    params->set("count", Element::create(1));
    params->set("prerotate", Element::create(FORENSIC_PREROTATE_TEST_SH));
    params->set("postrotate", Element::create(FORENSIC_POSTROTATE_TEST_SH));
    EXPECT_NO_THROW(BackendStore::parseFile(params));
    EXPECT_TRUE(BackendStore::instance());
}

// Verify that parsing extra parameters works
TEST_F(BackendStoreTest, parseExtraParameters) {
    ASSERT_NO_THROW(BackendStore::instance().reset(new RotatingFile("path", "name")));
    ElementPtr params = Element::createMap();
    params->set("request-parser-format", Element::create("'request'"));
    params->set("response-parser-format", Element::create("'response'"));
    params->set("timestamp-format", Element::create("timestamp"));
    EXPECT_NO_THROW(BackendStore::parseExtraParameters(params));

    auto request_format = BackendStore::instance()->getRequestFormatExpression();
    EXPECT_TRUE(request_format);

    auto response_format = BackendStore::instance()->getResponseFormatExpression();
    EXPECT_TRUE(response_format);

    EXPECT_NE(request_format, response_format);

    auto timestamp_format = BackendStore::instance()->getTimestampFormat();
    EXPECT_EQ(timestamp_format, "timestamp");
}

TEST_F(BackendStoreTest, fileNoParameters) {
    EXPECT_NO_THROW(BackendStore::parseFile(ConstElementPtr()));
    EXPECT_TRUE(BackendStore::instance());
    EXPECT_EQ(BackendStore::getParameters()["path"], LEGAL_LOG_DIR);
}

TEST_F(BackendStoreTest, databaseNoParameters) {
    EXPECT_THROW(BackendStore::parseDatabase(ConstElementPtr()), BadValue);
    EXPECT_FALSE(BackendStore::instance());
}

TEST_F(BackendStoreTest, wrongDatabaseType) {
    ElementPtr parameters = Element::createMap();
    parameters->set("type", Element::create(""));
    EXPECT_THROW_MSG(BackendStore::parseDatabase(parameters), InvalidType,
                     "Database access parameter 'type' does not specify a "
                     "supported database backend: ");
    EXPECT_FALSE(BackendStore::instance());

    parameters->set("type", Element::create("awesomesql"));
    EXPECT_THROW_MSG(BackendStore::parseDatabase(parameters), InvalidType,
                     "Database access parameter 'type' does not specify a "
                     "supported database backend: awesomesql");
    EXPECT_FALSE(BackendStore::instance());
}

} // end of anonymous namespace