Fix ckmc headers grammar
[platform/core/security/key-manager.git] / tests / test_sql.cpp
1 #include <dpl/db/sql_connection.h>
2 #include <boost/test/unit_test.hpp>
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 #include <string>
7 #include <sstream>
8 #include <assert.h>
9 #include <sqlcipher.h>
10 #include <ckm/ckm-type.h>
11 #include <errno.h>
12
13 #include <test_common.h>
14
15 #pragma GCC diagnostic push
16 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
17
18
19 const char *encrypt_me = "/tmp/encryptme.db";
20 const char *encrypt_me_not = "/tmp/encryptmenot.db";
21
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";
27
28 CKM::RawBuffer raw_password = createDefaultPass();
29
30 BOOST_AUTO_TEST_SUITE(SQL_TEST)
31 BOOST_AUTO_TEST_CASE(sqlTestConversion){
32
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);
39 }
40
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);
49 }
50
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);
59 }
60
61 BOOST_AUTO_TEST_CASE(sqlTestConnectionUnencrypted) {
62     using namespace CKM::DB;
63     BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
64     {
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));
69     }
70     {
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());
77         std::string value;
78         BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
79         BOOST_REQUIRE(value == "one for the money");
80     }
81 }
82
83 BOOST_AUTO_TEST_CASE(sqlTestConnectionEncrypted) {
84     using namespace CKM::DB;
85     BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
86     {
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));
92     }
93     {
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());
101         std::string value;
102         BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
103         BOOST_REQUIRE(value == "one for the money");
104     }
105 }
106
107 BOOST_AUTO_TEST_CASE(sqlTestConnectionEncryptedNegative) {
108
109     using namespace CKM::DB;
110     BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
111     {
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));
117     }
118     {
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);
124         }
125         BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
126
127         SqlConnection::DataCommandUniquePtr selectCommand;
128         BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(select_table),
129                 SqlConnection::Exception::SyntaxError)
130     }
131 }
132 BOOST_AUTO_TEST_SUITE_END()
133 #pragma GCC diagnostic pop