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 
28 ExternalTest::ExternalTest(const char *name, int argc, char **argv)
29  : TestInstance(name)
30 {
31  m_argc = argc;
32  m_argv = argv;
33 }
34 
36 {
37 }
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 }
ExternalTest::m_argc
int m_argc
Program argument count.
Definition: ExternalTest.h:62
string.h
libgen.h
ExternalTest::m_argv
char ** m_argv
Program argument values.
Definition: ExternalTest.h:65
wait.h
stdio.h
ExternalTest::ExternalTest
ExternalTest(const char *name, int argc, char **argv)
Class constructor.
Definition: ExternalTest.cpp:28
forkexec
int forkexec(const char *path, const char *argv[])
Create a new process and execute program.
Definition: forkexec.cpp:27
TestResult
Represents a Test result created by a TestInstance.
Definition: TestResult.h:47
pid_t
ProcessID pid_t
Used for process IDs and process group IDs.
Definition: types.h:32
OK
#define OK
Definition: TestResult.h:36
stat.h
ExternalTest::~ExternalTest
virtual ~ExternalTest()
Destructor.
Definition: ExternalTest.cpp:35
unistd.h
TestInstance::m_name
String m_name
Name of the test instance.
Definition: TestInstance.h:68
FAIL
#define FAIL
Definition: TestResult.h:37
stdlib.h
waitpid
pid_t waitpid(pid_t pid, int *stat_loc, int options)
Wait for a child process to stop or terminate.
Definition: waitpid.cpp:23
ExternalTest.h
errno.h
ExternalTest::run
virtual TestResult run()
Run the external test.
Definition: ExternalTest.cpp:39
TestInstance
Represents a test instance.
Definition: TestInstance.h:35