97f77565901a35c5095a9ce74409b469956e787e
[metze/samba/wip.git] / lib / util / tests / asn1_tests.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    util_asn1 testing
5
6    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "../asn1.h"
25
26 struct oid_data {
27         const char *oid;        /* String OID */
28         const char *bin_oid;    /* Binary OID represented as string */
29 };
30
31 /* Data for successful OIDs conversions */
32 static const struct oid_data oid_data_ok[] = {
33         {
34                 .oid = "2.5.4.0",
35                 .bin_oid = "550400"
36         },
37         {
38                 .oid = "2.5.4.1",
39                 .bin_oid = "550401"
40         },
41         {
42                 .oid = "2.5.4.130",
43                 .bin_oid = "55048102"
44         },
45         {
46                 .oid = "2.5.130.4",
47                 .bin_oid = "55810204"
48         },
49         {
50                 .oid = "2.5.4.16387",
51                 .bin_oid = "5504818003"
52         },
53         {
54                 .oid = "2.5.16387.4",
55                 .bin_oid = "5581800304"
56         },
57         {
58                 .oid = "2.5.2097155.4",
59                 .bin_oid = "558180800304"
60         },
61         {
62                 .oid = "2.5.4.130.16387.2097155.268435459",
63                 .bin_oid = "55048102818003818080038180808003"
64         },
65 };
66
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)*/
76 };
77
78 /* Data for successful Partial OIDs conversions */
79 static const struct oid_data partial_oid_data_ok[] = {
80         {
81                 .oid = "2.5.4.130:0x81",
82                 .bin_oid = "5504810281"
83         },
84         {
85                 .oid = "2.5.4.16387:0x8180",
86                 .bin_oid = "55048180038180"
87         },
88         {
89                 .oid = "2.5.4.16387:0x81",
90                 .bin_oid = "550481800381"
91         },
92         {
93                 .oid = "2.5.2097155.4:0x818080",
94                 .bin_oid = "558180800304818080"
95         },
96         {
97                 .oid = "2.5.2097155.4:0x8180",
98                 .bin_oid = "5581808003048180"
99         },
100         {
101                 .oid = "2.5.2097155.4:0x81",
102                 .bin_oid = "55818080030481"
103         },
104 };
105
106
107 /* Testing ber_write_OID_String() function */
108 static bool test_ber_write_OID_String(struct torture_context *tctx)
109 {
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;
115
116         mem_ctx = talloc_new(tctx);
117
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");
122
123                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
124                 torture_assert(tctx, hex_str, "No memory!");
125
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));
130         }
131
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)));
140         }
141
142         talloc_free(mem_ctx);
143
144         return true;
145 }
146
147 /* Testing ber_read_OID_String() function */
148 static bool test_ber_read_OID_String(struct torture_context *tctx)
149 {
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;
155
156         mem_ctx = talloc_new(tctx);
157
158         for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
159                 oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);
160
161                 torture_assert(tctx, ber_read_OID_String(mem_ctx, oid_blob, &oid),
162                                 "ber_read_OID_String failed");
163
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));
168         }
169
170         talloc_free(mem_ctx);
171
172         return true;
173 }
174
175 /* Testing ber_write_partial_OID_String() function */
176 static bool test_ber_write_partial_OID_String(struct torture_context *tctx)
177 {
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;
183
184         mem_ctx = talloc_new(tctx);
185
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");
190
191                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
192                 torture_assert(tctx, hex_str, "No memory!");
193
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));
198         }
199
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");
205
206                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
207                 torture_assert(tctx, hex_str, "No memory!");
208
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));
213         }
214
215         talloc_free(mem_ctx);
216
217         return true;
218 }
219
220 /* Testing ber_read_partial_OID_String() function */
221 static bool test_ber_read_partial_OID_String(struct torture_context *tctx)
222 {
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;
228
229         mem_ctx = talloc_new(tctx);
230
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);
234
235                 torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
236                                 "ber_read_partial_OID_String failed");
237
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));
242         }
243
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);
248
249                 torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
250                                 "ber_read_partial_OID_String failed");
251
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));
256         }
257
258         talloc_free(mem_ctx);
259
260         return true;
261 }
262
263
264 /* LOCAL-ASN1 test suite creation */
265 struct torture_suite *torture_local_util_asn1(TALLOC_CTX *mem_ctx)
266 {
267         struct torture_suite *suite = torture_suite_create(mem_ctx, "ASN1");
268
269         torture_suite_add_simple_test(suite, "ber_write_OID_String",
270                                       test_ber_write_OID_String);
271
272         torture_suite_add_simple_test(suite, "ber_read_OID_String",
273                                       test_ber_read_OID_String);
274
275         torture_suite_add_simple_test(suite, "ber_write_partial_OID_String",
276                                       test_ber_write_partial_OID_String);
277
278         torture_suite_add_simple_test(suite, "ber_read_partial_OID_String",
279                                       test_ber_read_partial_OID_String);
280
281         return suite;
282 }