Home | History | Annotate | Line # | Download | only in tests
leaseq_unittest.c revision 1.1
      1  1.1  christos /*	$NetBSD: leaseq_unittest.c,v 1.1 2018/04/07 22:34:28 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) 2015-2017 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.1  christos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
     11  1.1  christos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     12  1.1  christos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
     13  1.1  christos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     14  1.1  christos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
     15  1.1  christos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     16  1.1  christos  * PERFORMANCE OF THIS SOFTWARE.
     17  1.1  christos  */
     18  1.1  christos 
     19  1.1  christos #include <config.h>
     20  1.1  christos 
     21  1.1  christos #include "dhcpd.h"
     22  1.1  christos 
     23  1.1  christos #include <atf-c.h>
     24  1.1  christos 
     25  1.1  christos /*
     26  1.1  christos  * Test the lease queue code.  These tests will verify that we can
     27  1.1  christos  * add, find and remove leases from the lease queue code
     28  1.1  christos  *
     29  1.1  christos  * Thoughout the tests
     30  1.1  christos  * lq will be the lease queue for which we add or removing leases
     31  1.1  christos  * test_leaseX will be the leases we add or remove
     32  1.1  christos  * We only care about the sort_time in the lease structure for these
     33  1.1  christos  * tests but need to do a lease_reference in order to keep the ref
     34  1.1  christos  * count positive to avoid the omapi code trying to free the object.
     35  1.1  christos  * We can't use lease_allocate easily as we haven't set up the omapi
     36  1.1  christos  * object information in the test.
     37  1.1  christos  */
     38  1.1  christos 
     39  1.1  christos #if defined (BINARY_LEASES)
     40  1.1  christos #define INIT_LQ(LQ) memset(&(LQ), 0, sizeof(struct leasechain))
     41  1.1  christos #else
     42  1.1  christos #define INIT_LQ(LQ) lq = NULL
     43  1.1  christos #endif
     44  1.1  christos 
     45  1.1  christos /* Test basic leaseq functions with a single lease */
     46  1.1  christos /*- empty, add, get, and remove */
     47  1.1  christos ATF_TC(leaseq_basic);
     48  1.1  christos ATF_TC_HEAD(leaseq_basic, tc)
     49  1.1  christos {
     50  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Verify basic functions");
     51  1.1  christos }
     52  1.1  christos 
     53  1.1  christos ATF_TC_BODY(leaseq_basic, tc)
     54  1.1  christos {
     55  1.1  christos 	LEASE_STRUCT lq;
     56  1.1  christos 	struct lease test_lease1, *check_lease;
     57  1.1  christos 
     58  1.1  christos 	INIT_LQ(lq);
     59  1.1  christos 
     60  1.1  christos 	memset(&test_lease1, 0, sizeof(struct lease));
     61  1.1  christos 	test_lease1.sort_time = 10;
     62  1.1  christos 	check_lease = NULL;
     63  1.1  christos 	lease_reference(&check_lease, &test_lease1, MDL);
     64  1.1  christos 
     65  1.1  christos 	/* Check that the lq is empty */
     66  1.1  christos 	if ((LEASE_NOT_EMPTY(lq)) || (LEASE_NOT_EMPTYP(&lq)))
     67  1.1  christos 		atf_tc_fail("lq not empty at start.");
     68  1.1  christos 
     69  1.1  christos 	/* And that getting the first lease is okay when queue is empty  */
     70  1.1  christos 	if ((LEASE_GET_FIRST(lq) != NULL) || (LEASE_GET_FIRSTP(&lq) != NULL))
     71  1.1  christos 		atf_tc_fail("lease not null");
     72  1.1  christos 
     73  1.1  christos 	/* Add a lease */
     74  1.1  christos 	LEASE_INSERTP(&lq, &test_lease1);
     75  1.1  christos 
     76  1.1  christos 	/* lq shouldn't be empty anymore */
     77  1.1  christos 	if (!(LEASE_NOT_EMPTY(lq)) || !(LEASE_NOT_EMPTYP(&lq)))
     78  1.1  christos 		atf_tc_fail("lq empty after insertion.");
     79  1.1  christos 
     80  1.1  christos 	/* We should have the same lease we inserted */
     81  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
     82  1.1  christos 	if (check_lease != &test_lease1)
     83  1.1  christos 		atf_tc_fail("leases don't match");
     84  1.1  christos 
     85  1.1  christos 	/* We should have the same lease we inserted */
     86  1.1  christos 	check_lease = LEASE_GET_FIRSTP(&lq);
     87  1.1  christos 	if (check_lease != &test_lease1)
     88  1.1  christos 		atf_tc_fail("leases don't match");
     89  1.1  christos 
     90  1.1  christos 	/* Check that doing a get on the last lease returns NULL */
     91  1.1  christos 	if ((LEASE_GET_NEXT(lq, check_lease) != NULL) ||
     92  1.1  christos 	    (LEASE_GET_NEXTP(&lq, check_lease) != NULL)) {
     93  1.1  christos 		atf_tc_fail("Next not null");
     94  1.1  christos 	}
     95  1.1  christos 
     96  1.1  christos 	/* Remove the lease */
     97  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease1);
     98  1.1  christos 
     99  1.1  christos 	/* and verify the lease queue is empty again */
    100  1.1  christos 	if ((LEASE_NOT_EMPTY(lq)) || (LEASE_NOT_EMPTYP(&lq)))
    101  1.1  christos 		atf_tc_fail("lq not empty afer removal");
    102  1.1  christos 
    103  1.1  christos 	/* And that getting the first lease is okay when queue is empty  */
    104  1.1  christos 	if ((LEASE_GET_FIRST(lq) != NULL) || (LEASE_GET_FIRSTP(&lq) != NULL))
    105  1.1  christos 		atf_tc_fail("lease not null");
    106  1.1  christos }
    107  1.1  christos 
    108  1.1  christos /* Test if we can add leases to the end of the list and remove them
    109  1.1  christos  * from the end */
    110  1.1  christos ATF_TC(leaseq_add_to_end);
    111  1.1  christos ATF_TC_HEAD(leaseq_add_to_end, tc)
    112  1.1  christos {
    113  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Verify adding to end of list");
    114  1.1  christos }
    115  1.1  christos 
    116  1.1  christos ATF_TC_BODY(leaseq_add_to_end, tc)
    117  1.1  christos {
    118  1.1  christos 	LEASE_STRUCT lq;
    119  1.1  christos 	struct lease test_lease[3], *check_lease;
    120  1.1  christos 	int i;
    121  1.1  christos 
    122  1.1  christos 	INIT_LQ(lq);
    123  1.1  christos 
    124  1.1  christos 	/* create and add 3 leases */
    125  1.1  christos 	for (i = 0; i < 3; i++) {
    126  1.1  christos 		memset(&test_lease[i], 0, sizeof(struct lease));
    127  1.1  christos 		test_lease[i].sort_time = i;
    128  1.1  christos 		check_lease = NULL;
    129  1.1  christos 		lease_reference(&check_lease, &test_lease[i], MDL);
    130  1.1  christos 		LEASE_INSERTP(&lq, &test_lease[i]);
    131  1.1  christos 	}
    132  1.1  christos 
    133  1.1  christos 	/* check ordering of leases */
    134  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    135  1.1  christos 	for (i = 0; i < 3; i++) {
    136  1.1  christos 		if (check_lease != &test_lease[i])
    137  1.1  christos 			atf_tc_fail("leases don't match, %d", i);
    138  1.1  christos 		check_lease = LEASE_GET_NEXT(lq, check_lease);
    139  1.1  christos 	}
    140  1.1  christos 	if (check_lease != NULL)
    141  1.1  christos 		atf_tc_fail("lease not null");
    142  1.1  christos 
    143  1.1  christos 	/* Remove the last lease and check the list */
    144  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease[2]);
    145  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    146  1.1  christos 	if (check_lease != &test_lease[0])
    147  1.1  christos 		atf_tc_fail("wrong lease after remove, 1");
    148  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    149  1.1  christos 	if (check_lease != &test_lease[1])
    150  1.1  christos 		atf_tc_fail("wrong lease after remove, 2");
    151  1.1  christos 
    152  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease[1]);
    153  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    154  1.1  christos 	if (check_lease != &test_lease[0])
    155  1.1  christos 		atf_tc_fail("wrong lease after remove, 3");
    156  1.1  christos 
    157  1.1  christos 	LEASE_REMOVEP(&lq, check_lease);
    158  1.1  christos 
    159  1.1  christos 	/* The lease queue should now be empty */
    160  1.1  christos 	if (LEASE_NOT_EMPTY(lq))
    161  1.1  christos 		atf_tc_fail("lq not empty afer removal");
    162  1.1  christos }
    163  1.1  christos 
    164  1.1  christos /* Test if we can add leases to the start of the list and remove them
    165  1.1  christos  * from the start */
    166  1.1  christos ATF_TC(leaseq_add_to_start);
    167  1.1  christos ATF_TC_HEAD(leaseq_add_to_start, tc)
    168  1.1  christos {
    169  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Verify adding to start of list");
    170  1.1  christos }
    171  1.1  christos 
    172  1.1  christos ATF_TC_BODY(leaseq_add_to_start, tc)
    173  1.1  christos {
    174  1.1  christos 	LEASE_STRUCT lq;
    175  1.1  christos 	struct lease test_lease[3], *check_lease;
    176  1.1  christos 	int i;
    177  1.1  christos 
    178  1.1  christos 	INIT_LQ(lq);
    179  1.1  christos 
    180  1.1  christos 	/* create 3 leases */
    181  1.1  christos 	for (i = 0; i < 3; i++) {
    182  1.1  christos 		memset(&test_lease[i], 0, sizeof(struct lease));
    183  1.1  christos 		test_lease[i].sort_time = i;
    184  1.1  christos 		check_lease = NULL;
    185  1.1  christos 		lease_reference(&check_lease, &test_lease[i], MDL);
    186  1.1  christos 	}
    187  1.1  christos 
    188  1.1  christos 	/* Add leases */
    189  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[2]);
    190  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[1]);
    191  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[0]);
    192  1.1  christos 
    193  1.1  christos 	/* check ordering of leases */
    194  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    195  1.1  christos 	for (i = 0; i < 3; i++) {
    196  1.1  christos 		if (check_lease != &test_lease[i])
    197  1.1  christos 			atf_tc_fail("leases don't match, %d", i);
    198  1.1  christos 		check_lease = LEASE_GET_NEXT(lq, check_lease);
    199  1.1  christos 	}
    200  1.1  christos 	if (check_lease != NULL)
    201  1.1  christos 		atf_tc_fail("lease not null");
    202  1.1  christos 
    203  1.1  christos 	/* Remove the first lease and check the next one */
    204  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    205  1.1  christos 	LEASE_REMOVEP(&lq, check_lease);
    206  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    207  1.1  christos 	if (check_lease != &test_lease[1])
    208  1.1  christos 		atf_tc_fail("wrong lease after remove, 1");
    209  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    210  1.1  christos 	if (check_lease != &test_lease[2])
    211  1.1  christos 		atf_tc_fail("wrong lease after remove, 2");
    212  1.1  christos 
    213  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    214  1.1  christos 	LEASE_REMOVEP(&lq, check_lease);
    215  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    216  1.1  christos 	if (check_lease != &test_lease[2])
    217  1.1  christos 		atf_tc_fail("wrong lease after remove, 3");
    218  1.1  christos 
    219  1.1  christos 	LEASE_REMOVEP(&lq, check_lease);
    220  1.1  christos 
    221  1.1  christos 	/* The lease queue should now be empty */
    222  1.1  christos 	if (LEASE_NOT_EMPTY(lq))
    223  1.1  christos 		atf_tc_fail("lq not empty afer removal");
    224  1.1  christos }
    225  1.1  christos 
    226  1.1  christos /* Test if we can add leases to the middle of the list and remove them
    227  1.1  christos  * from the middle */
    228  1.1  christos ATF_TC(leaseq_add_to_middle);
    229  1.1  christos ATF_TC_HEAD(leaseq_add_to_middle, tc)
    230  1.1  christos {
    231  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Verify adding to end of list");
    232  1.1  christos }
    233  1.1  christos 
    234  1.1  christos ATF_TC_BODY(leaseq_add_to_middle, tc)
    235  1.1  christos {
    236  1.1  christos 	LEASE_STRUCT lq;
    237  1.1  christos 	struct lease test_lease[3], *check_lease;
    238  1.1  christos 	int i;
    239  1.1  christos 
    240  1.1  christos 	INIT_LQ(lq);
    241  1.1  christos 
    242  1.1  christos 	/* create 3 leases */
    243  1.1  christos 	for (i = 0; i < 3; i++) {
    244  1.1  christos 		memset(&test_lease[i], 0, sizeof(struct lease));
    245  1.1  christos 		test_lease[i].sort_time = i;
    246  1.1  christos 		check_lease = NULL;
    247  1.1  christos 		lease_reference(&check_lease, &test_lease[i], MDL);
    248  1.1  christos 	}
    249  1.1  christos 
    250  1.1  christos 	/* Add leases */
    251  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[0]);
    252  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[2]);
    253  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[1]);
    254  1.1  christos 
    255  1.1  christos 	/* check ordering of leases */
    256  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    257  1.1  christos 	for (i = 0; i < 3; i++) {
    258  1.1  christos 		if (check_lease != &test_lease[i])
    259  1.1  christos 			atf_tc_fail("leases don't match, %d", i);
    260  1.1  christos 		check_lease = LEASE_GET_NEXT(lq, check_lease);
    261  1.1  christos 	}
    262  1.1  christos 	if (check_lease != NULL)
    263  1.1  christos 		atf_tc_fail("lease not null");
    264  1.1  christos 
    265  1.1  christos 	/* Remove the middle lease and check the list */
    266  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease[1]);
    267  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    268  1.1  christos 	if (check_lease != &test_lease[0])
    269  1.1  christos 		atf_tc_fail("wrong lease after remove, 1");
    270  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    271  1.1  christos 	if (check_lease != &test_lease[2])
    272  1.1  christos 		atf_tc_fail("wrong lease after remove, 2");
    273  1.1  christos 
    274  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease[0]);
    275  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    276  1.1  christos 	if (check_lease != &test_lease[2])
    277  1.1  christos 		atf_tc_fail("wrong lease after remove, 3");
    278  1.1  christos 
    279  1.1  christos 	LEASE_REMOVEP(&lq, check_lease);
    280  1.1  christos 
    281  1.1  christos 	/* The lease queue should now be empty */
    282  1.1  christos 	if (LEASE_NOT_EMPTY(lq))
    283  1.1  christos 		atf_tc_fail("lq not empty afer removal");
    284  1.1  christos }
    285  1.1  christos 
    286  1.1  christos /* Test if we can cycle the leases we add three leases then remove
    287  1.1  christos  * the first one, update it's sort time and re add it */
    288  1.1  christos ATF_TC(leaseq_cycle);
    289  1.1  christos ATF_TC_HEAD(leaseq_cycle, tc)
    290  1.1  christos {
    291  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Verify cycling the list");
    292  1.1  christos }
    293  1.1  christos 
    294  1.1  christos ATF_TC_BODY(leaseq_cycle, tc)
    295  1.1  christos {
    296  1.1  christos 	LEASE_STRUCT lq;
    297  1.1  christos 	struct lease test_lease[3], *check_lease;
    298  1.1  christos 	int i;
    299  1.1  christos 
    300  1.1  christos 	INIT_LQ(lq);
    301  1.1  christos 
    302  1.1  christos 	/* create and add 3 leases */
    303  1.1  christos 	for (i = 0; i < 3; i++) {
    304  1.1  christos 		memset(&test_lease[i], 0, sizeof(struct lease));
    305  1.1  christos 		test_lease[i].sort_time = i;
    306  1.1  christos 		check_lease = NULL;
    307  1.1  christos 		lease_reference(&check_lease, &test_lease[i], MDL);
    308  1.1  christos 		LEASE_INSERTP(&lq, &test_lease[i]);
    309  1.1  christos 	}
    310  1.1  christos 
    311  1.1  christos 	/* Remove first lease, update it and re-insert it */
    312  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease[0]);
    313  1.1  christos 	test_lease[0].sort_time = 4;
    314  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[0]);
    315  1.1  christos 
    316  1.1  christos 	/* check ordering of leases */
    317  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    318  1.1  christos 	if (check_lease != &test_lease[1])
    319  1.1  christos 		atf_tc_fail("leases don't match, 1");
    320  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    321  1.1  christos 	if (check_lease != &test_lease[2])
    322  1.1  christos 		atf_tc_fail("leases don't match, 2");
    323  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    324  1.1  christos 	if (check_lease != &test_lease[0])
    325  1.1  christos 		atf_tc_fail("leases don't match, 3");
    326  1.1  christos 
    327  1.1  christos 	/* Remove first lease, update it and re-insert it */
    328  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease[1]);
    329  1.1  christos 	test_lease[1].sort_time = 5;
    330  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[1]);
    331  1.1  christos 
    332  1.1  christos 	/* check ordering of leases */
    333  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    334  1.1  christos 	if (check_lease != &test_lease[2])
    335  1.1  christos 		atf_tc_fail("leases don't match, 4");
    336  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    337  1.1  christos 	if (check_lease != &test_lease[0])
    338  1.1  christos 		atf_tc_fail("leases don't match, 5");
    339  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    340  1.1  christos 	if (check_lease != &test_lease[1])
    341  1.1  christos 		atf_tc_fail("leases don't match, 6");
    342  1.1  christos 
    343  1.1  christos 	/* Remove first lease, update it and re-insert it */
    344  1.1  christos 	LEASE_REMOVEP(&lq, &test_lease[2]);
    345  1.1  christos 	test_lease[2].sort_time = 6;
    346  1.1  christos 	LEASE_INSERTP(&lq, &test_lease[2]);
    347  1.1  christos 
    348  1.1  christos 	/* check ordering of leases */
    349  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    350  1.1  christos 	if (check_lease != &test_lease[0])
    351  1.1  christos 		atf_tc_fail("leases don't match, 7");
    352  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    353  1.1  christos 	if (check_lease != &test_lease[1])
    354  1.1  christos 		atf_tc_fail("leases don't match, 8");
    355  1.1  christos 	check_lease = LEASE_GET_NEXT(lq, check_lease);
    356  1.1  christos 	if (check_lease != &test_lease[2])
    357  1.1  christos 		atf_tc_fail("leases don't match, 9");
    358  1.1  christos }
    359  1.1  christos 
    360  1.1  christos /* Test what happens if we set the growth factor to a smallish number
    361  1.1  christos  * and add enough leases to require it to grow multiple times
    362  1.1  christos  * Mostly this is for the binary leases case but we can most of the
    363  1.1  christos  * test for both.
    364  1.1  christos  */
    365  1.1  christos 
    366  1.1  christos ATF_TC(leaseq_long);
    367  1.1  christos ATF_TC_HEAD(leaseq_long, tc)
    368  1.1  christos {
    369  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Verify a long list");
    370  1.1  christos }
    371  1.1  christos 
    372  1.1  christos ATF_TC_BODY(leaseq_long, tc)
    373  1.1  christos {
    374  1.1  christos 	LEASE_STRUCT lq;
    375  1.1  christos 	struct lease test_lease[20], *check_lease;
    376  1.1  christos 	int i;
    377  1.1  christos 
    378  1.1  christos 	INIT_LQ(lq);
    379  1.1  christos #if defined (BINARY_LEASES)
    380  1.1  christos 	lc_init_growth(&lq, 5);
    381  1.1  christos #endif
    382  1.1  christos 
    383  1.1  christos 	/* create and add 10 leases */
    384  1.1  christos 	for (i = 0; i < 10; i++) {
    385  1.1  christos 		memset(&test_lease[i], 0, sizeof(struct lease));
    386  1.1  christos 		test_lease[i].sort_time = i;
    387  1.1  christos 		check_lease = NULL;
    388  1.1  christos 		lease_reference(&check_lease, &test_lease[i], MDL);
    389  1.1  christos 
    390  1.1  christos 		LEASE_INSERTP(&lq, &test_lease[i]);
    391  1.1  christos 	}
    392  1.1  christos 
    393  1.1  christos 	/* check ordering of leases */
    394  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    395  1.1  christos 	for (i = 0; i < 10; i++) {
    396  1.1  christos 		if (check_lease != &test_lease[i])
    397  1.1  christos 			atf_tc_fail("leases don't match, %d", i);
    398  1.1  christos 		check_lease = LEASE_GET_NEXT(lq, check_lease);
    399  1.1  christos 	}
    400  1.1  christos 
    401  1.1  christos 	/* Remove first 5 */
    402  1.1  christos 	for (i = 0; i < 5; i++) {
    403  1.1  christos 		LEASE_REMOVEP(&lq, &test_lease[i]);
    404  1.1  christos 	}
    405  1.1  christos 
    406  1.1  christos 	/* Add 10 more */
    407  1.1  christos 	for (i = 10; i < 20; i++) {
    408  1.1  christos 		memset(&test_lease[i], 0, sizeof(struct lease));
    409  1.1  christos 		test_lease[i].sort_time = i;
    410  1.1  christos 		check_lease = NULL;
    411  1.1  christos 		lease_reference(&check_lease, &test_lease[i], MDL);
    412  1.1  christos 
    413  1.1  christos 		LEASE_INSERTP(&lq, &test_lease[i]);
    414  1.1  christos 	}
    415  1.1  christos 
    416  1.1  christos 	/* and the first 5 */
    417  1.1  christos 	for (i = 0; i < 5; i++) {
    418  1.1  christos 		LEASE_INSERTP(&lq, &test_lease[i]);
    419  1.1  christos 	}
    420  1.1  christos 
    421  1.1  christos 	/* and check them all out */
    422  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    423  1.1  christos 	for (i = 0; i < 20; i++) {
    424  1.1  christos 		if (check_lease != &test_lease[i])
    425  1.1  christos 			atf_tc_fail("leases don't match, %d", i);
    426  1.1  christos 		check_lease = LEASE_GET_NEXT(lq, check_lease);
    427  1.1  christos 	}
    428  1.1  christos }
    429  1.1  christos 
    430  1.1  christos /* Test if we can add leases that all have the same sort time
    431  1.1  christos  */
    432  1.1  christos ATF_TC(leaseq_same_time);
    433  1.1  christos ATF_TC_HEAD(leaseq_same_time, tc)
    434  1.1  christos {
    435  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Verify adding leases with same time");
    436  1.1  christos }
    437  1.1  christos 
    438  1.1  christos ATF_TC_BODY(leaseq_same_time, tc)
    439  1.1  christos {
    440  1.1  christos 	LEASE_STRUCT lq;
    441  1.1  christos 	struct lease test_lease[20], *check_lease;
    442  1.1  christos 	int i;
    443  1.1  christos 
    444  1.1  christos 	INIT_LQ(lq);
    445  1.1  christos 
    446  1.1  christos 	/* create and add 20 leases */
    447  1.1  christos 	for (i = 0; i < 20; i++) {
    448  1.1  christos 		memset(&test_lease[i], 0, sizeof(struct lease));
    449  1.1  christos 		if (i < 10)
    450  1.1  christos 			test_lease[i].sort_time = 10;
    451  1.1  christos 		else
    452  1.1  christos 			test_lease[i].sort_time = 20;
    453  1.1  christos 		check_lease = NULL;
    454  1.1  christos 		lease_reference(&check_lease, &test_lease[i], MDL);
    455  1.1  christos 		LEASE_INSERTP(&lq, &test_lease[i]);
    456  1.1  christos 	}
    457  1.1  christos 
    458  1.1  christos #if defined (BINARY_LEASES)
    459  1.1  christos 	/* the ordering isn't well defined for either but we check
    460  1.1  christos 	 * to see what happens in the binary case.  At the start
    461  1.1  christos 	 * the leases should stay in the order they were added.
    462  1.1  christos 	 */
    463  1.1  christos 
    464  1.1  christos 	/* check ordering of leases */
    465  1.1  christos 	check_lease = LEASE_GET_FIRST(lq);
    466  1.1  christos 	for (i = 0; i < 20; i++) {
    467  1.1  christos 		if (check_lease != &test_lease[i])
    468  1.1  christos 			atf_tc_fail("leases don't match 1, %d", i);
    469  1.1  christos 		check_lease = LEASE_GET_NEXT(lq, check_lease);
    470  1.1  christos 	}
    471  1.1  christos 	if (check_lease != NULL)
    472  1.1  christos 		atf_tc_fail("lease not null");
    473  1.1  christos #endif
    474  1.1  christos 
    475  1.1  christos 	/* Remove first 10, update their time, but still before
    476  1.1  christos 	 * the previous 10 and re add them */
    477  1.1  christos 	for (i = 0; i < 10; i++) {
    478  1.1  christos 		LEASE_REMOVEP(&lq, &test_lease[i]);
    479  1.1  christos 		test_lease[i].sort_time = 15;
    480  1.1  christos 		LEASE_INSERTP(&lq, &test_lease[i]);
    481  1.1  christos 	}
    482  1.1  christos 
    483  1.1  christos 	/* The ordering becomes random at this point so we don't
    484  1.1  christos 	 * check it.  We try to remove them all and see that
    485  1.1  christos 	 * we got to an empty queue.
    486  1.1  christos 	 */
    487  1.1  christos 	for (i = 0; i < 20; i++) {
    488  1.1  christos 		LEASE_REMOVEP(&lq, &test_lease[i]);
    489  1.1  christos 	}
    490  1.1  christos 	if (LEASE_NOT_EMPTY(lq))
    491  1.1  christos 		atf_tc_fail("queue not empty");
    492  1.1  christos 
    493  1.1  christos }
    494  1.1  christos 
    495  1.1  christos ATF_TP_ADD_TCS(tp)
    496  1.1  christos {
    497  1.1  christos 	ATF_TP_ADD_TC(tp, leaseq_basic);
    498  1.1  christos 	ATF_TP_ADD_TC(tp, leaseq_add_to_end);
    499  1.1  christos 	ATF_TP_ADD_TC(tp, leaseq_add_to_start);
    500  1.1  christos 	ATF_TP_ADD_TC(tp, leaseq_add_to_middle);
    501  1.1  christos 	ATF_TP_ADD_TC(tp, leaseq_cycle);
    502  1.1  christos 	ATF_TP_ADD_TC(tp, leaseq_long);
    503  1.1  christos 	ATF_TP_ADD_TC(tp, leaseq_same_time);
    504  1.1  christos 	return (atf_no_error());
    505  1.1  christos }
    506