47dac475efc9bbe9f11aab204f87f01da4faf250
[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 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(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
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 namespace DB {
135
136     class Transaction
137     {
138     public:
139
140         static int db_result;
141
142         Transaction() {
143             db_result = perm_begin();
144             RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
145                               "perm_begin returned: " << db_result);
146         }
147
148         ~Transaction() {
149             db_result = perm_end();
150         }
151     };
152 } // namespace DB
153
154 // Database Transaction macros
155 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
156 // They are used to prevent developer from forgetting to close transaction.
157 // Also note that variables defined between these macros will not be visible
158 // after DB_END.
159 #define DB_BEGIN                       \
160         {                              \
161         DB::Transaction db_transaction;
162
163 #define DB_END }                                                                   \
164         RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
165         "perm_end returned: " << DB::Transaction::db_result);
166
167 // Common macros and labels used in tests
168 extern const char *WGT_APP_ID;
169
170 #endif