2 * Copyright (c) 2016 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/test/unit_test.hpp>
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"
41 const char *encrypt_me = "/tmp/encryptme.db";
42 const char *encrypt_me_not = "/tmp/encryptmenot.db";
44 const char *create_table = "CREATE TABLE t1(a,b);";
45 const char *insert_table = "INSERT INTO t1(a,b) VALUES ("
46 " 'one for the money',"
47 " 'two for the show');";
48 const char *select_table = "SELECT * FROM t1";
50 CKM::RawBuffer raw_password = createDefaultPass();
52 BOOST_AUTO_TEST_SUITE(SQL_TEST)
53 BOOST_AUTO_TEST_CASE(sqlTestConversion){
55 BOOST_REQUIRE_MESSAGE(raw_password.size() == RAW_PASS_SIZE,
56 "Password should have 32 characters, got: " << raw_password.size());
57 std::string pass_check = rawToHexString(raw_password);
58 BOOST_REQUIRE_MESSAGE(pass_check.length() == HEX_PASS_SIZE,
59 "Hex string should have 64 characters, got: " << pass_check.length());
60 BOOST_CHECK(pass_check == pattern);
63 BOOST_AUTO_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, "Hexed password size should be 34");
72 BOOST_CHECK(pass_hex == tmppattern);
75 BOOST_AUTO_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 BOOST_AUTO_TEST_CASE(sqlTestSetKeyTooLong) {
86 using namespace CKM::DB;
87 BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
88 SqlConnection connection(encrypt_me_not,
89 SqlConnection::Flag::CRW);
90 CKM::RawBuffer wrong_key(RAW_PASS_SIZE + 1, 1);
91 BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
92 SqlConnection::Exception::InvalidArguments);
95 BOOST_AUTO_TEST_CASE(sqlTestConnectionUnencrypted) {
96 using namespace CKM::DB;
97 BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
99 SqlConnection encrypting_you_not(encrypt_me_not,
100 SqlConnection::Flag::CRW);
101 BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(create_table));
102 BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(insert_table));
105 SqlConnection encrypting_you_not(encrypt_me_not,
106 SqlConnection::Flag::RW);
107 SqlConnection::DataCommandUniquePtr selectCommand;
108 BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you_not.
109 PrepareDataCommand(select_table));
110 BOOST_REQUIRE_NO_THROW(selectCommand->Step());
112 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
113 BOOST_REQUIRE(value == "one for the money");
117 BOOST_AUTO_TEST_CASE(sqlTestConnectionEncrypted) {
118 using namespace CKM::DB;
119 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
121 SqlConnection encrypting_you(encrypt_me,
122 SqlConnection::Flag::CRW);
123 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
124 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
125 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
128 SqlConnection encrypting_you(encrypt_me,
129 SqlConnection::Flag::RW);
130 encrypting_you.SetKey(raw_password);
131 SqlConnection::DataCommandUniquePtr selectCommand;
132 BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you.
133 PrepareDataCommand(select_table));
134 BOOST_REQUIRE_NO_THROW(selectCommand->Step());
136 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
137 BOOST_REQUIRE(value == "one for the money");
141 BOOST_AUTO_TEST_CASE(sqlTestConnectionEncryptedNegative) {
143 using namespace CKM::DB;
144 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
146 SqlConnection encrypting_you(encrypt_me,
147 SqlConnection::Flag::CRW);
148 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
149 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
150 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
153 SqlConnection encrypting_you(encrypt_me,
154 SqlConnection::Flag::RW);
155 CKM::RawBuffer wrong_password;
156 for(std::size_t i = 0; i < RAW_PASS_SIZE; i++) {
157 wrong_password.push_back(raw_password[i] + 1);
159 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
161 SqlConnection::DataCommandUniquePtr selectCommand;
162 BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(select_table),
163 SqlConnection::Exception::SyntaxError)
166 BOOST_AUTO_TEST_SUITE_END()
167 #pragma GCC diagnostic pop