df289dbd29aa03fdb27ef5fec3d24c5c0bcb6895
[platform/core/test/security-tests.git] / tests / ckm / ckm-common.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 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       ckm-common.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <sys/smack.h>
23 #include <ckmc/ckmc-type.h>
24 #include <ckm-common.h>
25 #include <tests_common.h>
26 #include <access_provider2.h>
27 #include <ckm/ckm-manager.h>
28 #include <dbus_access.h>
29
30 const char* SERVICE[] = {
31         "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2dlistener_2eservice",
32         "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2eservice" };
33
34 void start_service(ServiceIdx idx)
35 {
36     DBusAccess dbus(SERVICE[idx]);
37     dbus.start();
38 }
39
40 void stop_service(ServiceIdx idx)
41 {
42     DBusAccess dbus(SERVICE[idx]);
43     dbus.stop();
44 }
45
46
47 void switch_to_storage_user(const char* label)
48 {
49     AccessProvider ap(label);
50     ap.allowAPI("key-manager::api-storage", "rw");
51     ap.applyAndSwithToUser(APP_UID, APP_GID);
52 }
53
54 void switch_to_storage_ocsp_user(const char* label)
55 {
56     AccessProvider ap(label);
57     ap.allowAPI("key-manager::api-storage", "rw");
58     ap.allowAPI("key-manager::api-ocsp", "rw");
59     ap.applyAndSwithToUser(APP_UID, APP_GID);
60 }
61
62 DBCleanup::~DBCleanup()
63 {
64     // Let it throw. If db can't be cleared further tests are unreliable
65     CKM::ManagerShPtr mgr = CKM::Manager::create();
66     for(const auto& it:m_aliases)
67         mgr->removeAlias(it);
68     m_aliases.clear();
69 }
70
71 // returns process label
72 CharPtr get_label()
73 {
74     int ret;
75     char* my_label = NULL;
76     RUNNER_ASSERT_MSG(0 <= (ret = smack_new_label_from_self(&my_label)),
77                          "Failed to get smack label for self. Error: " << ret);
78
79     return CharPtr(my_label, free);
80 }
81
82 std::string aliasWithLabel(const char *label, const char *alias)
83 {
84     if(label)
85     {
86         std::stringstream ss;
87         ss << label << std::string(ckmc_label_name_separator) << alias;
88         return ss.str();
89     }
90     return std::string(alias);
91 }
92
93 // changes process label
94 void change_label(const char* label)
95 {
96     int ret = smack_set_label_for_self(label);
97     RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self. Error: " << ret);
98 }
99
100 ScopedLabel::ScopedLabel(const char* label) : m_original_label(get_label())
101 {
102     change_label(label);
103 }
104
105 ScopedLabel::~ScopedLabel()
106 {
107     /*
108      * Let it throw. If we can't restore label then remaining tests results will be
109      * unreliable anyway.
110      */
111     change_label(m_original_label.get());
112 }