]> git.ipfire.org Git - thirdparty/dhcp.git/blob - client/tests/duid_unittest.c
copy rights update
[thirdparty/dhcp.git] / client / tests / duid_unittest.c
1 /*
2 * Copyright (C) 2017-2022 Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Internet Systems Consortium, Inc.
17 * PO Box 360
18 * Newmarket, NH 03857 USA
19 * <info@isc.org>
20 * https://www.isc.org/
21 *
22 */
23
24 #include "config.h"
25 #include <atf-c.h>
26 #include <omapip/omapip_p.h>
27 #include "dhcpd.h"
28
29
30 /* Tests to see if the routine to read a secondary lease file
31 * for the DUID works properly. The tests:
32 * Test file x:
33 * no test file - should result in no duid
34 * Test filx 0:
35 * A test file but no DUID def, no duid
36 * Test file 1:
37 * Can it read a single DUID in the file?
38 * Test file 2:
39 * Can it find a second DUID in the file after a good lease and
40 * a badly formatted lease?
41 * Test file 3:
42 * Can it find a later DUID after a good one and a bad one?
43 * and to give a bit more coverage test file 1 should use LLT
44 * test file 2 should use LL and test file 3 should use LL for
45 * the first one and LLT for the third one.
46 */
47
48 int duidx_len = 0;
49
50 int duid0_len = 0;
51
52 int duid1_len = 14;
53 char duid1_data[] = {0, 1, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
54
55 int duid2_len = 10;
56 char duid2_data[] = {0, 3, 0, 1, 15, 16, 17, 18, 19, 20};
57
58 int duid3_len = 14;
59 char duid3_data[] = {0, 1, 0, 1, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
60
61 ATF_TC(read_duid_test);
62
63 ATF_TC_HEAD(read_duid_test, tc) {
64 atf_tc_set_md_var(tc, "descr", "read secondary file looking for duid");
65 }
66
67 ATF_TC_BODY(read_duid_test, tc) {
68
69 static const char *srcdir;
70 char duid_fname[1024];
71
72 /* Get the srcidr so we can find our test files */
73 if (atf_tc_has_config_var(tc, "srcdir"))
74 srcdir = atf_tc_get_config_var(tc, "srcdir");
75 /* point the duid file at our filename space
76 We will update it per test below */
77 path_dhclient_duid = duid_fname;
78
79 /* Initialize client globals. */
80 memset(&default_duid, 0, sizeof(default_duid));
81
82 /* Try to read a nonexistent test file
83 */
84 sprintf(duid_fname, "%s/duidx_test.txt", srcdir);
85 read_client_duid();
86 if (default_duid.len != duidx_len) {
87 atf_tc_fail("failed to properly read duid1");
88 }
89
90 /* Try to read test file 0
91 * This doesn't have a DUID.
92 */
93 sprintf(duid_fname, "%s/duid0_test.txt", srcdir);
94 read_client_duid();
95 if (default_duid.len != duid0_len) {
96 atf_tc_fail("failed to properly read duid0");
97 }
98
99 /* Try to read test file 1
100 * This has a single good LLT DUID in it
101 */
102 sprintf(duid_fname, "%s/duid1_test.txt", srcdir);
103 read_client_duid();
104 if ((default_duid.len != duid1_len) ||
105 (memcmp(default_duid.data, duid1_data, duid1_len) != 0)) {
106 atf_tc_fail("failed to properly read duid1");
107 }
108
109 /* Try to read test file 2
110 * This has two good LL DUIDs in it with several good and bad leases between them.
111 */
112 sprintf(duid_fname, "%s/duid2_test.txt", srcdir);
113 read_client_duid();
114 if ((default_duid.len != duid2_len) ||
115 (memcmp(default_duid.data, duid2_data, duid2_len) != 0)) {
116 atf_tc_fail("failed to properly read duid2");
117 }
118
119 /* Try to read test file 3
120 * This has a good LL DUID, a bad LLT DUID and a good LLT DUID
121 */
122 sprintf(duid_fname, "%s/duid3_test.txt", srcdir);
123 read_client_duid();
124 if ((default_duid.len != duid3_len) ||
125 (memcmp(default_duid.data, duid3_data, duid3_len) != 0)) {
126 atf_tc_fail("failed to properly read duid3");
127 }
128
129 }
130
131
132 ATF_TP_ADD_TCS(tp) {
133 ATF_TP_ADD_TC(tp, read_duid_test);
134
135 return (atf_no_error());
136 }