1 #include <dpl/db/sql_connection.h>
2 #include <boost/test/unit_test.hpp>
10 #include <ckm/ckm-type.h>
13 #include <test_common.h>
15 #pragma GCC diagnostic push
16 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
19 const char *encrypt_me = "/tmp/encryptme.db";
20 const char *encrypt_me_not = "/tmp/encryptmenot.db";
22 const char *create_table = "CREATE TABLE t1(a,b);";
23 const char *insert_table = "INSERT INTO t1(a,b) VALUES ("
24 " 'one for the money',"
25 " 'two for the show');";
26 const char *select_table = "SELECT * FROM t1";
28 CKM::RawBuffer raw_password = createDefaultPass();
30 BOOST_AUTO_TEST_SUITE(SQL_TEST)
31 BOOST_AUTO_TEST_CASE(sqlTestConversion){
33 BOOST_REQUIRE_MESSAGE(raw_password.size() == RAW_PASS_SIZE,
34 "Password should have 32 characters, got: " << raw_password.size());
35 std::string pass_check = rawToHexString(raw_password);
36 BOOST_REQUIRE_MESSAGE(pass_check.length() == HEX_PASS_SIZE,
37 "Hex string should have 64 characters, got: " << pass_check.length());
38 BOOST_CHECK(pass_check == pattern);
41 BOOST_AUTO_TEST_CASE(sqlTestSetKeyTooShort) {
42 using namespace CKM::DB;
43 BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
44 SqlConnection connection(encrypt_me_not,
45 SqlConnection::Flag::CRW);
46 CKM::RawBuffer wrong_key(RAW_PASS_SIZE - 1, 1);
47 BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
48 SqlConnection::Exception::InvalidArguments);
51 BOOST_AUTO_TEST_CASE(sqlTestSetKeyTooLong) {
52 using namespace CKM::DB;
53 BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
54 SqlConnection connection(encrypt_me_not,
55 SqlConnection::Flag::CRW);
56 CKM::RawBuffer wrong_key(RAW_PASS_SIZE + 1, 1);
57 BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
58 SqlConnection::Exception::InvalidArguments);
61 BOOST_AUTO_TEST_CASE(sqlTestConnectionUnencrypted) {
62 using namespace CKM::DB;
63 BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
65 SqlConnection encrypting_you_not(encrypt_me_not,
66 SqlConnection::Flag::CRW);
67 BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(create_table));
68 BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(insert_table));
71 SqlConnection encrypting_you_not(encrypt_me_not,
72 SqlConnection::Flag::RW);
73 SqlConnection::DataCommandUniquePtr selectCommand;
74 BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you_not.
75 PrepareDataCommand(select_table));
76 BOOST_REQUIRE_NO_THROW(selectCommand->Step());
78 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
79 BOOST_REQUIRE(value == "one for the money");
83 BOOST_AUTO_TEST_CASE(sqlTestConnectionEncrypted) {
84 using namespace CKM::DB;
85 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
87 SqlConnection encrypting_you(encrypt_me,
88 SqlConnection::Flag::CRW);
89 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
90 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
91 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
94 SqlConnection encrypting_you(encrypt_me,
95 SqlConnection::Flag::RW);
96 encrypting_you.SetKey(raw_password);
97 SqlConnection::DataCommandUniquePtr selectCommand;
98 BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you.
99 PrepareDataCommand(select_table));
100 BOOST_REQUIRE_NO_THROW(selectCommand->Step());
102 BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
103 BOOST_REQUIRE(value == "one for the money");
107 BOOST_AUTO_TEST_CASE(sqlTestConnectionEncryptedNegative) {
109 using namespace CKM::DB;
110 BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
112 SqlConnection encrypting_you(encrypt_me,
113 SqlConnection::Flag::CRW);
114 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
115 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
116 BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
119 SqlConnection encrypting_you(encrypt_me,
120 SqlConnection::Flag::RW);
121 CKM::RawBuffer wrong_password;
122 for(std::size_t i = 0; i < RAW_PASS_SIZE; i++) {
123 wrong_password.push_back(raw_password[i] + 1);
125 BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
127 SqlConnection::DataCommandUniquePtr selectCommand;
128 BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(select_table),
129 SqlConnection::Exception::SyntaxError)
132 BOOST_AUTO_TEST_SUITE_END()
133 #pragma GCC diagnostic pop