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