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"
26 #include "torture/local/proto.h"
30 const char *oid
; /* String OID */
31 const char *bin_oid
; /* Binary OID represented as string */
34 /* Data for successful OIDs conversions */
35 static const struct oid_data oid_data_ok
[] = {
54 .bin_oid
= "5504818003"
58 .bin_oid
= "5581800304"
61 .oid
= "2.5.2097155.4",
62 .bin_oid
= "558180800304"
65 .oid
= "2.5.4.130.16387.2097155.268435459",
66 .bin_oid
= "55048102818003818080038180808003"
70 /* Data for successful OIDs conversions */
71 static const char *oid_data_err
[] = {
73 ".2.5.4.130", /* first sub-identifier is empty */
74 "2.5.4.130.", /* last sub-identifier is empty */
75 "2..5.4.130", /* second sub-identifier is empty */
76 "2.5..4.130", /* third sub-identifier is empty */
77 "2.abc.4.130", /* invalid sub-identifier */
78 "2.5abc.4.130", /* invalid sub-identifier (alphanumeric)*/
81 /* Data for successful Partial OIDs conversions */
82 static const struct oid_data partial_oid_data_ok
[] = {
84 .oid
= "2.5.4.130:0x81",
85 .bin_oid
= "5504810281"
88 .oid
= "2.5.4.16387:0x8180",
89 .bin_oid
= "55048180038180"
92 .oid
= "2.5.4.16387:0x81",
93 .bin_oid
= "550481800381"
96 .oid
= "2.5.2097155.4:0x818080",
97 .bin_oid
= "558180800304818080"
100 .oid
= "2.5.2097155.4:0x8180",
101 .bin_oid
= "5581808003048180"
104 .oid
= "2.5.2097155.4:0x81",
105 .bin_oid
= "55818080030481"
109 static const struct {
112 } integer_tests
[] = {
114 .blob
= { discard_const_p(uint8_t, "\x02\x01\x00"), 3},
118 .blob
= { discard_const_p(uint8_t, "\x02\x01\x7f"), 3},
122 .blob
= { discard_const_p(uint8_t, "\x02\x02\x00\x80"), 4},
126 .blob
= { discard_const_p(uint8_t, "\x02\x02\x01\x00"), 4},
130 .blob
= { discard_const_p(uint8_t, "\x02\x01\x80"), 3},
134 .blob
= { discard_const_p(uint8_t, "\x02\x02\xff\x7f"), 4},
138 .blob
= { discard_const_p(uint8_t, "\x02\x01\xff"), 3},
142 .blob
= { discard_const_p(uint8_t, "\x02\x02\xff\x01"), 4},
146 .blob
= { discard_const_p(uint8_t, "\x02\x02\x00\xff"), 4},
150 .blob
= { discard_const_p(uint8_t, "\x02\x04\x80\x00\x00\x00"), 6},
154 .blob
= { discard_const_p(uint8_t, "\x02\x04\x7f\xff\xff\xff"), 6},
159 /* Testing ber_write_OID_String() function */
160 static bool test_ber_write_OID_String(struct torture_context
*tctx
)
166 const struct oid_data
*data
= oid_data_ok
;
168 mem_ctx
= talloc_new(tctx
);
170 /* check for valid OIDs */
171 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
172 torture_assert(tctx
, ber_write_OID_String(mem_ctx
, &blob
, data
[i
].oid
),
173 "ber_write_OID_String failed");
175 hex_str
= hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
);
176 torture_assert(tctx
, hex_str
, "No memory!");
178 torture_assert(tctx
, strequal(data
[i
].bin_oid
, hex_str
),
179 talloc_asprintf(mem_ctx
,
180 "Failed: oid=%s, bin_oid:%s",
181 data
[i
].oid
, data
[i
].bin_oid
));
184 /* check for invalid OIDs */
185 for (i
= 0; i
< ARRAY_SIZE(oid_data_err
); i
++) {
187 !ber_write_OID_String(mem_ctx
, &blob
, oid_data_err
[i
]),
188 talloc_asprintf(mem_ctx
,
189 "Should fail for [%s] -> %s",
191 hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
)));
194 talloc_free(mem_ctx
);
199 /* Testing ber_read_OID_String() function */
200 static bool test_ber_read_OID_String(struct torture_context
*tctx
)
206 const struct oid_data
*data
= oid_data_ok
;
208 mem_ctx
= talloc_new(tctx
);
210 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
211 oid_blob
= strhex_to_data_blob(mem_ctx
, data
[i
].bin_oid
);
213 torture_assert(tctx
, ber_read_OID_String(mem_ctx
, oid_blob
, &oid
),
214 "ber_read_OID_String failed");
216 torture_assert(tctx
, strequal(data
[i
].oid
, oid
),
217 talloc_asprintf(mem_ctx
,
218 "Failed: oid=%s, bin_oid:%s",
219 data
[i
].oid
, data
[i
].bin_oid
));
222 talloc_free(mem_ctx
);
227 /* Testing ber_write_partial_OID_String() function */
228 static bool test_ber_write_partial_OID_String(struct torture_context
*tctx
)
234 const struct oid_data
*data
= oid_data_ok
;
236 mem_ctx
= talloc_new(tctx
);
238 /* ber_write_partial_OID_String() should work with not partial OIDs also */
239 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
240 torture_assert(tctx
, ber_write_partial_OID_String(mem_ctx
, &blob
, data
[i
].oid
),
241 "ber_write_partial_OID_String failed");
243 hex_str
= hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
);
244 torture_assert(tctx
, hex_str
, "No memory!");
246 torture_assert(tctx
, strequal(data
[i
].bin_oid
, hex_str
),
247 talloc_asprintf(mem_ctx
,
248 "Failed: oid=%s, bin_oid:%s",
249 data
[i
].oid
, data
[i
].bin_oid
));
252 /* ber_write_partial_OID_String() test with partial OIDs */
253 data
= partial_oid_data_ok
;
254 for (i
= 0; i
< ARRAY_SIZE(partial_oid_data_ok
); i
++) {
255 torture_assert(tctx
, ber_write_partial_OID_String(mem_ctx
, &blob
, data
[i
].oid
),
256 "ber_write_partial_OID_String failed");
258 hex_str
= hex_encode_talloc(mem_ctx
, blob
.data
, blob
.length
);
259 torture_assert(tctx
, hex_str
, "No memory!");
261 torture_assert(tctx
, strequal(data
[i
].bin_oid
, hex_str
),
262 talloc_asprintf(mem_ctx
,
263 "Failed: oid=%s, bin_oid:%s",
264 data
[i
].oid
, data
[i
].bin_oid
));
267 talloc_free(mem_ctx
);
272 /* Testing ber_read_partial_OID_String() function */
273 static bool test_ber_read_partial_OID_String(struct torture_context
*tctx
)
279 const struct oid_data
*data
= oid_data_ok
;
281 mem_ctx
= talloc_new(tctx
);
283 /* ber_read_partial_OID_String() should work with not partial OIDs also */
284 for (i
= 0; i
< ARRAY_SIZE(oid_data_ok
); i
++) {
285 oid_blob
= strhex_to_data_blob(mem_ctx
, data
[i
].bin_oid
);
287 torture_assert(tctx
, ber_read_partial_OID_String(mem_ctx
, oid_blob
, &oid
),
288 "ber_read_partial_OID_String failed");
290 torture_assert(tctx
, strequal(data
[i
].oid
, oid
),
291 talloc_asprintf(mem_ctx
,
292 "Failed: oid=%s, bin_oid:%s",
293 data
[i
].oid
, data
[i
].bin_oid
));
296 /* ber_read_partial_OID_String() test with partial OIDs */
297 data
= partial_oid_data_ok
;
298 for (i
= 0; i
< ARRAY_SIZE(partial_oid_data_ok
); i
++) {
299 oid_blob
= strhex_to_data_blob(mem_ctx
, data
[i
].bin_oid
);
301 torture_assert(tctx
, ber_read_partial_OID_String(mem_ctx
, oid_blob
, &oid
),
302 "ber_read_partial_OID_String failed");
304 torture_assert(tctx
, strequal(data
[i
].oid
, oid
),
305 talloc_asprintf(mem_ctx
,
306 "Failed: oid=%s, bin_oid:%s",
307 data
[i
].oid
, data
[i
].bin_oid
));
310 talloc_free(mem_ctx
);
316 * Testing asn1_read_Integer and asn1_write_Integer functions,
317 * inspired by Love Hornquist Astrand
320 static bool test_asn1_Integer(struct torture_context
*tctx
)
326 mem_ctx
= talloc_new(tctx
);
328 for (i
= 0; i
< ARRAY_SIZE(integer_tests
); i
++) {
333 data
= asn1_init(mem_ctx
, ASN1_MAX_TREE_DEPTH
);
338 if (!asn1_write_Integer(data
, integer_tests
[i
].value
)) goto err
;
340 if (!asn1_blob(data
, &blob
)) {
344 torture_assert_data_blob_equal(tctx
, blob
, integer_tests
[i
].blob
, "asn1_write_Integer gave incorrect result");
346 if (!asn1_load(data
, blob
)) goto err
;
347 torture_assert(tctx
, asn1_read_Integer(data
, &val
), "asn1_write_Integer output could not be read by asn1_read_Integer()");
349 torture_assert_int_equal(tctx
, val
, integer_tests
[i
].value
,
350 "readback of asn1_write_Integer output by asn1_read_Integer() failed");
357 talloc_free(mem_ctx
);
362 /* LOCAL-ASN1 test suite creation */
363 struct torture_suite
*torture_local_util_asn1(TALLOC_CTX
*mem_ctx
)
365 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "asn1");
367 torture_suite_add_simple_test(suite
, "ber_write_OID_String",
368 test_ber_write_OID_String
);
370 torture_suite_add_simple_test(suite
, "ber_read_OID_String",
371 test_ber_read_OID_String
);
373 torture_suite_add_simple_test(suite
, "ber_write_partial_OID_String",
374 test_ber_write_partial_OID_String
);
376 torture_suite_add_simple_test(suite
, "ber_read_partial_OID_String",
377 test_ber_read_partial_OID_String
);
379 torture_suite_add_simple_test(suite
, "asn1_Integer",