odp-util: Format and scan multiple MPLS labels.
[cascardo/ovs.git] / tests / test-lockfile.c
index b37fd22..d0c8dc4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 #include <config.h>
-
+#undef NDEBUG
 #include "lockfile.h"
-
 #include <errno.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
-
+#include "ovstest.h"
 #include "process.h"
 #include "timeval.h"
 #include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 struct test {
     const char *name;
     void (*function)(void);
 };
 
-static const struct test tests[];
+static void run_help(void);
 
 #define CHECK(A, B) check(A, B, #A, #B, __FILE__, __LINE__)
 static void
@@ -91,12 +90,12 @@ run_lock_blocks_same_process_twice(void)
     lockfile_unlock(lockfile);
 }
 
+#ifndef _WIN32
 static enum { PARENT, CHILD }
 do_fork(void)
 {
     switch (fork()) {
     case 0:
-        time_postfork();
         lockfile_postfork();
         return CHILD;
 
@@ -153,27 +152,6 @@ run_lock_and_unlock_allows_other_process(void)
     }
 }
 
-static void
-run_lock_multiple(void)
-{
-    struct lockfile *a, *b, *c, *dummy;
-
-    CHECK(lockfile_lock("a", &a), 0);
-    CHECK(lockfile_lock("b", &b), 0);
-    CHECK(lockfile_lock("c", &c), 0);
-
-    lockfile_unlock(a);
-    CHECK(lockfile_lock("a", &a), 0);
-    CHECK(lockfile_lock("a", &dummy), EDEADLK);
-    lockfile_unlock(a);
-
-    lockfile_unlock(b);
-    CHECK(lockfile_lock("a", &a), 0);
-
-    lockfile_unlock(c);
-    lockfile_unlock(a);
-}
-
 /* Checks that locking a dangling symlink works OK.  (It used to hang.) */
 static void
 run_lock_symlink(void)
@@ -236,50 +214,74 @@ run_lock_symlink_to_dir(void)
 
     lockfile_unlock(a);
 }
+#endif /* _WIN32 */
 
 static void
-run_help(void)
+run_lock_multiple(void)
 {
-    size_t i;
+    struct lockfile *a, *b, *c, *dummy;
 
-    printf("usage: %s TESTNAME\n"
-           "where TESTNAME is one of the following:\n",
-           program_name);
-    for (i = 0; tests[i].name; i++) {
-        fprintf(stderr, "\t%s\n", tests[i].name);
-    }
+    CHECK(lockfile_lock("a", &a), 0);
+    CHECK(lockfile_lock("b", &b), 0);
+    CHECK(lockfile_lock("c", &c), 0);
+
+    lockfile_unlock(a);
+    CHECK(lockfile_lock("a", &a), 0);
+    CHECK(lockfile_lock("a", &dummy), EDEADLK);
+    lockfile_unlock(a);
+
+    lockfile_unlock(b);
+    CHECK(lockfile_lock("a", &a), 0);
+
+    lockfile_unlock(c);
+    lockfile_unlock(a);
 }
 
+
 static const struct test tests[] = {
 #define TEST(NAME) { #NAME, run_##NAME }
     TEST(lock_and_unlock),
     TEST(lock_and_unlock_twice),
     TEST(lock_blocks_same_process),
     TEST(lock_blocks_same_process_twice),
+#ifndef _WIN32
     TEST(lock_blocks_other_process),
     TEST(lock_twice_blocks_other_process),
     TEST(lock_and_unlock_allows_other_process),
-    TEST(lock_multiple),
     TEST(lock_symlink),
     TEST(lock_symlink_to_dir),
+#endif /* _WIN32 */
+    TEST(lock_multiple),
     TEST(help),
     { NULL, NULL }
 #undef TEST
 };
 
-int
-main(int argc, char *argv[])
+static void
+run_help(void)
+{
+    size_t i;
+
+    printf("usage: %s TESTNAME\n"
+           "where TESTNAME is one of the following:\n",
+           program_name);
+    for (i = 0; tests[i].name; i++) {
+        fprintf(stderr, "\t%s\n", tests[i].name);
+    }
+}
+
+static void
+test_lockfile_main(int argc, char *argv[])
 {
-    extern struct vlog_module VLM_lockfile;
     size_t i;
 
     set_program_name(argv[0]);
-    vlog_set_levels(&VLM_lockfile, VLF_ANY_FACILITY, VLL_ERR);
+    vlog_set_pattern(VLF_CONSOLE, "%c|%p|%m");
+    vlog_set_levels(NULL, VLF_SYSLOG, VLL_OFF);
 
     if (argc != 2) {
         ovs_fatal(0, "exactly one argument required; use \"%s help\" for help",
                   program_name);
-        return 1;
     }
 
     for (i = 0; tests[i].name; i++) {
@@ -290,6 +292,7 @@ main(int argc, char *argv[])
             (tests[i].function)();
 
             n_children = 0;
+#ifndef _WIN32
             while (wait(&status) > 0) {
                 if (WIFEXITED(status) && WEXITSTATUS(status) == 11) {
                     n_children++;
@@ -301,6 +304,7 @@ main(int argc, char *argv[])
             if (errno != ECHILD) {
                 ovs_fatal(errno, "wait");
             }
+#endif /* _WIN32 */
 
             printf("%s: success (%d child%s)\n",
                    tests[i].name, n_children, n_children != 1 ? "ren" : "");
@@ -311,3 +315,4 @@ main(int argc, char *argv[])
               argv[1], program_name);
 }
 
+OVSTEST_REGISTER("test-lockfile", test_lockfile_main);