Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / src / 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 extern const char *PRIVS1[];
76 extern const char *PRIVS2[];
77 extern const char *PRIVS2_NO_R[];
78 extern const char *PRIVS2_R[];
79 extern const char *PRIVS2_R_AND_NO_R[];
80
81 extern const char *PRIVS_WGT[];
82 extern const char *PRIVS_OSP[];
83 extern const char *PRIVS_EFL[];
84
85 extern const char *PRIV_APPSETTING[];
86 extern const char *PRIV_APPSETTING_RULES[];
87
88 typedef std::vector< std::vector<std::string> > rules_t;
89
90 // Rules from WRT_test_privilege_control_rules1.smack for wgt
91 const rules_t rules1 = {
92     { USER_APP_ID, "test_book_1", "r" },
93     { USER_APP_ID, "test_book_2", "w" },
94     { USER_APP_ID, "test_book_3", "x" },
95     { USER_APP_ID, "test_book_4", "rw" },
96     { USER_APP_ID, "test_book_5", "rx" },
97     { USER_APP_ID, "test_book_6", "wx" },
98     { USER_APP_ID, "test_book_7", "rwx" },
99     { "test_subject_1", USER_APP_ID, "r" },
100     { "test_subject_2", USER_APP_ID, "w" },
101     { "test_subject_3", USER_APP_ID, "x" },
102     { "test_subject_4", USER_APP_ID, "rw" },
103     { "test_subject_5", USER_APP_ID, "rx" },
104     { "test_subject_6", USER_APP_ID, "wx" },
105     { "test_subject_7", USER_APP_ID, "rwx" }
106 };
107
108 // Rules from WRT_test_privilege_control_rules2.smack
109 const rules_t rules2 = {
110     { USER_APP_ID, "test_book_8", "r" },
111     { USER_APP_ID, "test_book_9", "w" },
112     { USER_APP_ID, "test_book_10", "x" },
113     { USER_APP_ID, "test_book_11", "rw" },
114     { USER_APP_ID, "test_book_12", "rx" },
115     { USER_APP_ID, "test_book_13", "wx" },
116     { USER_APP_ID, "test_book_14", "rwx" },
117     { USER_APP_ID, "test_book_15", "rwxat" },
118     { "test_subject_8", USER_APP_ID, "r" },
119     { "test_subject_9", USER_APP_ID, "w" },
120     { "test_subject_10", USER_APP_ID, "x" },
121     { "test_subject_11", USER_APP_ID, "rw" },
122     { "test_subject_12", USER_APP_ID, "rx" },
123     { "test_subject_13", USER_APP_ID, "wx" },
124     { "test_subject_14", USER_APP_ID, "rwx" },
125     { "test_subject_15", USER_APP_ID, "rwxat" }
126 };
127
128 // Rules from WRT_test_privilege_control_rules_no_r.smack
129 const rules_t rules2_no_r = {
130     { USER_APP_ID, "test_book_9", "w" },
131     { USER_APP_ID, "test_book_10", "x" },
132     { USER_APP_ID, "test_book_11", "w" },
133     { USER_APP_ID, "test_book_12", "x" },
134     { USER_APP_ID, "test_book_13", "x" },
135     { USER_APP_ID, "test_book_14", "wx" },
136     { USER_APP_ID, "test_book_15", "wxat" },
137     { "test_subject_9", USER_APP_ID, "w" },
138     { "test_subject_10", USER_APP_ID, "x" },
139     { "test_subject_11", USER_APP_ID, "w" },
140     { "test_subject_12", USER_APP_ID, "x" },
141     { "test_subject_13", USER_APP_ID, "x" },
142     { "test_subject_14", USER_APP_ID, "wx" },
143     { "test_subject_15", USER_APP_ID, "wxat" }
144 };
145
146 // Rules from test_privilege_control_rules.smack
147 // minus WRT_test_privilege_control_rules_no_r.smack
148 const rules_t rules2_r = {
149     { USER_APP_ID, "test_book_8", "r" },
150     { USER_APP_ID, "test_book_11", "r" },
151     { USER_APP_ID, "test_book_12", "r" },
152     { USER_APP_ID, "test_book_14", "r" },
153     { USER_APP_ID, "test_book_15", "r" },
154     { "test_subject_8", USER_APP_ID, "r" },
155     { "test_subject_11", USER_APP_ID, "r" },
156     { "test_subject_12", USER_APP_ID, "r" },
157     { "test_subject_14", USER_APP_ID, "r" },
158     { "test_subject_15", USER_APP_ID, "r" }
159 };
160
161 // Rules from EFL_test_privilege_control_rules_efl.smack for rpm
162 const rules_t rules_efl = {
163     { USER_APP_ID, "test_book_efl", "r" }
164 };
165
166 // Rules from WRT_test_privilege_control_rules_wgt.smack for wgt
167 const rules_t rules_wgt = {
168     { USER_APP_ID, "test_book_wgt_8", "r" },
169     { USER_APP_ID, "test_book_wgt_9", "w" },
170     { USER_APP_ID, "test_book_wgt_10", "x" },
171     { USER_APP_ID, "test_book_wgt_11", "rw" },
172     { USER_APP_ID, "test_book_wgt_12", "rx" },
173     { USER_APP_ID, "test_book_wgt_13", "wx" },
174     { USER_APP_ID, "test_book_wgt_14", "rwx" },
175     { USER_APP_ID, "test_book_wgt_15", "rwxat" },
176     { "test_subject_wgt_8", USER_APP_ID, "r" },
177     { "test_subject_wgt_9", USER_APP_ID, "w" },
178     { "test_subject_wgt_10", USER_APP_ID, "x" },
179     { "test_subject_wgt_11", USER_APP_ID, "rw" },
180     { "test_subject_wgt_12", USER_APP_ID, "rx" },
181     { "test_subject_wgt_13", USER_APP_ID, "wx" },
182     { "test_subject_wgt_14", USER_APP_ID, "rwx" },
183     { "test_subject_wgt_15", USER_APP_ID, "rwxat" }
184 };
185
186 // Rules from OSP_test_privilege_control_rules_osp.smack for osp
187 const rules_t rules_osp = {
188     { USER_APP_ID, "test_book_osp_8", "r" },
189     { USER_APP_ID, "test_book_osp_9", "w" },
190     { USER_APP_ID, "test_book_osp_10", "x" },
191     { USER_APP_ID, "test_book_osp_11", "rw" },
192     { USER_APP_ID, "test_book_osp_12", "rx" },
193     { USER_APP_ID, "test_book_osp_13", "wx" },
194     { USER_APP_ID, "test_book_osp_14", "rwx" },
195     { USER_APP_ID, "test_book_osp_15", "rwxat" },
196     { "test_subject_osp_8", USER_APP_ID, "r" },
197     { "test_subject_osp_9", USER_APP_ID, "w" },
198     { "test_subject_osp_10", USER_APP_ID, "x" },
199     { "test_subject_osp_11", USER_APP_ID, "rw" },
200     { "test_subject_osp_12", USER_APP_ID, "rx" },
201     { "test_subject_osp_13", USER_APP_ID, "wx" },
202     { "test_subject_osp_14", USER_APP_ID, "rwx" },
203     { "test_subject_osp_15", USER_APP_ID, "rwxat" }
204 };
205
206 int test_have_all_accesses(const rules_t &rules);
207 int test_have_any_accesses(const rules_t &rules);
208 int test_have_nosmack_accesses(const rules_t &rules);
209
210 void read_user_gids(std::set<unsigned> &set, const uid_t user_id);
211 void check_groups(const std::set<unsigned> &groups_prev, const char *dac_file);
212
213 int file_exists(const char *path);
214 void check_app_installed(const char *app_path);
215
216 void check_perm_app_has_permission(const char *app_label,
217                                    const char *permission,
218                                    bool is_enabled_expected);
219
220 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
221                        int /*typeflag*/, struct FTW* /*ftwbuf*/);
222 int nftw_check_labels_app_private_dir(const char *fpath, const struct stat *sb,
223                                       int /*typeflag*/, struct FTW* /*ftwbuf*/);
224 int nftw_check_labels_app_floor_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_perm_app_setup_path_PUBLIC_RO(bool smack);
232 void test_revoke_permissions(int line_no, const char* app_id);
233 void test_app_enable_permissions_efl(bool smack);
234 void test_app_disable_permissions_efl(bool smack);
235 void test_app_disable_permissions(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_ */