s3:tldap: simplify read_ldap_more() by using asn1_peek_full_tag()
[samba.git] / source3 / lib / test_adouble.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * Copyright (C) 2021 Ralph Boehme <slow@samba.org>
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 "adouble.c"
21 #include <cmocka.h>
22
23 static int setup_talloc_context(void **state)
24 {
25         TALLOC_CTX *frame = talloc_stackframe();
26
27         *state = frame;
28         return 0;
29 }
30
31 static int teardown_talloc_context(void **state)
32 {
33         TALLOC_CTX *frame = *state;
34
35         TALLOC_FREE(frame);
36         return 0;
37 }
38
39 /*
40  * Basic and sane buffer.
41  */
42 static uint8_t ad_basic[] = {
43         0x00, 0x05, 0x16, 0x07, /* Magic */
44         0x00, 0x02, 0x00, 0x00, /* Version */
45         0x00, 0x00, 0x00, 0x00, /* Filler */
46         0x00, 0x00, 0x00, 0x00, /* Filler */
47         0x00, 0x00, 0x00, 0x00, /* Filler */
48         0x00, 0x00, 0x00, 0x00, /* Filler */
49         0x00, 0x02,             /* Count */
50         /* adentry 1: FinderInfo */
51         0x00, 0x00, 0x00, 0x09, /* eid: FinderInfo */
52         0x00, 0x00, 0x00, 0x32, /* offset */
53         0x00, 0x00, 0x00, 0x20, /* length */
54         /* adentry 2: Resourcefork */
55         0x00, 0x00, 0x00, 0x02, /* eid: Resourcefork */
56         0x00, 0x00, 0x00, 0x52, /* offset */
57         0xff, 0xff, 0xff, 0x00, /* length */
58         /* FinderInfo data: 32 bytes */
59         0x00, 0x00, 0x00, 0x00,
60         0x00, 0x00, 0x00, 0x00,
61         0x00, 0x00, 0x00, 0x00,
62         0x00, 0x00, 0x00, 0x00,
63         0x00, 0x00, 0x00, 0x00,
64         0x00, 0x00, 0x00, 0x00,
65         0x00, 0x00, 0x00, 0x00,
66         0x00, 0x00, 0x00, 0x00,
67 };
68
69 /*
70  * An empty FinderInfo entry.
71  */
72 static uint8_t ad_finderinfo1[] = {
73         0x00, 0x05, 0x16, 0x07, /* Magic */
74         0x00, 0x02, 0x00, 0x00, /* Version */
75         0x00, 0x00, 0x00, 0x00, /* Filler */
76         0x00, 0x00, 0x00, 0x00, /* Filler */
77         0x00, 0x00, 0x00, 0x00, /* Filler */
78         0x00, 0x00, 0x00, 0x00, /* Filler */
79         0x00, 0x02,             /* Count */
80         /* adentry 1: FinderInfo */
81         0x00, 0x00, 0x00, 0x09, /* eid: FinderInfo */
82         0x00, 0x00, 0x00, 0x52, /* off: points at end of buffer */
83         0x00, 0x00, 0x00, 0x00, /* len: 0, so off+len don't exceed bufferlen */
84         /* adentry 2: Resourcefork */
85         0x00, 0x00, 0x00, 0x02, /* eid: Resourcefork */
86         0x00, 0x00, 0x00, 0x52, /* offset */
87         0xff, 0xff, 0xff, 0x00, /* length */
88         /* FinderInfo data: 32 bytes */
89         0x00, 0x00, 0x00, 0x00,
90         0x00, 0x00, 0x00, 0x00,
91         0x00, 0x00, 0x00, 0x00,
92         0x00, 0x00, 0x00, 0x00,
93         0x00, 0x00, 0x00, 0x00,
94         0x00, 0x00, 0x00, 0x00,
95         0x00, 0x00, 0x00, 0x00,
96         0x00, 0x00, 0x00, 0x00,
97 };
98
99 /*
100  * A dangerous FinderInfo with correct length exceeding buffer by one byte.
101  */
102 static uint8_t ad_finderinfo2[] = {
103         0x00, 0x05, 0x16, 0x07, /* Magic */
104         0x00, 0x02, 0x00, 0x00, /* Version */
105         0x00, 0x00, 0x00, 0x00, /* Filler */
106         0x00, 0x00, 0x00, 0x00, /* Filler */
107         0x00, 0x00, 0x00, 0x00, /* Filler */
108         0x00, 0x00, 0x00, 0x00, /* Filler */
109         0x00, 0x02,             /* Count */
110         /* adentry 1: FinderInfo */
111         0x00, 0x00, 0x00, 0x09, /* eid: FinderInfo */
112         0x00, 0x00, 0x00, 0x33, /* off: points at beginng of data + 1 */
113         0x00, 0x00, 0x00, 0x20, /* len: 32, so off+len exceeds bufferlen by 1 */
114         /* adentry 2: Resourcefork */
115         0x00, 0x00, 0x00, 0x02, /* eid: Resourcefork */
116         0x00, 0x00, 0x00, 0x52, /* offset */
117         0xff, 0xff, 0xff, 0x00, /* length */
118         /* FinderInfo data: 32 bytes */
119         0x00, 0x00, 0x00, 0x00,
120         0x00, 0x00, 0x00, 0x00,
121         0x00, 0x00, 0x00, 0x00,
122         0x00, 0x00, 0x00, 0x00,
123         0x00, 0x00, 0x00, 0x00,
124         0x00, 0x00, 0x00, 0x00,
125         0x00, 0x00, 0x00, 0x00,
126         0x00, 0x00, 0x00, 0x00,
127 };
128
129 static uint8_t ad_finderinfo3[] = {
130         0x00, 0x05, 0x16, 0x07, /* Magic */
131         0x00, 0x02, 0x00, 0x00, /* Version */
132         0x00, 0x00, 0x00, 0x00, /* Filler */
133         0x00, 0x00, 0x00, 0x00, /* Filler */
134         0x00, 0x00, 0x00, 0x00, /* Filler */
135         0x00, 0x00, 0x00, 0x00, /* Filler */
136         0x00, 0x02,             /* Count */
137         /* adentry 1: FinderInfo */
138         0x00, 0x00, 0x00, 0x09, /* eid: FinderInfo */
139         0x00, 0x00, 0x00, 0x33, /* off: points at beginng of data + 1 */
140         0x00, 0x00, 0x00, 0x1f, /* len: 31, so off+len don't exceed buf */
141         /* adentry 2: Resourcefork */
142         0x00, 0x00, 0x00, 0x02, /* eid: Resourcefork */
143         0x00, 0x00, 0x00, 0x52, /* offset */
144         0xff, 0xff, 0xff, 0x00, /* length */
145         /* FinderInfo data: 32 bytes */
146         0x00, 0x00, 0x00, 0x00,
147         0x00, 0x00, 0x00, 0x00,
148         0x00, 0x00, 0x00, 0x00,
149         0x00, 0x00, 0x00, 0x00,
150         0x00, 0x00, 0x00, 0x00,
151         0x00, 0x00, 0x00, 0x00,
152         0x00, 0x00, 0x00, 0x00,
153         0x00, 0x00, 0x00, 0x00,
154 };
155
156 /*
157  * A dangerous name entry.
158  */
159 static uint8_t ad_name[] = {
160         0x00, 0x05, 0x16, 0x07, /* Magic */
161         0x00, 0x02, 0x00, 0x00, /* Version */
162         0x00, 0x00, 0x00, 0x00, /* Filler */
163         0x00, 0x00, 0x00, 0x00, /* Filler */
164         0x00, 0x00, 0x00, 0x00, /* Filler */
165         0x00, 0x00, 0x00, 0x00, /* Filler */
166         0x00, 0x02,             /* Count */
167         /* adentry 1: FinderInfo */
168         0x00, 0x00, 0x00, 0x09, /* eid: FinderInfo */
169         0x00, 0x00, 0x00, 0x32, /* offset */
170         0x00, 0x00, 0x00, 0x20, /* length */
171         /* adentry 2: Name */
172         0x00, 0x00, 0x00, 0x03, /* eid: Name */
173         0x00, 0x00, 0x00, 0x52, /* off: points at end of buffer */
174         0x00, 0x00, 0x00, 0x01, /* len: 1, so off+len exceeds bufferlen */
175         /* FinderInfo data: 32 bytes */
176         0x00, 0x00, 0x00, 0x00,
177         0x00, 0x00, 0x00, 0x00,
178         0x00, 0x00, 0x00, 0x00,
179         0x00, 0x00, 0x00, 0x00,
180         0x00, 0x00, 0x00, 0x00,
181         0x00, 0x00, 0x00, 0x00,
182         0x00, 0x00, 0x00, 0x00,
183         0x00, 0x00, 0x00, 0x00,
184 };
185
186 /*
187  * A empty ADEID_FILEDATESI entry.
188  */
189 static uint8_t ad_date1[] = {
190         0x00, 0x05, 0x16, 0x07, /* Magic */
191         0x00, 0x02, 0x00, 0x00, /* Version */
192         0x00, 0x00, 0x00, 0x00, /* Filler */
193         0x00, 0x00, 0x00, 0x00, /* Filler */
194         0x00, 0x00, 0x00, 0x00, /* Filler */
195         0x00, 0x00, 0x00, 0x00, /* Filler */
196         0x00, 0x02,             /* Count */
197         /* adentry 1: FinderInfo */
198         0x00, 0x00, 0x00, 0x09, /* eid: FinderInfo */
199         0x00, 0x00, 0x00, 0x32, /* offset */
200         0x00, 0x00, 0x00, 0x20, /* length */
201         /* adentry 2: Dates */
202         0x00, 0x00, 0x00, 0x08, /* eid: dates */
203         0x00, 0x00, 0x00, 0x52, /* off: end of buffer */
204         0x00, 0x00, 0x00, 0x00, /* len: 0, empty entry, valid */
205         /* FinderInfo data: 32 bytes */
206         0x00, 0x00, 0x00, 0x00,
207         0x00, 0x00, 0x00, 0x00,
208         0x00, 0x00, 0x00, 0x00,
209         0x00, 0x00, 0x00, 0x00,
210         0x00, 0x00, 0x00, 0x00,
211         0x00, 0x00, 0x00, 0x00,
212         0x00, 0x00, 0x00, 0x00,
213         0x00, 0x00, 0x00, 0x00,
214 };
215
216 /*
217  * A dangerous ADEID_FILEDATESI entry, invalid length.
218  */
219 static uint8_t ad_date2[] = {
220         0x00, 0x05, 0x16, 0x07, /* Magic */
221         0x00, 0x02, 0x00, 0x00, /* Version */
222         0x00, 0x00, 0x00, 0x00, /* Filler */
223         0x00, 0x00, 0x00, 0x00, /* Filler */
224         0x00, 0x00, 0x00, 0x00, /* Filler */
225         0x00, 0x00, 0x00, 0x00, /* Filler */
226         0x00, 0x02,             /* Count */
227         /* adentry 1: FinderInfo */
228         0x00, 0x00, 0x00, 0x09, /* eid: FinderInfo */
229         0x00, 0x00, 0x00, 0x32, /* offset */
230         0x00, 0x00, 0x00, 0x20, /* length */
231         /* adentry 2: Dates */
232         0x00, 0x00, 0x00, 0x08, /* eid: dates */
233         0x00, 0x00, 0x00, 0x43, /* off: FinderInfo buf but one byte short */
234         0x00, 0x00, 0x00, 0x0f, /* len: 15, so off+len don't exceed bufferlen */
235         /* FinderInfo data: 32 bytes */
236         0x00, 0x00, 0x00, 0x00,
237         0x00, 0x00, 0x00, 0x00,
238         0x00, 0x00, 0x00, 0x00,
239         0x00, 0x00, 0x00, 0x00,
240         0x00, 0x00, 0x00, 0x00,
241         0x00, 0x00, 0x00, 0x00,
242         0x00, 0x00, 0x00, 0x00,
243         0x00, 0x00, 0x00, 0x00,
244 };
245
246 static struct adouble *parse_adouble(TALLOC_CTX *mem_ctx,
247                                      uint8_t *adbuf,
248                                      size_t adsize,
249                                      off_t filesize)
250 {
251         struct adouble *ad = NULL;
252         bool ok;
253
254         ad = talloc_zero(mem_ctx, struct adouble);
255         ad->ad_data = talloc_zero_size(ad, adsize);
256         assert_non_null(ad);
257
258         memcpy(ad->ad_data, adbuf, adsize);
259
260         ok = ad_unpack(ad, 2, filesize);
261         if (!ok) {
262                 return NULL;
263         }
264
265         return ad;
266 }
267
268 static void parse_abouble_basic(void **state)
269 {
270         TALLOC_CTX *frame = *state;
271         struct adouble *ad = NULL;
272         char *p = NULL;
273
274         ad = parse_adouble(frame, ad_basic, sizeof(ad_basic), 0xffffff52);
275         assert_non_null(ad);
276
277         p = ad_get_entry(ad, ADEID_FINDERI);
278         assert_non_null(p);
279
280         return;
281 }
282
283 static void parse_abouble_finderinfo1(void **state)
284 {
285         TALLOC_CTX *frame = *state;
286         struct adouble *ad = NULL;
287         char *p = NULL;
288
289         ad = parse_adouble(frame,
290                            ad_finderinfo1,
291                            sizeof(ad_finderinfo1),
292                            0xffffff52);
293         assert_non_null(ad);
294
295         p = ad_get_entry(ad, ADEID_FINDERI);
296         assert_null(p);
297
298         return;
299 }
300
301 static void parse_abouble_finderinfo2(void **state)
302 {
303         TALLOC_CTX *frame = *state;
304         struct adouble *ad = NULL;
305
306         ad = parse_adouble(frame,
307                            ad_finderinfo2,
308                            sizeof(ad_finderinfo2),
309                            0xffffff52);
310         assert_null(ad);
311
312         return;
313 }
314
315 static void parse_abouble_finderinfo3(void **state)
316 {
317         TALLOC_CTX *frame = *state;
318         struct adouble *ad = NULL;
319
320         ad = parse_adouble(frame,
321                            ad_finderinfo3,
322                            sizeof(ad_finderinfo3),
323                            0xffffff52);
324         assert_null(ad);
325
326         return;
327 }
328
329 static void parse_abouble_name(void **state)
330 {
331         TALLOC_CTX *frame = *state;
332         struct adouble *ad = NULL;
333
334         ad = parse_adouble(frame, ad_name, sizeof(ad_name), 0x52);
335         assert_null(ad);
336
337         return;
338 }
339
340 static void parse_abouble_date1(void **state)
341 {
342         TALLOC_CTX *frame = *state;
343         struct adouble *ad = NULL;
344         char *p = NULL;
345
346         ad = parse_adouble(frame, ad_date1, sizeof(ad_date1), 0x52);
347         assert_non_null(ad);
348
349         p = ad_get_entry(ad, ADEID_FILEDATESI);
350         assert_null(p);
351
352         return;
353 }
354
355 static void parse_abouble_date2(void **state)
356 {
357         TALLOC_CTX *frame = *state;
358         struct adouble *ad = NULL;
359
360         ad = parse_adouble(frame, ad_date2, sizeof(ad_date2), 0x52);
361         assert_null(ad);
362
363         return;
364 }
365
366 int main(int argc, char *argv[])
367 {
368         int rc;
369         const struct CMUnitTest tests[] = {
370                 cmocka_unit_test(parse_abouble_basic),
371                 cmocka_unit_test(parse_abouble_finderinfo1),
372                 cmocka_unit_test(parse_abouble_finderinfo2),
373                 cmocka_unit_test(parse_abouble_finderinfo3),
374                 cmocka_unit_test(parse_abouble_name),
375                 cmocka_unit_test(parse_abouble_date1),
376                 cmocka_unit_test(parse_abouble_date2),
377         };
378
379         if (argc == 2) {
380                 cmocka_set_test_filter(argv[1]);
381         }
382         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
383
384         rc = cmocka_run_group_tests(tests,
385                                     setup_talloc_context,
386                                     teardown_talloc_context);
387
388         return rc;
389 }