Fix smack_check() function
[platform/core/test/security-tests.git] / src / common / tests_common.h
1 /*
2  * Copyright (c) 2013-2015 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 const std::string TMP_DIR("/tmp");
42
43 bool 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 int files_compare(int fd1, int fd2);
50
51 #define RUNNER_TEST_SMACK(Proc)                                                     \
52     void Proc();                                                                    \
53     static int Static##Proc##Init()                                                 \
54     {                                                                               \
55         if(smack_check())                                                           \
56             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
57                                                                                     \
58         return 0;                                                                   \
59     }                                                                               \
60     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
61     void Proc()
62
63 #define RUNNER_TEST_NOSMACK(Proc)                                                   \
64     void Proc();                                                                    \
65     static int Static##Proc##Init()                                                 \
66     {                                                                               \
67         if(!(smack_check()))                                                        \
68             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
69                                                                                     \
70         return 0;                                                                   \
71     }                                                                               \
72     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
73     void Proc()
74
75 #define RUNNER_CHILD_TEST_SMACK(Proc)                                                \
76     void Proc();                                                                     \
77     void Proc##Child();                                                              \
78     static int Static##Proc##Init()                                                  \
79     {                                                                                \
80         if(smack_check())                                                            \
81             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
82         return 0;                                                                    \
83     }                                                                                \
84     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
85     void Proc(){                                                                     \
86             DPL::Test::RunChildProc(&Proc##Child);                                   \
87     }                                                                                \
88     void Proc##Child()
89
90 #define RUNNER_CHILD_TEST_NOSMACK(Proc)                                              \
91     void Proc();                                                                     \
92     void Proc##Child();                                                              \
93     static int Static##Proc##Init()                                                  \
94     {                                                                                \
95         if(!(smack_check()))                                                         \
96             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
97         return 0;                                                                    \
98     }                                                                                \
99     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
100     void Proc(){                                                                     \
101             DPL::Test::RunChildProc(&Proc##Child);                                   \
102     }                                                                                \
103     void Proc##Child()
104
105 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc)                                         \
106     void Proc();                                                                     \
107     void Proc##Multi();                                                              \
108     static int Static##Proc##Init()                                                  \
109     {                                                                                \
110         if(smack_check())                                                            \
111             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
112         return 0;                                                                    \
113     }                                                                                \
114     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
115     void Proc(){                                                                     \
116             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
117     }                                                                                \
118     void Proc##Multi()
119
120 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc)                                       \
121     void Proc();                                                                     \
122     void Proc##Multi();                                                              \
123     static int Static##Proc##Init()                                                  \
124     {                                                                                \
125         if(!(smack_check()))                                                         \
126             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
127         return 0;                                                                    \
128     }                                                                                \
129     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
130     void Proc(){                                                                     \
131             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
132     }                                                                                \
133     void Proc##Multi()
134
135 namespace DB {
136
137     class Transaction
138     {
139     public:
140
141         static int db_result;
142
143         Transaction() {
144             db_result = perm_begin();
145             RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
146                               "perm_begin returned: " << db_result);
147         }
148
149         ~Transaction() {
150             db_result = perm_end();
151         }
152     };
153 } // namespace DB
154
155 // Database Transaction macros
156 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
157 // They are used to prevent developer from forgetting to close transaction.
158 // Also note that variables defined between these macros will not be visible
159 // after DB_END.
160 #define DB_BEGIN                       \
161         {                              \
162         DB::Transaction db_transaction;
163
164 #define DB_END }                                                                   \
165         RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
166         "perm_end returned: " << DB::Transaction::db_result);
167
168 // Common macros and labels used in tests
169 extern const char *WGT_APP_ID;
170
171 #endif