Use ScopedClose instead unique_ptr.
[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/scoped_close.h>
29 #include <dpl/test/test_runner.h>
30 #include <dpl/test/test_runner_child.h>
31 #include <dpl/test/test_runner_multiprocess.h>
32 #include <privilege-control.h>
33 #include <sys/smack.h>
34
35 #include "gdbbacktrace.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 typedef std::unique_ptr<smack_accesses, std::function<void(smack_accesses*)> > AccessesUniquePtr;
43 typedef std::unique_ptr<int, std::function<void(int*)> > FDUniquePtr;
44
45 int smack_runtime_check(void);
46 int smack_check(void);
47 int drop_root_privileges(void);
48
49 #define RUNNER_TEST_SMACK(Proc)                                                     \
50     void Proc();                                                                    \
51     static int Static##Proc##Init()                                                 \
52     {                                                                               \
53         if(smack_check())                                                           \
54             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
55                                                                                     \
56         return 0;                                                                   \
57     }                                                                               \
58     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
59     void Proc()
60
61 #define RUNNER_TEST_NOSMACK(Proc)                                                   \
62     void Proc();                                                                    \
63     static int Static##Proc##Init()                                                 \
64     {                                                                               \
65         if(!(smack_check()))                                                        \
66             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);  \
67                                                                                     \
68         return 0;                                                                   \
69     }                                                                               \
70     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();              \
71     void Proc()
72
73 #define RUNNER_CHILD_TEST_SMACK(Proc)                                                \
74     void Proc();                                                                     \
75     void Proc##Child();                                                              \
76     static int Static##Proc##Init()                                                  \
77     {                                                                                \
78         if(smack_check())                                                            \
79             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
80         return 0;                                                                    \
81     }                                                                                \
82     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
83     void Proc(){                                                                     \
84             DPL::Test::RunChildProc(&Proc##Child);                                   \
85     }                                                                                \
86     void Proc##Child()
87
88 #define RUNNER_CHILD_TEST_NOSMACK(Proc)                                              \
89     void Proc();                                                                     \
90     void Proc##Child();                                                              \
91     static int Static##Proc##Init()                                                  \
92     {                                                                                \
93         if(!(smack_check()))                                                         \
94             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
95         return 0;                                                                    \
96     }                                                                                \
97     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
98     void Proc(){                                                                     \
99             DPL::Test::RunChildProc(&Proc##Child);                                   \
100     }                                                                                \
101     void Proc##Child()
102
103 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc)                                         \
104     void Proc();                                                                     \
105     void Proc##Multi();                                                              \
106     static int Static##Proc##Init()                                                  \
107     {                                                                                \
108         if(smack_check())                                                            \
109             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
110         return 0;                                                                    \
111     }                                                                                \
112     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
113     void Proc(){                                                                     \
114             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
115     }                                                                                \
116     void Proc##Multi()
117
118 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc)                                       \
119     void Proc();                                                                     \
120     void Proc##Multi();                                                              \
121     static int Static##Proc##Init()                                                  \
122     {                                                                                \
123         if(!(smack_check()))                                                         \
124             DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc);   \
125         return 0;                                                                    \
126     }                                                                                \
127     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();               \
128     void Proc(){                                                                     \
129             DPL::Test::RunMultiProc(&Proc##Multi);                                   \
130     }                                                                                \
131     void Proc##Multi()
132
133 #define RUNNER_ASSERT_MSG_BT(test, msg) RUNNER_ASSERT_MSG(test,\
134                                                           msg << gdbbacktrace())
135 #define RUNNER_ASSERT_BT(test) RUNNER_ASSERT_MSG_BT(test, "")
136
137 void closeFdPtr(int *fd);
138 void setLabelForSelf(const int line, const char *label);
139
140 class ScopedClose : public DPL::ScopedResource<DPL::ScopedClosePolicy>
141 {
142     typedef DPL::ScopedClosePolicy Policy;
143     typedef DPL::ScopedResource<Policy> BaseType;
144     typedef DPL::ScopedClosePolicy::Type Type;
145
146 public:
147     explicit ScopedClose(Type handle = Policy::NullValue()) :
148         BaseType(handle)
149     { }
150     Type* Ptr() {
151         return &m_value;
152     }
153 };
154
155 namespace DB {
156
157     class Transaction
158     {
159     public:
160
161         static int db_result;
162
163         Transaction() {
164             db_result = perm_begin();
165             RUNNER_ASSERT_MSG_BT(PC_OPERATION_SUCCESS == db_result,
166                               "perm_begin returned: " << db_result);
167         }
168
169         ~Transaction() {
170             db_result = perm_end();
171         }
172     };
173 } // namespace DB
174
175 // Database Transaction macros
176 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
177 // They are used to prevent developer from forgetting to close transaction.
178 // Also note that variables defined between these macros will not be visible
179 // after DB_END.
180 #define DB_BEGIN                       \
181         {                              \
182         DB::Transaction db_transaction;
183
184 #define DB_END }                                                                   \
185         RUNNER_ASSERT_MSG_BT(PC_OPERATION_SUCCESS == DB::Transaction::db_result,      \
186         "perm_end returned: " << DB::Transaction::db_result);
187
188 // Common macros and labels used in tests
189 extern const char *WGT_APP_ID;
190
191 #endif