Added test cases for SMACK L-access mode
[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 const uid_t APP_UID = 5000;
35 const gid_t APP_GID = 5000;
36 const uid_t DB_ALARM_UID = 6001;
37 const gid_t DB_ALARM_GID = 6001;
38
39 typedef std::unique_ptr<smack_accesses, std::function<void(smack_accesses*)> > AccessesUniquePtr;
40 typedef std::unique_ptr<int, std::function<void(int*)> > FDUniquePtr;
41
42 int smack_runtime_check(void);
43 int smack_check(void);
44 int drop_root_privileges(void);
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
131 void closeFdPtr(int *fd);
132 void setLabelForSelf(const int line, const char *label);
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