Add test to early rules loading.
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / test_cases.cpp
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      Jan Olszak (j.olszak@samsung.com)
20  * @author      Rafal Krypa (r.krypa@samsung.com)
21  * @version     1.0
22  * @brief       libprivilege-control test runer
23  */
24
25 #include <string>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <vector>
30 #include <errno.h>
31 #include <memory>
32 #include <ftw.h>
33 #include <dpl/test/test_runner.h>
34 #include <dpl/test/test_runner_child.h>
35 #include <dpl/log/log.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/mman.h>
39 #include <sys/xattr.h>
40 #include <sys/smack.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sys/wait.h>
45 #include <privilege-control.h>
46 #include <fstream>
47 #include <sstream>
48 #include <sys/stat.h>
49
50 #define SMACK_RULES_DIR          "/opt/etc/smack-app/accesses.d/"
51 #define SMACK_STARTUP_RULES_FILE "/opt/etc/smack-app-early/accesses.d/WRT"
52 #define SMACK_LOAD2              "/smack/load2"
53 #define TEST_APP_DIR             "/etc/smack/test_privilege_control_DIR/app_dir"
54 #define TEST_NON_APP_DIR         "/etc/smack/test_privilege_control_DIR/non_app_dir"
55 #define APPID_DIR                "test_APP_ID_dir"
56 #define APPID_SHARED_DIR         "test_APP_ID_shared_dir"
57 #define CANARY_LABEL             "tiny_yellow_canary"
58
59 #define APP_ID                 "test_APP"
60 #define APP_SET_PRIV_PATH      "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP"
61 #define APP_SET_PRIV_PATH_REAL "/etc/smack/test_privilege_control_DIR/test_set_app_privilege/test_APP_REAL"
62
63 #define WGT_APP_ID            "QwCqJ0ttyS"
64 #define WGT_PARTNER_APP_ID    "7btsV1Y0sX"
65 #define WGT_PLATFORM_APP_ID   "G4DE3U2vmW"
66 #define WGT_APP_PATH          "/opt/usr/apps/QwCqJ0ttyS/bin/QwCqJ0ttyS.TestMisiuPysiu123"
67 #define WGT_PARTNER_APP_PATH  "/opt/usr/apps/7btsV1Y0sX/bin/7btsV1Y0sX.MisiuPysiu123Partner"
68 #define WGT_PLATFORM_APP_PATH "/opt/usr/apps/G4DE3U2vmW/bin/G4DE3U2vmW.MisiuPysiu123Platform"
69 #define OSP_APP_ID            "uqNfgEjqc7"
70 #define OSP_PARTNER_APP_ID    "j4RuPsZrNt"
71 #define OSP_PLATFORM_APP_ID   "V5LKqDFBXm"
72 #define OSP_APP_PATH          "/opt/usr/apps/uqNfgEjqc7/bin/PysiuMisiu123Osp"
73 #define OSP_PARTNER_APP_PATH  "/opt/usr/apps/j4RuPsZrNt/bin/PysiuMisiu123OspPartner"
74 #define OSP_PLATFORM_APP_PATH "/opt/usr/apps/V5LKqDFBXm/bin/PysiuMisiu123OspPlatform"
75 #define EARLY_RULE_SUBJECT    "livebox.web-provider"
76 #define EARLY_RULE_RIGHTS     "rwx--"
77
78 const char *PRIVS[] = { "WRT", "test_privilege_control_rules", NULL };
79 const char *PRIVS2[] = { "test_privilege_control_rules2", NULL };
80 const char *PRIVS2_NO_R[] = { "test_privilege_control_rules2_no_r", NULL };
81 const char *PRIVS2_R[] = { "test_privilege_control_rules2_r", NULL };
82 const char *PRIVS2_R_AND_NO_R[] = { "test_privilege_control_rules2_r", "test_privilege_control_rules2_no_r", NULL };
83 const char *PRIVS_WGT[] = { "test_privilege_control_rules_wgt", NULL };
84 const char *PRIVS_OSP[] = { "test_privilege_control_rules_osp", NULL };
85
86 #define LIBPRIVILEGE_APP_GROUP_LIST    "/usr/share/privilege-control/app_group_list"
87 #define LIBPRIVILEGE_TEST_DAC_FILE     "/usr/share/privilege-control/test_privilege_control_rules.dac"
88 #define LIBPRIVILEGE_TEST_DAC_FILE_WGT "/usr/share/privilege-control/WRT_test_privilege_control_rules_wgt.dac"
89 #define LIBPRIVILEGE_TEST_DAC_FILE_OSP "/usr/share/privilege-control/OSP_test_privilege_control_rules_osp.dac"
90
91 #define APP_TEST_APP_1 "test-application1"
92 #define APP_TEST_APP_2 "test-application_2"
93 #define APP_TEST_APP_3 "test-app-3"
94 #define APP_TEST_AV_1  "test-antivirus1"
95 #define APP_TEST_AV_2  "test-antivirus_2"
96 #define APP_TEST_AV_3  "test-av-3"
97
98 #define SMACK_APPS_LABELS_DATABASE          "/opt/dbspace/.privilege_control_all_apps_id.db"
99 #define SMACK_AVS_LABELS_DATABASE           "/opt/dbspace/.privilege_control_all_avs_id.db"
100 #define SMACK_PUBLIC_DIRS_DATABASE          "/opt/dbspace/.privilege_control_public_dirs.db"
101 #define SMACK_APPS_SETTINGS_LABELS_DATABASE "/opt/dbspace/.privilege_control_app_setting.db"
102 #define SMACK_SETTINGS_DIRS_DATABASE        "/opt/dbspace/.privilege_control_setting_dir.db"
103
104 #define APP_TEST_SETTINGS_ASP1 "test-app-settings-asp1"
105 #define APP_TEST_SETTINGS_ASP2 "test-app-settings-asp2"
106 #define APP_TEST_AV_ASP1       "test-app-av-asp1"
107 #define APP_TEST_AV_ASP2       "test-app-av-asp2"
108
109 #define SOCK_PATH "/tmp/test-smack-socket"
110
111 #define APP_GID       5000
112 #define APP_UID       5000
113 #define APP_USER_NAME "app"
114 #define APP_HOME_DIR  "/opt/home/app"
115
116 #define APP_FRIEND_1 "app_friend_1"
117 #define APP_FRIEND_2 "app_friend_2"
118
119 // How many open file descriptors should ftw() function use?
120 #define FTW_MAX_FDS 16
121
122 // Rules from test_privilege_control_rules.smack
123 const std::vector< std::vector<std::string> > rules = {
124     { APP_ID, "test_book_1", "r" },
125     { APP_ID, "test_book_2", "w" },
126     { APP_ID, "test_book_3", "x" },
127     { APP_ID, "test_book_4", "rw" },
128     { APP_ID, "test_book_5", "rx" },
129     { APP_ID, "test_book_6", "wx" },
130     { APP_ID, "test_book_7", "rwx" },
131     { "test_subject_1", APP_ID, "r" },
132     { "test_subject_2", APP_ID, "w" },
133     { "test_subject_3", APP_ID, "x" },
134     { "test_subject_4", APP_ID, "rw" },
135     { "test_subject_5", APP_ID, "rx" },
136     { "test_subject_6", APP_ID, "wx" },
137     { "test_subject_7", APP_ID, "rwx" },
138     { APP_ID, APPID_SHARED_DIR, "rwxat"}
139 };
140
141 // Rules from test_privilege_control_rules2.smack
142 const std::vector< std::vector<std::string> > rules2 = {
143     { APP_ID, "test_book_8", "r" },
144     { APP_ID, "test_book_9", "w" },
145     { APP_ID, "test_book_10", "x" },
146     { APP_ID, "test_book_11", "rw" },
147     { APP_ID, "test_book_12", "rx" },
148     { APP_ID, "test_book_13", "wx" },
149     { APP_ID, "test_book_14", "rwx" },
150     { APP_ID, "test_book_15", "rwxat" },
151     { "test_subject_8", APP_ID, "r" },
152     { "test_subject_9", APP_ID, "w" },
153     { "test_subject_10", APP_ID, "x" },
154     { "test_subject_11", APP_ID, "rw" },
155     { "test_subject_12", APP_ID, "rx" },
156     { "test_subject_13", APP_ID, "wx" },
157     { "test_subject_14", APP_ID, "rwx" },
158     { "test_subject_15", APP_ID, "rwxat" }
159 };
160
161 // Rules from test_privilege_control_rules_no_r.smack
162 const std::vector< std::vector<std::string> > rules2_no_r = {
163     { APP_ID, "test_book_9", "w" },
164     { APP_ID, "test_book_10", "x" },
165     { APP_ID, "test_book_11", "w" },
166     { APP_ID, "test_book_12", "x" },
167     { APP_ID, "test_book_13", "wx" },
168     { APP_ID, "test_book_14", "wx" },
169     { APP_ID, "test_book_15", "wxat" },
170     { "test_subject_9", APP_ID, "w" },
171     { "test_subject_10", APP_ID, "x" },
172     { "test_subject_11", APP_ID, "w" },
173     { "test_subject_12", APP_ID, "x" },
174     { "test_subject_13", APP_ID, "wx" },
175     { "test_subject_14", APP_ID, "wx" },
176     { "test_subject_15", APP_ID, "wxat" }
177 };
178
179 // Rules from test_privilege_control_rules.smack
180 // minus test_privilege_control_rules_no_r.smack
181 const std::vector< std::vector<std::string> > rules2_r = {
182     { APP_ID, "test_book_8", "r" },
183     { APP_ID, "test_book_11", "r" },
184     { APP_ID, "test_book_12", "r" },
185     { APP_ID, "test_book_14", "r" },
186     { APP_ID, "test_book_15", "r" },
187     { "test_subject_8", APP_ID, "r" },
188     { "test_subject_11", APP_ID, "r" },
189     { "test_subject_12", APP_ID, "r" },
190     { "test_subject_14", APP_ID, "r" },
191     { "test_subject_15", APP_ID, "r" }
192 };
193
194 // Rules from test_privilege_control_rules_wgt.smack for wgt
195 const std::vector< std::vector<std::string> > rules_wgt = {
196     { WGT_APP_ID, "test_book_8", "r" },
197     { WGT_APP_ID, "test_book_9", "w" },
198     { WGT_APP_ID, "test_book_10", "x" },
199     { WGT_APP_ID, "test_book_11", "rw" },
200     { WGT_APP_ID, "test_book_12", "rx" },
201     { WGT_APP_ID, "test_book_13", "wx" },
202     { WGT_APP_ID, "test_book_14", "rwx" },
203     { WGT_APP_ID, "test_book_15", "rwxat" },
204     { "test_subject_8", WGT_APP_ID, "r" },
205     { "test_subject_9", WGT_APP_ID, "w" },
206     { "test_subject_10", WGT_APP_ID, "x" },
207     { "test_subject_11", WGT_APP_ID, "rw" },
208     { "test_subject_12", WGT_APP_ID, "rx" },
209     { "test_subject_13", WGT_APP_ID, "wx" },
210     { "test_subject_14", WGT_APP_ID, "rwx" },
211     { "test_subject_15", WGT_APP_ID, "rwxat" }
212 };
213
214 // Rules from test_privilege_control_rules_wgt.smack for wgt_partner
215 const std::vector< std::vector<std::string> > rules_wgt_partner = {
216     { WGT_PARTNER_APP_ID, "test_book_8", "r" },
217     { WGT_PARTNER_APP_ID, "test_book_9", "w" },
218     { WGT_PARTNER_APP_ID, "test_book_10", "x" },
219     { WGT_PARTNER_APP_ID, "test_book_11", "rw" },
220     { WGT_PARTNER_APP_ID, "test_book_12", "rx" },
221     { WGT_PARTNER_APP_ID, "test_book_13", "wx" },
222     { WGT_PARTNER_APP_ID, "test_book_14", "rwx" },
223     { WGT_PARTNER_APP_ID, "test_book_15", "rwxat" },
224     { "test_subject_8", WGT_PARTNER_APP_ID, "r" },
225     { "test_subject_9", WGT_PARTNER_APP_ID, "w" },
226     { "test_subject_10", WGT_PARTNER_APP_ID, "x" },
227     { "test_subject_11", WGT_PARTNER_APP_ID, "rw" },
228     { "test_subject_12", WGT_PARTNER_APP_ID, "rx" },
229     { "test_subject_13", WGT_PARTNER_APP_ID, "wx" },
230     { "test_subject_14", WGT_PARTNER_APP_ID, "rwx" },
231     { "test_subject_15", WGT_PARTNER_APP_ID, "rwxat" }
232 };
233
234 // Rules from test_privilege_control_rules_wgt.smack for wgt_platform
235 const std::vector< std::vector<std::string> > rules_wgt_platform = {
236     { WGT_PLATFORM_APP_ID, "test_book_8", "r" },
237     { WGT_PLATFORM_APP_ID, "test_book_9", "w" },
238     { WGT_PLATFORM_APP_ID, "test_book_10", "x" },
239     { WGT_PLATFORM_APP_ID, "test_book_11", "rw" },
240     { WGT_PLATFORM_APP_ID, "test_book_12", "rx" },
241     { WGT_PLATFORM_APP_ID, "test_book_13", "wx" },
242     { WGT_PLATFORM_APP_ID, "test_book_14", "rwx" },
243     { WGT_PLATFORM_APP_ID, "test_book_15", "rwxat" },
244     { "test_subject_8", WGT_PLATFORM_APP_ID, "r" },
245     { "test_subject_9", WGT_PLATFORM_APP_ID, "w" },
246     { "test_subject_10", WGT_PLATFORM_APP_ID, "x" },
247     { "test_subject_11", WGT_PLATFORM_APP_ID, "rw" },
248     { "test_subject_12", WGT_PLATFORM_APP_ID, "rx" },
249     { "test_subject_13", WGT_PLATFORM_APP_ID, "wx" },
250     { "test_subject_14", WGT_PLATFORM_APP_ID, "rwx" },
251     { "test_subject_15", WGT_PLATFORM_APP_ID, "rwxat" }
252 };
253
254 // Rules from test_privilege_control_rules_osp.smack for osp
255 const std::vector< std::vector<std::string> > rules_osp = {
256     { OSP_APP_ID, "test_book_8", "r" },
257     { OSP_APP_ID, "test_book_9", "w" },
258     { OSP_APP_ID, "test_book_10", "x" },
259     { OSP_APP_ID, "test_book_11", "rw" },
260     { OSP_APP_ID, "test_book_12", "rx" },
261     { OSP_APP_ID, "test_book_13", "wx" },
262     { OSP_APP_ID, "test_book_14", "rwx" },
263     { OSP_APP_ID, "test_book_15", "rwxat" },
264     { "test_subject_8", OSP_APP_ID, "r" },
265     { "test_subject_9", OSP_APP_ID, "w" },
266     { "test_subject_10", OSP_APP_ID, "x" },
267     { "test_subject_11", OSP_APP_ID, "rw" },
268     { "test_subject_12", OSP_APP_ID, "rx" },
269     { "test_subject_13", OSP_APP_ID, "wx" },
270     { "test_subject_14", OSP_APP_ID, "rwx" },
271     { "test_subject_15", OSP_APP_ID, "rwxat" }
272 };
273
274 // Rules from test_privilege_control_rules_osp.smack for osp_partner
275 const std::vector< std::vector<std::string> > rules_osp_partner = {
276     { OSP_PARTNER_APP_ID, "test_book_8", "r" },
277     { OSP_PARTNER_APP_ID, "test_book_9", "w" },
278     { OSP_PARTNER_APP_ID, "test_book_10", "x" },
279     { OSP_PARTNER_APP_ID, "test_book_11", "rw" },
280     { OSP_PARTNER_APP_ID, "test_book_12", "rx" },
281     { OSP_PARTNER_APP_ID, "test_book_13", "wx" },
282     { OSP_PARTNER_APP_ID, "test_book_14", "rwx" },
283     { OSP_PARTNER_APP_ID, "test_book_15", "rwxat" },
284     { "test_subject_8", OSP_PARTNER_APP_ID, "r" },
285     { "test_subject_9", OSP_PARTNER_APP_ID, "w" },
286     { "test_subject_10", OSP_PARTNER_APP_ID, "x" },
287     { "test_subject_11", OSP_PARTNER_APP_ID, "rw" },
288     { "test_subject_12", OSP_PARTNER_APP_ID, "rx" },
289     { "test_subject_13", OSP_PARTNER_APP_ID, "wx" },
290     { "test_subject_14", OSP_PARTNER_APP_ID, "rwx" },
291     { "test_subject_15", OSP_PARTNER_APP_ID, "rwxat" }
292 };
293
294 // Rules from test_privilege_control_rules_osp.smack for osp_platform
295 const std::vector< std::vector<std::string> > rules_osp_platform = {
296     { OSP_PLATFORM_APP_ID, "test_book_8", "r" },
297     { OSP_PLATFORM_APP_ID, "test_book_9", "w" },
298     { OSP_PLATFORM_APP_ID, "test_book_10", "x" },
299     { OSP_PLATFORM_APP_ID, "test_book_11", "rw" },
300     { OSP_PLATFORM_APP_ID, "test_book_12", "rx" },
301     { OSP_PLATFORM_APP_ID, "test_book_13", "wx" },
302     { OSP_PLATFORM_APP_ID, "test_book_14", "rwx" },
303     { OSP_PLATFORM_APP_ID, "test_book_15", "rwxat" },
304     { "test_subject_8", OSP_PLATFORM_APP_ID, "r" },
305     { "test_subject_9", OSP_PLATFORM_APP_ID, "w" },
306     { "test_subject_10", OSP_PLATFORM_APP_ID, "x" },
307     { "test_subject_11", OSP_PLATFORM_APP_ID, "rw" },
308     { "test_subject_12", OSP_PLATFORM_APP_ID, "rx" },
309     { "test_subject_13", OSP_PLATFORM_APP_ID, "wx" },
310     { "test_subject_14", OSP_PLATFORM_APP_ID, "rwx" },
311     { "test_subject_15", OSP_PLATFORM_APP_ID, "rwxat" }
312 };
313
314 namespace {
315 typedef std::unique_ptr<smack_accesses,std::function<void (smack_accesses*)> > SmackUniquePtr;
316
317 const char *OSP_BLAHBLAH = "/usr/share/privilege-control/OSP_feature.blah.blahblah.smack";
318 const char *WRT_BLAHBLAH = "/usr/share/privilege-control/WGT_blahblah.smack";
319 const char *OTHER_BLAHBLAH = "/usr/share/privilege-control/blahblah.smack";
320 const char *OSP_BLAHBLAH_DAC = "/usr/share/privilege-control/OSP_feature.blah.blahblah.dac";
321 const char *WRT_BLAHBLAH_DAC = "/usr/share/privilege-control/WGT_blahblah.dac";
322 const char *OTHER_BLAHBLAH_DAC = "/usr/share/privilege-control/blahblah.dac";
323 const char *BLAHBLAH_FEATURE = "http://feature/blah/blahblah";
324
325 /**
326  * Check if every rule is true.
327  * @return 1 if ALL rules in SMACK, 0 if ANY rule isn't
328  */
329 int test_have_all_accesses(const std::vector< std::vector<std::string> > &rules)
330 {
331     int result;
332     for (uint i = 0; i < rules.size(); ++i) {
333         result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
334         if (result != 1)
335             return result;
336     }
337     return 1;
338 }
339
340 /**
341  * Check if every rule is true.
342  * @return 1 if ANY rule in SMACK, 0 if
343  */
344 int test_have_any_accesses(const std::vector< std::vector<std::string> > &rules)
345 {
346     int result;
347     for (uint i = 0; i < rules.size(); ++i) {
348         result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
349         if (result == 1)
350             return 1;
351     }
352     return 0;
353 }
354
355 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
356                        int /*typeflag*/, struct FTW* /*ftwbuf*/)
357 {
358     smack_lsetlabel(fpath, NULL, SMACK_LABEL_ACCESS);
359     smack_lsetlabel(fpath, NULL, SMACK_LABEL_EXEC);
360     smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
361
362     return 0;
363 }
364
365 int nftw_set_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
366                                 int /*typeflag*/, struct FTW* /*ftwbuf*/)
367 {
368     smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_ACCESS);
369     smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_EXEC);
370     smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
371
372     return 0;
373 }
374
375 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
376                                   int /*typeflag*/, struct FTW* /*ftwbuf*/)
377 {
378     int result;
379     char *label;
380
381     /* ACCESS */
382     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
383     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
384     result = strcmp(CANARY_LABEL, label);
385     RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is overwritten");
386
387     /* EXEC */
388     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
389     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
390     result = strcmp(CANARY_LABEL, label);
391     RUNNER_ASSERT_MSG(result == 0, "EXEC label on " << fpath << " is overwritten");
392
393     /* TRANSMUTE */
394     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
395     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
396     RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
397
398     return 0;
399 }
400
401 int nftw_check_labels_app_dir(const char *fpath, const struct stat *sb,
402                               int /*typeflag*/, struct FTW* /*ftwbuf*/)
403 {
404     int result;
405     char *label;
406
407     /* ACCESS */
408     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
409     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
410     RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
411     result = strcmp(APPID_DIR, label);
412     RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
413
414     /* EXEC */
415     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
416     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
417     if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR)) {
418         RUNNER_ASSERT_MSG(label != NULL, "EXEC label on " << fpath << " is not set");
419         result = strcmp(APPID_DIR, label);
420         RUNNER_ASSERT_MSG(result == 0, "EXEC label on executable file " << fpath << " is incorrect");
421     } else if (S_ISLNK(sb->st_mode)) {
422         struct stat buf;
423         char *target = realpath(fpath, NULL);
424         RUNNER_ASSERT_MSG(0 == stat(target, &buf),"Stat failed for " << fpath);
425         free(target);
426         if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG)) {
427             RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
428         } else {
429             RUNNER_ASSERT_MSG(label != NULL, "EXEC label on " << fpath << " is not set");
430             result = strcmp(APPID_DIR, label);
431             RUNNER_ASSERT_MSG(result == 0, "EXEC label on link to executable file " << fpath << " is incorrect");
432         }
433     } else
434         RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
435
436     /* TRANSMUTE */
437     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
438     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
439     RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
440
441     return 0;
442 }
443
444 int nftw_check_labels_app_shared_dir(const char *fpath, const struct stat *sb,
445                                      int /*typeflag*/, struct FTW* /*ftwbuf*/)
446 {
447     int result;
448     char *label;
449
450     /* ACCESS */
451     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
452     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
453     RUNNER_ASSERT_MSG(label != NULL, "ACCESS label on " << fpath << " is not set");
454     result = strcmp(APPID_SHARED_DIR, label);
455     RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect");
456
457     result = smack_have_access(APP_ID, APPID_SHARED_DIR, "rwxat");
458     RUNNER_ASSERT_MSG(result == 1,
459         "Error rwxat access was not given shared dir. Subject: " <<
460         APP_ID << ". Object: " << APPID_SHARED_DIR << ". Result: " << result);
461     /* EXEC */
462     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
463     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
464     RUNNER_ASSERT_MSG(label == NULL, "EXEC label on " << fpath << " is set");
465
466     /* TRANSMUTE */
467     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
468     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
469     if (S_ISDIR(sb->st_mode)) {
470         RUNNER_ASSERT_MSG(label != NULL, "TRANSMUTE label on " << fpath << " is not set");
471         result = strcmp("TRUE", label);
472         RUNNER_ASSERT_MSG(result == 0, "TRANSMUTE label on " << fpath << " is not set");
473     } else
474         RUNNER_ASSERT_MSG(label == NULL, "TRANSMUTE label on " << fpath << " is set");
475
476     return 0;
477 }
478
479 int check_labels_dir(const char *fpath, const struct stat *sb,
480                      const char *labels_db_path, const char *dir_db_path,
481                      const char *access)
482 {
483     int result;
484     char *label;
485     char *label_gen;
486     char *scanf_label_format;
487     char label_temp[SMACK_LABEL_LEN + 1];
488     FILE *file_db;
489
490     /* ACCESS */
491     result = smack_lgetlabel(fpath, &label_gen, SMACK_LABEL_ACCESS);
492     RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
493     RUNNER_ASSERT_MSG(label_gen != NULL, "ACCESS label on " << fpath << " is not set");
494
495     /* EXEC */
496     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
497     if (result != 0) {
498         free(label_gen);
499         RUNNER_ASSERT_MSG(false, "Could not get label for the path");
500     }
501     if (label != NULL) {
502         free(label_gen);
503         free(label);
504         RUNNER_ASSERT_MSG(false, "EXEC label on " << fpath << " is set.");
505     }
506
507     /* TRANSMUTE */
508     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
509     if (result != 0) {
510         free(label_gen);
511         free(label);
512         RUNNER_ASSERT_MSG(false, "Could not get label for the path");
513     }
514     if (S_ISDIR(sb->st_mode)) {
515         if (label == NULL) {
516             free(label_gen);
517             free(label);
518             RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is not set");
519         }
520         result = strcmp("TRUE", label);
521         if (result != 0) {
522             free(label_gen);
523             free(label);
524             RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is not set to TRUE");
525         }
526     } else if (label != NULL) {
527         free(label_gen);
528         free(label);
529         RUNNER_ASSERT_MSG(false, "TRANSMUTE label on " << fpath << " is set");
530     }
531
532     free(label);
533
534     if (0 > asprintf(&scanf_label_format, "%%%ds\\n", SMACK_LABEL_LEN)) {
535         free(label_gen);
536         RUNNER_ASSERT_MSG(false, "asprintf failed");
537     }
538
539     file_db = fopen(labels_db_path, "r");
540     if (file_db == NULL) {
541         free(label_gen);
542         free(scanf_label_format);
543         RUNNER_ASSERT_MSG(false, "Can not open database for apps");
544     }
545     while (fscanf(file_db, scanf_label_format, label_temp) == 1) {
546         result = smack_have_access(label_temp, label_gen, access);
547         if (result != 1) {
548             fclose(file_db);
549             free(label_gen);
550             free(scanf_label_format);
551             RUNNER_ASSERT_MSG(false,
552                 "Error " << access << " access was not given for subject: "
553                 << label_temp << ". Result: " << result);
554         }
555     }
556     fclose(file_db);
557
558     file_db = fopen(dir_db_path, "r");
559     if (file_db == NULL) {
560         free(label_gen);
561         free(scanf_label_format);
562         RUNNER_ASSERT_MSG(false, "Can not open database for dirs");
563     }
564     bool is_dir = false;
565     while (fscanf(file_db, scanf_label_format, label_temp) == 1) {
566         if (strcmp(label_gen, label_temp) == 0) {
567             is_dir = true;
568             break;
569         }
570     }
571     free(scanf_label_format);
572     free(label_gen);
573     fclose(file_db);
574
575     RUNNER_ASSERT_MSG(is_dir, "Error autogenerated label is not in dirs db.");
576
577     return 0;
578 }
579
580 int nftw_check_labels_app_public_dir(const char *fpath, const struct stat *sb,
581                                      int /*typeflag*/, struct FTW* /*ftwbuf*/)
582 {
583     return check_labels_dir(fpath, sb,
584         SMACK_APPS_LABELS_DATABASE,
585         SMACK_PUBLIC_DIRS_DATABASE, "rx");
586 }
587
588 int nftw_check_labels_app_settings_dir(const char *fpath, const struct stat *sb,
589                                        int /*typeflag*/, struct FTW* /*ftwbuf*/)
590 {
591     return check_labels_dir(fpath, sb,
592         SMACK_APPS_SETTINGS_LABELS_DATABASE,
593         SMACK_SETTINGS_DIRS_DATABASE, "rwx");
594 }
595
596 int file_exists(const char *path)
597 {
598     FILE *file = fopen(path, "r");
599     if (file) {
600         fclose(file);
601         return 0;
602     }
603     return -1;
604 }
605
606 void osp_blahblah_check(int line_no, const std::vector<std::string> &rules)
607 {
608     std::ifstream smack_file(OSP_BLAHBLAH);
609     RUNNER_ASSERT_MSG(smack_file, "Line: " << line_no << " Failed to create " << OSP_BLAHBLAH);
610
611     auto it = rules.begin();
612     std::string line;
613     while (std::getline(smack_file,line)) {
614         RUNNER_ASSERT_MSG(it != rules.end(), "Line: " << line_no << "Additional line in file: " << line);
615         RUNNER_ASSERT_MSG(*it == line, "Line: " << line_no << " " << *it << "!=" << line);
616         it++;
617     }
618
619     RUNNER_ASSERT_MSG(it == rules.end(), "Line: " << line_no << " Missing line in file: " << *it);
620
621     smack_file.close();
622 }
623
624 void osp_blahblah_dac_check(int line_no, const std::vector<unsigned> &gids)
625 {
626     std::ifstream dac_file(OSP_BLAHBLAH_DAC);
627     RUNNER_ASSERT_MSG(dac_file, "Line: " << line_no << " Failed to create " << OSP_BLAHBLAH_DAC);
628
629     auto it = gids.begin();
630     std::string line;
631     while (std::getline(dac_file,line)) {
632         std::istringstream is(line);
633         unsigned gid;
634         is >> gid;
635         RUNNER_ASSERT_MSG(it != gids.end(), "Line: " << line_no << "Additional line in file: " << gid);
636         RUNNER_ASSERT_MSG(*it == gid, "Line: " << line_no << " " << *it << "!=" << gid);
637         it++;
638     }
639
640     RUNNER_ASSERT_MSG(it == gids.end(), "Line: " << line_no << " Missing line in file: " << *it);
641
642     dac_file.close();
643 }
644
645 void remove_smack_files()
646 {
647     // TODO array
648     unlink(OSP_BLAHBLAH);
649     unlink(WRT_BLAHBLAH);
650     unlink(OTHER_BLAHBLAH);
651     unlink(OSP_BLAHBLAH_DAC);
652     unlink(WRT_BLAHBLAH_DAC);
653     unlink(OTHER_BLAHBLAH_DAC);
654 }
655
656 int cleaning_smack_app_files (void)
657 {
658     unlink(SMACK_RULES_DIR APP_TEST_APP_1);
659
660     unlink(SMACK_RULES_DIR APP_TEST_APP_2);
661
662     unlink(SMACK_RULES_DIR APP_TEST_APP_3);
663
664     unlink(SMACK_RULES_DIR APP_TEST_AV_1);
665
666     unlink(SMACK_RULES_DIR APP_TEST_AV_2);
667
668     unlink(SMACK_RULES_DIR APP_TEST_AV_3);
669
670     return 0;
671 }
672
673 int cleaning_smack_database_files (void)
674 {
675     int fd = -1;
676
677     //clean app database
678     unlink(SMACK_APPS_LABELS_DATABASE);
679     fd = open(SMACK_APPS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
680     if (fd == -1) {
681         return -1;
682     }
683
684     //clean av database
685     unlink(SMACK_AVS_LABELS_DATABASE);
686     fd = open(SMACK_AVS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
687     if (fd == -1) {
688         return -1;
689     }
690
691     //clean app settings database
692     unlink(SMACK_APPS_SETTINGS_LABELS_DATABASE);
693     fd = open(SMACK_APPS_SETTINGS_LABELS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
694     if (fd == -1) {
695         return -1;
696     }
697
698     //clean public dirs database
699     unlink(SMACK_PUBLIC_DIRS_DATABASE);
700     fd = open(SMACK_PUBLIC_DIRS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
701     if (fd == -1) {
702         return -1;
703     }
704
705     //clean settings dirs database
706     unlink(SMACK_SETTINGS_DIRS_DATABASE);
707     fd = open(SMACK_SETTINGS_DIRS_DATABASE, O_RDWR | O_EXCL | O_CREAT, 0644);
708     if (fd == -1) {
709         return -1;
710     }
711
712     return 0;
713 }
714
715 void add_lables_to_db()
716 {
717     FILE *file_db;
718
719     file_db = fopen(SMACK_AVS_LABELS_DATABASE, "a");
720     RUNNER_ASSERT_MSG(file_db != NULL, "Error database file "
721         << SMACK_AVS_LABELS_DATABASE << " can not be opened to apend!");
722     if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP1)) {
723         fclose(file_db);
724         RUNNER_ASSERT_MSG(false, "Error writing to database file");
725     }
726     if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP2)) {
727         fclose(file_db);
728         RUNNER_ASSERT_MSG(false, "Error writing to database file");
729     }
730     fclose(file_db);
731
732     file_db = fopen(SMACK_APPS_SETTINGS_LABELS_DATABASE, "a");
733     RUNNER_ASSERT_MSG(file_db != NULL, "Error database file "
734         << SMACK_APPS_SETTINGS_LABELS_DATABASE << " can not be opened to apend!");
735     if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP1)) {
736         fclose(file_db);
737         RUNNER_ASSERT_MSG(false, "Error writing to database file");
738     }
739     if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP2)) {
740         fclose(file_db);
741         RUNNER_ASSERT_MSG(false, "Error writing to database file");
742     }
743     fclose(file_db);
744
745     file_db = fopen(SMACK_APPS_LABELS_DATABASE, "a");
746     RUNNER_ASSERT_MSG(file_db != NULL, "Error database file "
747         << SMACK_APPS_LABELS_DATABASE << " can not be opened to apend!");
748     if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP1)) {
749         fclose(file_db);
750         RUNNER_ASSERT_MSG(false, "Error writing to database file");
751     }
752     if (0 > fprintf(file_db, "%s\n", APP_TEST_AV_ASP2)) {
753         fclose(file_db);
754         RUNNER_ASSERT_MSG(false, "Error writing to database file");
755     }
756     if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP1)) {
757         fclose(file_db);
758         RUNNER_ASSERT_MSG(false, "Error writing to database file");
759     }
760     if (0 > fprintf(file_db, "%s\n", APP_TEST_SETTINGS_ASP2)) {
761         fclose(file_db);
762         RUNNER_ASSERT_MSG(false, "Error writing to database file");
763     }
764     fclose(file_db);
765 }
766 } // namespace
767
768 RUNNER_TEST_GROUP_INIT(libprivilegecontrol)
769
770 /**
771  * Test setting labels for all files and folders in given path.
772  */
773 RUNNER_TEST(privilege_control02_app_label_dir)
774 {
775     int result;
776
777     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
778     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
779
780     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
781     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
782
783     result = app_setup_path(APPID_DIR, TEST_APP_DIR, APP_PATH_PRIVATE);
784     RUNNER_ASSERT_MSG(result == 0, "app_setup_path() failed");
785
786     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_dir, FTW_MAX_FDS, FTW_PHYS);
787     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for app dir");
788
789     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
790     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
791 }
792
793 RUNNER_TEST(privilege_control03_app_label_shared_dir)
794 {
795     int result;
796
797     result = app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APP_ID);
798     RUNNER_ASSERT_MSG(result != 0, "app_setup_path(APP_ID, APP_ID) didn't fail");
799
800     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
801     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
802
803     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
804     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
805
806     result = app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_GROUP_RW, APPID_SHARED_DIR);
807     RUNNER_ASSERT_MSG(result == 0, "app_setup_path() failed");
808
809     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_shared_dir, FTW_MAX_FDS, FTW_PHYS);
810     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
811
812     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
813     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
814 }
815
816 /**
817  * Add permisions from  test_privilege_control_rules template
818  */
819 RUNNER_TEST(privilege_control04_add_permissions)
820 {
821     int result = app_add_permissions(APP_ID, PRIVS);
822     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
823         " Error adding app permissions. Result: " << result);
824
825     // Check if the accesses are realy applied..
826     result = test_have_all_accesses(rules);
827     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
828
829     //// File exists?
830     FILE *pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
831     RUNNER_ASSERT_MSG(pFile != NULL,
832         "SMACK file NOT created!. Errno: " << errno);
833
834     //// Is it empty?
835     fseek(pFile, 0L, SEEK_END);
836     int smack_file_length = ftell(pFile);
837     RUNNER_ASSERT_MSG(smack_file_length > 0,
838         "SMACK file empty, but privileges list was not empty.. Errno: " << errno);
839
840     if (pFile != NULL)
841         fclose(pFile);
842 }
843
844 /**
845  * Revoke permissions from the list. Should be executed as privileged user.
846  */
847 RUNNER_CHILD_TEST(privilege_control06_revoke_permissions)
848 {
849     int result;
850     int fd;
851
852     // Revoke permissions
853     result = app_revoke_permissions(APP_ID);
854     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
855         "Error revoking app permissions. Result: " << result);
856
857     result = app_revoke_permissions(WGT_APP_ID);
858     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
859         "Error revoking app permissions. Result: " << result);
860     result = app_revoke_permissions(WGT_PARTNER_APP_ID);
861     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
862         "Error revoking app permissions. Result: " << result);
863     result = app_revoke_permissions(WGT_PLATFORM_APP_ID);
864     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
865         "Error revoking app permissions. Result: " << result);
866
867     result = app_revoke_permissions(OSP_APP_ID);
868     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
869         "Error revoking app permissions. Result: " << result);
870     result = app_revoke_permissions(OSP_PARTNER_APP_ID);
871     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
872         "Error revoking app permissions. Result: " << result);
873     result = app_revoke_permissions(OSP_PLATFORM_APP_ID);
874     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
875         "Error revoking app permissions. Result: " << result);
876
877     // Are all the permissions revoked?
878     result = test_have_any_accesses(rules);
879     RUNNER_ASSERT_MSG(result != 1, "Not all permisions revoked.");
880     result = test_have_any_accesses(rules_wgt);
881     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
882     result = test_have_any_accesses(rules_wgt_partner);
883     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
884     result = test_have_any_accesses(rules_wgt_platform);
885     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
886
887     result = test_have_any_accesses(rules);
888     RUNNER_ASSERT_MSG(result != 1, "Not all permisions revoked.");
889     result = test_have_any_accesses(rules_osp);
890     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
891     result = test_have_any_accesses(rules_osp_partner);
892     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
893     result = test_have_any_accesses(rules_osp_platform);
894     RUNNER_ASSERT_MSG(result == 0, "Not all permisions revoked.");
895
896     FILE *pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
897     RUNNER_ASSERT_MSG(pFile != NULL,
898         "SMACK file removed!. Errno: " << errno);
899     //// Is it empty?
900     fseek(pFile, 0L, SEEK_END);
901     int smack_file_length = ftell(pFile);
902     if (pFile != NULL)
903         fclose(pFile);
904     RUNNER_ASSERT_MSG(smack_file_length == 0,
905         "SMACK file not empty.. Errno: " << errno);
906
907     pFile = fopen(SMACK_RULES_DIR WGT_APP_ID, "rb");
908     RUNNER_ASSERT_MSG(pFile != NULL,
909         "SMACK file removed!. Errno: " << errno);
910     //// Is it empty?
911     fseek(pFile, 0L, SEEK_END);
912     smack_file_length = ftell(pFile);
913     if (pFile != NULL)
914         fclose(pFile);
915     RUNNER_ASSERT_MSG(smack_file_length == 0,
916         "SMACK file not empty.. Errno: " << errno);
917
918     pFile = fopen(SMACK_RULES_DIR WGT_PARTNER_APP_ID, "rb");
919     RUNNER_ASSERT_MSG(pFile != NULL,
920         "SMACK file removed!. Errno: " << errno);
921     //// Is it empty?
922     fseek(pFile, 0L, SEEK_END);
923     smack_file_length = ftell(pFile);
924     if (pFile != NULL)
925         fclose(pFile);
926     RUNNER_ASSERT_MSG(smack_file_length == 0,
927         "SMACK file not empty.. Errno: " << errno);
928
929     pFile = fopen(SMACK_RULES_DIR WGT_PLATFORM_APP_ID, "rb");
930     RUNNER_ASSERT_MSG(pFile != NULL,
931         "SMACK file removed!. Errno: " << errno);
932     //// Is it empty?
933     fseek(pFile, 0L, SEEK_END);
934     smack_file_length = ftell(pFile);
935     if (pFile != NULL)
936         fclose(pFile);
937     RUNNER_ASSERT_MSG(smack_file_length == 0,
938         "SMACK file not empty.. Errno: " << errno);
939
940     pFile = fopen(SMACK_RULES_DIR OSP_APP_ID, "rb");
941     RUNNER_ASSERT_MSG(pFile != NULL,
942         "SMACK file removed!. Errno: " << errno);
943     //// Is it empty?
944     fseek(pFile, 0L, SEEK_END);
945     smack_file_length = ftell(pFile);
946     if (pFile != NULL)
947         fclose(pFile);
948     RUNNER_ASSERT_MSG(smack_file_length == 0,
949         "SMACK file not empty.. Errno: " << errno);
950
951     pFile = fopen(SMACK_RULES_DIR OSP_PARTNER_APP_ID, "rb");
952     RUNNER_ASSERT_MSG(pFile != NULL,
953         "SMACK file removed!. Errno: " << errno);
954     //// Is it empty?
955     fseek(pFile, 0L, SEEK_END);
956     smack_file_length = ftell(pFile);
957     if (pFile != NULL)
958         fclose(pFile);
959     RUNNER_ASSERT_MSG(smack_file_length == 0,
960         "SMACK file not empty.. Errno: " << errno);
961
962     pFile = fopen(SMACK_RULES_DIR OSP_PLATFORM_APP_ID, "rb");
963     RUNNER_ASSERT_MSG(pFile != NULL,
964         "SMACK file removed!. Errno: " << errno);
965     //// Is it empty?
966     fseek(pFile, 0L, SEEK_END);
967     smack_file_length = ftell(pFile);
968     if (pFile != NULL)
969         fclose(pFile);
970     RUNNER_ASSERT_MSG(smack_file_length == 0,
971         "SMACK file not empty.. Errno: " << errno);
972 }
973
974 static void read_gids(std::set<unsigned> &set, const char *file_path)
975 {
976     FILE *f = fopen(file_path, "r");
977     RUNNER_ASSERT_MSG(f != NULL, "Unable to open file " << file_path);
978     unsigned gid;
979     while (fscanf(f, "%u\n", &gid) == 1) {
980         set.insert(gid);
981     }
982 }
983
984 RUNNER_TEST(privilege_control05_add_shared_dir_readers)
985 {
986 #define  TEST_OBJ            "TEST_OBJECT"
987 #define  TEST_OBJ_SOME_OTHER "TEST_OBJA"
988 #define test_string_01       "TEST_raz TEST_OBJECT r-x-- -----"
989 #define test_string_21       "TEST_trzy TEST_OBJA -wx--\n"
990 #define test_string_22       "TEST_trzy TEST_OBJECT r-x-- -----\n"
991
992     int result;
993     int i;
994     int fd = -1;
995     char *path;
996
997     const char *app_labels_wrong[] = {"-TEST_raz", NULL};
998     const char *app_labels[] = {"TEST_raz", "TEST_dwa", "TEST_trzy", NULL};
999     const int READ_BUF_SIZE = 1000;
1000     char buf[READ_BUF_SIZE];
1001     FILE *file = NULL;
1002     struct smack_accesses *rules = NULL;
1003
1004     //test what happens when the label is not correct SMACK label
1005     result = smack_accesses_new(&rules);
1006     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in smack_accesses_new. Error: " << result);
1007
1008     result = add_shared_dir_readers(TEST_OBJ,app_labels_wrong);
1009     RUNNER_ASSERT_MSG(result == PC_ERR_INVALID_PARAM, "add_shared_dir_readers should fail here");
1010
1011     result = smack_have_access(app_labels_wrong[0],TEST_OBJ,"rx");
1012     RUNNER_ASSERT_MSG(result != 1, "add_shared_dir_readers should not grant permission here");
1013
1014     smack_accesses_free(rules);
1015
1016     //ok, now the correct list of apps
1017     result = smack_accesses_new(&rules);
1018     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in smack_accesses_new. Error: " << result);
1019
1020     for (i = 0; i < 3; i++) {
1021         (void)app_uninstall(app_labels[i]);
1022         result = app_install(app_labels[i]);
1023         RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in app_install.");
1024
1025         RUNNER_ASSERT(0 <= asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[i]));
1026         fd = open(path, O_WRONLY, 0644);
1027         RUNNER_ASSERT_MSG(fd != -1, "Error in opening file " << path);
1028
1029         if (i == 1) {
1030             result = smack_accesses_add(rules,app_labels[i],TEST_OBJ,"wt");
1031             RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
1032         }
1033         if (i == 2) {
1034             smack_accesses_free(rules);
1035             result = smack_accesses_new(&rules);
1036             result = smack_accesses_add(rules,app_labels[i],TEST_OBJ_SOME_OTHER,"wx");
1037             RUNNER_ASSERT_MSG(result == 0, "smack_accesses_add failed");
1038         }
1039         result = smack_accesses_apply(rules);
1040         RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
1041
1042         result = smack_accesses_save(rules, fd);
1043         RUNNER_ASSERT_MSG(fd != -1, "smack_accesses_apply failed");
1044
1045         free(path);
1046         close(fd);
1047     }
1048
1049     smack_accesses_free(rules);
1050
1051     // THE TEST - accesses
1052
1053     result = add_shared_dir_readers(TEST_OBJ,app_labels);
1054     RUNNER_ASSERT_MSG(result == 0, "add_shared_dir_readers failed");
1055
1056     result = smack_have_access(app_labels[0],TEST_OBJ,"rx");
1057     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1058
1059     result = smack_have_access(app_labels[1],TEST_OBJ,"rx");
1060     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1061
1062     result = smack_have_access(app_labels[2],TEST_OBJ,"rx");
1063     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1064
1065     result = smack_have_access(app_labels[1],TEST_OBJ,"rwxt");
1066     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1067
1068     result = smack_have_access(app_labels[2],TEST_OBJ_SOME_OTHER,"wx");
1069     RUNNER_ASSERT_MSG(result == 1, "add_shared_dir_readers ERROR");
1070
1071
1072     //TEST the operations on empty files
1073
1074     RUNNER_ASSERT(0 <= asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[0]));
1075     file = fopen(path, "r");
1076
1077     RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
1078
1079     RUNNER_ASSERT(NULL != fgets(buf, READ_BUF_SIZE, file));
1080     result = strcmp(buf, test_string_01);
1081     RUNNER_ASSERT_MSG( result != 0, "add_shared_dir_readers ERROR, file not formatted" << path );
1082
1083     free(path);
1084     fclose(file);
1085
1086     //TEST the operations on non empty files
1087     RUNNER_ASSERT(0 <= asprintf(&path, SMACK_RULES_DIR "/%s", app_labels[2]));
1088     file = NULL;
1089     file = fopen(path, "r");
1090     RUNNER_ASSERT_MSG(file, "fopen failed, errno:" << errno);
1091
1092     RUNNER_ASSERT(NULL != fgets(buf, READ_BUF_SIZE, file));
1093     result = strcmp(buf, test_string_21);
1094     RUNNER_ASSERT_MSG( result == 0, "add_shared_dir_readers ERROR, file not formatted" );
1095
1096     RUNNER_ASSERT(NULL != fgets(buf, READ_BUF_SIZE, file));
1097     result = strcmp(buf, test_string_22);
1098     RUNNER_ASSERT_MSG( result == 0, "add_shared_dir_readers ERROR, file not formatted" );
1099
1100     free(path);
1101     fclose(file);
1102 }
1103
1104
1105 /**
1106  * Set APP privileges.
1107  */
1108
1109 void check_groups(const char *dac_file)
1110 {
1111     std::set<unsigned> groups_check;
1112     read_gids(groups_check, LIBPRIVILEGE_APP_GROUP_LIST);
1113     read_gids(groups_check, dac_file);
1114
1115     int groups_cnt = getgroups(0, NULL);
1116     RUNNER_ASSERT_MSG(groups_cnt > 0, "Wrong number of supplementary groupsCnt");
1117     gid_t *groups_list = (gid_t*) calloc(groups_cnt, sizeof(gid_t));
1118     RUNNER_ASSERT_MSG(groups_list != NULL, "Memory allocation failed");
1119     RUNNER_ASSERT(-1 != getgroups(groups_cnt, groups_list));
1120
1121     for (int i = 0; i < groups_cnt; ++i) {
1122         //getgroups() can return multiple number of the same group
1123         //they are returned in sequence, so we will given number when last
1124         //element of this number is reached
1125         if ((i < groups_cnt - 1) && (groups_list[i + 1] == groups_list[i]))
1126             continue;
1127         if (groups_check.erase(groups_list[i]) == 0) {
1128             // getgroups() may also return process' main group
1129             if (groups_list[i] != getgid())
1130                 RUNNER_ASSERT_MSG(false, "Application belongs to unknown group (GID=" << groups_list[i] << ")");
1131         }
1132     }
1133     free(groups_list);
1134     std::string groups_left;
1135     for (std::set<unsigned>::iterator it = groups_check.begin(); it != groups_check.end(); it++) {
1136         groups_left.append(std::to_string(*it)).append(" ");
1137     }
1138     RUNNER_ASSERT_MSG(groups_check.empty(), "Application doesn't belong to some required groups: " << groups_left);
1139 }
1140
1141 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege)
1142 {
1143     int result;
1144
1145     // Preset exec label
1146     smack_lsetlabel(APP_SET_PRIV_PATH_REAL, APP_ID, SMACK_LABEL_EXEC);
1147     smack_lsetlabel(APP_SET_PRIV_PATH, APP_ID "_symlink", SMACK_LABEL_EXEC);
1148
1149     /**
1150      * TODO This test should also verify set_app_privilege behavior for OSP and
1151      * WRT apps. To do that we'll have to install real apps on device as a
1152      * precondition.
1153      */
1154
1155     // Set APP privileges
1156     result = set_app_privilege(APP_ID, NULL, APP_SET_PRIV_PATH);
1157     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
1158
1159     // Check if SMACK label really set
1160     char *label;
1161     result = smack_new_label_from_self(&label);
1162     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1163     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1164     result = strcmp(APP_ID, label);
1165     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1166
1167     // Check if DAC privileges really set
1168     RUNNER_ASSERT_MSG(getuid() == APP_UID, "Wrong UID");
1169     RUNNER_ASSERT_MSG(getgid() == APP_GID, "Wrong GID");
1170
1171     result = strcmp(getenv("HOME"), APP_HOME_DIR);
1172     RUNNER_ASSERT_MSG(result == 0, "Wrong HOME DIR");
1173
1174     result = strcmp(getenv("USER"), APP_USER_NAME);
1175     RUNNER_ASSERT_MSG(result == 0, "Wrong user USER NAME");
1176
1177     check_groups(LIBPRIVILEGE_TEST_DAC_FILE);
1178 }
1179
1180 /**
1181  * Set APP privileges. wgt.
1182  */
1183 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_wgt)
1184 {
1185     int result;
1186
1187     result = app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS_WGT, 1);
1188     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1189         " Error enabling app permissions. Result: " << result);
1190
1191     result = test_have_all_accesses(rules_wgt);
1192     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1193
1194     result = set_app_privilege(WGT_APP_ID, "wgt", WGT_APP_PATH);
1195     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
1196
1197     // Check if SMACK label really set
1198     char *label;
1199     result = smack_new_label_from_self(&label);
1200     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1201     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1202     result = strcmp(WGT_APP_ID, label);
1203     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1204
1205
1206     check_groups(LIBPRIVILEGE_TEST_DAC_FILE_WGT);
1207 }
1208
1209 /**
1210  * Set APP privileges. wgt_partner.
1211  */
1212 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_wgt_partner)
1213 {
1214     int result;
1215
1216     result = app_enable_permissions(WGT_PARTNER_APP_ID, APP_TYPE_WGT_PARTNER, PRIVS_WGT, 1);
1217     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1218         " Error enabling app permissions. Result: " << result);
1219
1220     result = test_have_all_accesses(rules_wgt_partner);
1221     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1222
1223     result = set_app_privilege(WGT_PARTNER_APP_ID, "wgt_partner", WGT_PARTNER_APP_PATH);
1224     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
1225
1226     // Check if SMACK label really set
1227     char *label;
1228     result = smack_new_label_from_self(&label);
1229     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1230     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1231     result = strcmp(WGT_PARTNER_APP_ID, label);
1232     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1233
1234     check_groups(LIBPRIVILEGE_TEST_DAC_FILE_WGT);
1235 }
1236
1237 /**
1238  * Set APP privileges. wgt_platform.
1239  */
1240 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_wgt_platform)
1241 {
1242     int result;
1243
1244     result = app_enable_permissions(WGT_PLATFORM_APP_ID, APP_TYPE_WGT_PLATFORM, PRIVS_WGT, 1);
1245     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1246         " Error enabling app permissions. Result: " << result);
1247
1248     result = test_have_all_accesses(rules_wgt_platform);
1249     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1250
1251     result = set_app_privilege(WGT_PLATFORM_APP_ID, "wgt_platform", WGT_PLATFORM_APP_PATH);
1252     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
1253
1254     // Check if SMACK label really set
1255     char *label;
1256     result = smack_new_label_from_self(&label);
1257     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1258     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1259     result = strcmp(WGT_PLATFORM_APP_ID, label);
1260     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1261
1262     check_groups(LIBPRIVILEGE_TEST_DAC_FILE_WGT);
1263 }
1264
1265 /**
1266  * Set APP privileges. osp app.
1267  */
1268 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_osp)
1269 {
1270     int result;
1271
1272     result = app_enable_permissions(OSP_APP_ID, APP_TYPE_OSP, PRIVS_OSP, 1);
1273     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1274         " Error enabling app permissions. Result: " << result);
1275
1276     result = test_have_all_accesses(rules_osp);
1277     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1278
1279     result = set_app_privilege(OSP_APP_ID, NULL, OSP_APP_PATH);
1280     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
1281
1282     // Check if SMACK label really set
1283     char *label;
1284     result = smack_new_label_from_self(&label);
1285     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1286     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1287     result = strcmp(OSP_APP_ID, label);
1288     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1289
1290     check_groups(LIBPRIVILEGE_TEST_DAC_FILE_OSP);
1291 }
1292
1293 /**
1294  * Set APP privileges. partner osp app.
1295  */
1296 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_osp_partner)
1297 {
1298     int result;
1299
1300     result = app_enable_permissions(OSP_PARTNER_APP_ID, APP_TYPE_OSP_PARTNER, PRIVS_OSP, 1);
1301     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1302         " Error enabling app permissions. Result: " << result);
1303
1304     result = test_have_all_accesses(rules_osp_partner);
1305     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1306
1307     result = set_app_privilege(OSP_PARTNER_APP_ID, NULL, OSP_PARTNER_APP_PATH);
1308     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
1309
1310     // Check if SMACK label really set
1311     char *label;
1312     result = smack_new_label_from_self(&label);
1313     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1314     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1315     result = strcmp(OSP_PARTNER_APP_ID, label);
1316     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1317
1318     check_groups(LIBPRIVILEGE_TEST_DAC_FILE_OSP);
1319 }
1320
1321 /**
1322  * Set APP privileges. platform osp app.
1323  */
1324 RUNNER_CHILD_TEST(privilege_control05_set_app_privilege_osp_platform)
1325 {
1326     int result;
1327
1328     result = app_enable_permissions(OSP_PLATFORM_APP_ID, APP_TYPE_OSP_PLATFORM, PRIVS_OSP, 1);
1329     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1330         " Error enabling app permissions. Result: " << result);
1331
1332     result = test_have_all_accesses(rules_osp_platform);
1333     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1334
1335     result = set_app_privilege(OSP_PLATFORM_APP_ID, NULL, OSP_PLATFORM_APP_PATH);
1336     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS, "Error in set_app_privilege. Error: " << result);
1337
1338     // Check if SMACK label really set
1339     char *label;
1340     result = smack_new_label_from_self(&label);
1341     RUNNER_ASSERT_MSG(result == 0, "Error getting current process label");
1342     RUNNER_ASSERT_MSG(label != NULL, "Process label is not set");
1343     result = strcmp(OSP_PLATFORM_APP_ID, label);
1344     RUNNER_ASSERT_MSG(result == 0, "Process label " << label << " is incorrect");
1345
1346     check_groups(LIBPRIVILEGE_TEST_DAC_FILE_OSP);
1347 }
1348
1349 RUNNER_TEST(privilege_control08_app_give_access)
1350 {
1351     const char *subject = "lkjq345v34sfa";
1352     const char *object = "lk9290f92lkjz";
1353     smack_accesses *tmp = NULL;
1354
1355     RUNNER_ASSERT(0 == smack_accesses_new(&tmp));
1356
1357     SmackUniquePtr smack(tmp, smack_accesses_free);
1358
1359     RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "r--a-"));
1360     RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
1361
1362     app_give_access(subject, object, "wt");
1363
1364     RUNNER_ASSERT(1 == smack_have_access(subject, object, "rwat"));
1365     RUNNER_ASSERT(0 == smack_have_access(subject, object, "x"));
1366
1367     app_revoke_access(subject, object);
1368
1369     RUNNER_ASSERT(1 == smack_have_access(subject, object, "ra"));
1370     RUNNER_ASSERT(0 == smack_have_access(subject, object, "w"));
1371     RUNNER_ASSERT(0 == smack_have_access(subject, object, "x"));
1372     RUNNER_ASSERT(0 == smack_have_access(subject, object, "t"));
1373
1374     RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "-"));
1375     RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
1376 }
1377
1378 RUNNER_TEST(privilege_control09_app_give_access)
1379 {
1380     const char *subject = "ljk132flkjv";
1381     const char *object = "jjsiqsc32vs";
1382     smack_accesses *tmp = NULL;
1383
1384     RUNNER_ASSERT(0 == smack_accesses_new(&tmp));
1385
1386     SmackUniquePtr smack(tmp, smack_accesses_free);
1387
1388     RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "---t-"));
1389     RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
1390
1391     RUNNER_ASSERT(PC_OPERATION_SUCCESS == app_give_access(subject, object, "rw"));
1392     RUNNER_ASSERT(PC_OPERATION_SUCCESS == app_give_access(subject, object, "rwx"));
1393
1394     RUNNER_ASSERT(1 == smack_have_access(subject, object, "rwxt"));
1395     RUNNER_ASSERT(0 == smack_have_access(subject, object, "a"));
1396
1397     RUNNER_ASSERT(PC_OPERATION_SUCCESS == app_revoke_access(subject, object));
1398
1399     RUNNER_ASSERT(1 == smack_have_access(subject, object, "t"));
1400     RUNNER_ASSERT(0 == smack_have_access(subject, object, "r"));
1401     RUNNER_ASSERT(0 == smack_have_access(subject, object, "w"));
1402     RUNNER_ASSERT(0 == smack_have_access(subject, object, "x"));
1403     RUNNER_ASSERT(0 == smack_have_access(subject, object, "a"));
1404
1405     RUNNER_ASSERT(0 == smack_accesses_add(smack.get(), subject, object, "-----"));
1406     RUNNER_ASSERT(0 == smack_accesses_apply(smack.get()));
1407 }
1408
1409 /**
1410  * Add new API feature
1411  */
1412 RUNNER_TEST(privilege_control11_add_api_feature)
1413 {
1414     int result;
1415
1416     remove_smack_files();
1417
1418
1419     // argument validation
1420     result = add_api_feature(APP_TYPE_OSP, NULL, NULL, NULL, 0);
1421     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1422
1423     result = add_api_feature(APP_TYPE_OSP,"", NULL, NULL, 0);
1424     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1425
1426
1427     // already existing features
1428     result = add_api_feature(APP_TYPE_OSP,"http://tizen.org/privilege/messaging.read", NULL, NULL, 0);
1429     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1430
1431     result = add_api_feature(APP_TYPE_WGT,"http://tizen.org/privilege/messaging.sms", NULL, NULL, 0);
1432     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1433
1434     result = add_api_feature(APP_TYPE_OTHER,"http://tizen.org/privilege/messaging", NULL, NULL, 0);
1435     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1436
1437     result = add_api_feature(APP_TYPE_OTHER,"http://tizen.org/messaging", NULL, NULL, 0);
1438     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1439
1440     result = add_api_feature(APP_TYPE_OTHER,"http://messaging", NULL, NULL, 0);
1441     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1442
1443     result = add_api_feature(APP_TYPE_OTHER,"messaging.read", NULL, NULL, 0);
1444     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1445
1446
1447     // empty features
1448     result = add_api_feature(APP_TYPE_OSP,"blahblah", NULL, NULL, 0);
1449     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1450
1451     result = add_api_feature(APP_TYPE_WGT,"blahblah", NULL, NULL, 0);
1452     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1453
1454     result = add_api_feature(APP_TYPE_OTHER,"blahblah", NULL, NULL, 0);
1455     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1456
1457
1458     // smack files existence
1459     result = file_exists(OSP_BLAHBLAH);
1460     RUNNER_ASSERT(result == -1);
1461
1462     result = file_exists(WRT_BLAHBLAH);
1463     RUNNER_ASSERT(result == -1);
1464
1465     result = file_exists(OTHER_BLAHBLAH);
1466     RUNNER_ASSERT(result == -1);
1467
1468
1469     // empty rules
1470     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, { NULL }, NULL, 0);
1471     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1472     result = file_exists(OSP_BLAHBLAH);
1473     RUNNER_ASSERT(result == -1);
1474
1475     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "", NULL }, NULL, 0);
1476     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1477     result = file_exists(OSP_BLAHBLAH);
1478     RUNNER_ASSERT(result == 0);
1479     remove_smack_files();
1480
1481     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { " \t\n", "\t \n", "\n\t  ", NULL }, NULL, 0);
1482     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1483     result = file_exists(OSP_BLAHBLAH);
1484     RUNNER_ASSERT(result == 0);
1485     remove_smack_files();
1486
1487
1488     // malformed rules
1489     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "malformed", NULL }, NULL, 0);
1490     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1491     result = file_exists(OSP_BLAHBLAH);
1492     RUNNER_ASSERT(result == -1);
1493
1494     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "malformed malformed", NULL }, NULL, 0);
1495     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1496     result = file_exists(OSP_BLAHBLAH);
1497     RUNNER_ASSERT(result == -1);
1498
1499     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "-malformed malformed rwxat", NULL }, NULL, 0);
1500     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1501     result = file_exists(OSP_BLAHBLAH);
1502     RUNNER_ASSERT(result == -1);
1503
1504     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "~/\"\\ malformed rwxat", NULL }, NULL, 0);
1505     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1506     result = file_exists(OSP_BLAHBLAH);
1507     RUNNER_ASSERT(result == -1);
1508
1509     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "subject object rwxat something else", NULL }, NULL, 0);
1510     RUNNER_ASSERT(result == PC_ERR_INVALID_PARAM);
1511     result = file_exists(OSP_BLAHBLAH);
1512     RUNNER_ASSERT(result == -1);
1513
1514
1515     // correct rules
1516     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "malformed malformed maaaaaalformed", NULL }, NULL, 0);
1517     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1518     osp_blahblah_check(__LINE__, { "malformed malformed r--a- -----" });
1519     remove_smack_files();
1520
1521     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) { "subject object foo", NULL }, NULL, 0);
1522     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1523     osp_blahblah_check(__LINE__, { "subject object ----- -----" });
1524     remove_smack_files();
1525
1526     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {
1527             "subject    object\t rwxat",
1528             " \t \n",
1529             "subject2\tobject2 txarw",
1530             "",
1531             NULL
1532         }, NULL, 0);
1533     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1534     osp_blahblah_check(__LINE__, { "subject object rwxat -----", "subject2 object2 rwxat -----"});
1535     remove_smack_files();
1536
1537     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {
1538             "Sub::jE,ct object a-RwX",
1539             NULL
1540         }, NULL, 0);
1541     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1542     osp_blahblah_check(__LINE__, { "Sub::jE,ct object rwxa- -----"});
1543     remove_smack_files();
1544
1545     // TODO For now identical/complementary rules are not merged.
1546     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {
1547             "subject object rwxat",
1548             " \t \n",
1549             "subject object txarw",
1550             "",
1551             NULL
1552         }, NULL, 0);
1553     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1554     osp_blahblah_check(__LINE__, { "subject object rwxat -----", "subject object rwxat -----"});
1555     remove_smack_files();
1556
1557
1558     // empty group ids
1559     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {0,1,2},0);
1560     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1561     osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1562     result = file_exists(OSP_BLAHBLAH_DAC);
1563     RUNNER_ASSERT(result == -1);
1564     remove_smack_files();
1565
1566
1567     // valid group ids
1568     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {0,1,2},3);
1569     printf("%d \n", result);
1570     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1571     osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1572     osp_blahblah_dac_check(__LINE__, {0,1,2});
1573     remove_smack_files();
1574
1575     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {0,1,2},1);
1576     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1577     osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1578     osp_blahblah_dac_check(__LINE__, {0});
1579     remove_smack_files();
1580
1581     result = add_api_feature(APP_TYPE_OSP, BLAHBLAH_FEATURE, (const char*[]) {"a a a",NULL},(const gid_t[]) {1,1,1},3);
1582     RUNNER_ASSERT(result == PC_OPERATION_SUCCESS);
1583     osp_blahblah_check(__LINE__, { "a a ---a- -----"});
1584     osp_blahblah_dac_check(__LINE__, {1,1,1});
1585     remove_smack_files();
1586 }
1587
1588 /*
1589  * Check app_install function
1590  */
1591 RUNNER_TEST(privilege_control01_app_install)
1592 {
1593     int result;
1594     int fd = -1;
1595
1596     unlink(SMACK_RULES_DIR APP_ID);
1597
1598     app_uninstall(APP_ID);
1599
1600     result = app_install(APP_ID);
1601     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result << ". Errno: " << strerror(errno));
1602
1603     // checking if file really exists
1604     fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
1605     RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << SMACK_RULES_DIR << APP_ID << " : " << fd << ". Errno: " << strerror(errno));
1606     close(fd);
1607
1608     // try install second time app with the same ID - it should pass.
1609     result = app_install(APP_ID);
1610     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result << ". Errno: " << strerror(errno));
1611 }
1612
1613 /*
1614  * Check app_install function
1615  */
1616 RUNNER_TEST(privilege_control07_app_uninstall)
1617 {
1618     int result;
1619     int fd = -1;
1620
1621     result = app_uninstall(APP_ID);
1622     RUNNER_ASSERT_MSG(result == 0, "app_uninstall returned " << result << ". Errno: " << strerror(errno));
1623
1624     // checking if file really exists
1625     fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
1626     RUNNER_ASSERT_MSG(fd == -1, "SMACK file NOT deleted after app_uninstall");
1627     close(fd);
1628 }
1629
1630 void checkOnlyAvAccess(const char *av_id, const char *app_id, const char *comment)
1631 {
1632     int result;
1633     result = smack_have_access(av_id, app_id, "rwx");
1634     RUNNER_ASSERT_MSG(result == 1,
1635         "Error while checking " << av_id << " rwx access to "
1636         << app_id << " " << comment << " Result: " << result);
1637     result = smack_have_access(av_id, app_id, "a");
1638     RUNNER_ASSERT_MSG(result == 0,
1639         "Error while checking " << av_id << " a access to "
1640         << app_id << " " << comment << " Result: " << result);
1641     result = smack_have_access(av_id, app_id, "t");
1642     RUNNER_ASSERT_MSG(result == 0,
1643         "Error while checking " << av_id << " t access to "
1644         << app_id << " " << comment << " Result: " << result);
1645 }
1646
1647 /*
1648  * Check app_register_av function
1649  * Notice that this test case may have no sense if previous would fail (privilege_control06_app_install)
1650  */
1651 RUNNER_TEST(privilege_control10_app_register_av)
1652 {
1653     int result;
1654
1655     // cleaning
1656     smack_revoke_subject(APP_TEST_AV_1);
1657     smack_revoke_subject(APP_TEST_AV_2);
1658
1659     cleaning_smack_app_files();
1660     cleaning_smack_database_files();
1661
1662     // Adding two apps before antivir
1663     result = app_install(APP_TEST_APP_1);
1664     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result << ". Errno: " << strerror(errno));
1665
1666     result = app_install(APP_TEST_APP_2);
1667     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result << ". Errno: " << strerror(errno));
1668
1669     // Adding antivir
1670     result = app_register_av(APP_TEST_AV_1);
1671     RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result << ". Errno: " << strerror(errno));
1672
1673     // Checking added apps accesses
1674     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_1)");
1675     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_1)");
1676
1677     // Adding third app
1678     result = app_install(APP_TEST_APP_3);
1679     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result << ". Errno: " << strerror(errno));
1680
1681     // Checking app accesses
1682     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "app_install(APP_TEST_APP_3)");
1683     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "app_install(APP_TEST_APP_3)");
1684     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_3, "app_install(APP_TEST_APP_3)");
1685
1686     // Adding second antivir
1687     result = app_register_av(APP_TEST_AV_2);
1688     RUNNER_ASSERT_MSG(result == 0, "app_register_av returned " << result << ". Errno: " << strerror(errno));
1689
1690     // Checking app accesses
1691     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
1692     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
1693     checkOnlyAvAccess(APP_TEST_AV_1, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
1694     checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_1, "app_register_av(APP_TEST_AV_2)");
1695     checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_2, "app_register_av(APP_TEST_AV_2)");
1696     checkOnlyAvAccess(APP_TEST_AV_2, APP_TEST_APP_3, "app_register_av(APP_TEST_AV_2)");
1697
1698     // cleaning
1699     smack_revoke_subject(APP_TEST_AV_1);
1700     smack_revoke_subject(APP_TEST_AV_2);
1701
1702     cleaning_smack_app_files();
1703     cleaning_smack_database_files();
1704 }
1705
1706 /**
1707  * Grant SMACK permissions based on permissions list.
1708  */
1709 RUNNER_TEST(privilege_control11_app_enable_permissions)
1710 {
1711     int result;
1712     int smack_file_length;
1713     FILE *pFile;
1714
1715 /**
1716  * Test - Enabling all permissions with persistant mode enabled
1717  */
1718
1719     result = app_revoke_permissions(APP_ID);
1720     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1721         "Error revoking app permissions. Result: " << result);
1722
1723     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1724     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1725         " Error enabling app permissions. Result: " << result);
1726
1727     // Check if the accesses are realy applied..
1728     result = test_have_all_accesses(rules2);
1729     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1730
1731     //// File exists?
1732     pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1733     RUNNER_ASSERT_MSG(pFile != NULL,
1734         "SMACK file NOT created!. Errno: " << errno);
1735
1736     //// Is it empty?
1737     fseek(pFile, 0L, SEEK_END);
1738     smack_file_length = ftell(pFile);
1739     RUNNER_ASSERT_MSG(smack_file_length > 0,
1740         "SMACK file empty with persistant mode 1. Errno: " << errno);
1741
1742     if (pFile != NULL)
1743         fclose(pFile);
1744
1745     // Clean up
1746     result = app_revoke_permissions(APP_ID);
1747     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1748         "Error revoking app permissions. Result: " << result);
1749
1750 /**
1751  * Test - Enabling all permissions with persistant mode disabled
1752  */
1753
1754     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 0);
1755     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1756         " Error enabling app permissions. Result: " << result);
1757
1758     // Check if the accesses are realy applied..
1759     result = test_have_all_accesses(rules2);
1760     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1761
1762     //// File exists?
1763     pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1764     RUNNER_ASSERT_MSG(pFile != NULL,
1765         "SMACK file NOT created!. Errno: " << errno);
1766
1767     //// Is it empty?
1768     fseek(pFile, 0L, SEEK_END);
1769     smack_file_length = ftell(pFile);
1770     RUNNER_ASSERT_MSG(smack_file_length == 0,
1771         "SMACK file not empty with persistant mode 0. Errno: " << errno);
1772
1773     if (pFile != NULL)
1774         fclose(pFile);
1775
1776     // Clean up
1777     result = app_revoke_permissions(APP_ID);
1778     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1779         "Error revoking app permissions. Result: " << result);
1780
1781 /**
1782  * Test - Enabling all permissions in two complementary files
1783  */
1784
1785     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_R_AND_NO_R, 1);
1786     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1787         " Error enabling app permissions. Result: " << result);
1788
1789     // Check if the accesses are realy applied..
1790     result = test_have_all_accesses(rules2);
1791     RUNNER_ASSERT_MSG(result == 1, "Permissions not added.");
1792
1793     //// File exists?
1794     pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1795     RUNNER_ASSERT_MSG(pFile != NULL,
1796         "SMACK file NOT created!. Errno: " << errno);
1797
1798     //// Is it empty?
1799     fseek(pFile, 0L, SEEK_END);
1800     smack_file_length = ftell(pFile);
1801     RUNNER_ASSERT_MSG(smack_file_length > 0,
1802         "SMACK file empty with persistant mode 1. Errno: " << errno);
1803
1804     if (pFile != NULL)
1805         fclose(pFile);
1806
1807     // Clean up
1808     result = app_revoke_permissions(APP_ID);
1809     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1810         "Error revoking app permissions. Result: " << result);
1811
1812 /**
1813  * Test - Enabling some permissions and then enabling complementary permissions
1814  */
1815
1816     // Enable permission for rules 2 no r
1817     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R, 1);
1818     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1819         " Error enabling app permissions without r. Result: " << result);
1820
1821     // Check if the accesses are realy applied..
1822     result = test_have_all_accesses(rules2_no_r);
1823     RUNNER_ASSERT_MSG(result == 1, "Permissions without r not added.");
1824
1825     //// File exists?
1826     pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1827     RUNNER_ASSERT_MSG(pFile != NULL,
1828         "SMACK file NOT created!. Errno: " << errno);
1829
1830     //// Is it empty?
1831     fseek(pFile, 0L, SEEK_END);
1832     smack_file_length = ftell(pFile);
1833     RUNNER_ASSERT_MSG(smack_file_length > 0,
1834         "SMACK file empty with persistant mode 1. Errno: " << errno);
1835
1836     if (pFile != NULL)
1837         fclose(pFile);
1838
1839     // Enable permission for rules 2
1840     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1841     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1842         " Error enabling app all permissions. Result: " << result);
1843
1844     // Check if the accesses are realy applied..
1845     result = test_have_all_accesses(rules2);
1846     RUNNER_ASSERT_MSG(result == 1, "Permissions all not added.");
1847
1848     // Clean up
1849     result = app_revoke_permissions(APP_ID);
1850     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1851         "Error revoking app permissions. Result: " << result);
1852
1853 /**
1854  * Test - Enabling some permissions and then enabling all permissions
1855  */
1856
1857     // Enable permission for rules 2 no r
1858     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R, 1);
1859     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1860         " Error enabling app permissions without r. Result: " << result);
1861
1862     // Check if the accesses are realy applied..
1863     result = test_have_all_accesses(rules2_no_r);
1864     RUNNER_ASSERT_MSG(result == 1, "Permissions without r not added.");
1865
1866     //// File exists?
1867     pFile = fopen(SMACK_RULES_DIR APP_ID, "rb");
1868     RUNNER_ASSERT_MSG(pFile != NULL,
1869         "SMACK file NOT created!. Errno: " << errno);
1870
1871     //// Is it empty?
1872     fseek(pFile, 0L, SEEK_END);
1873     smack_file_length = ftell(pFile);
1874     RUNNER_ASSERT_MSG(smack_file_length > 0,
1875         "SMACK file empty with persistant mode 1. Errno: " << errno);
1876
1877     if (pFile != NULL)
1878         fclose(pFile);
1879
1880     // Enable permission for rules 2
1881     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_R, 1);
1882     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1883         " Error enabling app permissions with only r. Result: " << result);
1884
1885     // Check if the accesses are realy applied..
1886     result = test_have_all_accesses(rules2);
1887     RUNNER_ASSERT_MSG(result == 1, "Permissions with only r not added.");
1888
1889     // Clean up
1890     result = app_revoke_permissions(APP_ID);
1891     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1892         "Error revoking app permissions. Result: " << result);
1893 }
1894
1895 /*
1896  * Until app_disable_permissions is not fixed this test should remain
1897  * commented
1898  */
1899 /**
1900  * Remove previously granted SMACK permissions based on permissions list.
1901  */
1902 /*RUNNER_TEST(privilege_control12_app_disable_permissions)
1903 {
1904 */
1905 /**
1906  * Test - disable all granted permissions.
1907  */
1908 /*    int result;
1909
1910     // Prepare permissions that we want to disable
1911     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1912     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1913             " Error enabling app permissions. Result: " << result);
1914
1915     // Disable permissions
1916     result = app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2);
1917     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1918             "Error disabling app permissions. Result: " << result);
1919
1920     // Are all the permissions disabled?
1921     result = test_have_any_accesses(rules2);
1922     RUNNER_ASSERT_MSG(result!=1, "Not all permisions disabled.");
1923 */
1924 /**
1925  * Test - disable some granted permissions leaving non complementary and then disabling those too.
1926  */
1927 /*
1928     // Prepare permissions that will not be disabled
1929     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS, 1);
1930     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1931             " Error adding app first permissions. Result: " << result);
1932
1933     // Prepare permissions that we want to disable
1934     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1935     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1936             " Error adding app second permissions. Result: " << result);
1937
1938     // Disable second permissions
1939     result = app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2);
1940     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1941             "Error disabling app second permissions. Result: " << result);
1942
1943     // Are all second permissions disabled?
1944     result = test_have_any_accesses(rules2);
1945     RUNNER_ASSERT_MSG(result!=1, "Not all first permisions disabled.");
1946
1947     // Are all first permissions not disabled?
1948     result = test_have_all_accesses(rules);
1949     RUNNER_ASSERT_MSG(result==1, "Some of second permissions disabled.");
1950
1951     // Disable first permissions
1952     result = app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS);
1953     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1954             "Error disabling app first permissions. Result: " << result);
1955
1956     // Are all second permissions disabled?
1957     result = test_have_any_accesses(rules);
1958     RUNNER_ASSERT_MSG(result!=1, "Not all second permisions disabled.");
1959 */
1960 /**
1961  * Test - disable only no r granted permissions.
1962  */
1963 /*
1964     // Prepare permissions
1965     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
1966     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1967             " Error adding app permissions. Result: " << result);
1968
1969     // Disable same permissions without r
1970     result = app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R);
1971     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1972             "Error disabling app no r permissions. Result: " << result);
1973
1974     // Is any r permissions disabled?
1975     result = test_have_all_accesses(rules2_r);
1976     RUNNER_ASSERT_MSG(result==1, "Some of r permissions disabled.");
1977     // Are all no r permissions disabled?
1978     result = test_have_any_accesses(rules2_no_r);
1979     RUNNER_ASSERT_MSG(result!=1, "Not all no r permissions disabled.");
1980
1981     // Prepare permissions
1982     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2_NO_R, 1);
1983     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1984             " Error adding app no r permissions. Result: " << result);
1985
1986     // Disable all permissions
1987     result = app_disable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2);
1988     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
1989             "Error disabling app permissions. Result: " << result);
1990 }
1991 */
1992 /**
1993  * Reset SMACK permissions for an application by revoking all previously
1994  * granted rules and enabling them again from a rules file from disk.
1995  */
1996
1997 RUNNER_TEST(privilege_control13_app_reset_permissions)
1998 {
1999     int result;
2000
2001 /**
2002  * Test - doing reset and checking if rules exist again.
2003  */
2004
2005     // Prepare permissions to reset
2006     result = app_enable_permissions(APP_ID, APP_TYPE_OTHER, PRIVS2, 1);
2007     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2008         " Error adding app permissions. Result: " << result);
2009
2010     // Reset permissions
2011     result = app_reset_permissions(APP_ID);
2012     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2013         "Error reseting app permissions. Result: " << result);
2014
2015     // Are all second permissions not disabled?
2016     result = test_have_all_accesses(rules2);
2017     RUNNER_ASSERT_MSG(result == 1, "Not all permissions added.");
2018
2019     // Disable permissions
2020     result = app_revoke_permissions(APP_ID);
2021     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2022         "Error disabling app permissions. Result: " << result);
2023 }
2024
2025 /**
2026  * Make two applications "friends", by giving them both full permissions on
2027  * each other.
2028  */
2029 RUNNER_TEST(privilege_control14_app_add_friend)
2030 {
2031     int result;
2032
2033 /**
2034  * Test - making friends with no permissions on each other
2035  */
2036
2037     result = app_revoke_permissions(APP_FRIEND_1);
2038     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2039         "Error revoking app permissions. Result: " << result);
2040     result = app_revoke_permissions(APP_FRIEND_2);
2041     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2042         "Error revoking app permissions. Result: " << result);
2043
2044     app_uninstall(APP_FRIEND_1);
2045     app_uninstall(APP_FRIEND_2);
2046
2047     // Installing friends to be
2048     result = app_install(APP_FRIEND_1);
2049     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2050         " Error installing first app. Result: " << result);
2051     result = app_install(APP_FRIEND_2);
2052     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2053         " Error installing second app. Result: " << result);
2054
2055     // Making friends
2056     result = app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
2057     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2058         " Error making friends. Errno: " << result);
2059
2060     // Checking if friends were made
2061     result = smack_have_access(APP_FRIEND_1, APP_FRIEND_2, "wrxat");
2062     RUNNER_ASSERT_MSG(result == 1,
2063         " Error first one sided friednship failed. Result: " << result);
2064     result = smack_have_access(APP_FRIEND_2, APP_FRIEND_1, "wrxat");
2065     RUNNER_ASSERT_MSG(result == 1,
2066         " Error second one sided friednship failed. Result: " << result);
2067
2068     // Clean up
2069     result = app_revoke_permissions(APP_FRIEND_1);
2070     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2071         "Error revoking app permissions. Result: " << result);
2072     result = app_revoke_permissions(APP_FRIEND_2);
2073     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2074         "Error revoking app permissions. Result: " << result);
2075
2076     app_uninstall(APP_FRIEND_1);
2077     app_uninstall(APP_FRIEND_2);
2078
2079 /**
2080  * Test - making friends with nonexisting friend
2081  */
2082
2083     // Installing one friend
2084     result = app_install(APP_FRIEND_1);
2085     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2086         " Error installing first app. Errno: " << result);
2087
2088     // Adding imaginairy friend as second
2089     result = app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
2090     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2091         " Error making friends (first) with imaginairy friend failed. Result: "
2092         << result);
2093     // Adding imaginairy friend as first
2094     result = app_add_friend(APP_FRIEND_2, APP_FRIEND_1);
2095     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2096         " Error making friends (second) with imaginairy friend failed. Result: "
2097         << result);
2098     // Clean up
2099     result = app_revoke_permissions(APP_FRIEND_1);
2100     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2101         "Error revoking app permissions. Result: " << result);
2102     result = app_revoke_permissions(APP_FRIEND_2);
2103     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2104         "Error revoking app permissions. Result: " << result);
2105
2106     app_uninstall(APP_FRIEND_1);
2107     app_uninstall(APP_FRIEND_2);
2108
2109 /**
2110  * Test - making friends with some permissions already added
2111  */
2112     unsigned int i;
2113     unsigned int j;
2114
2115     struct smack_accesses *rulesFriend = NULL;
2116
2117     std::vector<std::string> accessesFriend =
2118     { "r", "w", "x", "rw", "rx", "wx", "rwx", "rwxat" };
2119
2120     // Installing friends to be
2121     result = app_install(APP_FRIEND_1);
2122     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2123         " Error installing first app. Result: " << result);
2124     result = app_install(APP_FRIEND_2);
2125     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2126         " Error installing second app. Result: " << result);
2127
2128     for (i = 0; i < accessesFriend.size(); ++i)
2129     {
2130         for (j = 0; j < accessesFriend.size(); ++j)
2131         {
2132             // Adding rules before making friends
2133             result = smack_accesses_new(&rulesFriend);
2134             RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2135                 "Error in smack_accesses_new. Result: " << result);
2136
2137             result = smack_accesses_add(rulesFriend,
2138                 APP_FRIEND_1, APP_FRIEND_2, accessesFriend[i].c_str());
2139             RUNNER_ASSERT_MSG(result == 0,
2140                 "Unable to add modify rulesFirend (first). Result: " << result);
2141             result = smack_accesses_add(rulesFriend, APP_FRIEND_2,
2142                 APP_FRIEND_1, accessesFriend[j].c_str());
2143             RUNNER_ASSERT_MSG(result == 0,
2144                 "Unable to add modify rulesFirend (second). Result: " << result);
2145
2146             result = smack_accesses_apply(rulesFriend);
2147             RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2148                 "Error in smack_accesses_apply. Result: " << result);
2149
2150             // Adding friends
2151             result = app_add_friend(APP_FRIEND_1, APP_FRIEND_2);
2152             RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2153                 " Error making friends. Result: " << result);
2154
2155             // Checking if friends were made
2156             result = smack_have_access(APP_FRIEND_1, APP_FRIEND_2, "wrxat");
2157             RUNNER_ASSERT_MSG(result == 1,
2158                 " Error first one sided friednship failed. Result: " << result);
2159             result = smack_have_access(APP_FRIEND_2, APP_FRIEND_1, "wrxat");
2160             RUNNER_ASSERT_MSG(result == 1,
2161                 " Error second one sided friednship failed. Result: " << result);
2162
2163             // Deleting all rules between friends
2164             smack_accesses_add_modify(rulesFriend,
2165                 APP_FRIEND_1, APP_FRIEND_2,"","rwxat");
2166             smack_accesses_add_modify(rulesFriend,
2167                 APP_FRIEND_2, APP_FRIEND_1,"","rwxat");
2168
2169             result = smack_accesses_apply(rulesFriend);
2170
2171             smack_accesses_free(rulesFriend);
2172             rulesFriend = NULL;
2173         }
2174     }
2175
2176     // Clean up
2177     result = app_revoke_permissions(APP_FRIEND_1);
2178     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2179         "Error revoking app permissions. Result: " << result);
2180     result = app_revoke_permissions(APP_FRIEND_2);
2181     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
2182         "Error revoking app permissions. Result: " << result);
2183
2184     app_uninstall(APP_FRIEND_1);
2185     app_uninstall(APP_FRIEND_2);
2186 }
2187
2188 static void smack_set_random_label_based_on_pid_on_self(void)
2189 {
2190     int result;
2191     std::stringstream ss;
2192
2193     ss << "s-" << getpid() << "-" << getppid();
2194     result = smack_set_label_for_self(ss.str().c_str());
2195     RUNNER_ASSERT_MSG(result == 0, "smack_set_label_for_self("
2196         << ss.str().c_str() << ") failed");
2197 }
2198
2199 static void smack_unix_sock_server(int sock)
2200 {
2201     int fd, result;
2202     char *smack_label;
2203
2204     alarm(2);
2205     fd = accept(sock, NULL, NULL);
2206     alarm(0);
2207     if (fd < 0)
2208         return;
2209     result = smack_new_label_from_self(&smack_label);
2210     if (result != 0) {
2211         close(fd);
2212         close(sock);
2213         free(smack_label);
2214         RUNNER_ASSERT_MSG(0, "smack_new_label_from_self() failed");
2215     }
2216     result = write(fd, smack_label, strlen(smack_label));
2217     if (result != (int)strlen(smack_label)) {
2218         close(fd);
2219         close(sock);
2220         free(smack_label);
2221         RUNNER_ASSERT_MSG(0, "write() failed: " << strerror(errno));
2222     }
2223     close(fd);
2224     free(smack_label);
2225 }
2226
2227 RUNNER_TEST(privilege_control15_app_id_from_socket)
2228 {
2229     int pid;
2230     struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
2231
2232     unlink(SOCK_PATH);
2233     pid = fork();
2234     RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
2235
2236     smack_set_random_label_based_on_pid_on_self();
2237
2238     if (!pid) { /* child process, server */
2239         int sock, result;
2240
2241         /* Set the process label before creating a socket */
2242         sock = socket(AF_UNIX, SOCK_STREAM, 0);
2243         RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno));
2244         result = bind(sock,
2245             (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
2246         if (result != 0) {
2247             close(sock);
2248             RUNNER_ASSERT_MSG(0, "bind failed: " << strerror(errno));
2249         }
2250         result = listen(sock, 1);
2251         if (result != 0) {
2252             close(sock);
2253             RUNNER_ASSERT_MSG(0, "listen failed: " << strerror(errno));
2254         }
2255         smack_unix_sock_server(sock);
2256
2257         /* Change the process label with listening socket */
2258         smack_unix_sock_server(sock);
2259
2260         pid = fork();
2261         RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
2262         /* Now running two concurrent servers.
2263            Test if socket label was unaffected by fork() */
2264         smack_unix_sock_server(sock);
2265         /* Let's give the two servers different labels */
2266         smack_unix_sock_server(sock);
2267         close(sock);
2268         waitpid(pid, NULL, 0);
2269         exit(0);
2270     } else { /* parent process, client */
2271         sleep(1); /* Give server some time to setup listening socket */
2272         int i;
2273         for (i = 0; i < 4; ++i) {
2274             int sock;
2275             int result;
2276             char smack_label1[SMACK_LABEL_LEN + 1];
2277             char *smack_label2;
2278
2279             sock = socket(AF_UNIX, SOCK_STREAM, 0);
2280             RUNNER_ASSERT_MSG(sock >= 0,
2281                 "socket failed: " << strerror(errno));
2282             result = connect(sock,
2283                 (struct sockaddr*) &sockaddr, sizeof(struct sockaddr_un));
2284             if (result != 0) {
2285                 close(sock);
2286                 RUNNER_ASSERT_MSG(0, "connect failed: " << strerror(errno));
2287             }
2288
2289             alarm(2);
2290             result = read(sock, smack_label1, SMACK_LABEL_LEN);
2291             alarm(0);
2292             if (result < 0) {
2293                 close(sock);
2294                 RUNNER_ASSERT_MSG(0, "read failed: " << strerror(errno));
2295             }
2296             smack_label1[result] = '\0';
2297             smack_label2 = app_id_from_socket(sock);
2298             if (smack_label2 == NULL) {
2299                 close(sock);
2300                 RUNNER_ASSERT_MSG(0, "app_id_from_socket failed");
2301             }
2302             result = strcmp(smack_label1, smack_label2);
2303             if (result != 0) {
2304                 close(sock);
2305                 RUNNER_ASSERT_MSG(0, "smack labels differ: '" << smack_label1
2306                     << "' != '" << smack_label2 << "-" << random() << "'");
2307             }
2308             close(sock);
2309         }
2310         waitpid(pid, NULL, 0);
2311     }
2312 }
2313
2314 RUNNER_TEST(privilege_control16_app_setup_path){
2315     const char *path1 = "/usr/share/privilege-control/app_setup_access_test";
2316     const char *path2 = "/usr/share/privilege-control/app_setup_access_test/directory";
2317     const char *path3 = "/usr/share/privilege-control/app_setup_access_test/one";
2318     const char *path4 = "/usr/share/privilege-control/app_setup_access_test/directory/two";
2319     const char *label1 = "qwert123456za";
2320     const char *label2 = "trewq654123az";
2321
2322     std::unique_ptr<char, std::function<void(void*)> > labelPtr(NULL,free);
2323
2324     mkdir(path1,0);
2325     mkdir(path2,0);
2326
2327     int fd = creat(path3, S_IRWXU);
2328     if (fd >= 0)
2329         close(fd);
2330     fd = creat(path4, S_IRWXU);
2331     if (fd >= 0)
2332         close(fd);
2333
2334     char *label = NULL;
2335
2336     RUNNER_ASSERT(PC_OPERATION_SUCCESS == app_setup_path("somepackageid", path1, APP_PATH_ANY_LABEL, label1));
2337     RUNNER_ASSERT(0 == smack_lgetlabel(path3, &label, SMACK_LABEL_ACCESS));
2338     labelPtr.reset(label);
2339     label = NULL;
2340     RUNNER_ASSERT(0 == strcmp(labelPtr.get(), label1));
2341
2342     RUNNER_ASSERT(PC_OPERATION_SUCCESS == app_setup_path("somepackageid", path1, APP_PATH_ANY_LABEL, label2));
2343     RUNNER_ASSERT(0 == smack_lgetlabel(path4, &label, SMACK_LABEL_EXEC));
2344     labelPtr.reset(label);
2345     label = NULL;
2346     RUNNER_ASSERT(0 == strcmp(labelPtr.get(), label2));
2347
2348     RUNNER_ASSERT(0 == smack_lgetlabel(path1, &label, SMACK_LABEL_EXEC));
2349     labelPtr.reset(label);
2350     label = NULL;
2351     RUNNER_ASSERT(labelPtr.get() == NULL);
2352 }
2353
2354 RUNNER_TEST(privilege_control17_appsettings_privilege)
2355 {
2356 #define APP_1     "app_1"
2357 #define APP_1_DIR "/tmp/app_1"
2358
2359 #define APP_2     "app_2"
2360 #define APP_2_DIR "/tmp/app_2"
2361
2362 #define APP_TEST "app_test"
2363
2364 #define PRIV_APPSETTING (const char*[]) {"http://tizen.org/privilege/appsetting", NULL}
2365
2366     int ret;
2367     char *app1_dir_label;
2368     char *app2_dir_label;
2369     //prepare test
2370
2371
2372     (void)app_uninstall(APP_TEST);
2373     (void)app_uninstall(APP_1);
2374     (void)app_uninstall(APP_2);
2375
2376     //install some app 1
2377     ret = app_install(APP_1);
2378     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in app_install." << ret);
2379
2380     mkdir(APP_1_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
2381
2382     //register settings folder for app 1
2383     ret = app_setup_path(APP_1, APP_1_DIR, APP_PATH_SETTINGS_RW );
2384     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in app_setup_path: " << ret);
2385
2386     //install "app_test" and give it appsettings privilege
2387     ret = app_install(APP_TEST);
2388     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in app_install.");
2389
2390
2391     ret = app_enable_permissions(APP_TEST, APP_TYPE_OSP, PRIV_APPSETTING, true);
2392
2393     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,
2394         " Error enabling app permissions. Result: " << ret);
2395
2396     //check if "app_test" has an RX access to the app "app_1"
2397     ret = smack_have_access(APP_TEST, APP_1, "rx");
2398     RUNNER_ASSERT_MSG(ret,"access denies");
2399
2400
2401     //check if "app_test" has an RWX access to a folder registered by "app_1"
2402     ret = smack_getlabel(APP_1_DIR, &app1_dir_label, SMACK_LABEL_ACCESS );
2403     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
2404     ret = smack_have_access(APP_TEST, app1_dir_label, "rwx");
2405     RUNNER_ASSERT_MSG(ret,"access denies");
2406
2407
2408     //intstall another app: "app_2"
2409     ret = app_install(APP_2);
2410     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in app_install.");
2411
2412     mkdir(APP_2_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
2413     //register settings folder for that "app_2"
2414     ret = app_setup_path(APP_2, APP_2_DIR, APP_PATH_SETTINGS_RW );
2415     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS, "Error in app_setup_path: " << ret);
2416
2417     //check if "app_test" has an RX access to the app "app_2"
2418     ret = smack_have_access(APP_TEST, APP_2, "rx");
2419     RUNNER_ASSERT_MSG(ret,"access denies");
2420
2421     //check if "app_test" has an RWX access to a folder registered by "app_2"
2422     ret = smack_getlabel(APP_2_DIR, &app2_dir_label, SMACK_LABEL_ACCESS );
2423     RUNNER_ASSERT_MSG(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
2424     ret = smack_have_access(APP_TEST, app2_dir_label, "rwx");
2425     RUNNER_ASSERT_MSG(ret,"access denies");
2426
2427     free (app1_dir_label);
2428     free (app2_dir_label);
2429     rmdir(APP_1_DIR);
2430     rmdir(APP_2_DIR);
2431
2432     (void)app_uninstall(APP_TEST);
2433     (void)app_uninstall(APP_1);
2434     (void)app_uninstall(APP_2);
2435 }
2436
2437 RUNNER_TEST(privilege_control18_app_setup_path_public)
2438 {
2439     int result;
2440
2441     cleaning_smack_database_files();
2442     add_lables_to_db();
2443
2444     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
2445     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
2446
2447     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2448     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
2449
2450     result = app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_PUBLIC_RO);
2451     RUNNER_ASSERT_MSG(result == 0, "app_setup_path() failed");
2452
2453     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
2454     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
2455
2456     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2457     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
2458
2459     cleaning_smack_database_files();
2460 }
2461
2462 RUNNER_TEST(privilege_control19_app_setup_path_settings)
2463 {
2464     int result;
2465
2466     cleaning_smack_database_files();
2467     add_lables_to_db();
2468
2469     result = nftw(TEST_APP_DIR, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
2470     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_APP_DIR);
2471
2472     result = nftw(TEST_NON_APP_DIR, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2473     RUNNER_ASSERT_MSG(result == 0, "Unable to clean up Smack labels in " << TEST_NON_APP_DIR);
2474
2475     result = app_setup_path(APP_ID, TEST_APP_DIR, APP_PATH_SETTINGS_RW);
2476     RUNNER_ASSERT_MSG(result == 0, "app_setup_path() failed");
2477
2478     result = nftw(TEST_APP_DIR, &nftw_check_labels_app_settings_dir, FTW_MAX_FDS, FTW_PHYS);
2479     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for shared app dir");
2480
2481     result = nftw(TEST_NON_APP_DIR, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
2482     RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for non-app dir");
2483
2484     cleaning_smack_database_files();
2485 }
2486
2487 RUNNER_TEST(privilege_control20_early_rules)
2488 {
2489     int result;
2490     int fd = -1;
2491     int pass_1 = 0;
2492     int pass_2 = 0;
2493     char *single_line_format = NULL;
2494     char *perm = NULL;
2495     FILE *file = NULL;
2496     char subject[SMACK_LABEL_LEN + 1];
2497     char object[SMACK_LABEL_LEN + 1];
2498     char rule_add[6];    // "rwxat" + '\0'
2499     char rule_remove[6];    // "rwxat" + '\0'
2500
2501     subject[SMACK_LABEL_LEN] = '\0';
2502     object[SMACK_LABEL_LEN] = '\0';
2503     rule_add[5] = '\0';
2504     rule_remove[5] = '\0';
2505
2506     unlink(SMACK_RULES_DIR APP_ID);
2507
2508     app_uninstall(APP_ID);
2509
2510     result = app_install(APP_ID);
2511     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result << ". Errno: " << strerror(errno));
2512     result = app_install(APP_TEST_APP_1);
2513     RUNNER_ASSERT_MSG(result == 0, "app_install returned " << result << ". Errno: " << strerror(errno));
2514
2515     // checking if file really exists
2516     fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
2517     close(fd);
2518     RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << SMACK_RULES_DIR << APP_ID << " : " << fd << ". Errno: " << strerror(errno));
2519     fd = -1;
2520
2521     result = app_enable_permissions(APP_ID, APP_TYPE_WGT, (const char**) &perm, 1);
2522     RUNNER_ASSERT_MSG(result == 0, "app_enable_permission failed: " << result);
2523     result = app_enable_permissions(APP_TEST_APP_1, APP_TYPE_WGT, (const char**) &perm, 1);
2524     RUNNER_ASSERT_MSG(result == 0, "app_enable_permission failed: " << result);
2525
2526     file = fopen(SMACK_STARTUP_RULES_FILE, "r");
2527     RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
2528
2529     result = asprintf(&single_line_format, "%%%ds %%%ds %%5s %%5s\\n", SMACK_LABEL_LEN, SMACK_LABEL_LEN);
2530
2531     while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
2532         if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
2533             pass_1 = 1; // Found rule for APP_ID
2534             continue;
2535         }
2536         if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
2537             pass_2 = 1; // Found rule for APP_TEST_APP_1
2538             continue;
2539         }
2540     }
2541     free(single_line_format);
2542     single_line_format = NULL;
2543     fclose(file);
2544     file = NULL;
2545
2546     RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " not found");
2547     RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " not found");
2548
2549     // Checking if "early rule" for APP_ID was really removed
2550     // We also should make sure that "early rules" for other apps wasn't removed
2551     result = app_uninstall(APP_ID);
2552     RUNNER_ASSERT_MSG(result == 0, "app_uninstall returned " << result << ". Errno: " << strerror(errno));
2553     pass_1 = 1;
2554     pass_2 = 0;
2555
2556     file = fopen(SMACK_STARTUP_RULES_FILE, "r");
2557         RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
2558
2559         result = asprintf(&single_line_format, "%%%ds %%%ds %%5s %%5s\\n", SMACK_LABEL_LEN, SMACK_LABEL_LEN);
2560
2561         while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
2562                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
2563                         pass_1 = 0; // Found rule for APP_ID - it should NOT be here
2564                         continue;
2565                 }
2566                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
2567                         pass_2 = 1; // Found rule for APP_TEST_APP_1
2568                         continue;
2569                 }
2570         }
2571         free(single_line_format);
2572         single_line_format = NULL;
2573         fclose(file);
2574         file = NULL;
2575
2576     RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " found");
2577     RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " not found");
2578
2579     // Removing and checking "early rule" for APP_TEST_APP_1
2580         result = app_uninstall(APP_TEST_APP_1);
2581         RUNNER_ASSERT_MSG(result == 0, "app_uninstall returned " << result << ". Errno: " << strerror(errno));
2582         pass_1 = 1;
2583         pass_2 = 1;
2584
2585         file = fopen(SMACK_STARTUP_RULES_FILE, "r");
2586         RUNNER_ASSERT_MSG(file != NULL, "File open failed: " << SMACK_STARTUP_RULES_FILE << " : " << file << ". Errno: " << strerror(errno));
2587
2588         result = asprintf(&single_line_format, "%%%ds %%%ds %%5s %%5s\\n", SMACK_LABEL_LEN, SMACK_LABEL_LEN);
2589
2590         while(fscanf(file, single_line_format, subject, object, rule_add, rule_remove) == 4) {
2591                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_ID, SMACK_LABEL_LEN) == 0) {
2592                         pass_1 = 0; // Found rule for APP_ID - it should NOT be here
2593                         continue;
2594                 }
2595                 if(strncmp(subject, EARLY_RULE_SUBJECT, SMACK_LABEL_LEN) == 0 && strncmp(object, APP_TEST_APP_1, SMACK_LABEL_LEN) == 0) {
2596                         pass_2 = 0; // Found rule for APP_TEST_APP_1 - it should NOT be here
2597                         continue;
2598                 }
2599         }
2600         free(single_line_format);
2601         single_line_format = NULL;
2602         fclose(file);
2603         file = NULL;
2604
2605         RUNNER_ASSERT_MSG(pass_1 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_ID << " " << EARLY_RULE_RIGHTS << " found");
2606         RUNNER_ASSERT_MSG(pass_2 == 1, "Rule " << EARLY_RULE_SUBJECT << " " << APP_TEST_APP_1 << " " << EARLY_RULE_RIGHTS << " found");
2607 }