21a419a9bd0fe81f81740b3897655ccf1f71e3ff
[platform/core/test/security-tests.git] / tests / security-manager-tests / common / sm_db.h
1 /*
2  * Copyright (c) 2014 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        sm_db.h
19  * @author      Marcin Lis (m.lis@samsung.com)
20  * @version     1.0
21  * @brief       security-manager tests database record check functions
22  */
23
24 #ifndef SECURITY_MANAGER_TEST_DB_H_
25 #define SECURITY_MANAGER_TEST_DB_H_
26
27 #include <string>
28 #include "db_sqlite.h"
29
30 typedef std::vector<std::string> privileges_t;
31
32 /**
33  * @class       TestSecurityManagerDatabase
34  * @brief       Class containing methods for testing libprivlege database.
35  */
36 class TestSecurityManagerDatabase
37 {
38 public:
39 /**
40  * @brief A usefull constant to indicate that app/pkg should be present in db
41  */
42     const static bool NOT_REMOVED;
43 /**
44  * @brief A usefull constant to indicate that app/pkg should not be present in db
45  */
46     const static bool REMOVED;
47 /**
48  * @brief A constructor
49  */
50     TestSecurityManagerDatabase();
51
52 /**
53  * @brief A destructor
54  */
55     ~TestSecurityManagerDatabase() = default;
56
57 /**
58  * @brief Method for testing database after "security_manager_app_install" was run.
59  *
60  * It checks existence of proper: - app_name
61  *                                - pkg_name
62  *
63  * @param  app_name           name of the app previously used in security_manager_app_install.
64  * @param  pkg_name           name of the pkg previously used in security_manager_app_install.
65  */
66     void test_db_after__app_install(const std::string &app_name, const std::string &pkg_name);
67
68 /**
69  * @brief Method for testing database after "security_manager_app_install" was run.
70  *
71  * It checks existence of proper: - app_name
72  *                                - pkg_name
73  *                                - privileges
74  * TODO: appPaths are currently not handled directly by security-manager, so they are not tested.
75  *
76  * @param  app_name           name of the app previously used in security_manager_app_install.
77  * @param  pkg_name           name of the pkg previously used in security_manager_app_install.
78  * @param  privileges         vector of privileges previously used in security_manager_app_install.
79  */
80     void test_db_after__app_install(const std::string &app_name, const std::string &pkg_name,
81                                     const privileges_t &privileges);
82
83 /**
84  * @brief Method for testing database after "security_manager_app_uninstall" was run.
85  *
86  * It checks absence of proper: - app_name
87  *                              - optionally pkg_name
88  *
89  * @param  app_name           name of the app previously used in security_manager_app_uninstall.
90  * @param  pkg_name           name of the pkg previously used in security_manager_app_uninstall.
91  * @param  is_pkg_removed     tells if pkg_id is expected to remain in db or not.
92  */
93     void test_db_after__app_uninstall(const std::string &app_name, const std::string &pkg_name,
94                                       const bool is_pkg_removed);
95
96 /**
97  * @brief Method for testing database after "security_manager_app_uninstall" was run.
98  *
99  * It checks absence of proper: - app_name
100  *                              - optionally pkg_name
101  *                              - app privileges
102  * TODO: appPaths are currently not handled directly by security-manager, so they are not tested.
103  *
104  * @param  app_name           name of the app previously used in security_manager_app_uninstall.
105  * @param  pkg_name           name of the pkg previously used in security_manager_app_uninstall.
106  * @param  privileges         vector of privileges previously used in security_manager_app_uninstall.
107  * @param  is_pkg_removed     tells if pkg_id is expected to remain in db or not.
108  */
109     void test_db_after__app_uninstall(const std::string &app_name, const std::string &pkg_name,
110                                       const privileges_t &privileges, const bool is_pkg_removed);
111
112 /**
113  * @brief It checks db for existence of a all privileges from install request.
114  *
115  * @param  app_name           name of the app previously used i.e. in security_manager_app_install.
116  * @param  pkg_name           name of the pkg previously used i.e. in security_manager_app_install.
117  * @param  privileges         vector of privileges previously used i.e. in security_manager_app_install.
118  */
119     void check_privileges(const std::string &app_name, const std::string &pkg_name,
120                           const privileges_t &privileges);
121
122 /**
123  * @brief It checks in db if all app privileges from install request are removed.
124  *
125  * @param  app_name           name of the app previously used i.e. in security_manager_app_uninstall.
126  * @param  pkg_name           name of the pkg previously used i.e. in security_manager_app_uninstall.
127  * @param  privileges         vector of privileges previously used i.e. in security_manager_app_uninstall.
128  */
129     void check_privileges_removed(const std::string &app_name, const std::string &pkg_name,
130                                   const privileges_t &privileges);
131
132 private:
133 /**
134  * @var base
135  * @brief Sqlite3DBase object giving simple access to database
136  *
137  * Connection to database is open first time it is needed
138  * and closed in destructor of TestSecurityManagerDatabase.
139  */
140     Sqlite3DBase m_base;
141
142 /**
143  * @brief Check db for [non]existence of given app_name in pkg_name
144  *
145  * @param  app_name        name of application
146  * @param  pkg_name        name of package
147  * @param  is_app_removed  tells if app is expected in db
148  */
149     void check_app_and_pkg(const std::string &app_name, const std::string &pkg_name,
150                            const bool is_app_removed);
151
152 /**
153  * @brief Check db for [non]existence of given pkg_name
154  *
155  * @param  pkg_name        name of the package
156  * @param  is_pkg_removed  tells if pkg is expected in db
157  */
158     void check_pkg(const std::string &pkg_name,
159                    const bool is_pkg_removed);
160
161 /**
162  * @brief Check db for existence of a single privilege.
163  *
164  * @param  app_name        name of application
165  * @param  pkg_name        application's package name
166  * @param  privilege       name of the privilege
167  *
168  * @return true            when privilege present
169  *         false           when privilege not present
170  */
171     bool check_privilege(const std::string &app_name, const std::string &pkg_name,
172                          const std::string &privilege);
173 };
174
175 #endif /* SECURITY_MANAGER_TEST_DB_H_ */