Home | History | Annotate | Line # | Download | only in tests
option_unittest.c revision 1.2.10.1
      1  1.2.10.1      cjep /*	$NetBSD: option_unittest.c,v 1.2.10.1 2021/05/31 22:15:05 cjep Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*
      4  1.2.10.1      cjep  * Copyright (C) 2018-2021 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 #include <atf-c.h>
     21       1.1  christos #include "dhcpd.h"
     22       1.1  christos 
     23       1.1  christos ATF_TC(option_refcnt);
     24       1.1  christos 
     25       1.1  christos ATF_TC_HEAD(option_refcnt, tc)
     26       1.1  christos {
     27       1.1  christos     atf_tc_set_md_var(tc, "descr",
     28       1.1  christos 		      "Verify option reference count does not overflow.");
     29       1.1  christos }
     30       1.1  christos 
     31       1.1  christos /* This test does a simple check to see if option reference count is
     32       1.1  christos  * decremented even an error path exiting parse_option_buffer()
     33       1.1  christos  */
     34       1.1  christos ATF_TC_BODY(option_refcnt, tc)
     35       1.1  christos {
     36       1.1  christos     struct option_state *options;
     37       1.1  christos     struct option *option;
     38       1.1  christos     unsigned code;
     39       1.1  christos     int refcnt;
     40       1.1  christos     unsigned char buffer[3] = { 15, 255, 0 };
     41       1.1  christos 
     42       1.1  christos     initialize_common_option_spaces();
     43       1.1  christos 
     44       1.1  christos     options = NULL;
     45       1.1  christos     if (!option_state_allocate(&options, MDL)) {
     46       1.1  christos 	atf_tc_fail("can't allocate option state");
     47       1.1  christos     }
     48       1.1  christos 
     49       1.1  christos     option = NULL;
     50       1.1  christos     code = 15; /* domain-name */
     51       1.1  christos     if (!option_code_hash_lookup(&option, dhcp_universe.code_hash,
     52       1.1  christos 				 &code, 0, MDL)) {
     53       1.1  christos 	atf_tc_fail("can't find option 15");
     54       1.1  christos     }
     55       1.1  christos     if (option == NULL) {
     56       1.1  christos 	atf_tc_fail("option is NULL");
     57       1.1  christos     }
     58       1.1  christos     refcnt = option->refcnt;
     59       1.1  christos 
     60       1.1  christos     buffer[0] = 15;
     61       1.1  christos     buffer[1] = 255; /* invalid */
     62       1.1  christos     buffer[2] = 0;
     63       1.1  christos 
     64       1.1  christos     if (parse_option_buffer(options, buffer, 3, &dhcp_universe)) {
     65       1.1  christos 	atf_tc_fail("parse_option_buffer is expected to fail");
     66       1.1  christos     }
     67       1.1  christos 
     68       1.1  christos     if (refcnt != option->refcnt) {
     69       1.1  christos 	atf_tc_fail("refcnt changed from %d to %d", refcnt, option->refcnt);
     70       1.1  christos     }
     71       1.1  christos }
     72       1.1  christos 
     73       1.1  christos ATF_TC(pretty_print_option);
     74       1.1  christos 
     75       1.1  christos ATF_TC_HEAD(pretty_print_option, tc)
     76       1.1  christos {
     77       1.1  christos     atf_tc_set_md_var(tc, "descr",
     78       1.1  christos 		      "Verify pretty_print_option does not overrun its buffer.");
     79       1.1  christos }
     80       1.1  christos 
     81       1.1  christos 
     82       1.1  christos /*
     83       1.1  christos  * This test verifies that pretty_print_option() will not overrun its
     84       1.1  christos  * internal, static buffer when given large 'x/X' format options.
     85       1.1  christos  *
     86       1.1  christos  */
     87       1.1  christos ATF_TC_BODY(pretty_print_option, tc)
     88       1.1  christos {
     89       1.1  christos     struct option *option;
     90       1.1  christos     unsigned code;
     91       1.1  christos     unsigned char bad_data[32*1024];
     92       1.1  christos     unsigned char good_data[] = { 1,2,3,4,5,6 };
     93       1.1  christos     int emit_commas = 1;
     94       1.1  christos     int emit_quotes = 1;
     95       1.1  christos     const char *output_buf;
     96       1.1  christos 
     97       1.1  christos     /* Initialize whole thing to non-printable chars */
     98       1.1  christos     memset(bad_data, 0x1f, sizeof(bad_data));
     99       1.1  christos 
    100       1.1  christos     initialize_common_option_spaces();
    101       1.1  christos 
    102       1.1  christos     /* We'll use dhcp_client_identitifer because it happens to be format X */
    103       1.1  christos     code = 61;
    104       1.1  christos     option = NULL;
    105       1.1  christos     if (!option_code_hash_lookup(&option, dhcp_universe.code_hash,
    106       1.1  christos 				 &code, 0, MDL)) {
    107       1.1  christos 	    atf_tc_fail("can't find option %d", code);
    108       1.1  christos     }
    109       1.1  christos 
    110       1.1  christos     if (option == NULL) {
    111       1.1  christos 	    atf_tc_fail("option is NULL");
    112       1.1  christos     }
    113       1.1  christos 
    114       1.1  christos     /* First we will try a good value we know should fit. */
    115       1.1  christos     output_buf = pretty_print_option (option, good_data, sizeof(good_data),
    116       1.1  christos                                       emit_commas, emit_quotes);
    117       1.1  christos 
    118       1.1  christos     /* Make sure we get what we expect */
    119       1.1  christos     if (!output_buf || strcmp(output_buf, "1:2:3:4:5:6")) {
    120       1.1  christos 	    atf_tc_fail("pretty_print_option did not return \"<error>\"");
    121       1.1  christos     }
    122       1.1  christos 
    123       1.1  christos 
    124       1.1  christos     /* Now we'll try a data value that's too large */
    125       1.1  christos     output_buf = pretty_print_option (option, bad_data, sizeof(bad_data),
    126       1.1  christos                                       emit_commas, emit_quotes);
    127       1.1  christos 
    128       1.1  christos     /* Make sure we safely get an error */
    129       1.1  christos     if (!output_buf || strcmp(output_buf, "<error>")) {
    130       1.1  christos 	    atf_tc_fail("pretty_print_option did not return \"<error>\"");
    131       1.1  christos     }
    132       1.1  christos }
    133       1.1  christos 
    134  1.2.10.1      cjep ATF_TC(parse_X);
    135  1.2.10.1      cjep 
    136  1.2.10.1      cjep ATF_TC_HEAD(parse_X, tc)
    137  1.2.10.1      cjep {
    138  1.2.10.1      cjep     atf_tc_set_md_var(tc, "descr",
    139  1.2.10.1      cjep 		      "Verify parse_X survices option too big.");
    140  1.2.10.1      cjep }
    141  1.2.10.1      cjep 
    142  1.2.10.1      cjep /* Initializes a parse struct from an input buffer of data. */
    143  1.2.10.1      cjep static void init_parse(struct parse *cfile, char* name, char *input) {
    144  1.2.10.1      cjep     memset(cfile, 0, sizeof(struct parse));
    145  1.2.10.1      cjep     cfile->tlname = name;
    146  1.2.10.1      cjep     cfile->lpos = cfile->line = 1;
    147  1.2.10.1      cjep     cfile->cur_line = cfile->line1;
    148  1.2.10.1      cjep     cfile->prev_line = cfile->line2;
    149  1.2.10.1      cjep     cfile->token_line = cfile->cur_line;
    150  1.2.10.1      cjep     cfile->cur_line[0] = cfile->prev_line[0] = 0;
    151  1.2.10.1      cjep     cfile->file = -1;
    152  1.2.10.1      cjep     cfile->eol_token = 0;
    153  1.2.10.1      cjep 
    154  1.2.10.1      cjep     cfile->inbuf = input;
    155  1.2.10.1      cjep     cfile->buflen = strlen(input);
    156  1.2.10.1      cjep     cfile->bufsiz = 0;
    157  1.2.10.1      cjep }
    158  1.2.10.1      cjep 
    159  1.2.10.1      cjep /*
    160  1.2.10.1      cjep  * This test verifies that parse_X does not overwrite the output
    161  1.2.10.1      cjep  * buffer when given input data that exceeds the output buffer
    162  1.2.10.1      cjep  * capacity.
    163  1.2.10.1      cjep */
    164  1.2.10.1      cjep ATF_TC_BODY(parse_X, tc)
    165  1.2.10.1      cjep {
    166  1.2.10.1      cjep     struct parse cfile;
    167  1.2.10.1      cjep     u_int8_t output[10];
    168  1.2.10.1      cjep     unsigned len;
    169  1.2.10.1      cjep 
    170  1.2.10.1      cjep     /* Input hex literal */
    171  1.2.10.1      cjep     char *input = "01:02:03:04:05:06:07:08";
    172  1.2.10.1      cjep     unsigned expected_len = 8;
    173  1.2.10.1      cjep 
    174  1.2.10.1      cjep     /* Normal output plus two filler bytes */
    175  1.2.10.1      cjep     u_int8_t expected_plus_two[] = {
    176  1.2.10.1      cjep         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xff, 0xff
    177  1.2.10.1      cjep     };
    178  1.2.10.1      cjep 
    179  1.2.10.1      cjep     /* Safe output when option is too long */
    180  1.2.10.1      cjep     unsigned short_buf_len = 4;
    181  1.2.10.1      cjep     u_int8_t expected_too_long[] = {
    182  1.2.10.1      cjep         0x01, 0x02, 0x03, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
    183  1.2.10.1      cjep     };
    184  1.2.10.1      cjep 
    185  1.2.10.1      cjep     /* First we'll run one that works normally */
    186  1.2.10.1      cjep     memset(output, 0xff, sizeof(output));
    187  1.2.10.1      cjep     init_parse(&cfile, "hex_fits", input);
    188  1.2.10.1      cjep 
    189  1.2.10.1      cjep     len = parse_X(&cfile, output, expected_len);
    190  1.2.10.1      cjep 
    191  1.2.10.1      cjep     // Len should match the expected len.
    192  1.2.10.1      cjep     if (len != expected_len) {
    193  1.2.10.1      cjep 	    atf_tc_fail("parse_X failed, output len: %d", len);
    194  1.2.10.1      cjep     }
    195  1.2.10.1      cjep 
    196  1.2.10.1      cjep     // We should not have written anything past the end of the buffer.
    197  1.2.10.1      cjep     if (memcmp(output, expected_plus_two, sizeof(output))) {
    198  1.2.10.1      cjep 	    atf_tc_fail("parse_X failed, output does not match expected");
    199  1.2.10.1      cjep     }
    200  1.2.10.1      cjep 
    201  1.2.10.1      cjep     // Now we'll try it with a buffer that's too small.
    202  1.2.10.1      cjep     init_parse(&cfile, "hex_too_long", input);
    203  1.2.10.1      cjep     memset(output, 0xff, sizeof(output));
    204  1.2.10.1      cjep 
    205  1.2.10.1      cjep     len = parse_X(&cfile, output, short_buf_len);
    206  1.2.10.1      cjep 
    207  1.2.10.1      cjep     // On errors, len should be zero.
    208  1.2.10.1      cjep     if (len != 0) {
    209  1.2.10.1      cjep 	    atf_tc_fail("parse_X failed, we should have had an error");
    210  1.2.10.1      cjep     }
    211  1.2.10.1      cjep 
    212  1.2.10.1      cjep     // We should not have written anything past the end of the buffer.
    213  1.2.10.1      cjep     if (memcmp(output, expected_too_long, sizeof(output))) {
    214  1.2.10.1      cjep         atf_tc_fail("parse_X overwrote buffer!");
    215  1.2.10.1      cjep     }
    216  1.2.10.1      cjep }
    217       1.1  christos 
    218       1.1  christos /* This macro defines main() method that will call specified
    219       1.1  christos    test cases. tp and simple_test_case names can be whatever you want
    220       1.1  christos    as long as it is a valid variable identifier. */
    221       1.1  christos ATF_TP_ADD_TCS(tp)
    222       1.1  christos {
    223       1.1  christos     ATF_TP_ADD_TC(tp, option_refcnt);
    224       1.1  christos     ATF_TP_ADD_TC(tp, pretty_print_option);
    225  1.2.10.1      cjep     ATF_TP_ADD_TC(tp, parse_X);
    226       1.1  christos 
    227       1.1  christos     return (atf_no_error());
    228       1.1  christos }
    229