Separate ckm-test by the need of capability
[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 <stdexcept>
27 #include <ckm/ckm-type.h>
28 #include <ckm/ckm-manager-async.h>
29 #include <ckmc/ckmc-type.h>
30 #include <ckmc/ckmc-error.h>
31 #include <tests_common.h>
32 #include <sys/types.h>
33
34 extern const std::string SMACK_USER_APP_PREFIX;
35 extern const char *SYSTEM_LABEL;
36 extern const char *TEST_LABEL;
37 extern const char *TEST_LABEL_2;
38 extern const char *TEST_LABEL_3;
39 extern const char *TEST_LABEL_4;
40 extern const char *TEST_LABEL_5;
41
42 // support for error printing
43 const char * CKMCErrorToString(int error);
44 const char * CKMErrorToString(int error);
45 std::string CKMCReadableError(int error);
46
47 // RUNNER_ASSERT wrappers
48 template <typename F, typename... Args>
49 void assert_result(int expected, F&& func, Args... args)
50 {
51     int ret = func(args...);
52     RUNNER_ASSERT_MSG(ret == expected,
53                       "Expected: " << CKMCErrorToString(expected) << "(" << expected << ")"
54                       " got: " << CKMCErrorToString(ret) << "(" << ret << ")");
55 }
56
57 template <typename F, typename... Args>
58 void assert_positive(F&& func, Args... args)
59 {
60     assert_result(CKMC_ERROR_NONE, std::move(func), args...);
61 }
62
63 template <typename F, typename... Args>
64 void assert_invalid_param(F&& func, Args... args)
65 {
66     assert_result(CKMC_ERROR_INVALID_PARAMETER, std::move(func), args...);
67 }
68
69
70 // list operations
71 template <typename T>
72 size_t list_size(const T* list)
73 {
74     size_t size = 0;
75     while(list) {
76         list = list->next;
77         size++;
78     }
79     return size;
80 }
81
82 // scoped free
83 typedef std::unique_ptr<char, void (*)(void *)> CharPtr;
84
85 std::string getLabel();
86 // returns process owner id
87 std::string getOwnerIdFromSelf();
88
89 std::string aliasWithLabel(const char *label, const char *alias);
90 std::string aliasWithLabelFromSelf(const char *alias);
91
92 void save_data(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
93 void save_data(const char* alias, const char *data, size_t len, int expected_err);
94 class ScopedSaveData
95 {
96 public:
97     ScopedSaveData(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
98     virtual ~ScopedSaveData();
99
100 private:
101     std::string m_alias;
102 };
103
104 class ScopedDBUnlock
105 {
106 public:
107     ScopedDBUnlock(uid_t user_id, const char* passwd);
108     virtual ~ScopedDBUnlock();
109
110 private:
111     uid_t m_uid;
112 };
113
114 void check_remove_allowed(const char* alias);
115 void check_remove_denied(const char* alias);
116 void check_remove_not_visible(const char* alias);
117 void check_read(const char* alias,
118                 const char *label,
119                 const char *test_data,
120                 size_t len,
121                 int expected_code = CKMC_ERROR_NONE);
122 void check_read(const char* alias,
123                 const char *label,
124                 const char *test_data,
125                 int expected_code = CKMC_ERROR_NONE);
126 void check_read_allowed(const char* alias, const char *data);
127 void check_read_not_visible(const char* alias);
128 void check_key(const char *alias,
129                int expected_error = CKMC_ERROR_NONE,
130                ckmc_key_type_e expected_type = CKMC_KEY_NONE);
131 void check_key_allowed(const char *alias, ckmc_key_type_e expected_type = CKMC_KEY_NONE);
132 void check_key_not_visible(const char *alias);
133 void check_cert_allowed(const char *alias);
134 void check_cert_not_visible(const char *alias);
135 void allow_access(const char* alias, const char* accessor, int permissionMask);
136 void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode);
137 void deny_access(const char* alias, const char* accessor);
138 void deny_access_negative(const char* alias, const char* accessor, int expectedCode);
139
140 void unlock_user_data(uid_t user_id, const char *passwd);
141 void remove_user_data(uid_t user_id);
142 void reset_user_data(uid_t user_id, const char *passwd);
143
144 ckmc_raw_buffer_s prepare_message_buffer(const char * input);
145 void check_alias_list(const CKM::AliasVector& expected);
146
147 typedef enum {
148     ALIAS_KEY,
149     ALIAS_CERT,
150     ALIAS_DATA
151 } alias_type_;
152 size_t count_aliases(alias_type_ type, size_t minimum_initial_element_count = 0);
153 std::string sharedDatabase(const CKM::Alias & alias);
154 CKM::RawBuffer createRandomBuffer(size_t random_bytes);
155 ckmc_raw_buffer_s* createRandomBufferCAPI(size_t random_bytes);
156
157 ckmc_key_s *generate_AES_key(size_t lengthBits, const char *passwd);
158 void validate_AES_key(ckmc_key_s *analyzed);
159 void compare_AES_keys(ckmc_key_s *first, ckmc_key_s *second); // true if equal
160
161 // Test env class for database cleanup. Pass database uids to cleanup before and after test
162 template <uid_t ...Args>
163 class RemoveDataEnv;
164
165 template <>
166 class RemoveDataEnv<>
167 {
168 public:
169     void init(const std::string&)
170     {}
171     void finish()
172     {}
173 };
174
175 template <uid_t UID, uid_t ...Args>
176 class RemoveDataEnv<UID, Args...> : public RemoveDataEnv<Args...>
177 {
178 public:
179     void init(const std::string & str) {
180         remove_user_data(UID);
181         RemoveDataEnv<Args...>::init(str);
182     }
183     void finish() {
184         RemoveDataEnv<Args...>::finish();
185         remove_user_data(UID);
186     }
187 };
188
189 typedef std::shared_ptr<ckmc_raw_buffer_s> RawBufferPtr;
190 typedef std::shared_ptr<struct __ckmc_param_list> ParamListPtr;
191
192 ParamListPtr createParamListPtr();
193
194 void assert_buffers_equal(const ckmc_raw_buffer_s b1, const ckmc_raw_buffer_s b2, bool equal=true);
195
196 RawBufferPtr create_raw_buffer(ckmc_raw_buffer_s* buffer);
197
198
199 template <typename F, typename... Args>
200 void test_no_observer(F&& func, Args... args)
201 {
202     CKM::ManagerAsync::ObserverPtr obs;
203     CKM::ManagerAsync mgr;
204
205     try {
206         (mgr.*func)(obs, args...);
207         RUNNER_ASSERT_MSG(false, "function() should have thrown an exception");
208     } catch (const std::invalid_argument& e) {
209         RUNNER_ASSERT(true);
210     } catch (...) {
211         RUNNER_ASSERT_MSG(false, "Unexpected exception");
212     }
213 }