s4/asn1: Added test for ber_write_partial_OID_String()
[ira/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 successfull OIDs conversions */
32 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 successfull Partial OIDs conversions */
68 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         struct oid_data *data = oid_data_ok;
104
105         mem_ctx = talloc_new(NULL);
106
107         for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
108                 torture_assert(tctx, ber_write_OID_String(&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_write_partial_OID_String() function */
126 static bool test_ber_write_partial_OID_String(struct torture_context *tctx)
127 {
128         int i;
129         char *hex_str;
130         DATA_BLOB blob;
131         TALLOC_CTX *mem_ctx;
132         struct oid_data *data = oid_data_ok;
133
134         mem_ctx = talloc_new(NULL);
135
136         /* ber_write_partial_OID_String() should work with not partial OIDs also */
137         for (i = 0; i < ARRAY_SIZE(oid_data_ok); i++) {
138                 torture_assert(tctx, ber_write_partial_OID_String(&blob, data[i].oid),
139                                 "ber_write_OID_String failed");
140
141                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
142                 torture_assert(tctx, hex_str, "No memory!");
143
144                 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
145                                 talloc_asprintf(mem_ctx,
146                                                 "Failed: oid=%s, bin_oid:%s",
147                                                 data[i].oid, data[i].bin_oid));
148         }
149
150         /* ber_write_partial_OID_String() test with partial OIDs */
151         data = partial_oid_data_ok;
152         for (i = 0; i < ARRAY_SIZE(partial_oid_data_ok); i++) {
153                 torture_assert(tctx, ber_write_partial_OID_String(&blob, data[i].oid),
154                                 "ber_write_OID_String failed");
155
156                 hex_str = hex_encode_talloc(mem_ctx, blob.data, blob.length);
157                 torture_assert(tctx, hex_str, "No memory!");
158
159                 torture_assert(tctx, strequal(data[i].bin_oid, hex_str),
160                                 talloc_asprintf(mem_ctx,
161                                                 "Failed: oid=%s, bin_oid:%s",
162                                                 data[i].oid, data[i].bin_oid));
163         }
164
165         talloc_free(mem_ctx);
166
167         return true;
168 }
169
170
171 /* LOCAL-ASN1 test suite creation */
172 struct torture_suite *torture_local_util_asn1(TALLOC_CTX *mem_ctx)
173 {
174         struct torture_suite *suite = torture_suite_create(mem_ctx, "ASN1");
175
176         torture_suite_add_simple_test(suite, "ber_write_OID_String",
177                                       test_ber_write_OID_String);
178
179         torture_suite_add_simple_test(suite, "ber_write_partial_OID_String",
180                                       test_ber_write_partial_OID_String);
181
182         return suite;
183 }