check: Use mkstemp instead of tempnam if possible
[platform/upstream/gstreamer.git] / libs / gst / check / libcheck / check_msg.c
1 /*
2  * Check: a unit test framework for C
3  * Copyright (C) 2001 2002, Arien Malec
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "libcompat.h"
22
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27
28 #include "check_error.h"
29 #include "internal-check.h"
30 #include "check_list.h"
31 #include "check_impl.h"
32 #include "check_msg.h"
33 #include "check_pack.h"
34 #include "check_str.h"
35
36
37 /* 'Pipe' is implemented as a temporary file to overcome message
38  * volume limitations outlined in bug #482012. This scheme works well
39  * with the existing usage wherein the parent does not begin reading
40  * until the child has done writing and exited.
41  *
42  * Pipe life cycle:
43  * - The parent creates a tmpfile().
44  * - The fork() call has the effect of duplicating the file descriptor
45  *   and copying (on write) the FILE* data structures.
46  * - The child writes to the file, and its dup'ed file descriptor and
47  *   data structures are cleaned up on child process exit.
48  * - Before reading, the parent rewind()'s the file to reset both
49  *   FILE* and underlying file descriptor location data.
50  * - When finished, the parent fclose()'s the FILE*, deleting the
51  *   temporary file, per tmpfile()'s semantics.
52  *
53  * This scheme may break down if the usage changes to asynchronous
54  * reading and writing.
55  */
56
57 static FILE *send_file1;
58 static char *send_file1_name;
59 static FILE *send_file2;
60 static char *send_file2_name;
61
62 static FILE *get_pipe (void);
63 static void setup_pipe (void);
64 static void teardown_pipe (void);
65 static TestResult *construct_test_result (RcvMsg * rmsg, int waserror);
66 static void tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx,
67     RcvMsg * rmsg);
68 static FILE *
69 get_pipe (void)
70 {
71   if (send_file2 != 0) {
72     return send_file2;
73   }
74
75   if (send_file1 != 0) {
76     return send_file1;
77   }
78
79   eprintf ("No messaging setup", __FILE__, __LINE__);
80
81   return NULL;
82 }
83
84 void
85 send_failure_info (const char *msg)
86 {
87   FailMsg fmsg;
88
89   fmsg.msg = strdup (msg);
90   ppack (get_pipe (), CK_MSG_FAIL, (CheckMsg *) & fmsg);
91   free (fmsg.msg);
92 }
93
94 void
95 send_duration_info (int duration)
96 {
97   DurationMsg dmsg;
98
99   dmsg.duration = duration;
100   ppack (get_pipe (), CK_MSG_DURATION, (CheckMsg *) & dmsg);
101 }
102
103 void
104 send_loc_info (const char *file, int line)
105 {
106   LocMsg lmsg;
107
108   lmsg.file = strdup (file);
109   lmsg.line = line;
110   ppack (get_pipe (), CK_MSG_LOC, (CheckMsg *) & lmsg);
111   free (lmsg.file);
112 }
113
114 void
115 send_ctx_info (enum ck_result_ctx ctx)
116 {
117   CtxMsg cmsg;
118
119   cmsg.ctx = ctx;
120   ppack (get_pipe (), CK_MSG_CTX, (CheckMsg *) & cmsg);
121 }
122
123 TestResult *
124 receive_test_result (int waserror)
125 {
126   FILE *fp;
127   RcvMsg *rmsg;
128   TestResult *result;
129
130   fp = get_pipe ();
131   if (fp == NULL) {
132     eprintf ("Error in call to get_pipe", __FILE__, __LINE__ - 2);
133   }
134
135   rewind (fp);
136   rmsg = punpack (fp);
137
138   if (rmsg == NULL) {
139     eprintf ("Error in call to punpack", __FILE__, __LINE__ - 4);
140   }
141
142   teardown_pipe ();
143   setup_pipe ();
144
145   result = construct_test_result (rmsg, waserror);
146   rcvmsg_free (rmsg);
147   return result;
148 }
149
150 static void
151 tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx, RcvMsg * rmsg)
152 {
153   if (ctx == CK_CTX_TEST) {
154     tr->file = rmsg->test_file;
155     tr->line = rmsg->test_line;
156     rmsg->test_file = NULL;
157     rmsg->test_line = -1;
158   } else {
159     tr->file = rmsg->fixture_file;
160     tr->line = rmsg->fixture_line;
161     rmsg->fixture_file = NULL;
162     rmsg->fixture_line = -1;
163   }
164 }
165
166 static TestResult *
167 construct_test_result (RcvMsg * rmsg, int waserror)
168 {
169   TestResult *tr;
170
171   if (rmsg == NULL)
172     return NULL;
173
174   tr = tr_create ();
175
176   if (rmsg->msg != NULL || waserror) {
177     tr->ctx = rmsg->lastctx;
178     tr->msg = rmsg->msg;
179     rmsg->msg = NULL;
180     tr_set_loc_by_ctx (tr, tr->ctx, rmsg);
181   } else if (rmsg->lastctx == CK_CTX_SETUP) {
182     tr->ctx = CK_CTX_SETUP;
183     tr->msg = NULL;
184     tr_set_loc_by_ctx (tr, CK_CTX_SETUP, rmsg);
185   } else {
186     tr->ctx = CK_CTX_TEST;
187     tr->msg = NULL;
188     tr->duration = rmsg->duration;
189     tr_set_loc_by_ctx (tr, CK_CTX_TEST, rmsg);
190   }
191
192   return tr;
193 }
194
195 void
196 setup_messaging (void)
197 {
198   setup_pipe ();
199 }
200
201 void
202 teardown_messaging (void)
203 {
204   teardown_pipe ();
205 }
206
207 /**
208  * Open a temporary file.
209  *
210  * If the file could be unlinked upon creation, the name
211  * of the file is not returned via 'name'. However, if the
212  * file could not be unlinked, the name is returned,
213  * expecting the caller to both delete the file and
214  * free the 'name' field after the file is closed.
215  */
216 FILE *
217 open_tmp_file (char **name)
218 {
219   FILE *file = NULL;
220
221   *name = NULL;
222
223 #if !HAVE_MKSTEMP
224   /* Windows does not like tmpfile(). This is likely because tmpfile()
225    * call unlink() on the file before returning it, to make sure the
226    * file is deleted when it is closed. The unlink() call also fails
227    * on Windows if the file is still open. */
228   /* also note that mkstemp is apparently a C90 replacement for tmpfile */
229   /* perhaps all we need to do on Windows is set TMPDIR to whatever is
230      stored in TEMP for tmpfile to work */
231   /* and finally, the "b" from "w+b" is ignored on OS X, not sure about WIN32 */
232
233   file = tmpfile ();
234   if (file == NULL) {
235     char *tmp = getenv ("TEMP");
236     char *tmp_file = tempnam (tmp, "check_");
237
238     /*
239      * Note, tempnam is not enough to get a unique name. Between
240      * getting the name and opening the file, something else also
241      * calling tempnam() could get the same name. It has been observed
242      * on MinGW-w64 builds on Wine that this exact thing happens
243      * if multiple instances of a unit tests are running concurrently.
244      * To prevent two concurrent unit tests from getting the same file,
245      * we append the pid to the file. The pid should be unique on the
246      * system.
247      */
248     char *uniq_tmp_file = ck_strdup_printf ("%s.%d", tmp_file, getpid ());
249
250     file = fopen (uniq_tmp_file, "w+b");
251     *name = uniq_tmp_file;
252     free (tmp_file);
253   }
254 #else
255   int fd = -1;
256   const char *tmp_dir = getenv ("TEMP");
257   if (!tmp_dir) {
258     tmp_dir = ".";
259   }
260   *name = ck_strdup_printf ("%s/check_XXXXXX", tmp_dir);
261   if (-1 < (fd = mkstemp (*name))) {
262     file = fdopen (fd, "w+b");
263     if (0 == unlink (*name) || NULL == file) {
264       free (*name);
265       *name = NULL;
266     }
267   }
268 #endif
269   return file;
270 }
271
272 static void
273 setup_pipe (void)
274 {
275   if (send_file1 == NULL) {
276     send_file1 = open_tmp_file (&send_file1_name);
277     return;
278   }
279   if (send_file2 == NULL) {
280     send_file2 = open_tmp_file (&send_file2_name);
281     return;
282   }
283   eprintf ("Only one nesting of suite runs supported", __FILE__, __LINE__);
284 }
285
286 static void
287 teardown_pipe (void)
288 {
289   if (send_file2 != 0) {
290     fclose (send_file2);
291     send_file2 = 0;
292     if (send_file2_name != NULL) {
293       unlink (send_file2_name);
294       free (send_file2_name);
295       send_file2_name = NULL;
296     }
297   } else if (send_file1 != 0) {
298     fclose (send_file1);
299     send_file1 = 0;
300     if (send_file1_name != NULL) {
301       unlink (send_file1_name);
302       free (send_file1_name);
303       send_file1_name = NULL;
304     }
305   } else {
306     eprintf ("No messaging setup", __FILE__, __LINE__);
307   }
308 }