91d90d0db29abf5f378d3e30b88f744f48d27746
[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 #include <string>
22
23 #include <sys/smack.h>
24 #include <ckmc/ckmc-type.h>
25 #include <ckm-common.h>
26 #include <tests_common.h>
27 #include <access_provider2.h>
28 #include <ckm/ckm-manager.h>
29 #include <dbus_access.h>
30
31 const char* SERVICE[] = {
32         "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2dlistener_2eservice",
33         "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2eservice" };
34
35 void start_service(ServiceIdx idx)
36 {
37     DBusAccess dbus(SERVICE[idx]);
38     dbus.start();
39 }
40
41 void stop_service(ServiceIdx idx)
42 {
43     DBusAccess dbus(SERVICE[idx]);
44     dbus.stop();
45 }
46
47
48 void switch_to_storage_user(const char* label)
49 {
50     AccessProvider ap(label);
51     ap.allowAPI("key-manager::api-storage", "rw");
52     ap.applyAndSwithToUser(APP_UID, APP_GID);
53 }
54
55 void switch_to_storage_ocsp_user(const char* label)
56 {
57     AccessProvider ap(label);
58     ap.allowAPI("key-manager::api-storage", "rw");
59     ap.allowAPI("key-manager::api-ocsp", "rw");
60     ap.applyAndSwithToUser(APP_UID, APP_GID);
61 }
62
63 DBCleanup::~DBCleanup()
64 {
65     // Let it throw. If db can't be cleared further tests are unreliable
66     CKM::ManagerShPtr mgr = CKM::Manager::create();
67     for(const auto& it:m_aliases)
68         mgr->removeAlias(it);
69     m_aliases.clear();
70 }
71
72 // returns process label
73 CharPtr get_label()
74 {
75     int ret;
76     char* my_label = NULL;
77     RUNNER_ASSERT_MSG(0 <= (ret = smack_new_label_from_self(&my_label)),
78                          "Failed to get smack label for self. Error: " << ret);
79
80     return CharPtr(my_label, free);
81 }
82
83 std::string aliasWithLabel(const char *label, const char *alias)
84 {
85     if(label)
86     {
87         std::stringstream ss;
88         ss << label << std::string(ckmc_label_name_separator) << alias;
89         return ss.str();
90     }
91     return std::string(alias);
92 }
93
94 // changes process label
95 void change_label(const char* label)
96 {
97     int ret = smack_set_label_for_self(label);
98     RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self. Error: " << ret);
99 }
100
101 ScopedLabel::ScopedLabel(const char* label) : m_original_label(get_label())
102 {
103     change_label(label);
104 }
105
106 ScopedLabel::~ScopedLabel()
107 {
108     /*
109      * Let it throw. If we can't restore label then remaining tests results will be
110      * unreliable anyway.
111      */
112     change_label(m_original_label.get());
113 }
114
115 const char * CKMCErrorToString(int error) {
116 #define ERRORDESCRIBE(name) case name: return #name
117     switch(error) {
118         ERRORDESCRIBE(CKMC_ERROR_NONE);
119         ERRORDESCRIBE(CKMC_ERROR_INVALID_PARAMETER);
120         ERRORDESCRIBE(CKMC_ERROR_OUT_OF_MEMORY);
121         ERRORDESCRIBE(CKMC_ERROR_PERMISSION_DENIED);
122         ERRORDESCRIBE(CKMC_ERROR_SOCKET);
123         ERRORDESCRIBE(CKMC_ERROR_BAD_REQUEST);
124         ERRORDESCRIBE(CKMC_ERROR_BAD_RESPONSE);
125         ERRORDESCRIBE(CKMC_ERROR_SEND_FAILED);
126         ERRORDESCRIBE(CKMC_ERROR_RECV_FAILED);
127         ERRORDESCRIBE(CKMC_ERROR_AUTHENTICATION_FAILED);
128         ERRORDESCRIBE(CKMC_ERROR_BUFFER_TOO_SMALL);
129         ERRORDESCRIBE(CKMC_ERROR_SERVER_ERROR);
130         ERRORDESCRIBE(CKMC_ERROR_DB_LOCKED);
131         ERRORDESCRIBE(CKMC_ERROR_DB_ERROR);
132         ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_EXISTS);
133         ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_UNKNOWN);
134         ERRORDESCRIBE(CKMC_ERROR_VERIFICATION_FAILED);
135         ERRORDESCRIBE(CKMC_ERROR_INVALID_FORMAT);
136         ERRORDESCRIBE(CKMC_ERROR_FILE_ACCESS_DENIED);
137         ERRORDESCRIBE(CKMC_ERROR_NOT_EXPORTABLE);
138         ERRORDESCRIBE(CKMC_ERROR_FILE_SYSTEM);
139         ERRORDESCRIBE(CKMC_ERROR_UNKNOWN);
140         default: return "Error not defined";
141     }
142 #undef ERRORDESCRIBE
143 }
144
145 std::string CKMCReadableError(int error) {
146     std::string output("Error: ");
147     output += std::to_string(error);
148     output += " Description: ";
149     output += CKMCErrorToString(error);
150     return output;
151 }
152