forked from libcheck/check
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_check_fork.c
More file actions
172 lines (146 loc) · 3.97 KB
/
check_check_fork.c
File metadata and controls
172 lines (146 loc) · 3.97 KB
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
/*
* 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., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "../lib/libcompat.h"
#include <sys/types.h>
#include <stdlib.h>
#include <check.h>
#include "check_check.h"
static int counter;
static pid_t mypid;
static void fork_sub_setup (void)
{
counter = 0;
mypid = getpid();
}
START_TEST(test_inc)
{
counter++;
}
END_TEST
START_TEST(test_nofork_sideeffects)
{
ck_assert_msg(counter == 1,
"Side effects not seen across tests");
}
END_TEST
START_TEST(test_nofork_pid)
{
ck_assert_msg(mypid == getpid(),
"Unit test is in a different adresss space from setup code");
}
END_TEST
static Suite *make_fork_sub_suite (void)
{
Suite *s;
TCase *tc;
s = suite_create("Fork Sub");
tc = tcase_create("Core");
suite_add_tcase (s, tc);
tcase_add_unchecked_fixture(tc, fork_sub_setup,NULL);
tcase_add_test(tc,test_inc);
tcase_add_test(tc,test_nofork_sideeffects);
tcase_add_test(tc,test_nofork_pid);
return s;
}
static SRunner *fork_sr;
static SRunner *fork_dummy_sr;
void fork_setup (void)
{
fork_sr = srunner_create(make_fork_sub_suite());
fork_dummy_sr = srunner_create (make_fork_sub_suite());
srunner_set_fork_status(fork_sr,CK_NOFORK);
srunner_run_all(fork_sr,CK_VERBOSE);
}
void fork_teardown (void)
{
srunner_free(fork_sr);
}
START_TEST(test_default_fork)
{
#if defined(HAVE_FORK) && HAVE_FORK == 1
ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_FORK,
"Default fork status not set correctly");
#else
ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_NOFORK,
"Default fork status not set correctly");
#endif /* HAVE_FORK */
}
END_TEST
START_TEST(test_set_nofork)
{
srunner_set_fork_status(fork_dummy_sr, CK_NOFORK);
ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_NOFORK,
"Fork status not changed correctly");
}
END_TEST
/*
* The following tests will fail if fork is unavailable, as
* attempting to set the fork mode as anything but
* CK_NOFORK is considered an error.
*/
#if defined(HAVE_FORK) && HAVE_FORK==1
START_TEST(test_set_fork)
{
srunner_set_fork_status(fork_dummy_sr, CK_FORK);
ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_FORK,
"Fork status not changed correctly");
}
END_TEST
START_TEST(test_env)
{
char envvar[] = "CK_FORK=no";
putenv(envvar);
ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_NOFORK,
"Fork status does not obey environment variable");
}
END_TEST
START_TEST(test_env_and_set)
{
char envvar[] = "CK_FORK=no";
putenv(envvar);
srunner_set_fork_status(fork_dummy_sr, CK_FORK);
ck_assert_msg(srunner_fork_status(fork_dummy_sr) == CK_FORK,
"Explicit setting of fork status should override env");
}
END_TEST
#endif /* HAVE_FORK */
START_TEST(test_nofork)
{
ck_assert_msg(srunner_ntests_failed(fork_sr) == 0,
"Errors on nofork test");
}
END_TEST
Suite *make_fork_suite(void)
{
Suite *s;
TCase *tc;
s = suite_create("Fork");
tc = tcase_create("Core");
suite_add_tcase(s, tc);
tcase_add_test(tc,test_default_fork);
tcase_add_test(tc,test_set_nofork);
#if defined(HAVE_FORK) && HAVE_FORK==1
tcase_add_test(tc,test_set_fork);
tcase_add_test(tc,test_env);
tcase_add_test(tc,test_env_and_set);
#endif /* HAVE_FORK */
tcase_add_test(tc,test_nofork);
return s;
}