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 CHILD_TEST_FAIL = 0;
49 const int CHILD_TEST_PASS = 1;
50 const int CHILD_TEST_IGNORED = 2;
55 if (-1 == (devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY))))
58 // replace stdout with /dev/null
59 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO)))
62 // replace stderr with /dev/null
63 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO)))
73 } // namespace anonymous
78 PipeWrapper::PipeWrapper()
80 if (-1 == pipe(m_pipefd)) {
81 m_pipefd[0] = PIPE_CLOSED;
82 m_pipefd[1] = PIPE_CLOSED;
86 PipeWrapper::~PipeWrapper()
92 bool PipeWrapper::isReady()
94 return m_pipefd[0] != PIPE_CLOSED || m_pipefd[1] != PIPE_CLOSED;
97 void PipeWrapper::setUsage(Usage usage)
99 if (usage == READONLY) {
102 if (usage == WRITEONLY) {
107 PipeWrapper::Status PipeWrapper::send(int code, std::string &message)
109 if (m_pipefd[1] == PIPE_CLOSED) {
113 std::ostringstream output;
114 output << toBinaryString(code);
115 output << toBinaryString(static_cast<int>(message.size()));
118 std::string binary = output.str();
119 int size = binary.size();
121 if ((writeHelp(&size,
122 sizeof(int)) == ERROR) ||
123 (writeHelp(binary.c_str(), size) == ERROR))
130 PipeWrapper::Status PipeWrapper::receive(int &code, std::string &data, time_t deadline)
132 if (m_pipefd[0] == PIPE_CLOSED) {
139 if ((ret = readHelp(&size, sizeof(int), deadline)) != SUCCESS) {
143 std::vector<char> buffer;
146 if ((ret = readHelp(&buffer[0], size, deadline)) != SUCCESS) {
151 DPL::BinaryQueue queue;
152 queue.AppendCopy(&buffer[0], size);
154 queue.FlattenConsume(&code, sizeof(int));
155 queue.FlattenConsume(&size, sizeof(int));
159 queue.FlattenConsume(&buffer[0], size);
160 data.assign(buffer.begin(), buffer.end());
161 } catch (DPL::BinaryQueue::Exception::Base &e) {
167 void PipeWrapper::closeAll()
173 std::string PipeWrapper::toBinaryString(int data)
175 char buffer[sizeof(int)];
176 memcpy(buffer, &data, sizeof(int));
177 return std::string(buffer, buffer + sizeof(int));
180 void PipeWrapper::closeHelp(int desc)
182 if (m_pipefd[desc] != PIPE_CLOSED) {
183 TEMP_FAILURE_RETRY(close(m_pipefd[desc]));
184 m_pipefd[desc] = PIPE_CLOSED;
188 PipeWrapper::Status PipeWrapper::writeHelp(const void *buffer, int size)
191 const char *p = static_cast<const char *>(buffer);
192 while (ready != size) {
193 int ret = write(m_pipefd[1], &p[ready], size - ready);
195 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
209 PipeWrapper::Status PipeWrapper::readHelp(void *buf, int size, time_t deadline)
212 char *buffer = static_cast<char*>(buf);
213 while (ready != size) {
214 time_t wait = deadline - time(0);
215 wait = wait < 1 ? 1 : wait;
216 pollfd fds = { m_pipefd[0], POLLIN, 0 };
218 int pollReturn = poll(&fds, 1, wait * 1000);
220 if (pollReturn == 0) {
221 return TIMEOUT; // Timeout
224 if (pollReturn < -1) {
228 int ret = read(m_pipefd[0], &buffer[ready], size - ready);
230 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
234 if (ret == -1 || ret == 0) {
244 void RunChildProc(TestRunner::TestCase procChild)
247 if (!pipe.isReady()) {
248 throw TestRunner::TestFailed("Pipe creation failed");
254 throw TestRunner::TestFailed("Child creation failed");
259 pipe.setUsage(PipeWrapper::READONLY);
264 int pipeReturn = pipe.receive(code, message, time(0) + 10);
266 if (pipeReturn != PipeWrapper::SUCCESS) { // Timeout or reading error
272 waitpid(pid, &status, 0);
274 if (pipeReturn == PipeWrapper::TIMEOUT) {
275 throw TestRunner::TestFailed("Timeout");
278 if (pipeReturn == PipeWrapper::ERROR) {
279 throw TestRunner::TestFailed("Reading pipe error");
282 if (code == CHILD_TEST_FAIL) {
283 throw TestRunner::TestFailed(message);
284 } else if (code == CHILD_TEST_IGNORED) {
285 throw TestRunner::Ignored(message);
290 // End Runner after current test
291 TestRunnerSingleton::Instance().Terminate();
293 int code = CHILD_TEST_PASS;
296 bool allowLogs = TestRunnerSingleton::Instance().GetAllowChildLogs();
300 closeOutput(); // if fails nothing we can do
303 pipe.setUsage(PipeWrapper::WRITEONLY);
307 } catch (const DPL::Test::TestRunner::TestFailed &e) {
308 msg = e.GetMessage();
309 code = CHILD_TEST_FAIL;
310 } catch (const DPL::Test::TestRunner::Ignored &e) {
311 msg = e.GetMessage();
312 code = CHILD_TEST_IGNORED;
313 } catch (...) { // catch all exception generated by "user" code
314 msg = "unhandled exeception";
315 code = CHILD_TEST_FAIL;
322 pipe.send(code, msg);