Release 0.1.66
[platform/core/security/key-manager.git] / unit-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 const RawBuffer BUF = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
26
27 } // namespace anonymous
28
29 BOOST_AUTO_TEST_SUITE(BINARY_QUEUE_TEST)
30
31 POSITIVE_TEST_CASE(push)
32 {
33         BinaryQueue bq;
34         BOOST_REQUIRE(bq.Size() == 0);
35
36         bq.Push(RawBuffer(BUF));
37         BOOST_REQUIRE(bq.Size() == BUF.size());
38
39         bq.Push(RawBuffer(BUF));
40         BOOST_REQUIRE(bq.Size() == 2 * BUF.size());
41 }
42
43 NEGATIVE_TEST_CASE(push)
44 {
45         BinaryQueue bq;
46
47         bq.Push(RawBuffer());
48         BOOST_REQUIRE(bq.Size() == 0);
49 }
50
51 POSITIVE_TEST_CASE(pop)
52 {
53         const size_t PART1_SIZE = BUF.size() / 2;
54
55         BinaryQueue bq;
56
57         bq.Push(RawBuffer(BUF.begin(), BUF.begin() + PART1_SIZE));
58         bq.Push(RawBuffer(BUF.begin() + PART1_SIZE, BUF.end()));
59
60         RawBuffer out(3 * BUF.size() / 4);
61         bq.Pop(out.data(), out.size());
62         BOOST_REQUIRE(std::equal(out.begin(), out.end(), BUF.begin()));
63
64         const size_t REST_SIZE = BUF.size() - out.size();
65         BOOST_REQUIRE(bq.Size() == REST_SIZE);
66
67         bq.Pop(out.data(), REST_SIZE);
68         BOOST_REQUIRE(std::equal(out.begin(), out.begin() + REST_SIZE, BUF.begin() + out.size()));
69         BOOST_REQUIRE(std::equal(out.begin() + REST_SIZE, out.end(), BUF.begin() + REST_SIZE));
70         BOOST_REQUIRE(bq.Size() == 0);
71 }
72
73 NEGATIVE_TEST_CASE(pop)
74 {
75         BinaryQueue bq;
76
77         char out;
78         BOOST_REQUIRE_THROW(bq.Pop(&out, sizeof(out)), BinaryQueue::Exception::OutOfData);
79
80         bq.Push(RawBuffer(BUF));
81
82         bq.Pop(&out, 0);
83         BOOST_REQUIRE(bq.Size() == BUF.size());
84
85         RawBuffer out2(BUF.size() + 1);
86         BOOST_REQUIRE_THROW(bq.Pop(out2.data(), out2.size()), BinaryQueue::Exception::OutOfData);
87         BOOST_REQUIRE(bq.Size() == BUF.size());
88 }
89
90 BOOST_AUTO_TEST_SUITE_END()