Skip to content

Commit 3e38ea9

Browse files
fix: Add more constraints to config (#1831)
Log file size and rotation should also not allowed to be 0.
1 parent 7834b63 commit 3e38ea9

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/util/newconfig/ConfigConstraints.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ static constinit NumberValueConstraint<uint16_t> gValidateUint16{
362362
std::numeric_limits<uint16_t>::min(),
363363
std::numeric_limits<uint16_t>::max()
364364
};
365+
366+
// log file size minimum is 1mb, log rotation time minimum is 1hr
367+
static constinit NumberValueConstraint<uint32_t> gValidateLogSize{1, std::numeric_limits<uint32_t>::max()};
368+
static constinit NumberValueConstraint<uint32_t> gValidateLogRotationTime{1, std::numeric_limits<uint32_t>::max()};
365369
static constinit NumberValueConstraint<uint32_t> gValidateUint32{
366370
std::numeric_limits<uint32_t>::min(),
367371
std::numeric_limits<uint32_t>::max()

src/util/newconfig/ConfigDefinition.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,9 @@ static ClioConfigDefinition gClioConfig = ClioConfigDefinition{
350350
{"server.admin_password", ConfigValue{ConfigType::String}.optional()},
351351
{"server.processing_policy",
352352
ConfigValue{ConfigType::String}.defaultValue("parallel").withConstraint(gValidateProcessingPolicy)},
353-
{"server.parallel_requests_limit", ConfigValue{ConfigType::Integer}.optional()},
354-
{"server.ws_max_sending_queue_size", ConfigValue{ConfigType::Integer}.defaultValue(1500)},
353+
{"server.parallel_requests_limit", ConfigValue{ConfigType::Integer}.optional().withConstraint(gValidateUint16)},
354+
{"server.ws_max_sending_queue_size",
355+
ConfigValue{ConfigType::Integer}.defaultValue(1500).withConstraint(gValidateUint32)},
355356
{"server.__ng_web_server", ConfigValue{ConfigType::Boolean}.defaultValue(false)},
356357

357358
{"prometheus.enabled", ConfigValue{ConfigType::Boolean}.defaultValue(true)},
@@ -387,12 +388,13 @@ static ClioConfigDefinition gClioConfig = ClioConfigDefinition{
387388

388389
{"log_directory", ConfigValue{ConfigType::String}.optional()},
389390

390-
{"log_rotation_size", ConfigValue{ConfigType::Integer}.defaultValue(2048).withConstraint(gValidateUint32)},
391+
{"log_rotation_size", ConfigValue{ConfigType::Integer}.defaultValue(2048).withConstraint(gValidateLogSize)},
391392

392-
{"log_directory_max_size", ConfigValue{ConfigType::Integer}.defaultValue(50 * 1024).withConstraint(gValidateUint32)
393-
},
393+
{"log_directory_max_size",
394+
ConfigValue{ConfigType::Integer}.defaultValue(50 * 1024).withConstraint(gValidateLogSize)},
394395

395-
{"log_rotation_hour_interval", ConfigValue{ConfigType::Integer}.defaultValue(12).withConstraint(gValidateUint32)},
396+
{"log_rotation_hour_interval",
397+
ConfigValue{ConfigType::Integer}.defaultValue(12).withConstraint(gValidateLogRotationTime)},
396398

397399
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("none").withConstraint(gValidateLogTag)},
398400

tests/unit/LoggerTests.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
#include <fmt/core.h>
3232
#include <gtest/gtest.h>
3333

34+
#include <cstddef>
35+
#include <cstdint>
36+
#include <limits>
3437
#include <string>
3538
#include <string_view>
39+
#include <vector>
3640
using namespace util;
3741

3842
// Used as a fixture for tests with enabled logging
@@ -90,11 +94,14 @@ struct LoggerInitTest : LoggerTest {
9094

9195
{"log_directory", ConfigValue{ConfigType::String}.optional()},
9296

93-
{"log_rotation_size", ConfigValue{ConfigType::Integer}.defaultValue(2048)},
97+
{"log_rotation_size",
98+
ConfigValue{ConfigType::Integer}.defaultValue(2048).withConstraint(config::gValidateLogSize)},
9499

95-
{"log_directory_max_size", ConfigValue{ConfigType::Integer}.defaultValue(50 * 1024)},
100+
{"log_directory_max_size",
101+
ConfigValue{ConfigType::Integer}.defaultValue(50 * 1024).withConstraint(config::gValidateLogSize)},
96102

97-
{"log_rotation_hour_interval", ConfigValue{ConfigType::Integer}.defaultValue(12)},
103+
{"log_rotation_hour_interval",
104+
ConfigValue{ConfigType::Integer}.defaultValue(12).withConstraint(config::gValidateLogRotationTime)},
98105

99106
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("none")},
100107
};
@@ -168,6 +175,23 @@ TEST_F(LoggerInitTest, ChannelLogLevel)
168175
}
169176
}
170177

178+
TEST_F(LoggerInitTest, LogSizeAndHourRotationCannotBeZero)
179+
{
180+
std::vector<std::string_view> const keys{
181+
"log_rotation_hour_interval", "log_directory_max_size", "log_rotation_size"
182+
};
183+
184+
auto const parsingErrors =
185+
config_.parse(ConfigFileJson{boost::json::object{{keys[0], 0}, {keys[1], 0}, {keys[2], 0}}});
186+
ASSERT_TRUE(parsingErrors->size() == 3);
187+
for (std::size_t i = 0; i < parsingErrors->size(); ++i) {
188+
EXPECT_EQ(
189+
(*parsingErrors)[i].error,
190+
fmt::format("{} Number must be between 1 and {}", keys[i], std::numeric_limits<uint32_t>::max())
191+
);
192+
}
193+
}
194+
171195
#ifndef COVERAGE_ENABLED
172196
TEST_F(LoggerTest, LOGMacro)
173197
{

0 commit comments

Comments
 (0)