CKM: add tests for system-db.
[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 <unordered_set>
26 #include <memory>
27 #include <ckm/ckm-type.h>
28 #include <ckmc/ckmc-type.h>
29 #include <ckmc/ckmc-error.h>
30 #include <tests_common.h>
31 #include <sys/types.h>
32
33 void switch_to_storage_user(const char* label);
34 void switch_to_storage_ocsp_user(const char* label);
35
36
37 // RUNNER_ASSERT wrappers
38 template <typename F, typename... Args>
39 void assert_result(int expected, F&& func, Args... args)
40 {
41     int ret = func(args...);
42     RUNNER_ASSERT_MSG(ret == expected, "Expected " << expected << " got: " << ret);
43 }
44
45 template <typename F, typename... Args>
46 void assert_positive(F&& func, Args... args)
47 {
48     assert_result(CKMC_ERROR_NONE, std::move(func), args...);
49 }
50
51 template <typename F, typename... Args>
52 void assert_invalid_param(F&& func, Args... args)
53 {
54     assert_result(CKMC_ERROR_INVALID_PARAMETER, std::move(func), args...);
55 }
56
57
58 // list operations
59 template <typename T>
60 size_t list_size(const T* list)
61 {
62     size_t size = 0;
63     while(list) {
64         list = list->next;
65         size++;
66     }
67     return size;
68 }
69
70
71 // service lifecycle management
72 enum ServiceIdx {
73     LISTENER,
74     MANAGER
75 };
76 void start_service(ServiceIdx idx);
77 void stop_service(ServiceIdx idx);
78
79 // support for error printing
80 const char * CKMCErrorToString(int error);
81 std::string CKMCReadableError(int error);
82
83 // Class responsible for db cleanup after positive tests. Will remove all used aliases in destructor
84 class DBCleanup
85 {
86 public:
87     DBCleanup() {}
88
89     const char* alias(const char* alias)
90     {
91         return m_aliases.insert(CKM::Alias(alias)).first->c_str();
92     }
93
94     ~DBCleanup();
95
96 private:
97     std::unordered_set<CKM::Alias> m_aliases;
98 };
99
100 // scoped free
101 typedef std::unique_ptr<char, void (*)(void *)> CharPtr;
102
103 // returns process label
104 CharPtr get_label();
105
106 std::string aliasWithLabel(const char *label, const char *alias);
107
108 // changes process label
109 void change_label(const char* label);
110
111 // changes process label upon construction and restores it upon destruction
112 class ScopedLabel
113 {
114 public:
115     ScopedLabel(const char* label);
116     ~ScopedLabel();
117
118 private:
119     CharPtr m_original_label;
120 };
121
122 void save_data(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
123 class ScopedSaveData
124 {
125 public:
126     ScopedSaveData(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
127     virtual ~ScopedSaveData();
128
129 private:
130     std::string m_alias;
131 };
132
133 class GarbageCollector
134 {
135 public:
136     void save(const char* alias, const char *data, int expected_err = CKMC_ERROR_NONE);
137     virtual ~GarbageCollector();
138
139 private:
140     struct save_item {
141         std::string item_alias;
142         std::string owner_label;
143         uid_t       owner_uid;
144         gid_t       owner_gid;
145     };
146     std::vector<save_item>  m_garbage;
147 };
148
149 class ScopedDBUnlock
150 {
151 public:
152     ScopedDBUnlock(uid_t user_id, const char* passwd);
153     virtual ~ScopedDBUnlock();
154
155 private:
156     uid_t m_uid;
157 };
158
159 void check_remove_allowed(const char* alias);
160 void check_remove_denied(const char* alias);
161 void check_remove_not_visible(const char* alias);
162 void check_read(const char* alias, const char *label, const char *test_data, int expected_code = CKMC_ERROR_NONE);
163 void check_read_allowed(const char* alias, const char *data);
164 void check_read_not_visible(const char* alias);
165 void allow_access(const char* alias, const char* accessor, int permissionMask);
166 void allow_access_negative(const char* alias, const char* accessor, int permissionMask, int expectedCode);
167 void deny_access(const char* alias, const char* accessor);
168 void deny_access_negative(const char* alias, const char* accessor, int expectedCode);
169
170 void unlock_user_data(uid_t user_id, const char *passwd);
171 void remove_user_data(uid_t user_id);
172 void reset_user_data(uid_t user_id, const char *passwd);
173
174 ckmc_raw_buffer_s prepare_message_buffer(const char * input);
175 void check_alias_list(const CKM::AliasVector& expected);