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