security-manager: test security_manager_set_process_groups_from_appid
[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 /**
133  * @brief Method for setting privilege to groups mapping in security-manager database
134  *
135  * @param privilege name of the privilege
136  * @param gids vector of group ids
137  */
138     void setup_privilege_gids(const std::string &privilege, const std::vector<gid_t> &gids);
139
140 private:
141 /**
142  * @var base
143  * @brief Sqlite3DBase object giving simple access to database
144  *
145  * Connection to database is open first time it is needed
146  * and closed in destructor of TestSecurityManagerDatabase.
147  */
148     Sqlite3DBase m_base;
149
150 /**
151  * @brief Check db for [non]existence of given app_name in pkg_name
152  *
153  * @param  app_name        name of application
154  * @param  pkg_name        name of package
155  * @param  is_app_removed  tells if app is expected in db
156  */
157     void check_app_and_pkg(const std::string &app_name, const std::string &pkg_name,
158                            const bool is_app_removed);
159
160 /**
161  * @brief Check db for [non]existence of given pkg_name
162  *
163  * @param  pkg_name        name of the package
164  * @param  is_pkg_removed  tells if pkg is expected in db
165  */
166     void check_pkg(const std::string &pkg_name,
167                    const bool is_pkg_removed);
168
169 /**
170  * @brief Check db for existence of a single privilege.
171  *
172  * @param  app_name        name of application
173  * @param  pkg_name        application's package name
174  * @param  privilege       name of the privilege
175  *
176  * @return true            when privilege present
177  *         false           when privilege not present
178  */
179     bool check_privilege(const std::string &app_name, const std::string &pkg_name,
180                          const std::string &privilege);
181 };
182
183 #endif /* SECURITY_MANAGER_TEST_DB_H_ */