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