Make some single arg constructors explicit
[platform/core/security/key-manager.git] / tests / test_base64.cpp
1 /*
2  *  Copyright (c) 2016 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  * @file        test_base64.cpp
17  * @author      Kyungwook Tak (k.tak@samsung.com)
18  * @version     1.0
19  * @brief       base64 encoder / decoder test
20  */
21 #include <base64.h>
22
23 #include <vector>
24 #include <cstring>
25 #include <boost/test/unit_test.hpp>
26
27 #include <ckm/ckm-type.h>
28 #include <exception.h>
29
30 using CKM::Base64Encoder;
31 using CKM::Base64Decoder;
32 using CKM::RawBuffer;
33
34 namespace {
35
36 constexpr unsigned char RAW_DATA[] =  {
37         0xf8, 0x87, 0x0a, 0xc5, 0xd3, 0x6d, 0x44, 0x49, 0x03, 0x9f, 0xbd, 0x1e, 0xa8, 0x2f, 0xf6, 0xc3,
38         0xdf, 0x3b, 0x02, 0x13, 0x58, 0x1b, 0x12, 0x30, 0x1c, 0xd7, 0xad, 0xa5, 0x1f, 0x5d, 0x01, 0x33
39 };
40
41 const std::vector<unsigned char>
42 RAW_DATA_VEC(RAW_DATA, RAW_DATA + sizeof(RAW_DATA) / sizeof(unsigned char));
43
44 const RawBuffer rawbuf(RAW_DATA_VEC.begin(), RAW_DATA_VEC.end());
45
46 }
47
48 BOOST_AUTO_TEST_SUITE(BASE64_TEST)
49
50 BOOST_AUTO_TEST_CASE(ENCODE_DECODE_POSITIVE)
51 {
52         /* try encode */
53         Base64Encoder encoder;
54         BOOST_REQUIRE_NO_THROW(encoder.append(rawbuf));
55         BOOST_REQUIRE_NO_THROW(encoder.finalize());
56
57         RawBuffer encdata;
58         BOOST_REQUIRE_NO_THROW(encdata = encoder.get());
59         BOOST_REQUIRE_NO_THROW(encoder.reset());
60
61         /* try decode */
62         Base64Decoder decoder;
63         BOOST_REQUIRE_NO_THROW(decoder.append(encdata));
64         BOOST_REQUIRE_NO_THROW(decoder.finalize());
65
66         RawBuffer decdata;
67         BOOST_REQUIRE_NO_THROW(decdata = decoder.get());
68         BOOST_REQUIRE_NO_THROW(decoder.reset());
69
70         /* compare with orig data */
71         BOOST_REQUIRE_MESSAGE(
72                 rawbuf.size() == decdata.size() &&
73                 memcmp(rawbuf.data(), decdata.data(), rawbuf.size()) == 0,
74                 "Original data and encoded-decoded data is different!");
75 }
76
77 BOOST_AUTO_TEST_CASE(THROW_SOMETHING)
78 {
79         /* encode data */
80         Base64Encoder encoder;
81         BOOST_REQUIRE_THROW(encoder.get(), CKM::Exc::InternalError);
82
83         BOOST_REQUIRE_NO_THROW(encoder.append(rawbuf));
84         BOOST_REQUIRE_NO_THROW(encoder.finalize());
85
86         BOOST_REQUIRE_THROW(encoder.append(rawbuf),
87                                                 CKM::Exc::InternalError);
88         BOOST_REQUIRE_THROW(encoder.finalize(),
89                                                 CKM::Exc::InternalError);
90
91         RawBuffer encdata;
92         BOOST_REQUIRE_NO_THROW(encdata = encoder.get());
93
94         /* decode data */
95         Base64Decoder decoder;
96         BOOST_REQUIRE_THROW(decoder.get(), CKM::Exc::InternalError);
97
98         BOOST_REQUIRE_NO_THROW(decoder.append(encdata));
99         BOOST_REQUIRE_NO_THROW(decoder.finalize());
100
101         BOOST_REQUIRE_THROW(decoder.append(encdata),
102                                                 CKM::Exc::InternalError);
103         BOOST_REQUIRE_THROW(decoder.finalize(),
104                                                 CKM::Exc::InternalError);
105
106         RawBuffer decdata;
107         BOOST_REQUIRE_NO_THROW(decdata = decoder.get());
108 }
109
110 BOOST_AUTO_TEST_CASE(ILLEGAL_DATA)
111 {
112         Base64Decoder decoder;
113         BOOST_REQUIRE_NO_THROW(decoder.append(rawbuf));
114         BOOST_REQUIRE(!decoder.finalize());
115 }
116
117 BOOST_AUTO_TEST_SUITE_END()