2 * Copyright (c) 2016 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Kyungwook Tak <k.tak@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * @author Zofia Abramowska (z.abramowska@samsung.com)
23 #include <dpl/db/sql_connection.h>
24 #include <boost_macros_wrapper.h>
31 #include <sqlcipher.h>
32 #include <ckm/ckm-type.h>
35 #include <test_common.h>
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
40 const char *encrypt_me = "/tmp/encryptme.db";
41 const char *encrypt_me_not = "/tmp/encryptmenot.db";
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";
49 CKM::RawBuffer raw_password = createDefaultPass();
51 BOOST_AUTO_TEST_SUITE(SQL_TEST)
52 POSITIVE_TEST_CASE(sqlTestConversion)
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);
62 POSITIVE_TEST_CASE(sqlTestConversionBig)
64 /* 192 ~ 208 in hex */
65 const std::string tmppattern = "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0";
67 auto pass = createPass(192, 209);
68 BOOST_REQUIRE_MESSAGE(pass.size() == 17, "Password size should be 17");
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);
76 NEGATIVE_TEST_CASE(sqlTestSetKeyTooShort)
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);
87 NEGATIVE_TEST_CASE(sqlTestSetKeyTooLong)
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);
98 POSITIVE_TEST_CASE(sqlTestConnectionUnencrypted)
100 using namespace CKM::DB;
101 BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
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));
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());
118 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
119 BOOST_REQUIRE(value == "one for the money");
123 POSITIVE_TEST_CASE(sqlTestConnectionEncrypted)
125 using namespace CKM::DB;
126 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
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));
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());
145 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
146 BOOST_REQUIRE(value == "one for the money");
150 NEGATIVE_TEST_CASE(sqlTestConnectionEncryptedNegative)
152 using namespace CKM::DB;
153 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
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));
164 SqlConnection encrypting_you(encrypt_me,
165 SqlConnection::Flag::RW);
166 CKM::RawBuffer wrong_password;
168 for (std::size_t i = 0; i < RAW_PASS_SIZE; i++)
169 wrong_password.push_back(raw_password[i] + 1);
171 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
173 SqlConnection::DataCommandUniquePtr selectCommand;
174 BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(
176 SqlConnection::Exception::SyntaxError);
180 BOOST_AUTO_TEST_SUITE_END()
181 #pragma GCC diagnostic pop