From d4aae4b195b374ec631f5f0ec6bdd5c3e3d1af54 Mon Sep 17 00:00:00 2001 From: Sergey Yakubov <sergey.yakubov@desy.de> Date: Sat, 5 Jun 2021 00:41:59 +0200 Subject: [PATCH] fix --- broker/src/asapo_broker/database/encoding.go | 2 +- .../asapo_broker/database/encoding_test.go | 33 +++++++++++++++++++ common/cpp/src/database/CMakeLists.txt | 2 +- common/cpp/src/database/encoding.cpp | 30 ++++++++--------- 4 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 broker/src/asapo_broker/database/encoding_test.go diff --git a/broker/src/asapo_broker/database/encoding.go b/broker/src/asapo_broker/database/encoding.go index 7c61d5428..e1126fa84 100644 --- a/broker/src/asapo_broker/database/encoding.go +++ b/broker/src/asapo_broker/database/encoding.go @@ -3,7 +3,7 @@ package database import "net/url" func shouldEscape(c byte, db bool) bool { - if c == '$' || c == ' ' { + if c == '$' || c == ' ' || c == '%' { return true } if !db { diff --git a/broker/src/asapo_broker/database/encoding_test.go b/broker/src/asapo_broker/database/encoding_test.go new file mode 100644 index 000000000..5f98641ef --- /dev/null +++ b/broker/src/asapo_broker/database/encoding_test.go @@ -0,0 +1,33 @@ +package database + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestEncoding(t *testing.T) { + stream:=`ss$` + source :=`ads%&%41.sss` + streamEncoded := encodeStringForColName(stream) + sourceEncoded := encodeStringForDbName(source) + streamDecoded := decodeString(streamEncoded) + sourceDecoded := decodeString(sourceEncoded) + assert.Equal(t, streamDecoded, stream) + assert.Equal(t, sourceDecoded, source) + + r := Request{ + DbName: source, + DbCollectionName: stream, + GroupId: stream, + Op: "", + DatasetOp: false, + MinDatasetSize: 0, + ExtraParam: "", + } + err := encodeRequest(&r) + assert.Equal(t, r.DbCollectionName, streamEncoded) + assert.Equal(t, r.GroupId, streamEncoded) + assert.Equal(t, r.DbName, sourceEncoded) + + assert.Nil(t,err) +} diff --git a/common/cpp/src/database/CMakeLists.txt b/common/cpp/src/database/CMakeLists.txt index 776cdc055..16acadfd9 100644 --- a/common/cpp/src/database/CMakeLists.txt +++ b/common/cpp/src/database/CMakeLists.txt @@ -27,5 +27,5 @@ set(TEST_SOURCE_FILES ../../unittests/database/test_encoding.cpp) set(TEST_LIBRARIES "${TARGET_NAME}") include_directories(${ASAPO_CXX_COMMON_INCLUDE_DIR}) -gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}") +gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}" "${CMAKE_CURRENT_SOURCE_DIR}/mongodb_client.cpp") diff --git a/common/cpp/src/database/encoding.cpp b/common/cpp/src/database/encoding.cpp index 131e7e26b..67048aa57 100644 --- a/common/cpp/src/database/encoding.cpp +++ b/common/cpp/src/database/encoding.cpp @@ -5,7 +5,7 @@ namespace asapo { bool ShouldEscape(char c, bool db) { - if (c == '$' || c == ' ') { + if (c == '$' || c == ' ' || c == '%') { return true; } if (!db) { @@ -55,25 +55,22 @@ std::string Escape(const std::string &s, bool db) { return t; } - -inline int ishex(int x) -{ - return (x >= '0' && x <= '9') || - (x >= 'a' && x <= 'f') || +inline int ishex(int x) { + return (x >= '0' && x <= '9') || + (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F'); } -int decode(const char *s, char *dec) -{ - char *o; - const char *end = s + strlen(s); +int decode(const char* s, char* dec) { + char* o; + const char* end = s + strlen(s); int c; for (o = dec; s <= end; o++) { c = *s++; // if (c == '+') c = ' '; - if (c == '%' && ( !ishex(*s++) || - !ishex(*s++) || + if (c == '%' && (!ishex(*s++) || + !ishex(*s++) || !sscanf(s - 2, "%2x", &c))) return -1; if (dec) *o = c; @@ -82,7 +79,6 @@ int decode(const char *s, char *dec) return o - dec; } - std::string EncodeDbName(const std::string &dbname) { return Escape(dbname, true); } @@ -93,14 +89,14 @@ std::string EncodeColName(const std::string &colname) { std::string DecodeName(const std::string &name) { char decoded[name.size()]; - auto res = decode(name.c_str(),decoded); - return res>=0?decoded:""; + auto res = decode(name.c_str(), decoded); + return res >= 0 ? decoded : ""; } bool ShouldEscapeQuery(char c) { char chars[] = "-[]{}()*+?\\.,^$|#"; - for (auto i=0;i<strlen(chars);i++) { - if (c==chars[i]) { + for (auto i = 0; i < strlen(chars); i++) { + if (c == chars[i]) { return true; } }; -- GitLab