28b18333c26b44ad85ad53fcb17186bc99c18ee6
[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 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(copy_assignment)
32 {
33         BinaryQueue bq1;
34         bq1.AppendCopy(buf.data(), buf.size());
35
36         BinaryQueue bq2;
37         bq2 = bq1;
38
39         BOOST_REQUIRE(bq1.Size() == bq2.Size() && bq1.Size() == buf.size());
40
41         RawBuffer buf1(bq1.Size(), 0x00);
42         RawBuffer buf2(bq2.Size(), 0x00);
43
44         bq1.Flatten(buf1.data(), buf1.size());
45         bq2.Flatten(buf2.data(), buf2.size());
46
47         BOOST_REQUIRE(buf1 == buf2);
48 }
49
50 POSITIVE_TEST_CASE(append_copy_to)
51 {
52         BinaryQueue bq1;
53         bq1.AppendCopy(buf.data(), buf.size());
54
55         BinaryQueue bq2;
56         bq1.AppendCopyTo(bq2);
57
58         BOOST_REQUIRE(bq1.Size() == bq2.Size() && bq1.Size() == buf.size());
59
60         RawBuffer buf1(bq1.Size(), 0x00);
61         RawBuffer buf2(bq2.Size(), 0x00);
62
63         bq1.Flatten(buf1.data(), buf1.size());
64         bq2.Flatten(buf2.data(), buf2.size());
65
66         BOOST_REQUIRE(buf1 == buf2);
67 }
68
69 POSITIVE_TEST_CASE(append_move_to)
70 {
71         BinaryQueue bq1;
72         bq1.AppendCopy(buf.data(), buf.size());
73
74         BinaryQueue bq2;
75         bq1.AppendMoveTo(bq2);
76
77         BOOST_REQUIRE(bq2.Size() == buf.size() && bq1.Empty());
78
79         RawBuffer buf2(bq2.Size(), 0x00);
80         bq2.Flatten(buf2.data(), buf2.size());
81         BOOST_REQUIRE(buf == buf2);
82 }
83
84 POSITIVE_TEST_CASE(read)
85 {
86         BinaryQueue bq1;
87         bq1.AppendCopy(buf.data(), buf.size());
88
89         auto bq2 = bq1.Read(buf.size());
90         BOOST_REQUIRE(bq1.Empty());
91
92         RawBuffer buf2(bq2->Size(), 0x00);
93         bq2->Flatten(buf2.data(), buf2.size());
94         BOOST_REQUIRE(buf == buf2);
95 }
96
97 POSITIVE_TEST_CASE(write)
98 {
99         BinaryQueue bq1;
100         bq1.AppendCopy(buf.data(), buf.size());
101
102         BinaryQueue bq2;
103         bq2.Write(bq1, bq1.Size());
104
105         RawBuffer buf1(bq1.Size(), 0x00);
106         RawBuffer buf2(bq2.Size(), 0x00);
107
108         bq1.Flatten(buf1.data(), buf1.size());
109         bq2.Flatten(buf2.data(), buf2.size());
110
111         BOOST_REQUIRE(buf1 == buf2);
112 }
113
114 POSITIVE_TEST_CASE(bucket_visitor)
115 {
116         static std::vector<unsigned char> globalBuf;
117
118         class BucketVisitorTest : public BinaryQueue::BucketVisitor {
119         public:
120                 virtual void OnVisitBucket(const void *buffer, size_t bufferSize) override
121                 {
122                         for (size_t i = 0; i < bufferSize; ++i)
123                                 globalBuf.push_back(static_cast<const unsigned char *>(buffer)[i]);
124                 }
125         };
126
127         BucketVisitorTest visitor;
128
129         constexpr size_t BucketNum = 3;
130         BinaryQueue bq;
131         for (size_t i = 0; i < BucketNum; ++i)
132                 bq.AppendCopy(buf.data(), buf.size());
133
134         bq.VisitBuckets(&visitor);
135
136         BOOST_REQUIRE(globalBuf.size() == buf.size() * BucketNum);
137         for (size_t i = 0; i < BucketNum; ++i)
138                 for (size_t j = 0; j < buf.size(); ++j)
139                         BOOST_REQUIRE(globalBuf[i * buf.size() + j] == buf[j]);
140 }
141
142 BOOST_AUTO_TEST_SUITE_END()