summaryrefslogtreecommitdiff
path: root/common/tests/misc_unittest.c
blob: dc248a9b2baad56a034529e1a01d04772e1e920d (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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.
 */

#include <config.h>
#include <atf-c.h>
#include "dhcpd.h"

struct basic_test {
	int count;
	int used_0;
	int used_25;
	int used_50;
	int used_75;
	int used_100;
};

struct basic_test basic_values[] =
	{{         0, 0,         0,          0,          0,          0},
	 {        10, 0,         2,          5,          7,         10},
	 {        20, 0,         5,         10,         15,         20},
	 {        50, 0,        12,         25,         37,         50},
	 {       100, 0,        25,         50,         75,        100},
	 {      1000, 0,       250,        500,        750,       1000},
	 {     10000, 0,      2500,       5000,       7500,      10000},
	 {    100000, 0,     25000,      50000,      75000,     100000},
	 {   1000000, 0,    250000,     500000,     750000,    1000000},
	 {  10000000, 0,   2500000,    5000000,    7500000,   10000000},
	 { 100000000, 0,  25000000,   50000000,   75000000,  100000000},
	 {1000000000, 0, 250000000,  500000000,  750000000, 1000000000},
	 {2000000000, 0, 500000000, 1000000000, 1500000000, 2000000000},
	 {-1, 0, 0, 0, 0, 0}};

ATF_TC(find_percent_basic);

ATF_TC_HEAD(find_percent_basic, tc)
{
    atf_tc_set_md_var(tc, "descr", "Verify basic percent calculation.");
}

/* This test does a simple check to see if we get the right value
 * given a count and a percnetage of that count
 */
ATF_TC_BODY(find_percent_basic, tc)
{
    struct basic_test *basic_value;
    int used;

    for (basic_value = &basic_values[0];
	 basic_value->count != -1;
	 basic_value++) {
	used = FIND_PERCENT(basic_value->count, 0);
	if (used != basic_value->used_0) {
	    atf_tc_fail("Wrong value for 0 - count: %d, used %d",
			basic_value->count, used);
	}

	used = FIND_PERCENT(basic_value->count, 25);
	if (used != basic_value->used_25) {
	    atf_tc_fail("Wrong value for 25 - count: %d, used %d",
			basic_value->count, used);
	}

	used = FIND_PERCENT(basic_value->count, 50);
	if (used != basic_value->used_50) {
	    atf_tc_fail("Wrong value for 50 - count: %d, used %d",
			basic_value->count, used);
	}

	used = FIND_PERCENT(basic_value->count, 75);
	if (used != basic_value->used_75) {
	    atf_tc_fail("Wrong value for 75 - count: %d, used %d",
			basic_value->count, used);
	}

	used = FIND_PERCENT(basic_value->count, 100);
	if (used != basic_value->used_100) {
	    atf_tc_fail("Wrong value for 100 - count: %d, used %d",
			basic_value->count, used);
	}
    }
    return;
}

ATF_TC(find_percent_adv);

ATF_TC_HEAD(find_percent_adv, tc)
{
    atf_tc_set_md_var(tc, "descr", "Verify advanced percent calculation.");
}

/* This test tries some more complicated items, such as using the macro
 * in a function or passing expressions into macro
 */
ATF_TC_BODY(find_percent_adv, tc)
{
    if (FIND_PERCENT(10*10, 10) != 10) {
	atf_tc_fail("Wrong value for adv 1");
    }

    if (FIND_PERCENT(20*20, 80) != 320) {
	atf_tc_fail("Wrong value for adv 2");
    }

    if (FIND_PERCENT(1000000*1000, 50) != 500000000) {
	atf_tc_fail("Wrong value for adv 3");
    }

    if (FIND_PERCENT(100+100, 10) != 20) {
	atf_tc_fail("Wrong value for adv 4");
    }

    if (FIND_PERCENT(1000+2000, 90) != 2700) {
	atf_tc_fail("Wrong value for adv 5");
    }

    if (FIND_PERCENT(10000 - 8000, 70) != 1400) {
	atf_tc_fail("Wrong value for adv 6");
    }

    if (FIND_PERCENT(2000000000 / 1000, 25) != 500000) {
	atf_tc_fail("Wrong value for adv 7");
    }

    if (FIND_PERCENT(10*10, 10)/2 != 5) {
	atf_tc_fail("Wrong value for adv 8");
    }

    if (FIND_PERCENT(100*10, 50) * 2 != 1000) {
	atf_tc_fail("Wrong value for adv 9");
    }

    if (FIND_PERCENT(100*10, 50) * 2 > 1000) {
	atf_tc_fail("Wrong value for adv 10");
    }

    if (FIND_PERCENT(100+100, 20) * 2 < 60) {
	atf_tc_fail("Wrong value for adv 11");
    }

    return;
}

ATF_TC(print_hex_only);

ATF_TC_HEAD(print_hex_only, tc)
{
    atf_tc_set_md_var(tc, "descr", "Verify hex data formatting.");
}

/* This test exercises the print_hex_only function
 */
ATF_TC_BODY(print_hex_only, tc)
{
    unsigned char data[] =  {0xaa,0xbb,0xcc,0xdd};
    char* ref = "aa:bb:cc:dd";
    char buf[14];
    memset(buf, 'x', sizeof(buf));
    int data_len = sizeof(data);
    int expected_len = 12;

    /* Proper input values should produce proper result */
    print_hex_only (data_len, data, expected_len, buf);
    if (strlen(buf) != strlen(ref)) {
	    atf_tc_fail("len of result is wrong");
    }

    if (strcmp(buf, ref)) {
	    atf_tc_fail("result doesn't match ref");
    }

    /* Make sure we didn't overrun the buffer */
    if (buf[expected_len] != 'x') {
	    atf_tc_fail("data over run detected");
    }

    /* Buffer == null doesn't crash */
    print_hex_only (data_len, data, expected_len, NULL);

    /* Limit == 0 doesn't write (or crash) */
    *buf = '-';
    print_hex_only (data_len, data, 0, buf);
    if (*buf != '-') {
	    atf_tc_fail("limit of zero, altered buffer");
    }

    /* data == NULL doesn't write (or crash) */
    print_hex_only (data_len, NULL, expected_len, buf);
    if (*buf != '-') {
	    atf_tc_fail("limit of zero, altered buffer");
    }

    /* Limit too small should produce zero length string */
    *buf = '-';
    print_hex_only (data_len, data, expected_len - 1, buf);
    if (*buf != 0x0) {
	    atf_tc_fail("limit too small should have failed");
    }

    /* Data length of 0 should produce zero length string */
    *buf = '-';
    print_hex_only (0, data, expected_len, buf);
    if (*buf != 0x0) {
	    atf_tc_fail("limit too small should have failed");
    }
}
    	
/* This macro defines main() method that will call specified
   test cases. tp and simple_test_case names can be whatever you want
   as long as it is a valid variable identifier. */
ATF_TP_ADD_TCS(tp)
{
    ATF_TP_ADD_TC(tp, find_percent_basic);
    ATF_TP_ADD_TC(tp, find_percent_adv);
    ATF_TP_ADD_TC(tp, print_hex_only);

    return (atf_no_error());
}