1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* Tests the newly added functions: MRns_name_compress_list and
* MRns_name_uncompress_list. These two functions rely on most of
* the other functions in ns_name.c. If these tests pass, then the
* majority of those functions work.
*
* This is not exhaustive test of these functions, much more could be
* done.
*/
#include "config.h"
#include <atf-c.h>
#include "dhcpd.h"
ATF_TC(MRns_name_list_funcs);
ATF_TC_HEAD(MRns_name_list_funcs, tc) {
atf_tc_set_md_var(tc, "descr", "MRns_name list funcs test, "
"compress from text, decompress to text");
}
void concat_lists (const char* label, struct data_string* list1,
struct data_string* list2, const char *refbuf, size_t reflen);
ATF_TC_BODY(MRns_name_list_funcs, tc) {
const char text_list[] = "one.two.com,three.two.com,four.two.com";
unsigned char comp_list[] = {
0x03,0x6f,0x6e,0x65,0x03,0x74,0x77,0x6f,0x03,0x63,0x6f,
0x6d,0x00,0x05,0x74,0x68,0x72,0x65,0x65,0xc0,0x04,0x04,
0x66,0x6f,0x75,0x72,0xc0,0x04};
unsigned char compbuf[sizeof(comp_list)];
char textbuf[sizeof(text_list)];
int ret;
memset(compbuf, 0x00, sizeof(compbuf));
/* Compress the reference text list */
ret = MRns_name_compress_list(text_list, sizeof(text_list),
compbuf, sizeof(compbuf));
/* Verify compressed length is correct */
ATF_REQUIRE_MSG((ret == sizeof(compbuf)), "compressed len %d wrong", ret);
/* Verify compressed content is correct */
ATF_REQUIRE_MSG((memcmp(comp_list, compbuf, sizeof(compbuf)) == 0),
"compressed buffer content wrong");
/* Decompress the new compressed list */
ret = MRns_name_uncompress_list(compbuf, ret, textbuf, sizeof(textbuf));
/* Verify decompressed length is correct */
ATF_REQUIRE_MSG((ret == strlen(text_list)),
"uncompressed len %d wrong", ret);
/* Verify decompressed content is correct */
ATF_REQUIRE_MSG((memcmp(textbuf, text_list, sizeof(textbuf)) == 0),
"uncompressed buffer content wrong");
}
ATF_TC(concat_dclists);
ATF_TC_HEAD(concat_dclists, tc) {
atf_tc_set_md_var(tc, "descr", "concat_dclists function test, "
"permutate concating empty and non-empty lists");
}
ATF_TC_BODY(concat_dclists, tc) {
/* Compressed list version of "booya.com" */
const char data[] =
{0x05, 0x62, 0x6f, 0x6f, 0x79, 0x61, 0x03, 0x63, 0x6f, 0x6d, 0x00 };
/* Concatenation of data with itself */
const char data2[] =
{0x05, 0x62, 0x6f, 0x6f, 0x79, 0x61, 0x03, 0x63, 0x6f, 0x6d, 0x00,
0xc0, 0x00 };
struct data_string nonempty;
struct data_string empty;
/* Make a non-empty compressed list from data[] */
nonempty.len = sizeof(data);
buffer_allocate(&(nonempty.buffer), nonempty.len, MDL);
memcpy(nonempty.buffer->data, data, nonempty.len);
nonempty.data = nonempty.buffer->data;
/* Permutate NULL with non-empty list */
concat_lists("null + null", NULL, NULL, "", 1);
concat_lists("null + nonempty", NULL, &nonempty, data, sizeof(data));
concat_lists("nonempty + null", &nonempty, NULL, data, sizeof(data));
/* Permutate zeroed-out list with non-empty list */
memset (&empty, 0x00, sizeof(struct data_string));
concat_lists("zero-list + zero-list", &empty, &empty, "", 1);
concat_lists("zero-list + nonempty", &empty, &nonempty, data, sizeof(data));
concat_lists("nonempty + zero-list", &nonempty, &empty, data, sizeof(data));
/* Create an empty list which is a buffer with 1 null in it */
/* Make sure those work the same as zeroed out data_strings */
buffer_allocate (&empty.buffer, 1, MDL);
empty.len = 1;
empty.data = empty.buffer->data;
/* Permutate with empty list */
concat_lists("empty + empty", &empty, &empty, "", 1);
concat_lists("empty + nonempty", &empty, &nonempty, data, sizeof(data));
concat_lists("nonempty + empty", &nonempty, &empty, data, sizeof(data));
/* Permutate with list len of 0 */
empty.len = 0;
concat_lists("zero-len + zero-len", &empty, &empty, "", 1);
concat_lists("zero-len + nonempty", &empty, &nonempty, data, sizeof(data));
concat_lists("nonempty + zero-len", &nonempty, &empty, data, sizeof(data));
/* Lastly, make sure two non-empty lists concat correctly */
concat_lists("nonempty + nonempty", &nonempty, &nonempty,
data2, sizeof(data2));
data_string_forget(&empty, MDL);
data_string_forget(&nonempty, MDL);
}
/* Helper function which tests concatenating two compressed lists
*
* \param label text to print in error message
* \param list1 data_string containing first list
* \param list2 data_string containing second list
* \param refbuf buffer containing the expected concatentated data
* \param reflen length of the expected data
*/
void concat_lists (const char* label, struct data_string* list1,
struct data_string* list2, const char *refbuf, size_t reflen)
{
struct data_string result;
int rc;
memset (&result, 0x00, sizeof(struct data_string));
rc = concat_dclists (&result, list1, list2);
ATF_REQUIRE_MSG((rc >= 0), "%s concat_dclists call failed %d",
label, rc);
ATF_REQUIRE_MSG(result.len == reflen, "%s result len: %d incorrect",
label, result.len);
if (refbuf != NULL) {
ATF_REQUIRE_MSG((memcmp(result.data, refbuf, reflen) == 0),
"%s content is incorrect", label);
}
data_string_forget(&result, MDL);
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, MRns_name_list_funcs);
ATF_TP_ADD_TC(tp, concat_dclists);
return (atf_no_error());
}
|