/
engine_unittest.c
265 lines (222 loc) · 6.09 KB
/
engine_unittest.c
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
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
*
* unit tests for PGM engine.
*
* Copyright (c) 2009 Miru Limited.
*
* 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
*/
/* getprotobyname_r */
#define _BSD_SOURCE 1
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <glib.h>
#include <check.h>
#ifdef _WIN32
# define PGM_CHECK_NOFORK 1
#endif
/* mock state */
struct pgm_rwlock_t;
struct pgm_slist_t;
static gint mock_time_init = 0;
static struct pgm_rwlock_t mock_pgm_sock_list_lock;
static struct pgm_slist_t* mock_pgm_sock_list = NULL;
#define pgm_time_init mock_pgm_time_init
#define pgm_time_shutdown mock_pgm_time_shutdown
#define pgm_close mock_pgm_close
#define pgm_sock_list_lock mock_pgm_sock_list_lock
#define pgm_sock_list mock_pgm_sock_list
#define ENGINE_DEBUG
#include "engine.c"
static
void
mock_setup (void)
{
mock_time_init = FALSE;
}
static
void
mock_teardown (void)
{
// null
}
/* mock functions for external references */
size_t
pgm_pkt_offset (
const bool can_fragment,
const sa_family_t pgmcc_family /* 0 = disable */
)
{
return 0;
}
PGM_GNUC_INTERNAL
int
pgm_get_nprocs (void)
{
return 1;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_time_init (
pgm_error_t** error
)
{
mock_time_init++;
return TRUE;
}
PGM_GNUC_INTERNAL
bool
mock_pgm_time_shutdown (void)
{
if (!mock_time_init)
return FALSE;
mock_time_init--;
return TRUE;
}
bool
mock_pgm_close (
pgm_sock_t* sock,
bool flush
)
{
return TRUE;
}
/* target:
* bool
* pgm_init (pgm_error_t** error)
*/
/* reference counting on init */
START_TEST (test_init_pass_001)
{
fail_unless (TRUE == pgm_init (NULL), "init failed");
fail_unless (TRUE == pgm_init (NULL), "init failed");
#ifdef PGM_CHECK_NOFORK
/* clean up state */
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (FALSE == pgm_shutdown (), "shutdown failed");
#endif
}
END_TEST
/* timing module already init */
START_TEST (test_init_pass_003)
{
pgm_error_t* err = NULL;
fail_unless (TRUE == pgm_time_init (&err), "time-init failed: %s", (err && err->message) ? err->message : "(null)");
fail_unless (TRUE == pgm_init (&err), "init failed: %s", (err && err->message) ? err->message : "(null)");
fail_unless (TRUE == pgm_init (&err), "init failed: %s", (err && err->message) ? err->message : "(null)");
#ifdef PGM_CHECK_NOFORK
/* clean up state */
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (FALSE == pgm_shutdown (), "shutdown failed");
fail_unless (TRUE == pgm_time_shutdown (), "time-shutdown failed");
fail_unless (FALSE == pgm_time_shutdown (), "time-shutdown failed");
#endif
}
END_TEST
/* target:
* bool
* pgm_shutdown (void)
*/
START_TEST (test_shutdown_pass_001)
{
fail_unless (TRUE == pgm_init (NULL), "init failed");
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
#ifdef PGM_CHECK_NOFORK
fail_unless (FALSE == pgm_shutdown (), "shutdown failed");
#endif
}
END_TEST
/* no init */
START_TEST (test_shutdown_pass_002)
{
fail_unless (FALSE == pgm_shutdown (), "shutdown failed");
}
END_TEST
/* double call */
START_TEST (test_shutdown_pass_003)
{
fail_unless (TRUE == pgm_init (NULL), "init failed");
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (FALSE == pgm_shutdown (), "shutdown failed");
}
END_TEST
/* check reference counting */
START_TEST (test_shutdown_pass_004)
{
fail_unless (TRUE == pgm_init (NULL), "init failed");
fail_unless (TRUE == pgm_init (NULL), "init failed");
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (FALSE == pgm_shutdown (), "shutdown failed");
}
END_TEST
/* target:
* bool
* pgm_supported (void)
*/
START_TEST (test_supported_pass_001)
{
fail_unless (FALSE == pgm_supported(), "supported failed");
fail_unless (TRUE == pgm_init (NULL), "init failed");
fail_unless (TRUE == pgm_supported(), "supported failed");
fail_unless (TRUE == pgm_shutdown (), "shutdown failed");
fail_unless (FALSE == pgm_supported(), "supported failed");
}
END_TEST
static
Suite*
make_test_suite (void)
{
Suite* s;
s = suite_create (__FILE__);
TCase* tc_init = tcase_create ("init");
tcase_add_checked_fixture (tc_init, mock_setup, mock_teardown);
suite_add_tcase (s, tc_init);
tcase_add_test (tc_init, test_init_pass_001);
tcase_add_test (tc_init, test_init_pass_003);
TCase* tc_shutdown = tcase_create ("shutdown");
tcase_add_checked_fixture (tc_shutdown, mock_setup, mock_teardown);
suite_add_tcase (s, tc_shutdown);
tcase_add_test (tc_shutdown, test_shutdown_pass_001);
tcase_add_test (tc_shutdown, test_shutdown_pass_002);
tcase_add_test (tc_shutdown, test_shutdown_pass_003);
tcase_add_test (tc_shutdown, test_shutdown_pass_004);
TCase* tc_supported = tcase_create ("supported");
tcase_add_checked_fixture (tc_supported, mock_setup, mock_teardown);
suite_add_tcase (s, tc_supported);
tcase_add_test (tc_supported, test_supported_pass_001);
return s;
}
static
Suite*
make_master_suite (void)
{
Suite* s = suite_create ("Master");
return s;
}
int
main (void)
{
SRunner* sr = srunner_create (make_master_suite ());
srunner_add_suite (sr, make_test_suite ());
srunner_run_all (sr, CK_ENV);
int number_failed = srunner_ntests_failed (sr);
srunner_free (sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
/* eof */