File: test.c

package info (click to toggle)
autounit 0.20.1-5
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,700 kB
  • sloc: sh: 8,777; ansic: 2,606; makefile: 119
file content (374 lines) | stat: -rw-r--r-- 10,126 bytes parent folder | download | duplicates (5)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* Autounit test
 * Copyright (C) 2001-2002  Simon Janes
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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 <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#include <glib.h>

#include <au-netstring.h>
#include <autounit.h>
#include <autounit-private.h>

static gint au_default_fail_method(autounit_test_t *t, char *msg);
static gint au_default_succeed_method(autounit_test_t *t, char *msg);

autounit_test_t*
au_new_test(GString *testName,
	    autounit_test_fp_t test_fp)
{
  autounit_test_t *new_t = g_new(autounit_test_t, 1);
  new_t->test_name = g_string_new(testName->str);
  new_t->test_status = g_string_new("");
  new_t->test_run = FALSE;
  new_t->total_assertions = 0;
  new_t->failed_assertions = 0;
  new_t->test_seconds_elapsed = 0;
  new_t->test_useconds_elapsed = 0;
  new_t->test_fp = test_fp;
  new_t->do_fork = TRUE;
  new_t->suite = (autounit_suite_t*)0;
  new_t->fail_fp = au_default_fail_method;
  new_t->succeed_fp = au_default_succeed_method;
  new_t->test_data = (gpointer)0;
  new_t->ref_count = 0;
  return new_t;
}

/**
 * Free's memory used by test, please don't call this on anything
 * still linked into a suite, I'm trying to decide if it should call
 * remove for the user before it deletes.
 */
void
au_delete_test(autounit_test_t *t)
{
    if(t->ref_count <= 0)
    {
        g_string_free(t->test_name, TRUE);
        t->test_name = 0;
        g_string_free(t->test_status, TRUE);
        t->test_status = 0;
        g_free(t);
    }
}

void
au_test_set_fork_mode(autounit_test_t *t, gboolean mode)
{
  t->do_fork = mode;
}

gboolean
au_test_get_fork_mode(autounit_test_t *t)
{
    return t->do_fork;
}

gint
au_test_ref(autounit_test_t *t)
{
    return ++t->ref_count;
}

gint
au_test_unref(autounit_test_t *t)
{
    t->ref_count--;
    au_delete_test(t);
    return t->ref_count;
}

void
au_test_append_msg(autounit_test_t *t, char *msg)
{
    t->test_status = g_string_append(t->test_status, msg);
}

static gint
au_default_fail_method(autounit_test_t *t, char *msg)
{
    au_test_append_msg(t, msg);
    t->failed_assertions++;
    t->total_assertions++;
    return 1;
}

static gint
au_default_succeed_method(autounit_test_t *t, char *msg)
{
    t->total_assertions++;
    return 1;
}


/**
 * Convert a test structure into a stream of netstrings to pass back
 * via the pipe to the test case parent process.  We omit pointers.
 * This collection of netstrings itself then becomes one big
 * netstring.  In the future, it might be the case that Autounit
 * controls a farm of crosscompile machines... you never know. This
 * is the simplest thing I can think of that will satisfy being
 * reliable now and in the future.
 */
GString *
au_test_serialize(autounit_test_t *t)
{
  /**
   * the fields we care about...
   * GString *test_name;
   * gboolean test_run;
   * GString *test_status;
   * gint     failed_assertions;
   * gdouble  test_seconds_elapsed;
   * gulong   test_useconds_elapsed;
  */

  GString *str;
  GString *netstr;
  GString *tmp_test_name_ns;
  GString *tmp_test_run_ns;
  GString *tmp_test_status_ns;
  GString *tmp_failed_assertions_ns;
  GString *tmp_test_seconds_elapsed_ns;
  GString *tmp_test_useconds_elapsed_ns;
  
  tmp_test_name_ns = au_gstring_to_netstring(t->test_name);
  tmp_test_run_ns  = au_gboolean_to_netstring(t->test_run);
  tmp_test_status_ns = au_gstring_to_netstring(t->test_status);
  tmp_failed_assertions_ns = au_gint_to_netstring(t->failed_assertions);
  tmp_test_seconds_elapsed_ns = au_gdouble_to_netstring(
      t->test_seconds_elapsed);
  tmp_test_useconds_elapsed_ns = au_gulong_to_netstring(
      t->test_useconds_elapsed);

  str = g_string_new("");
  str = g_string_append(str,tmp_test_name_ns->str);
  str = g_string_append(str,tmp_test_run_ns->str);
  str = g_string_append(str,tmp_test_status_ns->str);
  str = g_string_append(str,tmp_failed_assertions_ns->str);
  str = g_string_append(str,tmp_test_seconds_elapsed_ns->str);
  str = g_string_append(str,tmp_test_useconds_elapsed_ns->str);
  netstr = au_gstring_to_netstring(str);

  g_string_free(str,TRUE);
  g_string_free(tmp_test_name_ns,TRUE);
  g_string_free(tmp_test_run_ns,TRUE);
  g_string_free(tmp_test_status_ns,TRUE);
  g_string_free(tmp_failed_assertions_ns,TRUE);
  g_string_free(tmp_test_seconds_elapsed_ns,TRUE);
  g_string_free(tmp_test_useconds_elapsed_ns,TRUE);

  return netstr;
}

autounit_test_t*
au_test_unserialize(GString *nst)
{
  /**
   * The fields we expect
   * string test_name
   * boolean test_run
   * string test_status
   * int failed_assertions
   * double test_seconds_elapsed
   * ulong test_useconds_elapsed
   */

  GString *test_name_ns;
  GString *test_run_ns;
  GString *test_status_ns;
  GString *failed_assertions_ns;
  GString *test_seconds_elapsed_ns;
  GString *test_useconds_elapsed_ns;
  GString *test_name;
  GString *test_status;
  autounit_test_t *us_test;
  GString *tmp_ns = g_string_new(nst->str);

  test_name_ns = au_pop_netstring(tmp_ns);
  test_run_ns = au_pop_netstring(tmp_ns);
  test_status_ns = au_pop_netstring(tmp_ns);
  failed_assertions_ns = au_pop_netstring(tmp_ns);
  test_seconds_elapsed_ns = au_pop_netstring(tmp_ns);
  test_useconds_elapsed_ns = au_pop_netstring(tmp_ns);
  
  test_name = au_netstring_to_gstring(test_name_ns);
  test_status = au_netstring_to_gstring(test_status_ns);
  
  us_test = au_new_test(test_name,0);
  us_test->test_run = au_netstring_to_gboolean(test_run_ns);
  g_string_append(us_test->test_status, test_status->str);
  us_test->failed_assertions = au_netstring_to_gint(failed_assertions_ns);
  us_test->test_seconds_elapsed = au_netstring_to_gdouble(
      test_seconds_elapsed_ns);
  us_test->test_useconds_elapsed = au_netstring_to_gulong(
      test_useconds_elapsed_ns);
  
  g_string_free(tmp_ns,TRUE);
  g_string_free(test_name,TRUE);
  g_string_free(test_status,TRUE);
  g_string_free(failed_assertions_ns,TRUE);
  g_string_free(test_seconds_elapsed_ns,TRUE);
  g_string_free(test_useconds_elapsed_ns,TRUE);
  
  return us_test;
}

void
au_run_test(autounit_test_t *t)
{
  /* Because there was basically no difference between au_run_test and
  ** au_run_stress_test, I decided to make the debugged forking-au_run_test
  ** a private function and the other two some wrappers. */
  autounit_stress_report_t stress_report;
  gint i = 1;

  au_log_start("au_run_test");
  stress_report.round = &i;
  stress_report.modulo = 1;

  au_run_test_fork(t, &stress_report);
  au_log_end("au_run_test");
}

void
au_run_stress_test(autounit_test_t *t,
		   autounit_stress_report_t *stress_report)
{
  au_log_start("au_run_stress_test");
  au_run_test_fork(t,stress_report);
  au_log_end("au_run_stress_test");
}

static void
au_run_test_child(autounit_test_t *t)
{
  GTimer *elapsed_timer;

  elapsed_timer = g_timer_new();
  {
    autounit_test_fp_t su_fn = (autounit_test_setup_fp_t) t->suite->setup_fp;
    autounit_test_fp_t fn = (autounit_test_fp_t) t->test_fp;
    autounit_test_fp_t td_fn = (autounit_test_teardown_fp_t) t->suite->teardown_fp;
    if (su_fn) su_fn(t);
    fn(t);
    if (td_fn) td_fn(t);
  }
  g_timer_stop(elapsed_timer);
  t->test_seconds_elapsed = g_timer_elapsed(elapsed_timer,
					    &t->test_useconds_elapsed);
  t->test_run = TRUE;
  g_timer_destroy(elapsed_timer); 
}

void
au_run_test_fork(autounit_test_t *t,
		 autounit_stress_report_t *status)
{
  /* start timer
  ** fork process
  ** in parent process:
  **   wait on child to return results and exit
  ** in new process:
  **   invoke setup_fp
  **   call test_fp
  **   call teardown_fp
  **   return results to parent process
  */
  au_log_start("au_run_test");

  if (t->do_fork) {
    int fork_result;
    int C2P_pipe[2];
    if (pipe(C2P_pipe) < 0) {
      au_assert(t, 0, _("pipe failed"));
      return;
    }

    switch(fork_result = fork()) {
    case -1:
      au_assert(t, 0, _("fork failed"));
      return;

    case 0:
      {
	/* Child */
	GString *child_test_result;
	au_run_test_child(t);
	child_test_result = au_test_serialize(t);
	close(C2P_pipe[0]);
	write(C2P_pipe[1],child_test_result->str,child_test_result->len);
	close(C2P_pipe[1]);
	au_log_end("au_run_test");
	exit(0);
      }

    default:
      {
	/* Parent */
	gint childstatus;
	close(C2P_pipe[1]);
	waitpid(fork_result, &childstatus, 0);
    
	if (WIFEXITED(childstatus)) {
	  autounit_test_t *tmp_t;
	  GString *parent_test_result = au_read_netstring(C2P_pipe[0]);
	  tmp_t = au_test_unserialize(parent_test_result);
    
	  /* Copy child's assertions to the parent's copy, we'll assume
	     that the test_name didn't change. */
	  t->failed_assertions += tmp_t->failed_assertions;
	  t->test_seconds_elapsed = tmp_t->test_seconds_elapsed;
	  t->test_useconds_elapsed = tmp_t->test_useconds_elapsed;
	  t->test_status = g_string_append(t->test_status,
                                           tmp_t->test_status->str);
          au_delete_test(tmp_t);
	} else {
	  if (WIFSIGNALED(childstatus)) {
	    au_assert(t, 0, _("test failed with signal"));
	  }
#ifdef WCOREDUMP          
	  if (WCOREDUMP(childstatus)) {
            t->failed_assertions++;
	    au_assert(t, 0, _("test failed with core dump"));
	  }
#endif          
	}
      }
    }
  }
  else {
    au_run_test_child(t);
  }
  
  if (t->failed_assertions > 0) {
    g_print(_("!"));
  } else {
    if ((*(status->round) % status->modulo) == 0) {
      g_print(_("."));
    }
  }
}