#include <string>
#include <sstream>
#include <ckm/ckm-type.h>
+#include <chrono>
+#include <db-crypto.h>
#include <errno.h>
-
#include <test_common.h>
+using CKM::DB::SqlConnection;
+
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
-const char *encrypt_me = "/tmp/encryptme.db";
-const char *encrypt_me_not = "/tmp/encryptmenot.db";
-
-const char *create_table = "CREATE TABLE t1(a,b);";
-const char *insert_table = "INSERT INTO t1(a,b) VALUES ("
+const char *ENCRYPT_ME = "/tmp/encryptme.db";
+const char *ENCRYPT_ME_NOT = "/tmp/encryptmenot.db";
+const char *TEST_DB_CKM = "/tmp/eff_test_ckm.db";
+const char *CREATE_TABLE = "CREATE TABLE t1(a,b);";
+const char *INSERT_TABLE = "INSERT INTO t1(a,b) VALUES ("
" 'one for the money',"
" 'two for the show');";
-const char *select_table = "SELECT * FROM t1";
+const char *SELECT_TABLE = "SELECT * FROM t1";
+
+const char *SELECT_COMMAND = "SELECT COUNT(idx) FROM NAMES WHERE name=?101 AND label=?102;";
+const char *INSERT_COMMAND = "INSERT INTO NAMES("
+ "name, label) "
+ "VALUES(?101, ?102);";
+const char *DELETE_COMMAND = "DELETE FROM NAMES WHERE name=?101 AND label=?102;";
+constexpr int TIMES = 100;
+
+CKM::RawBuffer RAW_PASSWORD = createDefaultPass();
+
+class MockCrypto : public CKM::DB::Crypto {
+public:
+ MockCrypto(const std::string &path, const CKM::RawBuffer &rawPass)
+ :Crypto("", path, rawPass) {}
+
+ void countTimePreparedStatements(const char * statement){
+ auto begin = std::chrono::steady_clock::now();
+ for (int i = 0; i < TIMES; ++i) {
+ BOOST_REQUIRE_NO_THROW(m_connection->PrepareDataCommand(statement));
+ }
+ auto end = std::chrono::steady_clock::now();
+
+ std::cout << "Average " << statement << "\npreparation time: "
+ << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / TIMES
+ << "[µs]\n";
+ }
+
+ void countTimePreparedStatementsSteps(const char * statement)
+ {
+ auto begin = std::chrono::steady_clock::now();
+ for (int i = 0; i < TIMES; ++i) {
+ SqlConnection::DataCommandUniquePtr command;
+ std::string str = "name";
+ str += i;
+ BOOST_REQUIRE_NO_THROW(command = m_connection->PrepareDataCommand(statement));
+ BOOST_REQUIRE_NO_THROW(command->BindString(101, str.c_str()));
+ BOOST_REQUIRE_NO_THROW(command->BindString(102, str.c_str()));
+ BOOST_REQUIRE_NO_THROW(command->Step());
+ }
+ auto end = std::chrono::steady_clock::now();
+ std::cout << "Average " << statement << "\npreparation&execution time: "
+ << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / TIMES
+ << "[µs]\n";
+ }
-CKM::RawBuffer raw_password = createDefaultPass();
+};
BOOST_AUTO_TEST_SUITE(SQL_TEST)
POSITIVE_TEST_CASE(sqlTestConversion)
{
- BOOST_REQUIRE_MESSAGE(raw_password.size() == RAW_PASS_SIZE,
- "Password should have 32 characters, got: " << raw_password.size());
- std::string pass_check = rawToHexString(raw_password);
+ BOOST_REQUIRE_MESSAGE(RAW_PASSWORD.size() == RAW_PASS_SIZE,
+ "Password should have 32 characters, got: " << RAW_PASSWORD.size());
+ std::string pass_check = rawToHexString(RAW_PASSWORD);
BOOST_REQUIRE_MESSAGE(pass_check.length() == HEX_PASS_SIZE,
"Hex string should have 64 characters, got: " << pass_check.length());
BOOST_CHECK(pass_check == pattern);
NEGATIVE_TEST_CASE(sqlTestSetKeyTooShort)
{
- using namespace CKM::DB;
- BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
- SqlConnection connection(encrypt_me_not,
+ BOOST_CHECK(unlink(ENCRYPT_ME_NOT) == 0 || errno == ENOENT);
+ SqlConnection connection(ENCRYPT_ME_NOT,
SqlConnection::Flag::CRW);
CKM::RawBuffer wrong_key(RAW_PASS_SIZE - 1, 1);
BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
NEGATIVE_TEST_CASE(sqlTestSetKeyTooLong)
{
- using namespace CKM::DB;
- BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
- SqlConnection connection(encrypt_me_not,
+ BOOST_CHECK(unlink(ENCRYPT_ME_NOT) == 0 || errno == ENOENT);
+ SqlConnection connection(ENCRYPT_ME_NOT,
SqlConnection::Flag::CRW);
CKM::RawBuffer wrong_key(RAW_PASS_SIZE + 1, 1);
BOOST_REQUIRE_THROW(connection.SetKey(wrong_key),
POSITIVE_TEST_CASE(sqlTestConnectionUnencrypted)
{
- using namespace CKM::DB;
- BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT);
+ BOOST_CHECK(unlink(ENCRYPT_ME_NOT) == 0 || errno == ENOENT);
{
- SqlConnection encrypting_you_not(encrypt_me_not,
+ SqlConnection encrypting_you_not(ENCRYPT_ME_NOT,
SqlConnection::Flag::CRW);
- BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(create_table));
- BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(insert_table));
+ BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(CREATE_TABLE));
+ BOOST_REQUIRE_NO_THROW(encrypting_you_not.ExecCommand(INSERT_TABLE));
}
{
- SqlConnection encrypting_you_not(encrypt_me_not,
+ SqlConnection encrypting_you_not(ENCRYPT_ME_NOT,
SqlConnection::Flag::RW);
SqlConnection::DataCommandUniquePtr selectCommand;
BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you_not.
- PrepareDataCommand(select_table));
+ PrepareDataCommand(SELECT_TABLE));
BOOST_REQUIRE_NO_THROW(selectCommand->Step());
std::string value;
BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
POSITIVE_TEST_CASE(sqlTestConnectionEncrypted)
{
- using namespace CKM::DB;
- BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
+ BOOST_CHECK(unlink(ENCRYPT_ME) == 0 || errno == ENOENT);
{
- SqlConnection encrypting_you(encrypt_me,
+ SqlConnection encrypting_you(ENCRYPT_ME,
SqlConnection::Flag::CRW);
- BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
- BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
- BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
+ BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(RAW_PASSWORD));
+ BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(CREATE_TABLE));
+ BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(INSERT_TABLE));
}
{
- SqlConnection encrypting_you(encrypt_me,
+ SqlConnection encrypting_you(ENCRYPT_ME,
SqlConnection::Flag::RW);
- encrypting_you.SetKey(raw_password);
+ encrypting_you.SetKey(RAW_PASSWORD);
SqlConnection::DataCommandUniquePtr selectCommand;
BOOST_REQUIRE_NO_THROW(selectCommand = encrypting_you.
- PrepareDataCommand(select_table));
+ PrepareDataCommand(SELECT_TABLE));
BOOST_REQUIRE_NO_THROW(selectCommand->Step());
std::string value;
BOOST_REQUIRE_NO_THROW(value = selectCommand->GetColumnString(0));
NEGATIVE_TEST_CASE(sqlTestConnectionEncryptedNegative)
{
- using namespace CKM::DB;
- BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT);
+ BOOST_CHECK(unlink(ENCRYPT_ME) == 0 || errno == ENOENT);
{
- SqlConnection encrypting_you(encrypt_me,
+ SqlConnection encrypting_you(ENCRYPT_ME,
SqlConnection::Flag::CRW);
- BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(raw_password));
- BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(create_table));
- BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(insert_table));
+ BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(RAW_PASSWORD));
+ BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(CREATE_TABLE));
+ BOOST_REQUIRE_NO_THROW(encrypting_you.ExecCommand(INSERT_TABLE));
}
{
- SqlConnection encrypting_you(encrypt_me,
+ SqlConnection encrypting_you(ENCRYPT_ME,
SqlConnection::Flag::RW);
CKM::RawBuffer wrong_password;
for (std::size_t i = 0; i < RAW_PASS_SIZE; i++)
- wrong_password.push_back(raw_password[i] + 1);
+ wrong_password.push_back(RAW_PASSWORD[i] + 1);
BOOST_REQUIRE_NO_THROW(encrypting_you.SetKey(wrong_password));
SqlConnection::DataCommandUniquePtr selectCommand;
BOOST_REQUIRE_THROW(selectCommand = encrypting_you.PrepareDataCommand(
- select_table),
+ SELECT_TABLE),
SqlConnection::Exception::SyntaxError);
}
}
+POSITIVE_TEST_CASE(sqlTestEfficiency)
+{
+ MockCrypto mc(TEST_DB_CKM, RAW_PASSWORD);
+
+ mc.countTimePreparedStatements(INSERT_COMMAND);
+ mc.countTimePreparedStatements(SELECT_COMMAND);
+ mc.countTimePreparedStatements(DELETE_COMMAND);
+ mc.countTimePreparedStatementsSteps(INSERT_COMMAND);
+ mc.countTimePreparedStatementsSteps(SELECT_COMMAND);
+ mc.countTimePreparedStatementsSteps(DELETE_COMMAND);
+}
+
BOOST_AUTO_TEST_SUITE_END()
#pragma GCC diagnostic pop