f7251f5f718290b3981df3f22a5b8ab38db67f64
[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 <vector>
29 #include <string>
30 #include <set>
31 #include <ftw.h>
32 #include <privilege-control.h>
33 #include <tests_common.h>
34 #include <unistd.h>
35
36 // How many open file descriptors should ftw() function use?
37 #define FTW_MAX_FDS 16
38
39 #define SOCK_PATH "/tmp/test-smack-socket"
40
41 #define TEST_APP_DIR             "/etc/smack/test_privilege_control_DIR/app_dir"
42 #define TEST_NON_APP_DIR         "/etc/smack/test_privilege_control_DIR/non_app_dir"
43
44 #define APP_ID                 "test_APP"
45 #define APPID_DIR              "test_APP_ID_dir"
46 #define GENERATED_APP_ID       "User" // TODO to be replaced in the future
47
48 #define PERM_TO_REDEFINE       "Test::RedefinePermission"
49 #define PERM_SUB_TO_REDEFINE   "Test::RedefinePermission::Sub"
50
51 #define APP_1     "app_1"
52 #define APP_1_DIR "/tmp/app_1"
53
54 #define APP_2     "app_2"
55 #define APP_2_DIR "/tmp/app_2"
56
57 #define APP_TEST "app_test"
58
59 #define EFL_APP_ID             "hello-tizen"
60
61 #define LIBPRIVILEGE_TEST_DAC_FILE_WGT "/usr/share/privilege-control/WRT_test_privilege_control_rules_wgt.dac"
62 #define LIBPRIVILEGE_TEST_DAC_FILE_OSP "/usr/share/privilege-control/OSP_test_privilege_control_rules_osp.dac"
63 #define LIBPRIVILEGE_TEST_DAC_FILE_EFL "/usr/share/privilege-control/EFL_test_privilege_control_rules_efl.dac"
64
65 #define OSP_APP_ID            "uqNfgEjqc7"
66
67 #define WGT_APP_PATH          "/usr/bin/test-app-wgt"
68 #define OSP_APP_PATH          "/usr/bin/test-app-osp"
69 #define EFL_APP_PATH          "/usr/bin/test-app-efl"
70
71 #define APP_SET_PRIV_PATH      "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP"
72
73 extern const char *USER_APP_ID;
74
75 const std::string RDB_PATH("/opt/dbspace/.rules-db.db3");
76 const std::string RDB_PATH_BACKUP("/opt/dbspace/.rules-db.db3.backup");
77
78 extern const char *PRIVS1[];
79 extern const char *PRIVS2[];
80 extern const char *PRIVS2_NO_R[];
81 extern const char *PRIVS2_R[];
82 extern const char *PRIVS2_R_AND_NO_R[];
83
84 extern const char *PRIVS_WGT[];
85 extern const char *PRIVS_OSP[];
86 extern const char *PRIVS_EFL[];
87
88 extern const char *PRIV_APPSETTING[];
89 extern const char *PRIV_APPSETTING_RULES[];
90
91 typedef std::vector< std::vector<std::string> > rules_t;
92
93 // Rules from WRT_test_privilege_control_rules1.smack for wgt
94 const rules_t rules1 = {
95     { USER_APP_ID, "test_book_1", "r" },
96     { USER_APP_ID, "test_book_2", "w" },
97     { USER_APP_ID, "test_book_3", "x" },
98     { USER_APP_ID, "test_book_4", "rw" },
99     { USER_APP_ID, "test_book_5", "rx" },
100     { USER_APP_ID, "test_book_6", "wx" },
101     { USER_APP_ID, "test_book_7", "rwx" },
102     { "test_subject_1", USER_APP_ID, "r" },
103     { "test_subject_2", USER_APP_ID, "w" },
104     { "test_subject_3", USER_APP_ID, "x" },
105     { "test_subject_4", USER_APP_ID, "rw" },
106     { "test_subject_5", USER_APP_ID, "rx" },
107     { "test_subject_6", USER_APP_ID, "wx" },
108     { "test_subject_7", USER_APP_ID, "rwx" }
109 };
110
111 // Rules from WRT_test_privilege_control_rules2.smack
112 const rules_t rules2 = {
113     { USER_APP_ID, "test_book_8", "r" },
114     { USER_APP_ID, "test_book_9", "w" },
115     { USER_APP_ID, "test_book_10", "x" },
116     { USER_APP_ID, "test_book_11", "rw" },
117     { USER_APP_ID, "test_book_12", "rx" },
118     { USER_APP_ID, "test_book_13", "wx" },
119     { USER_APP_ID, "test_book_14", "rwx" },
120     { USER_APP_ID, "test_book_15", "rwxat" },
121     { "test_subject_8", USER_APP_ID, "r" },
122     { "test_subject_9", USER_APP_ID, "w" },
123     { "test_subject_10", USER_APP_ID, "x" },
124     { "test_subject_11", USER_APP_ID, "rw" },
125     { "test_subject_12", USER_APP_ID, "rx" },
126     { "test_subject_13", USER_APP_ID, "wx" },
127     { "test_subject_14", USER_APP_ID, "rwx" },
128     { "test_subject_15", USER_APP_ID, "rwxat" }
129 };
130
131 // Rules from WRT_test_privilege_control_rules_no_r.smack
132 const rules_t rules2_no_r = {
133     { USER_APP_ID, "test_book_9", "w" },
134     { USER_APP_ID, "test_book_10", "x" },
135     { USER_APP_ID, "test_book_11", "w" },
136     { USER_APP_ID, "test_book_12", "x" },
137     { USER_APP_ID, "test_book_13", "x" },
138     { USER_APP_ID, "test_book_14", "wx" },
139     { USER_APP_ID, "test_book_15", "wxat" },
140     { "test_subject_9", USER_APP_ID, "w" },
141     { "test_subject_10", USER_APP_ID, "x" },
142     { "test_subject_11", USER_APP_ID, "w" },
143     { "test_subject_12", USER_APP_ID, "x" },
144     { "test_subject_13", USER_APP_ID, "x" },
145     { "test_subject_14", USER_APP_ID, "wx" },
146     { "test_subject_15", USER_APP_ID, "wxat" }
147 };
148
149 // Rules from test_privilege_control_rules.smack
150 // minus WRT_test_privilege_control_rules_no_r.smack
151 const rules_t rules2_r = {
152     { USER_APP_ID, "test_book_8", "r" },
153     { USER_APP_ID, "test_book_11", "r" },
154     { USER_APP_ID, "test_book_12", "r" },
155     { USER_APP_ID, "test_book_14", "r" },
156     { USER_APP_ID, "test_book_15", "r" },
157     { "test_subject_8", USER_APP_ID, "r" },
158     { "test_subject_11", USER_APP_ID, "r" },
159     { "test_subject_12", USER_APP_ID, "r" },
160     { "test_subject_14", USER_APP_ID, "r" },
161     { "test_subject_15", USER_APP_ID, "r" }
162 };
163
164 // Rules from EFL_test_privilege_control_rules_efl.smack for rpm
165 const rules_t rules_efl = {
166     { USER_APP_ID, "test_book_efl", "r" }
167 };
168
169 // Rules from WRT_test_privilege_control_rules_wgt.smack for wgt
170 const rules_t rules_wgt = {
171     { USER_APP_ID, "test_book_wgt_8", "r" },
172     { USER_APP_ID, "test_book_wgt_9", "w" },
173     { USER_APP_ID, "test_book_wgt_10", "x" },
174     { USER_APP_ID, "test_book_wgt_11", "rw" },
175     { USER_APP_ID, "test_book_wgt_12", "rx" },
176     { USER_APP_ID, "test_book_wgt_13", "wx" },
177     { USER_APP_ID, "test_book_wgt_14", "rwx" },
178     { USER_APP_ID, "test_book_wgt_15", "rwxat" },
179     { "test_subject_wgt_8", USER_APP_ID, "r" },
180     { "test_subject_wgt_9", USER_APP_ID, "w" },
181     { "test_subject_wgt_10", USER_APP_ID, "x" },
182     { "test_subject_wgt_11", USER_APP_ID, "rw" },
183     { "test_subject_wgt_12", USER_APP_ID, "rx" },
184     { "test_subject_wgt_13", USER_APP_ID, "wx" },
185     { "test_subject_wgt_14", USER_APP_ID, "rwx" },
186     { "test_subject_wgt_15", USER_APP_ID, "rwxat" }
187 };
188
189 // Rules from OSP_test_privilege_control_rules_osp.smack for osp
190 const rules_t rules_osp = {
191     { USER_APP_ID, "test_book_osp_8", "r" },
192     { USER_APP_ID, "test_book_osp_9", "w" },
193     { USER_APP_ID, "test_book_osp_10", "x" },
194     { USER_APP_ID, "test_book_osp_11", "rw" },
195     { USER_APP_ID, "test_book_osp_12", "rx" },
196     { USER_APP_ID, "test_book_osp_13", "wx" },
197     { USER_APP_ID, "test_book_osp_14", "rwx" },
198     { USER_APP_ID, "test_book_osp_15", "rwxat" },
199     { "test_subject_osp_8", USER_APP_ID, "r" },
200     { "test_subject_osp_9", USER_APP_ID, "w" },
201     { "test_subject_osp_10", USER_APP_ID, "x" },
202     { "test_subject_osp_11", USER_APP_ID, "rw" },
203     { "test_subject_osp_12", USER_APP_ID, "rx" },
204     { "test_subject_osp_13", USER_APP_ID, "wx" },
205     { "test_subject_osp_14", USER_APP_ID, "rwx" },
206     { "test_subject_osp_15", USER_APP_ID, "rwxat" }
207 };
208
209 int test_have_all_accesses(const rules_t &rules);
210 int test_have_any_accesses(const rules_t &rules);
211 int test_have_nosmack_accesses(const rules_t &rules);
212
213 void read_user_gids(std::set<unsigned> &set, const uid_t user_id);
214 void check_groups(const std::set<unsigned> &groups_prev, const char *dac_file);
215
216 int file_exists(const char *path);
217 void check_app_installed(const char *app_path);
218
219 void check_perm_app_has_permission(const char *app_label,
220                                    const char *permission,
221                                    bool is_enabled_expected);
222
223 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
224                        int /*typeflag*/, struct FTW* /*ftwbuf*/);
225 int nftw_check_labels_app_private_dir(const char *fpath, const struct stat *sb,
226                                       int /*typeflag*/, struct FTW* /*ftwbuf*/);
227 int nftw_check_labels_app_floor_dir(const char *fpath, const struct stat *sb,
228                                      int /*typeflag*/, struct FTW* /*ftwbuf*/);
229 int nftw_set_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
230                                 int /*typeflag*/, struct FTW* /*ftwbuf*/);
231 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
232                                   int /*typeflag*/, struct FTW* /*ftwbuf*/);
233
234 void test_perm_app_setup_path_PUBLIC_RO(bool smack);
235 void test_revoke_permissions(int line_no, const char* app_id);
236 void test_app_enable_permissions_efl(bool smack);
237 void test_app_disable_permissions_efl(bool smack);
238 void test_app_disable_permissions(bool smack);
239 bool check_all_accesses(bool smack, const rules_t &rules);
240 bool check_no_accesses(bool smack, const rules_t &rules);
241
242 #endif /* LIBPRIVILEGE_CONTROL_TEST_COMMON_H_ */