1 /* $NetBSD: dispatch_test.c,v 1.1.1.4 2014/12/10 03:34:42 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2012, 2014 Internet Systems Consortium, Inc. ("ISC") 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* Id */ 20 21 /*! \file */ 22 23 #include <config.h> 24 25 #include <atf-c.h> 26 27 #include <unistd.h> 28 29 #include <isc/buffer.h> 30 #include <isc/socket.h> 31 #include <isc/task.h> 32 #include <isc/timer.h> 33 34 #include <dns/dispatch.h> 35 #include <dns/name.h> 36 #include <dns/view.h> 37 38 #include "dnstest.h" 39 40 dns_dispatchmgr_t *dispatchmgr = NULL; 41 dns_dispatchset_t *dset = NULL; 42 43 static isc_result_t 44 make_dispatchset(unsigned int ndisps) { 45 isc_result_t result; 46 isc_sockaddr_t any; 47 unsigned int attrs; 48 dns_dispatch_t *disp = NULL; 49 50 result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr); 51 if (result != ISC_R_SUCCESS) 52 return (result); 53 54 isc_sockaddr_any(&any); 55 attrs = DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_UDP; 56 result = dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr, 57 &any, 512, 6, 1024, 17, 19, attrs, 58 attrs, &disp); 59 if (result != ISC_R_SUCCESS) 60 return (result); 61 62 result = dns_dispatchset_create(mctx, socketmgr, taskmgr, disp, 63 &dset, ndisps); 64 dns_dispatch_detach(&disp); 65 66 return (result); 67 } 68 69 static void 70 teardown(void) { 71 if (dset != NULL) 72 dns_dispatchset_destroy(&dset); 73 if (dispatchmgr != NULL) 74 dns_dispatchmgr_destroy(&dispatchmgr); 75 } 76 77 /* 78 * Individual unit tests 79 */ 80 ATF_TC(dispatchset_create); 81 ATF_TC_HEAD(dispatchset_create, tc) { 82 atf_tc_set_md_var(tc, "descr", "create dispatch set"); 83 } 84 ATF_TC_BODY(dispatchset_create, tc) { 85 isc_result_t result; 86 87 UNUSED(tc); 88 89 result = dns_test_begin(NULL, ISC_TRUE); 90 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 91 92 result = make_dispatchset(1); 93 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 94 teardown(); 95 96 result = make_dispatchset(10); 97 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 98 teardown(); 99 100 dns_test_end(); 101 } 102 103 104 105 ATF_TC(dispatchset_get); 106 ATF_TC_HEAD(dispatchset_get, tc) { 107 atf_tc_set_md_var(tc, "descr", "test dispatch set round-robin"); 108 } 109 ATF_TC_BODY(dispatchset_get, tc) { 110 isc_result_t result; 111 dns_dispatch_t *d1, *d2, *d3, *d4, *d5; 112 113 UNUSED(tc); 114 115 result = dns_test_begin(NULL, ISC_TRUE); 116 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 117 118 result = make_dispatchset(1); 119 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 120 121 d1 = dns_dispatchset_get(dset); 122 d2 = dns_dispatchset_get(dset); 123 d3 = dns_dispatchset_get(dset); 124 d4 = dns_dispatchset_get(dset); 125 d5 = dns_dispatchset_get(dset); 126 127 ATF_CHECK_EQ(d1, d2); 128 ATF_CHECK_EQ(d2, d3); 129 ATF_CHECK_EQ(d3, d4); 130 ATF_CHECK_EQ(d4, d5); 131 132 teardown(); 133 134 result = make_dispatchset(4); 135 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); 136 137 d1 = dns_dispatchset_get(dset); 138 d2 = dns_dispatchset_get(dset); 139 d3 = dns_dispatchset_get(dset); 140 d4 = dns_dispatchset_get(dset); 141 d5 = dns_dispatchset_get(dset); 142 143 ATF_CHECK_EQ(d1, d5); 144 ATF_CHECK(d1 != d2); 145 ATF_CHECK(d2 != d3); 146 ATF_CHECK(d3 != d4); 147 ATF_CHECK(d4 != d5); 148 149 teardown(); 150 dns_test_end(); 151 } 152 153 154 /* 155 * Main 156 */ 157 ATF_TP_ADD_TCS(tp) { 158 ATF_TP_ADD_TC(tp, dispatchset_create); 159 ATF_TP_ADD_TC(tp, dispatchset_get); 160 return (atf_no_error()); 161 } 162 163