xref: /netbsd-src/external/mpl/dhcp/dist/common/tests/domain_name_test.c (revision 56e2dc54c361727ddc9248251f043840f68d71b4)
1 /*	$NetBSD: domain_name_test.c,v 1.2 2020/08/03 21:10:56 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2019 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(pretty_print_domain_name);
24 
ATF_TC_HEAD(pretty_print_domain_name,tc)25 ATF_TC_HEAD(pretty_print_domain_name, tc)
26 {
27     atf_tc_set_md_var(tc, "descr",
28 		      "Verify pretty_print_option can render a domain name.");
29 }
30 
31 /*
32  * This test verifies that pretty_print_option() correctly render a
33  * domain name.
34  *
35  */
ATF_TC_BODY(pretty_print_domain_name,tc)36 ATF_TC_BODY(pretty_print_domain_name, tc)
37 {
38     struct option *option;
39     unsigned code;
40     unsigned char good_data[] =
41 	{ 0x05, 0x62, 0x6f, 0x6f, 0x79, 0x61, 0x03, 0x63, 0x6f, 0x6d, 0x00 };
42     unsigned char short_data[] =
43 	{ 0x05, 0x62, 0x6f, 0x6f, 0x79, 0x61, 0x03, 0x63, 0x6f, 0x6d };
44     unsigned char long_data[] =
45 	{ 0x05, 0x62, 0x6f, 0x6f, 0x79, 0x61, 0x03, 0x63, 0x6f, 0x6d, 0x00,
46 	  0x01, 0x02 };
47     const char *output_buf;
48 
49     initialize_common_option_spaces();
50 
51     /* We'll use v4-lost because it happens to be format d */
52     code = 137;
53     option = NULL;
54     if (!option_code_hash_lookup(&option, dhcp_universe.code_hash,
55 				 &code, 0, MDL)) {
56 	    atf_tc_fail("can't find option %d", code);
57     }
58 
59     if (option == NULL) {
60 	    atf_tc_fail("option is NULL");
61     }
62 
63     /* First we will try a good value we know should fit. */
64     output_buf = pretty_print_option(option, good_data, sizeof(good_data), 0,
65 				     0);
66 
67     /* Make sure we get what we expect */
68     if (!output_buf || strcmp(output_buf, "booya.com.")) {
69 	    atf_tc_fail("pretty_print_option did not return 'booya.com.'");
70     }
71 
72     fprintf(stderr, "good!\n");
73 
74     /* Now we'll try a data value that's too short */
75     output_buf = pretty_print_option(option, short_data, sizeof(short_data),
76 				     0, 0);
77 
78     /* Make sure we safely get an error */
79     if (!output_buf || strcmp(output_buf, "<error>")) {
80 	    atf_tc_fail("pretty_print_option did not return \"<error>\"");
81     }
82 
83     /* Now we'll try a data value that's too large */
84     output_buf = pretty_print_option(option, long_data, sizeof(long_data), 0,
85 				     0);
86 
87     /* This logs but does not return an error */
88     if (!output_buf || strcmp(output_buf, "booya.com.")) {
89 	atf_tc_fail("pretty_print_option did not return 'booya.com.' (large)");
90     }
91 }
92 
93 
94 /* This macro defines main() method that will call specified
95    test cases. tp and simple_test_case names can be whatever you want
96    as long as it is a valid variable identifier. */
ATF_TP_ADD_TCS(tp)97 ATF_TP_ADD_TCS(tp)
98 {
99     ATF_TP_ADD_TC(tp, pretty_print_domain_name);
100 
101     return (atf_no_error());
102 }
103