2 * Copyright (c) 2016-2019 Samsung Electronics Co., Ltd. All rights reserved
4 * Contact: Kyungwook Tak <k.tak@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @file test_safe-buffer.cpp
19 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
25 #include <boost/test/unit_test.hpp>
26 #include <test_common.h>
28 #include <ckm/ckm-raw-buffer.h>
34 const size_t LEN = 100;
37 explicit Item(size_t a) : mA(a) {}
40 bool operator==(const size_t &other) const
49 size_t buffer_erase_test()
51 typename T::value_type *data = NULL;
52 typename T::size_type i = 0;
57 for (i = 0; i < LEN; ++i)
58 buffer.push_back(typename T::value_type(i));
62 for (i = 0; i < LEN; ++i)
63 BOOST_CHECK(data[i] == i);
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
74 for (i = 0; i < LEN; ++i)
75 cnt += (data[i] == i ? 1 : 0);
80 } // namespace anonymous
82 BOOST_AUTO_TEST_SUITE(SAFE_BUFFER_TEST)
84 // Tests for SafeBuffer. Checks if memory occupied by the buffer is wiped after it's deleted.
86 BOOST_AUTO_TEST_CASE(SafeBufferTest_uc_control_group)
88 size_t cnt = buffer_erase_test<std::vector<unsigned char>>();
90 BOOST_REQUIRE_MESSAGE(cnt > LEN / 2,
91 "Less than 1/2 of data matches the original.");
94 BOOST_AUTO_TEST_CASE(SafeBufferTest_item_control_group)
96 size_t cnt = buffer_erase_test<std::vector<Item>>();
98 BOOST_REQUIRE_MESSAGE(cnt > LEN / 2,
99 "Less than 1/2 of data matches the original.");
102 BOOST_AUTO_TEST_CASE(SafeBufferTest_uc)
104 size_t cnt = buffer_erase_test<RawBuffer>();
106 BOOST_REQUIRE_MESSAGE(cnt <= LEN / 10,
107 "More than 1/10 of data matches the original.");
110 BOOST_AUTO_TEST_CASE(SafeBufferTest_item)
112 size_t cnt = buffer_erase_test<SafeBuffer<Item>::Type>();
114 BOOST_REQUIRE_MESSAGE(cnt <= LEN / 10,
115 "More than 1/10 of data matches the original.");
118 BOOST_AUTO_TEST_SUITE_END()