Fix build break with boost 1.71.0
[platform/core/security/key-manager.git] / tests / test_safe-buffer.cpp
1 /*
2  *  Copyright (c) 2016-2019 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  *  Contact: Kyungwook Tak <k.tak@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  * @file        test_safe-buffer.cpp
19  * @author      Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
20  * @version
21  * @brief
22  */
23 #include <vector>
24
25 #include <boost/test/unit_test.hpp>
26 #include <test_common.h>
27
28 #include <ckm/ckm-raw-buffer.h>
29
30 using namespace CKM;
31
32 namespace {
33
34 const size_t LEN = 100;
35
36 struct Item {
37         explicit Item(size_t a) : mA(a) {}
38         ~Item() {}
39
40         bool operator==(const size_t &other) const
41         {
42                 return mA == other;
43         }
44
45         size_t mA;
46 };
47
48 template <typename T>
49 size_t buffer_erase_test()
50 {
51         typename T::value_type *data = NULL;
52         typename T::size_type i = 0;
53
54         {
55                 T buffer;
56
57                 for (i = 0; i < LEN; ++i)
58                         buffer.push_back(typename T::value_type(i));
59
60                 data = buffer.data();
61
62                 for (i = 0; i < LEN; ++i)
63                         BOOST_CHECK(data[i] == i);
64         }
65
66         /*
67          *  operator delete of RawBuffer which is called after buffer memory is erased
68          *  (see erase_on_dealloc::deallocate) sometimes leaves garbage in the beginning of that memory.
69          *  Therefore the test will be marked as failing only if more than 1/10 of the data matches
70          *  original
71          */
72         size_t cnt = 0;
73
74         for (i = 0; i < LEN; ++i)
75                 cnt += (data[i] == i ? 1 : 0);
76
77         return cnt;
78 }
79
80 } // namespace anonymous
81
82 BOOST_AUTO_TEST_SUITE(SAFE_BUFFER_TEST)
83
84 // Tests for SafeBuffer. Checks if memory occupied by the buffer is wiped after it's deleted.
85
86 BOOST_AUTO_TEST_CASE(SafeBufferTest_uc_control_group)
87 {
88         size_t cnt = buffer_erase_test<std::vector<unsigned char>>();
89
90         BOOST_REQUIRE_MESSAGE(cnt > LEN / 2,
91                                                   "Less than 1/2 of data matches the original.");
92 }
93
94 BOOST_AUTO_TEST_CASE(SafeBufferTest_item_control_group)
95 {
96         size_t cnt = buffer_erase_test<std::vector<Item>>();
97
98         BOOST_REQUIRE_MESSAGE(cnt > LEN / 2,
99                                                   "Less than 1/2 of data matches the original.");
100 }
101
102 BOOST_AUTO_TEST_CASE(SafeBufferTest_uc)
103 {
104         size_t cnt = buffer_erase_test<RawBuffer>();
105
106         BOOST_REQUIRE_MESSAGE(cnt <= LEN / 10,
107                                                   "More than 1/10 of data matches the original.");
108 }
109
110 BOOST_AUTO_TEST_CASE(SafeBufferTest_item)
111 {
112         size_t cnt = buffer_erase_test<SafeBuffer<Item>::Type>();
113
114         BOOST_REQUIRE_MESSAGE(cnt <= LEN / 10,
115                                                   "More than 1/10 of data matches the original.");
116 }
117
118 BOOST_AUTO_TEST_SUITE_END()