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