Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed number of slash validation for MQTT topics #438

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions source/util/MqttUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ bool MqttUtils::ValidateAwsIotMqttTopicName(std::string topic)
topic = reserved_topic.suffix();
}

const std::size_t count_slashes = std::count(topic.cbegin(), topic.cend(), '/');
if (count_slashes > MAX_NUMBER_OF_FORWARD_SLASHES)
{
LOGM_ERROR(
TAG,
"Number of forward slashes in topic (%lu) exceeds maximum (%lu)",
count_slashes,
MAX_NUMBER_OF_FORWARD_SLASHES);
return false;
}

// Since std::string is based on char, the size of the string and number of UTF8 chars is same.
if (topic.size() > MAX_LENGTH_OF_TOPIC)
{
Expand Down
5 changes: 0 additions & 5 deletions source/util/MqttUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@ namespace Aws
//
// https://docs.aws.amazon.com/general/latest/gr/iot-core.html#message-broker-limits
//
// A topic in a publish or subscribe request can have no more than 7 forward slashes (/).
// This excludes the first 3 slashes in the mandatory segments for
// Basic Ingest topics ($AWS/rules/rule-name/).
//
// The topic passed to AWS IoT Core when sending a publish request can be no larger
// than 256 bytes of UTF-8 encoded characters. This excludes the first 3 mandatory
// segments for Basic Ingest topics ($AWS/rules/rule-name/).
//
static constexpr std::size_t MAX_NUMBER_OF_FORWARD_SLASHES{7};
static constexpr std::size_t MAX_LENGTH_OF_TOPIC{256};

/**
Expand Down
35 changes: 0 additions & 35 deletions test/config/TestConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,41 +1310,6 @@ TEST_F(ConfigTestFixture, SensorPublishInvalidConfigMqttTopicEmpty)
ASSERT_FALSE(settings.enabled);
}

TEST_F(ConfigTestFixture, SensorPublishInvalidConfigMqttTopic)
{
constexpr char jsonString[] = R"(
{
"endpoint": "endpoint value",
"cert": "/tmp/aws-iot-device-client-test-file",
"root-ca": "/tmp/aws-iot-device-client-test/AmazonRootCA1.pem",
"key": "/tmp/aws-iot-device-client-test-file",
"thing-name": "thing-name value",
"sensor-publish": {
"sensors": [
{
"addr": "/tmp/sensors/my-sensor-server",
"eom_delimiter": "[\r\n]+",
"mqtt_topic": "////////my-sensor-data"
}
]
}
})";
JsonObject jsonObject(jsonString);
JsonView jsonView = jsonObject.View();

PlainConfig config;
config.LoadFromJson(jsonView);

#if defined(EXCLUDE_SENSOR_PUBLISH)
GTEST_SKIP();
#endif
ASSERT_FALSE(config.Validate()); // Invalid mqtt_topic.
ASSERT_TRUE(config.sensorPublish.enabled);
ASSERT_EQ(config.sensorPublish.settings.size(), 1);
const auto &settings = config.sensorPublish.settings[0];
ASSERT_FALSE(settings.enabled);
}

TEST_F(ConfigTestFixture, SensorPublishInvalidConfigEomDelimiter)
{
constexpr char jsonString[] = R"(
Expand Down
15 changes: 0 additions & 15 deletions test/util/TestMqttUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ TEST(MqttUtils, ReservedTopicValid)
ASSERT_TRUE(MqttUtils::ValidateAwsIotMqttTopicName(topic));
}

TEST(MqttUtils, TopicNotValidExceedsMaxSlashes)
{
std::string topic(MqttUtils::MAX_NUMBER_OF_FORWARD_SLASHES + 1, '/');

ASSERT_FALSE(MqttUtils::ValidateAwsIotMqttTopicName(topic));
}

TEST(MqttUtils, TopicValidExceedsMaxSlashesWithReservedTopic)
{
std::string topic(RESERVED_TOPIC);
std::fill_n(std::back_inserter(topic), MqttUtils::MAX_NUMBER_OF_FORWARD_SLASHES, '/');

ASSERT_TRUE(MqttUtils::ValidateAwsIotMqttTopicName(topic));
}

TEST(MqttUtils, TopicNotValidExceedsMaxLength)
{
std::string topic(MqttUtils::MAX_LENGTH_OF_TOPIC + 1, 'A');
Expand Down