Refactor BinaryQueue and tests
[platform/core/security/key-manager.git] / tests / test_binary-queue.cpp
1 /*
2  *  Copyright (c) 2000 - 2020 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 #include <boost_macros_wrapper.h>
17
18 #include <dpl/binary_queue.h>
19 #include <ckm/ckm-raw-buffer.h>
20
21 using namespace CKM;
22
23 namespace {
24
25 constexpr unsigned char buf[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
26 constexpr size_t bufSize = sizeof(buf);
27
28 } // namespace anonymous
29
30 BOOST_AUTO_TEST_SUITE(BINARY_QUEUE_TEST)
31
32 POSITIVE_TEST_CASE(append_copy)
33 {
34         BinaryQueue bq;
35         BOOST_REQUIRE(bq.Size() == 0);
36
37         bq.AppendCopy(buf, bufSize);
38
39         BOOST_REQUIRE(bq.Size() == bufSize);
40
41         bq.AppendCopy(buf, bufSize);
42
43         BOOST_REQUIRE(bq.Size() == 2 * bufSize);
44 }
45
46 NEGATIVE_TEST_CASE(append_copy)
47 {
48         BinaryQueue bq;
49
50         bq.AppendCopy(buf, 0);
51
52         BOOST_REQUIRE(bq.Size() == 0);
53 }
54
55 POSITIVE_TEST_CASE(flatten_consume)
56 {
57         BinaryQueue bq;
58
59         constexpr size_t part1Size = bufSize / 2;
60
61         bq.AppendCopy(buf, part1Size);
62         bq.AppendCopy(buf + part1Size, bufSize - part1Size);
63
64         char out[3 * bufSize / 4];
65         bq.FlattenConsume(out, sizeof(out));
66         BOOST_REQUIRE(memcmp(buf, out, sizeof(out)) == 0);
67
68         constexpr size_t remainingSize = bufSize - sizeof(out);
69         BOOST_REQUIRE(bq.Size() == remainingSize);
70
71         bq.FlattenConsume(out, remainingSize);
72
73         BOOST_REQUIRE(memcmp(buf + sizeof(out), out, remainingSize) == 0);
74         BOOST_REQUIRE(memcmp(buf + remainingSize,
75                              out + remainingSize,
76                              sizeof(out) - remainingSize) == 0);
77
78         BOOST_REQUIRE(bq.Size() == 0);
79 }
80
81 NEGATIVE_TEST_CASE(flatten_consume)
82 {
83         BinaryQueue bq;
84
85         char out;
86         BOOST_REQUIRE_THROW(bq.FlattenConsume(&out, sizeof(out)),
87                             BinaryQueue::Exception::OutOfData);
88
89         bq.AppendCopy(buf, bufSize);
90
91         bq.FlattenConsume(&out, 0);
92         BOOST_REQUIRE(bq.Size() == bufSize);
93
94         char out2[bufSize + 1];
95         BOOST_REQUIRE_THROW(bq.FlattenConsume(out2, sizeof(out2)),
96                             BinaryQueue::Exception::OutOfData);
97         BOOST_REQUIRE(bq.Size() == bufSize);
98 }
99
100 BOOST_AUTO_TEST_SUITE_END()