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>
47 const int CHILD_TEST_FAIL = 0;
48 const int CHILD_TEST_PASS = 1;
49 const int CHILD_TEST_IGNORED = 2;
54 if (-1 == (devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY))))
57 // replace stdout with /dev/null
58 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO)))
61 // replace stderr with /dev/null
62 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO)))
72 } // namespace anonymous
77 PipeWrapper::PipeWrapper()
79 if (-1 == pipe(m_pipefd)) {
80 m_pipefd[0] = PIPE_CLOSED;
81 m_pipefd[1] = PIPE_CLOSED;
85 PipeWrapper::~PipeWrapper()
91 bool PipeWrapper::isReady()
93 return m_pipefd[0] != PIPE_CLOSED || m_pipefd[1] != PIPE_CLOSED;
96 void PipeWrapper::setUsage(Usage usage)
98 if (usage == READONLY) {
101 if (usage == WRITEONLY) {
106 PipeWrapper::Status PipeWrapper::send(int code, std::string &message)
108 if (m_pipefd[1] == PIPE_CLOSED) {
112 std::ostringstream output;
113 output << toBinaryString(code);
114 output << toBinaryString(static_cast<int>(message.size()));
117 std::string binary = output.str();
118 int size = binary.size();
120 if ((writeHelp(&size,
121 sizeof(int)) == ERROR) ||
122 (writeHelp(binary.c_str(), size) == ERROR))
129 PipeWrapper::Status PipeWrapper::receive(int &code, std::string &data, time_t deadline)
131 if (m_pipefd[0] == PIPE_CLOSED) {
138 if ((ret = readHelp(&size, sizeof(int), deadline)) != SUCCESS) {
142 std::vector<char> buffer;
145 if ((ret = readHelp(&buffer[0], size, deadline)) != SUCCESS) {
150 VcoreDPL::BinaryQueue queue;
151 queue.AppendCopy(&buffer[0], size);
153 queue.FlattenConsume(&code, sizeof(int));
154 queue.FlattenConsume(&size, sizeof(int));
158 queue.FlattenConsume(&buffer[0], size);
159 data.assign(buffer.begin(), buffer.end());
160 } catch (VcoreDPL::BinaryQueue::Exception::Base &e) {
166 void PipeWrapper::closeAll()
172 std::string PipeWrapper::toBinaryString(int data)
174 char buffer[sizeof(int)];
175 memcpy(buffer, &data, sizeof(int));
176 return std::string(buffer, buffer + sizeof(int));
179 void PipeWrapper::closeHelp(int desc)
181 if (m_pipefd[desc] != PIPE_CLOSED) {
182 TEMP_FAILURE_RETRY(close(m_pipefd[desc]));
183 m_pipefd[desc] = PIPE_CLOSED;
187 PipeWrapper::Status PipeWrapper::writeHelp(const void *buffer, int size)
190 const char *p = static_cast<const char *>(buffer);
191 while (ready != size) {
192 int ret = write(m_pipefd[1], &p[ready], size - ready);
194 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
208 PipeWrapper::Status PipeWrapper::readHelp(void *buf, int size, time_t deadline)
211 char *buffer = static_cast<char*>(buf);
212 while (ready != size) {
213 time_t wait = deadline - time(0);
214 wait = wait < 1 ? 1 : wait;
215 pollfd fds = { m_pipefd[0], POLLIN, 0 };
217 int pollReturn = poll(&fds, 1, wait * 1000);
219 if (pollReturn == 0) {
220 return TIMEOUT; // Timeout
223 if (pollReturn < -1) {
227 int ret = read(m_pipefd[0], &buffer[ready], size - ready);
229 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
233 if (ret == -1 || ret == 0) {
243 void RunChildProc(TestRunner::TestCase procChild)
246 if (!pipe.isReady()) {
247 throw TestRunner::TestFailed("Pipe creation failed");
253 throw TestRunner::TestFailed("Child creation failed");
258 pipe.setUsage(PipeWrapper::READONLY);
263 int pipeReturn = pipe.receive(code, message, time(0) + 10);
265 if (pipeReturn != PipeWrapper::SUCCESS) { // Timeout or reading error
271 waitpid(pid, &status, 0);
273 if (pipeReturn == PipeWrapper::TIMEOUT) {
274 throw TestRunner::TestFailed("Timeout");
277 if (pipeReturn == PipeWrapper::ERROR) {
278 throw TestRunner::TestFailed("Reading pipe error");
281 if (code == CHILD_TEST_FAIL) {
282 throw TestRunner::TestFailed(message);
283 } else if (code == CHILD_TEST_IGNORED) {
284 throw TestRunner::Ignored(message);
289 // End Runner after current test
290 TestRunnerSingleton::Instance().Terminate();
292 int code = CHILD_TEST_PASS;
295 bool allowLogs = TestRunnerSingleton::Instance().GetAllowChildLogs();
299 closeOutput(); // if fails nothing we can do
302 pipe.setUsage(PipeWrapper::WRITEONLY);
306 } catch (const VcoreDPL::Test::TestRunner::TestFailed &e) {
307 msg = e.GetMessage();
308 code = CHILD_TEST_FAIL;
309 } catch (const VcoreDPL::Test::TestRunner::Ignored &e) {
310 msg = e.GetMessage();
311 code = CHILD_TEST_IGNORED;
312 } catch (...) { // catch all exception generated by "user" code
313 msg = "unhandled exeception";
314 code = CHILD_TEST_FAIL;
321 pipe.send(code, msg);
325 } // namespace VcoreDPL