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