2 * Copyright (C) 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
30 #include "../task_p.h"
37 /* task event handler, sets a boolean to true */
42 set(isc_task_t *task, isc_event_t *event) {
43 int *value = (int *) event->ev_arg;
47 isc_event_free(&event);
54 set_and_drop(isc_task_t *task, isc_event_t *event) {
55 int *value = (int *) event->ev_arg;
59 isc_event_free(&event);
61 *value = (int) isc_taskmgr_mode(taskmgr);
64 isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_normal);
68 * Individual unit tests
73 ATF_TC_HEAD(create_task, tc) {
74 atf_tc_set_md_var(tc, "descr", "create and destroy a task");
76 ATF_TC_BODY(create_task, tc) {
78 isc_task_t *task = NULL;
82 result = isc_test_begin(NULL, ISC_TRUE);
83 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
85 result = isc_task_create(taskmgr, 0, &task);
86 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
88 isc_task_destroy(&task);
89 ATF_REQUIRE_EQ(task, NULL);
96 ATF_TC_HEAD(all_events, tc) {
97 atf_tc_set_md_var(tc, "descr", "process task events");
99 ATF_TC_BODY(all_events, tc) {
101 isc_task_t *task = NULL;
110 result = isc_mutex_init(&set_lock);
111 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
113 result = isc_test_begin(NULL, ISC_TRUE);
114 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
116 result = isc_task_create(taskmgr, 0, &task);
117 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
120 event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST,
121 set, &a, sizeof (isc_event_t));
122 ATF_REQUIRE(event != NULL);
125 isc_task_send(task, &event);
127 event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST,
128 set, &b, sizeof (isc_event_t));
129 ATF_REQUIRE(event != NULL);
132 isc_task_send(task, &event);
134 while ((a == 0 || b == 0) && i++ < 5000) {
135 #ifndef ISC_PLATFORM_USETHREADS
136 while (isc__taskmgr_ready(taskmgr))
137 isc__taskmgr_dispatch(taskmgr);
145 isc_task_destroy(&task);
146 ATF_REQUIRE_EQ(task, NULL);
151 /* Privileged events */
152 ATF_TC(privileged_events);
153 ATF_TC_HEAD(privileged_events, tc) {
154 atf_tc_set_md_var(tc, "descr", "process privileged events");
156 ATF_TC_BODY(privileged_events, tc) {
158 isc_task_t *task1 = NULL, *task2 = NULL;
160 int a = 0, b = 0, c = 0, d = 0, e = 0;
166 result = isc_mutex_init(&set_lock);
167 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
169 result = isc_test_begin(NULL, ISC_TRUE);
170 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
172 #ifdef ISC_PLATFORM_USETHREADS
174 * Pause the task manager so we can fill up the work queue
175 * without things happening while we do it.
177 isc__taskmgr_pause(taskmgr);
180 result = isc_task_create(taskmgr, 0, &task1);
181 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
182 isc_task_setname(task1, "privileged", NULL);
183 ATF_CHECK(!isc_task_privilege(task1));
184 isc_task_setprivilege(task1, ISC_TRUE);
185 ATF_CHECK(isc_task_privilege(task1));
187 result = isc_task_create(taskmgr, 0, &task2);
188 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
189 isc_task_setname(task2, "normal", NULL);
190 ATF_CHECK(!isc_task_privilege(task2));
192 /* First event: privileged */
193 event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
194 set, &a, sizeof (isc_event_t));
195 ATF_REQUIRE(event != NULL);
198 isc_task_send(task1, &event);
200 /* Second event: not privileged */
201 event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
202 set, &b, sizeof (isc_event_t));
203 ATF_REQUIRE(event != NULL);
206 isc_task_send(task2, &event);
208 /* Third event: privileged */
209 event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
210 set, &c, sizeof (isc_event_t));
211 ATF_REQUIRE(event != NULL);
214 isc_task_send(task1, &event);
216 /* Fourth event: privileged */
217 event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
218 set, &d, sizeof (isc_event_t));
219 ATF_REQUIRE(event != NULL);
222 isc_task_send(task1, &event);
224 /* Fifth event: not privileged */
225 event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
226 set, &e, sizeof (isc_event_t));
227 ATF_REQUIRE(event != NULL);
230 isc_task_send(task2, &event);
232 ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
233 isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
234 ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);
236 #ifdef ISC_PLATFORM_USETHREADS
237 isc__taskmgr_resume(taskmgr);
240 /* We're waiting for *all* variables to be set */
241 while ((a == 0 || b == 0 || c == 0 || d == 0 || e == 0) && i++ < 5000) {
242 #ifndef ISC_PLATFORM_USETHREADS
243 while (isc__taskmgr_ready(taskmgr))
244 isc__taskmgr_dispatch(taskmgr);
250 * We can't guarantee what order the events fire, but
251 * we do know the privileged tasks that set a, c, and d
252 * would have fired first.
258 /* ...and the non-privileged tasks that set b and e, last */
262 ATF_CHECK_EQ(counter, 6);
264 isc_task_setprivilege(task1, ISC_FALSE);
265 ATF_CHECK(!isc_task_privilege(task1));
267 ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
269 isc_task_destroy(&task1);
270 ATF_REQUIRE_EQ(task1, NULL);
271 isc_task_destroy(&task2);
272 ATF_REQUIRE_EQ(task2, NULL);
278 * Edge case: this tests that the task manager behaves as expected when
279 * we explicitly set it into normal mode *while* running privileged.
281 ATF_TC(privilege_drop);
282 ATF_TC_HEAD(privilege_drop, tc) {
283 atf_tc_set_md_var(tc, "descr", "process privileged events");
285 ATF_TC_BODY(privilege_drop, tc) {
287 isc_task_t *task1 = NULL, *task2 = NULL;
289 int a = -1, b = -1, c = -1, d = -1, e = -1; /* non valid states */
295 result = isc_mutex_init(&set_lock);
296 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
298 result = isc_test_begin(NULL, ISC_TRUE);
299 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
301 #ifdef ISC_PLATFORM_USETHREADS
303 * Pause the task manager so we can fill up the work queue
304 * without things happening while we do it.
306 isc__taskmgr_pause(taskmgr);
309 result = isc_task_create(taskmgr, 0, &task1);
310 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
311 isc_task_setname(task1, "privileged", NULL);
312 ATF_CHECK(!isc_task_privilege(task1));
313 isc_task_setprivilege(task1, ISC_TRUE);
314 ATF_CHECK(isc_task_privilege(task1));
316 result = isc_task_create(taskmgr, 0, &task2);
317 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
318 isc_task_setname(task2, "normal", NULL);
319 ATF_CHECK(!isc_task_privilege(task2));
321 /* First event: privileged */
322 event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
323 set_and_drop, &a, sizeof (isc_event_t));
324 ATF_REQUIRE(event != NULL);
327 isc_task_send(task1, &event);
329 /* Second event: not privileged */
330 event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
331 set_and_drop, &b, sizeof (isc_event_t));
332 ATF_REQUIRE(event != NULL);
335 isc_task_send(task2, &event);
337 /* Third event: privileged */
338 event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
339 set_and_drop, &c, sizeof (isc_event_t));
340 ATF_REQUIRE(event != NULL);
343 isc_task_send(task1, &event);
345 /* Fourth event: privileged */
346 event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
347 set_and_drop, &d, sizeof (isc_event_t));
348 ATF_REQUIRE(event != NULL);
351 isc_task_send(task1, &event);
353 /* Fifth event: not privileged */
354 event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
355 set_and_drop, &e, sizeof (isc_event_t));
356 ATF_REQUIRE(event != NULL);
359 isc_task_send(task2, &event);
361 ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
362 isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
363 ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);
365 #ifdef ISC_PLATFORM_USETHREADS
366 isc__taskmgr_resume(taskmgr);
369 /* We're waiting for all variables to be set. */
370 while ((a == -1 || b == -1 || c == -1 || d == -1 || e == -1) &&
372 #ifndef ISC_PLATFORM_USETHREADS
373 while (isc__taskmgr_ready(taskmgr))
374 isc__taskmgr_dispatch(taskmgr);
380 * We can't guarantee what order the events fire, but
381 * we do know *exactly one* of the privileged tasks will
382 * have run in privileged mode...
384 ATF_CHECK(a == isc_taskmgrmode_privileged ||
385 c == isc_taskmgrmode_privileged ||
386 d == isc_taskmgrmode_privileged);
387 ATF_CHECK(a + c + d == isc_taskmgrmode_privileged);
389 /* ...and neither of the non-privileged tasks did... */
390 ATF_CHECK(b == isc_taskmgrmode_normal || e == isc_taskmgrmode_normal);
392 /* ...but all five of them did run. */
393 ATF_CHECK_EQ(counter, 6);
395 ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
397 isc_task_destroy(&task1);
398 ATF_REQUIRE_EQ(task1, NULL);
399 isc_task_destroy(&task2);
400 ATF_REQUIRE_EQ(task2, NULL);
409 ATF_TP_ADD_TC(tp, create_task);
410 ATF_TP_ADD_TC(tp, all_events);
411 ATF_TP_ADD_TC(tp, privileged_events);
412 ATF_TP_ADD_TC(tp, privilege_drop);
414 return (atf_no_error());