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