Home | History | Annotate | Line # | Download | only in tests
option_unittest.c revision 1.2.2.2
      1 /*	$NetBSD: option_unittest.c,v 1.2.2.2 2018/04/16 01:59:46 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * This Source Code Form is subject to the terms of the Mozilla Public
      7  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      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 #include <config.h>
     20 #include <atf-c.h>
     21 #include "dhcpd.h"
     22 
     23 ATF_TC(option_refcnt);
     24 
     25 ATF_TC_HEAD(option_refcnt, tc)
     26 {
     27     atf_tc_set_md_var(tc, "descr",
     28 		      "Verify option reference count does not overflow.");
     29 }
     30 
     31 /* This test does a simple check to see if option reference count is
     32  * decremented even an error path exiting parse_option_buffer()
     33  */
     34 ATF_TC_BODY(option_refcnt, tc)
     35 {
     36     struct option_state *options;
     37     struct option *option;
     38     unsigned code;
     39     int refcnt;
     40     unsigned char buffer[3] = { 15, 255, 0 };
     41 
     42     initialize_common_option_spaces();
     43 
     44     options = NULL;
     45     if (!option_state_allocate(&options, MDL)) {
     46 	atf_tc_fail("can't allocate option state");
     47     }
     48 
     49     option = NULL;
     50     code = 15; /* domain-name */
     51     if (!option_code_hash_lookup(&option, dhcp_universe.code_hash,
     52 				 &code, 0, MDL)) {
     53 	atf_tc_fail("can't find option 15");
     54     }
     55     if (option == NULL) {
     56 	atf_tc_fail("option is NULL");
     57     }
     58     refcnt = option->refcnt;
     59 
     60     buffer[0] = 15;
     61     buffer[1] = 255; /* invalid */
     62     buffer[2] = 0;
     63 
     64     if (parse_option_buffer(options, buffer, 3, &dhcp_universe)) {
     65 	atf_tc_fail("parse_option_buffer is expected to fail");
     66     }
     67 
     68     if (refcnt != option->refcnt) {
     69 	atf_tc_fail("refcnt changed from %d to %d", refcnt, option->refcnt);
     70     }
     71 }
     72 
     73 ATF_TC(pretty_print_option);
     74 
     75 ATF_TC_HEAD(pretty_print_option, tc)
     76 {
     77     atf_tc_set_md_var(tc, "descr",
     78 		      "Verify pretty_print_option does not overrun its buffer.");
     79 }
     80 
     81 
     82 /*
     83  * This test verifies that pretty_print_option() will not overrun its
     84  * internal, static buffer when given large 'x/X' format options.
     85  *
     86  */
     87 ATF_TC_BODY(pretty_print_option, tc)
     88 {
     89     struct option *option;
     90     unsigned code;
     91     unsigned char bad_data[32*1024];
     92     unsigned char good_data[] = { 1,2,3,4,5,6 };
     93     int emit_commas = 1;
     94     int emit_quotes = 1;
     95     const char *output_buf;
     96 
     97     /* Initialize whole thing to non-printable chars */
     98     memset(bad_data, 0x1f, sizeof(bad_data));
     99 
    100     initialize_common_option_spaces();
    101 
    102     /* We'll use dhcp_client_identitifer because it happens to be format X */
    103     code = 61;
    104     option = NULL;
    105     if (!option_code_hash_lookup(&option, dhcp_universe.code_hash,
    106 				 &code, 0, MDL)) {
    107 	    atf_tc_fail("can't find option %d", code);
    108     }
    109 
    110     if (option == NULL) {
    111 	    atf_tc_fail("option is NULL");
    112     }
    113 
    114     /* First we will try a good value we know should fit. */
    115     output_buf = pretty_print_option (option, good_data, sizeof(good_data),
    116                                       emit_commas, emit_quotes);
    117 
    118     /* Make sure we get what we expect */
    119     if (!output_buf || strcmp(output_buf, "1:2:3:4:5:6")) {
    120 	    atf_tc_fail("pretty_print_option did not return \"<error>\"");
    121     }
    122 
    123 
    124     /* Now we'll try a data value that's too large */
    125     output_buf = pretty_print_option (option, bad_data, sizeof(bad_data),
    126                                       emit_commas, emit_quotes);
    127 
    128     /* Make sure we safely get an error */
    129     if (!output_buf || strcmp(output_buf, "<error>")) {
    130 	    atf_tc_fail("pretty_print_option did not return \"<error>\"");
    131     }
    132 }
    133 
    134 
    135 /* This macro defines main() method that will call specified
    136    test cases. tp and simple_test_case names can be whatever you want
    137    as long as it is a valid variable identifier. */
    138 ATF_TP_ADD_TCS(tp)
    139 {
    140     ATF_TP_ADD_TC(tp, option_refcnt);
    141     ATF_TP_ADD_TC(tp, pretty_print_option);
    142 
    143     return (atf_no_error());
    144 }
    145