option_unittest.c revision 1.1.1.1 1 1.1 christos /* $NetBSD: option_unittest.c,v 1.1.1.1 2018/04/07 22:34:26 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) 2018 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.1 christos
135 1.1 christos /* This macro defines main() method that will call specified
136 1.1 christos test cases. tp and simple_test_case names can be whatever you want
137 1.1 christos as long as it is a valid variable identifier. */
138 1.1 christos ATF_TP_ADD_TCS(tp)
139 1.1 christos {
140 1.1 christos ATF_TP_ADD_TC(tp, option_refcnt);
141 1.1 christos ATF_TP_ADD_TC(tp, pretty_print_option);
142 1.1 christos
143 1.1 christos return (atf_no_error());
144 1.1 christos }
145