diff --git a/lib/NamedEntity.cc b/lib/NamedEntity.cc index ad7c385c..484c8b1f 100644 --- a/lib/NamedEntity.cc +++ b/lib/NamedEntity.cc @@ -18,19 +18,29 @@ */ #include "NamedEntity.h" +#include + +/** + * Allowed characters for property, namespace, cluster and topic names are + * alphanumeric (a-zA-Z_0-9) and these special chars -=:. + * @param name + * @return + */ bool NamedEntity::checkName(const std::string& name) { for (char c : name) { + if (isalnum(c)) { + continue; + } + switch (c) { + case '-': case '=': case ':': - case ' ': - case '!': - case '\t': - case '\r': - case '\n': - return false; + case '.': + continue; default: - break; + // Invalid character was found + return false; } } diff --git a/tests/NamespaceNameTest.cc b/tests/NamespaceNameTest.cc index 85a57494..51975c82 100644 --- a/tests/NamespaceNameTest.cc +++ b/tests/NamespaceNameTest.cc @@ -42,3 +42,11 @@ TEST(NamespaceNameTest, testNamespaceNameV2) { std::shared_ptr nn2 = NamespaceName::get("property", "namespace"); ASSERT_TRUE(*nn1 == *nn2); } + +TEST(NamespaceNameTest, testNamespaceNameLegalCharacters) { + std::shared_ptr nn1 = NamespaceName::get("cluster-1:=.", "namespace-1:=."); + ASSERT_EQ("cluster-1:=.", nn1->getProperty()); + ASSERT_TRUE(nn1->getCluster().empty()); + ASSERT_EQ("namespace-1:=.", nn1->getLocalName()); + ASSERT_TRUE(nn1->isV2()); +} diff --git a/tests/TopicNameTest.cc b/tests/TopicNameTest.cc index 377a9319..3db2a6c0 100644 --- a/tests/TopicNameTest.cc +++ b/tests/TopicNameTest.cc @@ -140,6 +140,15 @@ TEST(TopicNameTest, testIllegalCharacters) { ASSERT_FALSE(topicName); } +TEST(TopicNameTest, testLegalNonAlphaCharacters) { + std::shared_ptr topicName = TopicName::get("persistent://cluster-1:=./namespace-1:=./topic"); + ASSERT_TRUE(topicName); + ASSERT_EQ("cluster-1:=.", topicName->getProperty()); + ASSERT_EQ("namespace-1:=.", topicName->getNamespacePortion()); + ASSERT_EQ("persistent", topicName->getDomain()); + ASSERT_EQ("topic", topicName->getLocalName()); +} + TEST(TopicNameTest, testIllegalUrl) { std::shared_ptr topicName = TopicName::get("persistent:::/property/cluster/namespace/topic"); ASSERT_FALSE(topicName);