2 * Copyright (c) 2013-2015 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_case_extended.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>
39 const uid_t APP_UID = 5000;
40 const gid_t APP_GID = 5000;
41 const uid_t APP_UID_2 = 5200;
42 const gid_t APP_GID_2 = 5200;
43 const uid_t DB_ALARM_UID = 6001;
44 const gid_t DB_ALARM_GID = 6001;
45 const std::string TMP_DIR("/tmp");
47 bool smack_check(void);
48 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
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 std::string formatCstr(const char *cstr);
53 int files_compare(int fd1, int fd2);
54 void mkdirSafe(const std::string &path, mode_t mode);
55 void mktreeSafe(const std::string &path, mode_t mode);
56 void creatSafe(const std::string &path, mode_t mode);
57 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
58 void removeDir(const std::string &path);
61 #define RUNNER_TEST_SMACK(Proc, ...) \
62 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
63 static int Static##Proc##Init() \
66 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
67 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
70 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
71 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
73 #define RUNNER_TEST_NOSMACK(Proc, ...) \
74 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
75 static int Static##Proc##Init() \
78 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
79 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
82 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
83 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
85 #define RUNNER_CHILD_TEST_SMACK(Proc, ...) \
86 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
87 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
88 static int Static##Proc##Init() \
91 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
92 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
95 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
96 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
97 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
99 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
101 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...) \
102 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
103 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
104 static int Static##Proc##Init() \
106 if (!smack_check()) \
107 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
108 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
111 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
112 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
113 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
115 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
117 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...) \
118 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
119 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
120 static int Static##Proc##Init() \
123 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
124 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
127 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
128 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
129 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
131 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
133 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...) \
134 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
135 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
136 static int Static##Proc##Init() \
138 if (!smack_check()) \
139 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
140 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
143 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
144 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
145 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
147 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
155 static int db_result;
158 db_result = perm_begin();
159 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == db_result,
160 "perm_begin returned: " << db_result);
164 db_result = perm_end();
169 // Database Transaction macros
170 // PLEASE NOTE Both DB_BEGIN and DB_END need to be called in the same scope.
171 // They are used to prevent developer from forgetting to close transaction.
172 // Also note that variables defined between these macros will not be visible
176 DB::Transaction db_transaction;
179 RUNNER_ASSERT_MSG(PC_OPERATION_SUCCESS == DB::Transaction::db_result, \
180 "perm_end returned: " << DB::Transaction::db_result);
182 // Common macros and labels used in tests
183 extern const char *WGT_APP_ID;