2 Unix SMB/CIFS implementation.
6 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
7 Copyright (C) Volker Lendecke 2004
8 Copyright (C) Andrew Bartlett 2011
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "torture/torture.h"
29 const char *oid
; /* String OID */
30 const char *bin_oid
; /* Binary OID represented as string */
33 /* Data for successful OIDs conversions */
34 static const struct oid_data oid_data_ok
[] = {
53 .bin_oid
= "5504818003"
57 .bin_oid
= "5581800304"
60 .oid
= "2.5.2097155.4",
61 .bin_oid
= "558180800304"
64 .oid
= "2.5.4.130.16387.2097155.268435459",
65 .bin_oid
= "55048102818003818080038180808003"
69 /* Data for successful OIDs conversions */
70 static const char *oid_data_err
[] = {
72 ".2.5.4.130", /* first sub-identifier is empty */
73 "2.5.4.130.", /* last sub-identifier is empty */
74 "2..5.4.130", /* second sub-identifier is empty */
75 "2.5..4.130", /* third sub-identifier is empty */
76 "2.abc.4.130", /* invalid sub-identifier */
77 "2.5abc.4.130", /* invalid sub-identifier (alpha-numeric)*/
80 /* Data for successful Partial OIDs conversions */
81 static const struct oid_data partial_oid_data_ok
[] = {
83 .oid
= "2.5.4.130:0x81",
84 .bin_oid
= "5504810281"
87 .oid
= "2.5.4.16387:0x8180",
88 .bin_oid
= "55048180038180"
91 .oid
= "2.5.4.16387:0x81",
92 .bin_oid
= "550481800381"
95 .oid
= "2.5.2097155.4:0x818080",
96 .bin_oid
= "558180800304818080"
99 .oid
= "2.5.2097155.4:0x8180",
100 .bin_oid
= "5581808003048180"
103 .oid
= "2.5.2097155.4:0x81",
104 .bin_oid
= "55818080030481"
108 static const struct {
111 } integer_tests
[] = {
113 .blob
= {"\x02\x01\x00", 3},
117 .blob
= {"\x02\x01\x7f", 3},
121 .blob
= {"\x02\x02\x00\x80", 4},
125 .blob
= {"\x02\x02\x01\x00", 4},
129 .blob
= {"\x02\x01\x80", 3},
133 .blob
= {"\x02\x02\xff\x7f", 4},
137 .blob
= {"\x02\x01\xff", 3},
141 .blob
= {"\x02\x02\xff\x01", 4},
145 .blob
= {"\x02\x02\x00\xff", 4},
149 .blob
= {"\x02\x04\x80\x00\x00\x00", 6},
153 .blob
= {"\x02\x04\x7f\xff\xff\xff", 6},
158 /* Testing ber_write_OID_String() function */
159 static bool test_ber_write_OID_String(struct torture_context
*tctx
)
165 const struct oid_data
*data
= oid_data_ok
;
167 mem_ctx
= talloc_new(tctx
);
169 /* check for valid OIDs */
170 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
171 torture_assert(tctx
, ber_write_OID_String(mem_ctx
, &blob
, data
[i
].oid
),
172 "ber_write_OID_String failed");
174 hex_str
= hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
);
175 torture_assert(tctx
, hex_str
, "No memory!");
177 torture_assert(tctx
, strequal(data
[i
].bin_oid
, hex_str
),
178 talloc_asprintf(mem_ctx
,
179 "Failed: oid=%s, bin_oid:%s",
180 data
[i
].oid
, data
[i
].bin_oid
));
183 /* check for invalid OIDs */
184 for (i
= 0; i
< ARRAY_SIZE(oid_data_err
); i
++) {
186 !ber_write_OID_String(mem_ctx
, &blob
, oid_data_err
[i
]),
187 talloc_asprintf(mem_ctx
,
188 "Should fail for [%s] -> %s",
190 hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
)));
193 talloc_free(mem_ctx
);
198 /* Testing ber_read_OID_String() function */
199 static bool test_ber_read_OID_String(struct torture_context
*tctx
)
205 const struct oid_data
*data
= oid_data_ok
;
207 mem_ctx
= talloc_new(tctx
);
209 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
210 oid_blob
= strhex_to_data_blob(mem_ctx
, data
[i
].bin_oid
);
212 torture_assert(tctx
, ber_read_OID_String(mem_ctx
, oid_blob
, &oid
),
213 "ber_read_OID_String failed");
215 torture_assert(tctx
, strequal(data
[i
].oid
, oid
),
216 talloc_asprintf(mem_ctx
,
217 "Failed: oid=%s, bin_oid:%s",
218 data
[i
].oid
, data
[i
].bin_oid
));
221 talloc_free(mem_ctx
);
226 /* Testing ber_write_partial_OID_String() function */
227 static bool test_ber_write_partial_OID_String(struct torture_context
*tctx
)
233 const struct oid_data
*data
= oid_data_ok
;
235 mem_ctx
= talloc_new(tctx
);
237 /* ber_write_partial_OID_String() should work with not partial OIDs also */
238 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
239 torture_assert(tctx
, ber_write_partial_OID_String(mem_ctx
, &blob
, data
[i
].oid
),
240 "ber_write_partial_OID_String failed");
242 hex_str
= hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
);
243 torture_assert(tctx
, hex_str
, "No memory!");
245 torture_assert(tctx
, strequal(data
[i
].bin_oid
, hex_str
),
246 talloc_asprintf(mem_ctx
,
247 "Failed: oid=%s, bin_oid:%s",
248 data
[i
].oid
, data
[i
].bin_oid
));
251 /* ber_write_partial_OID_String() test with partial OIDs */
252 data
= partial_oid_data_ok
;
253 for (i
= 0; i
< ARRAY_SIZE(partial_oid_data_ok
); i
++) {
254 torture_assert(tctx
, ber_write_partial_OID_String(mem_ctx
, &blob
, data
[i
].oid
),
255 "ber_write_partial_OID_String failed");
257 hex_str
= hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
);
258 torture_assert(tctx
, hex_str
, "No memory!");
260 torture_assert(tctx
, strequal(data
[i
].bin_oid
, hex_str
),
261 talloc_asprintf(mem_ctx
,
262 "Failed: oid=%s, bin_oid:%s",
263 data
[i
].oid
, data
[i
].bin_oid
));
266 talloc_free(mem_ctx
);
271 /* Testing ber_read_partial_OID_String() function */
272 static bool test_ber_read_partial_OID_String(struct torture_context
*tctx
)
278 const struct oid_data
*data
= oid_data_ok
;
280 mem_ctx
= talloc_new(tctx
);
282 /* ber_read_partial_OID_String() should work with not partial OIDs also */
283 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
284 oid_blob
= strhex_to_data_blob(mem_ctx
, data
[i
].bin_oid
);
286 torture_assert(tctx
, ber_read_partial_OID_String(mem_ctx
, oid_blob
, &oid
),
287 "ber_read_partial_OID_String failed");
289 torture_assert(tctx
, strequal(data
[i
].oid
, oid
),
290 talloc_asprintf(mem_ctx
,
291 "Failed: oid=%s, bin_oid:%s",
292 data
[i
].oid
, data
[i
].bin_oid
));
295 /* ber_read_partial_OID_String() test with partial OIDs */
296 data
= partial_oid_data_ok
;
297 for (i
= 0; i
< ARRAY_SIZE(partial_oid_data_ok
); i
++) {
298 oid_blob
= strhex_to_data_blob(mem_ctx
, data
[i
].bin_oid
);
300 torture_assert(tctx
, ber_read_partial_OID_String(mem_ctx
, oid_blob
, &oid
),
301 "ber_read_partial_OID_String failed");
303 torture_assert(tctx
, strequal(data
[i
].oid
, oid
),
304 talloc_asprintf(mem_ctx
,
305 "Failed: oid=%s, bin_oid:%s",
306 data
[i
].oid
, data
[i
].bin_oid
));
309 talloc_free(mem_ctx
);
315 * Testing asn1_read_Integer and asn1_write_Integer functions,
316 * inspired by Love Hornquist Astrand
319 static bool test_asn1_Integer(struct torture_context
*tctx
)
324 mem_ctx
= talloc_new(tctx
);
326 for (i
= 0; i
< ARRAY_SIZE(integer_tests
); i
++) {
331 data
= asn1_init(mem_ctx
);
336 asn1_write_Integer(data
, integer_tests
[i
].value
);
338 blob
.data
= data
->data
;
339 blob
.length
= data
->length
;
340 torture_assert_data_blob_equal(tctx
, blob
, integer_tests
[i
].blob
, "asn1_write_Integer gave incorrect result");
342 asn1_load(data
, blob
);
343 torture_assert(tctx
, asn1_read_Integer(data
, &val
), "asn1_write_Integer output could not be read by asn1_read_Integer()");
345 torture_assert_int_equal(tctx
, val
, integer_tests
[i
].value
,
346 "readback of asn1_write_Integer output by asn1_read_Integer() failed");
349 talloc_free(mem_ctx
);
355 /* LOCAL-ASN1 test suite creation */
356 struct torture_suite
*torture_local_util_asn1(TALLOC_CTX
*mem_ctx
)
358 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "asn1");
360 torture_suite_add_simple_test(suite
, "ber_write_OID_String",
361 test_ber_write_OID_String
);
363 torture_suite_add_simple_test(suite
, "ber_read_OID_String",
364 test_ber_read_OID_String
);
366 torture_suite_add_simple_test(suite
, "ber_write_partial_OID_String",
367 test_ber_write_partial_OID_String
);
369 torture_suite_add_simple_test(suite
, "ber_read_partial_OID_String",
370 test_ber_read_partial_OID_String
);
372 torture_suite_add_simple_test(suite
, "asn1_Integer",