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