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