~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/tests/test_util.h

  • Committer: Steve Langasek
  • Date: 2013-11-07 03:06:51 UTC
  • Revision ID: steve.langasek@canonical.com-20131107030651-f1jzeyi7ifvvw1h8
Attempt to cherry-pick fixes for bug #1199778 to raring; something is still
missing though, the test suite segfaults on one of the json loads.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#ifndef TEST_UTIL_H
2
2
#define TEST_UTIL_H
3
3
 
4
 
#include <string.h>
5
4
#include "event_operator.h"
6
 
 
7
 
/* TEST_ENSURE_CLEAN_ENV:
8
 
 *
9
 
 * Ensure the environment is as pristine as possible (to avoid follow-on
10
 
 * errors caused by not freeing objects in a previous test, say)
11
 
 */
12
 
#define TEST_ENSURE_CLEAN_ENV()                                      \
13
 
{                                                                    \
14
 
        setvbuf(stdout, NULL, _IONBF, 0);                            \
15
 
                                                                     \
16
 
        if (job_classes) {                                           \
17
 
                TEST_HASH_EMPTY (job_classes);                       \
18
 
        }                                                            \
19
 
                                                                     \
20
 
        if (conf_sources) {                                          \
21
 
                TEST_LIST_EMPTY (conf_sources);                      \
22
 
        }                                                            \
23
 
                                                                     \
24
 
        if (nih_io_watches) {                                        \
25
 
                TEST_LIST_EMPTY (nih_io_watches);                    \
26
 
        }                                                            \
27
 
                                                                     \
28
 
        if (nih_timers) {                                            \
29
 
                TEST_LIST_EMPTY (nih_timers);                        \
30
 
        }                                                            \
31
 
                                                                     \
32
 
        if (nih_child_watches) {                                     \
33
 
                TEST_LIST_EMPTY (nih_child_watches);                 \
34
 
        }                                                            \
35
 
                                                                     \
36
 
        if (events) {                                                \
37
 
                TEST_LIST_EMPTY (events);                            \
38
 
        }                                                            \
39
 
}
40
 
 
41
 
/**
42
 
 * _TEST_WATCH_UPDATE:
43
 
 * @force: if TRUE, force an update,
44
 
 * @timeout: struct timeval pointer, or NULL if no timeout required.
45
 
 *
46
 
 * Request NIH look for a file event relating to any NihIo objects,
47
 
 * with an optional timeout. Behaviour can be forced via @force.
48
 
 **/
49
 
#define _TEST_WATCH_UPDATE(force, timeout)                           \
50
 
{                                                                    \
51
 
        int         nfds = 0;                                        \
52
 
        int         ret = 0;                                         \
53
 
        fd_set      readfds, writefds, exceptfds;                    \
54
 
                                                                     \
55
 
        FD_ZERO (&readfds);                                          \
56
 
        FD_ZERO (&writefds);                                         \
57
 
        FD_ZERO (&exceptfds);                                        \
58
 
                                                                     \
59
 
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);  \
60
 
        if (! force) {                                               \
61
 
          ret = select (nfds, &readfds, &writefds,                   \
62
 
                        &exceptfds, timeout);                        \
63
 
        }                                                            \
64
 
        if (force || ret > 0)                                        \
65
 
                nih_io_handle_fds (&readfds, &writefds, &exceptfds); \
66
 
}
67
 
 
68
 
/**
69
 
 * TEST_WATCH_UPDATE:
70
 
 *
71
 
 * Request NIH look for a file event relating to any NihIo objects,
72
 
 * */
73
 
#define TEST_WATCH_UPDATE()                                          \
74
 
        _TEST_WATCH_UPDATE (0, NULL)
75
 
 
76
 
/**
77
 
 * TEST_WATCH_UPDATE_TIMEOUT:
78
 
 * @timeout: struct timeval pointer.
79
 
 *
80
 
 * Request NIH look for a file event relating to any NihIo objects
81
 
 * within time period @timeout.
82
 
 **/
83
 
#define TEST_WATCH_UPDATE_TIMEOUT(timeout)                           \
84
 
        _TEST_WATCH_UPDATE (0, timeout)
85
 
 
86
 
/**
87
 
 * TEST_WATCH_UPDATE_TIMEOUT_SECS:
88
 
 * @secs: seconds to wait before timeout.
89
 
 *
90
 
 * Request NIH look for a file event relating to any NihIo objects
91
 
 * within @secs timeout.
92
 
 **/
93
 
#define TEST_WATCH_UPDATE_TIMEOUT_SECS(secs)                         \
94
 
{                                                                    \
95
 
        struct timeval _t;                                           \
96
 
        _t.tv_sec  = secs;                                           \
97
 
        _t.tv_usec = 0;                                              \
98
 
        _TEST_WATCH_UPDATE (0, &_t);                                 \
99
 
}
100
 
 
101
 
/**
102
 
 * TEST_FORCE_WATCH_UPDATE:
103
 
 *
104
 
 * Force NIH to look for a file event relating to any NihIo objects.
105
 
 **/
106
 
#define TEST_FORCE_WATCH_UPDATE()                                    \
107
 
        _TEST_WATCH_UPDATE (1, NULL)
108
 
 
109
 
/**
110
 
 * TEST_FORCE_WATCH_UPDATE_TIMEOUT:
111
 
 * @timeout: struct timeval pointer.
112
 
 *
113
 
 * Force NIH to look for a file event relating to any NihIo objects
114
 
 * within time period @timeout.
115
 
 **/
116
 
#define TEST_FORCE_WATCH_UPDATE_TIMEOUT(timeout)                     \
117
 
        _TEST_WATCH_UPDATE (1, timeout)
118
 
 
119
 
/**
120
 
 * TEST_FORCE_WATCH_UPDATE_TIMEOUT_SECS:
121
 
 * @timeout: struct timeval pointer.
122
 
 *
123
 
 * Force NIH to look for a file event relating to any NihIo objects
124
 
 * within time period @timeout.
125
 
 **/
126
 
#define TEST_FORCE_WATCH_UPDATE_TIMEOUT_SECS(secs)                   \
127
 
{                                                                    \
128
 
        struct timeval _t;                                           \
129
 
        _t.tv_sec  = secs;                                           \
130
 
        _t.tv_usec = 0;                                              \
131
 
        _TEST_WATCH_UPDATE (1, &_t);                                 \
132
 
}
133
 
 
134
 
/**
135
 
 * ENSURE_DIRECTORY_EMPTY:
136
 
 * @path: Full path to a directory.
137
 
 *
138
 
 * Ensure specified directory is empty.
139
 
 **/
140
 
#define ENSURE_DIRECTORY_EMPTY(path)                                 \
141
 
{                                                                    \
142
 
        DIR            *dp = NULL;                                   \
143
 
        struct dirent  *file = NULL;                                 \
144
 
        int             count = 0;                                   \
145
 
                                                                     \
146
 
        dp = opendir (path);                                         \
147
 
        TEST_NE_P (dp, NULL);                                        \
148
 
                                                                     \
149
 
        while((file = readdir (dp))) {                               \
150
 
                if (!strcmp (".", file->d_name) ||                   \
151
 
                                !strcmp ("..", file->d_name))        \
152
 
                        continue;                                    \
153
 
                count++;                                             \
154
 
        }                                                            \
155
 
                                                                     \
156
 
        closedir (dp);                                               \
157
 
                                                                     \
158
 
        TEST_EQ (count, 0);                                          \
159
 
}
160
 
 
161
 
/**
162
 
 * obj_string_check:
163
 
 *
164
 
 * @a: first object,
165
 
 * @b: second object,
166
 
 * @name: name of string element.
167
 
 *
168
 
 * Compare string element @name in objects @a and @b.
169
 
 *
170
 
 * Returns: 0 if strings are identical
171
 
 * (or both NULL), else 1.
172
 
 **/
173
 
#define obj_string_check(a, b, name) \
174
 
        string_check ((a)->name, (b)->name)
175
 
 
176
 
/**
177
 
 * obj_num_check:
178
 
 *
179
 
 * @a: first object,
180
 
 * @b: second object.
181
 
 * @name: name of numeric element.
182
 
 *
183
 
 * Compare numeric element @name in objects @a and @b.
184
 
 *
185
 
 * Returns: 0 if @a and @b are identical, else 1.
186
 
 **/
187
 
#define obj_num_check(a, b, name) \
188
 
        (a->name != b->name)
189
 
 
190
 
/**
191
 
 * TEST_CMP_INT_ARRAYS:
192
 
 * @a: first array,
193
 
 * @b: second array,
194
 
 * @sizea: size of @a,
195
 
 * @sizeb: size of @b.
196
 
 *
197
 
 * Compare integer arrays @a and @b for equivalence.
198
 
 *
199
 
 * Returns: 0 if arrays are identical, else -1.
200
 
 **/
201
 
#define TEST_CMP_INT_ARRAYS(a, b, sizea, sizeb) \
202
 
({int ret = 0; \
203
 
 size_t __i; \
204
 
 if (sizea == sizeb) { \
205
 
         for (__i = 0; \
206
 
                 __i < sizea; \
207
 
                 __i++) { \
208
 
                if ((a)[__i] != (b)[__i]) { \
209
 
                        ret = -1; \
210
 
                        break; \
211
 
                } \
212
 
        } \
213
 
 } else \
214
 
        ret = -1; \
215
 
 ret;})
216
 
 
217
 
/**
218
 
 * TEST_CMP_STR_ARRAYS:
219
 
 * @a: first string array,
220
 
 * @b: second string array,
221
 
 * @sizea: length of @a, 
222
 
 * @sizeb: length of @b.
223
 
 *
224
 
 * Compare string arrays @a and @b for equivalence.
225
 
 *
226
 
 * Returns: 0 if arrays are identical, else -1.
227
 
 **/
228
 
#define TEST_CMP_STR_ARRAYS(a, b, sizea, sizeb) \
229
 
({ int ret = 0; \
230
 
 if (sizea == sizeb) { \
231
 
         for (size_t __i = 0; \
232
 
                 __i < sizea; \
233
 
                 __i++) { \
234
 
                if (strcmp (a[__i], b[__i])) { \
235
 
                        ret = -1; \
236
 
                        break; \
237
 
                } \
238
 
        } \
239
 
 } else \
240
 
        ret = -1; \
241
 
 ret;})
242
 
 
243
 
/**
244
 
 * TEST_TWO_LISTS_FOREACH:
245
 
 * @list1: entry in the first list to iterate,
246
 
 * @list2: entry in the second list to iterate,
247
 
 * @iter1: name of iterator variable for @list1,
248
 
 * @iter2: name of iterator variable for @list2.
249
 
 *
250
 
 * Dual version of NIH_LIST_FOREACH() which iterates 
251
 
 * two lists in tandem.
252
 
 **/
253
 
#define TEST_TWO_LISTS_FOREACH(list1, list2, iter1, iter2) \
254
 
        for (NihList *iter1 = (list1)->next, \
255
 
                     *iter2 = (list2)->next; \
256
 
                iter1 != (list1) && iter2 != (list2); \
257
 
                iter1 = iter1->next, \
258
 
                iter2 = iter2->next)
259
 
 
260
 
/**
261
 
 * TEST_TWO_HASHES_FOREACH:
262
 
 * @hash1: entry in the first hash to iterate,
263
 
 * @hash2: entry in the second hash to iterate,
264
 
 * @iter1: name of iterator variable for @hash1,
265
 
 * @iter2: name of iterator variable for @hash2.
266
 
 *
267
 
 * Dual version of NIH_HASH_FOREACH() which iterates
268
 
 * two hashes in tandem.
269
 
 **/
270
 
#define TEST_TWO_HASHES_FOREACH(hash1, hash2, iter1, iter2) \
271
 
        for (size_t _##iter##_i = 0; _##iter##_i < (hash1)->size; \
272
 
             _##iter##_i++) \
273
 
                TEST_TWO_LISTS_FOREACH (&(hash1)->bins[_##iter##_i], \
274
 
                                        &(hash2)->bins[_##iter##_i], \
275
 
                                iter1, iter2)
276
 
 
277
 
/**
278
 
 * TEST_TWO_TREES_FOREACH:
279
 
 * @tree1: root of the first tree to iterate,
280
 
 * @tree2: root of the second tree to iterate,
281
 
 * @iter1: name of iterator variable for @tree1,
282
 
 * @iter2: name of iterator variable for @tree2.
283
 
 *
284
 
 * Dual version of NIH_TREE_FOREACH() which walks
285
 
 * two trees in tandem.
286
 
 **/
287
 
#define TEST_TWO_TREES_FOREACH(tree1, tree2, iter1, iter2) \
288
 
        for (NihTree *iter1 = nih_tree_next (tree1, NULL), \
289
 
                        *iter2 = nih_tree_next (tree2, NULL); \
290
 
                        iter1 != NULL && iter2 != NULL; \
291
 
                        iter1 = nih_tree_next (tree1, iter1), \
292
 
                        iter2 = nih_tree_next (tree2, iter2))
293
 
 
294
 
 
295
 
/**
296
 
 * TEST_ARRAY_SIZE:
297
 
 * @array: array.
298
 
 * 
299
 
 * Determine size of specified array.
300
 
 *
301
 
 * Returns: array size.
302
 
 **/
303
 
#define TEST_ARRAY_SIZE(array) \
304
 
        (sizeof (array) / sizeof (array[0]))
 
5
#include "conf.h"
 
6
#include "job_class.h"
 
7
#include "session.h"
 
8
#include "log.h"
305
9
 
306
10
/* Prototypes */
307
 
int string_check (const char *a, const char *b)
308
 
        __attribute__ ((warn_unused_result));
309
 
 
310
11
int event_operator_diff (EventOperator *a, EventOperator *b)
311
12
        __attribute__ ((warn_unused_result));
312
13
 
313
 
Session * session_from_chroot (const char *chroot)
 
14
Session *session_from_chroot (const char *chroot)
314
15
        __attribute__ ((warn_unused_result));
315
16
 
 
17
void ensure_env_clean (void);
 
18
 
 
19
void clean_env (void);
 
20
 
316
21
#endif /* TEST_UTIL_H */