1 /* $NetBSD: misc_unittest.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 #include <config.h>
20 #include <atf-c.h>
21 #include "dhcpd.h"
22
23 struct basic_test {
24 int count;
25 int used_0;
26 int used_25;
27 int used_50;
28 int used_75;
29 int used_100;
30 };
31
32 struct basic_test basic_values[] =
33 {{ 0, 0, 0, 0, 0, 0},
34 { 10, 0, 2, 5, 7, 10},
35 { 20, 0, 5, 10, 15, 20},
36 { 50, 0, 12, 25, 37, 50},
37 { 100, 0, 25, 50, 75, 100},
38 { 1000, 0, 250, 500, 750, 1000},
39 { 10000, 0, 2500, 5000, 7500, 10000},
40 { 100000, 0, 25000, 50000, 75000, 100000},
41 { 1000000, 0, 250000, 500000, 750000, 1000000},
42 { 10000000, 0, 2500000, 5000000, 7500000, 10000000},
43 { 100000000, 0, 25000000, 50000000, 75000000, 100000000},
44 {1000000000, 0, 250000000, 500000000, 750000000, 1000000000},
45 {2000000000, 0, 500000000, 1000000000, 1500000000, 2000000000},
46 {-1, 0, 0, 0, 0, 0}};
47
48 ATF_TC(find_percent_basic);
49
ATF_TC_HEAD(find_percent_basic,tc)50 ATF_TC_HEAD(find_percent_basic, tc)
51 {
52 atf_tc_set_md_var(tc, "descr", "Verify basic percent calculation.");
53 }
54
55 /* This test does a simple check to see if we get the right value
56 * given a count and a percnetage of that count
57 */
ATF_TC_BODY(find_percent_basic,tc)58 ATF_TC_BODY(find_percent_basic, tc)
59 {
60 struct basic_test *basic_value;
61 int used;
62
63 for (basic_value = &basic_values[0];
64 basic_value->count != -1;
65 basic_value++) {
66 used = FIND_PERCENT(basic_value->count, 0);
67 if (used != basic_value->used_0) {
68 atf_tc_fail("Wrong value for 0 - count: %d, used %d",
69 basic_value->count, used);
70 }
71
72 used = FIND_PERCENT(basic_value->count, 25);
73 if (used != basic_value->used_25) {
74 atf_tc_fail("Wrong value for 25 - count: %d, used %d",
75 basic_value->count, used);
76 }
77
78 used = FIND_PERCENT(basic_value->count, 50);
79 if (used != basic_value->used_50) {
80 atf_tc_fail("Wrong value for 50 - count: %d, used %d",
81 basic_value->count, used);
82 }
83
84 used = FIND_PERCENT(basic_value->count, 75);
85 if (used != basic_value->used_75) {
86 atf_tc_fail("Wrong value for 75 - count: %d, used %d",
87 basic_value->count, used);
88 }
89
90 used = FIND_PERCENT(basic_value->count, 100);
91 if (used != basic_value->used_100) {
92 atf_tc_fail("Wrong value for 100 - count: %d, used %d",
93 basic_value->count, used);
94 }
95 }
96 return;
97 }
98
99 ATF_TC(find_percent_adv);
100
ATF_TC_HEAD(find_percent_adv,tc)101 ATF_TC_HEAD(find_percent_adv, tc)
102 {
103 atf_tc_set_md_var(tc, "descr", "Verify advanced percent calculation.");
104 }
105
106 /* This test tries some more complicated items, such as using the macro
107 * in a function or passing expressions into macro
108 */
ATF_TC_BODY(find_percent_adv,tc)109 ATF_TC_BODY(find_percent_adv, tc)
110 {
111 if (FIND_PERCENT(10*10, 10) != 10) {
112 atf_tc_fail("Wrong value for adv 1");
113 }
114
115 if (FIND_PERCENT(20*20, 80) != 320) {
116 atf_tc_fail("Wrong value for adv 2");
117 }
118
119 if (FIND_PERCENT(1000000*1000, 50) != 500000000) {
120 atf_tc_fail("Wrong value for adv 3");
121 }
122
123 if (FIND_PERCENT(100+100, 10) != 20) {
124 atf_tc_fail("Wrong value for adv 4");
125 }
126
127 if (FIND_PERCENT(1000+2000, 90) != 2700) {
128 atf_tc_fail("Wrong value for adv 5");
129 }
130
131 if (FIND_PERCENT(10000 - 8000, 70) != 1400) {
132 atf_tc_fail("Wrong value for adv 6");
133 }
134
135 if (FIND_PERCENT(2000000000 / 1000, 25) != 500000) {
136 atf_tc_fail("Wrong value for adv 7");
137 }
138
139 if (FIND_PERCENT(10*10, 10)/2 != 5) {
140 atf_tc_fail("Wrong value for adv 8");
141 }
142
143 if (FIND_PERCENT(100*10, 50) * 2 != 1000) {
144 atf_tc_fail("Wrong value for adv 9");
145 }
146
147 if (FIND_PERCENT(100*10, 50) * 2 > 1000) {
148 atf_tc_fail("Wrong value for adv 10");
149 }
150
151 if (FIND_PERCENT(100+100, 20) * 2 < 60) {
152 atf_tc_fail("Wrong value for adv 11");
153 }
154
155 return;
156 }
157
158 ATF_TC(print_hex_only);
159
ATF_TC_HEAD(print_hex_only,tc)160 ATF_TC_HEAD(print_hex_only, tc)
161 {
162 atf_tc_set_md_var(tc, "descr", "Verify hex data formatting.");
163 }
164
165 /* This test exercises the print_hex_only function
166 */
ATF_TC_BODY(print_hex_only,tc)167 ATF_TC_BODY(print_hex_only, tc)
168 {
169 unsigned char data[] = {0xaa,0xbb,0xcc,0xdd};
170 char* ref = "aa:bb:cc:dd";
171 char buf[14];
172 memset(buf, 'x', sizeof(buf));
173 int data_len = sizeof(data);
174 int expected_len = 12;
175
176 /* Proper input values should produce proper result */
177 print_hex_only (data_len, data, expected_len, buf);
178 if (strlen(buf) != strlen(ref)) {
179 atf_tc_fail("len of result is wrong");
180 }
181
182 if (strcmp(buf, ref)) {
183 atf_tc_fail("result doesn't match ref");
184 }
185
186 /* Make sure we didn't overrun the buffer */
187 if (buf[expected_len] != 'x') {
188 atf_tc_fail("data over run detected");
189 }
190
191 /* Buffer == null doesn't crash */
192 print_hex_only (data_len, data, expected_len, NULL);
193
194 /* Limit == 0 doesn't write (or crash) */
195 *buf = '-';
196 print_hex_only (data_len, data, 0, buf);
197 if (*buf != '-') {
198 atf_tc_fail("limit of zero, altered buffer");
199 }
200
201 /* data == NULL doesn't write (or crash) */
202 print_hex_only (data_len, NULL, expected_len, buf);
203 if (*buf != '-') {
204 atf_tc_fail("limit of zero, altered buffer");
205 }
206
207 /* Limit too small should produce zero length string */
208 *buf = '-';
209 print_hex_only (data_len, data, expected_len - 1, buf);
210 if (*buf != 0x0) {
211 atf_tc_fail("limit too small should have failed");
212 }
213
214 /* Data length of 0 should produce zero length string */
215 *buf = '-';
216 print_hex_only (0, data, expected_len, buf);
217 if (*buf != 0x0) {
218 atf_tc_fail("limit too small should have failed");
219 }
220 }
221
222 /* This macro defines main() method that will call specified
223 test cases. tp and simple_test_case names can be whatever you want
224 as long as it is a valid variable identifier. */
ATF_TP_ADD_TCS(tp)225 ATF_TP_ADD_TCS(tp)
226 {
227 ATF_TP_ADD_TC(tp, find_percent_basic);
228 ATF_TP_ADD_TC(tp, find_percent_adv);
229 ATF_TP_ADD_TC(tp, print_hex_only);
230
231 return (atf_no_error());
232 }
233