summaryrefslogtreecommitdiff
path: root/libs/gst/check/libcheck/check_msg.c
blob: bf4099712f51c71cce0741a6aef74b816367eedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Check: a unit test framework for C
 * Copyright (C) 2001 2002, Arien Malec
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

#include "check_error.h"
#include "check.h"
#include "check_list.h"
#include "check_impl.h"
#include "check_msg.h"
#include "check_pack.h"


/* 'Pipe' is implemented as a temporary file to overcome message
 * volume limitations outlined in bug #482012. This scheme works well
 * with the existing usage wherein the parent does not begin reading
 * until the child has done writing and exited.
 *
 * Pipe life cycle:
 * - The parent creates a tmpfile().
 * - The fork() call has the effect of duplicating the file descriptor
 *   and copying (on write) the FILE* data structures.
 * - The child writes to the file, and its dup'ed file descriptor and
 *   data structures are cleaned up on child process exit.
 * - Before reading, the parent rewind()'s the file to reset both
 *   FILE* and underlying file descriptor location data.
 * - When finished, the parent fclose()'s the FILE*, deleting the
 *   temporary file, per tmpfile()'s semantics.
 *
 * This scheme may break down if the usage changes to asynchronous
 * reading and writing.
 */

static FILE *send_file1;
static FILE *send_file2;

static FILE *get_pipe (void);
static void setup_pipe (void);
static void teardown_pipe (void);
static TestResult *construct_test_result (RcvMsg * rmsg, int waserror);
static void tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx,
    RcvMsg * rmsg);
static FILE *
get_pipe (void)
{
  if (send_file2 != 0) {
    return send_file2;
  }

  if (send_file1 != 0) {
    return send_file1;
  }
  printf ("send_file1=%p,send_file2=%p", send_file1, send_file2);
  eprintf ("No messaging setup", __FILE__, __LINE__);

  return NULL;
}

void
send_failure_info (const char *msg)
{
  FailMsg fmsg;

  fmsg.msg = (char *) msg;
  ppack (fileno (get_pipe ()), CK_MSG_FAIL, (CheckMsg *) & fmsg);
}

void
send_loc_info (const char *file, int line)
{
  LocMsg lmsg;

  lmsg.file = (char *) file;
  lmsg.line = line;
  ppack (fileno (get_pipe ()), CK_MSG_LOC, (CheckMsg *) & lmsg);
}

void
send_ctx_info (enum ck_result_ctx ctx)
{
  CtxMsg cmsg;

  cmsg.ctx = ctx;
  ppack (fileno (get_pipe ()), CK_MSG_CTX, (CheckMsg *) & cmsg);
}

TestResult *
receive_test_result (int waserror)
{
  FILE *fp;
  RcvMsg *rmsg;
  TestResult *result;

  fp = get_pipe ();
  if (fp == NULL)
    eprintf ("Error in call to get_pipe", __FILE__, __LINE__ - 2);
  rewind (fp);
  rmsg = punpack (fileno (fp));
  teardown_pipe ();
  setup_pipe ();

  result = construct_test_result (rmsg, waserror);
  rcvmsg_free (rmsg);
  return result;
}

static void
tr_set_loc_by_ctx (TestResult * tr, enum ck_result_ctx ctx, RcvMsg * rmsg)
{
  if (ctx == CK_CTX_TEST) {
    tr->file = rmsg->test_file;
    tr->line = rmsg->test_line;
    rmsg->test_file = NULL;
    rmsg->test_line = -1;
  } else {
    tr->file = rmsg->fixture_file;
    tr->line = rmsg->fixture_line;
    rmsg->fixture_file = NULL;
    rmsg->fixture_line = -1;
  }
}

static TestResult *
construct_test_result (RcvMsg * rmsg, int waserror)
{
  TestResult *tr;

  if (rmsg == NULL)
    return NULL;

  tr = tr_create ();

  if (rmsg->msg != NULL || waserror) {
    tr->ctx = (cur_fork_status () == CK_FORK) ? rmsg->lastctx : rmsg->failctx;
    tr->msg = rmsg->msg;
    rmsg->msg = NULL;
    tr_set_loc_by_ctx (tr, tr->ctx, rmsg);
  } else if (rmsg->lastctx == CK_CTX_SETUP) {
    tr->ctx = CK_CTX_SETUP;
    tr->msg = NULL;
    tr_set_loc_by_ctx (tr, CK_CTX_SETUP, rmsg);
  } else {
    tr->ctx = CK_CTX_TEST;
    tr->msg = NULL;
    tr_set_loc_by_ctx (tr, CK_CTX_TEST, rmsg);
  }

  return tr;
}

void
setup_messaging (void)
{
  setup_pipe ();
}

void
teardown_messaging (void)
{
  teardown_pipe ();
}

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 ();
  }
}

static void
teardown_pipe (void)
{
  if (send_file2 != 0) {
    fclose (send_file2);
    send_file2 = 0;
  } else if (send_file1 != 0) {
    fclose (send_file1);
    send_file1 = 0;
  } else {
    eprintf ("No messaging setup", __FILE__, __LINE__);
  }
}