1/*	$NetBSD: dns_unittest.c,v 1.2 2018/04/07 22:37:29 christos Exp $	*/
2
3/*
4 * Copyright (C) 2013-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/*
24 * This file provides unit tests for the dns and ddns code.
25 * Currently this is limited to verifying the dhcid code is
26 * working properly.  In time we may be able to expand the
27 * tests to cover other areas.
28 *
29 * The tests for the interim txt records comapre to previous
30 * internally generated values.
31 *
32 * The tests for the standard dhcid records compare to values
33 * from rfc 4701
34 */
35
36#if defined (NSUPDATE)
37
38char *name_1 = "chi6.example.com";
39u_int8_t clid_1[] = {0x00, 0x01, 0x00, 0x06, 0x41, 0x2d, 0xf1, 0x66, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
40u_int8_t std_result_1[] = {0x00, 0x02, 0x01, 0x63, 0x6f, 0xc0, 0xb8, 0x27, 0x1c,
41			  0x82, 0x82, 0x5b, 0xb1, 0xac, 0x5c, 0x41, 0xcf, 0x53,
42			  0x51, 0xaa, 0x69, 0xb4, 0xfe, 0xbd, 0x94, 0xe8, 0xf1,
43			  0x7c, 0xdb, 0x95, 0x00, 0x0d, 0xa4, 0x8c, 0x40};
44char *int_result_1 = "\"02abf8cd3753dc1847be40858becd77865";
45
46char *name_2 = "chi.example.com";
47u_int8_t clid_2[] = {0x01, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c};
48u_int8_t std_result_2[] = {0x00, 0x01, 0x01, 0x39, 0x20, 0xfe, 0x5d, 0x1d, 0xce,
49			  0xb3, 0xfd, 0x0b, 0xa3, 0x37, 0x97, 0x56, 0xa7, 0x0d,
50			  0x73, 0xb1, 0x70, 0x09, 0xf4, 0x1d, 0x58, 0xbd, 0xdb,
51			  0xfc, 0xd6, 0xa2, 0x50, 0x39, 0x56, 0xd8, 0xda};
52char *int_result_2 = "\"31934ffa9344a3ab86c380505a671e5113";
53
54char *name_3 = "client.example.com";
55u_int8_t clid_3[] = {0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
56u_int8_t std_result_3[] = {0x00, 0x00, 0x01, 0xc4, 0xb9, 0xa5, 0xb2, 0x49, 0x65,
57			  0x13, 0x43, 0x15, 0x8d, 0xde, 0x7b, 0xcc, 0x77, 0x16,
58			  0x98, 0x41, 0xf7, 0xa4, 0x24, 0x3a, 0x57, 0x2b, 0x5c,
59			  0x28, 0x3f, 0xff, 0xed, 0xeb, 0x3f, 0x75, 0xe6};
60char *int_result_3 = "\"0046b6cacea62dc1d4567b068175d1f808";
61
62void call_get_std_dhcid(int test, int type,
63			u_int8_t *clid, unsigned clidlen,
64			char *name, unsigned namelen,
65			u_int8_t *dhcid, unsigned dhcid_len)
66{
67  dhcp_ddns_cb_t ddns_cb;
68  struct data_string *id;
69
70  memset(&ddns_cb, 0, sizeof(ddns_cb));
71  ddns_cb.dhcid_class = dns_rdatatype_dhcid;;
72
73  id = &ddns_cb.fwd_name;
74  if (!buffer_allocate(&id->buffer, namelen, MDL))
75    atf_tc_fail("Unable to allocate buffer for std test %d", test);
76  id->data = id->buffer->data;
77  memcpy(id->buffer->data, name, namelen);
78  id->len = namelen;
79
80    if (get_dhcid(&ddns_cb, type, clid, clidlen) != 1) {
81        atf_tc_fail("Unable to get std dhcid for %d", test);
82    } else if (ddns_cb.dhcid_class != dns_rdatatype_dhcid) {
83        atf_tc_fail("Wrong class for std dhcid for %d", test);
84    } else if (ddns_cb.dhcid.len != dhcid_len) {
85        atf_tc_fail("Wrong length for std dhcid for %d", test);
86    } else if (memcmp(ddns_cb.dhcid.data, dhcid, dhcid_len) != 0) {
87        atf_tc_fail("Wrong digest for std dhcid for %d", test);
88    }
89
90    /* clean up  */
91    data_string_forget(&ddns_cb.dhcid, MDL);
92
93    return;
94}
95ATF_TC(standard_dhcid);
96
97ATF_TC_HEAD(standard_dhcid, tc)
98{
99    atf_tc_set_md_var(tc, "descr", "Verify standard dhcid construction.");
100}
101
102
103ATF_TC_BODY(standard_dhcid, tc)
104{
105
106  call_get_std_dhcid(1, 2, clid_1, sizeof(clid_1),
107		     name_1, strlen(name_1),
108		     std_result_1, 35);
109
110
111  call_get_std_dhcid(2, 1, clid_2, sizeof(clid_2),
112		     name_2, strlen(name_2),
113		     std_result_2, 35);
114
115
116  call_get_std_dhcid(3, 0, clid_3, sizeof(clid_3),
117		     name_3, strlen(name_3),
118		     std_result_3, 35);
119}
120
121void call_get_int_dhcid(int test, int type,
122			u_int8_t *clid, unsigned clidlen,
123			char *dhcid, unsigned dhcid_len)
124{
125  dhcp_ddns_cb_t ddns_cb;
126
127  memset(&ddns_cb, 0, sizeof(ddns_cb));
128  ddns_cb.dhcid_class = dns_rdatatype_txt;;
129
130    if (get_dhcid(&ddns_cb, type, clid, clidlen) != 1) {
131        atf_tc_fail("Unable to get txt dhcid for %d", test);
132    } else if (ddns_cb.dhcid_class != dns_rdatatype_txt) {
133        atf_tc_fail("Wrong class for txt dhcid for %d", test);
134    } else if (ddns_cb.dhcid.len != dhcid_len) {
135        atf_tc_fail("Wrong length for txt dhcid for %d", test);
136    } else if (memcmp(ddns_cb.dhcid.data, dhcid, dhcid_len) != 0) {
137        atf_tc_fail("Wrong digest for txt dhcid for %d", test);
138    }
139
140    /* clean up  */
141    data_string_forget(&ddns_cb.dhcid, MDL);
142
143    return;
144}
145
146ATF_TC(interim_dhcid);
147
148ATF_TC_HEAD(interim_dhcid, tc)
149{
150    atf_tc_set_md_var(tc, "descr", "Verify interim dhcid construction.");
151}
152
153ATF_TC_BODY(interim_dhcid, tc)
154{
155
156  call_get_int_dhcid(1, 2, clid_1, sizeof(clid_1),
157		     int_result_1, 35);
158
159
160  call_get_int_dhcid(2, DHO_DHCP_CLIENT_IDENTIFIER,
161		     clid_2, sizeof(clid_2),
162		     int_result_2, 35);
163
164
165  call_get_int_dhcid(3, 0, clid_3, sizeof(clid_3),
166		     int_result_3, 35);
167
168}
169
170/* This macro defines main() method that will call specified
171   test cases. tp and simple_test_case names can be whatever you want
172   as long as it is a valid variable identifier. */
173ATF_TP_ADD_TCS(tp)
174{
175    ATF_TP_ADD_TC(tp, interim_dhcid);
176    ATF_TP_ADD_TC(tp, standard_dhcid);
177
178    return (atf_no_error());
179}
180
181#else /* NSUPDATE */
182ATF_TC(untested);
183ATF_TC_HEAD(untested, tc) {
184    atf_tc_set_md_var(tc, "descr", "skipping dns test");
185}
186ATF_TC_BODY(untested, tc) {
187    IGNORE_UNUSED(tc);
188    atf_tc_skip("NSUPDATE is not defined");
189}
190ATF_TP_ADD_TCS(tp) {
191    ATF_TP_ADD_TC(tp, untested);
192
193    return (atf_no_error());
194}
195#endif /* NSUPDATE */
196