2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file tests_common.h
19 * @author Lukasz Kostyra (l.kostyra@partner.samsung.com)
21 * @brief Common functions and macros used in security-tests package.
24 #ifndef _TESTS_COMMON_H_
25 #define _TESTS_COMMON_H_
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>
35 #include "gdbbacktrace.h"
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;
42 typedef std::unique_ptr<smack_accesses, std::function<void(smack_accesses*)> > AccessesUniquePtr;
43 typedef std::unique_ptr<int, std::function<void(int*)> > FDUniquePtr;
45 int smack_runtime_check(void);
46 int smack_check(void);
47 int drop_root_privileges(void);
49 #define RUNNER_TEST_SMACK(Proc) \
51 static int Static##Proc##Init() \
54 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
58 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
61 #define RUNNER_TEST_NOSMACK(Proc) \
63 static int Static##Proc##Init() \
65 if(!(smack_check())) \
66 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
70 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
73 #define RUNNER_CHILD_TEST_SMACK(Proc) \
76 static int Static##Proc##Init() \
79 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
82 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
84 DPL::Test::RunChildProc(&Proc##Child); \
88 #define RUNNER_CHILD_TEST_NOSMACK(Proc) \
91 static int Static##Proc##Init() \
93 if(!(smack_check())) \
94 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
97 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
99 DPL::Test::RunChildProc(&Proc##Child); \
103 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc) \
105 void Proc##Multi(); \
106 static int Static##Proc##Init() \
109 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
112 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
114 DPL::Test::RunMultiProc(&Proc##Multi); \
118 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc) \
120 void Proc##Multi(); \
121 static int Static##Proc##Init() \
123 if(!(smack_check())) \
124 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
127 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
129 DPL::Test::RunMultiProc(&Proc##Multi); \
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, "")
137 void closeFdPtr(int *fd);
138 void setLabelForSelf(const int line, const char *label);
140 class ScopedClose : public DPL::ScopedResource<DPL::ScopedClosePolicy>
142 typedef DPL::ScopedClosePolicy Policy;
143 typedef DPL::ScopedResource<Policy> BaseType;
144 typedef DPL::ScopedClosePolicy::Type Type;
147 explicit ScopedClose(Type handle = Policy::NullValue()) :
161 static int db_result;
164 db_result = perm_begin();
165 RUNNER_ASSERT_MSG_BT(PC_OPERATION_SUCCESS == db_result,
166 "perm_begin returned: " << db_result);
170 db_result = perm_end();
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
182 DB::Transaction db_transaction;
185 RUNNER_ASSERT_MSG_BT(PC_OPERATION_SUCCESS == DB::Transaction::db_result, \
186 "perm_end returned: " << DB::Transaction::db_result);
188 // Common macros and labels used in tests
189 extern const char *WGT_APP_ID;