1275970Scy/* 2275970Scy * Copyright (C) 2011, 2012 Internet Systems Consortium, Inc. ("ISC") 3275970Scy * 4275970Scy * Permission to use, copy, modify, and/or distribute this software for any 5275970Scy * purpose with or without fee is hereby granted, provided that the above 6275970Scy * copyright notice and this permission notice appear in all copies. 7275970Scy * 8275970Scy * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 9275970Scy * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 10275970Scy * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 11275970Scy * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 12275970Scy * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 13275970Scy * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 14275970Scy * PERFORMANCE OF THIS SOFTWARE. 15275970Scy */ 16275970Scy 17275970Scy/* $Id$ */ 18275970Scy 19275970Scy/*! \file */ 20275970Scy 21275970Scy#include <config.h> 22275970Scy 23275970Scy#include <atf-c.h> 24275970Scy 25275970Scy#include <unistd.h> 26275970Scy 27275970Scy#include <isc/task.h> 28275970Scy#include <isc/taskpool.h> 29275970Scy 30275970Scy#include "isctest.h" 31275970Scy 32275970Scy/* 33275970Scy * Individual unit tests 34275970Scy */ 35275970Scy 36275970Scy/* Create a taskpool */ 37275970ScyATF_TC(create_pool); 38275970ScyATF_TC_HEAD(create_pool, tc) { 39275970Scy atf_tc_set_md_var(tc, "descr", "create a taskpool"); 40275970Scy} 41275970ScyATF_TC_BODY(create_pool, tc) { 42275970Scy isc_result_t result; 43275970Scy isc_taskpool_t *pool = NULL; 44275970Scy 45275970Scy UNUSED(tc); 46275970Scy 47275970Scy result = isc_test_begin(NULL, ISC_TRUE); 48275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 49275970Scy 50275970Scy result = isc_taskpool_create(taskmgr, mctx, 8, 2, &pool); 51275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 52275970Scy ATF_REQUIRE_EQ(isc_taskpool_size(pool), 8); 53275970Scy 54275970Scy isc_taskpool_destroy(&pool); 55275970Scy ATF_REQUIRE_EQ(pool, NULL); 56275970Scy 57275970Scy isc_test_end(); 58275970Scy} 59275970Scy 60275970Scy/* Resize a taskpool */ 61275970ScyATF_TC(expand_pool); 62275970ScyATF_TC_HEAD(expand_pool, tc) { 63275970Scy atf_tc_set_md_var(tc, "descr", "expand a taskpool"); 64275970Scy} 65275970ScyATF_TC_BODY(expand_pool, tc) { 66275970Scy isc_result_t result; 67275970Scy isc_taskpool_t *pool1 = NULL, *pool2 = NULL, *hold = NULL; 68275970Scy 69275970Scy UNUSED(tc); 70275970Scy 71275970Scy result = isc_test_begin(NULL, ISC_TRUE); 72275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 73275970Scy 74275970Scy result = isc_taskpool_create(taskmgr, mctx, 10, 2, &pool1); 75275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 76275970Scy ATF_REQUIRE_EQ(isc_taskpool_size(pool1), 10); 77275970Scy 78275970Scy /* resizing to a smaller size should have no effect */ 79275970Scy hold = pool1; 80275970Scy result = isc_taskpool_expand(&pool1, 5, &pool2); 81275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 82275970Scy ATF_REQUIRE_EQ(isc_taskpool_size(pool2), 10); 83275970Scy ATF_REQUIRE_EQ(pool2, hold); 84275970Scy ATF_REQUIRE_EQ(pool1, NULL); 85275970Scy pool1 = pool2; 86275970Scy pool2 = NULL; 87275970Scy 88275970Scy /* resizing to the same size should have no effect */ 89275970Scy hold = pool1; 90275970Scy result = isc_taskpool_expand(&pool1, 10, &pool2); 91275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 92275970Scy ATF_REQUIRE_EQ(isc_taskpool_size(pool2), 10); 93275970Scy ATF_REQUIRE_EQ(pool2, hold); 94275970Scy ATF_REQUIRE_EQ(pool1, NULL); 95275970Scy pool1 = pool2; 96275970Scy pool2 = NULL; 97275970Scy 98275970Scy /* resizing to larger size should make a new pool */ 99275970Scy hold = pool1; 100275970Scy result = isc_taskpool_expand(&pool1, 20, &pool2); 101275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 102275970Scy ATF_REQUIRE_EQ(isc_taskpool_size(pool2), 20); 103275970Scy ATF_REQUIRE(pool2 != hold); 104275970Scy ATF_REQUIRE_EQ(pool1, NULL); 105275970Scy 106275970Scy isc_taskpool_destroy(&pool2); 107275970Scy ATF_REQUIRE_EQ(pool2, NULL); 108275970Scy 109275970Scy isc_test_end(); 110275970Scy} 111275970Scy 112275970Scy/* Get tasks */ 113275970ScyATF_TC(get_tasks); 114275970ScyATF_TC_HEAD(get_tasks, tc) { 115275970Scy atf_tc_set_md_var(tc, "descr", "create a taskpool"); 116275970Scy} 117275970ScyATF_TC_BODY(get_tasks, tc) { 118275970Scy isc_result_t result; 119275970Scy isc_taskpool_t *pool = NULL; 120275970Scy isc_task_t *task1 = NULL, *task2 = NULL, *task3 = NULL; 121275970Scy 122275970Scy UNUSED(tc); 123275970Scy 124275970Scy result = isc_test_begin(NULL, ISC_TRUE); 125275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 126275970Scy 127275970Scy result = isc_taskpool_create(taskmgr, mctx, 2, 2, &pool); 128275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 129275970Scy ATF_REQUIRE_EQ(isc_taskpool_size(pool), 2); 130275970Scy 131275970Scy /* two tasks in pool; make sure we can access them more than twice */ 132275970Scy isc_taskpool_gettask(pool, &task1); 133275970Scy ATF_REQUIRE(ISCAPI_TASK_VALID(task1)); 134275970Scy 135275970Scy isc_taskpool_gettask(pool, &task2); 136275970Scy ATF_REQUIRE(ISCAPI_TASK_VALID(task2)); 137275970Scy 138275970Scy isc_taskpool_gettask(pool, &task3); 139275970Scy ATF_REQUIRE(ISCAPI_TASK_VALID(task3)); 140275970Scy 141275970Scy isc_task_destroy(&task1); 142275970Scy isc_task_destroy(&task2); 143275970Scy isc_task_destroy(&task3); 144275970Scy 145275970Scy isc_taskpool_destroy(&pool); 146275970Scy ATF_REQUIRE_EQ(pool, NULL); 147275970Scy 148275970Scy isc_test_end(); 149275970Scy} 150275970Scy 151275970Scy/* Get tasks */ 152275970ScyATF_TC(set_privilege); 153275970ScyATF_TC_HEAD(set_privilege, tc) { 154275970Scy atf_tc_set_md_var(tc, "descr", "create a taskpool"); 155275970Scy} 156275970ScyATF_TC_BODY(set_privilege, tc) { 157275970Scy isc_result_t result; 158275970Scy isc_taskpool_t *pool = NULL; 159275970Scy isc_task_t *task1 = NULL, *task2 = NULL, *task3 = NULL; 160275970Scy 161275970Scy UNUSED(tc); 162275970Scy 163275970Scy result = isc_test_begin(NULL, ISC_TRUE); 164275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 165275970Scy 166275970Scy result = isc_taskpool_create(taskmgr, mctx, 2, 2, &pool); 167275970Scy ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 168275970Scy ATF_REQUIRE_EQ(isc_taskpool_size(pool), 2); 169275970Scy 170275970Scy isc_taskpool_setprivilege(pool, ISC_TRUE); 171275970Scy 172275970Scy isc_taskpool_gettask(pool, &task1); 173275970Scy isc_taskpool_gettask(pool, &task2); 174275970Scy isc_taskpool_gettask(pool, &task3); 175275970Scy 176275970Scy ATF_CHECK(ISCAPI_TASK_VALID(task1)); 177275970Scy ATF_CHECK(ISCAPI_TASK_VALID(task2)); 178275970Scy ATF_CHECK(ISCAPI_TASK_VALID(task3)); 179275970Scy 180275970Scy ATF_CHECK(isc_task_privilege(task1)); 181275970Scy ATF_CHECK(isc_task_privilege(task2)); 182275970Scy ATF_CHECK(isc_task_privilege(task3)); 183275970Scy 184275970Scy isc_taskpool_setprivilege(pool, ISC_FALSE); 185275970Scy 186275970Scy ATF_CHECK(!isc_task_privilege(task1)); 187275970Scy ATF_CHECK(!isc_task_privilege(task2)); 188275970Scy ATF_CHECK(!isc_task_privilege(task3)); 189275970Scy 190275970Scy isc_task_destroy(&task1); 191275970Scy isc_task_destroy(&task2); 192275970Scy isc_task_destroy(&task3); 193275970Scy 194275970Scy isc_taskpool_destroy(&pool); 195275970Scy ATF_REQUIRE_EQ(pool, NULL); 196275970Scy 197275970Scy isc_test_end(); 198275970Scy} 199275970Scy 200275970Scy/* 201275970Scy * Main 202275970Scy */ 203275970ScyATF_TP_ADD_TCS(tp) { 204275970Scy ATF_TP_ADD_TC(tp, create_pool); 205275970Scy ATF_TP_ADD_TC(tp, expand_pool); 206275970Scy ATF_TP_ADD_TC(tp, get_tasks); 207275970Scy ATF_TP_ADD_TC(tp, set_privilege); 208275970Scy 209275970Scy return (atf_no_error()); 210275970Scy} 211275970Scy 212