Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / src / security-server-tests / security_server_tests_privilege.cpp
1 #include <dpl/test/test_runner.h>
2
3 #include <libprivilege-control_test_common.h>
4
5 #include <security-server.h>
6 #include <privilege-control.h>
7
8 const char *SSTP_APP_ID = "sstp_test_app";
9 const char *SSTP_OTHER_LABEL = "sstp_test_other_label";
10
11 const char *SSTP_PERMS[] = {
12     "sstp_test_rules1",
13     "sstp_test_rules2",
14     nullptr
15 };
16
17 const char *SSTP_PERMS1[] = {
18     SSTP_PERMS[0],
19     nullptr
20 };
21
22 const char *SSTP_PERMS2[] = {
23     SSTP_PERMS[1],
24     nullptr
25 };
26
27 void check_security_server_app_has_privilege(const char *app_label,
28                                              const char *permission,
29                                              int is_enabled_expected)
30 {
31     int result;
32     int is_enabled;
33
34     result = security_server_app_has_privilege(app_label, APP_TYPE_WGT, permission, &is_enabled);
35     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
36             "Error calling security_server_app_has_privilege. Result: " << result);
37
38     RUNNER_ASSERT_MSG(is_enabled == is_enabled_expected,
39             "Result of security_server_app_has_privilege should be: " << is_enabled_expected);
40 }
41
42 RUNNER_TEST_GROUP_INIT(SECURITY_SERVER_TESTS_PRIVILEGE);
43
44 RUNNER_TEST(sstp_01_security_server_app_has_privilege)
45 {
46     int result;
47
48     DB_BEGIN
49
50     result = perm_app_uninstall(SSTP_APP_ID);
51     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
52             "Error uninstalling app. Result" << result);
53
54     result = perm_app_install(SSTP_APP_ID);
55     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
56             "Error installing app. Result" << result);
57
58     result = perm_app_disable_permissions(SSTP_APP_ID, APP_TYPE_WGT, SSTP_PERMS);
59     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
60             "Error disabling app r and no r permissions. Result: " << result);
61
62     DB_END
63
64     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[0], 0);
65     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[1], 0);
66     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[0], 0);
67     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[1], 0);
68
69     DB_BEGIN
70
71     result = perm_app_enable_permissions(SSTP_APP_ID, APP_TYPE_WGT, SSTP_PERMS1, false);
72     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
73             "Error registering app r permissions. Result: " << result);
74
75     DB_END
76
77     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[0], 1);
78     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[1], 0);
79     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[0], 0);
80     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[1], 0);
81
82     DB_BEGIN
83
84     result = perm_app_enable_permissions(SSTP_APP_ID, APP_TYPE_WGT, SSTP_PERMS2, false);
85     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
86             "Error registering app r permissions. Result: " << result);
87
88     DB_END
89
90     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[0], 1);
91     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[1], 1);
92     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[0], 0);
93     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[1], 0);
94
95     DB_BEGIN
96
97     result = perm_app_disable_permissions(SSTP_APP_ID, APP_TYPE_WGT, SSTP_PERMS1);
98     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
99             "Error disabling app r and no r permissions. Result: " << result);
100
101     DB_END
102
103     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[0], 0);
104     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[1], 1);
105     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[0], 0);
106     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[1], 0);
107
108     DB_BEGIN
109
110     result = perm_app_disable_permissions(SSTP_APP_ID, APP_TYPE_WGT, SSTP_PERMS2);
111     RUNNER_ASSERT_MSG(result == PC_OPERATION_SUCCESS,
112             "Error disabling app r and no r permissions. Result: " << result);
113
114     DB_END
115
116     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[0], 0);
117     check_security_server_app_has_privilege(USER_APP_ID, SSTP_PERMS[1], 0);
118     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[0], 0);
119     check_security_server_app_has_privilege(SSTP_OTHER_LABEL, SSTP_PERMS[1], 0);
120 }
121
122 int main(int argc, char *argv[])
123 {
124     return DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
125 }