Sqlite3 support for tests. Libprivilege tests: check database
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / common / db_sqlite.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15 */
16
17 /*
18  * @file        libprivilege-control_test_db_sqlite.cpp
19  * @author      Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
20  * @version     1.0
21  * @brief       libprivilege-control tests API for sqlite3 database access
22  */
23
24 #include <dpl/test/test_runner.h>
25 #include "db_sqlite.h"
26
27 Sqlite3DBase::Sqlite3DBase(const std::string& db_path, int flags)
28     : m_db_handle(NULL), m_db_path(db_path), m_flags(flags)
29 {
30 }
31
32 Sqlite3DBase::~Sqlite3DBase()
33 {
34     sqlite3_close(m_db_handle);
35 }
36
37 #define VFS_NOT_USED    NULL
38
39 void Sqlite3DBase::open(const Tracker& tracker)
40 {
41     if(m_db_handle) //database already opened
42         return;
43
44     int ret = sqlite3_open_v2(m_db_path.c_str(), &m_db_handle, m_flags, VFS_NOT_USED);
45     RUNNER_ASSERT_MSG(m_db_handle,
46                       "DB: Error testing \"" << tracker.str() <<
47                       "\" Sqlite3DBase: Error opening the database: Unable to allocate memory.");
48     RUNNER_ASSERT_MSG(ret == SQLITE_OK,
49                       "DB: Error testing \"" << tracker.str() <<
50                       "\" Sqlite3DBase: Error opening the database: " <<
51                       sqlite3_errmsg(m_db_handle));
52 }
53
54 void Sqlite3DBase::close(const Tracker& tracker)
55 {
56     int ret = sqlite3_close(m_db_handle);
57     RUNNER_ASSERT_MSG(ret == SQLITE_OK,
58                       "DB: Error testing \"" << tracker.str() <<
59                       "\" Sqlite3DBase: Error closing the database: " <<
60                       sqlite3_errmsg(m_db_handle));
61
62     m_db_handle = NULL;
63 }
64
65 bool Sqlite3DBase::is_open(void) const
66 {
67     return !!m_db_handle;
68 }
69
70 void Sqlite3DBase::execute(const Tracker& tracker, const std::string& sql_querry,
71     Sqlite3DBaseSelectResult& result)
72 {
73     char* tmp = NULL;
74     std::string errmsg;
75
76     int ret = sqlite3_exec(m_db_handle, sql_querry.c_str(), callback, &result, &tmp);
77     if(tmp) {
78         errmsg.assign(tmp);
79     }
80     sqlite3_free(tmp);
81
82     RUNNER_ASSERT_MSG(ret == SQLITE_OK || ret == SQLITE_ABORT,
83                       "DB: Error testing \"" << tracker.str() <<
84                       "\" Sqlite3DBase: Error executing statement <" << sql_querry << "> : " <<
85                       errmsg);
86 }
87
88 int Sqlite3DBase::callback(void* p_result, int cols, char** data, char** header)
89 {
90     int i;
91     Sqlite3DBaseSelectResult* result = static_cast<Sqlite3DBaseSelectResult*>(p_result);
92
93     // if this is first record get column names
94     if(result->rows.empty()) {
95         for(i = 0; i < cols; ++i) {
96             result->header.push_back(header[i] ? header[i] : "");
97         }
98     }
99     result->rows.push_back(Sqlite3Row());
100     for(i = 0; i < cols; ++i) {
101         result->rows.back().push_back(data[i] ? data[i] : "");
102     }
103     return SQLITE_OK;
104 }