Add cynara test client class
[platform/core/test/security-tests.git] / tests / common / tests_common.h
1 /*
2  * Copyright (c) 2013 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 /*
18  * @file        tests_common.h
19  * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
20  * @version     1.0
21  * @brief       Common functions and macros used in security-tests package.
22  */
23
24 #ifndef _TESTS_COMMON_H_
25 #define _TESTS_COMMON_H_
26
27 #include <sys/smack.h>
28 #include <dpl/test/test_runner.h>
29 #include <dpl/test/test_runner_child.h>
30 #include <dpl/test/test_runner_multiprocess.h>
31 #include <privilege-control.h>
32 #include <sys/smack.h>
33 #include <string>
34
35 #include "gdbbacktrace.h"
36
37 const uid_t APP_UID = 5000;
38 const gid_t APP_GID = 5000;
39 const uid_t DB_ALARM_UID = 6001;
40 const gid_t DB_ALARM_GID = 6001;
41
42 int smack_runtime_check(void);
43 int smack_check(void);
44 int drop_root_privileges(void);
45 void setLabelForSelf(const int line, const char *label);
46 void add_process_group(const char* group_name);
47 void remove_process_group(const char* group_name);
48 std::string formatCstr(const char *cstr);
49
50 #define RUNNER_TEST_SMACK(Proc)                                                     \
51     void Proc();                                                                    \
52     static int Static##Proc##Init()                                                 \
53     {                                                                               \
54         if(smack_check())                                                           \
55             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
56                                                                                     \
57         return 0;                                                                   \
58     }                                                                               \
59     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
60     void Proc()
61
62 #define RUNNER_TEST_NOSMACK(Proc)                                                   \
63     void Proc();                                                                    \
64     static int Static##Proc##Init()                                                 \
65     {                                                                               \
66         if(!(smack_check()))                                                        \
67             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
68                                                                                     \
69         return 0;                                                                   \
70     }                                                                               \
71     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
72     void Proc()
73
74 #define RUNNER_CHILD_TEST_SMACK(Proc)                                                \
75     void Proc();                                                                     \
76     void Proc##Child();                                                              \
77     static int Static##Proc##Init()                                                  \
78     {                                                                                \
79         if(smack_check())                                                            \
80             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
81         return 0;                                                                    \
82     }                                                                                \
83     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
84     void Proc(){                                                                     \
85             DPL::Test::RunChildProc(&Proc##Child);                                   \
86     }                                                                                \
87     void Proc##Child()
88
89 #define RUNNER_CHILD_TEST_NOSMACK(Proc)                                              \
90     void Proc();                                                                     \
91     void Proc##Child();                                                              \
92     static int Static##Proc##Init()                                                  \
93     {                                                                                \
94         if(!(smack_check()))                                                         \
95             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
96         return 0;                                                                    \
97     }                                                                                \
98     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
99     void Proc(){                                                                     \
100             DPL::Test::RunChildProc(&Proc##Child);                                   \
101     }                                                                                \
102     void Proc##Child()
103
104 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc)                                         \
105     void Proc();                                                                     \
106     void Proc##Multi();                                                              \
107     static int Static##Proc##Init()                                                  \
108     {                                                                                \
109         if(smack_check())                                                            \
110             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
111         return 0;                                                                    \
112     }                                                                                \
113     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
114     void Proc(){                                                                     \
115             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
116     }                                                                                \
117     void Proc##Multi()
118
119 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc)                                       \
120     void Proc();                                                                     \
121     void Proc##Multi();                                                              \
122     static int Static##Proc##Init()                                                  \
123     {                                                                                \
124         if(!(smack_check()))                                                         \
125             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
126         return 0;                                                                    \
127     }                                                                                \
128     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
129     void Proc(){                                                                     \
130             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
131     }                                                                                \
132     void Proc##Multi()
133
134 #define RUNNER_ASSERT_MSG_BT(test, msg) RUNNER_ASSERT_MSG(test,\
135                                                           msg << gdbbacktrace())
136 #define RUNNER_ASSERT_BT(test) RUNNER_ASSERT_MSG_BT(test, "")
137
138 namespace DB {
139
140     class Transaction
141     {
142     public:
143
144         static int db_result;
145
146         Transaction() {
147             db_result = perm_begin();
148             RUNNER_ASSERT_MSG_BT(PC_OPERATION_SUCCESS == db_result,
149                               "perm_begin returned: " << db_result);
150         }
151
152         ~Transaction() {
153             db_result = perm_end();
154         }
155     };
156 } // namespace DB
157
158 // Database Transaction macros
159 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
160 // They are used to prevent developer from forgetting to close transaction.
161 // Also note that variables defined between these macros will not be visible
162 // after DB_END.
163 #define DB_BEGIN                       \
164         {                              \
165         DB::Transaction db_transaction;
166
167 #define DB_END }                                                                   \
168         RUNNER_ASSERT_MSG_BT(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
169         "perm_end returned: " << DB::Transaction::db_result);
170
171 // Common macros and labels used in tests
172 extern const char *WGT_APP_ID;
173
174 #endif