CKM: Run async api tests in single process
[platform/core/test/security-tests.git] / src / ckm / ckm-common.h
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.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <string>
25 #include <memory>
26 #include <ckm/ckm-type.h>
27 #include <ckmc/ckmc-type.h>
28 #include <ckmc/ckmc-error.h>
29 #include <tests_common.h>
30 #include <sys/types.h>
31 #include <access_provider2.h>
32
33 // RUNNER_ASSERT wrappers
34 template <typename F, typename... Args>
35 void assert_result(int expected, F&& func, Args... args)
36 {
37     int ret = func(args...);
38     RUNNER_ASSERT_MSG(ret == expected, "Expected " << expected << " got: " << ret);
39 }
40
41 template <typename F, typename... Args>
42 void assert_positive(F&& func, Args... args)
43 {
44     assert_result(CKMC_ERROR_NONE, std::move(func), args...);
45 }
46
47 template <typename F, typename... Args>
48 void assert_invalid_param(F&& func, Args... args)
49 {
50     assert_result(CKMC_ERROR_INVALID_PARAMETER, std::move(func), args...);
51 }
52
53
54 // list operations
55 template <typename T>
56 size_t list_size(const T* list)
57 {
58     size_t size = 0;
59     while(list) {
60         list = list->next;
61         size++;
62     }
63     return size;
64 }
65
66
67 // service lifecycle management
68 enum ServiceIdx {
69     LISTENER,
70     MANAGER
71 };
72 void start_service(ServiceIdx idx);
73 void stop_service(ServiceIdx idx);
74
75 // support for error printing
76 const char * CKMCErrorToString(int error);
77 std::string CKMCReadableError(int error);
78
79 // scoped free
80 typedef std::unique_ptr<char, void (*)(void *)> CharPtr;
81
82 // returns process label
83 CharPtr get_label();
84
85 std::string aliasWithLabel(const char *label, const char *alias);
86
87 // changes process label
88 void change_label(const char* label);
89
90 // changes process label upon construction and restores it upon destruction
91 class ScopedLabel
92 {
93 public:
94     ScopedLabel(const char* label);
95     ~ScopedLabel();
96
97 private:
98     CharPtr m_original_label;
99 };
100
101 void save_data(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
102 void save_data(const char* alias, const char *data, size_t len, int expected_err);
103 class ScopedSaveData
104 {
105 public:
106     ScopedSaveData(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
107     virtual ~ScopedSaveData();
108
109 private:
110     std::string m_alias;
111 };
112
113 class GarbageCollector
114 {
115 public:
116     void save(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
117     void save(const char* alias, const char *data, size_t len, int expected_err);
118     void add(const char* alias);
119     virtual ~GarbageCollector();
120
121 private:
122     struct save_item {
123         std::string item_alias;
124         std::string owner_label;
125         uid_t       owner_uid;
126         gid_t       owner_gid;
127     };
128     std::vector<save_item>  m_garbage;
129 };
130
131 class ScopedDBUnlock
132 {
133 public:
134     ScopedDBUnlock(uid_t user_id, const char* passwd);
135     virtual ~ScopedDBUnlock();
136
137 private:
138     uid_t m_uid;
139 };
140
141 void check_remove_allowed(const char* alias);
142 void check_remove_denied(const char* alias);
143 void check_remove_not_visible(const char* alias);
144 void check_read(const char* alias,
145                 const char *label,
146                 const char *test_data,
147                 size_t len,
148                 int expected_code = CKMC_ERROR_NONE);
149 void check_read(const char* alias,
150                 const char *label,
151                 const char *test_data,
152                 int expected_code = CKMC_ERROR_NONE);
153 void check_read_allowed(const char* alias, const char *data);
154 void check_read_not_visible(const char* alias);
155 void check_key(const char *alias,
156                int expected_error = CKMC_ERROR_NONE,
157                ckmc_key_type_e expected_type = CKMC_KEY_NONE);
158 void check_key_allowed(const char *alias, ckmc_key_type_e expected_type = CKMC_KEY_NONE);
159 void check_key_not_visible(const char *alias);
160 void check_cert_allowed(const char *alias);
161 void check_cert_not_visible(const char *alias);
162 void allow_access(const char* alias, const char* accessor, int permissionMask);
163 void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode);
164 void deny_access(const char* alias, const char* accessor);
165 void deny_access_negative(const char* alias, const char* accessor, int expectedCode);
166
167 void unlock_user_data(uid_t user_id, const char *passwd);
168 void remove_user_data(uid_t user_id);
169 void reset_user_data(uid_t user_id, const char *passwd);
170
171 ckmc_raw_buffer_s prepare_message_buffer(const char * input);
172 void check_alias_list(const CKM::AliasVector& expected);
173
174 typedef enum {
175     ALIAS_KEY,
176     ALIAS_CERT,
177     ALIAS_DATA
178 } alias_type_;
179 size_t count_aliases(alias_type_ type, size_t minimum_initial_element_count = 0);
180 std::string sharedDatabase(const CKM::Alias & alias);
181
182 // Test env class for database cleanup. Pass database uids to cleanup before and after test
183 template <uid_t ...Args>
184 class RemoveDataEnv;
185
186 template <>
187 class RemoveDataEnv<>
188 {
189 public:
190     void init(const std::string&)
191     {}
192     void finish()
193     {}
194 };
195
196 template <uid_t UID, uid_t ...Args>
197 class RemoveDataEnv<UID, Args...> : public RemoveDataEnv<Args...>
198 {
199 public:
200     void init(const std::string & str) {
201         remove_user_data(UID);
202         RemoveDataEnv<Args...>::init(str);
203     }
204     void finish() {
205         RemoveDataEnv<Args...>::finish();
206         remove_user_data(UID);
207     }
208 };