s4 dns: Import DNS win32 error codes from MS-ERREF
[Samba/gebeck_regimport.git] / lib / util / tests / asn1_tests.c
blob97f77565901a35c5095a9ce74409b469956e787e
1 /*
2 Unix SMB/CIFS implementation.
4 util_asn1 testing
6 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "../asn1.h"
26 struct oid_data {
27 const char *oid; /* String OID */
28 const char *bin_oid; /* Binary OID represented as string */
31 /* Data for successful OIDs conversions */
32 static const struct oid_data oid_data_ok[] = {
34 .oid = "2.5.4.0",
35 .bin_oid = "550400"
38 .oid = "2.5.4.1",
39 .bin_oid = "550401"
42 .oid = "2.5.4.130",
43 .bin_oid = "55048102"
46 .oid = "2.5.130.4",
47 .bin_oid = "55810204"
50 .oid = "2.5.4.16387",
51 .bin_oid = "5504818003"
54 .oid = "2.5.16387.4",
55 .bin_oid = "5581800304"
58 .oid = "2.5.2097155.4",
59 .bin_oid = "558180800304"
62 .oid = "2.5.4.130.16387.2097155.268435459",
63 .bin_oid = "55048102818003818080038180808003"
67 /* Data for successful OIDs conversions */
68 static const char *oid_data_err[] = {
69 "", /* empty OID */
70 ".2.5.4.130", /* first sub-identifier is empty */
71 "2.5.4.130.", /* last sub-identifier is empty */
72 "2..5.4.130", /* second sub-identifier is empty */
73 "2.5..4.130", /* third sub-identifier is empty */
74 "2.abc.4.130", /* invalid sub-identifier */
75 "2.5abc.4.130", /* invalid sub-identifier (alpha-numeric)*/
78 /* Data for successful Partial OIDs conversions */
79 static const struct oid_data partial_oid_data_ok[] = {
81 .oid = "2.5.4.130:0x81",
82 .bin_oid = "5504810281"
85 .oid = "2.5.4.16387:0x8180",
86 .bin_oid = "55048180038180"
89 .oid = "2.5.4.16387:0x81",
90 .bin_oid = "550481800381"
93 .oid = "2.5.2097155.4:0x818080",
94 .bin_oid = "558180800304818080"
97 .oid = "2.5.2097155.4:0x8180",
98 .bin_oid = "5581808003048180"
101 .oid = "2.5.2097155.4:0x81",
102 .bin_oid = "55818080030481"
107 /* Testing ber_write_OID_String() function */
108 static bool test_ber_write_OID_String(struct torture_context *tctx)
110 int i;
111 char *hex_str;
112 DATA_BLOB blob;
113 TALLOC_CTX *mem_ctx;
114 const struct oid_data *data = oid_data_ok;
116 mem_ctx = talloc_new(tctx);
118 /* check for valid OIDs */
119 for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
120 torture_assert(tctx, ber_write_OID_String(mem_ctx, &blob, data[i].oid),
121 "ber_write_OID_String failed");
123 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
124 torture_assert(tctx, hex_str, "No memory!");
126 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
127 talloc_asprintf(mem_ctx,
128 "Failed: oid=%s, bin_oid:%s",
129 data[i].oid, data[i].bin_oid));
132 /* check for invalid OIDs */
133 for (i = 0; i < ARRAY_SIZE(oid_data_err); i++) {
134 torture_assert(tctx,
135 !ber_write_OID_String(mem_ctx, &blob, oid_data_err[i]),
136 talloc_asprintf(mem_ctx,
137 "Should fail for [%s] -> %s",
138 oid_data_err[i],
139 hex_encode_talloc(mem_ctx, blob.data, blob.length)));
142 talloc_free(mem_ctx);
144 return true;
147 /* Testing ber_read_OID_String() function */
148 static bool test_ber_read_OID_String(struct torture_context *tctx)
150 int i;
151 const char *oid;
152 DATA_BLOB oid_blob;
153 TALLOC_CTX *mem_ctx;
154 const struct oid_data *data = oid_data_ok;
156 mem_ctx = talloc_new(tctx);
158 for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
159 oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);
161 torture_assert(tctx, ber_read_OID_String(mem_ctx, oid_blob, &oid),
162 "ber_read_OID_String failed");
164 torture_assert(tctx, strequal(data[i].oid, oid),
165 talloc_asprintf(mem_ctx,
166 "Failed: oid=%s, bin_oid:%s",
167 data[i].oid, data[i].bin_oid));
170 talloc_free(mem_ctx);
172 return true;
175 /* Testing ber_write_partial_OID_String() function */
176 static bool test_ber_write_partial_OID_String(struct torture_context *tctx)
178 int i;
179 char *hex_str;
180 DATA_BLOB blob;
181 TALLOC_CTX *mem_ctx;
182 const struct oid_data *data = oid_data_ok;
184 mem_ctx = talloc_new(tctx);
186 /* ber_write_partial_OID_String() should work with not partial OIDs also */
187 for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
188 torture_assert(tctx, ber_write_partial_OID_String(mem_ctx, &blob, data[i].oid),
189 "ber_write_partial_OID_String failed");
191 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
192 torture_assert(tctx, hex_str, "No memory!");
194 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
195 talloc_asprintf(mem_ctx,
196 "Failed: oid=%s, bin_oid:%s",
197 data[i].oid, data[i].bin_oid));
200 /* ber_write_partial_OID_String() test with partial OIDs */
201 data = partial_oid_data_ok;
202 for (i = 0; i < ARRAY_SIZE(partial_oid_data_ok); i++) {
203 torture_assert(tctx, ber_write_partial_OID_String(mem_ctx, &blob, data[i].oid),
204 "ber_write_partial_OID_String failed");
206 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
207 torture_assert(tctx, hex_str, "No memory!");
209 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
210 talloc_asprintf(mem_ctx,
211 "Failed: oid=%s, bin_oid:%s",
212 data[i].oid, data[i].bin_oid));
215 talloc_free(mem_ctx);
217 return true;
220 /* Testing ber_read_partial_OID_String() function */
221 static bool test_ber_read_partial_OID_String(struct torture_context *tctx)
223 int i;
224 const char *oid;
225 DATA_BLOB oid_blob;
226 TALLOC_CTX *mem_ctx;
227 const struct oid_data *data = oid_data_ok;
229 mem_ctx = talloc_new(tctx);
231 /* ber_read_partial_OID_String() should work with not partial OIDs also */
232 for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
233 oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);
235 torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
236 "ber_read_partial_OID_String failed");
238 torture_assert(tctx, strequal(data[i].oid, oid),
239 talloc_asprintf(mem_ctx,
240 "Failed: oid=%s, bin_oid:%s",
241 data[i].oid, data[i].bin_oid));
244 /* ber_read_partial_OID_String() test with partial OIDs */
245 data = partial_oid_data_ok;
246 for (i = 0; i < ARRAY_SIZE(partial_oid_data_ok); i++) {
247 oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);
249 torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
250 "ber_read_partial_OID_String failed");
252 torture_assert(tctx, strequal(data[i].oid, oid),
253 talloc_asprintf(mem_ctx,
254 "Failed: oid=%s, bin_oid:%s",
255 data[i].oid, data[i].bin_oid));
258 talloc_free(mem_ctx);
260 return true;
264 /* LOCAL-ASN1 test suite creation */
265 struct torture_suite *torture_local_util_asn1(TALLOC_CTX *mem_ctx)
267 struct torture_suite *suite = torture_suite_create(mem_ctx, "ASN1");
269 torture_suite_add_simple_test(suite, "ber_write_OID_String",
270 test_ber_write_OID_String);
272 torture_suite_add_simple_test(suite, "ber_read_OID_String",
273 test_ber_read_OID_String);
275 torture_suite_add_simple_test(suite, "ber_write_partial_OID_String",
276 test_ber_write_partial_OID_String);
278 torture_suite_add_simple_test(suite, "ber_read_partial_OID_String",
279 test_ber_read_partial_OID_String);
281 return suite;