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 <sys/smack.h>
38 const uid_t APP_UID = 5001;
39 const gid_t APP_GID = 5001;
40 const uid_t APP_UID_2 = 5200;
41 const gid_t APP_GID_2 = 5200;
42 const uid_t DB_ALARM_UID = 6001;
43 const gid_t DB_ALARM_GID = 6001;
44 const std::string TMP_DIR("/tmp");
46 bool smack_check(void);
47 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
48 void setLabelForSelf(const int line, const char *label);
49 void add_process_group(const char* group_name);
50 void remove_process_group(const char* group_name);
51 std::string formatCstr(const char *cstr);
52 int files_compare(int fd1, int fd2);
53 void mkdirSafe(const std::string &path, mode_t mode);
54 void mktreeSafe(const std::string &path, mode_t mode);
55 void creatSafe(const std::string &path, mode_t mode);
56 void symlinkSafe(const std::string &targetPath, const std::string &linkPath);
57 void removeDir(const std::string &path);
60 #define RUNNER_TEST_SMACK(Proc, ...) \
61 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
62 static int Static##Proc##Init() \
65 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
66 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
69 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
70 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
72 #define RUNNER_TEST_NOSMACK(Proc, ...) \
73 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
74 static int Static##Proc##Init() \
77 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
78 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
81 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
82 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
84 #define RUNNER_CHILD_TEST_SMACK(Proc, ...) \
85 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
86 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
87 static int Static##Proc##Init() \
90 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
91 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
94 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
95 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
96 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
98 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
100 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...) \
101 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
102 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
103 static int Static##Proc##Init() \
105 if (!smack_check()) \
106 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
107 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
110 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
111 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
112 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
114 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
116 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...) \
117 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
118 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
119 static int Static##Proc##Init() \
122 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
123 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
126 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
127 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
128 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
130 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
132 #define RUNNER_MULTIPROCESS_TEST_NOSMACK(Proc, ...) \
133 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
134 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
135 static int Static##Proc##Init() \
137 if (!smack_check()) \
138 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
139 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
142 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
143 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
144 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
146 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)