Add test to xml-utils.cpp 89/258689/4
authorMichał Szaknis <m.szaknis@samsung.com>
Thu, 20 May 2021 10:30:06 +0000 (12:30 +0200)
committerMichał Szaknis <m.szaknis@samsung.com>
Tue, 25 May 2021 10:42:10 +0000 (12:42 +0200)
Change-Id: I74c1130c20245ade53dbb86c72e38528409be23f

unit-tests/CMakeLists.txt
unit-tests/test_xml-utils.cpp [new file with mode: 0644]

index 30eaac0..23813f6 100644 (file)
@@ -90,6 +90,7 @@ SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/test_ss-crypto.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test_sw-backend.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test_xml-parser.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test_xml-utils.cpp
 
     ${MANAGER_PATH}/client/client-common.cpp
     ${MANAGER_PATH}/client-async/descriptor-set.cpp
diff --git a/unit-tests/test_xml-utils.cpp b/unit-tests/test_xml-utils.cpp
new file mode 100644 (file)
index 0000000..47ef62e
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ *  Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include <boost_macros_wrapper.h>
+#include <random>
+#include <string>
+#include "ckm/ckm-raw-buffer.h"
+#include "xml-utils.h"
+
+namespace {
+
+const std::string WHITECHAR_REMOVAL_INPUT =
+    "\t\v\r <some_tag> some_value </some_tag> \n\
+    <some_tag> some_value </some_tag> \t\v\r \n\
+    \t\v\r <some_tag> some_value </some_tag> \t\v\r \n\
+     \t\v\r  \n\
+    ";
+
+const std::string WHITECHAR_REMOVAL_OUTPUT =
+    R"(<some_tag> some_value </some_tag>
+<some_tag> some_value </some_tag>
+<some_tag> some_value </some_tag>
+)";
+
+const std::string SINGLE_LINE_INPUT =
+    "\t\v\r <some_tag> some_value </some_tag> \t\v\r";
+
+const std::string SINGLE_LINE_OUTPUT =
+    "<some_tag>some_value</some_tag>";
+
+const std::string WHITECHARS = "\r\n\v\t ";
+
+bool contains_whitespace(const std::string& str) {
+    return str.find_first_of(WHITECHARS) != std::string::npos;
+}
+
+}
+
+BOOST_AUTO_TEST_SUITE(XML_UTILS_TEST)
+
+POSITIVE_TEST_CASE(TrimTest) {
+    BOOST_REQUIRE(CKM::XML::trimEachLine(WHITECHAR_REMOVAL_INPUT) == WHITECHAR_REMOVAL_OUTPUT);
+    BOOST_REQUIRE(CKM::XML::trim(SINGLE_LINE_INPUT) == SINGLE_LINE_OUTPUT);
+
+    const CKM::RawBuffer buffer_input(SINGLE_LINE_INPUT.begin(), SINGLE_LINE_INPUT.end());
+    const CKM::RawBuffer buffer_output(SINGLE_LINE_OUTPUT.begin(), SINGLE_LINE_OUTPUT.end());
+    const CKM::RawBuffer converted = CKM::XML::removeWhiteChars(buffer_input);
+    BOOST_REQUIRE(converted == buffer_output);
+}
+
+NEGATIVE_TEST_CASE(TrimTest) {
+    std::string raw_bytes;
+    for (unsigned int c=0; c < 0x100; ++c)
+        raw_bytes += static_cast<char>(c);
+
+    BOOST_REQUIRE(!contains_whitespace(CKM::XML::trim(raw_bytes)));
+
+    const std::size_t length = 0x1'000'000;
+    std::string long_random_string;
+    std::random_device rd;
+    std::uniform_int_distribution<> distb(0, 0x100);
+    for (std::size_t i=0; i < length; ++i)
+        long_random_string += static_cast<char>(distb(rd));
+    BOOST_REQUIRE(!contains_whitespace(CKM::XML::trim(long_random_string)));
+
+    BOOST_REQUIRE(CKM::XML::trim("") == "");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+