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