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>
30 #include <ckm/ckm-type.h>
33 #include <test_common.h>
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
38 const char *encrypt_me = "/tmp/encryptme.db";
39 const char *encrypt_me_not = "/tmp/encryptmenot.db";
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";
47 CKM::RawBuffer raw_password = createDefaultPass();
49 BOOST_AUTO_TEST_SUITE(SQL_TEST)
50 POSITIVE_TEST_CASE(sqlTestConversion)
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);
60 POSITIVE_TEST_CASE(sqlTestConversionBig)
62 /* 192 ~ 208 in hex */
63 const std::string tmppattern = "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0";
65 auto pass = createPass(192, 209);
66 BOOST_REQUIRE_MESSAGE(pass.size() == 17, "Password size should be 17");
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);
74 NEGATIVE_TEST_CASE(sqlTestSetKeyTooShort)
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);
85 NEGATIVE_TEST_CASE(sqlTestSetKeyTooLong)
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);
96 POSITIVE_TEST_CASE(sqlTestConnectionUnencrypted)
98 using namespace CKM::DB;
99 BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
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));
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());
116 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
117 BOOST_REQUIRE(value == "one for the money");
121 POSITIVE_TEST_CASE(sqlTestConnectionEncrypted)
123 using namespace CKM::DB;
124 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
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));
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());
143 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
144 BOOST_REQUIRE(value == "one for the money");
148 NEGATIVE_TEST_CASE(sqlTestConnectionEncryptedNegative)
150 using namespace CKM::DB;
151 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
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));
162 SqlConnection encrypting_you(encrypt_me,
163 SqlConnection::Flag::RW);
164 CKM::RawBuffer wrong_password;
166 for (std::size_t i = 0; i < RAW_PASS_SIZE; i++)
167 wrong_password.push_back(raw_password[i] + 1);
169 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
171 SqlConnection::DataCommandUniquePtr selectCommand;
172 BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(
174 SqlConnection::Exception::SyntaxError);
178 BOOST_AUTO_TEST_SUITE_END()
179 #pragma GCC diagnostic pop