bd007543ae4c365609fce91866ab9deac892db13
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / libprivilege-control_test_common.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  * @file    libprivilege-control-test.cpp
18  * @author  Jan Olszak (j.olszak@samsung.com)
19  * @author  Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
20  * @version 1.0
21  * @brief   Main file for libprivilege-control unit tests.
22  */
23
24 #include <fcntl.h>
25 #include <fstream>
26 #include <iostream>
27 #include <set>
28 #include <string>
29 #include <string.h>
30 #include <sys/sendfile.h>
31 #include <sys/smack.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <vector>
35 #include <grp.h>
36 #include <pwd.h>
37
38 #include <libprivilege-control_test_common.h>
39 #include <tests_common.h>
40 #include "common/duplicates.h"
41 #include <memory.h>
42
43 #define CANARY_LABEL             "tiny_yellow_canary"
44
45 const char *USER_APP_ID = "User";
46
47 const char *PRIVS1[] = { "WRT", "test_privilege_control_rules1", NULL };
48 const char *PRIVS2[] = { "test_privilege_control_rules2", NULL };
49 const char *PRIVS2_NO_R[] = { "test_privilege_control_rules2_no_r", NULL };
50 const char *PRIVS2_R[] = { "test_privilege_control_rules2_r", NULL };
51 const char *PRIVS2_R_AND_NO_R[] = { "test_privilege_control_rules2_r", "test_privilege_control_rules2_no_r", NULL };
52
53 const char *PRIVS_WGT[] = { "test_privilege_control_rules_wgt", NULL };
54 const char *PRIVS_OSP[] = { "test_privilege_control_rules_osp", NULL };
55 const char *PRIVS_EFL[] = { "test_privilege_control_rules_efl", NULL };
56
57 const char *PRIV_APPSETTING[] {"org.tizen.privilege.appsetting", NULL};
58 const char *PRIV_APPSETTING_RULES[] = { "~APP~ ~SETTINGS_PATH~ rwx",
59                                         "~APP~ ~ALL_APPS~ rx",
60                                         NULL};
61 /**
62  * Check if every rule is true.
63  * @return 1 if ALL rules in SMACK, 0 if ANY rule isn't, -1 on failure
64  */
65 int test_have_all_accesses(const rules_t &rules)
66 {
67     int result = 1;
68     for (uint i = 0; i < rules.size(); ++i) {
69         int access = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
70         if (access < 0)
71             return -1;
72         if (access == 0)
73             result = 0;
74     }
75     return result;
76 }
77
78 /**
79  * Check if every rule is true.
80  * @return 1 if ANY rule in SMACK, 0 if NO rule in SMACK, -1 on failure
81  */
82 int test_have_any_accesses(const rules_t &rules)
83 {
84     int result = 0;
85     for (uint i = 0; i < rules.size(); ++i) {
86         int access = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
87         if (access < 0)
88             return -1;
89         if (access > 0)
90             result = 1;
91     }
92     return result;
93 }
94
95 /**
96  * NOSMACK version of test_have_accesses functions.
97  *
98  * This will be used in many tests. Checks if for every rule smack_have_access returns error.
99  * If for any of rules smack_have_access will return something different than error, this result
100  * is being returned to caller.
101  */
102 int test_have_nosmack_accesses(const rules_t &rules)
103 {
104     int result;
105     for (uint i = 0; i < rules.size(); ++i) {
106         result = smack_have_access(rules[i][0].c_str(),rules[i][1].c_str(),rules[i][2].c_str());
107         if (result != -1)
108             return result;
109     }
110     return -1;
111 }
112
113 bool check_all_accesses(bool smack, const rules_t &rules)
114 {
115     if (smack)
116         return test_have_all_accesses(rules) == 1;
117     else
118         return test_have_nosmack_accesses(rules) == -1;
119 }
120
121 bool check_no_accesses(bool smack, const rules_t &rules)
122 {
123     if (smack)
124         return test_have_any_accesses(rules) == 0;
125     else
126         return test_have_nosmack_accesses(rules) == -1;
127 }
128
129 void read_gids(std::set<unsigned> &set, const char *file_path)
130 {
131     FILE *f = fopen(file_path, "r");
132     RUNNER_ASSERT_MSG_BT(f != NULL, "Unable to open file " << file_path);
133     unsigned gid;
134     while (fscanf(f, "%u\n", &gid) == 1) {
135         set.insert(gid);
136     }
137     fclose(f);
138 }
139
140 void read_user_gids(std::set<unsigned> &set, const uid_t user_id)
141 {
142     int ret;
143
144     struct passwd *pw = getpwuid(user_id);
145     RUNNER_ASSERT_MSG_BT(pw != NULL, "getpwuid() failed.");
146
147     int groups_cnt = 0;
148     gid_t *groups_list = NULL;
149     ret = getgrouplist(pw->pw_name,  pw->pw_gid, groups_list, &groups_cnt);
150     RUNNER_ASSERT_MSG_BT(ret == -1, "getgrouplist() failed.");
151     if (groups_cnt == 0)
152         return;
153     groups_list = (gid_t*) calloc(groups_cnt, sizeof(gid_t));
154     RUNNER_ASSERT_MSG_BT(groups_list != NULL, "Memory allocation failed.");
155
156     ret = getgrouplist(pw->pw_name,  pw->pw_gid, groups_list, &groups_cnt);
157     if (ret == -1) {
158         free(groups_list);
159         RUNNER_ASSERT_MSG_BT(false, "getgrouplist() failed.");
160     }
161
162     for (int i = 0; i < groups_cnt; ++i) {
163         set.insert(groups_list[i]);
164     }
165     free(groups_list);
166 }
167
168 void read_current_gids(std::set<unsigned> &set)
169 {
170     int groups_cnt = getgroups(0, NULL);
171     RUNNER_ASSERT_MSG_BT(groups_cnt > 0, "Wrong number of supplementary groups.");
172     gid_t *groups_list = (gid_t*) calloc(groups_cnt, sizeof(gid_t));
173     RUNNER_ASSERT_MSG_BT(groups_list != NULL, "Memory allocation failed.");
174     if (getgroups(groups_cnt, groups_list) == -1){
175         free(groups_list);
176         RUNNER_ASSERT_MSG_BT(false, "getgroups failed.");
177     }
178
179     for (int i = 0; i < groups_cnt; ++i) {
180         set.insert(groups_list[i]);
181     }
182     free(groups_list);
183 }
184
185 void check_groups(const std::set<unsigned> &groups_prev, const char *dac_file)
186 {
187     std::set<unsigned> groups_check;
188     std::set<unsigned> groups_current;
189     if(dac_file != NULL)
190         read_gids(groups_check, dac_file);
191     read_current_gids(groups_current);
192
193     std::string groups_left;
194     for (auto it = groups_prev.begin(); it != groups_prev.end(); ++it)
195     {
196         (void)groups_check.erase(*it);
197         if(groups_current.erase(*it) == 0)
198             groups_left.append(std::to_string(*it)).append(" ");
199     }
200     RUNNER_ASSERT_MSG_BT(groups_left.empty(),
201         "Application lost some groups: " << groups_left);
202
203     for (auto it = groups_check.begin(); it != groups_check.end(); ++it)
204     {
205         if(groups_current.erase(*it) == 0)
206             groups_left.append(std::to_string(*it)).append(" ");
207     }
208     RUNNER_ASSERT_MSG_BT(groups_left.empty(),
209         "Application doesn't belong to some required groups: " << groups_left);
210
211     for (auto it = groups_current.begin(); it != groups_current.end(); ++it)
212     {
213         groups_left.append(std::to_string(*it)).append(" ");
214     }
215     RUNNER_ASSERT_MSG_BT(groups_left.empty(),
216         "Application belongs to groups it should't belong to: " << groups_left);
217 }
218
219 int file_exists(const char *path)
220 {
221     FILE *file = fopen(path, "r");
222     if (file) {
223         fclose(file);
224         return 0;
225     }
226     return -1;
227 }
228
229 void check_app_installed(const char *app_path)
230 {
231     RUNNER_ASSERT_MSG_BT(file_exists(app_path) == 0,
232             " App not installed: " << app_path);
233 }
234
235 int nftw_remove_labels(const char *fpath, const struct stat* /*sb*/,
236                        int /*typeflag*/, struct FTW* /*ftwbuf*/)
237 {
238     smack_lsetlabel(fpath, NULL, SMACK_LABEL_ACCESS);
239     smack_lsetlabel(fpath, NULL, SMACK_LABEL_EXEC);
240     smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
241
242     return 0;
243 }
244
245 int nftw_check_labels_app_dir(const char *fpath, const struct stat *sb,
246                                int /*typeflag*/, struct FTW* /*ftwbuf*/)
247 {
248     int result;
249     CStringPtr labelPtr;
250     char* label = NULL;
251
252     /* ACCESS */
253     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
254     labelPtr.reset(label);
255     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
256     RUNNER_ASSERT_MSG_BT(labelPtr.get() != NULL, "ACCESS label on " << fpath << " is not set");
257     result = strcmp(USER_APP_ID, labelPtr.get());
258     RUNNER_ASSERT_MSG_BT(result == 0, "ACCESS label on " << fpath << " is incorrect");
259
260     /* EXEC */
261     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
262     labelPtr.reset(label);
263     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
264     if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR)) {
265         RUNNER_ASSERT_MSG_BT(labelPtr.get() != NULL, "EXEC label on " << fpath << " is not set");
266         result = strcmp(USER_APP_ID, labelPtr.get());
267         RUNNER_ASSERT_MSG_BT(result == 0, "EXEC label on executable file " << fpath << " is incorrect");
268     } else if (S_ISLNK(sb->st_mode)) {
269         struct stat buf;
270         char *target = realpath(fpath, NULL);
271         RUNNER_ASSERT_MSG_BT(0 == stat(target, &buf),"Stat failed for " << fpath);
272         free(target);
273         if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG)) {
274             RUNNER_ASSERT_MSG_BT(labelPtr.get() == NULL, "EXEC label on " << fpath << " is set");
275         } else {
276             RUNNER_ASSERT_MSG_BT(labelPtr.get() != NULL, "EXEC label on " << fpath << " is not set");
277             result = strcmp(USER_APP_ID, labelPtr.get());
278             RUNNER_ASSERT_MSG_BT(result == 0, "EXEC label on link to executable file " << fpath << " is incorrect");
279         }
280     } else
281         RUNNER_ASSERT_MSG_BT(labelPtr.get() == NULL, "EXEC label on " << fpath << " is set");
282
283     /* TRANSMUTE */
284     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
285     labelPtr.reset(label);
286     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
287     RUNNER_ASSERT_MSG_BT(labelPtr.get() == NULL, "TRANSMUTE label on " << fpath << " is set");
288
289     return 0;
290  }
291
292 int nftw_set_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
293                                 int /*typeflag*/, struct FTW* /*ftwbuf*/)
294 {
295     smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_ACCESS);
296     smack_lsetlabel(fpath, CANARY_LABEL, SMACK_LABEL_EXEC);
297     smack_lsetlabel(fpath, NULL, SMACK_LABEL_TRANSMUTE);
298
299     return 0;
300 }
301
302 int nftw_check_labels_non_app_dir(const char *fpath, const struct stat* /*sb*/,
303                                   int /*typeflag*/, struct FTW* /*ftwbuf*/)
304 {
305     int result;
306     CStringPtr labelPtr;
307     char* label = NULL;
308
309     /* ACCESS */
310     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
311     labelPtr.reset(label);
312     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
313     result = strcmp(CANARY_LABEL, labelPtr.get());
314     RUNNER_ASSERT_MSG_BT(result == 0, "ACCESS label on " << fpath << " is overwritten");
315
316     /* EXEC */
317     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
318     labelPtr.reset(label);
319     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
320     result = strcmp(CANARY_LABEL, labelPtr.get());
321     RUNNER_ASSERT_MSG_BT(result == 0, "EXEC label on " << fpath << " is overwritten");
322
323     /* TRANSMUTE */
324     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
325     labelPtr.reset(label);
326     RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
327     RUNNER_ASSERT_MSG_BT(labelPtr.get() == NULL, "TRANSMUTE label on " << fpath << " is set");
328
329     return 0;
330 }
331
332 void test_revoke_permissions(int line_no, const char* app_id)
333 {
334     int result;
335
336     // Cleanup
337     DB_BEGIN
338
339     result = perm_app_uninstall(app_id);
340     RUNNER_ASSERT_MSG_BT(result == 0, "Line: " << line_no <<
341             "perm_app_uninstall returned " << result);
342
343     // Close transaction to commit uninstallation before further actions
344     DB_END
345
346     DB_BEGIN
347
348     // Install test apps
349     result = perm_app_install(app_id);
350     RUNNER_ASSERT_MSG_BT(result == 0, "Line: " << line_no <<
351             "perm_app_install returned " << result);
352
353     // Close transaction to commit installation before further actions
354     DB_END
355
356     DB_BEGIN
357
358     // TEST:
359     // Revoke permissions
360     result = perm_app_revoke_permissions(app_id);
361     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS, "Line: " << line_no <<
362         "Error revoking app permissions. Result: " << result);
363
364     DB_END
365
366     DB_BEGIN
367
368     // Cleanup - uninstall test apps
369     result = perm_app_uninstall(app_id);
370     RUNNER_ASSERT_MSG_BT(result == 0, "Line: " << line_no <<
371             "perm_app_uninstall returned " << result);
372
373     DB_END
374 }
375
376 void test_app_enable_permissions_efl(bool smack)
377 {
378     int result;
379
380     DB_BEGIN
381
382     // Prepare
383     result = perm_app_uninstall(EFL_APP_ID);
384     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
385             "perm_app_uninstall failed: " << result);
386     result = perm_app_install(EFL_APP_ID);
387     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
388             "perm_app_install failed: " << result);
389
390     // Register a permission:
391     result = perm_app_enable_permissions(EFL_APP_ID, APP_TYPE_EFL, PRIVS_EFL, false);
392     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
393         "Error registering app permissions. Result: " << result);
394
395     DB_END
396
397     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, {{USER_APP_ID,"test_book_efl", "r"}}),
398             "SMACK accesses not granted for EFL_APP");
399
400     DB_BEGIN
401
402     // Cleanup
403     result = perm_app_uninstall(EFL_APP_ID);
404     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
405             "perm_app_uninstall failed: " << result);
406
407     DB_END
408 }
409
410 void test_app_disable_permissions_efl(bool smack)
411 {
412     int result;
413
414     DB_BEGIN
415
416     // Prepare
417     result = perm_app_uninstall(EFL_APP_ID);
418     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
419             "perm_app_uninstall failed: " << result);
420
421     result = perm_app_install(EFL_APP_ID);
422     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
423             "perm_app_install failed: " << result);
424
425     result = perm_app_disable_permissions(EFL_APP_ID, APP_TYPE_EFL, PRIVS_EFL);
426     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
427         "Error disabling app permissions. Result: " << result);
428
429     DB_END
430
431     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, {{USER_APP_ID,"test_book_efl", "r"}}),
432             "SMACK accesses not disabled for EFL_APP");
433
434     DB_BEGIN
435
436     // Register a permission
437     result = perm_app_enable_permissions(EFL_APP_ID, APP_TYPE_EFL, PRIVS_EFL, false);
438     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
439         "Error registering app permissions. Result: " << result);
440
441     DB_END
442
443     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, {{USER_APP_ID,"test_book_efl", "r"}}),
444             "SMACK accesses not granted for EFL_APP");
445
446     DB_BEGIN
447
448     // Disable a permission
449     result = perm_app_disable_permissions(EFL_APP_ID, APP_TYPE_EFL, PRIVS_EFL);
450     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
451         "Error disabling app permissions. Result: " << result);
452
453     DB_END
454
455     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, {{USER_APP_ID,"test_book_efl", "r"}}),
456             "SMACK accesses not disabled for EFL_APP");
457
458     DB_BEGIN
459
460     // Cleanup
461     result = perm_app_uninstall(EFL_APP_ID);
462     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
463             "perm_app_uninstall failed: " << result);
464
465     DB_END
466 }
467
468 void test_app_disable_permissions(bool smack)
469 {
470     int result;
471
472     DB_BEGIN
473
474     // Prepare
475     result = perm_app_uninstall(WGT_APP_ID);
476     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
477             "perm_app_uninstall failed: " << result);
478
479     result = perm_app_install(WGT_APP_ID);
480     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
481             "perm_app_install failed: " << result);
482
483     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS1);
484     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
485             "Error disabling app first permissions. Result: " << result);
486
487     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2);
488     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
489         "Error disabling app permissions. Result: " << result);
490
491     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R);
492     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
493             "Error disabling app no r permissions. Result: " << result);
494
495     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R);
496     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
497             "Error disabling app r permissions. Result: " << result);
498
499     DB_END
500
501     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, rules2),
502             "SMACK accesses not disabled.");
503
504     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, rules1),
505             "SMACK accesses not disabled.");
506
507     DB_BEGIN
508
509 /**
510  * Test - disable all granted permissions.
511  */
512
513     // Prepare permissions that we want to disable
514     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, false);
515     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
516             " Error registering app permissions. Result: " << result);
517
518     DB_END
519
520     // Are all the permissions enabled?
521     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, rules2), "Not all permisions enabled.");
522
523     DB_BEGIN
524
525     // Disable permissions
526     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2);
527     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
528             "Error disabling app permissions. Result: " << result);
529
530     DB_END
531
532     // Are all the permissions disabled?
533     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, rules2), "Not all permisions disabled.");
534
535 /**
536  * Test - disable some granted permissions leaving non complementary and then disabling those too.
537  */
538
539     DB_BEGIN
540
541     // Prepare permissions that will not be disabled
542     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS1, false);
543     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
544             " Error adding app first permissions. Result: " << result);
545
546     // Prepare permissions that we want to disable
547     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2, false);
548     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
549             " Error adding app second permissions. Result: " << result);
550
551     // Disable second permissions
552     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2);
553     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
554             "Error disabling app second permissions. Result: " << result);
555
556     DB_END
557
558     // Are all second permissions disabled?
559     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, rules2), "Not all first permisions disabled.");
560
561     // Are all first permissions not disabled?
562     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, rules1), "Some of second permissions disabled.");
563
564     DB_BEGIN
565
566     // Disable first permissions
567     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS1);
568     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
569             "Error disabling app first permissions. Result: " << result);
570
571     DB_END
572
573     // Are all second permissions disabled?
574     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, rules1), "Not all second permisions disabled.");
575
576 /**
577  * Test - disable only no r granted permissions.
578  */
579
580     DB_BEGIN
581
582     // Prepare permissions
583     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R, false);
584     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
585             " Error registering app r permissions. Result: " << result);
586
587     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R, false);
588     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
589             " Error registering app no r permissions. Result: " << result);
590
591     // Disable same permissions without r
592     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R);
593     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
594             "Error disabling app no r permissions. Result: " << result);
595
596     DB_END
597
598     // Is any r permissions disabled?
599     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, rules2_r), "Some of r permissions disabled.");
600     // Are all no r permissions disabled?
601     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, rules2_no_r), "Not all no r permissions disabled.");
602
603     DB_BEGIN
604
605     // Prepare permissions
606     result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R, false);
607     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
608             " Error adding app no r permissions. Result: " << result);
609
610     DB_END
611
612     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, rules2_no_r), "Not all no r permissions enabled.");
613
614     DB_BEGIN
615
616     // Disable all permissions
617     result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R);
618     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
619             "Error disabling app permissions. Result: " << result);
620
621     DB_END
622
623     RUNNER_ASSERT_MSG_BT(check_no_accesses(smack, rules2_r), "Not all r permissions disabled.");
624
625     DB_BEGIN
626
627     // Clean up after test:
628     result = perm_app_uninstall(WGT_APP_ID);
629     RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS, "perm_app_uninstall returned " << result << ". Errno: " << strerror(errno));
630
631     DB_END
632 }
633
634 void test_appsettings_privilege(bool smack)
635 {
636     int ret;
637     CStringPtr app1DirLabelPtr;
638     CStringPtr app2DirLabelPtr;
639     char* label = NULL;
640
641     DB_BEGIN
642
643     (void)perm_app_uninstall(APP_TEST);
644     (void)perm_app_uninstall(APP_1);
645     (void)perm_app_uninstall(APP_2);
646
647     //install some app 1
648     ret = perm_app_install(APP_1);
649     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install." << ret);
650
651     mkdir(APP_1_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
652
653     //register settings folder for app 1
654     ret = perm_app_setup_path(APP_1, APP_1_DIR, APP_PATH_SETTINGS_RW );
655     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Error in perm_app_setup_path: " << ret);
656
657     //install "app_test" and give it appsettings privilege
658     ret = perm_app_install(APP_TEST);
659     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
660
661     //register appsettings feature
662     ret = perm_add_api_feature(APP_TYPE_OSP, PRIV_APPSETTING[0], PRIV_APPSETTING_RULES, NULL, 0);
663     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS,
664         " Error registering api feature. Result: " << ret);
665
666     ret = perm_app_enable_permissions(APP_TEST, APP_TYPE_OSP, PRIV_APPSETTING, false);
667     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS,
668         " Error registering app permissions. Result: " << ret);
669
670     DB_END
671
672     //check if "app_test" has an RWX access to a folder registered by "app_1"
673     ret = smack_getlabel(APP_1_DIR, &label, SMACK_LABEL_ACCESS );
674     app1DirLabelPtr.reset(label);
675     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
676     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, {{USER_APP_ID, app1DirLabelPtr.get(), "rwx"}}), "access denied to smack label: " << app1DirLabelPtr.get());
677
678
679     DB_BEGIN
680
681     //intstall another app: "app_2"
682     ret = perm_app_install(APP_2);
683     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Error in perm_app_install.");
684
685     mkdir(APP_2_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
686     //register settings folder for that "app_2"
687     ret = perm_app_setup_path(APP_2, APP_2_DIR, APP_PATH_SETTINGS_RW );
688     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS, "Error in perm_app_setup_path: " << ret);
689
690     DB_END
691
692     //check if "app_test" has an RWX access to a folder registered by "app_2"
693     ret = smack_getlabel(APP_2_DIR, &label, SMACK_LABEL_ACCESS );
694     app2DirLabelPtr.reset(label);
695     RUNNER_ASSERT_MSG_BT(ret == PC_OPERATION_SUCCESS,"smack_getlabel failed");
696     RUNNER_ASSERT_MSG_BT(check_all_accesses(smack, {{USER_APP_ID, app2DirLabelPtr.get(), "rwx"}}), "access denies");
697
698     rmdir(APP_1_DIR);
699     rmdir(APP_2_DIR);
700
701     DB_BEGIN
702
703     (void)perm_app_uninstall(APP_TEST);
704     (void)perm_app_uninstall(APP_1);
705     (void)perm_app_uninstall(APP_2);
706
707     DB_END
708 }