Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : *
4 : * Copyright (C) 2019 Michael Hanselmann <public@hansmi.ch>
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <stdarg.h>
21 : #include <stddef.h>
22 : #include <stdint.h>
23 : #include <setjmp.h>
24 : #include <cmocka.h>
25 :
26 : #include "includes.h"
27 : #include "./lib.h"
28 :
29 : struct test_ctx {
30 : };
31 :
32 7 : static int setup_context(void **state)
33 : {
34 7 : struct test_ctx *test_ctx;
35 :
36 7 : test_ctx = talloc_zero(NULL, struct test_ctx);
37 7 : assert_non_null(test_ctx);
38 :
39 7 : *state = test_ctx;
40 :
41 7 : return 0;
42 : }
43 :
44 7 : static int teardown_context(void **state)
45 : {
46 7 : struct test_ctx *test_ctx =
47 7 : talloc_get_type_abort(*state, struct test_ctx);
48 :
49 7 : talloc_free(test_ctx);
50 :
51 7 : return 0;
52 : }
53 :
54 7 : static struct schema_conv process_data_blob(void **state, DATA_BLOB input)
55 : {
56 7 : struct test_ctx *test_ctx =
57 7 : talloc_get_type_abort(*state, struct test_ctx);
58 7 : struct conv_options opt;
59 7 : struct schema_conv ret;
60 :
61 7 : assert_non_null(test_ctx);
62 7 : assert_non_null(input.data);
63 :
64 7 : opt.in = fmemopen(input.data, input.length, "r");
65 7 : opt.out = fopen("/dev/null", "w");
66 7 : opt.ldb_ctx = ldb_init(test_ctx, NULL);
67 :
68 7 : assert_non_null(opt.in);
69 7 : assert_non_null(opt.out);
70 7 : assert_non_null(opt.ldb_ctx);
71 :
72 7 : opt.basedn = ldb_dn_new(test_ctx, opt.ldb_ctx, "");
73 :
74 7 : assert_non_null(opt.basedn);
75 :
76 7 : ret = process_file(test_ctx, &opt);
77 :
78 7 : fclose(opt.in);
79 7 : fclose(opt.out);
80 :
81 7 : return ret;
82 : }
83 :
84 1 : static void test_unknown_syntax_oid(void **state)
85 : {
86 1 : struct schema_conv ret;
87 :
88 1 : ret = process_data_blob(state, data_blob_string_const(
89 : "attributetype ( 999.555.999.555.999\n"
90 : "NAME 'mailLocalAddress'\n"
91 : "DESC 'RFC822 email address of this recipient'\n"
92 : "EQUALITY caseIgnoreIA5Match\n"
93 : "SYNTAX 999.555.999.555.999{256} )\n"
94 : ));
95 :
96 1 : assert_int_equal(ret.count, 1);
97 1 : assert_int_equal(ret.failures, 1);
98 1 : }
99 :
100 1 : static void test_unterminated_token_value(void **state)
101 : {
102 1 : struct schema_conv ret;
103 :
104 1 : ret = process_data_blob(state, data_blob_string_const(
105 : "attributetype ( 2.16.840.1.113730.3.1.47\n"
106 : "\tNAME 'mailRoutingAX 1.3.6.1.4.1.1466.115.121.1.26{256}\n"
107 : "\tSI GLE-VALUE )\n"
108 : ));
109 :
110 1 : assert_int_equal(ret.count, 1);
111 1 : assert_int_equal(ret.failures, 1);
112 1 : }
113 :
114 1 : static void test_unterminated_must_value(void **state)
115 : {
116 1 : struct schema_conv ret;
117 :
118 1 : ret = process_data_blob(state, data_blob_string_const(
119 : "attributetype ( 1\n"
120 : "\tSYNTAX 1./)# MUST ( foobar $\n"
121 : ));
122 :
123 1 : assert_int_equal(ret.count, 1);
124 1 : assert_int_equal(ret.failures, 1);
125 1 : }
126 :
127 1 : static void test_unterminated_may_value(void **state)
128 : {
129 1 : struct schema_conv ret;
130 :
131 1 : ret = process_data_blob(state, data_blob_string_const(
132 : "attributetype ( 1\n"
133 : "\tSYNTAX 1.3.6.1.4.1.1466.115.121.1./)# MAY ( javaClassNames $\n"
134 : ));
135 :
136 1 : assert_int_equal(ret.count, 1);
137 1 : assert_int_equal(ret.failures, 1);
138 1 : }
139 :
140 1 : static void test_unterminated_sup_value(void **state)
141 : {
142 1 : struct schema_conv ret;
143 :
144 1 : ret = process_data_blob(state, data_blob_string_const(
145 : "attributetype ( 1\n"
146 : "\tSYNTAX 1./)# SUP ( foobar $\n"
147 : ));
148 :
149 1 : assert_int_equal(ret.count, 1);
150 1 : assert_int_equal(ret.failures, 1);
151 1 : }
152 :
153 1 : static void test_unknown_token(void **state)
154 : {
155 1 : struct schema_conv ret;
156 :
157 1 : ret = process_data_blob(state, data_blob_string_const(
158 : "attributetype ( 1\n"
159 : "\tFOOBAR 123\n"
160 : " )\n"
161 : ));
162 :
163 1 : assert_int_equal(ret.count, 1);
164 1 : assert_int_equal(ret.failures, 1);
165 1 : }
166 :
167 1 : static void test_missing_name(void **state)
168 : {
169 1 : struct schema_conv ret;
170 :
171 1 : ret = process_data_blob(state, data_blob_string_const(
172 : "objectclass ( 1.3.6.3.6.1.4.1.1466.115.121.1.26{256} )"
173 : ));
174 :
175 1 : assert_int_equal(ret.count, 1);
176 1 : assert_int_equal(ret.failures, 1);
177 1 : }
178 :
179 1 : int main(void) {
180 1 : const struct CMUnitTest tests[] = {
181 : cmocka_unit_test_setup_teardown(test_unknown_syntax_oid,
182 : setup_context,
183 : teardown_context),
184 : cmocka_unit_test_setup_teardown(test_unterminated_token_value,
185 : setup_context,
186 : teardown_context),
187 : cmocka_unit_test_setup_teardown(test_unterminated_must_value,
188 : setup_context,
189 : teardown_context),
190 : cmocka_unit_test_setup_teardown(test_unterminated_may_value,
191 : setup_context,
192 : teardown_context),
193 : cmocka_unit_test_setup_teardown(test_unterminated_sup_value,
194 : setup_context,
195 : teardown_context),
196 : cmocka_unit_test_setup_teardown(test_unknown_token,
197 : setup_context,
198 : teardown_context),
199 : cmocka_unit_test_setup_teardown(test_missing_name,
200 : setup_context,
201 : teardown_context),
202 : };
203 :
204 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
205 :
206 1 : return cmocka_run_group_tests(tests, NULL, NULL);
207 : }
|