1 /* $NetBSD: ns_name_test.c,v 1.2 2018/04/07 22:37:29 christos Exp $ */
2
3 /*
4 * Copyright (C) 2014-2017 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 /* Tests the newly added functions: MRns_name_compress_list and
20 * MRns_name_uncompress_list. These two functions rely on most of
21 * the other functions in ns_name.c. If these tests pass, then the
22 * majority of those functions work.
23 *
24 * This is not exhaustive test of these functions, much more could be
25 * done.
26 */
27 #include "config.h"
28 #include <atf-c.h>
29 #include "dhcpd.h"
30
31 ATF_TC(MRns_name_list_funcs);
32
ATF_TC_HEAD(MRns_name_list_funcs,tc)33 ATF_TC_HEAD(MRns_name_list_funcs, tc) {
34 atf_tc_set_md_var(tc, "descr", "MRns_name list funcs test, "
35 "compress from text, decompress to text");
36 }
37
38 void concat_lists (const char* label, struct data_string* list1,
39 struct data_string* list2, const char *refbuf, size_t reflen);
40
ATF_TC_BODY(MRns_name_list_funcs,tc)41 ATF_TC_BODY(MRns_name_list_funcs, tc) {
42
43 const char text_list[] = "one.two.com,three.two.com,four.two.com";
44 unsigned char comp_list[] = {
45 0x03,0x6f,0x6e,0x65,0x03,0x74,0x77,0x6f,0x03,0x63,0x6f,
46 0x6d,0x00,0x05,0x74,0x68,0x72,0x65,0x65,0xc0,0x04,0x04,
47 0x66,0x6f,0x75,0x72,0xc0,0x04};
48 unsigned char compbuf[sizeof(comp_list)];
49 char textbuf[sizeof(text_list)];
50 int ret;
51
52 memset(compbuf, 0x00, sizeof(compbuf));
53
54 /* Compress the reference text list */
55 ret = MRns_name_compress_list(text_list, sizeof(text_list),
56 compbuf, sizeof(compbuf));
57
58 /* Verify compressed length is correct */
59 ATF_REQUIRE_MSG((ret == sizeof(compbuf)), "compressed len %d wrong", ret);
60
61 /* Verify compressed content is correct */
62 ATF_REQUIRE_MSG((memcmp(comp_list, compbuf, sizeof(compbuf)) == 0),
63 "compressed buffer content wrong");
64
65 /* Decompress the new compressed list */
66 ret = MRns_name_uncompress_list(compbuf, ret, textbuf, sizeof(textbuf));
67
68 /* Verify decompressed length is correct */
69 ATF_REQUIRE_MSG((ret == strlen(text_list)),
70 "uncompressed len %d wrong", ret);
71
72 /* Verify decompressed content is correct */
73 ATF_REQUIRE_MSG((memcmp(textbuf, text_list, sizeof(textbuf)) == 0),
74 "uncompressed buffer content wrong");
75 }
76
77 ATF_TC(concat_dclists);
78
ATF_TC_HEAD(concat_dclists,tc)79 ATF_TC_HEAD(concat_dclists, tc) {
80 atf_tc_set_md_var(tc, "descr", "concat_dclists function test, "
81 "permutate concating empty and non-empty lists");
82 }
83
ATF_TC_BODY(concat_dclists,tc)84 ATF_TC_BODY(concat_dclists, tc) {
85 /* Compressed list version of "booya.com" */
86 const char data[] =
87 {0x05, 0x62, 0x6f, 0x6f, 0x79, 0x61, 0x03, 0x63, 0x6f, 0x6d, 0x00 };
88
89 /* Concatenation of data with itself */
90 const char data2[] =
91 {0x05, 0x62, 0x6f, 0x6f, 0x79, 0x61, 0x03, 0x63, 0x6f, 0x6d, 0x00,
92 0xc0, 0x00 };
93
94 struct data_string nonempty;
95 struct data_string empty;
96
97 /* Make a non-empty compressed list from data[] */
98 nonempty.len = sizeof(data);
99 buffer_allocate(&(nonempty.buffer), nonempty.len, MDL);
100 memcpy(nonempty.buffer->data, data, nonempty.len);
101 nonempty.data = nonempty.buffer->data;
102
103 /* Permutate NULL with non-empty list */
104 concat_lists("null + null", NULL, NULL, "", 1);
105 concat_lists("null + nonempty", NULL, &nonempty, data, sizeof(data));
106 concat_lists("nonempty + null", &nonempty, NULL, data, sizeof(data));
107
108 /* Permutate zeroed-out list with non-empty list */
109 memset (&empty, 0x00, sizeof(struct data_string));
110 concat_lists("zero-list + zero-list", &empty, &empty, "", 1);
111 concat_lists("zero-list + nonempty", &empty, &nonempty, data, sizeof(data));
112 concat_lists("nonempty + zero-list", &nonempty, &empty, data, sizeof(data));
113
114 /* Create an empty list which is a buffer with 1 null in it */
115 /* Make sure those work the same as zeroed out data_strings */
116 buffer_allocate (&empty.buffer, 1, MDL);
117 empty.len = 1;
118 empty.data = empty.buffer->data;
119
120 /* Permutate with empty list */
121 concat_lists("empty + empty", &empty, &empty, "", 1);
122 concat_lists("empty + nonempty", &empty, &nonempty, data, sizeof(data));
123 concat_lists("nonempty + empty", &nonempty, &empty, data, sizeof(data));
124
125 /* Permutate with list len of 0 */
126 empty.len = 0;
127 concat_lists("zero-len + zero-len", &empty, &empty, "", 1);
128 concat_lists("zero-len + nonempty", &empty, &nonempty, data, sizeof(data));
129 concat_lists("nonempty + zero-len", &nonempty, &empty, data, sizeof(data));
130
131 /* Lastly, make sure two non-empty lists concat correctly */
132 concat_lists("nonempty + nonempty", &nonempty, &nonempty,
133 data2, sizeof(data2));
134
135 data_string_forget(&empty, MDL);
136 data_string_forget(&nonempty, MDL);
137 }
138
139 /* Helper function which tests concatenating two compressed lists
140 *
141 * \param label text to print in error message
142 * \param list1 data_string containing first list
143 * \param list2 data_string containing second list
144 * \param refbuf buffer containing the expected concatentated data
145 * \param reflen length of the expected data
146 */
concat_lists(const char * label,struct data_string * list1,struct data_string * list2,const char * refbuf,size_t reflen)147 void concat_lists (const char* label, struct data_string* list1,
148 struct data_string* list2, const char *refbuf, size_t reflen)
149 {
150 struct data_string result;
151 int rc;
152
153 memset (&result, 0x00, sizeof(struct data_string));
154 rc = concat_dclists (&result, list1, list2);
155 ATF_REQUIRE_MSG((rc >= 0), "%s concat_dclists call failed %d",
156 label, rc);
157
158 ATF_REQUIRE_MSG(result.len == reflen, "%s result len: %d incorrect",
159 label, result.len);
160
161 if (refbuf != NULL) {
162 ATF_REQUIRE_MSG((memcmp(result.data, refbuf, reflen) == 0),
163 "%s content is incorrect", label);
164 }
165
166 data_string_forget(&result, MDL);
167 }
168
ATF_TP_ADD_TCS(tp)169 ATF_TP_ADD_TCS(tp)
170 {
171 ATF_TP_ADD_TC(tp, MRns_name_list_funcs);
172 ATF_TP_ADD_TC(tp, concat_dclists);
173
174 return (atf_no_error());
175 }
176