check: Import version 0.9.14
[platform/upstream/gstreamer.git] / libs / gst / check / libcheck / check_msg.c
index ae653ff..a55b0b8 100644 (file)
  * Boston, MA 02110-1301, USA.
  */
 
-#include "config.h"
+#include "../lib/libcompat.h"
 
 #include <sys/types.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 #include <stdlib.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -34,6 +31,7 @@
 #include "check_impl.h"
 #include "check_msg.h"
 #include "check_pack.h"
+#include "check_str.h"
 
 
 /* 'Pipe' is implemented as a temporary file to overcome message
@@ -57,7 +55,9 @@
  */
 
 static FILE *send_file1;
+static char *send_file1_name;
 static FILE *send_file2;
+static char *send_file2_name;
 
 static FILE *get_pipe (void);
 static void setup_pipe (void);
@@ -86,8 +86,18 @@ send_failure_info (const char *msg)
 {
   FailMsg fmsg;
 
-  fmsg.msg = (char *) msg;
-  ppack (fileno (get_pipe ()), CK_MSG_FAIL, (CheckMsg *) & fmsg);
+  fmsg.msg = strdup (msg);
+  ppack (get_pipe (), CK_MSG_FAIL, (CheckMsg *) & fmsg);
+  free (fmsg.msg);
+}
+
+void
+send_duration_info (int duration)
+{
+  DurationMsg dmsg;
+
+  dmsg.duration = duration;
+  ppack (get_pipe (), CK_MSG_DURATION, (CheckMsg *) & dmsg);
 }
 
 void
@@ -95,9 +105,10 @@ send_loc_info (const char *file, int line)
 {
   LocMsg lmsg;
 
-  lmsg.file = (char *) file;
+  lmsg.file = strdup (file);
   lmsg.line = line;
-  ppack (fileno (get_pipe ()), CK_MSG_LOC, (CheckMsg *) & lmsg);
+  ppack (get_pipe (), CK_MSG_LOC, (CheckMsg *) & lmsg);
+  free (lmsg.file);
 }
 
 void
@@ -106,7 +117,7 @@ send_ctx_info (enum ck_result_ctx ctx)
   CtxMsg cmsg;
 
   cmsg.ctx = ctx;
-  ppack (fileno (get_pipe ()), CK_MSG_CTX, (CheckMsg *) & cmsg);
+  ppack (get_pipe (), CK_MSG_CTX, (CheckMsg *) & cmsg);
 }
 
 TestResult *
@@ -117,10 +128,17 @@ receive_test_result (int waserror)
   TestResult *result;
 
   fp = get_pipe ();
-  if (fp == NULL)
+  if (fp == NULL) {
     eprintf ("Error in call to get_pipe", __FILE__, __LINE__ - 2);
+  }
+
   rewind (fp);
-  rmsg = punpack (fileno (fp));
+  rmsg = punpack (fp);
+
+  if (rmsg == NULL) {
+    eprintf ("Error in call to punpack", __FILE__, __LINE__ - 4);
+  }
+
   teardown_pipe ();
   setup_pipe ();
 
@@ -156,7 +174,7 @@ construct_test_result (RcvMsg * rmsg, int waserror)
   tr = tr_create ();
 
   if (rmsg->msg != NULL || waserror) {
-    tr->ctx = (cur_fork_status () == CK_FORK) ? rmsg->lastctx : rmsg->failctx;
+    tr->ctx = rmsg->lastctx;
     tr->msg = rmsg->msg;
     rmsg->msg = NULL;
     tr_set_loc_by_ctx (tr, tr->ctx, rmsg);
@@ -167,6 +185,7 @@ construct_test_result (RcvMsg * rmsg, int waserror)
   } else {
     tr->ctx = CK_CTX_TEST;
     tr->msg = NULL;
+    tr->duration = rmsg->duration;
     tr_set_loc_by_ctx (tr, CK_CTX_TEST, rmsg);
   }
 
@@ -185,16 +204,67 @@ teardown_messaging (void)
   teardown_pipe ();
 }
 
+/**
+ * Open a temporary file.
+ *
+ * If the file could be unlinked upon creation, the name
+ * of the file is not returned via 'name'. However, if the
+ * file could not be unlinked, the name is returned,
+ * expecting the caller to both delete the file and
+ * free the 'name' field after the file is closed.
+ */
+FILE *
+open_tmp_file (char **name)
+{
+  FILE *file;
+
+  *name = NULL;
+
+  /* Windows does not like tmpfile(). This is likely because tmpfile()
+   * call unlink() on the file before returning it, to make sure the
+   * file is deleted when it is closed. The unlink() call also fails
+   * on Windows if the file is still open. */
+  /* also note that mkstemp is apparently a C90 replacement for tmpfile */
+  /* perhaps all we need to do on Windows is set TMPDIR to whatever is
+     stored in TEMP for tmpfile to work */
+  /* and finally, the "b" from "w+b" is ignored on OS X, not sure about WIN32 */
+
+  file = tmpfile ();
+  if (file == NULL) {
+    char *tmp = getenv ("TEMP");
+    char *tmp_file = tempnam (tmp, "check_");
+
+    /*
+     * Note, tempnam is not enough to get a unique name. Between
+     * getting the name and opening the file, something else also
+     * calling tempnam() could get the same name. It has been observed
+     * on MinGW-w64 builds on Wine that this exact thing happens
+     * if multiple instances of a unit tests are running concurrently.
+     * To prevent two concurrent unit tests from getting the same file,
+     * we append the pid to the file. The pid should be unique on the
+     * system.
+     */
+    char *uniq_tmp_file = ck_strdup_printf ("%s.%d", tmp_file, getpid ());
+
+    file = fopen (uniq_tmp_file, "w+b");
+    *name = uniq_tmp_file;
+    free (tmp_file);
+  }
+  return file;
+}
+
 static void
 setup_pipe (void)
 {
-  if (send_file1 != 0) {
-    if (send_file2 != 0)
-      eprintf ("Only one nesting of suite runs supported", __FILE__, __LINE__);
-    send_file2 = tmpfile ();
-  } else {
-    send_file1 = tmpfile ();
+  if (send_file1 == NULL) {
+    send_file1 = open_tmp_file (&send_file1_name);
+    return;
+  }
+  if (send_file2 == NULL) {
+    send_file2 = open_tmp_file (&send_file2_name);
+    return;
   }
+  eprintf ("Only one nesting of suite runs supported", __FILE__, __LINE__);
 }
 
 static void
@@ -203,9 +273,19 @@ teardown_pipe (void)
   if (send_file2 != 0) {
     fclose (send_file2);
     send_file2 = 0;
+    if (send_file2_name != NULL) {
+      unlink (send_file2_name);
+      free (send_file2_name);
+      send_file2_name = NULL;
+    }
   } else if (send_file1 != 0) {
     fclose (send_file1);
     send_file1 = 0;
+    if (send_file1_name != NULL) {
+      unlink (send_file1_name);
+      free (send_file1_name);
+      send_file1_name = NULL;
+    }
   } else {
     eprintf ("No messaging setup", __FILE__, __LINE__);
   }