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