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