Fix groups issue in tests using perm_app_set_privilege api.
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / common / libprivilege-control_test_common.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        test_cases.cpp
19  * @author      Zofia Abramowska (z.abramowska@samsung.com)
20  * @author      Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
21  * @version     1.0
22  * @brief       libprivilege-control tests commons
23  */
24
25 #ifndef LIBPRIVILEGE_CONTROL_TEST_COMMON_H_
26 #define LIBPRIVILEGE_CONTROL_TEST_COMMON_H_
27
28 #include <memory>
29 #include <vector>
30 #include <string>
31 #include <set>
32 #include <ftw.h>
33 #include <privilege-control.h>
34 #include <tests_common.h>
35 #include <unistd.h>
36
37 // How many open file descriptors should ftw() function use?
38 #define FTW_MAX_FDS 16
39
40 #define SOCK_PATH "/tmp/test-smack-socket"
41
42 #define TEST_APP_DIR             "/etc/smack/test_privilege_control_DIR/app_dir"
43 #define TEST_NON_APP_DIR         "/etc/smack/test_privilege_control_DIR/non_app_dir"
44
45 #define APP_ID                 "test_APP"
46 #define APPID_DIR                "test_APP_ID_dir"
47 #define APPID_SHARED_DIR         "test_APP_ID_shared_dir"
48
49 #define APP_1     "app_1"
50 #define APP_1_DIR "/tmp/app_1"
51
52 #define APP_2     "app_2"
53 #define APP_2_DIR "/tmp/app_2"
54
55 #define APP_TEST "app_test"
56
57 #define EFL_APP_ID             "hello-tizen"
58
59 #define LIBPRIVILEGE_TEST_DAC_FILE_WGT "/usr/share/privilege-control/WRT_test_privilege_control_rules_wgt.dac"
60 #define LIBPRIVILEGE_TEST_DAC_FILE_OSP "/usr/share/privilege-control/OSP_test_privilege_control_rules_osp.dac"
61 #define LIBPRIVILEGE_TEST_DAC_FILE_EFL "/usr/share/privilege-control/EFL_test_privilege_control_rules_efl.dac"
62
63 #define OSP_APP_ID            "uqNfgEjqc7"
64
65 #define WGT_APP_PATH          "/opt/usr/apps/QwCqJ0ttyS/bin/QwCqJ0ttyS.TestMisiuPysiu123"
66 #define OSP_APP_PATH          "/opt/usr/apps/uqNfgEjqc7/bin/PysiuMisiu123Osp"
67 #define EFL_APP_PATH          "/usr/bin/hello-tizen"
68
69 #define APP_SET_PRIV_PATH      "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP"
70
71 const std::string RDB_PATH("/opt/dbspace/.rules-db.db3");
72 const std::string RDB_PATH_BACKUP("/opt/dbspace/.rules-db.db3.backup");
73
74 extern const char *PRIVS1[];
75 extern const char *PRIVS2[];
76 extern const char *PRIVS2_NO_R[];
77 extern const char *PRIVS2_R[];
78 extern const char *PRIVS2_R_AND_NO_R[];
79
80 extern const char *PRIVS_WGT[];
81 extern const char *PRIVS_OSP[];
82 extern const char *PRIVS_EFL[];
83
84 extern const char *PRIV_APPSETTING[];
85 extern const char *PRIV_APPSETTING_RULES[];
86
87 typedef std::vector< std::vector<std::string> > rules_t;
88
89 struct free_deleter {
90     void operator()(void* p) {
91         free(p);
92     }
93 };
94 typedef std::unique_ptr<char, free_deleter> CStringPtr;
95
96 // Rules from WRT_test_privilege_control_rules1.smack for wgt
97 const rules_t rules1 = {
98     { WGT_APP_ID, "test_book_1", "r" },
99     { WGT_APP_ID, "test_book_2", "w" },
100     { WGT_APP_ID, "test_book_3", "x" },
101     { WGT_APP_ID, "test_book_4", "rw" },
102     { WGT_APP_ID, "test_book_5", "rx" },
103     { WGT_APP_ID, "test_book_6", "wx" },
104     { WGT_APP_ID, "test_book_7", "rwx" },
105     { "test_subject_1", WGT_APP_ID, "r" },
106     { "test_subject_2", WGT_APP_ID, "w" },
107     { "test_subject_3", WGT_APP_ID, "x" },
108     { "test_subject_4", WGT_APP_ID, "rw" },
109     { "test_subject_5", WGT_APP_ID, "rx" },
110     { "test_subject_6", WGT_APP_ID, "wx" },
111     { "test_subject_7", WGT_APP_ID, "rwx" }
112 };
113
114 // Rules from WRT_test_privilege_control_rules2.smack
115 const rules_t rules2 = {
116     { WGT_APP_ID, "test_book_8", "r" },
117     { WGT_APP_ID, "test_book_9", "w" },
118     { WGT_APP_ID, "test_book_10", "x" },
119     { WGT_APP_ID, "test_book_11", "rw" },
120     { WGT_APP_ID, "test_book_12", "rx" },
121     { WGT_APP_ID, "test_book_13", "wx" },
122     { WGT_APP_ID, "test_book_14", "rwx" },
123     { WGT_APP_ID, "test_book_15", "rwxat" },
124     { "test_subject_8", WGT_APP_ID, "r" },
125     { "test_subject_9", WGT_APP_ID, "w" },
126     { "test_subject_10", WGT_APP_ID, "x" },
127     { "test_subject_11", WGT_APP_ID, "rw" },
128     { "test_subject_12", WGT_APP_ID, "rx" },
129     { "test_subject_13", WGT_APP_ID, "wx" },
130     { "test_subject_14", WGT_APP_ID, "rwx" },
131     { "test_subject_15", WGT_APP_ID, "rwxat" }
132 };
133
134 // Rules from WRT_test_privilege_control_rules_no_r.smack
135 const rules_t rules2_no_r = {
136     { WGT_APP_ID, "test_book_9", "w" },
137     { WGT_APP_ID, "test_book_10", "x" },
138     { WGT_APP_ID, "test_book_11", "w" },
139     { WGT_APP_ID, "test_book_12", "x" },
140     { WGT_APP_ID, "test_book_13", "x" },
141     { WGT_APP_ID, "test_book_14", "wx" },
142     { WGT_APP_ID, "test_book_15", "wxat" },
143     { "test_subject_9", WGT_APP_ID, "w" },
144     { "test_subject_10", WGT_APP_ID, "x" },
145     { "test_subject_11", WGT_APP_ID, "w" },
146     { "test_subject_12", WGT_APP_ID, "x" },
147     { "test_subject_13", WGT_APP_ID, "x" },
148     { "test_subject_14", WGT_APP_ID, "wx" },
149     { "test_subject_15", WGT_APP_ID, "wxat" }
150 };
151
152 // Rules from test_privilege_control_rules.smack
153 // minus WRT_test_privilege_control_rules_no_r.smack
154 const rules_t rules2_r = {
155     { WGT_APP_ID, "test_book_8", "r" },
156     { WGT_APP_ID, "test_book_11", "r" },
157     { WGT_APP_ID, "test_book_12", "r" },
158     { WGT_APP_ID, "test_book_14", "r" },
159     { WGT_APP_ID, "test_book_15", "r" },
160     { "test_subject_8", WGT_APP_ID, "r" },
161     { "test_subject_11", WGT_APP_ID, "r" },
162     { "test_subject_12", WGT_APP_ID, "r" },
163     { "test_subject_14", WGT_APP_ID, "r" },
164     { "test_subject_15", WGT_APP_ID, "r" }
165 };
166
167 // Rules from EFL_test_privilege_control_rules_efl.smack for rpm
168 const rules_t rules_efl = {
169     { EFL_APP_ID, "test_book_efl", "r" }
170 };
171
172 // Rules from WRT_test_privilege_control_rules_wgt.smack for wgt
173 const rules_t rules_wgt = {
174     { WGT_APP_ID, "test_book_8", "r" },
175     { WGT_APP_ID, "test_book_9", "w" },
176     { WGT_APP_ID, "test_book_10", "x" },
177     { WGT_APP_ID, "test_book_11", "rw" },
178     { WGT_APP_ID, "test_book_12", "rx" },
179     { WGT_APP_ID, "test_book_13", "wx" },
180     { WGT_APP_ID, "test_book_14", "rwx" },
181     { WGT_APP_ID, "test_book_15", "rwxat" },
182     { "test_subject_8", WGT_APP_ID, "r" },
183     { "test_subject_9", WGT_APP_ID, "w" },
184     { "test_subject_10", WGT_APP_ID, "x" },
185     { "test_subject_11", WGT_APP_ID, "rw" },
186     { "test_subject_12", WGT_APP_ID, "rx" },
187     { "test_subject_13", WGT_APP_ID, "wx" },
188     { "test_subject_14", WGT_APP_ID, "rwx" },
189     { "test_subject_15", WGT_APP_ID, "rwxat" }
190 };
191
192 // Rules from OSP_test_privilege_control_rules_osp.smack for osp
193 const rules_t rules_osp = {
194     { OSP_APP_ID, "test_book_8", "r" },
195     { OSP_APP_ID, "test_book_9", "w" },
196     { OSP_APP_ID, "test_book_10", "x" },
197     { OSP_APP_ID, "test_book_11", "rw" },
198     { OSP_APP_ID, "test_book_12", "rx" },
199     { OSP_APP_ID, "test_book_13", "wx" },
200     { OSP_APP_ID, "test_book_14", "rwx" },
201     { OSP_APP_ID, "test_book_15", "rwxat" },
202     { "test_subject_8", OSP_APP_ID, "r" },
203     { "test_subject_9", OSP_APP_ID, "w" },
204     { "test_subject_10", OSP_APP_ID, "x" },
205     { "test_subject_11", OSP_APP_ID, "rw" },
206     { "test_subject_12", OSP_APP_ID, "rx" },
207     { "test_subject_13", OSP_APP_ID, "wx" },
208     { "test_subject_14", OSP_APP_ID, "rwx" },
209     { "test_subject_15", OSP_APP_ID, "rwxat" }
210 };
211
212 int test_have_all_accesses(const rules_t &rules);
213 int test_have_any_accesses(const rules_t &rules);
214 int test_have_nosmack_accesses(const rules_t &rules);
215
216 void read_user_gids(std::set<unsigned> &set, const uid_t user_id);
217 void check_groups(const std::set<unsigned> &groups_prev, const char *dac_file);
218
219 int file_exists(const char *path);
220 void check_app_installed(const char *app_path);
221
222 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
223                        int /*typeflag*/, struct FTW* /*ftwbuf*/);
224 int nftw_check_labels_app_dir(const char *fpath, const struct stat *sb,
225                               int /*typeflag*/, struct FTW* /*ftwbuf*/);
226 int nftw_set_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
227                                 int /*typeflag*/, struct FTW* /*ftwbuf*/);
228 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
229                                   int /*typeflag*/, struct FTW* /*ftwbuf*/);
230
231 void test_revoke_permissions(int line_no, const char* app_id, const rules_t &rules, bool smack);
232 void test_app_enable_permissions_efl(bool smack);
233 void test_app_disable_permissions_efl(bool smack);
234 void test_app_disable_permissions(bool smack);
235 void test_appsettings_privilege(bool smack);
236 bool check_all_accesses(bool smack, const rules_t &rules);
237 bool check_no_accesses(bool smack, const rules_t &rules);
238
239 #endif /* LIBPRIVILEGE_CONTROL_TEST_COMMON_H_ */