tests: Check symlinks are readable as reparse points
[samba4-gss.git] / source3 / lib / test_adouble.c
blob615c22469c916253376661d5557d03dc26f8c43d
1 /*
2 * Unix SMB/CIFS implementation.
4 * Copyright (C) 2021 Ralph Boehme <slow@samba.org>
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.
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.
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/>.
20 #include "adouble.c"
21 #include <cmocka.h>
23 static int setup_talloc_context(void **state)
25 TALLOC_CTX *frame = talloc_stackframe();
27 *state = frame;
28 return 0;
31 static int teardown_talloc_context(void **state)
33 TALLOC_CTX *frame = *state;
35 TALLOC_FREE(frame);
36 return 0;
40 * Basic and sane buffer.
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,
70 * An empty FinderInfo entry.
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,
100 * A dangerous FinderInfo with correct length exceeding buffer by one byte.
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,
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,
157 * A dangerous name entry.
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,
187 * A empty ADEID_FILEDATESI entry.
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,
217 * A dangerous ADEID_FILEDATESI entry, invalid length.
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,
246 static struct adouble *parse_adouble(TALLOC_CTX *mem_ctx,
247 uint8_t *adbuf,
248 size_t adsize,
249 off_t filesize)
251 struct adouble *ad = NULL;
252 bool ok;
254 ad = talloc_zero(mem_ctx, struct adouble);
255 ad->ad_data = talloc_zero_size(ad, adsize);
256 assert_non_null(ad);
258 memcpy(ad->ad_data, adbuf, adsize);
260 ok = ad_unpack(ad, 2, filesize);
261 if (!ok) {
262 return NULL;
265 return ad;
268 static void parse_abouble_basic(void **state)
270 TALLOC_CTX *frame = *state;
271 struct adouble *ad = NULL;
272 char *p = NULL;
274 ad = parse_adouble(frame, ad_basic, sizeof(ad_basic), 0xffffff52);
275 assert_non_null(ad);
277 p = ad_get_entry(ad, ADEID_FINDERI);
278 assert_non_null(p);
280 return;
283 static void parse_abouble_finderinfo1(void **state)
285 TALLOC_CTX *frame = *state;
286 struct adouble *ad = NULL;
287 char *p = NULL;
289 ad = parse_adouble(frame,
290 ad_finderinfo1,
291 sizeof(ad_finderinfo1),
292 0xffffff52);
293 assert_non_null(ad);
295 p = ad_get_entry(ad, ADEID_FINDERI);
296 assert_null(p);
298 return;
301 static void parse_abouble_finderinfo2(void **state)
303 TALLOC_CTX *frame = *state;
304 struct adouble *ad = NULL;
306 ad = parse_adouble(frame,
307 ad_finderinfo2,
308 sizeof(ad_finderinfo2),
309 0xffffff52);
310 assert_null(ad);
312 return;
315 static void parse_abouble_finderinfo3(void **state)
317 TALLOC_CTX *frame = *state;
318 struct adouble *ad = NULL;
320 ad = parse_adouble(frame,
321 ad_finderinfo3,
322 sizeof(ad_finderinfo3),
323 0xffffff52);
324 assert_null(ad);
326 return;
329 static void parse_abouble_name(void **state)
331 TALLOC_CTX *frame = *state;
332 struct adouble *ad = NULL;
334 ad = parse_adouble(frame, ad_name, sizeof(ad_name), 0x52);
335 assert_null(ad);
337 return;
340 static void parse_abouble_date1(void **state)
342 TALLOC_CTX *frame = *state;
343 struct adouble *ad = NULL;
344 char *p = NULL;
346 ad = parse_adouble(frame, ad_date1, sizeof(ad_date1), 0x52);
347 assert_non_null(ad);
349 p = ad_get_entry(ad, ADEID_FILEDATESI);
350 assert_null(p);
352 return;
355 static void parse_abouble_date2(void **state)
357 TALLOC_CTX *frame = *state;
358 struct adouble *ad = NULL;
360 ad = parse_adouble(frame, ad_date2, sizeof(ad_date2), 0x52);
361 assert_null(ad);
363 return;
366 int main(int argc, char *argv[])
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),
379 if (argc == 2) {
380 cmocka_set_test_filter(argv[1]);
382 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
384 rc = cmocka_run_group_tests(tests,
385 setup_talloc_context,
386 teardown_talloc_context);
388 return rc;