FreeNOS
ExternalTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <errno.h>
22#include <unistd.h>
23#include <libgen.h>
24#include <sys/stat.h>
25#include <sys/wait.h>
26#include "ExternalTest.h"
27
28ExternalTest::ExternalTest(const char *name, int argc, char **argv)
29 : TestInstance(name)
30{
31 m_argc = argc;
32 m_argv = argv;
33}
34
38
40{
41 int status;
42 pid_t pid;
43 char **argv = new char * [m_argc + 2];
44
45 for (int i = 1; i < m_argc; i++)
46 argv[i] = m_argv[i];
47
48 argv[0] = *m_name;
49 argv[m_argc] = (char *) "-n";
50 argv[m_argc+1] = 0;
51
52#ifdef __HOST__
53 if ((pid = fork()) == 0)
54 execv(argv[0], argv);
55#else
56 pid = forkexec(*m_name, (const char **) argv);
57#endif
58 waitpid(pid, &status, 0);
59 delete[] argv;
60
61 return status == 0 ? OK : FAIL;
62}
virtual ~ExternalTest()
Destructor.
char ** m_argv
Program argument values.
virtual TestResult run()
Run the external test.
ExternalTest(const char *name, int argc, char **argv)
Class constructor.
int m_argc
Program argument count.
Represents a test instance.
String m_name
Name of the test instance.
Represents a Test result created by a TestInstance.
Definition TestResult.h:48
C int forkexec(const char *path, const char *argv[])
Create a new process and execute program.
Definition forkexec.cpp:27
ProcessID pid_t
Used for process IDs and process group IDs.
Definition types.h:32
C pid_t waitpid(pid_t pid, int *stat_loc, int options)
Wait for a child process to stop or terminate.
Definition waitpid.cpp:23
#define OK
Definition TestResult.h:36
#define FAIL
Definition TestResult.h:37