Skip to content

Commit

Permalink
Tighten config parser
Browse files Browse the repository at this point in the history
The config parser was rather liberal in what it accepted. For example
these three settings would all be accepted and interpreted as 180
seconds:

  expire_sessions_after_seconds 180s
  expire_sessions_after_seconds 180h
  expire_sessions_after_seconds 180 hours

This changeset will cause these things to get rejected.
  • Loading branch information
quinox committed Mar 7, 2024
1 parent 707810c commit 287f0c6
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 23 deletions.
1 change: 1 addition & 0 deletions FlashMQTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ add_executable(flashmq-tests
conffiletemp.cpp conffiletemp.h
filecloser.cpp filecloser.h
plugintests.cpp
configtests.cpp
sharedsubscriptionstests.cpp
websockettests.cpp
willtests.cpp
Expand Down
119 changes: 119 additions & 0 deletions FlashMQTests/configtests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "maintests.h"
#include "testhelpers.h"
#include "conffiletemp.h"
#include "exceptions.h"

void MainTests::test_loading_second_value()
{
/* this is expected to work*/
{
ConfFileTemp config;
config.writeLine("bridge {");
config.writeLine(" address localhost");
config.writeLine(" publish send/this 1"); // this value should be different from the default (0)
config.writeLine("}");
config.closeFile();

ConfigFileParser parser(config.getFilePath());
parser.loadFile(false);

Settings settings = parser.getSettings();

std::shared_ptr<BridgeConfig> bridge = settings.stealBridges().front();

FMQ_COMPARE(bridge->publishes[0].topic, "send/this");
FMQ_COMPARE(bridge->publishes[0].qos, (uint8_t)1);
}

/* this is expecte to fail because "address" doesn't take a second value */
{
ConfFileTemp config;
config.writeLine("bridge {");
config.writeLine(" address localhost thisisnotok");
config.writeLine(" publish send/this 1");
config.writeLine("}");
config.closeFile();

ConfigFileParser parser(config.getFilePath());
try
{
parser.loadFile(false);
FMQ_FAIL("The config parser is too liberal");
}
catch (ConfigFileException &ex)
{
/* Excellent, what we wanted */
}
}
}

void MainTests::test_parsing_numbers()
{
/* this should work: 180 */
{
ConfFileTemp config;
config.writeLine("expire_sessions_after_seconds 180");
config.closeFile();

ConfigFileParser parser(config.getFilePath());
parser.loadFile(false);

Settings settings = parser.getSettings();

FMQ_COMPARE(settings.expireSessionsAfterSeconds, (uint32_t)180);
}

/* this should fail: 180days */
{
ConfFileTemp config;
config.writeLine("expire_sessions_after_seconds 180days");
config.closeFile();

ConfigFileParser parser(config.getFilePath());
try
{
parser.loadFile(false);
FMQ_FAIL("The parser was too liberal");
}
catch (ConfigFileException&)
{
/* Good! This is where we want to end up in */
}
}

/* this should also fail: 180 days */
{
ConfFileTemp config;
config.writeLine("expire_sessions_after_seconds 180 days");
config.closeFile();

ConfigFileParser parser(config.getFilePath());
try
{
parser.loadFile(false);
FMQ_FAIL("The parser was too liberal");
}
catch (ConfigFileException&)
{
/* Good! This is where we want to end up in */
}
}

/* Last one that should fail: 180 days and a bit */
{
ConfFileTemp config;
config.writeLine("expire_sessions_after_seconds 180 days and a bit more");
config.closeFile();

ConfigFileParser parser(config.getFilePath());
try
{
parser.loadFile(false);
FMQ_FAIL("The parser was too liberal");
}
catch (ConfigFileException&)
{
/* Good! This is where we want to end up in */
}
}
}
2 changes: 2 additions & 0 deletions FlashMQTests/maintests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ MainTests::MainTests()
REGISTER_FUNCTION(test_acl_patterns_username);
REGISTER_FUNCTION(test_acl_patterns_clientid);
REGISTER_FUNCTION(test_loading_acl_file);
REGISTER_FUNCTION(test_loading_second_value);
REGISTER_FUNCTION(test_parsing_numbers);
REGISTER_FUNCTION(test_validUtf8Generic);

#ifndef FMQ_NO_SSE
Expand Down
2 changes: 2 additions & 0 deletions FlashMQTests/maintests.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class MainTests
void test_acl_patterns_clientid();
void test_loading_acl_file();

void test_loading_second_value();
void test_parsing_numbers();
void test_validUtf8Generic();

#ifndef FMQ_NO_SSE
Expand Down
Loading

0 comments on commit 287f0c6

Please sign in to comment.