summaryrefslogtreecommitdiff
path: root/server/tests/load_bal_unittest.c
blob: 8d5f8b87f0ea56c14f5a5a90cdcd8b8e26454343 (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
/*
 * Copyright (C) 2012-2017 by 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 "dhcpd.h"

#include <atf-c.h>

/*
 * Test the load balancing code.  
 *
 * The two main variables are:
 * packet => the "packet" being processed
 * state  => the "state" of the failover peer
 * We only fill in the fields necessary for our testing
 * packet->raw->secs   => amount of time the client has been trying
 * packet->raw->hlen   => the length of the mac address of the client
 * packet->raw->chaddr => the mac address of the client
 * To simplify the tests the mac address will be only 1 byte long and
 * not really matter.  Instead the hba will be all 1s and the tests
 * will use the primary/secondary flag to change the expected result.
 *
 * state->i_am => primary or secondary
 * state->load_balance_max_secs => maxixum time for a client to be trying
 *                                 before the other peer responds
 *                                 set to 5 for these tests
 * state->hba = array of hash buckets assigning the hash to primary or secondary
 *              set to all ones (all primary) for theses tests
 */

ATF_TC(load_balance);

ATF_TC_HEAD(load_balance, tc)
{
	atf_tc_set_md_var(tc, "descr", "This test case checks that "
			  "load balancing works.");
}

ATF_TC_BODY(load_balance, tc)
{
#if defined(FAILOVER_PROTOCOL) 
	struct packet packet;
	struct dhcp_packet raw;
	dhcp_failover_state_t pstate, sstate;
	u_int8_t hba[256];

	memset(&packet, 0, sizeof(struct packet));
	memset(&raw, 0, sizeof(struct dhcp_packet));
	packet.raw = &raw;
	raw.hlen = 1;
	raw.chaddr[0] = 14;

	memset(hba, 0xFF, 256);

	/* primary state */
	memset(&pstate, 0, sizeof(dhcp_failover_state_t));
	pstate.i_am = primary;
	pstate.load_balance_max_secs = 5;
	pstate.hba = hba;

	/* secondary state, we can reuse the hba as it doesn't change */
	memset(&sstate, 0, sizeof(dhcp_failover_state_t));
	sstate.i_am = secondary;
	sstate.load_balance_max_secs = 5;
	sstate.hba = hba;

	/* Basic check, primary accepted, secondary not */
	raw.secs = htons(0);
	if (load_balance_mine(&packet, &pstate) != 1) {
		atf_tc_fail("ERROR: primary not accepted %s:%d", MDL);
	}

	if (load_balance_mine(&packet, &sstate) != 0) {
		atf_tc_fail("ERROR: secondary accepted %s:%d", MDL);
	}
	

	/* Timeout not exceeded, primary accepted, secondary not */
	raw.secs = htons(2);
	if (load_balance_mine(&packet, &pstate) != 1) {
		atf_tc_fail("ERROR: primary not accepted %s:%d", MDL);
	}

	if (load_balance_mine(&packet, &sstate) != 0) {
		atf_tc_fail("ERROR: secondary accepted %s:%d", MDL);
	}
	
	/* Timeout exceeded, both accepted */
	raw.secs = htons(6);
	if (load_balance_mine(&packet, &pstate) != 1) {
		atf_tc_fail("ERROR: primary not accepted %s:%d", MDL);
	}

	if (load_balance_mine(&packet, &sstate) != 1) {
		atf_tc_fail("ERROR: secondary not accepted %s:%d", MDL);
	}

	/* Timeout exeeded with a large value, both accepted */
	raw.secs = htons(257);
	if (load_balance_mine(&packet, &pstate) != 1) {
		atf_tc_fail("ERROR: primary not accepted %s:%d", MDL);
	}

	if (load_balance_mine(&packet, &sstate) != 1) {
		atf_tc_fail("ERROR: secondary not accepted %s:%d", MDL);
	}
#else
    atf_tc_skip("failover is disabled");
#endif
}

ATF_TC(load_balance_swap);

ATF_TC_HEAD(load_balance_swap, tc)
{
	atf_tc_set_md_var(tc, "descr", "This test case checks that "
			  "load balancing works with byteswapping.");
}

ATF_TC_BODY(load_balance_swap, tc)
{
#if defined(FAILOVER_PROTOCOL) 
	struct packet packet;
	struct dhcp_packet raw;
	dhcp_failover_state_t pstate, sstate;
	u_int8_t hba[256];

	check_secs_byte_order = 1;

	memset(&packet, 0, sizeof(struct packet));
	memset(&raw, 0, sizeof(struct dhcp_packet));
	packet.raw = &raw;
	raw.hlen = 1;
	raw.chaddr[0] = 14;

	memset(hba, 0xFF, 256);

	/* primary state */
	memset(&pstate, 0, sizeof(dhcp_failover_state_t));
	pstate.i_am = primary;
	pstate.load_balance_max_secs = 5;
	pstate.hba = hba;

	/* secondary state, we can reuse the hba as it doesn't change */
	memset(&sstate, 0, sizeof(dhcp_failover_state_t));
	sstate.i_am = secondary;
	sstate.load_balance_max_secs = 5;
	sstate.hba = hba;

	/* Small byteswapped timeout, primary accepted, secondary not*/
	raw.secs = htons(256);
	if (load_balance_mine(&packet, &pstate) != 1) {
		atf_tc_fail("ERROR: primary not accepted %s:%d", MDL);
	}

	if (load_balance_mine(&packet, &sstate) != 0) {
		atf_tc_fail("ERROR: secondary accepted %s:%d", MDL);
	}

	/* Large byteswapped timeout, both accepted*/
	raw.secs = htons(256 * 6);
	if (load_balance_mine(&packet, &pstate) != 1) {
		atf_tc_fail("ERROR: primary not accepted %s:%d", MDL);
	}

	if (load_balance_mine(&packet, &sstate) != 1) {
		atf_tc_fail("ERROR: secondary not accepted %s:%d", MDL);
	}
#else
	atf_tc_skip("failover is disabled");
#endif
}


ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, load_balance);
	ATF_TP_ADD_TC(tp, load_balance_swap);

	return (atf_no_error());
}