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 2, 2024
1 parent 9e4c51f commit d7e8038
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 23 deletions.
118 changes: 118 additions & 0 deletions FlashMQTests/tst_maintests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ See LICENSE for license details.
*/

#include "tst_maintests.h"
#include "configfileparser.h"
#include "exceptions.h"
#include "settings.h"

#include <list>
#include <unordered_map>
Expand Down Expand Up @@ -505,6 +508,121 @@ void MainTests::test_loading_acl_file()
QVERIFY(true);
}

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 = new ConfigFileParser(config.getFilePath());
parser->loadFile(false);

Settings settings = parser->getSettings();

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

QCOMPARE(bridge->publishes[0].topic, "send/this");
QCOMPARE(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 = new ConfigFileParser(config.getFilePath());
try
{
parser->loadFile(false);
QFAIL("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 = new ConfigFileParser(config.getFilePath());
parser->loadFile(false);

Settings settings = parser->getSettings();

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

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

ConfigFileParser *parser = new ConfigFileParser(config.getFilePath());
try
{
parser->loadFile(false);
QFAIL("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 = new ConfigFileParser(config.getFilePath());
try
{
parser->loadFile(false);
QFAIL("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 = new ConfigFileParser(config.getFilePath());
try
{
parser->loadFile(false);
QFAIL("The parser was too liberal");
}
catch (ConfigFileException)
{
/* Good! This is where we want to end up in */
}
}
}

#ifndef FMQ_NO_SSE
void MainTests::test_sse_split()
{
Expand Down
2 changes: 2 additions & 0 deletions FlashMQTests/tst_maintests.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ private slots:
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
void test_sse_split();
Expand Down
Loading

0 comments on commit d7e8038

Please sign in to comment.