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