Home | History | Annotate | Line # | Download | only in tests
      1  1.2  christos /*	$NetBSD: mdb6_unittest.c,v 1.2 2018/04/07 22:37:30 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) 2007-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 <sys/types.h>
     22  1.1  christos #include <time.h>
     23  1.1  christos #include <netinet/in.h>
     24  1.1  christos 
     25  1.1  christos #include <stdarg.h>
     26  1.1  christos #include "dhcpd.h"
     27  1.1  christos #include "omapip/omapip.h"
     28  1.1  christos #include "omapip/hash.h"
     29  1.1  christos #include <isc/md5.h>
     30  1.1  christos 
     31  1.1  christos #include <atf-c.h>
     32  1.1  christos 
     33  1.1  christos #include <stdlib.h>
     34  1.1  christos 
     35  1.1  christos void build_prefix6(struct in6_addr *pref, const struct in6_addr *net_start_pref,
     36  1.1  christos                    int pool_bits, int pref_bits,
     37  1.1  christos                    const struct data_string *input);
     38  1.1  christos 
     39  1.1  christos /*
     40  1.1  christos  * Basic iaaddr manipulation.
     41  1.1  christos  * Verify construction and referencing of an iaaddr.
     42  1.1  christos  */
     43  1.1  christos 
     44  1.1  christos ATF_TC(iaaddr_basic);
     45  1.1  christos ATF_TC_HEAD(iaaddr_basic, tc)
     46  1.1  christos {
     47  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that basic "
     48  1.1  christos                       "IAADDR manipulation is possible.");
     49  1.1  christos }
     50  1.1  christos ATF_TC_BODY(iaaddr_basic, tc)
     51  1.1  christos {
     52  1.1  christos     struct iasubopt *iaaddr;
     53  1.1  christos     struct iasubopt *iaaddr_copy;
     54  1.1  christos 
     55  1.1  christos     /* set up dhcp globals */
     56  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
     57  1.1  christos 			NULL, NULL);
     58  1.1  christos 
     59  1.1  christos     /* and other common arguments */
     60  1.1  christos     iaaddr = NULL;
     61  1.1  christos     iaaddr_copy = NULL;
     62  1.1  christos 
     63  1.1  christos     /* tests */
     64  1.1  christos     if (iasubopt_allocate(&iaaddr, MDL) != ISC_R_SUCCESS) {
     65  1.1  christos         atf_tc_fail("ERROR: iasubopt_allocate() %s:%d", MDL);
     66  1.1  christos     }
     67  1.1  christos     if (iaaddr->state != FTS_FREE) {
     68  1.1  christos         atf_tc_fail("ERROR: bad state %s:%d", MDL);
     69  1.1  christos     }
     70  1.1  christos     if (iaaddr->active_index != 0) {
     71  1.1  christos         atf_tc_fail("ERROR: bad active_index :%d %s:%d",
     72  1.1  christos             iaaddr->active_index, MDL);
     73  1.1  christos     }
     74  1.1  christos     if (iaaddr->inactive_index != 0) {
     75  1.1  christos         atf_tc_fail("ERROR: bad inactive_index %d %s:%d",
     76  1.1  christos             iaaddr->inactive_index, MDL);
     77  1.1  christos     }
     78  1.1  christos     if (iasubopt_reference(&iaaddr_copy, iaaddr, MDL) != ISC_R_SUCCESS) {
     79  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
     80  1.1  christos     }
     81  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
     82  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
     83  1.1  christos     }
     84  1.1  christos     if (iasubopt_dereference(&iaaddr_copy, MDL) != ISC_R_SUCCESS) {
     85  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
     86  1.1  christos     }
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos /*
     90  1.1  christos  * Basic iaaddr sanity checks.
     91  1.1  christos  * Verify that the iaaddr code does some sanity checking.
     92  1.1  christos  */
     93  1.1  christos 
     94  1.1  christos ATF_TC(iaaddr_negative);
     95  1.1  christos ATF_TC_HEAD(iaaddr_negative, tc)
     96  1.1  christos {
     97  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that IAADDR "
     98  1.1  christos                       "option code can handle various negative scenarios.");
     99  1.1  christos }
    100  1.1  christos ATF_TC_BODY(iaaddr_negative, tc)
    101  1.1  christos {
    102  1.1  christos     struct iasubopt *iaaddr;
    103  1.1  christos     struct iasubopt *iaaddr_copy;
    104  1.1  christos 
    105  1.1  christos     /* set up dhcp globals */
    106  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    107  1.1  christos 			NULL, NULL);
    108  1.1  christos 
    109  1.1  christos     /* tests */
    110  1.1  christos     /* bogus allocate arguments */
    111  1.1  christos     if (iasubopt_allocate(NULL, MDL) != DHCP_R_INVALIDARG) {
    112  1.1  christos         atf_tc_fail("ERROR: iasubopt_allocate() %s:%d", MDL);
    113  1.1  christos     }
    114  1.1  christos     iaaddr = (struct iasubopt *)1;
    115  1.1  christos     if (iasubopt_allocate(&iaaddr, MDL) != DHCP_R_INVALIDARG) {
    116  1.1  christos         atf_tc_fail("ERROR: iasubopt_allocate() %s:%d", MDL);
    117  1.1  christos     }
    118  1.1  christos 
    119  1.1  christos     /* bogus reference arguments */
    120  1.1  christos     iaaddr = NULL;
    121  1.1  christos     if (iasubopt_allocate(&iaaddr, MDL) != ISC_R_SUCCESS) {
    122  1.1  christos         atf_tc_fail("ERROR: iasubopt_allocate() %s:%d", MDL);
    123  1.1  christos     }
    124  1.1  christos     if (iasubopt_reference(NULL, iaaddr, MDL) != DHCP_R_INVALIDARG) {
    125  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
    126  1.1  christos     }
    127  1.1  christos     iaaddr_copy = (struct iasubopt *)1;
    128  1.1  christos     if (iasubopt_reference(&iaaddr_copy, iaaddr,
    129  1.1  christos                            MDL) != DHCP_R_INVALIDARG) {
    130  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
    131  1.1  christos     }
    132  1.1  christos     iaaddr_copy = NULL;
    133  1.1  christos     if (iasubopt_reference(&iaaddr_copy, NULL, MDL) != DHCP_R_INVALIDARG) {
    134  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
    135  1.1  christos     }
    136  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    137  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
    138  1.1  christos     }
    139  1.1  christos 
    140  1.1  christos     /* bogus dereference arguments */
    141  1.1  christos     if (iasubopt_dereference(NULL, MDL) != DHCP_R_INVALIDARG) {
    142  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    143  1.1  christos     }
    144  1.1  christos     iaaddr = NULL;
    145  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != DHCP_R_INVALIDARG) {
    146  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    147  1.1  christos     }
    148  1.1  christos }
    149  1.1  christos 
    150  1.1  christos /*
    151  1.1  christos  * Basic ia_na manipulation.
    152  1.1  christos  */
    153  1.1  christos 
    154  1.1  christos ATF_TC(ia_na_basic);
    155  1.1  christos ATF_TC_HEAD(ia_na_basic, tc)
    156  1.1  christos {
    157  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that IA_NA code can "
    158  1.1  christos                       "handle various basic scenarios.");
    159  1.1  christos }
    160  1.1  christos ATF_TC_BODY(ia_na_basic, tc)
    161  1.1  christos {
    162  1.1  christos     uint32_t iaid;
    163  1.1  christos     struct ia_xx *ia_na;
    164  1.1  christos     struct ia_xx *ia_na_copy;
    165  1.1  christos     struct iasubopt *iaaddr;
    166  1.1  christos 
    167  1.1  christos     /* set up dhcp globals */
    168  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    169  1.1  christos 			NULL, NULL);
    170  1.1  christos 
    171  1.1  christos     /* and other common arguments */
    172  1.1  christos     iaid = 666;
    173  1.1  christos     ia_na = NULL;
    174  1.1  christos     ia_na_copy = NULL;
    175  1.1  christos     iaaddr = NULL;
    176  1.1  christos 
    177  1.1  christos     /* tests */
    178  1.1  christos     if (ia_allocate(&ia_na, iaid, "TestDUID", 8, MDL) != ISC_R_SUCCESS) {
    179  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    180  1.1  christos     }
    181  1.1  christos     if (memcmp(ia_na->iaid_duid.data, &iaid, sizeof(iaid)) != 0) {
    182  1.1  christos         atf_tc_fail("ERROR: bad IAID_DUID %s:%d", MDL);
    183  1.1  christos     }
    184  1.1  christos     if (memcmp(ia_na->iaid_duid.data+sizeof(iaid), "TestDUID", 8) != 0) {
    185  1.1  christos         atf_tc_fail("ERROR: bad IAID_DUID %s:%d", MDL);
    186  1.1  christos     }
    187  1.1  christos     if (ia_na->num_iasubopt != 0) {
    188  1.1  christos         atf_tc_fail("ERROR: bad num_iasubopt %s:%d", MDL);
    189  1.1  christos     }
    190  1.1  christos     if (ia_reference(&ia_na_copy, ia_na, MDL) != ISC_R_SUCCESS) {
    191  1.1  christos         atf_tc_fail("ERROR: ia_reference() %s:%d", MDL);
    192  1.1  christos     }
    193  1.1  christos     if (iasubopt_allocate(&iaaddr, MDL) != ISC_R_SUCCESS) {
    194  1.1  christos         atf_tc_fail("ERROR: iasubopt_allocate() %s:%d", MDL);
    195  1.1  christos     }
    196  1.1  christos     if (ia_add_iasubopt(ia_na, iaaddr, MDL) != ISC_R_SUCCESS) {
    197  1.1  christos         atf_tc_fail("ERROR: ia_add_iasubopt() %s:%d", MDL);
    198  1.1  christos     }
    199  1.1  christos     ia_remove_iasubopt(ia_na, iaaddr, MDL);
    200  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    201  1.1  christos         atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
    202  1.1  christos     }
    203  1.1  christos     if (ia_dereference(&ia_na, MDL) != ISC_R_SUCCESS) {
    204  1.1  christos         atf_tc_fail("ERROR: ia_dereference() %s:%d", MDL);
    205  1.1  christos     }
    206  1.1  christos     if (ia_dereference(&ia_na_copy, MDL) != ISC_R_SUCCESS) {
    207  1.1  christos         atf_tc_fail("ERROR: ia_dereference() %s:%d", MDL);
    208  1.1  christos     }
    209  1.1  christos }
    210  1.1  christos 
    211  1.1  christos /*
    212  1.1  christos  * Lots of iaaddr in our ia_na.
    213  1.1  christos  * Create many iaaddrs and attach them to an ia_na
    214  1.1  christos  * then clean up by removing them one at a time and
    215  1.1  christos  * all at once by dereferencing the ia_na.
    216  1.1  christos  */
    217  1.1  christos 
    218  1.1  christos ATF_TC(ia_na_manyaddrs);
    219  1.1  christos ATF_TC_HEAD(ia_na_manyaddrs, tc)
    220  1.1  christos {
    221  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that IA_NA can "
    222  1.1  christos                       "handle lots of addresses.");
    223  1.1  christos }
    224  1.1  christos ATF_TC_BODY(ia_na_manyaddrs, tc)
    225  1.1  christos {
    226  1.1  christos     uint32_t iaid;
    227  1.1  christos     struct ia_xx *ia_na;
    228  1.1  christos     struct iasubopt *iaaddr;
    229  1.1  christos     int i;
    230  1.1  christos 
    231  1.1  christos     /* set up dhcp globals */
    232  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    233  1.1  christos 			NULL, NULL);
    234  1.1  christos 
    235  1.1  christos     /* tests */
    236  1.1  christos     /* lots of iaaddr that we delete */
    237  1.1  christos     iaid = 666;
    238  1.1  christos     ia_na = NULL;
    239  1.1  christos     if (ia_allocate(&ia_na, iaid, "TestDUID", 8, MDL) != ISC_R_SUCCESS) {
    240  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    241  1.1  christos     }
    242  1.1  christos     for (i=0; i<100; i++) {
    243  1.1  christos         iaaddr = NULL;
    244  1.1  christos         if (iasubopt_allocate(&iaaddr, MDL) != ISC_R_SUCCESS) {
    245  1.1  christos             atf_tc_fail("ERROR: iasubopt_allocate() %s:%d", MDL);
    246  1.1  christos         }
    247  1.1  christos         if (ia_add_iasubopt(ia_na, iaaddr, MDL) != ISC_R_SUCCESS) {
    248  1.1  christos             atf_tc_fail("ERROR: ia_add_iasubopt() %s:%d", MDL);
    249  1.1  christos         }
    250  1.1  christos         if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    251  1.1  christos             atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
    252  1.1  christos         }
    253  1.1  christos     }
    254  1.1  christos 
    255  1.1  christos #if 0
    256  1.1  christos     for (i=0; i<100; i++) {
    257  1.1  christos         iaaddr = ia_na->iasubopt[random() % ia_na->num_iasubopt];
    258  1.1  christos         ia_remove_iasubopt(ia_na, iaaddr, MDL);
    259  1.1  christos         /* TODO: valgrind reports problem here: Invalid read of size 8
    260  1.1  christos          * Address 0x51e6258 is 56 bytes inside a block of size 88 free'd */
    261  1.1  christos     }
    262  1.1  christos #endif
    263  1.1  christos     if (ia_dereference(&ia_na, MDL) != ISC_R_SUCCESS) {
    264  1.1  christos         atf_tc_fail("ERROR: ia_dereference() %s:%d", MDL);
    265  1.1  christos     }
    266  1.1  christos 
    267  1.1  christos     /* lots of iaaddr, let dereference cleanup */
    268  1.1  christos     iaid = 666;
    269  1.1  christos     ia_na = NULL;
    270  1.1  christos     if (ia_allocate(&ia_na, iaid, "TestDUID", 8, MDL) != ISC_R_SUCCESS) {
    271  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    272  1.1  christos     }
    273  1.1  christos     for (i=0; i<100; i++) {
    274  1.1  christos         iaaddr = NULL;
    275  1.1  christos         if (iasubopt_allocate(&iaaddr, MDL) != ISC_R_SUCCESS) {
    276  1.1  christos             atf_tc_fail("ERROR: iasubopt_allocate() %s:%d", MDL);
    277  1.1  christos         }
    278  1.1  christos         if (ia_add_iasubopt(ia_na, iaaddr, MDL) != ISC_R_SUCCESS) {
    279  1.1  christos             atf_tc_fail("ERROR: ia_add_iasubopt() %s:%d", MDL);
    280  1.1  christos         }
    281  1.1  christos         if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    282  1.1  christos             atf_tc_fail("ERROR: iasubopt_reference() %s:%d", MDL);
    283  1.1  christos         }
    284  1.1  christos     }
    285  1.1  christos     if (ia_dereference(&ia_na, MDL) != ISC_R_SUCCESS) {
    286  1.1  christos         atf_tc_fail("ERROR: ia_dereference() %s:%d", MDL);
    287  1.1  christos     }
    288  1.1  christos }
    289  1.1  christos 
    290  1.1  christos /*
    291  1.1  christos  * Basic ia_na sanity checks.
    292  1.1  christos  * Verify that the ia_na code does some sanity checking.
    293  1.1  christos  */
    294  1.1  christos 
    295  1.1  christos ATF_TC(ia_na_negative);
    296  1.1  christos ATF_TC_HEAD(ia_na_negative, tc)
    297  1.1  christos {
    298  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that IA_NA option "
    299  1.1  christos                       "code can handle various negative scenarios.");
    300  1.1  christos }
    301  1.1  christos ATF_TC_BODY(ia_na_negative, tc)
    302  1.1  christos {
    303  1.1  christos     uint32_t iaid;
    304  1.1  christos     struct ia_xx *ia_na;
    305  1.1  christos     struct ia_xx *ia_na_copy;
    306  1.1  christos 
    307  1.1  christos     /* set up dhcp globals */
    308  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    309  1.1  christos 			NULL, NULL);
    310  1.1  christos 
    311  1.1  christos     /* tests */
    312  1.1  christos     /* bogus allocate arguments */
    313  1.1  christos     if (ia_allocate(NULL, 123, "", 0, MDL) != DHCP_R_INVALIDARG) {
    314  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    315  1.1  christos     }
    316  1.1  christos     ia_na = (struct ia_xx *)1;
    317  1.1  christos     if (ia_allocate(&ia_na, 456, "", 0, MDL) != DHCP_R_INVALIDARG) {
    318  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    319  1.1  christos     }
    320  1.1  christos 
    321  1.1  christos     /* bogus reference arguments */
    322  1.1  christos     iaid = 666;
    323  1.1  christos     ia_na = NULL;
    324  1.1  christos     if (ia_allocate(&ia_na, iaid, "TestDUID", 8, MDL) != ISC_R_SUCCESS) {
    325  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    326  1.1  christos     }
    327  1.1  christos     if (ia_reference(NULL, ia_na, MDL) != DHCP_R_INVALIDARG) {
    328  1.1  christos         atf_tc_fail("ERROR: ia_reference() %s:%d", MDL);
    329  1.1  christos     }
    330  1.1  christos     ia_na_copy = (struct ia_xx *)1;
    331  1.1  christos     if (ia_reference(&ia_na_copy, ia_na, MDL) != DHCP_R_INVALIDARG) {
    332  1.1  christos         atf_tc_fail("ERROR: ia_reference() %s:%d", MDL);
    333  1.1  christos     }
    334  1.1  christos     ia_na_copy = NULL;
    335  1.1  christos     if (ia_reference(&ia_na_copy, NULL, MDL) != DHCP_R_INVALIDARG) {
    336  1.1  christos         atf_tc_fail("ERROR: ia_reference() %s:%d", MDL);
    337  1.1  christos     }
    338  1.1  christos     if (ia_dereference(&ia_na, MDL) != ISC_R_SUCCESS) {
    339  1.1  christos         atf_tc_fail("ERROR: ia_dereference() %s:%d", MDL);
    340  1.1  christos     }
    341  1.1  christos 
    342  1.1  christos     /* bogus dereference arguments */
    343  1.1  christos     if (ia_dereference(NULL, MDL) != DHCP_R_INVALIDARG) {
    344  1.1  christos         atf_tc_fail("ERROR: ia_dereference() %s:%d", MDL);
    345  1.1  christos     }
    346  1.1  christos 
    347  1.1  christos     /* bogus remove */
    348  1.1  christos     iaid = 666;
    349  1.1  christos     ia_na = NULL;
    350  1.1  christos     if (ia_allocate(&ia_na, iaid, "TestDUID", 8, MDL) != ISC_R_SUCCESS) {
    351  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    352  1.1  christos     }
    353  1.1  christos     ia_remove_iasubopt(ia_na, NULL, MDL);
    354  1.1  christos     if (ia_dereference(&ia_na, MDL) != ISC_R_SUCCESS) {
    355  1.1  christos         atf_tc_fail("ERROR: ia_dereference() %s:%d", MDL);
    356  1.1  christos     }
    357  1.1  christos }
    358  1.1  christos 
    359  1.1  christos /*
    360  1.1  christos  * Basic ipv6_pool manipulation.
    361  1.1  christos  * Verify that basic pool operations work properly.
    362  1.1  christos  * The operations include creating a pool and creating,
    363  1.1  christos  * renewing, expiring, releasing and declining addresses.
    364  1.1  christos  */
    365  1.1  christos 
    366  1.1  christos ATF_TC(ipv6_pool_basic);
    367  1.1  christos ATF_TC_HEAD(ipv6_pool_basic, tc)
    368  1.1  christos {
    369  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that IPv6 pool "
    370  1.1  christos                       "manipulation is possible.");
    371  1.1  christos }
    372  1.1  christos ATF_TC_BODY(ipv6_pool_basic, tc)
    373  1.1  christos {
    374  1.1  christos     struct iasubopt *iaaddr;
    375  1.1  christos     struct in6_addr addr;
    376  1.1  christos     struct ipv6_pool *pool;
    377  1.1  christos     struct ipv6_pool *pool_copy;
    378  1.1  christos     char addr_buf[INET6_ADDRSTRLEN];
    379  1.1  christos     char *uid;
    380  1.1  christos     struct data_string ds;
    381  1.1  christos     struct iasubopt *expired_iaaddr;
    382  1.1  christos     unsigned int attempts;
    383  1.1  christos 
    384  1.1  christos     /* set up dhcp globals */
    385  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    386  1.1  christos 			NULL, NULL);
    387  1.1  christos 
    388  1.1  christos     /* and other common arguments */
    389  1.1  christos     inet_pton(AF_INET6, "1:2:3:4::", &addr);
    390  1.1  christos 
    391  1.1  christos     uid = "client0";
    392  1.1  christos     memset(&ds, 0, sizeof(ds));
    393  1.1  christos     ds.len = strlen(uid);
    394  1.1  christos     if (!buffer_allocate(&ds.buffer, ds.len, MDL)) {
    395  1.1  christos         atf_tc_fail("Out of memory");
    396  1.1  christos     }
    397  1.1  christos     ds.data = ds.buffer->data;
    398  1.1  christos     memcpy((char *)ds.data, uid, ds.len);
    399  1.1  christos 
    400  1.1  christos     /* tests */
    401  1.1  christos     /* allocate, reference */
    402  1.1  christos     pool = NULL;
    403  1.1  christos     if (ipv6_pool_allocate(&pool, D6O_IA_NA, &addr,
    404  1.1  christos                            64, 128, MDL) != ISC_R_SUCCESS) {
    405  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_allocate() %s:%d", MDL);
    406  1.1  christos     }
    407  1.1  christos     if (pool->num_active != 0) {
    408  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    409  1.1  christos     }
    410  1.1  christos     if (pool->bits != 64) {
    411  1.1  christos         atf_tc_fail("ERROR: bad bits %s:%d", MDL);
    412  1.1  christos     }
    413  1.1  christos     inet_ntop(AF_INET6, &pool->start_addr, addr_buf, sizeof(addr_buf));
    414  1.1  christos     if (strcmp(inet_ntop(AF_INET6, &pool->start_addr, addr_buf,
    415  1.1  christos                          sizeof(addr_buf)), "1:2:3:4::") != 0) {
    416  1.1  christos         atf_tc_fail("ERROR: bad start_addr %s:%d", MDL);
    417  1.1  christos     }
    418  1.1  christos     pool_copy = NULL;
    419  1.1  christos     if (ipv6_pool_reference(&pool_copy, pool, MDL) != ISC_R_SUCCESS) {
    420  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_reference() %s:%d", MDL);
    421  1.1  christos     }
    422  1.1  christos 
    423  1.1  christos     /* create_lease6, renew_lease6, expire_lease6 */
    424  1.1  christos     iaaddr = NULL;
    425  1.1  christos     if (create_lease6(pool, &iaaddr,
    426  1.1  christos                       &attempts, &ds, 1) != ISC_R_SUCCESS) {
    427  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    428  1.1  christos     }
    429  1.1  christos     if (pool->num_inactive != 1) {
    430  1.1  christos         atf_tc_fail("ERROR: bad num_inactive %s:%d", MDL);
    431  1.1  christos     }
    432  1.1  christos     if (renew_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    433  1.1  christos         atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    434  1.1  christos     }
    435  1.1  christos     if (pool->num_active != 1) {
    436  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    437  1.1  christos     }
    438  1.1  christos     expired_iaaddr = NULL;
    439  1.1  christos     if (expire_lease6(&expired_iaaddr, pool, 0) != ISC_R_SUCCESS) {
    440  1.1  christos         atf_tc_fail("ERROR: expire_lease6() %s:%d", MDL);
    441  1.1  christos     }
    442  1.1  christos     if (expired_iaaddr != NULL) {
    443  1.1  christos         atf_tc_fail("ERROR: should not have expired a lease %s:%d", MDL);
    444  1.1  christos     }
    445  1.1  christos     if (pool->num_active != 1) {
    446  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    447  1.1  christos     }
    448  1.1  christos     if (expire_lease6(&expired_iaaddr, pool, 1000) != ISC_R_SUCCESS) {
    449  1.1  christos         atf_tc_fail("ERROR: expire_lease6() %s:%d", MDL);
    450  1.1  christos     }
    451  1.1  christos     if (expired_iaaddr == NULL) {
    452  1.1  christos         atf_tc_fail("ERROR: should have expired a lease %s:%d", MDL);
    453  1.1  christos     }
    454  1.1  christos     if (iasubopt_dereference(&expired_iaaddr, MDL) != ISC_R_SUCCESS) {
    455  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    456  1.1  christos     }
    457  1.1  christos     if (pool->num_active != 0) {
    458  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    459  1.1  christos     }
    460  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    461  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    462  1.1  christos     }
    463  1.1  christos 
    464  1.1  christos     /* release_lease6, decline_lease6 */
    465  1.1  christos     if (create_lease6(pool, &iaaddr, &attempts,
    466  1.1  christos               &ds, 1) != ISC_R_SUCCESS) {
    467  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    468  1.1  christos     }
    469  1.1  christos     if (renew_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    470  1.1  christos         atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    471  1.1  christos     }
    472  1.1  christos     if (pool->num_active != 1) {
    473  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    474  1.1  christos     }
    475  1.1  christos     if (release_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    476  1.1  christos         atf_tc_fail("ERROR: decline_lease6() %s:%d", MDL);
    477  1.1  christos     }
    478  1.1  christos     if (pool->num_active != 0) {
    479  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    480  1.1  christos     }
    481  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    482  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    483  1.1  christos     }
    484  1.1  christos     if (create_lease6(pool, &iaaddr, &attempts,
    485  1.1  christos               &ds, 1) != ISC_R_SUCCESS) {
    486  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    487  1.1  christos     }
    488  1.1  christos     if (renew_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    489  1.1  christos         atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    490  1.1  christos     }
    491  1.1  christos     if (pool->num_active != 1) {
    492  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    493  1.1  christos     }
    494  1.1  christos     if (decline_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    495  1.1  christos         atf_tc_fail("ERROR: decline_lease6() %s:%d", MDL);
    496  1.1  christos     }
    497  1.1  christos     if (pool->num_active != 1) {
    498  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    499  1.1  christos     }
    500  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    501  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    502  1.1  christos     }
    503  1.1  christos 
    504  1.1  christos     /* dereference */
    505  1.1  christos     if (ipv6_pool_dereference(&pool, MDL) != ISC_R_SUCCESS) {
    506  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_reference() %s:%d", MDL);
    507  1.1  christos     }
    508  1.1  christos     if (ipv6_pool_dereference(&pool_copy, MDL) != ISC_R_SUCCESS) {
    509  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_reference() %s:%d", MDL);
    510  1.1  christos     }
    511  1.1  christos }
    512  1.1  christos 
    513  1.1  christos /*
    514  1.1  christos  * Basic ipv6_pool sanity checks.
    515  1.1  christos  * Verify that the ipv6_pool code does some sanity checking.
    516  1.1  christos  */
    517  1.1  christos 
    518  1.1  christos ATF_TC(ipv6_pool_negative);
    519  1.1  christos ATF_TC_HEAD(ipv6_pool_negative, tc)
    520  1.1  christos {
    521  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that IPv6 pool "
    522  1.1  christos                       "can handle negative cases.");
    523  1.1  christos }
    524  1.1  christos ATF_TC_BODY(ipv6_pool_negative, tc)
    525  1.1  christos {
    526  1.1  christos     struct in6_addr addr;
    527  1.1  christos     struct ipv6_pool *pool;
    528  1.1  christos     struct ipv6_pool *pool_copy;
    529  1.1  christos 
    530  1.1  christos     /* set up dhcp globals */
    531  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    532  1.1  christos 			NULL, NULL);
    533  1.1  christos 
    534  1.1  christos     /* and other common arguments */
    535  1.1  christos     inet_pton(AF_INET6, "1:2:3:4::", &addr);
    536  1.1  christos 
    537  1.1  christos     /* tests */
    538  1.1  christos     if (ipv6_pool_allocate(NULL, D6O_IA_NA, &addr,
    539  1.1  christos                            64, 128, MDL) != DHCP_R_INVALIDARG) {
    540  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_allocate() %s:%d", MDL);
    541  1.1  christos     }
    542  1.1  christos     pool = (struct ipv6_pool *)1;
    543  1.1  christos     if (ipv6_pool_allocate(&pool, D6O_IA_NA, &addr,
    544  1.1  christos                            64, 128, MDL) != DHCP_R_INVALIDARG) {
    545  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_allocate() %s:%d", MDL);
    546  1.1  christos     }
    547  1.1  christos     if (ipv6_pool_reference(NULL, pool, MDL) != DHCP_R_INVALIDARG) {
    548  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_reference() %s:%d", MDL);
    549  1.1  christos     }
    550  1.1  christos     pool_copy = (struct ipv6_pool *)1;
    551  1.1  christos     if (ipv6_pool_reference(&pool_copy, pool, MDL) != DHCP_R_INVALIDARG) {
    552  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_reference() %s:%d", MDL);
    553  1.1  christos     }
    554  1.1  christos     pool_copy = NULL;
    555  1.1  christos     if (ipv6_pool_reference(&pool_copy, NULL, MDL) != DHCP_R_INVALIDARG) {
    556  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_reference() %s:%d", MDL);
    557  1.1  christos     }
    558  1.1  christos     if (ipv6_pool_dereference(NULL, MDL) != DHCP_R_INVALIDARG) {
    559  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    560  1.1  christos     }
    561  1.1  christos     if (ipv6_pool_dereference(&pool_copy, MDL) != DHCP_R_INVALIDARG) {
    562  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    563  1.1  christos     }
    564  1.1  christos }
    565  1.1  christos 
    566  1.1  christos 
    567  1.1  christos /*
    568  1.1  christos  * Order of expiration.
    569  1.1  christos  * Add several addresses to a pool and check that
    570  1.1  christos  * they expire in the proper order.
    571  1.1  christos  */
    572  1.1  christos 
    573  1.1  christos ATF_TC(expire_order);
    574  1.1  christos ATF_TC_HEAD(expire_order, tc)
    575  1.1  christos {
    576  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that order "
    577  1.1  christos                       "of lease expiration is handled properly.");
    578  1.1  christos }
    579  1.1  christos ATF_TC_BODY(expire_order, tc)
    580  1.1  christos {
    581  1.1  christos     struct iasubopt *iaaddr;
    582  1.1  christos     struct ipv6_pool *pool;
    583  1.1  christos     struct in6_addr addr;
    584  1.1  christos         int i;
    585  1.1  christos     char *uid;
    586  1.1  christos     struct data_string ds;
    587  1.1  christos     struct iasubopt *expired_iaaddr;
    588  1.1  christos     unsigned int attempts;
    589  1.1  christos 
    590  1.1  christos     /* set up dhcp globals */
    591  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    592  1.1  christos 			NULL, NULL);
    593  1.1  christos 
    594  1.1  christos     /* and other common arguments */
    595  1.1  christos     inet_pton(AF_INET6, "1:2:3:4::", &addr);
    596  1.1  christos 
    597  1.1  christos     uid = "client0";
    598  1.1  christos     memset(&ds, 0, sizeof(ds));
    599  1.1  christos     ds.len = strlen(uid);
    600  1.1  christos     if (!buffer_allocate(&ds.buffer, ds.len, MDL)) {
    601  1.1  christos         atf_tc_fail("Out of memory");
    602  1.1  christos     }
    603  1.1  christos     ds.data = ds.buffer->data;
    604  1.1  christos     memcpy((char *)ds.data, uid, ds.len);
    605  1.1  christos 
    606  1.1  christos     iaaddr = NULL;
    607  1.1  christos     expired_iaaddr = NULL;
    608  1.1  christos 
    609  1.1  christos     /* tests */
    610  1.1  christos     pool = NULL;
    611  1.1  christos     if (ipv6_pool_allocate(&pool, D6O_IA_NA, &addr,
    612  1.1  christos                            64, 128, MDL) != ISC_R_SUCCESS) {
    613  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_allocate() %s:%d", MDL);
    614  1.1  christos     }
    615  1.1  christos 
    616  1.1  christos     for (i=10; i<100; i+=10) {
    617  1.1  christos         if (create_lease6(pool, &iaaddr, &attempts,
    618  1.1  christos                   &ds, i) != ISC_R_SUCCESS) {
    619  1.1  christos             atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    620  1.1  christos                 }
    621  1.1  christos         if (renew_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    622  1.1  christos             atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    623  1.1  christos                 }
    624  1.1  christos         if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    625  1.1  christos             atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    626  1.1  christos                 }
    627  1.1  christos         if (pool->num_active != (i / 10)) {
    628  1.1  christos             atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    629  1.1  christos                 }
    630  1.1  christos     }
    631  1.1  christos     if (pool->num_active != 9) {
    632  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    633  1.1  christos     }
    634  1.1  christos 
    635  1.1  christos     for (i=10; i<100; i+=10) {
    636  1.1  christos         if (expire_lease6(&expired_iaaddr,
    637  1.1  christos                   pool, 1000) != ISC_R_SUCCESS) {
    638  1.1  christos             atf_tc_fail("ERROR: expire_lease6() %s:%d", MDL);
    639  1.1  christos                 }
    640  1.1  christos         if (expired_iaaddr == NULL) {
    641  1.1  christos             atf_tc_fail("ERROR: should have expired a lease %s:%d",
    642  1.1  christos                    MDL);
    643  1.1  christos                 }
    644  1.1  christos         if (pool->num_active != (9 - (i / 10))) {
    645  1.1  christos             atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    646  1.1  christos                 }
    647  1.1  christos         if (expired_iaaddr->hard_lifetime_end_time != i) {
    648  1.1  christos             atf_tc_fail("ERROR: bad hard_lifetime_end_time %s:%d",
    649  1.1  christos                    MDL);
    650  1.1  christos                 }
    651  1.1  christos         if (iasubopt_dereference(&expired_iaaddr, MDL) !=
    652  1.1  christos                 ISC_R_SUCCESS) {
    653  1.1  christos             atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    654  1.1  christos                 }
    655  1.1  christos     }
    656  1.1  christos     if (pool->num_active != 0) {
    657  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    658  1.1  christos     }
    659  1.1  christos     expired_iaaddr = NULL;
    660  1.1  christos     if (expire_lease6(&expired_iaaddr, pool, 1000) != ISC_R_SUCCESS) {
    661  1.1  christos         atf_tc_fail("ERROR: expire_lease6() %s:%d", MDL);
    662  1.1  christos     }
    663  1.1  christos     if (ipv6_pool_dereference(&pool, MDL) != ISC_R_SUCCESS) {
    664  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    665  1.1  christos     }
    666  1.1  christos }
    667  1.1  christos 
    668  1.1  christos /*
    669  1.1  christos  * Reduce the expiration period of a lease.
    670  1.1  christos  * This test reduces the expiration period of
    671  1.1  christos  * a lease to verify we process reductions
    672  1.1  christos  * properly.
    673  1.1  christos  */
    674  1.1  christos ATF_TC(expire_order_reduce);
    675  1.1  christos ATF_TC_HEAD(expire_order_reduce, tc)
    676  1.1  christos {
    677  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that reducing "
    678  1.1  christos                       "the expiration time of a lease works properly.");
    679  1.1  christos }
    680  1.1  christos ATF_TC_BODY(expire_order_reduce, tc)
    681  1.1  christos {
    682  1.1  christos     struct iasubopt *iaaddr1, *iaaddr2;
    683  1.1  christos     struct ipv6_pool *pool;
    684  1.1  christos     struct in6_addr addr;
    685  1.1  christos     char *uid;
    686  1.1  christos     struct data_string ds;
    687  1.1  christos     struct iasubopt *expired_iaaddr;
    688  1.1  christos     unsigned int attempts;
    689  1.1  christos 
    690  1.1  christos     /* set up dhcp globals */
    691  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    692  1.1  christos 			NULL, NULL);
    693  1.1  christos 
    694  1.1  christos     /* and other common arguments */
    695  1.1  christos     inet_pton(AF_INET6, "1:2:3:4::", &addr);
    696  1.1  christos 
    697  1.1  christos     uid = "client0";
    698  1.1  christos     memset(&ds, 0, sizeof(ds));
    699  1.1  christos     ds.len = strlen(uid);
    700  1.1  christos     if (!buffer_allocate(&ds.buffer, ds.len, MDL)) {
    701  1.1  christos         atf_tc_fail("Out of memory");
    702  1.1  christos     }
    703  1.1  christos     ds.data = ds.buffer->data;
    704  1.1  christos     memcpy((char *)ds.data, uid, ds.len);
    705  1.1  christos 
    706  1.1  christos     pool = NULL;
    707  1.1  christos     iaaddr1 = NULL;
    708  1.1  christos     iaaddr2 = NULL;
    709  1.1  christos     expired_iaaddr = NULL;
    710  1.1  christos 
    711  1.1  christos     /*
    712  1.1  christos      * Add two leases iaaddr1 with expire time of 200
    713  1.1  christos      * and iaaddr2 with expire time of 300.  Then update
    714  1.1  christos      * iaaddr2 to expire in 100 instead.  This should cause
    715  1.1  christos      * iaaddr2 to move with the hash list.
    716  1.1  christos      */
    717  1.1  christos     /* create pool and add iaaddr1 and iaaddr2 */
    718  1.1  christos     if (ipv6_pool_allocate(&pool, D6O_IA_NA, &addr,
    719  1.1  christos                            64, 128, MDL) != ISC_R_SUCCESS) {
    720  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_allocate() %s:%d", MDL);
    721  1.1  christos     }
    722  1.1  christos     if (create_lease6(pool, &iaaddr1, &attempts, &ds, 200) != ISC_R_SUCCESS) {
    723  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    724  1.1  christos     }
    725  1.1  christos     if (renew_lease6(pool, iaaddr1) != ISC_R_SUCCESS) {
    726  1.1  christos             atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    727  1.1  christos     }
    728  1.1  christos     if (create_lease6(pool, &iaaddr2, &attempts, &ds, 300) != ISC_R_SUCCESS) {
    729  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    730  1.1  christos     }
    731  1.1  christos     if (renew_lease6(pool, iaaddr2) != ISC_R_SUCCESS) {
    732  1.1  christos             atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    733  1.1  christos     }
    734  1.1  christos 
    735  1.1  christos     /* verify pool */
    736  1.1  christos     if (pool->num_active != 2) {
    737  1.1  christos         atf_tc_fail("ERROR: bad num_active %s:%d", MDL);
    738  1.1  christos     }
    739  1.1  christos 
    740  1.1  christos     /* reduce lease for iaaddr2 */
    741  1.1  christos     iaaddr2->soft_lifetime_end_time = 100;
    742  1.1  christos     if (renew_lease6(pool, iaaddr2) != ISC_R_SUCCESS) {
    743  1.1  christos             atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    744  1.1  christos     }
    745  1.1  christos 
    746  1.1  christos     /* expire a lease, it should be iaaddr2 with an expire time of 100 */
    747  1.1  christos     if (expire_lease6(&expired_iaaddr, pool, 1000) != ISC_R_SUCCESS) {
    748  1.1  christos         atf_tc_fail("ERROR: expire_lease6() %s:%d", MDL);
    749  1.1  christos     }
    750  1.1  christos     if (expired_iaaddr == NULL) {
    751  1.1  christos         atf_tc_fail("ERROR: should have expired a lease %s:%d", MDL);
    752  1.1  christos     }
    753  1.1  christos     if (expired_iaaddr != iaaddr2) {
    754  1.1  christos         atf_tc_fail("Error: incorrect lease expired %s:%d", MDL);
    755  1.1  christos     }
    756  1.1  christos     if (expired_iaaddr->hard_lifetime_end_time != 100) {
    757  1.1  christos         atf_tc_fail("ERROR: bad hard_lifetime_end_time %s:%d", MDL);
    758  1.1  christos     }
    759  1.1  christos     if (iasubopt_dereference(&expired_iaaddr, MDL) != ISC_R_SUCCESS) {
    760  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    761  1.1  christos     }
    762  1.1  christos 
    763  1.1  christos     /* expire a lease, it should be iaaddr1 with an expire time of 200 */
    764  1.1  christos     if (expire_lease6(&expired_iaaddr, pool, 1000) != ISC_R_SUCCESS) {
    765  1.1  christos         atf_tc_fail("ERROR: expire_lease6() %s:%d", MDL);
    766  1.1  christos     }
    767  1.1  christos     if (expired_iaaddr == NULL) {
    768  1.1  christos         atf_tc_fail("ERROR: should have expired a lease %s:%d", MDL);
    769  1.1  christos     }
    770  1.1  christos     if (expired_iaaddr != iaaddr1) {
    771  1.1  christos         atf_tc_fail("Error: incorrect lease expired %s:%d", MDL);
    772  1.1  christos     }
    773  1.1  christos     if (expired_iaaddr->hard_lifetime_end_time != 200) {
    774  1.1  christos         atf_tc_fail("ERROR: bad hard_lifetime_end_time %s:%d", MDL);
    775  1.1  christos     }
    776  1.1  christos     if (iasubopt_dereference(&expired_iaaddr, MDL) != ISC_R_SUCCESS) {
    777  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    778  1.1  christos     }
    779  1.1  christos 
    780  1.1  christos     /* cleanup */
    781  1.1  christos     if (iasubopt_dereference(&iaaddr1, MDL) != ISC_R_SUCCESS) {
    782  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    783  1.1  christos     }
    784  1.1  christos     if (iasubopt_dereference(&iaaddr2, MDL) != ISC_R_SUCCESS) {
    785  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    786  1.1  christos     }
    787  1.1  christos     if (ipv6_pool_dereference(&pool, MDL) != ISC_R_SUCCESS) {
    788  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    789  1.1  christos     }
    790  1.1  christos }
    791  1.1  christos 
    792  1.1  christos /*
    793  1.1  christos  * Small pool.
    794  1.1  christos  * check that a small pool behaves properly.
    795  1.1  christos  */
    796  1.1  christos 
    797  1.1  christos ATF_TC(small_pool);
    798  1.1  christos ATF_TC_HEAD(small_pool, tc)
    799  1.1  christos {
    800  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that small pool "
    801  1.1  christos                       "is handled properly.");
    802  1.1  christos }
    803  1.1  christos ATF_TC_BODY(small_pool, tc)
    804  1.1  christos {
    805  1.1  christos     struct in6_addr addr;
    806  1.1  christos     struct ipv6_pool *pool;
    807  1.1  christos     struct iasubopt *iaaddr;
    808  1.1  christos     char *uid;
    809  1.1  christos     struct data_string ds;
    810  1.1  christos     unsigned int attempts;
    811  1.1  christos 
    812  1.1  christos     /* set up dhcp globals */
    813  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    814  1.1  christos 			NULL, NULL);
    815  1.1  christos 
    816  1.1  christos     /* and other common arguments */
    817  1.1  christos     inet_pton(AF_INET6, "1:2:3:4::", &addr);
    818  1.1  christos     addr.s6_addr[14] = 0x81;
    819  1.1  christos 
    820  1.1  christos     uid = "client0";
    821  1.1  christos     memset(&ds, 0, sizeof(ds));
    822  1.1  christos     ds.len = strlen(uid);
    823  1.1  christos     if (!buffer_allocate(&ds.buffer, ds.len, MDL)) {
    824  1.1  christos         atf_tc_fail("Out of memory");
    825  1.1  christos     }
    826  1.1  christos     ds.data = ds.buffer->data;
    827  1.1  christos     memcpy((char *)ds.data, uid, ds.len);
    828  1.1  christos 
    829  1.1  christos     pool = NULL;
    830  1.1  christos     iaaddr = NULL;
    831  1.1  christos 
    832  1.1  christos     /* tests */
    833  1.1  christos     if (ipv6_pool_allocate(&pool, D6O_IA_NA, &addr,
    834  1.1  christos                            127, 128, MDL) != ISC_R_SUCCESS) {
    835  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_allocate() %s:%d", MDL);
    836  1.1  christos     }
    837  1.1  christos 
    838  1.1  christos     if (create_lease6(pool, &iaaddr, &attempts,
    839  1.1  christos               &ds, 42) != ISC_R_SUCCESS) {
    840  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    841  1.1  christos     }
    842  1.1  christos     if (renew_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    843  1.1  christos         atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    844  1.1  christos     }
    845  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    846  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    847  1.1  christos     }
    848  1.1  christos     if (create_lease6(pool, &iaaddr, &attempts,
    849  1.1  christos               &ds, 11) != ISC_R_SUCCESS) {
    850  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    851  1.1  christos     }
    852  1.1  christos     if (renew_lease6(pool, iaaddr) != ISC_R_SUCCESS) {
    853  1.1  christos         atf_tc_fail("ERROR: renew_lease6() %s:%d", MDL);
    854  1.1  christos     }
    855  1.1  christos     if (iasubopt_dereference(&iaaddr, MDL) != ISC_R_SUCCESS) {
    856  1.1  christos         atf_tc_fail("ERROR: iasubopt_dereference() %s:%d", MDL);
    857  1.1  christos     }
    858  1.1  christos     if (create_lease6(pool, &iaaddr, &attempts,
    859  1.1  christos               &ds, 11) != ISC_R_NORESOURCES) {
    860  1.1  christos         atf_tc_fail("ERROR: create_lease6() %s:%d", MDL);
    861  1.1  christos     }
    862  1.1  christos     if (ipv6_pool_dereference(&pool, MDL) != ISC_R_SUCCESS) {
    863  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    864  1.1  christos     }
    865  1.1  christos }
    866  1.1  christos 
    867  1.1  christos /*
    868  1.1  christos  * Address to pool mapping.
    869  1.1  christos  * Verify that we find the proper pool for an address
    870  1.1  christos  * or don't find a pool if we don't have one for the given
    871  1.1  christos  * address.
    872  1.1  christos  */
    873  1.1  christos ATF_TC(many_pools);
    874  1.1  christos ATF_TC_HEAD(many_pools, tc)
    875  1.1  christos {
    876  1.1  christos     atf_tc_set_md_var(tc, "descr", "This test case checks that functions "
    877  1.1  christos                       "across all pools are working correctly.");
    878  1.1  christos }
    879  1.1  christos ATF_TC_BODY(many_pools, tc)
    880  1.1  christos {
    881  1.1  christos     struct in6_addr addr;
    882  1.1  christos     struct ipv6_pool *pool;
    883  1.1  christos 
    884  1.1  christos     /* set up dhcp globals */
    885  1.1  christos     dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
    886  1.1  christos 			NULL, NULL);
    887  1.1  christos 
    888  1.1  christos     /* and other common arguments */
    889  1.1  christos     inet_pton(AF_INET6, "1:2:3:4::", &addr);
    890  1.1  christos 
    891  1.1  christos     /* tests */
    892  1.1  christos 
    893  1.1  christos     pool = NULL;
    894  1.1  christos     if (ipv6_pool_allocate(&pool, D6O_IA_NA, &addr,
    895  1.1  christos                            64, 128, MDL) != ISC_R_SUCCESS) {
    896  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_allocate() %s:%d", MDL);
    897  1.1  christos     }
    898  1.1  christos     if (add_ipv6_pool(pool) != ISC_R_SUCCESS) {
    899  1.1  christos         atf_tc_fail("ERROR: add_ipv6_pool() %s:%d", MDL);
    900  1.1  christos     }
    901  1.1  christos     if (ipv6_pool_dereference(&pool, MDL) != ISC_R_SUCCESS) {
    902  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    903  1.1  christos     }
    904  1.1  christos     pool = NULL;
    905  1.1  christos     if (find_ipv6_pool(&pool, D6O_IA_NA, &addr) != ISC_R_SUCCESS) {
    906  1.1  christos         atf_tc_fail("ERROR: find_ipv6_pool() %s:%d", MDL);
    907  1.1  christos     }
    908  1.1  christos     if (ipv6_pool_dereference(&pool, MDL) != ISC_R_SUCCESS) {
    909  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    910  1.1  christos     }
    911  1.1  christos     inet_pton(AF_INET6, "1:2:3:4:ffff:ffff:ffff:ffff", &addr);
    912  1.1  christos     pool = NULL;
    913  1.1  christos     if (find_ipv6_pool(&pool, D6O_IA_NA, &addr) != ISC_R_SUCCESS) {
    914  1.1  christos         atf_tc_fail("ERROR: find_ipv6_pool() %s:%d", MDL);
    915  1.1  christos     }
    916  1.1  christos     if (ipv6_pool_dereference(&pool, MDL) != ISC_R_SUCCESS) {
    917  1.1  christos         atf_tc_fail("ERROR: ipv6_pool_dereference() %s:%d", MDL);
    918  1.1  christos     }
    919  1.1  christos     inet_pton(AF_INET6, "1:2:3:5::", &addr);
    920  1.1  christos     pool = NULL;
    921  1.1  christos     if (find_ipv6_pool(&pool, D6O_IA_NA, &addr) != ISC_R_NOTFOUND) {
    922  1.1  christos         atf_tc_fail("ERROR: find_ipv6_pool() %s:%d", MDL);
    923  1.1  christos     }
    924  1.1  christos     inet_pton(AF_INET6, "1:2:3:3:ffff:ffff:ffff:ffff", &addr);
    925  1.1  christos     pool = NULL;
    926  1.1  christos     if (find_ipv6_pool(&pool, D6O_IA_NA, &addr) != ISC_R_NOTFOUND) {
    927  1.1  christos         atf_tc_fail("ERROR: find_ipv6_pool() %s:%d", MDL);
    928  1.1  christos     }
    929  1.1  christos 
    930  1.1  christos /*  iaid = 666;
    931  1.1  christos     ia_na = NULL;
    932  1.1  christos     if (ia_allocate(&ia_na, iaid, "TestDUID", 8, MDL) != ISC_R_SUCCESS) {
    933  1.1  christos         atf_tc_fail("ERROR: ia_allocate() %s:%d", MDL);
    934  1.1  christos     }*/
    935  1.1  christos 
    936  1.1  christos     {
    937  1.1  christos         struct in6_addr r;
    938  1.1  christos         struct data_string ds;
    939  1.1  christos         u_char data[16];
    940  1.1  christos         char buf[64];
    941  1.1  christos         int i, j;
    942  1.1  christos 
    943  1.1  christos         memset(&ds, 0, sizeof(ds));
    944  1.1  christos         memset(data, 0xaa, sizeof(data));
    945  1.1  christos         ds.len = 16;
    946  1.1  christos         ds.data = data;
    947  1.1  christos 
    948  1.1  christos         inet_pton(AF_INET6, "3ffe:501:ffff:100::", &addr);
    949  1.1  christos         for (i = 32; i < 42; i++)
    950  1.1  christos             for (j = i + 1; j < 49; j++) {
    951  1.1  christos                 memset(&r, 0, sizeof(r));
    952  1.1  christos                 memset(buf, 0, 64);
    953  1.1  christos                 build_prefix6(&r, &addr, i, j, &ds);
    954  1.1  christos                 inet_ntop(AF_INET6, &r, buf, 64);
    955  1.1  christos                 printf("%d,%d-> %s/%d\n", i, j, buf, j);
    956  1.1  christos             }
    957  1.1  christos     }
    958  1.1  christos }
    959  1.1  christos 
    960  1.1  christos ATF_TP_ADD_TCS(tp)
    961  1.1  christos {
    962  1.1  christos     ATF_TP_ADD_TC(tp, iaaddr_basic);
    963  1.1  christos     ATF_TP_ADD_TC(tp, iaaddr_negative);
    964  1.1  christos     ATF_TP_ADD_TC(tp, ia_na_basic);
    965  1.1  christos     ATF_TP_ADD_TC(tp, ia_na_manyaddrs);
    966  1.1  christos     ATF_TP_ADD_TC(tp, ia_na_negative);
    967  1.1  christos     ATF_TP_ADD_TC(tp, ipv6_pool_basic);
    968  1.1  christos     ATF_TP_ADD_TC(tp, ipv6_pool_negative);
    969  1.1  christos     ATF_TP_ADD_TC(tp, expire_order);
    970  1.1  christos     ATF_TP_ADD_TC(tp, expire_order_reduce);
    971  1.1  christos     ATF_TP_ADD_TC(tp, small_pool);
    972  1.1  christos     ATF_TP_ADD_TC(tp, many_pools);
    973  1.1  christos 
    974  1.1  christos     return (atf_no_error());
    975  1.1  christos }
    976