You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2022/10/15 15:35:05 UTC
[pulsar-client-cpp] branch branch-3.0 updated: Fixed validation of cluster/namespace names containing legal non-alpha characters (#52)
This is an automated email from the ASF dual-hosted git repository.
mmerli pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/pulsar-client-cpp.git
The following commit(s) were added to refs/heads/branch-3.0 by this push:
new 6fdcf21 Fixed validation of cluster/namespace names containing legal non-alpha characters (#52)
6fdcf21 is described below
commit 6fdcf21aefaf8396be0f6cf5a18b08974c35944f
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Sat Oct 15 08:33:12 2022 -0700
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
---
lib/NamedEntity.cc | 24 +++++++++++++++++-------
tests/NamespaceNameTest.cc | 8 ++++++++
tests/TopicNameTest.cc | 9 +++++++++
3 files changed, 34 insertions(+), 7 deletions(-)
diff --git a/lib/NamedEntity.cc b/lib/NamedEntity.cc
index ad7c385..484c8b1 100644
--- a/lib/NamedEntity.cc
+++ b/lib/NamedEntity.cc
@@ -18,19 +18,29 @@
*/
#include "NamedEntity.h"
+#include <cctype>
+
+/**
+ * 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 85a5749..51975c8 100644
--- a/tests/NamespaceNameTest.cc
+++ b/tests/NamespaceNameTest.cc
@@ -42,3 +42,11 @@ TEST(NamespaceNameTest, testNamespaceNameV2) {
std::shared_ptr<NamespaceName> nn2 = NamespaceName::get("property", "namespace");
ASSERT_TRUE(*nn1 == *nn2);
}
+
+TEST(NamespaceNameTest, testNamespaceNameLegalCharacters) {
+ std::shared_ptr<NamespaceName> 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 377a931..3db2a6c 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 = 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 = TopicName::get("persistent:::/property/cluster/namespace/topic");
ASSERT_FALSE(topicName);