]> rtime.felk.cvut.cz Git - wvtest.git/blob - cpp/wvtestmain.cc
c67528975091325ba1ac1028a8375741a24f0960
[wvtest.git] / cpp / wvtestmain.cc
1 #include "wvtest.h"
2 #ifdef HAVE_WVCRASH
3 # include "wvcrash.h"
4 #endif
5 #include <stdlib.h>
6 #include <stdio.h>
7 #ifdef _WIN32
8 #include <io.h>
9 #include <windows.h>
10 #else
11 #include <unistd.h>
12 #include <fcntl.h>
13 #endif
14
15 static bool fd_is_valid(int fd)
16 {
17 #ifdef _WIN32
18     if ((HANDLE)_get_osfhandle(fd) != INVALID_HANDLE_VALUE) return true;
19 #endif    
20     int nfd = dup(fd);
21     if (nfd >= 0)
22     {
23         close(nfd);
24         return true;
25     }
26     return false;
27
28 }
29
30
31 static int fd_count(const char *when)
32 {
33     int count = 0;
34     
35     printf("fds open at %s:", when);
36     
37     for (int fd = 0; fd < 1024; fd++)
38     {
39         if (fd_is_valid(fd))
40         {
41             count++;
42             printf(" %d", fd);
43             fflush(stdout);
44         }
45     }
46     printf("\n");
47     
48     return count;
49 }
50
51
52 int main(int argc, char **argv)
53 {
54     char buf[200];
55 #if defined(_WIN32) && defined(HAVE_WVCRASH)
56     setup_console_crash();
57 #endif
58
59     // test wvtest itself.  Not very thorough, but you have to draw the
60     // line somewhere :)
61     WVPASS(true);
62     WVPASS(1);
63     WVFAIL(false);
64     WVFAIL(0);
65     int startfd, endfd;
66     char * const *prefixes = NULL;
67     
68     if (argc > 1)
69         prefixes = argv + 1;
70     
71     startfd = fd_count("start");
72     int ret = WvTest::run_all(prefixes);
73     
74     if (ret == 0) // don't pollute the strace output if we failed anyway
75     {
76         endfd = fd_count("end");
77     
78         WVPASS(startfd == endfd);
79 #ifndef _WIN32
80         if (startfd != endfd)
81         {
82             sprintf(buf, "ls -l /proc/%d/fd", getpid());
83             system(buf);
84         }
85 #endif    
86     }
87     
88     // keep 'make' from aborting if this environment variable is set
89     if (getenv("WVTEST_NO_FAIL"))
90         return 0;
91     else
92         return ret;
93 }