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/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>
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;
39 typedef std::unique_ptr<smack_accesses, std::function<void(smack_accesses*)> > AccessesUniquePtr;
40 typedef std::unique_ptr<int, std::function<void(int*)> > FDUniquePtr;
42 int smack_runtime_check(void);
43 int smack_check(void);
44 int drop_root_privileges(void);
46 #define RUNNER_TEST_SMACK(Proc) \
48 static int Static##Proc##Init() \
51 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
55 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
58 #define RUNNER_TEST_NOSMACK(Proc) \
60 static int Static##Proc##Init() \
62 if(!(smack_check())) \
63 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
67 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
70 #define RUNNER_CHILD_TEST_SMACK(Proc) \
73 static int Static##Proc##Init() \
76 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
79 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
81 DPL::Test::RunChildProc(&Proc##Child); \
85 #define RUNNER_CHILD_TEST_NOSMACK(Proc) \
88 static int Static##Proc##Init() \
90 if(!(smack_check())) \
91 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
94 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
96 DPL::Test::RunChildProc(&Proc##Child); \
100 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc) \
102 void Proc##Multi(); \
103 static int Static##Proc##Init() \
106 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
109 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
111 DPL::Test::RunMultiProc(&Proc##Multi); \
115 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc) \
117 void Proc##Multi(); \
118 static int Static##Proc##Init() \
120 if(!(smack_check())) \
121 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
124 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
126 DPL::Test::RunMultiProc(&Proc##Multi); \
131 void closeFileDsr(int *fd);
132 void dropRootPrivileges(const int line);
133 void setLabelForSelf(const int line, const char *label);
134 void addSmackRule(const int line, const char *subject, const char *object, const char *access);
135 void removeSmackRule(const int line, const char *subject, const char *object, const char *access);
144 static int db_result;
147 db_result = perm_begin();
148 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
149 "perm_begin returned: " << db_result);
153 db_result = perm_end();
158 // Database Transaction macros
159 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
160 // They are used to prevent developer from forgetting to close transaction.
161 // Also note that variables defined between these macros will not be visible
165 DB::Transaction db_transaction;
168 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result, \
169 "perm_end returned: " << DB::Transaction::db_result);
172 typedef std::unique_ptr<smack_accesses,std::function<void (smack_accesses*)> > SmackUniquePtr;
174 // Common macros and labels used in tests
175 extern const char *WGT_APP_ID;