/usr/include/wibble/test-main.h is in libwibble-dev 1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | // -*- C++ -*-
#include <wibble/sys/macros.h>
#include <unistd.h>
#ifdef POSIX
#include <sys/wait.h>
#include <sys/socket.h>
#endif
#include <cstring>
#include <cstdio>
#include <wibble/sys/pipe.h>
struct Main : RunFeedback {
int suite, test;
wibble::sys::Pipe p_status;
wibble::sys::Pipe p_confirm;
int status_fds[2];
int confirm_fds[2];
pid_t pid;
int argc;
char **argv;
pid_t finished;
int status_code;
int test_ok;
int suite_ok, suite_failed;
int total_ok, total_failed;
int announced_suite;
std::string current;
bool want_fork;
RunAll all;
Main() : suite(0), test(0) {
suite_ok = suite_failed = 0;
total_ok = total_failed = 0;
test_ok = 0;
announced_suite = -1;
}
void child() {
#ifdef POSIX
close( status_fds[0] );
close( confirm_fds[1] );
#endif
p_confirm = wibble::sys::Pipe( confirm_fds[0] );
if ( argc > 1 ) {
RunSuite *s = all.findSuite( argv[1] );
if (!s) {
std::cerr << "No such suite " << argv[1] << std::endl;
// todo dump possible suites?
exit(250);
}
if ( argc > 2 ) {
if ( !test ) {
char *end;
int t = strtol( argv[2], &end, 0 );
if ( end == argv[2] && t == 0 ) {
t = s->findTest( argv[2] );
if ( t < 0 ) {
std::cerr << "No such test " << argv[2]
<< " in suite " << argv[1] << std::endl;
// todo dump possible suites?
exit(250);
}
}
all.runTest( *s, t );
}
} else
all.runSuite( *s, test, 0, 1 );
}
if ( argc == 1 ) {
all.runFrom( suite, test );
}
status( "done" );
exit( 0 );
}
#ifdef POSIX
void testDied()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
/* std::cerr << "test died: " << test << "/"
<< suites[suite].testCount << std::endl; */
if ( WIFEXITED( status_code ) ) {
if ( WEXITSTATUS( status_code ) == 250 )
exit( 3 );
if ( WEXITSTATUS( status_code ) == 0 )
return;
}
std::cout << "--> FAILED: "<< current;
if ( WIFEXITED( status_code ) )
std::cout << " (exit status " << WEXITSTATUS( status_code ) << ")";
if ( WIFSIGNALED( status_code ) )
std::cout << " (caught signal " << WTERMSIG( status_code ) << ")";
std::cout << std::endl;
// re-announce the suite
announced_suite --;
++ test; // continue with next test
test_ok = 0;
suite_failed ++;
#pragma GCC diagnostic pop
}
#endif
void processStatus( std::string line ) {
// std::cerr << line << std::endl;
if ( line == "done" ) { // finished
#ifdef POSIX
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
if ( want_fork ) {
finished = waitpid( pid, &status_code, 0 );
assert_eq( pid, finished );
assert( WIFEXITED( status_code ) );
assert_eq( WEXITSTATUS( status_code ), 0 );
}
#pragma GCC diagnostic pop
#endif
std::cout << "overall " << total_ok << "/"
<< total_ok + total_failed
<< " ok" << std::endl;
exit( total_failed == 0 ? 0 : 1 );
}
if ( test_ok ) {
/* std::cerr << "test ok: " << test << "/"
<< suites[suite].testCount << std::endl; */
std::cout << "." << std::flush;
suite_ok ++;
++ test;
test_ok = 0;
}
if ( line[0] == 's' ) {
if ( line[2] == 'd' ) {
std::cout << " " << suite_ok << "/" << suite_ok + suite_failed
<< " ok" << std::endl;
++ suite; test = 0;
assert( !test_ok );
total_ok += suite_ok;
total_failed += suite_failed;
suite_ok = suite_failed = 0;
}
if ( line[2] == 's' ) {
if ( announced_suite < suite ) {
std::cout << std::string( line.begin() + 5, line.end() )
<< ": " << std::flush;
announced_suite = suite;
}
}
}
if ( line[0] == 't' ) {
if ( line[2] == 'd' ) {
confirm();
test_ok = 1;
}
if ( line[2] == 's' ) {
confirm();
current = std::string( line.begin() + 5, line.end() );
}
}
}
#ifdef POSIX
void parent() {
close( status_fds[1] );
close( confirm_fds[0] );
p_status = wibble::sys::Pipe( status_fds[ 0 ]);
std::string line;
while ( true ) {
if ( p_status.eof() ) {
finished = waitpid( pid, &status_code, 0 );
if ( finished < 0 ) {
perror( "waitpid failed" );
exit( 5 );
}
assert_eq( pid, finished );
testDied();
return;
}
line = p_status.nextLineBlocking();
processStatus( line );
}
}
#endif
void status( std::string line ) {
// std::cerr << "status: " << line << std::endl;
#ifdef POSIX
if ( want_fork ) {
line += "\n";
::write( status_fds[ 1 ], line.c_str(), line.length() );
} else
#endif
processStatus( line );
}
void confirm() {
std::string line( "ack\n" );
if ( want_fork )
::write( confirm_fds[ 1 ], line.c_str(), line.length() );
}
void waitForAck() {
if ( want_fork ) {
std::string line = p_confirm.nextLineBlocking();
assert_eq( std::string( "ack" ), line );
}
}
int main( int _argc, char **_argv )
{
argc = _argc;
argv = _argv;
all.suiteCount = sizeof(suites)/sizeof(RunSuite);
all.suites = suites;
all.feedback = this;
#ifdef POSIX
want_fork = argc <= 2;
#else
want_fork = false;
#endif
while (true) {
#ifdef POSIX
if ( socketpair( PF_UNIX,SOCK_STREAM, 0, status_fds ) )
return 1;
if ( socketpair( PF_UNIX,SOCK_STREAM, 0, confirm_fds ) )
return 1;
if ( want_fork ) {
pid = fork();
if ( pid < 0 )
return 2;
if ( pid == 0 ) { // child
child();
} else {
parent();
}
} else
#endif
child();
}
return 0;
}
};
int main( int argc, char **argv ) {
return Main().main( argc, argv );
}
|