asn1_tests.c: Make test data static const
[abartlet/samba.git/.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 Partial OIDs conversions */
68 static const struct oid_data partial_oid_data_ok[] = {
69         {
70                 .oid = "2.5.4.130:0x81",
71                 .bin_oid = "5504810281"
72         },
73         {
74                 .oid = "2.5.4.16387:0x8180",
75                 .bin_oid = "55048180038180"
76         },
77         {
78                 .oid = "2.5.4.16387:0x81",
79                 .bin_oid = "550481800381"
80         },
81         {
82                 .oid = "2.5.2097155.4:0x818080",
83                 .bin_oid = "558180800304818080"
84         },
85         {
86                 .oid = "2.5.2097155.4:0x8180",
87                 .bin_oid = "5581808003048180"
88         },
89         {
90                 .oid = "2.5.2097155.4:0x81",
91                 .bin_oid = "55818080030481"
92         },
93 };
94
95
96 /* Testing ber_write_OID_String() function */
97 static bool test_ber_write_OID_String(struct torture_context *tctx)
98 {
99         int i;
100         char *hex_str;
101         DATA_BLOB blob;
102         TALLOC_CTX *mem_ctx;
103         const struct oid_data *data = oid_data_ok;
104
105         mem_ctx = talloc_new(tctx);
106
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");
110
111                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
112                 torture_assert(tctx, hex_str, "No memory!");
113
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));
118         }
119
120         talloc_free(mem_ctx);
121
122         return true;
123 }
124
125 /* Testing ber_read_OID_String() function */
126 static bool test_ber_read_OID_String(struct torture_context *tctx)
127 {
128         int i;
129         const char *oid;
130         DATA_BLOB oid_blob;
131         TALLOC_CTX *mem_ctx;
132         const struct oid_data *data = oid_data_ok;
133
134         mem_ctx = talloc_new(tctx);
135
136         for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
137                 oid_blob = strhex_to_data_blob(mem_ctx, data[i].bin_oid);
138
139                 torture_assert(tctx, ber_read_OID_String(mem_ctx, oid_blob, &oid),
140                                 "ber_read_OID_String failed");
141
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));
146         }
147
148         talloc_free(mem_ctx);
149
150         return true;
151 }
152
153 /* Testing ber_write_partial_OID_String() function */
154 static bool test_ber_write_partial_OID_String(struct torture_context *tctx)
155 {
156         int i;
157         char *hex_str;
158         DATA_BLOB blob;
159         TALLOC_CTX *mem_ctx;
160         const struct oid_data *data = oid_data_ok;
161
162         mem_ctx = talloc_new(tctx);
163
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");
168
169                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
170                 torture_assert(tctx, hex_str, "No memory!");
171
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));
176         }
177
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");
183
184                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
185                 torture_assert(tctx, hex_str, "No memory!");
186
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));
191         }
192
193         talloc_free(mem_ctx);
194
195         return true;
196 }
197
198 /* Testing ber_read_partial_OID_String() function */
199 static bool test_ber_read_partial_OID_String(struct torture_context *tctx)
200 {
201         int i;
202         const char *oid;
203         DATA_BLOB oid_blob;
204         TALLOC_CTX *mem_ctx;
205         const struct oid_data *data = oid_data_ok;
206
207         mem_ctx = talloc_new(tctx);
208
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);
212
213                 torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
214                                 "ber_read_partial_OID_String failed");
215
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));
220         }
221
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);
226
227                 torture_assert(tctx, ber_read_partial_OID_String(mem_ctx, oid_blob, &oid),
228                                 "ber_read_partial_OID_String failed");
229
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));
234         }
235
236         talloc_free(mem_ctx);
237
238         return true;
239 }
240
241
242 /* LOCAL-ASN1 test suite creation */
243 struct torture_suite *torture_local_util_asn1(TALLOC_CTX *mem_ctx)
244 {
245         struct torture_suite *suite = torture_suite_create(mem_ctx, "ASN1");
246
247         torture_suite_add_simple_test(suite, "ber_write_OID_String",
248                                       test_ber_write_OID_String);
249
250         torture_suite_add_simple_test(suite, "ber_read_OID_String",
251                                       test_ber_read_OID_String);
252
253         torture_suite_add_simple_test(suite, "ber_write_partial_OID_String",
254                                       test_ber_write_partial_OID_String);
255
256         torture_suite_add_simple_test(suite, "ber_read_partial_OID_String",
257                                       test_ber_read_partial_OID_String);
258
259         return suite;
260 }