Skip to content

Commit c1a9808

Browse files
authored
Fixed validation of cluster/namespace names containing legal non-alpha characters (#52)
* Fixed validation of cluster/namespace names containing legal non-alpha characters * Fixed formatting
1 parent ba1a7e1 commit c1a9808

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

lib/NamedEntity.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,29 @@
1818
*/
1919
#include "NamedEntity.h"
2020

21+
#include <cctype>
22+
23+
/**
24+
* Allowed characters for property, namespace, cluster and topic names are
25+
* alphanumeric (a-zA-Z_0-9) and these special chars -=:.
26+
* @param name
27+
* @return
28+
*/
2129
bool NamedEntity::checkName(const std::string& name) {
2230
for (char c : name) {
31+
if (isalnum(c)) {
32+
continue;
33+
}
34+
2335
switch (c) {
36+
case '-':
2437
case '=':
2538
case ':':
26-
case ' ':
27-
case '!':
28-
case '\t':
29-
case '\r':
30-
case '\n':
31-
return false;
39+
case '.':
40+
continue;
3241
default:
33-
break;
42+
// Invalid character was found
43+
return false;
3444
}
3545
}
3646

tests/NamespaceNameTest.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ TEST(NamespaceNameTest, testNamespaceNameV2) {
4242
std::shared_ptr<NamespaceName> nn2 = NamespaceName::get("property", "namespace");
4343
ASSERT_TRUE(*nn1 == *nn2);
4444
}
45+
46+
TEST(NamespaceNameTest, testNamespaceNameLegalCharacters) {
47+
std::shared_ptr<NamespaceName> nn1 = NamespaceName::get("cluster-1:=.", "namespace-1:=.");
48+
ASSERT_EQ("cluster-1:=.", nn1->getProperty());
49+
ASSERT_TRUE(nn1->getCluster().empty());
50+
ASSERT_EQ("namespace-1:=.", nn1->getLocalName());
51+
ASSERT_TRUE(nn1->isV2());
52+
}

tests/TopicNameTest.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ TEST(TopicNameTest, testIllegalCharacters) {
140140
ASSERT_FALSE(topicName);
141141
}
142142

143+
TEST(TopicNameTest, testLegalNonAlphaCharacters) {
144+
std::shared_ptr<TopicName> topicName = TopicName::get("persistent://cluster-1:=./namespace-1:=./topic");
145+
ASSERT_TRUE(topicName);
146+
ASSERT_EQ("cluster-1:=.", topicName->getProperty());
147+
ASSERT_EQ("namespace-1:=.", topicName->getNamespacePortion());
148+
ASSERT_EQ("persistent", topicName->getDomain());
149+
ASSERT_EQ("topic", topicName->getLocalName());
150+
}
151+
143152
TEST(TopicNameTest, testIllegalUrl) {
144153
std::shared_ptr<TopicName> topicName = TopicName::get("persistent:::/property/cluster/namespace/topic");
145154
ASSERT_FALSE(topicName);

0 commit comments

Comments
 (0)