s3-util_sock: Rise debug level for getpeername failed messages.
[Samba/gebeck_regimport.git] / lib / util / tests / asn1_tests.c
blob25c82227aec28631290623a35df2eee906033878
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 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 Partial OIDs conversions */
68 struct oid_data partial_oid_data_ok[] = {
70 .oid = "2.5.4.130:0x81",
71 .bin_oid = "5504810281"
74 .oid = "2.5.4.16387:0x8180",
75 .bin_oid = "55048180038180"
78 .oid = "2.5.4.16387:0x81",
79 .bin_oid = "550481800381"
82 .oid = "2.5.2097155.4:0x818080",
83 .bin_oid = "558180800304818080"
86 .oid = "2.5.2097155.4:0x8180",
87 .bin_oid = "5581808003048180"
90 .oid = "2.5.2097155.4:0x81",
91 .bin_oid = "55818080030481"
96 /* Testing ber_write_OID_String() function */
97 static bool test_ber_write_OID_String(struct torture_context *tctx)
99 int i;
100 char *hex_str;
101 DATA_BLOB blob;
102 TALLOC_CTX *mem_ctx;
103 struct oid_data *data = oid_data_ok;
105 mem_ctx = talloc_new(tctx);
107 for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
108 torture_assert(tctx, ber_write_OID_String(mem_ctx, &blob, data[i].oid),
109 "ber_write_OID_String failed");
111 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
112 torture_assert(tctx, hex_str, "No memory!");
114 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
115 talloc_asprintf(mem_ctx,
116 "Failed: oid=%s, bin_oid:%s",
117 data[i].oid, data[i].bin_oid));
120 talloc_free(mem_ctx);
122 return true;
125 /* Testing ber_read_OID_String() function */
126 static bool test_ber_read_OID_String(struct torture_context *tctx)
128 int i;
129 const char *oid;
130 DATA_BLOB oid_blob;
131 TALLOC_CTX *mem_ctx;
132 struct oid_data *data = oid_data_ok;
134 mem_ctx = talloc_new(tctx);
136 for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
137 oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);
139 torture_assert(tctx, ber_read_OID_String(mem_ctx, oid_blob, &oid),
140 "ber_read_OID_String failed");
142 torture_assert(tctx, strequal(data[i].oid, oid),
143 talloc_asprintf(mem_ctx,
144 "Failed: oid=%s, bin_oid:%s",
145 data[i].oid, data[i].bin_oid));
148 talloc_free(mem_ctx);
150 return true;
153 /* Testing ber_write_partial_OID_String() function */
154 static bool test_ber_write_partial_OID_String(struct torture_context *tctx)
156 int i;
157 char *hex_str;
158 DATA_BLOB blob;
159 TALLOC_CTX *mem_ctx;
160 struct oid_data *data = oid_data_ok;
162 mem_ctx = talloc_new(tctx);
164 /* ber_write_partial_OID_String() should work with not partial OIDs also */
165 for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
166 torture_assert(tctx, ber_write_partial_OID_String(mem_ctx, &blob, data[i].oid),
167 "ber_write_partial_OID_String failed");
169 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
170 torture_assert(tctx, hex_str, "No memory!");
172 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
173 talloc_asprintf(mem_ctx,
174 "Failed: oid=%s, bin_oid:%s",
175 data[i].oid, data[i].bin_oid));
178 /* ber_write_partial_OID_String() test with partial OIDs */
179 data = partial_oid_data_ok;
180 for (i = 0; i < ARRAY_SIZE(partial_oid_data_ok); i++) {
181 torture_assert(tctx, ber_write_partial_OID_String(mem_ctx, &blob, data[i].oid),
182 "ber_write_partial_OID_String failed");
184 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
185 torture_assert(tctx, hex_str, "No memory!");
187 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
188 talloc_asprintf(mem_ctx,
189 "Failed: oid=%s, bin_oid:%s",
190 data[i].oid, data[i].bin_oid));
193 talloc_free(mem_ctx);
195 return true;
198 /* Testing ber_read_partial_OID_String() function */
199 static bool test_ber_read_partial_OID_String(struct torture_context *tctx)
201 int i;
202 const char *oid;
203 DATA_BLOB oid_blob;
204 TALLOC_CTX *mem_ctx;
205 struct oid_data *data = oid_data_ok;
207 mem_ctx = talloc_new(tctx);
209 /* ber_read_partial_OID_String() should work with not partial OIDs also */
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_partial_OID_String(mem_ctx, oid_blob, &oid),
214 "ber_read_partial_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 /* ber_read_partial_OID_String() test with partial OIDs */
223 data = partial_oid_data_ok;
224 for (i = 0; i < ARRAY_SIZE(partial_oid_data_ok); i++) {
225 oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);
227 torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
228 "ber_read_partial_OID_String failed");
230 torture_assert(tctx, strequal(data[i].oid, oid),
231 talloc_asprintf(mem_ctx,
232 "Failed: oid=%s, bin_oid:%s",
233 data[i].oid, data[i].bin_oid));
236 talloc_free(mem_ctx);
238 return true;
242 /* LOCAL-ASN1 test suite creation */
243 struct torture_suite *torture_local_util_asn1(TALLOC_CTX *mem_ctx)
245 struct torture_suite *suite = torture_suite_create(mem_ctx, "ASN1");
247 torture_suite_add_simple_test(suite, "ber_write_OID_String",
248 test_ber_write_OID_String);
250 torture_suite_add_simple_test(suite, "ber_read_OID_String",
251 test_ber_read_OID_String);
253 torture_suite_add_simple_test(suite, "ber_write_partial_OID_String",
254 test_ber_write_partial_OID_String);
256 torture_suite_add_simple_test(suite, "ber_read_partial_OID_String",
257 test_ber_read_partial_OID_String);
259 return suite;