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