Sqlite3 support for tests. Libprivilege tests: check database
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / common / db_sqlite.h
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.h
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 #ifndef LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_
25 #define LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_
26
27 #include <vector>
28 #include <string>
29 #include <sqlite3.h>
30 #include <tracker.h>
31
32 /**
33  * @def         DB_SQLITE_READWRITE_FLAG
34  * @brief       Sqlite3 flag set for opening database in RW mode
35  */
36 #define DB_SQLITE_READWRITE_FLAG      SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE \
37                                       | SQLITE_OPEN_READWRITE
38
39 /**
40  * @def         DB_SQLITE_READONLY_FLAG
41  * @brief       Sqlite3 flag set for opening database in RO mode
42  */
43 #define DB_SQLITE_READONLY_FLAG       SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE \
44                                       | SQLITE_OPEN_READONLY
45
46 /**
47  * @typedef     Sqlite3HeaderName
48  * @brief       Holds single column name of sqlite select querry response.
49  */
50 typedef std::string Sqlite3HeaderName;
51
52 /**
53  * @typedef     Sqlite3HeaderNameVector
54  * @brief       Holds column names vector of sqlite select querry response.
55  */
56 typedef std::vector<Sqlite3HeaderName> Sqlite3HeaderNameVector;
57
58 /**
59  * @typedef     Sqlite3RowCell
60  * @brief       Holds single cell of row of sqlite select querry response.
61  */
62 typedef std::string Sqlite3RowCell;
63
64 /**
65  * @typedef     Sqlite3Row
66  * @brief       Holds single row of sqlite select querry response.
67  */
68 typedef std::vector<Sqlite3RowCell> Sqlite3Row;
69
70 /**
71  * @typedef     Sqlite3RowVector
72  * @brief       Holds multiple rows of sqlite select querry response.
73  */
74 typedef std::vector<Sqlite3Row> Sqlite3RowVector;
75
76 /**
77  * @class       Sqlite3DBaseSelectResult
78  * @brief       Sqlite3 select querry response.
79  *
80  * Fields are public as there is no complicated logic to operate on them
81  */
82 struct Sqlite3DBaseSelectResult
83 {
84 /**
85  * @var         header
86  * @brief       Sqlite select querry column names
87  */
88     Sqlite3HeaderNameVector header;
89
90 /**
91  * @var         rows
92  * @brief       Sqlite select querry rows vector
93  */
94     Sqlite3RowVector rows;
95 };
96
97
98 /**
99  * @class       Sqlite3DBase
100  * @brief       Simple interface for executing select statements on sqlite3 database
101  *
102  * Fields are public as there is no complicated logic to operate on them
103  */
104 class Sqlite3DBase
105 {
106 public:
107 /**
108  * @brief A constructor
109  *
110  * @param db_path       path to database file
111  * @param flags         sqlite3 flags defining database opening mode
112  *                      (default value DB_SQLITE_READONLY_FLAG)
113  */
114     Sqlite3DBase(const std::string& db_path, int flags = DB_SQLITE_READONLY_FLAG);
115
116 /**
117  * @brief A destructor
118  */
119     ~Sqlite3DBase();
120
121 /**
122  * @brief Open database.
123  *
124  * If database is already opened do nothing.
125  *
126  * @param tracker       make assertion comments if error more useful
127  *
128  * @throw DPL::Test::TestRunner::TestFailed    when opening database fails
129  */
130     void open(const Tracker& tracker);
131
132 /**
133  * @brief Close database.
134  *
135  * @param tracker       make assertion comments if error more useful
136  *
137  * @throw DPL::Test::TestRunner::TestFailed    when closing database fails
138  */
139     void close(const Tracker& tracker);
140
141 /**
142  * @brief Get database connection status.
143  *
144  * @return      true     if database is open
145  *              false    if database is closed
146  */
147     bool is_open(void) const;
148
149 /**
150  * @brief Execute SQL querry on database
151  *
152  * @param tracker       make assertion comments if error more useful
153  * @param sql_querry    SQL querry
154  * @param result        returned result
155  *
156  * @throw DPL::Test::TestRunner::TestFailed    when execution of querry fails
157  */
158     void execute(const Tracker& tracker, const std::string& sql_querry,
159         Sqlite3DBaseSelectResult& result);
160
161 private:
162 /**
163  * @var db_handle
164  * @brief Handle to sqlite3 database
165  *
166  * NULL when database not opened.
167  */
168     sqlite3* m_db_handle;
169
170 /**
171  * @var db_path
172  * @brief Path to database file
173  */
174     std::string m_db_path;
175
176 /**
177  * @var flags
178  * @brief Sqlite3 flags defining database opening mode
179  */
180     int m_flags;
181
182 /**
183  * @brief Callback used to aquire results from SQL query
184  *
185  * It is run by sqlite for every row in query result.
186  *
187  * @param p_result      pointer to private data (Sqlite3DBaseSelectResult)
188  * @param cols          number of columns in SQL result
189  * @param data          array of strings containing single row
190  * @param header        array of strings containing column names
191  * @return SQLITE_OK    as we always know what to do with data
192  */
193     static int callback(void* p_result, int cols, char** data, char** header);
194 };
195
196 #endif /* LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_ */