Add init and finish functionality
[platform/core/test/security-tests.git] / src / framework / include / dpl / test / test_runner_child.h
1 /*
2  * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        test_runner_child.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
20  * @version     1.0
21  * @brief       This file is the header file of test runner
22  */
23 #ifndef DPL_TEST_RUNNER_CHILD_H
24 #define DPL_TEST_RUNNER_CHILD_H
25
26 #include <functional>
27 #include <memory>
28 #include <tuple>
29
30 #include <dpl/availability.h>
31 #include <dpl/test/test_case_extended.h>
32 #include <dpl/test/test_runner.h>
33
34 namespace DPL {
35 namespace Test {
36
37 class PipeWrapper : DPL::Noncopyable
38 {
39   public:
40     enum Usage {
41         READONLY,
42         WRITEONLY
43     };
44
45     enum Status {
46         SUCCESS,
47         TIMEOUT,
48         ERROR
49     };
50
51     PipeWrapper();
52
53     bool isReady();
54
55     void setUsage(Usage usage);
56
57     virtual ~PipeWrapper();
58
59     Status send(int code, std::string &message);
60     Status sendPerformance(const ConstPerformanceResultPtr &performance);
61
62     Status receive(int &code,
63                    std::string &data,
64                    PerformanceResultPtr &performance,
65                    time_t deadline);
66
67     void closeAll();
68
69   protected:
70
71     std::string toBinaryString(int data);
72
73     void closeHelp(int desc);
74
75     Status writeHelp(const void *buffer, int size);
76
77     Status readHelp(void *buf, int size, time_t deadline);
78
79     static const int PIPE_CLOSED = -1;
80
81     int m_pipefd[2];
82 };
83
84 void RunChildProc(const std::function<void(void)> &testFunc);
85 } // namespace Test
86 } // namespace DPL
87
88 #define RUNNER_CHILD_TEST(Proc, ...)                                                    \
89     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple);                              \
90     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple);                       \
91     static int Static##Proc##Init()                                                     \
92     {                                                                                   \
93         DPL::Test::TestRunnerSingleton::Instance().RegisterTest(                        \
94             new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc));                \
95         return 0;                                                                       \
96     }                                                                                   \
97     const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init();                  \
98     void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple) {                             \
99         DPL::Test::RunChildProc(std::bind(Proc##Child, optionalArgsTuple));             \
100     }                                                                                   \
101     void Proc##Child(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
102
103 #endif // DPL_TEST_RUNNER_CHILD_H