Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_sql.cpp
1 /*
2  *  Copyright (c) 2016 - 2020 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_sql.cpp
19  * @author      Zofia Abramowska (z.abramowska@samsung.com)
20  * @version
21  * @brief
22  */
23 #include <dpl/db/sql_connection.h>
24 #include <boost_macros_wrapper.h>
25 #include <iostream>
26 #include <iomanip>
27 #include <vector>
28 #include <string>
29 #include <sstream>
30 #include <ckm/ckm-type.h>
31 #include <errno.h>
32
33 #include <test_common.h>
34
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
37
38 const char *encrypt_me = "/tmp/encryptme.db";
39 const char *encrypt_me_not = "/tmp/encryptmenot.db";
40
41 const char *create_table = "CREATE TABLE t1(a,b);";
42 const char *insert_table = "INSERT INTO t1(a,b) VALUES ("
43                                                    " 'one for the money',"
44                                                    " 'two for the show');";
45 const char *select_table = "SELECT * FROM t1";
46
47 CKM::RawBuffer raw_password = createDefaultPass();
48
49 BOOST_AUTO_TEST_SUITE(SQL_TEST)
50 POSITIVE_TEST_CASE(sqlTestConversion)
51 {
52         BOOST_REQUIRE_MESSAGE(raw_password.size() == RAW_PASS_SIZE,
53                                                   "Password should have 32 characters, got: " << raw_password.size());
54         std::string pass_check = rawToHexString(raw_password);
55         BOOST_REQUIRE_MESSAGE(pass_check.length() == HEX_PASS_SIZE,
56                                                   "Hex string should have 64 characters, got: " << pass_check.length());
57         BOOST_CHECK(pass_check == pattern);
58 }
59
60 POSITIVE_TEST_CASE(sqlTestConversionBig)
61 {
62         /* 192 ~ 208 in hex */
63         const std::string tmppattern = "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0";
64
65         auto pass = createPass(192, 209);
66         BOOST_REQUIRE_MESSAGE(pass.size() == 17, "Password size should be 17");
67
68         auto pass_hex = rawToHexString(pass);
69         BOOST_REQUIRE_MESSAGE(pass_hex.length() == 34,
70                                                   "Hexed password size should be 34");
71         BOOST_CHECK(pass_hex == tmppattern);
72 }
73
74 NEGATIVE_TEST_CASE(sqlTestSetKeyTooShort)
75 {
76         using namespace CKM::DB;
77         BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
78         SqlConnection connection(encrypt_me_not,
79                                                          SqlConnection::Flag::CRW);
80         CKM::RawBuffer wrong_key(RAW_PASS_SIZE - 1, 1);
81         BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
82                                                 SqlConnection::Exception::InvalidArguments);
83 }
84
85 NEGATIVE_TEST_CASE(sqlTestSetKeyTooLong)
86 {
87         using namespace CKM::DB;
88         BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
89         SqlConnection connection(encrypt_me_not,
90                                                          SqlConnection::Flag::CRW);
91         CKM::RawBuffer wrong_key(RAW_PASS_SIZE + 1, 1);
92         BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
93                                                 SqlConnection::Exception::InvalidArguments);
94 }
95
96 POSITIVE_TEST_CASE(sqlTestConnectionUnencrypted)
97 {
98         using namespace CKM::DB;
99         BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
100
101         {
102                 SqlConnection encrypting_you_not(encrypt_me_not,
103                                                                                  SqlConnection::Flag::CRW);
104                 BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(create_table));
105                 BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(insert_table));
106         }
107
108         {
109                 SqlConnection encrypting_you_not(encrypt_me_not,
110                                                                                  SqlConnection::Flag::RW);
111                 SqlConnection::DataCommandUniquePtr selectCommand;
112                 BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you_not.
113                                                                                            PrepareDataCommand(select_table));
114                 BOOST_REQUIRE_NO_THROW(selectCommand->Step());
115                 std::string value;
116                 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
117                 BOOST_REQUIRE(value == "one for the money");
118         }
119 }
120
121 POSITIVE_TEST_CASE(sqlTestConnectionEncrypted)
122 {
123         using namespace CKM::DB;
124         BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
125
126         {
127                 SqlConnection encrypting_you(encrypt_me,
128                                                                          SqlConnection::Flag::CRW);
129                 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
130                 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
131                 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
132         }
133
134         {
135                 SqlConnection encrypting_you(encrypt_me,
136                                                                          SqlConnection::Flag::RW);
137                 encrypting_you.SetKey(raw_password);
138                 SqlConnection::DataCommandUniquePtr selectCommand;
139                 BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you.
140                                                                                            PrepareDataCommand(select_table));
141                 BOOST_REQUIRE_NO_THROW(selectCommand->Step());
142                 std::string value;
143                 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
144                 BOOST_REQUIRE(value == "one for the money");
145         }
146 }
147
148 NEGATIVE_TEST_CASE(sqlTestConnectionEncryptedNegative)
149 {
150         using namespace CKM::DB;
151         BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
152
153         {
154                 SqlConnection encrypting_you(encrypt_me,
155                                                                          SqlConnection::Flag::CRW);
156                 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
157                 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
158                 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
159         }
160
161         {
162                 SqlConnection encrypting_you(encrypt_me,
163                                                                          SqlConnection::Flag::RW);
164                 CKM::RawBuffer wrong_password;
165
166                 for (std::size_t i = 0; i < RAW_PASS_SIZE; i++)
167                         wrong_password.push_back(raw_password[i] + 1);
168
169                 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
170
171                 SqlConnection::DataCommandUniquePtr selectCommand;
172                 BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(
173                                                                                                 select_table),
174                                                         SqlConnection::Exception::SyntaxError);
175         }
176 }
177
178 BOOST_AUTO_TEST_SUITE_END()
179 #pragma GCC diagnostic pop