Skip to content

Commit

Permalink
WIP: Config suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
quinox committed Mar 7, 2024
1 parent 707810c commit 5b84e33
Show file tree
Hide file tree
Showing 10 changed files with 1,854 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ add_executable(flashmq
globber.cpp
x509manager.h x509manager.cpp
backgroundworker.h backgroundworker.cpp

edlib/src/edlib.cpp
)

target_include_directories(flashmq PRIVATE edlib/include)

target_link_libraries(flashmq pthread dl ssl crypto resolv anl)

execute_process(COMMAND ../.get-os-codename-and-stamp.sh OUTPUT_VARIABLE OS_CODENAME)
Expand Down
3 changes: 3 additions & 0 deletions FlashMQTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ add_executable(flashmq-tests
dnstests.cpp
testinitializer.h testinitializer.cpp

../edlib/src/edlib.cpp
)

target_include_directories(flashmq-tests PRIVATE ../edlib/include)

target_include_directories(flashmq-tests PUBLIC ..)

target_link_libraries(flashmq-tests pthread dl ssl crypto resolv anl)
Expand Down
1 change: 1 addition & 0 deletions FlashMQTests/maintests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ MainTests::MainTests()
REGISTER_FUNCTION(testOverrideWillDelayOnSessionDestructionByTakeOver);
REGISTER_FUNCTION(testDisabledWills);
REGISTER_FUNCTION(testMqtt5DelayedWillsDisabled);
REGISTER_FUNCTION(test_config_suggestion);
REGISTER_FUNCTION(testIncomingTopicAlias);
REGISTER_FUNCTION(testOutgoingTopicAlias);
REGISTER_FUNCTION(testOutgoingTopicAliasStoredPublishes);
Expand Down
1 change: 1 addition & 0 deletions FlashMQTests/maintests.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class MainTests
void testOverrideWillDelayOnSessionDestructionByTakeOver();
void testDisabledWills();
void testMqtt5DelayedWillsDisabled();
void test_config_suggestion();


void testIncomingTopicAlias();
Expand Down
42 changes: 42 additions & 0 deletions FlashMQTests/willtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "testhelpers.h"
#include "flashmqtestclient.h"
#include "conffiletemp.h"
#include "exceptions.h"

void MainTests::testMqtt3will()
{
Expand Down Expand Up @@ -323,3 +324,44 @@ void MainTests::testMqtt5DelayedWillsDisabled()
usleep(250000);
QVERIFY(receiver.receivedPackets.empty());
}


void MainTests::test_config_suggestion()
{
// User made a small typo
{
ConfFileTemp config;
config.writeLine("expire_session_after_seconds 180");
config.closeFile();

ConfigFileParser parser(config.getFilePath());

try {
parser.loadFile(false);
FMQ_FAIL("The parser is too liberal");
} catch (ConfigFileException ex)
{
/* Good! This is where we want to end up in */
FMQ_COMPARE(ex.what(), "Config key 'expire_session_after_seconds' is not valid (here). Did you mean: expire_sessions_after_seconds ?");
}
}

// User entered gibberish. Let's not suggest gibberish back
{
ConfFileTemp config;
config.writeLine("foobarbaz 180");
config.closeFile();

ConfigFileParser parser(config.getFilePath());

try {
parser.loadFile(false);
FMQ_FAIL("The parser is too liberal");
} catch (ConfigFileException ex)
{
FMQ_COMPARE(ex.what(), "Config key 'foobarbaz' is not valid (here).");
}
}

}

24 changes: 24 additions & 0 deletions configfileparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ See LICENSE for license details.
#include "exceptions.h"
#include "utils.h"
#include "globber.h"
#include "edlib.h"


/**
Expand All @@ -41,6 +42,29 @@ bool ConfigFileParser::testKeyValidity(const std::string &key, const std::string
{
std::ostringstream oss;
oss << "Config key '" << key << "' is not valid (here).";

// Maybe the user made a typo
EdlibAlignConfig cmpcnf = edlibDefaultAlignConfig();
const std::string *alternative = nullptr;
int alternative_distance = INT_MAX;

for (const std::string& possible_key : validKeys)
{
EdlibAlignResult result = edlibAlign(key.c_str(), key.length(), possible_key.c_str(), key.length(), cmpcnf);
if (result.status == EDLIB_STATUS_OK) {
// The mathemathical formula "x/y < 0.5"
// can be calculated with integers as "x*2/y < 1"
if ((result.editDistance * 2) / key.length() < 1 && result.editDistance < alternative_distance) {
alternative = &possible_key;
alternative_distance = result.editDistance;
}
}
edlibFreeAlignResult(result);
}
if (alternative != nullptr) {
oss << " Did you mean: " << alternative->c_str() << " ?";
}

throw ConfigFileException(oss.str());
}

Expand Down
20 changes: 20 additions & 0 deletions edlib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Martin Šošić

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions edlib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Local copy of https://github.com/Martinsos/edlib
Loading

0 comments on commit 5b84e33

Please sign in to comment.