xref: /minix3/external/bsd/dhcp/dist/common/tests/dns_unittest.c (revision d56f51ea7d8b9045e5c8e2028422523d3f9a5840)
1 /*	$NetBSD: dns_unittest.c,v 1.1.1.1 2014/07/12 11:57:48 spz Exp $	*/
2 /*
3  * Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <config.h>
19 #include <atf-c.h>
20 #include "dhcpd.h"
21 
22 /*
23  * This file provides unit tests for the dns and ddns code.
24  * Currently this is limited to verifying the dhcid code is
25  * working properly.  In time we may be able to expand the
26  * tests to cover other areas.
27  *
28  * The tests for the interim txt records comapre to previous
29  * internally generated values.
30  *
31  * The tests for the standard dhcid records compare to values
32  * from rfc 4701
33  */
34 
35 char *name_1 = "chi6.example.com";
36 u_int8_t clid_1[] = {0x00, 0x01, 0x00, 0x06, 0x41, 0x2d, 0xf1, 0x66, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
37 u_int8_t std_result_1[] = {0x00, 0x02, 0x01, 0x63, 0x6f, 0xc0, 0xb8, 0x27, 0x1c,
38 			  0x82, 0x82, 0x5b, 0xb1, 0xac, 0x5c, 0x41, 0xcf, 0x53,
39 			  0x51, 0xaa, 0x69, 0xb4, 0xfe, 0xbd, 0x94, 0xe8, 0xf1,
40 			  0x7c, 0xdb, 0x95, 0x00, 0x0d, 0xa4, 0x8c, 0x40};
41 char *int_result_1 = "\"02abf8cd3753dc1847be40858becd77865";
42 
43 char *name_2 = "chi.example.com";
44 u_int8_t clid_2[] = {0x01, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c};
45 u_int8_t std_result_2[] = {0x00, 0x01, 0x01, 0x39, 0x20, 0xfe, 0x5d, 0x1d, 0xce,
46 			  0xb3, 0xfd, 0x0b, 0xa3, 0x37, 0x97, 0x56, 0xa7, 0x0d,
47 			  0x73, 0xb1, 0x70, 0x09, 0xf4, 0x1d, 0x58, 0xbd, 0xdb,
48 			  0xfc, 0xd6, 0xa2, 0x50, 0x39, 0x56, 0xd8, 0xda};
49 char *int_result_2 = "\"31934ffa9344a3ab86c380505a671e5113";
50 
51 char *name_3 = "client.example.com";
52 u_int8_t clid_3[] = {0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
53 u_int8_t std_result_3[] = {0x00, 0x00, 0x01, 0xc4, 0xb9, 0xa5, 0xb2, 0x49, 0x65,
54 			  0x13, 0x43, 0x15, 0x8d, 0xde, 0x7b, 0xcc, 0x77, 0x16,
55 			  0x98, 0x41, 0xf7, 0xa4, 0x24, 0x3a, 0x57, 0x2b, 0x5c,
56 			  0x28, 0x3f, 0xff, 0xed, 0xeb, 0x3f, 0x75, 0xe6};
57 char *int_result_3 = "\"0046b6cacea62dc1d4567b068175d1f808";
58 
59 void call_get_std_dhcid(int test, int type,
60 			u_int8_t *clid, unsigned clidlen,
61 			char *name, unsigned namelen,
62 			u_int8_t *dhcid, unsigned dhcid_len)
63 {
64   dhcp_ddns_cb_t ddns_cb;
65   struct data_string *id;
66 
67   memset(&ddns_cb, 0, sizeof(ddns_cb));
68   ddns_cb.dhcid_class = dns_rdatatype_dhcid;;
69 
70   id = &ddns_cb.fwd_name;
71   if (!buffer_allocate(&id->buffer, namelen, MDL))
72     atf_tc_fail("Unable to allocate buffer for std test %d", test);
73   id->data = id->buffer->data;
74   memcpy(id->buffer->data, name, namelen);
75   id->len = namelen;
76 
77     if (get_dhcid(&ddns_cb, type, clid, clidlen) != 1) {
78         atf_tc_fail("Unable to get std dhcid for %d", test);
79     } else if (ddns_cb.dhcid_class != dns_rdatatype_dhcid) {
80         atf_tc_fail("Wrong class for std dhcid for %d", test);
81     } else if (ddns_cb.dhcid.len != dhcid_len) {
82         atf_tc_fail("Wrong length for std dhcid for %d", test);
83     } else if (memcmp(ddns_cb.dhcid.data, dhcid, dhcid_len) != 0) {
84         atf_tc_fail("Wrong digest for std dhcid for %d", test);
85     }
86 
87     /* clean up  */
88     data_string_forget(&ddns_cb.dhcid, MDL);
89 
90     return;
91 }
92 ATF_TC(standard_dhcid);
93 
94 ATF_TC_HEAD(standard_dhcid, tc)
95 {
96     atf_tc_set_md_var(tc, "descr", "Verify standard dhcid construction.");
97 }
98 
99 
100 ATF_TC_BODY(standard_dhcid, tc)
101 {
102 
103   call_get_std_dhcid(1, 2, clid_1, sizeof(clid_1),
104 		     name_1, strlen(name_1),
105 		     std_result_1, 35);
106 
107 
108   call_get_std_dhcid(2, 1, clid_2, sizeof(clid_2),
109 		     name_2, strlen(name_2),
110 		     std_result_2, 35);
111 
112 
113   call_get_std_dhcid(3, 0, clid_3, sizeof(clid_3),
114 		     name_3, strlen(name_3),
115 		     std_result_3, 35);
116 }
117 
118 void call_get_int_dhcid(int test, int type,
119 			u_int8_t *clid, unsigned clidlen,
120 			char *dhcid, unsigned dhcid_len)
121 {
122   dhcp_ddns_cb_t ddns_cb;
123 
124   memset(&ddns_cb, 0, sizeof(ddns_cb));
125   ddns_cb.dhcid_class = dns_rdatatype_txt;;
126 
127     if (get_dhcid(&ddns_cb, type, clid, clidlen) != 1) {
128         atf_tc_fail("Unable to get txt dhcid for %d", test);
129     } else if (ddns_cb.dhcid_class != dns_rdatatype_txt) {
130         atf_tc_fail("Wrong class for txt dhcid for %d", test);
131     } else if (ddns_cb.dhcid.len != dhcid_len) {
132         atf_tc_fail("Wrong length for txt dhcid for %d", test);
133     } else if (memcmp(ddns_cb.dhcid.data, dhcid, dhcid_len) != 0) {
134         atf_tc_fail("Wrong digest for txt dhcid for %d", test);
135     }
136 
137     /* clean up  */
138     data_string_forget(&ddns_cb.dhcid, MDL);
139 
140     return;
141 }
142 
143 ATF_TC(interim_dhcid);
144 
145 ATF_TC_HEAD(interim_dhcid, tc)
146 {
147     atf_tc_set_md_var(tc, "descr", "Verify interim dhcid construction.");
148 }
149 
150 ATF_TC_BODY(interim_dhcid, tc)
151 {
152 
153   call_get_int_dhcid(1, 2, clid_1, sizeof(clid_1),
154 		     int_result_1, 35);
155 
156 
157   call_get_int_dhcid(2, DHO_DHCP_CLIENT_IDENTIFIER,
158 		     clid_2, sizeof(clid_2),
159 		     int_result_2, 35);
160 
161 
162   call_get_int_dhcid(3, 0, clid_3, sizeof(clid_3),
163 		     int_result_3, 35);
164 
165 }
166 
167 /* This macro defines main() method that will call specified
168    test cases. tp and simple_test_case names can be whatever you want
169    as long as it is a valid variable identifier. */
170 ATF_TP_ADD_TCS(tp)
171 {
172     ATF_TP_ADD_TC(tp, interim_dhcid);
173     ATF_TP_ADD_TC(tp, standard_dhcid);
174 
175     return (atf_no_error());
176 }
177