diff options
Diffstat (limited to 'infrastructure/buildenv-testmain-template.cpp')
-rw-r--r-- | infrastructure/buildenv-testmain-template.cpp | 214 |
1 files changed, 194 insertions, 20 deletions
diff --git a/infrastructure/buildenv-testmain-template.cpp b/infrastructure/buildenv-testmain-template.cpp index 252a9f0f..9922a584 100644 --- a/infrastructure/buildenv-testmain-template.cpp +++ b/infrastructure/buildenv-testmain-template.cpp @@ -1,4 +1,4 @@ -// distribution boxbackup-0.10 (svn version: 494) +// distribution boxbackup-0.11rc1 (svn version: 2023_2024) // // Copyright (c) 2003 - 2006 // Ben Summers and contributors. All rights reserved. @@ -40,6 +40,9 @@ // AUTOMATICALLY GENERATED FILE // do not edit // +// Note that infrastructure/buildenv-testmain-template.cpp is NOT +// auto-generated, but test/*/_main.cpp are generated from it. +// // -------------------------------------------------------------------------- @@ -61,6 +64,11 @@ #include <stdarg.h> #include <fcntl.h> #include <errno.h> +#include <string> + +#ifdef HAVE_GETOPT_H + #include <getopt.h> +#endif #ifdef WIN32 #include "emu.h" @@ -68,6 +76,12 @@ #include <syslog.h> #endif +#include <string> + +#include "Logging.h" +#include "Test.h" +#include "Timer.h" + #include "MemLeakFindOn.h" int test(int argc, const char *argv[]); @@ -79,77 +93,234 @@ int test(int argc, const char *argv[]); #endif int failures = 0; +int first_fail_line; +std::string first_fail_file; +std::string bbackupd_args, bbstored_args, bbackupquery_args, test_args; int filedes_open_at_beginning = -1; #ifdef WIN32 // any way to check for open file descriptors on Win32? -inline int count_filedes() { return 0; } -inline bool checkfilesleftopen() { return false; } +inline bool check_filedes(bool x) { return 0; } +inline bool checkfilesleftopen() { return false; } #else // !WIN32 -int count_filedes() +#define FILEDES_MAX 256 + +bool filedes_open[FILEDES_MAX]; + +bool check_filedes(bool report) { - int c = 0; + bool allOk = true; // See how many file descriptors there are with values < 256 - for(int d = 0; d < 256; ++d) + for(int d = 0; d < FILEDES_MAX; ++d) { if(::fcntl(d, F_GETFD) != -1) { // File descriptor obviously exists - ++c; + if (report && !filedes_open[d]) + { + struct stat st; + if (fstat(d, &st) == 0) + { + int m = st.st_mode; + #define flag(x) ((m & x) ? #x " " : "") + BOX_FATAL("File descriptor " << d << + " left open (type == " << + flag(S_IFIFO) << + flag(S_IFCHR) << + flag(S_IFDIR) << + flag(S_IFBLK) << + flag(S_IFREG) << + flag(S_IFLNK) << + flag(S_IFSOCK) << + " or " << m << ")"); + } + else + { + BOX_FATAL("File descriptor " << d << + " left open (and stat failed)"); + } + + allOk = false; + + } + else if (!report) + { + filedes_open[d] = true; + } + } + else + { + if (report && filedes_open[d]) + { + BOX_FATAL("File descriptor " << d << + " was open, now closed"); + allOk = false; + } + else + { + filedes_open[d] = false; + } } } + + if (!report && allOk) + { + filedes_open_at_beginning = 0; + } - return c; + return !allOk; } bool checkfilesleftopen() { if(filedes_open_at_beginning == -1) { - // Not used correctly, pretend that there were things left open so this gets invesitgated + // Not used correctly, pretend that there were things + // left open so this gets investigated + BOX_FATAL("File descriptor test was not initialised"); return true; } - // make sure syslog log file is closed, if it was opened - ::closelog(); - // Count the file descriptors open - return filedes_open_at_beginning != count_filedes(); + return check_filedes(true); } #endif -int main(int argc, const char *argv[]) +int main(int argc, char * const * argv) { // Start memory leak testing MEMLEAKFINDER_START +#ifdef HAVE_GETOPT_H + struct option longopts[] = + { + { "bbackupd-args", required_argument, NULL, 'c' }, + { "bbstored-args", required_argument, NULL, 's' }, + { "test-daemon-args", required_argument, NULL, 'd' }, + { NULL, 0, NULL, 0 } + }; + + int ch; + + while ((ch = getopt_long(argc, argv, "c:d:s:t:TUV", longopts, NULL)) + != -1) + { + switch(ch) + { + case 'c': + { + if (bbackupd_args.length() > 0) + { + bbackupd_args += " "; + } + bbackupd_args += optarg; + } + break; + + case 'd': + { + if (test_args.length() > 0) + { + test_args += " "; + } + test_args += optarg; + } + break; + + case 's': + { + bbstored_args += " "; + bbstored_args += optarg; + } + break; + + case 't': + { + Console::SetTag(optarg); + } + break; + + case 'T': + { + Console::SetShowTime(true); + } + break; + + case 'U': + { + Console::SetShowTime(true); + Console::SetShowTimeMicros(true); + } + break; + + case 'V': + { + Logging::SetGlobalLevel(Log::EVERYTHING); + } + break; + + case '?': + { + fprintf(stderr, "Unknown option: '%c'\n", + optopt); + exit(2); + } + + default: + { + fprintf(stderr, "Unknown option code '%c'\n", + ch); + exit(2); + } + } + } + + argc -= optind - 1; + argv += optind - 1; +#endif // HAVE_GETOPT_H + // If there is more than one argument, then the test is doing something advanced, so leave it alone bool fulltestmode = (argc == 1); if(fulltestmode) { + // banner + BOX_NOTICE("Running test TEST_NAME in " MODE_TEXT " mode..."); + // Count open file descriptors for a very crude "files left open" test - filedes_open_at_beginning = count_filedes(); + check_filedes(false); - // banner - printf("Running test TEST_NAME in " MODE_TEXT " mode...\n"); + #ifdef WIN32 + // Under win32 we must initialise the Winsock library + // before using sockets + + WSADATA info; + TEST_THAT(WSAStartup(0x0101, &info) != SOCKET_ERROR) + #endif } + try { - int returncode = test(argc, argv); + #ifdef BOX_MEMORY_LEAK_TESTING + memleakfinder_init(); + #endif + + Timers::Init(); + int returncode = test(argc, (const char **)argv); + Timers::Cleanup(); // check for memory leaks, if enabled #ifdef BOX_MEMORY_LEAK_TESTING if(memleakfinder_numleaks() != 0) { failures++; - printf("FAILURE: Memory leaks detected\n"); + printf("FAILURE: Memory leaks detected in test code\n"); printf("==== MEMORY LEAKS =================================\n"); memleakfinder_reportleaks(); printf("===================================================\n"); @@ -166,7 +337,10 @@ int main(int argc, const char *argv[]) } if(failures > 0) { - printf("FAILED: %d tests failed\n", failures); + printf("FAILED: %d tests failed (first at " + "%s:%d)\n", failures, + first_fail_file.c_str(), + first_fail_line); } else { |