Change char unique_ptr to char vector
[platform/core/security/key-manager.git] / tests / test_safe-buffer.cpp
1 /*
2  *  Copyright (c) 2016 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 {
38     Item(size_t a) : mA(a) {}
39     ~Item() {}
40
41     bool operator==(const size_t& other) const {
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         T buffer;
55         for (i=0;i<LEN;++i)
56             buffer.push_back(typename T::value_type(i));
57
58         data = buffer.data();
59
60         for (i=0;i<LEN;++i)
61             BOOST_CHECK(data[i] == i);
62     }
63
64     /*
65      *  operator delete of RawBuffer which is called after buffer memory is erased
66      *  (see erase_on_dealloc::deallocate) sometimes leaves garbage in the beginning of that memory.
67      *  Therefore the test will be marked as failing only if more than 1/10 of the data matches
68      *  original
69      */
70     size_t cnt = 0;
71     for (i=0;i<LEN;++i)
72         cnt += (data[i] == i?1:0);
73
74     return cnt;
75 }
76
77 } // namespace anonymous
78
79 BOOST_AUTO_TEST_SUITE(SAFE_BUFFER_TEST)
80
81 // Tests for SafeBuffer. Checks if memory occupied by the buffer is wiped after it's deleted.
82
83 BOOST_AUTO_TEST_CASE(SafeBufferTest_uc_control_group) {
84     size_t cnt = buffer_erase_test<std::vector<unsigned char> >();
85
86     BOOST_REQUIRE_MESSAGE(cnt > LEN/2, "Less than 1/2 of data matches the original.");
87 }
88
89 BOOST_AUTO_TEST_CASE(SafeBufferTest_item_control_group) {
90     size_t cnt = buffer_erase_test<std::vector<Item> >();
91
92     BOOST_REQUIRE_MESSAGE(cnt > LEN/2, "Less than 1/2 of data matches the original.");
93 }
94
95 BOOST_AUTO_TEST_CASE(SafeBufferTest_uc) {
96     size_t cnt = buffer_erase_test<RawBuffer>();
97
98     BOOST_REQUIRE_MESSAGE(cnt <= LEN/10, "More than 1/10 of data matches the original.");
99 }
100
101 BOOST_AUTO_TEST_CASE(SafeBufferTest_item) {
102     size_t cnt = buffer_erase_test<SafeBuffer<Item>::Type>();
103
104     BOOST_REQUIRE_MESSAGE(cnt <= LEN/10, "More than 1/10 of data matches the original.");
105 }
106
107 BOOST_AUTO_TEST_SUITE_END()