WvStreams
wvtestmain.cc
00001 #include "wvtest.h"
00002 #include "wvcrash.h"
00003 #include "wvstring.h"
00004 #include <stdlib.h>
00005 #include <stdio.h>
00006 #ifdef _WIN32
00007 #include <io.h>
00008 #include <windows.h>
00009 #else
00010 #include <unistd.h>
00011 #include <fcntl.h>
00012 #endif
00013 
00014 static bool fd_is_valid(int fd)
00015 {
00016 #ifdef _WIN32
00017     if ((HANDLE)_get_osfhandle(fd) != INVALID_HANDLE_VALUE) return true;
00018 #endif    
00019     int nfd = dup(fd);
00020     if (nfd >= 0)
00021     {
00022         close(nfd);
00023         return true;
00024     }
00025     return false;
00026 
00027 }
00028 
00029 
00030 static int fd_count(const char *when)
00031 {
00032     int count = 0;
00033     
00034     printf("fds open at %s:", when);
00035     
00036     for (int fd = 0; fd < 1024; fd++)
00037     {
00038         if (fd_is_valid(fd))
00039         {
00040             count++;
00041             printf(" %d", fd);
00042             fflush(stdout);
00043         }
00044     }
00045     printf("\n");
00046     
00047     return count;
00048 }
00049 
00050 
00051 int main(int argc, char **argv)
00052 {
00053 #ifdef _WIN32
00054     setup_console_crash();
00055 #endif
00056 
00057     // test wvtest itself.  Not very thorough, but you have to draw the
00058     // line somewhere :)
00059     WVPASS(true);
00060     WVPASS(1);
00061     WVFAIL(false);
00062     WVFAIL(0);
00063     int startfd, endfd;
00064     char * const *prefixes = NULL;
00065     
00066     if (argc > 1)
00067         prefixes = argv + 1;
00068     
00069     startfd = fd_count("start");
00070     int ret = WvTest::run_all(prefixes);
00071     
00072     if (ret == 0) // don't pollute the strace output if we failed anyway
00073     {
00074         endfd = fd_count("end");
00075     
00076         WVPASS(startfd == endfd);
00077 #ifndef _WIN32
00078         if (startfd != endfd)
00079             system(WvString("ls -l /proc/%s/fd", getpid()));
00080 #endif    
00081     }
00082     
00083     // keep 'make' from aborting if this environment variable is set
00084     if (getenv("WVTEST_NO_FAIL"))
00085         return 0;
00086     else
00087         return ret;
00088 }