2 * Copyright (c) 2013 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.
17 * @file test_runner_child.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief This file is the implementation file of test runner
23 #include <dpl/test/test_runner.h>
24 #include <dpl/test/test_runner_child.h>
25 #include <dpl/test/test_results_collector.h>
26 #include <dpl/binary_queue.h>
27 #include <dpl/exception.h>
28 #include <dpl/scoped_free.h>
29 #include <dpl/foreach.h>
30 #include <dpl/colors.h>
42 #include <sys/types.h>
45 #include <dpl/utils/wrt_global_settings.h>
48 const int PIPE_CLOSED = -1;
50 const int CHILD_TEST_FAIL = 0;
51 const int CHILD_TEST_PASS = 1;
52 const int CHILD_TEST_IGNORED = 2;
57 if (-1 == (devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY))))
60 // replace stdout with /dev/null
61 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO)))
64 // replace stderr with /dev/null
65 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO)))
75 } // namespace anonymous
79 class PipeWrapper : DPL::Noncopyable
95 if (-1 == pipe(m_pipefd)) {
96 m_pipefd[0] = PIPE_CLOSED;
97 m_pipefd[1] = PIPE_CLOSED;
103 return m_pipefd[0] != PIPE_CLOSED || m_pipefd[1] != PIPE_CLOSED;
106 void setUsage(Usage usage)
108 if (usage == READONLY) {
111 if (usage == WRITEONLY) {
121 Status send(int code, std::string &message)
123 if (m_pipefd[1] == PIPE_CLOSED) {
127 std::ostringstream output;
128 output << toBinaryString(code);
129 output << toBinaryString(static_cast<int>(message.size()));
132 std::string binary = output.str();
133 int size = binary.size();
135 if ((writeHelp(&size,
136 sizeof(int)) == ERROR) ||
137 (writeHelp(binary.c_str(), size) == ERROR))
144 Status receive(int &code, std::string &data, time_t deadline)
146 if (m_pipefd[0] == PIPE_CLOSED) {
153 if ((ret = readHelp(&size, sizeof(int), deadline)) != SUCCESS) {
157 std::vector<char> buffer;
160 if ((ret = readHelp(&buffer[0], size, deadline)) != SUCCESS) {
165 DPL::BinaryQueue queue;
166 queue.AppendCopy(&buffer[0], size);
168 queue.FlattenConsume(&code, sizeof(int));
169 queue.FlattenConsume(&size, sizeof(int));
173 queue.FlattenConsume(&buffer[0], size);
174 data.assign(buffer.begin(), buffer.end());
175 } catch (DPL::BinaryQueue::Exception::Base &e) {
188 std::string toBinaryString(int data)
190 char buffer[sizeof(int)];
191 memcpy(buffer, &data, sizeof(int));
192 return std::string(buffer, buffer + sizeof(int));
195 void closeHelp(int desc)
197 if (m_pipefd[desc] != PIPE_CLOSED) {
198 TEMP_FAILURE_RETRY(close(m_pipefd[desc]));
199 m_pipefd[desc] = PIPE_CLOSED;
203 Status writeHelp(const void *buffer, int size)
206 const char *p = static_cast<const char *>(buffer);
207 while (ready != size) {
208 int ret = write(m_pipefd[1], &p[ready], size - ready);
210 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
224 Status readHelp(void *buf, int size, time_t deadline)
227 char *buffer = static_cast<char*>(buf);
228 while (ready != size) {
229 time_t wait = deadline - time(0);
230 wait = wait < 1 ? 1 : wait;
231 pollfd fds = { m_pipefd[0], POLLIN, 0 };
233 int pollReturn = poll(&fds, 1, wait * 1000);
235 if (pollReturn == 0) {
236 return TIMEOUT; // Timeout
239 if (pollReturn < -1) {
243 int ret = read(m_pipefd[0], &buffer[ready], size - ready);
245 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
249 if (ret == -1 || ret == 0) {
262 void RunChildProc(TestRunner::TestCase procChild)
265 if (!pipe.isReady()) {
266 throw TestRunner::TestFailed("Pipe creation failed");
272 throw TestRunner::TestFailed("Child creation failed");
277 pipe.setUsage(PipeWrapper::READONLY);
282 int pipeReturn = pipe.receive(code, message, time(0) + 10);
284 if (pipeReturn != PipeWrapper::SUCCESS) { // Timeout or reading error
290 waitpid(pid, &status, 0);
292 if (pipeReturn == PipeWrapper::TIMEOUT) {
293 throw TestRunner::TestFailed("Timeout");
296 if (pipeReturn == PipeWrapper::ERROR) {
297 throw TestRunner::TestFailed("Reading pipe error");
300 if (code == CHILD_TEST_FAIL) {
301 throw TestRunner::TestFailed(message);
302 } else if (code == CHILD_TEST_IGNORED) {
303 throw TestRunner::Ignored(message);
308 // End Runner after current test
309 TestRunnerSingleton::Instance().Terminate();
311 int code = CHILD_TEST_PASS;
314 bool allowLogs = TestRunnerSingleton::Instance().GetAllowChildLogs();
318 closeOutput(); // if fails nothing we can do
321 pipe.setUsage(PipeWrapper::WRITEONLY);
325 } catch (const DPL::Test::TestRunner::TestFailed &e) {
326 msg = e.GetMessage();
327 code = CHILD_TEST_FAIL;
328 } catch (const DPL::Test::TestRunner::Ignored &e) {
329 msg = e.GetMessage();
330 code = CHILD_TEST_IGNORED;
331 } catch (...) { // catch all exception generated by "user" code
332 msg = "unhandled exeception";
333 code = CHILD_TEST_FAIL;
340 pipe.send(code, msg);