source: box/trunk/infrastructure/buildenv-testmain-template.cpp @ 3077

Revision 3077, 7.0 KB checked in by chris, 8 days ago (diff)

Set console log level as well as global level in tests.

  • Property svn:eol-style set to native
Line 
1//
2//      AUTOMATICALLY GENERATED FILE
3//              do not edit
4//
5//      Note that infrastructure/buildenv-testmain-template.cpp is NOT
6//      auto-generated, but test/*/_main.cpp are generated from it.
7//
8
9
10// --------------------------------------------------------------------------
11//
12// File
13//              Name:    testmain.template.h
14//              Purpose: Template file for running tests
15//              Created: 2003/07/08
16//
17// --------------------------------------------------------------------------
18
19#include "Box.h"
20
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <unistd.h>
27
28#ifdef HAVE_GETOPT_H
29        #include <getopt.h>
30#endif
31
32#include <sys/stat.h>
33#include <sys/types.h>
34
35#include <exception>
36#include <string>
37
38#include "Logging.h"
39#include "Test.h"
40#include "Timer.h"
41
42#include "MemLeakFindOn.h"
43
44int test(int argc, const char *argv[]);
45
46#ifdef BOX_RELEASE_BUILD
47        #define MODE_TEXT       "release"
48#else
49        #define MODE_TEXT       "debug"
50#endif
51
52int failures = 0;
53int first_fail_line;
54std::string first_fail_file;
55
56#ifdef WIN32
57        #define QUIET_PROCESS "-Q"
58#else
59        #define QUIET_PROCESS ""
60#endif
61
62std::string bbackupd_args = QUIET_PROCESS,
63        bbstored_args = QUIET_PROCESS,
64        bbackupquery_args,
65        test_args;
66
67int filedes_open_at_beginning = -1;
68
69#ifdef WIN32
70
71// any way to check for open file descriptors on Win32?
72inline bool check_filedes(bool x) { return 0;     }
73inline bool checkfilesleftopen()  { return false; }
74
75#else // !WIN32
76
77#define FILEDES_MAX 256
78
79bool filedes_open[FILEDES_MAX];
80
81bool check_filedes(bool report)
82{
83        bool allOk = true;
84
85        // See how many file descriptors there are with values < 256
86        for(int d = 0; d < FILEDES_MAX; ++d)
87        {
88                if(::fcntl(d, F_GETFD) != -1)
89                {
90                        // File descriptor obviously exists
91                        if (report && !filedes_open[d])
92                        {
93                                struct stat st;
94                                if (fstat(d, &st) == 0)
95                                {
96                                        int m = st.st_mode;
97                                        #define flag(x) ((m & x) ? #x " " : "")
98                                        BOX_FATAL("File descriptor " << d << 
99                                                " left open (type == " <<
100                                                flag(S_IFIFO) <<
101                                                flag(S_IFCHR) <<
102                                                flag(S_IFDIR) <<
103                                                flag(S_IFBLK) <<
104                                                flag(S_IFREG) <<
105                                                flag(S_IFLNK) <<
106                                                flag(S_IFSOCK) <<
107                                                " or " << m << ")");
108                                }
109                                else
110                                {
111                                        BOX_FATAL("File descriptor " << d << 
112                                                " left open (and stat failed)");
113                                }
114       
115                                allOk = false;
116                               
117                        }
118                        else if (!report)
119                        {
120                                filedes_open[d] = true;
121                        }
122                }
123                else 
124                {
125                        if (report && filedes_open[d])
126                        {
127                                BOX_FATAL("File descriptor " << d << 
128                                        " was open, now closed");
129                                allOk = false;
130                        }
131                        else
132                        {
133                                filedes_open[d] = false;
134                        }
135                }
136        }
137
138        if (!report && allOk)
139        {
140                filedes_open_at_beginning = 0;
141        }
142       
143        return !allOk;
144}
145
146bool checkfilesleftopen()
147{
148        if(filedes_open_at_beginning == -1)
149        {
150                // Not used correctly, pretend that there were things
151                // left open so this gets investigated
152                BOX_FATAL("File descriptor test was not initialised");
153                return true;
154        }
155
156        // Count the file descriptors open
157        return check_filedes(true);
158}
159
160#endif
161
162int main(int argc, char * const * argv)
163{
164        // Start memory leak testing
165        MEMLEAKFINDER_START
166
167        Logging::SetProgramName(BOX_MODULE);
168
169#ifdef HAVE_GETOPT_H
170        #ifdef BOX_RELEASE_BUILD
171        int logLevel = Log::NOTICE; // need an int to do math with
172        #else
173        int logLevel = Log::INFO; // need an int to do math with
174        #endif
175
176        struct option longopts[] = 
177        {
178                { "bbackupd-args",      required_argument, NULL, 'c' },
179                { "bbstored-args",      required_argument, NULL, 's' },
180                { "test-daemon-args",   required_argument, NULL, 'd' },
181                { NULL,                 0,                 NULL,  0  }
182        };
183       
184        int ch;
185       
186        while ((ch = getopt_long(argc, argv, "c:d:qs:t:vPTUV", longopts, NULL))
187                != -1)
188        {
189                switch(ch)
190                {
191                        case 'c':
192                        {
193                                if (bbackupd_args.length() > 0)
194                                {
195                                        bbackupd_args += " ";
196                                }
197                                bbackupd_args += optarg;
198                        }
199                        break;
200
201                        case 'd':
202                        {
203                                if (test_args.length() > 0)
204                                {
205                                        test_args += " ";
206                                }
207                                test_args += optarg;
208                        }
209                        break;
210
211                        case 's':
212                        {
213                                bbstored_args += " ";
214                                bbstored_args += optarg;
215                        }
216                        break;
217
218                        #ifndef WIN32
219                        case 'P':
220                        {
221                                Console::SetShowPID(true);
222                        }
223                        break;
224                        #endif
225
226                        case 'q':
227                        {
228                                if(logLevel == Log::NOTHING)
229                                {
230                                        BOX_FATAL("Too many '-q': "
231                                                "Cannot reduce logging "
232                                                "level any more");
233                                        return 2;
234                                }
235                                logLevel--;
236                        }
237                        break;
238
239                        case 'v':
240                        {
241                                if(logLevel == Log::EVERYTHING)
242                                {
243                                        BOX_FATAL("Too many '-v': "
244                                                "Cannot increase logging "
245                                                "level any more");
246                                        return 2;
247                                }
248                                logLevel++;
249                        }
250                        break;
251
252                        case 'V':
253                        {
254                                logLevel = Log::EVERYTHING;
255                        }
256                        break;
257
258                        case 't':
259                        {
260                                Logging::SetProgramName(optarg);
261                                Console::SetShowTag(true);
262                        }
263                        break;
264
265                        case 'T':
266                        {
267                                Console::SetShowTime(true);
268                        }
269                        break;
270
271                        case 'U':
272                        {
273                                Console::SetShowTime(true);
274                                Console::SetShowTimeMicros(true);
275                        }
276                        break;
277
278                        case '?':
279                        {
280                                fprintf(stderr, "Unknown option: '%c'\n",
281                                        optopt);
282                                exit(2);
283                        }
284
285                        default:
286                        {
287                                fprintf(stderr, "Unknown option code '%c'\n",
288                                        ch);
289                                exit(2);
290                        }
291                }
292        }
293
294        Logging::SetGlobalLevel((Log::Level)logLevel);
295        Logging::FilterConsole((Log::Level)logLevel);
296
297        argc -= optind - 1;
298        argv += optind - 1;
299#endif // HAVE_GETOPT_H
300
301        // If there is more than one argument, then the test is doing something advanced, so leave it alone
302        bool fulltestmode = (argc == 1);
303
304        if(fulltestmode)
305        {
306                // banner
307                BOX_NOTICE("Running test TEST_NAME in " MODE_TEXT " mode...");
308
309                // Count open file descriptors for a very crude "files left open" test
310                check_filedes(false);
311
312                #ifdef WIN32
313                        // Under win32 we must initialise the Winsock library
314                        // before using sockets
315
316                        WSADATA info;
317                        TEST_THAT(WSAStartup(0x0101, &info) != SOCKET_ERROR)
318                #endif
319        }
320
321        try
322        {
323                #ifdef BOX_MEMORY_LEAK_TESTING
324                memleakfinder_init();
325                #endif
326
327                Timers::Init();
328                int returncode = test(argc, (const char **)argv);
329                Timers::Cleanup();
330
331                fflush(stdout);
332                fflush(stderr);
333               
334                // check for memory leaks, if enabled
335                #ifdef BOX_MEMORY_LEAK_TESTING
336                        if(memleakfinder_numleaks() != 0)
337                        {
338                                failures++;
339                                printf("FAILURE: Memory leaks detected in test code\n");
340                                printf("==== MEMORY LEAKS =================================\n");
341                                memleakfinder_reportleaks();
342                                printf("===================================================\n");
343                        }
344                #endif
345               
346                if(fulltestmode)
347                {
348                        bool filesleftopen = checkfilesleftopen();
349
350                        fflush(stdout);
351                        fflush(stderr);
352               
353                        if(filesleftopen)
354                        {
355                                failures++;
356                                printf("IMPLICIT TEST FAILED: Something left files open\n");
357                        }
358                        if(failures > 0)
359                        {
360                                printf("FAILED: %d tests failed (first at "
361                                        "%s:%d)\n", failures, 
362                                        first_fail_file.c_str(),
363                                        first_fail_line);
364                        }
365                        else
366                        {
367                                printf("PASSED\n");
368                        }
369                }
370               
371                return returncode;
372        }
373        catch(BoxException &e)
374        {
375                printf("FAILED: Exception caught: %s: %s\n", e.what(),
376                        e.GetMessage().c_str());
377                return 1;
378        }
379        catch(std::exception &e)
380        {
381                printf("FAILED: Exception caught: %s\n", e.what());
382                return 1;
383        }
384        catch(...)
385        {
386                printf("FAILED: Unknown exception caught\n");
387                return 1;
388        }
389        if(fulltestmode)
390        {
391                if(checkfilesleftopen())
392                {
393                        printf("WARNING: Files were left open\n");
394                }
395        }
396}
397
Note: See TracBrowser for help on using the repository browser.