2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 #include <boost_macros_wrapper.h>
20 #include "ckm/ckm-raw-buffer.h"
21 #include "xml-utils.h"
25 const std::string WHITECHAR_REMOVAL_INPUT =
26 "\t\v\r <some_tag> some_value </some_tag> \n\
27 <some_tag> some_value </some_tag> \t\v\r \n\
28 \t\v\r <some_tag> some_value </some_tag> \t\v\r \n\
32 const std::string WHITECHAR_REMOVAL_OUTPUT =
33 R"(<some_tag> some_value </some_tag>
34 <some_tag> some_value </some_tag>
35 <some_tag> some_value </some_tag>
38 const std::string SINGLE_LINE_INPUT =
39 "\t\v\r <some_tag> some_value </some_tag> \t\v\r";
41 const std::string SINGLE_LINE_OUTPUT =
42 "<some_tag>some_value</some_tag>";
44 const std::string WHITECHARS = "\r\n\v\t ";
46 bool contains_whitespace(const std::string& str) {
47 return str.find_first_of(WHITECHARS) != std::string::npos;
52 BOOST_AUTO_TEST_SUITE(XML_UTILS_TEST)
54 POSITIVE_TEST_CASE(TrimTest) {
55 BOOST_REQUIRE(CKM::XML::trimEachLine(WHITECHAR_REMOVAL_INPUT) == WHITECHAR_REMOVAL_OUTPUT);
56 BOOST_REQUIRE(CKM::XML::trim(SINGLE_LINE_INPUT) == SINGLE_LINE_OUTPUT);
58 const CKM::RawBuffer buffer_input(SINGLE_LINE_INPUT.begin(), SINGLE_LINE_INPUT.end());
59 const CKM::RawBuffer buffer_output(SINGLE_LINE_OUTPUT.begin(), SINGLE_LINE_OUTPUT.end());
60 const CKM::RawBuffer converted = CKM::XML::removeWhiteChars(buffer_input);
61 BOOST_REQUIRE(converted == buffer_output);
64 NEGATIVE_TEST_CASE(TrimTest) {
65 std::string raw_bytes;
66 for (unsigned int c=0; c < 0x100; ++c)
67 raw_bytes += static_cast<char>(c);
69 BOOST_REQUIRE(!contains_whitespace(CKM::XML::trim(raw_bytes)));
71 const std::size_t length = 0x1'000'000;
72 std::string long_random_string;
73 std::random_device rd;
74 std::uniform_int_distribution<> distb(0, 0x100);
75 for (std::size_t i=0; i < length; ++i)
76 long_random_string += static_cast<char>(distb(rd));
77 BOOST_REQUIRE(!contains_whitespace(CKM::XML::trim(long_random_string)));
79 BOOST_REQUIRE(CKM::XML::trim("") == "");
82 BOOST_AUTO_TEST_SUITE_END()