Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_xml-utils.cpp
1 /*
2  *  Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16
17 #include <boost_macros_wrapper.h>
18 #include <random>
19 #include <string>
20 #include "ckm/ckm-raw-buffer.h"
21 #include "xml-utils.h"
22
23 namespace {
24
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\
29      \t\v\r  \n\
30     ";
31
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>
36 )";
37
38 const std::string SINGLE_LINE_INPUT =
39     "\t\v\r <some_tag> some_value </some_tag> \t\v\r";
40
41 const std::string SINGLE_LINE_OUTPUT =
42     "<some_tag>some_value</some_tag>";
43
44 const std::string WHITECHARS = "\r\n\v\t ";
45
46 bool contains_whitespace(const std::string& str) {
47     return str.find_first_of(WHITECHARS) != std::string::npos;
48 }
49
50 }
51
52 BOOST_AUTO_TEST_SUITE(XML_UTILS_TEST)
53
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);
57
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);
62 }
63
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);
68
69     BOOST_REQUIRE(!contains_whitespace(CKM::XML::trim(raw_bytes)));
70
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)));
78
79     BOOST_REQUIRE(CKM::XML::trim("") == "");
80 }
81
82 BOOST_AUTO_TEST_SUITE_END()
83