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>
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 const std::string TMP_DIR("/tmp");
43 int smack_runtime_check(void);
44 int smack_check(void);
45 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
46 void setLabelForSelf(const int line, const char *label);
47 void add_process_group(const char* group_name);
48 void remove_process_group(const char* group_name);
49 std::string formatCstr(const char *cstr);
50 int files_compare(int fd1, int fd2);
51 void mkdirSafe(const std::string &path, mode_t mode);
52 void creatSafe(const std::string &path, mode_t mode);
53 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
54 void removeDir(const std::string &path);
56 #define RUNNER_TEST_SMACK(Proc) \
58 static int Static##Proc##Init() \
61 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
65 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
68 #define RUNNER_TEST_NOSMACK(Proc) \
70 static int Static##Proc##Init() \
72 if(!(smack_check())) \
73 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
77 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
80 #define RUNNER_CHILD_TEST_SMACK(Proc) \
83 static int Static##Proc##Init() \
86 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
89 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
91 DPL::Test::RunChildProc(&Proc##Child); \
95 #define RUNNER_CHILD_TEST_NOSMACK(Proc) \
98 static int Static##Proc##Init() \
100 if(!(smack_check())) \
101 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
104 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
106 DPL::Test::RunChildProc(&Proc##Child); \
110 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc) \
112 void Proc##Multi(); \
113 static int Static##Proc##Init() \
116 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
119 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
121 DPL::Test::RunMultiProc(&Proc##Multi); \
125 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc) \
127 void Proc##Multi(); \
128 static int Static##Proc##Init() \
130 if(!(smack_check())) \
131 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
134 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
136 DPL::Test::RunMultiProc(&Proc##Multi); \
146 static int db_result;
149 db_result = perm_begin();
150 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
151 "perm_begin returned: " << db_result);
155 db_result = perm_end();
160 // Database Transaction macros
161 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
162 // They are used to prevent developer from forgetting to close transaction.
163 // Also note that variables defined between these macros will not be visible
167 DB::Transaction db_transaction;
170 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result, \
171 "perm_end returned: " << DB::Transaction::db_result);
173 // Common macros and labels used in tests
174 extern const char *WGT_APP_ID;