8372653a016ba293aede400db63496eb25f4d1a5
[platform/core/test/security-tests.git] / tests / 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 <unordered_set>
25 #include <memory>
26 #include <ckm/ckm-type.h>
27 #include <ckmc/ckmc-error.h>
28 #include <tests_common.h>
29
30 void switch_to_storage_user(const char* label);
31 void switch_to_storage_ocsp_user(const char* label);
32
33
34 // RUNNER_ASSERT wrappers
35 template <typename F, typename... Args>
36 void assert_result(int expected, F&& func, Args... args)
37 {
38     int ret = func(args...);
39     RUNNER_ASSERT_MSG(ret == expected, "Expected " << expected << " got: " << ret);
40 }
41
42 template <typename F, typename... Args>
43 void assert_positive(F&& func, Args... args)
44 {
45     assert_result(CKMC_ERROR_NONE, std::move(func), args...);
46 }
47
48 template <typename F, typename... Args>
49 void assert_invalid_param(F&& func, Args... args)
50 {
51     assert_result(CKMC_ERROR_INVALID_PARAMETER, std::move(func), args...);
52 }
53
54
55 // list operations
56 template <typename T>
57 size_t list_size(const T* list)
58 {
59     size_t size = 0;
60     while(list) {
61         list = list->next;
62         size++;
63     }
64     return size;
65 }
66
67
68 // service lifecycle management
69 enum ServiceIdx {
70     LISTENER,
71     MANAGER
72 };
73 void start_service(ServiceIdx idx);
74 void stop_service(ServiceIdx idx);
75
76 // support for error printing
77 const char * CKMCErrorToString(int error);
78 std::string CKMCReadableError(int error);
79
80 // Class responsible for db cleanup after positive tests. Will remove all used aliases in destructor
81 class DBCleanup
82 {
83 public:
84     DBCleanup() {}
85
86     const char* alias(const char* alias)
87     {
88         return m_aliases.insert(CKM::Alias(alias)).first->c_str();
89     }
90
91     ~DBCleanup();
92
93 private:
94     std::unordered_set<CKM::Alias> m_aliases;
95 };
96
97 // scoped free
98 typedef std::unique_ptr<char, void (*)(void *)> CharPtr;
99
100 // returns process label
101 CharPtr get_label();
102
103 std::string aliasWithLabel(const char *label, const char *alias);
104
105 // changes process label
106 void change_label(const char* label);
107
108 // changes process label upon construction and restores it upon destruction
109 class ScopedLabel
110 {
111 public:
112     ScopedLabel(const char* label);
113     ~ScopedLabel();
114
115 private:
116     CharPtr m_original_label;
117 };