2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file ckm-common.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
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-control.h>
29 #include <ckm/ckm-manager.h>
30 #include <ckmc/ckmc-control.h>
31 #include <ckmc/ckmc-manager.h>
32 #include <service_manager.h>
34 const char* SERVICE[] = {
35 "central-key-manager-listener.service",
36 "central-key-manager.service" };
38 void start_service(ServiceIdx idx)
40 ServiceManager sm(SERVICE[idx]);
44 void stop_service(ServiceIdx idx)
46 ServiceManager sm(SERVICE[idx]);
51 void switch_to_storage_user(const char* label)
53 AccessProvider ap(label);
54 ap.allowAPI("key-manager::api-storage", "rw");
55 ap.applyAndSwithToUser(APP_UID, APP_GID);
58 void switch_to_storage_ocsp_user(const char* label)
60 AccessProvider ap(label);
61 ap.allowAPI("key-manager::api-storage", "rw");
62 ap.allowAPI("key-manager::api-ocsp", "rw");
63 ap.applyAndSwithToUser(APP_UID, APP_GID);
66 DBCleanup::~DBCleanup()
68 // Let it throw. If db can't be cleared further tests are unreliable
69 CKM::ManagerShPtr mgr = CKM::Manager::create();
70 for(const auto& it:m_aliases)
75 // returns process label
79 char* my_label = NULL;
80 RUNNER_ASSERT_MSG(0 <= (ret = smack_new_label_from_self(&my_label)),
81 "Failed to get smack label for self. Error: " << ret);
83 return CharPtr(my_label, free);
86 std::string aliasWithLabel(const char *label, const char *alias)
91 ss << label << std::string(ckmc_label_name_separator) << alias;
94 return std::string(alias);
97 // changes process label
98 void change_label(const char* label)
100 int ret = smack_set_label_for_self(label);
101 RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self. Error: " << ret);
104 ScopedLabel::ScopedLabel(const char* label) : m_original_label(get_label())
109 ScopedLabel::~ScopedLabel()
112 * Let it throw. If we can't restore label then remaining tests results will be
115 change_label(m_original_label.get());
118 const char * CKMCErrorToString(int error) {
119 #define ERRORDESCRIBE(name) case name: return #name
121 ERRORDESCRIBE(CKMC_ERROR_NONE);
122 ERRORDESCRIBE(CKMC_ERROR_INVALID_PARAMETER);
123 ERRORDESCRIBE(CKMC_ERROR_OUT_OF_MEMORY);
124 ERRORDESCRIBE(CKMC_ERROR_PERMISSION_DENIED);
125 ERRORDESCRIBE(CKMC_ERROR_SOCKET);
126 ERRORDESCRIBE(CKMC_ERROR_BAD_REQUEST);
127 ERRORDESCRIBE(CKMC_ERROR_BAD_RESPONSE);
128 ERRORDESCRIBE(CKMC_ERROR_SEND_FAILED);
129 ERRORDESCRIBE(CKMC_ERROR_RECV_FAILED);
130 ERRORDESCRIBE(CKMC_ERROR_AUTHENTICATION_FAILED);
131 ERRORDESCRIBE(CKMC_ERROR_BUFFER_TOO_SMALL);
132 ERRORDESCRIBE(CKMC_ERROR_SERVER_ERROR);
133 ERRORDESCRIBE(CKMC_ERROR_DB_LOCKED);
134 ERRORDESCRIBE(CKMC_ERROR_DB_ERROR);
135 ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_EXISTS);
136 ERRORDESCRIBE(CKMC_ERROR_DB_ALIAS_UNKNOWN);
137 ERRORDESCRIBE(CKMC_ERROR_VERIFICATION_FAILED);
138 ERRORDESCRIBE(CKMC_ERROR_INVALID_FORMAT);
139 ERRORDESCRIBE(CKMC_ERROR_FILE_ACCESS_DENIED);
140 ERRORDESCRIBE(CKMC_ERROR_NOT_EXPORTABLE);
141 ERRORDESCRIBE(CKMC_ERROR_FILE_SYSTEM);
142 ERRORDESCRIBE(CKMC_ERROR_UNKNOWN);
143 default: return "Error not defined";
148 std::string CKMCReadableError(int error) {
149 std::string output("Error: ");
150 output += std::to_string(error);
151 output += " Description: ";
152 output += CKMCErrorToString(error);
156 void unlock_user_data(uid_t user_id, const char *passwd)
159 auto control = CKM::Control::create();
160 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->unlockUserKey(user_id, passwd)),
161 "Error=" << CKM::ErrorToString(ret));
164 void remove_user_data(uid_t user_id)
167 auto control = CKM::Control::create();
168 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->lockUserKey(user_id)),
169 "Error=" << CKM::ErrorToString(ret));
170 RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->removeUserData(user_id)),
171 "Remove user data failed with error: " << CKM::ErrorToString(ret));
174 void reset_user_data(uid_t user_id, const char *passwd)
176 remove_user_data(user_id);
177 unlock_user_data(user_id, passwd);