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
// Copyright (C) 2020-2024 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 <cc/data.h>
#include <dhcpsrv/parsers/multi_threading_config_parser.h>
#include <dhcpsrv/cfg_multi_threading.h>
#include <util/multi_threading_mgr.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::data;
using namespace isc::dhcp;
using namespace isc::test;
using namespace isc::util;

namespace {

/// @brief Test fixture class for @c MultiThreadingConfigParser
class MultiThreadingConfigParserTest : public ::testing::Test {
public:

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

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

protected:

    /// @brief Setup for each test.
    virtual void SetUp();

    /// @brief Cleans up after each test.
    virtual void TearDown();
};

void
MultiThreadingConfigParserTest::SetUp() {
    MultiThreadingMgr::instance().setMode(false);
}

void
MultiThreadingConfigParserTest::TearDown() {
    MultiThreadingMgr::instance().setMode(false);
}

// Verifies that MultiThreadingConfigParser handles
// expected valid content
TEST_F(MultiThreadingConfigParserTest, validContent) {<--- syntax error
    struct Scenario {
        std::string description_;
        std::string json_;
    };

    std::vector<Scenario> scenarios = {
        {
        "enable-multi-threading, without thread-pool-size or packet-queue-size",
        "{ \n"
        "   \"enable-multi-threading\": true \n"
        "} \n"
        },
        {
        "enable-multi-threading disabled",
        "{ \n"
        "   \"enable-multi-threading\": false \n"
        "} \n"
        },
        {
        "enable-multi-threading, with thread-pool-size and packet-queue-size",
        "{ \n"
        "   \"enable-multi-threading\": true, \n"
        "   \"thread-pool-size\": 4, \n"
        "   \"packet-queue-size\": 64 \n"
        "} \n"
        }
    };

    // Iterate over the valid scenarios and verify they succeed.
    ConstElementPtr config_elems;
    ConstElementPtr multi_threading_config;
    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        {
            SrvConfig srv_config;
            // Construct the config JSON
            ASSERT_NO_THROW(config_elems = Element::fromJSON(scenario.json_))
                            << "invalid JSON, test is broken";

            // Parsing config should succeed.
            MultiThreadingConfigParser parser;
            try {
                parser.parse(srv_config, config_elems);
            } catch (const std::exception& ex) {
                ADD_FAILURE() << "parser threw an exception: " << ex.what();
            }

            multi_threading_config = srv_config.getDHCPMultiThreading();
            // Verify the resultant configuration.
            ASSERT_TRUE(multi_threading_config);

            bool enabled = false;
            uint32_t thread_count = 0;
            uint32_t queue_size = 0;

            CfgMultiThreading::extract(multi_threading_config, enabled,
                                       thread_count, queue_size);

            EXPECT_TRUE(multi_threading_config->equals(*config_elems));
        }
    }
}

// Verifies that MultiThreadingConfigParser correctly catches
// invalid content
TEST_F(MultiThreadingConfigParserTest, invalidContent) {
    struct Scenario {
        std::string description_;
        std::string json_;
    };

    std::vector<Scenario> scenarios = {
        {
        "enable-multi-threading not boolean",
        "{ \n"
        "   \"enable-multi-threading\": \"always\" \n"
        "} \n"
        },
        {
        "thread-pool-size not integer",
        "{ \n"
        "   \"thread-pool-size\": true \n"
        "} \n"
        },
        {
        "thread-pool-size negative",
        "{ \n"
        "   \"thread-pool-size\": -1 \n"
        "} \n"
        },
        {
        "thread-pool-size too large",
        "{ \n"
        "   \"thread-pool-size\": 200000 \n"
        "} \n"
        },
        {
        "packet-queue-size not integer",
        "{ \n"
        "   \"packet-queue-size\": true \n"
        "} \n"
        },
        {
        "packet-queue-size-size negative",
        "{ \n"
        "   \"packet-queue-size\": -1 \n"
        "} \n"
        },
        {
        "packet-queue-size too large",
        "{ \n"
        "   \"packet-queue-size\": 200000 \n"
        "} \n"
        }
    };

    // Iterate over the valid scenarios and verify they succeed.
    ConstElementPtr config_elems;
    ConstElementPtr queue_control;
    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        {
            SrvConfig srv_config;
            // Construct the config JSON
            ASSERT_NO_THROW(config_elems = Element::fromJSON(scenario.json_))
                            << "invalid JSON, test is broken";

            // Parsing config into a queue control should succeed.
            MultiThreadingConfigParser parser;
            EXPECT_THROW(parser.parse(srv_config, config_elems), DhcpConfigError);
        }
    }
}

}  // namespace