989654a71ecd81670c8f963693a43a4e33272807
[platform/core/test/security-tests.git] / src / framework / src / test_runner_multiprocess.cpp
1 /*
2  * Copyright (c) 2014 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_multiprocess.cpp
18  * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of multiprocess test runner
21  */
22
23 #include <sys/file.h>
24 #include <dpl/exception.h>
25 #include <dpl/test/test_runner.h>
26 #include <dpl/test/test_runner_child.h>
27 #include <dpl/test/test_runner_multiprocess.h>
28 #include <poll.h>
29 #include <limits.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32
33 namespace {
34
35 const int MULTI_TEST_ERROR    = -1;
36 const int MULTI_TEST_PASS     = 0;
37 const int MULTI_TEST_FAILED   = 1;
38 const int MULTI_TEST_IGNORED  = 2;
39
40 }
41
42 namespace DPL {
43 namespace Test {
44
45 SimplePipeWrapper::SimplePipeWrapper()
46 : PipeWrapper()
47 {
48
49 }
50
51 SimplePipeWrapper::~SimplePipeWrapper()
52 {
53
54 }
55
56 PipeWrapper::Status SimplePipeWrapper::send(std::string &message)
57 {
58     if (m_pipefd[1] == PIPE_CLOSED) {
59            return ERROR;
60     }
61
62     if (message.size() > PIPE_BUF-1) {
63         return ERROR;
64     }
65
66     char buffer[PIPE_BUF] = { 0 };
67
68
69     for(unsigned int i = 0; i < message.size(); ++i) {
70         buffer[i] = message[i];
71     }
72
73     return writeHelp(buffer, PIPE_BUF);
74 }
75
76 PipeWrapper::Status SimplePipeWrapper::receive(std::string &data, bool &empty, time_t deadline)
77 {
78     if (m_pipefd[0] == PIPE_CLOSED) {
79         return ERROR;
80     }
81
82     empty = false;
83
84     data.resize(PIPE_BUF);
85
86     char buffer[PIPE_BUF] = { 0 };
87
88     int ready = 0;
89     while (ready != PIPE_BUF) {
90         time_t wait = deadline - time(0);
91         wait = wait < 1 ? 1 : wait;
92         pollfd fds = { m_pipefd[0], POLLIN, 0 };
93
94         int pollReturn = poll(&fds, 1, wait * 1000);
95
96         if (pollReturn == 0) {
97             return TIMEOUT; // Timeout
98         }
99
100         if (pollReturn < -1) {
101             return ERROR;
102         }
103         int ret = read(m_pipefd[0], &buffer[ready], PIPE_BUF - ready);
104         if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
105             continue;
106         }
107
108         if (ret == -1) {
109             closeHelp(0);
110             return ERROR;
111         }
112         if (ret == 0) {
113             empty = true;
114             break;
115         }
116
117         ready += ret;
118     }
119
120
121     for(unsigned int i = 0; i < PIPE_BUF; ++i){
122         if(buffer[i] == 0) {
123             data.resize(i);
124             return SUCCESS;
125         }
126         data[i] = buffer[i];
127     }
128
129     return ERROR;
130 }
131
132 void RunMultiProc(TestRunner::TestCase procMulti)
133 {
134     SimplePipeWrapper pipe;
135     int code = MULTI_TEST_PASS;
136     std::string msg = "";
137     int pipeReturn;
138
139     int waitStatus;
140
141     pid_t top_pid = getpid();
142
143     if (!pipe.isReady()) {
144         throw TestRunner::TestFailed("Pipe creation failed");
145     }
146     // pipe
147
148     try {
149         procMulti();
150     } catch (const TestRunner::TestFailed &e) {
151         code = MULTI_TEST_FAILED;
152         msg = e.GetMessage();
153     } catch (const TestRunner::Ignored &e) {
154         code = MULTI_TEST_IGNORED;
155         msg = e.GetMessage();
156     } catch (const std::exception &) {
157         code = MULTI_TEST_FAILED;
158         msg = "std exception";
159     } catch (...) {
160         // Unknown exception failure
161         code = MULTI_TEST_FAILED;
162         msg = "unknown exception";
163     }
164
165     while (true) {
166         pid_t child_pid = wait(&waitStatus);
167         if (child_pid == -1) {
168             if (errno == ECHILD) {
169                 if (top_pid == getpid()) {
170                     std::string recMsg="";
171
172                     pipe.setUsage(PipeWrapper::READONLY);
173
174                     bool empty=false;
175                     while(true) {
176                         pipeReturn = pipe.receive(recMsg, empty, time(0) + 10);
177
178                         if (empty) {
179                             break;
180                         }
181                         if (pipeReturn == PipeWrapper::ERROR) {
182                             pipe.closeAll();
183                             throw TestRunner::TestFailed("Reading pipe error");
184                         } else if (pipeReturn == PipeWrapper::TIMEOUT) {
185                             pipe.closeAll();
186                             throw TestRunner::TestFailed("Timeout error");
187                         }
188                         msg = msg + "\n" + recMsg;
189                     }
190                     pipe.closeAll();
191
192                     switch(code) {
193                     case MULTI_TEST_PASS:
194                         return;
195                     case MULTI_TEST_FAILED:
196                         throw TestRunner::TestFailed(msg);
197                     case MULTI_TEST_IGNORED:
198                         throw TestRunner::Ignored(msg);
199                     default:
200                         throw TestRunner::TestFailed(msg);
201                     }
202                 } else {
203                     pipe.setUsage(PipeWrapper::WRITEONLY);
204
205                     pipeReturn = pipe.send(msg);
206
207                     if (pipeReturn == PipeWrapper::ERROR) {
208                         pipe.closeAll();
209                         code = MULTI_TEST_ERROR;
210                     }
211
212                     exit(code);
213                 }
214             }
215         } else if (WIFEXITED(waitStatus)) {
216             if ((signed char)WEXITSTATUS(waitStatus) == MULTI_TEST_FAILED) {
217                 switch (code) {
218                     case MULTI_TEST_PASS:
219                         code = MULTI_TEST_FAILED;
220                         break;
221                     case MULTI_TEST_FAILED:
222                         break;
223                     case MULTI_TEST_IGNORED:
224                         code = MULTI_TEST_FAILED;
225                         break;
226                     default:
227                         break;
228                     }
229             } else if ((signed char)WEXITSTATUS(waitStatus) == MULTI_TEST_IGNORED) {
230                 switch (code) {
231                 case MULTI_TEST_PASS:
232                     code = MULTI_TEST_IGNORED;
233                     break;
234                 case MULTI_TEST_FAILED:
235                     break;
236                 case MULTI_TEST_IGNORED:
237                     break;
238                 default:
239                     break;
240                 }
241             } else  if ((signed char)WEXITSTATUS(waitStatus) != MULTI_TEST_PASS) {
242                 code = MULTI_TEST_ERROR;
243                 msg = "PROCESS BAD CODE RETURN";
244             }
245         }
246     }
247 }
248 } // namespace Test
249 } // namespace DPL