2 * Copyright (c) 2013 - 2019 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>
34 #include <sys/types.h>
39 const uid_t APP_UID = 5001;
40 const gid_t APP_GID = 100;
42 bool smack_check(void);
43 int drop_root_privileges(uid_t appUid = APP_UID, gid_t appGid = APP_GID);
44 std::string formatCstr(const char *cstr);
45 int files_compare(int fd1, int fd2);
46 void mktreeSafe(const std::string &path, mode_t mode);
47 void waitPid(pid_t pid);
49 pid_t runInChild(const std::function<void(void)> &process);
50 void runInChildParentWait(const std::function<void(void)> &process);
52 #define RUNNER_TEST_SMACK(Proc, ...) \
53 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
54 static int Static##Proc##Init() \
57 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
58 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
61 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
62 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
64 #define RUNNER_TEST_NOSMACK(Proc, ...) \
65 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
66 static int Static##Proc##Init() \
69 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
70 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
73 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
74 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
76 #define RUNNER_CHILD_TEST_SMACK(Proc, ...) \
77 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
78 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
79 static int Static##Proc##Init() \
82 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
83 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
86 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
87 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
88 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
90 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
92 #define RUNNER_CHILD_TEST_NOSMACK(Proc, ...) \
93 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
94 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
95 static int Static##Proc##Init() \
98 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
99 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
102 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
103 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
104 DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple)); \
106 void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
108 #define RUNNER_MULTIPROCESS_TEST_SMACK(Proc, ...) \
109 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
110 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
111 static int Static##Proc##Init() \
114 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
115 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
118 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
119 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) { \
120 DPL::Test::RunMultiProc(std::bind(Proc##Multi, optionalArgsTuple)); \
122 void Proc##Multi(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)