Add tests for Cynara RE (Rush-Edition)
[platform/core/test/security-tests.git] / tests / cynara-tests / cynara_client.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd
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 /*
18  * @file        cynara_client.cpp
19  * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
20  * @version     1.0
21  * @brief       Tests for libcynara-client
22  */
23
24 #include <tests_common.h>
25 #include <privilege-control.h>
26 #include <cynara/cynara-client.h>
27
28 #include <memory>
29 #include <functional>
30
31
32 static const char *FAKE_PRIV_1 = "test_for_cynara_rules";
33 static const char *FAKE_PRIV_2 = "0fbc6f4f45c4641b373bfcc1bf4c1ad9";
34 static const char *PRIVILEGES_1[] = { FAKE_PRIV_1, NULL };
35 static const char *FAKE_APP_1 = "fake_app_1";
36
37
38 static void install_app_with_perms(const char *app_id, const char **perms) {
39     DB_BEGIN
40     int install_check = perm_app_install(app_id);
41     RUNNER_ASSERT_MSG_BT(install_check == PC_OPERATION_SUCCESS,
42         "Unable to install app " << perm_strerror(install_check));
43     int enable_perm_check = perm_app_enable_permissions(
44         app_id, APP_TYPE_WGT, perms, true);
45     RUNNER_ASSERT_MSG_BT(enable_perm_check == PC_OPERATION_SUCCESS,
46          "Unable to enable privileges " << perm_strerror(enable_perm_check));
47     DB_END
48 }
49
50 static void remove_perms_and_uninstall(const char *app_id, const char **perms) {
51     DB_BEGIN
52     int disable_perm_check = perm_app_disable_permissions(app_id, APP_TYPE_WGT, perms);
53     RUNNER_ASSERT_MSG_BT(disable_perm_check == PC_OPERATION_SUCCESS,
54         "Unable to disable privileges " << perm_strerror(disable_perm_check));
55     int uninstall_check = perm_app_uninstall(app_id);
56     RUNNER_ASSERT_MSG_BT(uninstall_check == PC_OPERATION_SUCCESS,
57         "Unable to install app " << perm_strerror(uninstall_check));
58     DB_END
59 }
60
61 static void do_cynara_finish(cynara *cynara_data) {
62     cynara_api_result finish_ret = static_cast<cynara_api_result>(cynara_finish(cynara_data));
63     if(std::uncaught_exception() == false)
64         RUNNER_ASSERT_MSG_BT(finish_ret == CYNARA_API_SUCCESS, "Cynara finish failed");
65 }
66
67 // cynara is an incomplete type, so deleter signature needs to be explicitly declared
68 static std::unique_ptr<cynara, std::function<decltype(do_cynara_finish)>> do_cynara_initialize() {
69     cynara *cynara_data = nullptr;
70     cynara_api_result init_ret = static_cast<cynara_api_result>(
71             cynara_initialize(&cynara_data, NULL));
72     RUNNER_ASSERT_MSG_BT(init_ret == CYNARA_API_SUCCESS, "Cynara initialize failed");
73     return std::function<decltype(do_cynara_initialize)>::result_type(cynara_data, do_cynara_finish);
74 }
75
76
77 RUNNER_TEST_GROUP_INIT(cynara_client)
78
79 RUNNER_TEST(initialize) {
80     auto cynara_data = do_cynara_initialize();
81     RUNNER_ASSERT_MSG_BT(cynara_data != nullptr, "Cynara initialize data invalid");
82 }
83
84 RUNNER_TEST(check_grant) {
85     install_app_with_perms(FAKE_APP_1, PRIVILEGES_1);
86
87     auto cynara_data = do_cynara_initialize();
88     cynara_api_result check_ret = static_cast<cynara_api_result>(cynara_check(
89         cynara_data.get(), "User", "", "", PRIVILEGES_1[0]));
90
91     remove_perms_and_uninstall(FAKE_APP_1, PRIVILEGES_1);
92     RUNNER_ASSERT_MSG_BT(check_ret == CYNARA_API_SUCCESS,
93         "Cynara check failed (ret=" << check_ret << ")");
94 }
95
96 RUNNER_TEST(check_deny) {
97     auto cynara_data = do_cynara_initialize();
98     cynara_api_result check_ret = static_cast<cynara_api_result>(cynara_check(
99         cynara_data.get(), "User", "", "", FAKE_PRIV_2));
100
101     RUNNER_ASSERT_MSG_BT(check_ret == CYNARA_API_ACCESS_DENIED,
102         "Cynara check failed (ret=" << check_ret << ")");
103 }