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