]> rtime.felk.cvut.cz Git - frescor/demo.git/commitdiff
Add wvtest from frsh-forb repository
authorMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 14 Oct 2010 19:40:01 +0000 (21:40 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 14 Oct 2010 19:43:31 +0000 (21:43 +0200)
FIXME: wvtest should be located somewhere else so that we do not need to
copy it here.

15 files changed:
build/Makefile.omk
build/scripts [new symlink]
build/wvtest [new symlink]
src/wvtest/c/Makefile [new file with mode: 0644]
src/wvtest/c/Makefile.omk [new file with mode: 0644]
src/wvtest/c/wvtest.c [new file with mode: 0644]
src/wvtest/c/wvtest.h [new file with mode: 0644]
src/wvtest/c/wvtestfrsh.c [new file with mode: 0644]
src/wvtest/c/wvtestfrsh.c~ [new file with mode: 0644]
src/wvtest/c/wvtestmain.c [new file with mode: 0644]
src/wvtest/cpp/Makefile [new file with mode: 0644]
src/wvtest/cpp/Makefile.omk [new file with mode: 0644]
src/wvtest/cpp/wvtest.cc [new file with mode: 0644]
src/wvtest/cpp/wvtest.h [new file with mode: 0644]
src/wvtest/cpp/wvtestmain.cc [new file with mode: 0644]

index 68a89ba274e6b028a438289bc74961610b4a34f1..8a57bc302f93d0f90576239126bc6aa57f437e3c 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS=$(sort $(ALL_OMK_SUBDIRS) ffmpeg)
+SUBDIRS=$(sort $(ALL_OMK_SUBDIRS) ffmpeg wvtest/c)
 
 test: run_diff
 
diff --git a/build/scripts b/build/scripts
new file mode 120000 (symlink)
index 0000000..adb0a1d
--- /dev/null
@@ -0,0 +1 @@
+../scripts/
\ No newline at end of file
diff --git a/build/wvtest b/build/wvtest
new file mode 120000 (symlink)
index 0000000..e8c279a
--- /dev/null
@@ -0,0 +1 @@
+../src/wvtest/
\ No newline at end of file
diff --git a/src/wvtest/c/Makefile b/src/wvtest/c/Makefile
new file mode 100644 (file)
index 0000000..b22a357
--- /dev/null
@@ -0,0 +1,14 @@
+# Generic directory or leaf node makefile for OCERA make framework
+
+ifndef MAKERULES_DIR
+MAKERULES_DIR := $(shell ( old_pwd="" ;  while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd`  ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) )
+endif
+
+ifeq ($(MAKERULES_DIR),)
+all : default
+.DEFAULT::
+       @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n"
+else
+include $(MAKERULES_DIR)/Makefile.rules
+endif
+
diff --git a/src/wvtest/c/Makefile.omk b/src/wvtest/c/Makefile.omk
new file mode 100644 (file)
index 0000000..6614b78
--- /dev/null
@@ -0,0 +1,7 @@
+
+include_HEADERS = wvtest.h
+
+CFLAGS += -DWVTEST_CONFIGURED
+
+lib_LIBRARIES = wvtest
+wvtest_SOURCES = wvtestmain.c wvtest.c wvtestfrsh.c
diff --git a/src/wvtest/c/wvtest.c b/src/wvtest/c/wvtest.c
new file mode 100644 (file)
index 0000000..ecbfbf7
--- /dev/null
@@ -0,0 +1,394 @@
+/*
+ * WvTest:
+ *   Copyright (C) 1997-2009 Net Integration Technologies, Inc.
+ *       Licensed under the GNU Library General Public License, version 2.
+ *       See the included file named LICENSE for license information.
+ */
+#include "wvtest.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#ifdef _WIN32
+#include <direct.h>
+#else
+#include <unistd.h>
+#include <sys/wait.h>
+#endif
+#include <errno.h>
+#include <signal.h>
+
+#include <stdlib.h>
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+# include <valgrind/memcheck.h>
+# include <valgrind/valgrind.h>
+#else
+# define VALGRIND_COUNT_ERRORS 0
+# define VALGRIND_DO_LEAK_CHECK
+# define VALGRIND_COUNT_LEAKS(a,b,c,d) (a=b=c=d=0)
+#endif
+
+#define MAX_TEST_TIME 40     // max seconds for a single test to run
+#define MAX_TOTAL_TIME 120*60 // max seconds for the entire suite to run
+
+#define TEST_START_FORMAT "! %s:%-5d %-40s "
+
+static int fails, runs;
+static time_t start_time;
+static bool run_twice;
+    
+static void alarm_handler(int sig);
+   
+static int memerrs()
+{
+    return (int)VALGRIND_COUNT_ERRORS;
+}
+
+static int memleaks()
+{
+    int leaked = 0, dubious = 0, reachable = 0, suppressed = 0;
+    VALGRIND_DO_LEAK_CHECK;
+    VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
+    printf("memleaks: sure:%d dubious:%d reachable:%d suppress:%d\n",
+          leaked, dubious, reachable, suppressed);
+    fflush(stdout);
+    
+    // dubious+reachable are normally non-zero because of globals...
+    // return leaked+dubious+reachable;
+    return leaked;
+}
+
+// Return 1 if no children are running or zombies, 0 if there are any running
+// or zombie children.
+// Will wait for any already-terminated children first.
+// Passes if no rogue children were running, fails otherwise.
+// If your test gets a failure in here, either you're not killing all your
+// children, or you're not calling waitpid(2) on all of them.
+static bool no_running_children()
+{
+#ifndef _WIN32
+    pid_t wait_result;
+
+    // Acknowledge and complain about any zombie children
+    do 
+    {
+       int status = 0;
+        wait_result = waitpid(-1, &status, WNOHANG);
+
+        if (wait_result > 0)
+        {
+            char buf[256];
+            snprintf(buf, sizeof(buf) - 1, "%d", wait_result);
+            buf[sizeof(buf)-1] = '\0';
+            WVFAILEQ("Unclaimed dead child process", buf);
+        }
+    } while (wait_result > 0);
+        
+    // There should not be any running children, so waitpid should return -1
+    WVPASS(errno == ECHILD);
+    WVPASS(wait_result == -1);
+    return (wait_result == -1 && errno == ECHILD);
+#endif
+    return true;
+}
+
+
+static void alarm_handler(int sig)
+{
+    printf("\n! WvTest  Current test took longer than %d seconds!  FAILED\n",
+          MAX_TEST_TIME);
+    fflush(stdout);
+    abort();
+}
+
+
+static const char *pathstrip(const char *filename)
+{
+    const char *cptr;
+    cptr = strrchr(filename, '/');
+    if (cptr) filename = cptr + 1;
+    cptr = strrchr(filename, '\\');
+    if (cptr) filename = cptr + 1;
+    return filename;
+}
+
+static bool prefix_match(const char *s, char * const *prefixes)
+{
+    char *const *prefix;
+    for (prefix = prefixes; prefix && *prefix; prefix++)
+    {
+       if (!strncasecmp(s, *prefix, strlen(*prefix)))
+           return true;
+    }
+    return false;
+}
+
+extern struct WvTest *__start_wvtest, *__stop_wvtest;
+
+int wvtest_run_all(char * const *prefixes)
+{
+    int old_valgrind_errs = 0, new_valgrind_errs;
+    int old_valgrind_leaks = 0, new_valgrind_leaks;
+    
+#ifdef _WIN32
+    /* I should be doing something to do with SetTimer here, 
+     * not sure exactly what just yet */
+#else
+    char *disable = getenv("WVTEST_DISABLE_TIMEOUT");
+    if (disable != NULL && disable[0] != '\0' && disable[0] != '0')
+        signal(SIGALRM, SIG_IGN);
+    else
+        signal(SIGALRM, alarm_handler);
+    alarm(MAX_TEST_TIME);
+#endif
+    start_time = time(NULL);
+    
+    // make sure we can always start out in the same directory, so tests have
+    // access to their files.  If a test uses chdir(), we want to be able to
+    // reverse it.
+    char wd[1024];
+    if (!getcwd(wd, sizeof(wd)))
+       strcpy(wd, ".");
+    
+    const char *slowstr1 = getenv("WVTEST_MIN_SLOWNESS");
+    const char *slowstr2 = getenv("WVTEST_MAX_SLOWNESS");
+    int min_slowness = 0, max_slowness = 65535;
+    if (slowstr1) min_slowness = atoi(slowstr1);
+    if (slowstr2) max_slowness = atoi(slowstr2);
+
+#ifdef _WIN32
+    run_twice = false;
+#else
+    char *parallel_str = getenv("WVTEST_PARALLEL");
+    if (parallel_str) 
+        run_twice = atoi(parallel_str) > 0;
+#endif
+
+    // there are lots of fflush() calls in here because stupid win32 doesn't
+    // flush very often by itself.
+    fails = runs = 0;
+    struct WvTest *cur, **p;
+    for (p = &__start_wvtest; p < &__stop_wvtest; p++)
+    {
+       cur = *p;
+       if (cur->slowness <= max_slowness
+           && cur->slowness >= min_slowness
+           && (!prefixes
+               || prefix_match(cur->idstr, prefixes)
+               || prefix_match(cur->descr, prefixes)))
+       {
+#ifndef _WIN32
+            // set SIGPIPE back to default, helps catch tests which don't set
+            // this signal to SIG_IGN (which is almost always what you want)
+            // on startup
+            signal(SIGPIPE, SIG_DFL);
+
+            pid_t child = 0;
+            if (run_twice)
+            {
+                // I see everything twice!
+                printf("Running test in parallel.\n");
+                child = fork();
+            }
+#endif
+
+           printf("\nTesting \"%s\" in %s:\n", cur->descr, cur->idstr);
+           fflush(stdout);
+           
+           cur->main();
+           chdir(wd);
+           
+           new_valgrind_errs = memerrs();
+           WVPASS(new_valgrind_errs == old_valgrind_errs);
+           old_valgrind_errs = new_valgrind_errs;
+           
+           new_valgrind_leaks = memleaks();
+           WVPASS(new_valgrind_leaks == old_valgrind_leaks);
+           old_valgrind_leaks = new_valgrind_leaks;
+           
+           fflush(stderr);
+           printf("\n");
+           fflush(stdout);
+
+#ifndef _WIN32
+            if (run_twice)
+            {
+                if (!child)
+                {
+                    // I see everything once!
+                    printf("Child exiting.\n");
+                    _exit(0);
+                }
+                else
+                {
+                    printf("Waiting for child to exit.\n");
+                    int result;
+                    while ((result = waitpid(child, NULL, 0)) == -1 && 
+                            errno == EINTR)
+                        printf("Waitpid interrupted, retrying.\n");
+                }
+            }
+#endif
+
+            WVPASS(no_running_children());
+       }
+    }
+    
+    WVPASS(runs > 0);
+    
+    if (prefixes && *prefixes && **prefixes)
+       printf("WvTest: WARNING: only ran tests starting with "
+              "specifed prefix(es).\n");
+    else
+       printf("WvTest: ran all tests.\n");
+    printf("WvTest: %d test%s, %d failure%s.\n",
+          runs, runs==1 ? "" : "s",
+          fails, fails==1 ? "": "s");
+    fflush(stdout);
+    
+    return fails != 0;
+}
+
+
+// If we aren't running in parallel, we want to output the name of the test
+// before we run it, so we know what happened if it crashes.  If we are
+// running in parallel, outputting this information in multiple printf()s
+// can confuse parsers, so we want to output everything in one printf().
+//
+// This function gets called by both start() and check().  If we're not
+// running in parallel, just print the data.  If we're running in parallel,
+// and we're starting a test, save a copy of the file/line/description until
+// the test is done and we can output it all at once.
+//
+// Yes, this is probably the worst API of all time.
+static void print_result_str(bool start, const char *_file, int _line, 
+                            const char *_condstr, const char *result)
+{
+    static char *file;
+    static char *condstr;
+    static int line;
+    char *cptr;
+    
+    if (start)
+    {
+        if (file) 
+            free(file);
+        if (condstr) 
+            free(condstr);
+        file = strdup(pathstrip(_file));
+        condstr = strdup(_condstr);
+        line = _line;
+
+        for (cptr = condstr; *cptr; cptr++)
+        {
+            if (!isprint((unsigned char)*cptr))
+                *cptr = '!';
+        }
+    }
+            
+    if (run_twice)
+    {
+        if (!start)
+            printf(TEST_START_FORMAT "%s\n", file, line, condstr, result);
+    }
+    else
+    {
+        if (start)
+            printf(TEST_START_FORMAT, file, line, condstr);
+        else
+            printf("%s\n", result);
+    }
+    fflush(stdout);
+
+    if (!start)
+    {
+        if (file)
+            free(file);
+        if (condstr)
+            free(condstr);
+        file = condstr = NULL;
+    }
+}
+
+static inline void
+print_result(bool start, const char *file, int line, 
+            const char *condstr, bool result)
+{
+       print_result_str(start, file, line, condstr, result ? "ok" : "FAILED");
+}
+
+void wvtest_start(const char *file, int line, const char *condstr)
+{
+    // Either print the file, line, and condstr, or save them for later.
+    print_result(true, file, line, condstr, false);
+}
+
+
+void wvtest_check(bool cond, const char *reason)
+{
+#ifndef _WIN32
+    alarm(MAX_TEST_TIME); // restart per-test timeout
+#endif
+    if (!start_time) start_time = time(NULL);
+    
+    if (time(NULL) - start_time > MAX_TOTAL_TIME)
+    {
+       printf("\n! WvTest   Total run time exceeded %d seconds!  FAILED\n",
+              MAX_TOTAL_TIME);
+       fflush(stdout);
+       abort();
+    }
+    
+    runs++;
+
+    print_result_str(false, NULL, 0, NULL, cond ? "ok" : (reason ? reason : "FAILED"));
+
+    if (!cond)
+    {
+       fails++;
+       
+       if (getenv("WVTEST_DIE_FAST"))
+           abort();
+    }
+}
+
+
+bool wvtest_start_check_eq(const char *file, int line,
+                           const char *a, const char *b, bool expect_pass)
+{
+    if (!a) a = "";
+    if (!b) b = "";
+    
+    size_t len = strlen(a) + strlen(b) + 8 + 1;
+    char str[len];
+    sprintf(str, "[%s] %s [%s]", a, expect_pass ? "==" : "!=", b);
+    
+    wvtest_start(file, line, str);
+    
+    bool cond = !strcmp(a, b);
+    if (!expect_pass)
+        cond = !cond;
+
+    wvtest_check(cond, NULL);
+    return cond;
+}
+
+
+bool wvtest_start_check_lt(const char *file, int line,
+                           const char *a, const char *b)
+{
+    if (!a) a = "";
+    if (!b) b = "";
+    
+    size_t len = strlen(a) + strlen(b) + 8 + 1;
+    char *str = malloc(len);
+    sprintf(str, "[%s] < [%s]", a, b);
+    
+    wvtest_start(file, line, str);
+    free(str);
+
+    bool cond = strcmp(a, b) < 0;
+    wvtest_check(cond, NULL);
+    return cond;
+}
diff --git a/src/wvtest/c/wvtest.h b/src/wvtest/c/wvtest.h
new file mode 100644 (file)
index 0000000..79c8da6
--- /dev/null
@@ -0,0 +1,69 @@
+/* -*- Mode: C -*-
+ * WvTest:
+ *   Copyright (C) 1997-2009 Net Integration Technologies, Inc.
+ *       Licensed under the GNU Library General Public License, version 2.
+ *       See the included file named LICENSE for license information.
+ */ 
+#ifndef __WVTEST_H
+#define __WVTEST_H
+
+#ifndef WVTEST_CONFIGURED
+# error "Missing settings: HAVE_VALGRIND_MEMCHECK_H HAVE_WVCRASH WVTEST_CONFIGURED"
+#endif
+
+#include <time.h>
+#include <string.h>
+#include <stdbool.h>
+
+typedef void wvtest_mainfunc();
+
+struct WvTest {
+       const char *descr, *idstr;
+       wvtest_mainfunc *main;
+       int slowness;
+       struct WvTest *next;
+};
+
+int wvtest_run_all(char * const *prefixes);
+void wvtest_start(const char *file, int line, const char *condstr);
+void wvtest_check(bool cond, const char *reason);
+static inline bool wvtest_start_check(const char *file, int line,
+                                     const char *condstr, bool cond)
+{ wvtest_start(file, line, condstr); wvtest_check(cond, NULL); return cond; }
+bool wvtest_start_check_eq(const char *file, int line,
+                          const char *a, const char *b, bool expect_pass);
+bool wvtest_start_check_lt(const char *file, int line,
+                          const char *a, const char *b);
+int wvtest_start_check_frsh(const char *file, int line,
+                           const char *condstr, int frsh_retval);
+
+
+#define WVPASS(cond) \
+    wvtest_start_check(__FILE__, __LINE__, #cond, (cond))
+#define WVPASSEQ(a, b) \
+    wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), true)
+#define WVPASSLT(a, b) \
+    wvtest_start_check_lt(__FILE__, __LINE__, (a), (b))
+#define WVFAIL(cond) \
+    wvtest_start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond))
+#define WVFAILEQ(a, b) \
+    wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), false)
+#define WVPASSNE(a, b) WVFAILEQ(a, b)
+#define WVFAILNE(a, b) WVPASSEQ(a, b)
+#define WVFRSH(frshretval) \
+       wvtest_start_check_frsh(__FILE__, __LINE__, #frshretval, (frshretval))
+
+
+#define WVTEST_MAIN3(_descr, ff, ll, _slowness)                                \
+       static void _wvtest_main_##ll();                                \
+       struct WvTest _wvtest_##ll = \
+       { .descr = _descr, .idstr = ff ":" #ll, .main = _wvtest_main_##ll, .slowness = _slowness }, \
+               *_wvtest_ptr_##ll __attribute__ ((section ("wvtest"))) = &_wvtest_##ll; \
+       static void _wvtest_main_##ll()
+#define WVTEST_MAIN2(descr, ff, ll, slowness)  \
+       WVTEST_MAIN3(descr, ff, ll, slowness)
+#define WVTEST_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 0)
+#define WVTEST_SLOW_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 1)
+
+
+#endif // __WVTEST_H
diff --git a/src/wvtest/c/wvtestfrsh.c b/src/wvtest/c/wvtestfrsh.c
new file mode 100644 (file)
index 0000000..cc1f276
--- /dev/null
@@ -0,0 +1,15 @@
+#include "wvtest.h"
+#include <frsh_error.h>
+
+int wvtest_start_check_frsh(const char *file, int line,
+                            const char *condstr, int frsh_retval)
+{
+       char errstr[80] = "ok";
+       wvtest_start(file, line, condstr);
+       if (frsh_retval != 0)
+               frsh_strerror (frsh_retval, errstr, sizeof(errstr));
+
+       wvtest_check(frsh_retval == 0, errstr);
+
+       return frsh_retval;
+}
diff --git a/src/wvtest/c/wvtestfrsh.c~ b/src/wvtest/c/wvtestfrsh.c~
new file mode 100644 (file)
index 0000000..ecee9dc
--- /dev/null
@@ -0,0 +1,14 @@
+#include <wvtest.h>
+
+int wvtest_start_check_frsh(const char *file, int line,
+                            const char *condstr, int frsh_retval)
+{
+       char errstr[80] = "ok";
+       wvtest_start(file, line, condstr);
+       if (frsh_retval != 0)
+               frsh_strerror (frsh_retval, errstr, sizeof(errstr));
+
+       wvtest_check(frsh_retval == 0, errstr);
+
+       return frsh_retval;
+}
diff --git a/src/wvtest/c/wvtestmain.c b/src/wvtest/c/wvtestmain.c
new file mode 100644 (file)
index 0000000..446e5ba
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * WvTest:
+ *   Copyright (C) 1997-2009 Net Integration Technologies, Inc.
+ *       Licensed under the GNU Library General Public License, version 2.
+ *       See the included file named LICENSE for license information.
+ */
+#include "wvtest.h"
+#ifdef HAVE_WVCRASH
+# include "wvcrash.h"
+#endif
+#include <stdlib.h>
+#include <stdio.h>
+#ifdef _WIN32
+#include <io.h>
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <fcntl.h>
+#endif
+
+static bool fd_is_valid(int fd)
+{
+#ifdef _WIN32
+    if ((HANDLE)_get_osfhandle(fd) != INVALID_HANDLE_VALUE) return true;
+#endif    
+    int nfd = dup(fd);
+    if (nfd >= 0)
+    {
+       close(nfd);
+       return true;
+    }
+    return false;
+
+}
+
+
+static int fd_count(const char *when)
+{
+    int count = 0;
+    int fd;
+    printf("fds open at %s:", when);
+    
+    for (fd = 0; fd < 1024; fd++)
+    {
+       if (fd_is_valid(fd))
+       {
+           count++;
+           printf(" %d", fd);
+           fflush(stdout);
+       }
+    }
+    printf("\n");
+    
+    return count;
+}
+
+
+int main(int argc, char **argv)
+{
+    char buf[200];
+#if defined(_WIN32) && defined(HAVE_WVCRASH)
+    setup_console_crash();
+#endif
+
+    // test wvtest itself.  Not very thorough, but you have to draw the
+    // line somewhere :)
+    WVPASS(true);
+    WVPASS(1);
+    WVFAIL(false);
+    WVFAIL(0);
+    int startfd, endfd;
+    char * const *prefixes = NULL;
+    
+    if (argc > 1)
+       prefixes = argv + 1;
+    
+    startfd = fd_count("start");
+    int ret = wvtest_run_all(prefixes);
+    
+//    if (ret == 0) // don't pollute the strace output if we failed anyway
+    if (0)
+    {
+       endfd = fd_count("end");
+    
+       WVPASS(startfd == endfd);
+#ifndef _WIN32
+       if (startfd != endfd)
+       {
+           sprintf(buf, "ls -l /proc/%d/fd", getpid());
+           system(buf);
+       }
+#endif    
+    }
+    
+    // keep 'make' from aborting if this environment variable is set
+    if (getenv("WVTEST_NO_FAIL"))
+       return 0;
+    else
+       return ret;
+}
diff --git a/src/wvtest/cpp/Makefile b/src/wvtest/cpp/Makefile
new file mode 100644 (file)
index 0000000..b22a357
--- /dev/null
@@ -0,0 +1,14 @@
+# Generic directory or leaf node makefile for OCERA make framework
+
+ifndef MAKERULES_DIR
+MAKERULES_DIR := $(shell ( old_pwd="" ;  while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd`  ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) )
+endif
+
+ifeq ($(MAKERULES_DIR),)
+all : default
+.DEFAULT::
+       @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n"
+else
+include $(MAKERULES_DIR)/Makefile.rules
+endif
+
diff --git a/src/wvtest/cpp/Makefile.omk b/src/wvtest/cpp/Makefile.omk
new file mode 100644 (file)
index 0000000..8bc2ce4
--- /dev/null
@@ -0,0 +1,7 @@
+
+include_HEADERS = wvtest.h
+
+CFLAGS += -DWVTEST_CONFIGURED
+
+lib_LIBRARIES = wvtest
+wvtest_SOURCES = wvtestmain.cc wvtest.cc
diff --git a/src/wvtest/cpp/wvtest.cc b/src/wvtest/cpp/wvtest.cc
new file mode 100644 (file)
index 0000000..6ae965d
--- /dev/null
@@ -0,0 +1,456 @@
+/*
+ * WvTest:
+ *   Copyright (C) 1997-2009 Net Integration Technologies, Inc.
+ *       Licensed under the GNU Library General Public License, version 2.
+ *       See the included file named LICENSE for license information.
+ */
+#include "wvtest.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#ifdef _WIN32
+#include <direct.h>
+#else
+#include <unistd.h>
+#include <sys/wait.h>
+#endif
+#include <errno.h>
+#include <signal.h>
+#include <frsh_error.h>
+
+#include <cstdlib>
+
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+# include <valgrind/memcheck.h>
+# include <valgrind/valgrind.h>
+#else
+# define VALGRIND_COUNT_ERRORS 0
+# define VALGRIND_DO_LEAK_CHECK
+# define VALGRIND_COUNT_LEAKS(a,b,c,d) (a=b=c=d=0)
+#endif
+
+#define MAX_TEST_TIME 40     // max seconds for a single test to run
+#define MAX_TOTAL_TIME 120*60 // max seconds for the entire suite to run
+
+#define TEST_START_FORMAT "! %s:%-5d %-40s "
+
+static int memerrs()
+{
+    return (int)VALGRIND_COUNT_ERRORS;
+}
+
+static int memleaks()
+{
+    int leaked = 0, dubious = 0, reachable = 0, suppressed = 0;
+    VALGRIND_DO_LEAK_CHECK;
+    VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
+    printf("memleaks: sure:%d dubious:%d reachable:%d suppress:%d\n",
+          leaked, dubious, reachable, suppressed);
+    fflush(stdout);
+    
+    // dubious+reachable are normally non-zero because of globals...
+    // return leaked+dubious+reachable;
+    return leaked;
+}
+
+// Return 1 if no children are running or zombies, 0 if there are any running
+// or zombie children.
+// Will wait for any already-terminated children first.
+// Passes if no rogue children were running, fails otherwise.
+// If your test gets a failure in here, either you're not killing all your
+// children, or you're not calling waitpid(2) on all of them.
+static bool no_running_children()
+{
+#ifndef _WIN32
+    pid_t wait_result;
+
+    // Acknowledge and complain about any zombie children
+    do 
+    {
+       int status = 0;
+        wait_result = waitpid(-1, &status, WNOHANG);
+
+        if (wait_result > 0)
+        {
+            char buf[256];
+            snprintf(buf, sizeof(buf) - 1, "%d", wait_result);
+            buf[sizeof(buf)-1] = '\0';
+            WVFAILEQ("Unclaimed dead child process", buf);
+        }
+    } while (wait_result > 0);
+        
+    // There should not be any running children, so waitpid should return -1
+    WVPASSEQ(errno, ECHILD);
+    WVPASSEQ(wait_result, -1);
+    return (wait_result == -1 && errno == ECHILD);
+#endif
+    return true;
+}
+
+
+WvTest *WvTest::first, *WvTest::last;
+int WvTest::fails, WvTest::runs;
+time_t WvTest::start_time;
+bool WvTest::run_twice = false;
+
+void WvTest::alarm_handler(int)
+{
+    printf("\n! WvTest  Current test took longer than %d seconds!  FAILED\n",
+          MAX_TEST_TIME);
+    fflush(stdout);
+    abort();
+}
+
+
+static const char *pathstrip(const char *filename)
+{
+    const char *cptr;
+    cptr = strrchr(filename, '/');
+    if (cptr) filename = cptr + 1;
+    cptr = strrchr(filename, '\\');
+    if (cptr) filename = cptr + 1;
+    return filename;
+}
+
+
+WvTest::WvTest(const char *_descr, const char *_idstr, MainFunc *_main,
+              int _slowness) :
+    descr(_descr), 
+    idstr(pathstrip(_idstr)), 
+    main(_main), 
+    slowness(_slowness),
+    next(NULL)
+{
+    if (first)
+       last->next = this;
+    else
+       first = this;
+    last = this;
+}
+
+
+static bool prefix_match(const char *s, const char * const *prefixes)
+{
+    for (const char * const *prefix = prefixes; prefix && *prefix; prefix++)
+    {
+       if (!strncasecmp(s, *prefix, strlen(*prefix)))
+           return true;
+    }
+    return false;
+}
+
+
+int WvTest::run_all(const char * const *prefixes)
+{
+    int old_valgrind_errs = 0, new_valgrind_errs;
+    int old_valgrind_leaks = 0, new_valgrind_leaks;
+    
+#ifdef _WIN32
+    /* I should be doing something to do with SetTimer here, 
+     * not sure exactly what just yet */
+#else
+    char *disable(getenv("WVTEST_DISABLE_TIMEOUT"));
+    if (disable != NULL && disable[0] != '\0' && disable[0] != '0')
+        signal(SIGALRM, SIG_IGN);
+    else
+        signal(SIGALRM, alarm_handler);
+    alarm(MAX_TEST_TIME);
+#endif
+    start_time = time(NULL);
+    
+    // make sure we can always start out in the same directory, so tests have
+    // access to their files.  If a test uses chdir(), we want to be able to
+    // reverse it.
+    char wd[1024];
+    if (!getcwd(wd, sizeof(wd)))
+       strcpy(wd, ".");
+    
+    const char *slowstr1 = getenv("WVTEST_MIN_SLOWNESS");
+    const char *slowstr2 = getenv("WVTEST_MAX_SLOWNESS");
+    int min_slowness = 0, max_slowness = 65535;
+    if (slowstr1) min_slowness = atoi(slowstr1);
+    if (slowstr2) max_slowness = atoi(slowstr2);
+
+#ifdef _WIN32
+    run_twice = false;
+#else
+    char *parallel_str = getenv("WVTEST_PARALLEL");
+    if (parallel_str) 
+        run_twice = atoi(parallel_str) > 0;
+#endif
+
+    // there are lots of fflush() calls in here because stupid win32 doesn't
+    // flush very often by itself.
+    fails = runs = 0;
+    for (WvTest *cur = first; cur; cur = cur->next)
+    {
+       if (cur->slowness <= max_slowness
+           && cur->slowness >= min_slowness
+           && (!prefixes
+               || prefix_match(cur->idstr, prefixes)
+               || prefix_match(cur->descr, prefixes)))
+       {
+#ifndef _WIN32
+            // set SIGPIPE back to default, helps catch tests which don't set
+            // this signal to SIG_IGN (which is almost always what you want)
+            // on startup
+            signal(SIGPIPE, SIG_DFL);
+
+            pid_t child = 0;
+            if (run_twice)
+            {
+                // I see everything twice!
+                printf("Running test in parallel.\n");
+                child = fork();
+            }
+#endif
+
+           printf("\nTesting \"%s\" in %s:\n", cur->descr, cur->idstr);
+           fflush(stdout);
+           
+           cur->main();
+           chdir(wd);
+           
+           new_valgrind_errs = memerrs();
+           WVPASS(new_valgrind_errs == old_valgrind_errs);
+           old_valgrind_errs = new_valgrind_errs;
+           
+           new_valgrind_leaks = memleaks();
+           WVPASS(new_valgrind_leaks == old_valgrind_leaks);
+           old_valgrind_leaks = new_valgrind_leaks;
+           
+           fflush(stderr);
+           printf("\n");
+           fflush(stdout);
+
+#ifndef _WIN32
+            if (run_twice)
+            {
+                if (!child)
+                {
+                    // I see everything once!
+                    printf("Child exiting.\n");
+                    _exit(0);
+                }
+                else
+                {
+                    printf("Waiting for child to exit.\n");
+                    int result;
+                    while ((result = waitpid(child, NULL, 0)) == -1 && 
+                            errno == EINTR)
+                        printf("Waitpid interrupted, retrying.\n");
+                }
+            }
+#endif
+
+            WVPASS(no_running_children());
+       }
+    }
+    
+    WVPASS(runs > 0);
+    
+    if (prefixes && *prefixes && **prefixes)
+       printf("WvTest: WARNING: only ran tests starting with "
+              "specifed prefix(es).\n");
+    else
+       printf("WvTest: ran all tests.\n");
+    printf("WvTest: %d test%s, %d failure%s.\n",
+          runs, runs==1 ? "" : "s",
+          fails, fails==1 ? "": "s");
+    fflush(stdout);
+    
+    return fails != 0;
+}
+
+
+// If we aren't running in parallel, we want to output the name of the test
+// before we run it, so we know what happened if it crashes.  If we are
+// running in parallel, outputting this information in multiple printf()s
+// can confuse parsers, so we want to output everything in one printf().
+//
+// This function gets called by both start() and check().  If we're not
+// running in parallel, just print the data.  If we're running in parallel,
+// and we're starting a test, save a copy of the file/line/description until
+// the test is done and we can output it all at once.
+//
+// Yes, this is probably the worst API of all time.
+void WvTest::print_result(bool start, const char *_file, int _line, 
+                         const char *_condstr, const char *result)
+{
+    static char *file;
+    static char *condstr;
+    static int line;
+    
+    if (start)
+    {
+        if (file) 
+            free(file);
+        if (condstr) 
+            free(condstr);
+        file = strdup(pathstrip(_file));
+        condstr = strdup(_condstr);
+        line = _line;
+
+        for (char *cptr = condstr; *cptr; cptr++)
+        {
+            if (!isprint((unsigned char)*cptr))
+                *cptr = '!';
+        }
+    }
+            
+    if (run_twice)
+    {
+        if (!start)
+            printf(TEST_START_FORMAT "%s\n", file, line, condstr, result);
+    }
+    else
+    {
+        if (start)
+            printf(TEST_START_FORMAT, file, line, condstr);
+        else
+            printf("%s\n", result);
+    }
+    fflush(stdout);
+
+    if (!start)
+    {
+        if (file)
+            free(file);
+        if (condstr)
+            free(condstr);
+        file = condstr = NULL;
+    }
+}
+
+
+void WvTest::start(const char *file, int line, const char *condstr)
+{
+    // Either print the file, line, and condstr, or save them for later.
+    print_result(true, file, line, condstr, false);
+}
+
+
+void WvTest::check(bool cond, const char *reason)
+{
+#ifndef _WIN32
+    alarm(MAX_TEST_TIME); // restart per-test timeout
+#endif
+    if (!start_time) start_time = time(NULL);
+    
+    if (time(NULL) - start_time > MAX_TOTAL_TIME)
+    {
+       printf("\n! WvTest   Total run time exceeded %d seconds!  FAILED\n",
+              MAX_TOTAL_TIME);
+       fflush(stdout);
+       abort();
+    }
+    
+    runs++;
+
+    print_result(false, NULL, 0, NULL, cond ? "ok" : reason);
+
+    if (!cond)
+    {
+       fails++;
+       
+       if (getenv("WVTEST_DIE_FAST"))
+           abort();
+    }
+}
+
+
+bool WvTest::start_check_eq(const char *file, int line,
+                           const char *a, const char *b, bool expect_pass)
+{
+    if (!a) a = "";
+    if (!b) b = "";
+    
+    size_t len = strlen(a) + strlen(b) + 8 + 1;
+    char *str = new char[len];
+    sprintf(str, "[%s] %s [%s]", a, expect_pass ? "==" : "!=", b);
+    
+    start(file, line, str);
+    delete[] str;
+    
+    bool cond = !strcmp(a, b);
+    if (!expect_pass)
+        cond = !cond;
+
+    check(cond);
+    return cond;
+}
+
+
+bool WvTest::start_check_eq(const char *file, int line,
+                           const std::string &a, const std::string &b, 
+                            bool expect_pass)
+{
+    return start_check_eq(file, line, a.c_str(), b.c_str(), expect_pass);
+}
+
+
+bool WvTest::start_check_eq(const char *file, int line, 
+                            int a, int b, bool expect_pass)
+{
+    size_t len = 128 + 128 + 8 + 1;
+    char *str = new char[len];
+    sprintf(str, "%d %s %d", a, expect_pass ? "==" : "!=", b);
+    
+    start(file, line, str);
+    delete[] str;
+    
+    bool cond = (a == b);
+    if (!expect_pass)
+        cond = !cond;
+
+    check(cond);
+    return cond;
+}
+
+
+bool WvTest::start_check_lt(const char *file, int line,
+                           const char *a, const char *b)
+{
+    if (!a) a = "";
+    if (!b) b = "";
+    
+    size_t len = strlen(a) + strlen(b) + 8 + 1;
+    char *str = new char[len];
+    sprintf(str, "[%s] < [%s]", a, b);
+    
+    start(file, line, str);
+    delete[] str;
+
+    bool cond = strcmp(a, b) < 0;
+    check(cond);
+    return cond;
+}
+
+
+bool WvTest::start_check_lt(const char *file, int line, int a, int b)
+{
+    size_t len = 128 + 128 + 8 + 1;
+    char *str = new char[len];
+    sprintf(str, "%d < %d", a, b);
+    
+    start(file, line, str);
+    delete[] str;
+    
+    bool cond = a < b;
+    check(cond);
+    return cond;
+}
+
+int WvTest::start_check_frsh(const char *file, int line,
+                            const char *condstr, int frsh_retval)
+{
+       char errstr[80] = "ok";
+       start(file, line, condstr);
+       if (frsh_retval != 0)
+               frsh_strerror (frsh_retval, errstr, sizeof(errstr));
+
+       check(frsh_retval == 0, errstr);
+
+       return frsh_retval;
+}
diff --git a/src/wvtest/cpp/wvtest.h b/src/wvtest/cpp/wvtest.h
new file mode 100644 (file)
index 0000000..287e3fb
--- /dev/null
@@ -0,0 +1,88 @@
+/* -*- Mode: C++ -*-
+ * WvTest:
+ *   Copyright (C) 1997-2009 Net Integration Technologies, Inc.
+ *       Licensed under the GNU Library General Public License, version 2.
+ *       See the included file named LICENSE for license information.
+ */ 
+#ifndef __WVTEST_H
+#define __WVTEST_H
+
+#ifndef WVTEST_CONFIGURED
+# error "Missing settings: HAVE_VALGRIND_MEMCHECK_H HAVE_WVCRASH WVTEST_CONFIGURED"
+#endif
+
+#include <time.h>
+#include <string>
+
+class WvTest
+{
+    typedef void MainFunc();
+    const char *descr, *idstr;
+    MainFunc *main;
+    int slowness;
+    WvTest *next;
+    static WvTest *first, *last;
+    static int fails, runs;
+    static time_t start_time;
+    static bool run_twice;
+    
+    static void alarm_handler(int sig);
+   
+    static void
+    print_result(bool start, const char *file, int line, 
+                const char *condstr, const char *result);
+
+    static inline void
+    print_result(bool start, const char *file, int line, 
+                const char *condstr, bool result)
+       { print_result(start, file, line, condstr, result ? "ok" : "FAILED"); }
+public:
+    WvTest(const char *_descr, const char *_idstr, MainFunc *_main, int _slow);
+    static int run_all(const char * const *prefixes = NULL);
+    static void start(const char *file, int line, const char *condstr);
+    static void check(bool cond, const char *reason = "FAILED");
+    static inline bool start_check(const char *file, int line,
+                                  const char *condstr, bool cond)
+        { start(file, line, condstr); check(cond); return cond; }
+    static bool start_check_eq(const char *file, int line,
+                              const char *a, const char *b, bool expect_pass);
+    static bool start_check_eq(const char *file, int line,
+                              const std::string &a, const std::string &b, 
+                               bool expect_pass);
+    static bool start_check_eq(const char *file, int line, int a, int b,
+                               bool expect_pass);
+    static bool start_check_lt(const char *file, int line,
+                               const char *a, const char *b);
+    static bool start_check_lt(const char *file, int line, int a, int b);
+    static int start_check_frsh(const char *file, int line,
+                               const char *condstr, int frsh_retval);
+};
+
+
+#define WVPASS(cond) \
+    WvTest::start_check(__FILE__, __LINE__, #cond, (cond))
+#define WVPASSEQ(a, b) \
+    WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), true)
+#define WVPASSLT(a, b) \
+    WvTest::start_check_lt(__FILE__, __LINE__, (a), (b))
+#define WVFAIL(cond) \
+    WvTest::start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond))
+#define WVFAILEQ(a, b) \
+    WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), false)
+#define WVPASSNE(a, b) WVFAILEQ(a, b)
+#define WVFAILNE(a, b) WVPASSEQ(a, b)
+#define WVFRSH(frshretval) \
+       WvTest::start_check_frsh(__FILE__, __LINE__, #frshretval, (frshretval))
+
+
+#define WVTEST_MAIN3(descr, ff, ll, slowness) \
+    static void _wvtest_main_##ll(); \
+    static WvTest _wvtest_##ll(descr, ff, _wvtest_main_##ll, slowness); \
+    static void _wvtest_main_##ll()
+#define WVTEST_MAIN2(descr, ff, ll, slowness) \
+    WVTEST_MAIN3(descr, ff, ll, slowness)
+#define WVTEST_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 0)
+#define WVTEST_SLOW_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 1)
+
+
+#endif // __WVTEST_H
diff --git a/src/wvtest/cpp/wvtestmain.cc b/src/wvtest/cpp/wvtestmain.cc
new file mode 100644 (file)
index 0000000..b44dfb5
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * WvTest:
+ *   Copyright (C) 1997-2009 Net Integration Technologies, Inc.
+ *       Licensed under the GNU Library General Public License, version 2.
+ *       See the included file named LICENSE for license information.
+ */
+#include "wvtest.h"
+#ifdef HAVE_WVCRASH
+# include "wvcrash.h"
+#endif
+#include <stdlib.h>
+#include <stdio.h>
+#ifdef _WIN32
+#include <io.h>
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <fcntl.h>
+#endif
+
+static bool fd_is_valid(int fd)
+{
+#ifdef _WIN32
+    if ((HANDLE)_get_osfhandle(fd) != INVALID_HANDLE_VALUE) return true;
+#endif    
+    int nfd = dup(fd);
+    if (nfd >= 0)
+    {
+       close(nfd);
+       return true;
+    }
+    return false;
+
+}
+
+
+static int fd_count(const char *when)
+{
+    int count = 0;
+    
+    printf("fds open at %s:", when);
+    
+    for (int fd = 0; fd < 1024; fd++)
+    {
+       if (fd_is_valid(fd))
+       {
+           count++;
+           printf(" %d", fd);
+           fflush(stdout);
+       }
+    }
+    printf("\n");
+    
+    return count;
+}
+
+
+int main(int argc, char **argv)
+{
+    char buf[200];
+#if defined(_WIN32) && defined(HAVE_WVCRASH)
+    setup_console_crash();
+#endif
+
+    // test wvtest itself.  Not very thorough, but you have to draw the
+    // line somewhere :)
+    WVPASS(true);
+    WVPASS(1);
+    WVFAIL(false);
+    WVFAIL(0);
+    int startfd, endfd;
+    char * const *prefixes = NULL;
+    
+    if (argc > 1)
+       prefixes = argv + 1;
+    
+    startfd = fd_count("start");
+    int ret = WvTest::run_all(prefixes);
+    
+    if (ret == 0) // don't pollute the strace output if we failed anyway
+    {
+       endfd = fd_count("end");
+    
+       WVPASS(startfd == endfd);
+#ifndef _WIN32
+       if (startfd != endfd)
+       {
+           sprintf(buf, "ls -l /proc/%d/fd", getpid());
+           system(buf);
+       }
+#endif    
+    }
+    
+    // keep 'make' from aborting if this environment variable is set
+    if (getenv("WVTEST_NO_FAIL"))
+       return 0;
+    else
+       return ret;
+}