1 /*
2 * Library directory_record type test program
3 *
4 * Copyright (C) 2018-2021, Joachim Metz <joachim.metz@gmail.com>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser 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 Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <file_stream.h>
24 #include <types.h>
25
26 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
27 #include <stdlib.h>
28 #endif
29
30 #include "fsapfs_test_libcerror.h"
31 #include "fsapfs_test_libfsapfs.h"
32 #include "fsapfs_test_macros.h"
33 #include "fsapfs_test_memory.h"
34 #include "fsapfs_test_unused.h"
35
36 #include "../libfsapfs/libfsapfs_directory_record.h"
37
38 uint8_t fsapfs_test_directory_record_key_data1[ 23 ] = {
39 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x0b, 0x14, 0xbe, 0x9c, 0x2e, 0x66, 0x73, 0x65,
40 0x76, 0x65, 0x6e, 0x74, 0x73, 0x64, 0x00 };
41
42 uint8_t fsapfs_test_directory_record_value_data1[ 18 ] = {
43 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0xe3, 0x85, 0x4a, 0x54, 0x55, 0x52, 0x15,
44 0x04, 0x00 };
45
46 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
47
48 /* Tests the libfsapfs_directory_record_initialize function
49 * Returns 1 if successful or 0 if not
50 */
fsapfs_test_directory_record_initialize(void)51 int fsapfs_test_directory_record_initialize(
52 void )
53 {
54 libcerror_error_t *error = NULL;
55 libfsapfs_directory_record_t *directory_record = NULL;
56 int result = 0;
57
58 #if defined( HAVE_FSAPFS_TEST_MEMORY )
59 int number_of_malloc_fail_tests = 1;
60 int number_of_memset_fail_tests = 1;
61 int test_number = 0;
62 #endif
63
64 /* Test regular cases
65 */
66 result = libfsapfs_directory_record_initialize(
67 &directory_record,
68 &error );
69
70 FSAPFS_TEST_ASSERT_EQUAL_INT(
71 "result",
72 result,
73 1 );
74
75 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
76 "directory_record",
77 directory_record );
78
79 FSAPFS_TEST_ASSERT_IS_NULL(
80 "error",
81 error );
82
83 result = libfsapfs_directory_record_free(
84 &directory_record,
85 &error );
86
87 FSAPFS_TEST_ASSERT_EQUAL_INT(
88 "result",
89 result,
90 1 );
91
92 FSAPFS_TEST_ASSERT_IS_NULL(
93 "directory_record",
94 directory_record );
95
96 FSAPFS_TEST_ASSERT_IS_NULL(
97 "error",
98 error );
99
100 /* Test error cases
101 */
102 result = libfsapfs_directory_record_initialize(
103 NULL,
104 &error );
105
106 FSAPFS_TEST_ASSERT_EQUAL_INT(
107 "result",
108 result,
109 -1 );
110
111 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
112 "error",
113 error );
114
115 libcerror_error_free(
116 &error );
117
118 directory_record = (libfsapfs_directory_record_t *) 0x12345678UL;
119
120 result = libfsapfs_directory_record_initialize(
121 &directory_record,
122 &error );
123
124 directory_record = NULL;
125
126 FSAPFS_TEST_ASSERT_EQUAL_INT(
127 "result",
128 result,
129 -1 );
130
131 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
132 "error",
133 error );
134
135 libcerror_error_free(
136 &error );
137
138 #if defined( HAVE_FSAPFS_TEST_MEMORY )
139
140 for( test_number = 0;
141 test_number < number_of_malloc_fail_tests;
142 test_number++ )
143 {
144 /* Test libfsapfs_directory_record_initialize with malloc failing
145 */
146 fsapfs_test_malloc_attempts_before_fail = test_number;
147
148 result = libfsapfs_directory_record_initialize(
149 &directory_record,
150 &error );
151
152 if( fsapfs_test_malloc_attempts_before_fail != -1 )
153 {
154 fsapfs_test_malloc_attempts_before_fail = -1;
155
156 if( directory_record != NULL )
157 {
158 libfsapfs_directory_record_free(
159 &directory_record,
160 NULL );
161 }
162 }
163 else
164 {
165 FSAPFS_TEST_ASSERT_EQUAL_INT(
166 "result",
167 result,
168 -1 );
169
170 FSAPFS_TEST_ASSERT_IS_NULL(
171 "directory_record",
172 directory_record );
173
174 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
175 "error",
176 error );
177
178 libcerror_error_free(
179 &error );
180 }
181 }
182 for( test_number = 0;
183 test_number < number_of_memset_fail_tests;
184 test_number++ )
185 {
186 /* Test libfsapfs_directory_record_initialize with memset failing
187 */
188 fsapfs_test_memset_attempts_before_fail = test_number;
189
190 result = libfsapfs_directory_record_initialize(
191 &directory_record,
192 &error );
193
194 if( fsapfs_test_memset_attempts_before_fail != -1 )
195 {
196 fsapfs_test_memset_attempts_before_fail = -1;
197
198 if( directory_record != NULL )
199 {
200 libfsapfs_directory_record_free(
201 &directory_record,
202 NULL );
203 }
204 }
205 else
206 {
207 FSAPFS_TEST_ASSERT_EQUAL_INT(
208 "result",
209 result,
210 -1 );
211
212 FSAPFS_TEST_ASSERT_IS_NULL(
213 "directory_record",
214 directory_record );
215
216 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
217 "error",
218 error );
219
220 libcerror_error_free(
221 &error );
222 }
223 }
224 #endif /* defined( HAVE_FSAPFS_TEST_MEMORY ) */
225
226 return( 1 );
227
228 on_error:
229 if( error != NULL )
230 {
231 libcerror_error_free(
232 &error );
233 }
234 if( directory_record != NULL )
235 {
236 libfsapfs_directory_record_free(
237 &directory_record,
238 NULL );
239 }
240 return( 0 );
241 }
242
243 /* Tests the libfsapfs_directory_record_free function
244 * Returns 1 if successful or 0 if not
245 */
fsapfs_test_directory_record_free(void)246 int fsapfs_test_directory_record_free(
247 void )
248 {
249 libcerror_error_t *error = NULL;
250 int result = 0;
251
252 /* Test error cases
253 */
254 result = libfsapfs_directory_record_free(
255 NULL,
256 &error );
257
258 FSAPFS_TEST_ASSERT_EQUAL_INT(
259 "result",
260 result,
261 -1 );
262
263 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
264 "error",
265 error );
266
267 libcerror_error_free(
268 &error );
269
270 return( 1 );
271
272 on_error:
273 if( error != NULL )
274 {
275 libcerror_error_free(
276 &error );
277 }
278 return( 0 );
279 }
280
281 /* Tests the libfsapfs_directory_record_read_key_data function
282 * Returns 1 if successful or 0 if not
283 */
fsapfs_test_directory_record_read_key_data(void)284 int fsapfs_test_directory_record_read_key_data(
285 void )
286 {
287 libcerror_error_t *error = NULL;
288 libfsapfs_directory_record_t *directory_record = NULL;
289 int result = 0;
290
291 /* Initialize test
292 */
293 result = libfsapfs_directory_record_initialize(
294 &directory_record,
295 &error );
296
297 FSAPFS_TEST_ASSERT_EQUAL_INT(
298 "result",
299 result,
300 1 );
301
302 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
303 "directory_record",
304 directory_record );
305
306 FSAPFS_TEST_ASSERT_IS_NULL(
307 "error",
308 error );
309
310 /* Test regular cases
311 */
312 result = libfsapfs_directory_record_read_key_data(
313 directory_record,
314 fsapfs_test_directory_record_key_data1,
315 23,
316 &error );
317
318 FSAPFS_TEST_ASSERT_EQUAL_INT(
319 "result",
320 result,
321 1 );
322
323 FSAPFS_TEST_ASSERT_IS_NULL(
324 "error",
325 error );
326
327 /* Test error cases
328 */
329 result = libfsapfs_directory_record_read_key_data(
330 NULL,
331 fsapfs_test_directory_record_key_data1,
332 23,
333 &error );
334
335 FSAPFS_TEST_ASSERT_EQUAL_INT(
336 "result",
337 result,
338 -1 );
339
340 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
341 "error",
342 error );
343
344 libcerror_error_free(
345 &error );
346
347 result = libfsapfs_directory_record_read_key_data(
348 directory_record,
349 NULL,
350 23,
351 &error );
352
353 FSAPFS_TEST_ASSERT_EQUAL_INT(
354 "result",
355 result,
356 -1 );
357
358 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
359 "error",
360 error );
361
362 libcerror_error_free(
363 &error );
364
365 result = libfsapfs_directory_record_read_key_data(
366 directory_record,
367 fsapfs_test_directory_record_key_data1,
368 (size_t) SSIZE_MAX + 1,
369 &error );
370
371 FSAPFS_TEST_ASSERT_EQUAL_INT(
372 "result",
373 result,
374 -1 );
375
376 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
377 "error",
378 error );
379
380 libcerror_error_free(
381 &error );
382
383 result = libfsapfs_directory_record_read_key_data(
384 directory_record,
385 fsapfs_test_directory_record_key_data1,
386 0,
387 &error );
388
389 FSAPFS_TEST_ASSERT_EQUAL_INT(
390 "result",
391 result,
392 -1 );
393
394 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
395 "error",
396 error );
397
398 libcerror_error_free(
399 &error );
400
401 /* Clean up
402 */
403 result = libfsapfs_directory_record_free(
404 &directory_record,
405 &error );
406
407 FSAPFS_TEST_ASSERT_EQUAL_INT(
408 "result",
409 result,
410 1 );
411
412 FSAPFS_TEST_ASSERT_IS_NULL(
413 "directory_record",
414 directory_record );
415
416 FSAPFS_TEST_ASSERT_IS_NULL(
417 "error",
418 error );
419
420 return( 1 );
421
422 on_error:
423 if( error != NULL )
424 {
425 libcerror_error_free(
426 &error );
427 }
428 if( directory_record != NULL )
429 {
430 libfsapfs_directory_record_free(
431 &directory_record,
432 NULL );
433 }
434 return( 0 );
435 }
436
437 /* Tests the libfsapfs_directory_record_read_value_data function
438 * Returns 1 if successful or 0 if not
439 */
fsapfs_test_directory_record_read_value_data(void)440 int fsapfs_test_directory_record_read_value_data(
441 void )
442 {
443 libcerror_error_t *error = NULL;
444 libfsapfs_directory_record_t *directory_record = NULL;
445 int result = 0;
446
447 /* Initialize test
448 */
449 result = libfsapfs_directory_record_initialize(
450 &directory_record,
451 &error );
452
453 FSAPFS_TEST_ASSERT_EQUAL_INT(
454 "result",
455 result,
456 1 );
457
458 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
459 "directory_record",
460 directory_record );
461
462 FSAPFS_TEST_ASSERT_IS_NULL(
463 "error",
464 error );
465
466 /* Test regular cases
467 */
468 result = libfsapfs_directory_record_read_value_data(
469 directory_record,
470 fsapfs_test_directory_record_value_data1,
471 18,
472 &error );
473
474 FSAPFS_TEST_ASSERT_EQUAL_INT(
475 "result",
476 result,
477 1 );
478
479 FSAPFS_TEST_ASSERT_IS_NULL(
480 "error",
481 error );
482
483 /* Test error cases
484 */
485 result = libfsapfs_directory_record_read_value_data(
486 NULL,
487 fsapfs_test_directory_record_value_data1,
488 18,
489 &error );
490
491 FSAPFS_TEST_ASSERT_EQUAL_INT(
492 "result",
493 result,
494 -1 );
495
496 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
497 "error",
498 error );
499
500 libcerror_error_free(
501 &error );
502
503 result = libfsapfs_directory_record_read_value_data(
504 directory_record,
505 NULL,
506 18,
507 &error );
508
509 FSAPFS_TEST_ASSERT_EQUAL_INT(
510 "result",
511 result,
512 -1 );
513
514 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
515 "error",
516 error );
517
518 libcerror_error_free(
519 &error );
520
521 result = libfsapfs_directory_record_read_value_data(
522 directory_record,
523 fsapfs_test_directory_record_value_data1,
524 (size_t) SSIZE_MAX + 1,
525 &error );
526
527 FSAPFS_TEST_ASSERT_EQUAL_INT(
528 "result",
529 result,
530 -1 );
531
532 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
533 "error",
534 error );
535
536 libcerror_error_free(
537 &error );
538
539 result = libfsapfs_directory_record_read_value_data(
540 directory_record,
541 fsapfs_test_directory_record_value_data1,
542 0,
543 &error );
544
545 FSAPFS_TEST_ASSERT_EQUAL_INT(
546 "result",
547 result,
548 -1 );
549
550 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
551 "error",
552 error );
553
554 libcerror_error_free(
555 &error );
556
557 /* Clean up
558 */
559 result = libfsapfs_directory_record_free(
560 &directory_record,
561 &error );
562
563 FSAPFS_TEST_ASSERT_EQUAL_INT(
564 "result",
565 result,
566 1 );
567
568 FSAPFS_TEST_ASSERT_IS_NULL(
569 "directory_record",
570 directory_record );
571
572 FSAPFS_TEST_ASSERT_IS_NULL(
573 "error",
574 error );
575
576 return( 1 );
577
578 on_error:
579 if( error != NULL )
580 {
581 libcerror_error_free(
582 &error );
583 }
584 if( directory_record != NULL )
585 {
586 libfsapfs_directory_record_free(
587 &directory_record,
588 NULL );
589 }
590 return( 0 );
591 }
592
593 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
594
595 /* The main program
596 */
597 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSAPFS_TEST_ATTRIBUTE_UNUSED)598 int wmain(
599 int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
600 wchar_t * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
601 #else
602 int main(
603 int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
604 char * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
605 #endif
606 {
607 FSAPFS_TEST_UNREFERENCED_PARAMETER( argc )
608 FSAPFS_TEST_UNREFERENCED_PARAMETER( argv )
609
610 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
611
612 FSAPFS_TEST_RUN(
613 "libfsapfs_directory_record_initialize",
614 fsapfs_test_directory_record_initialize );
615
616 FSAPFS_TEST_RUN(
617 "libfsapfs_directory_record_free",
618 fsapfs_test_directory_record_free );
619
620 FSAPFS_TEST_RUN(
621 "libfsapfs_directory_record_read_key_data",
622 fsapfs_test_directory_record_read_key_data );
623
624 FSAPFS_TEST_RUN(
625 "libfsapfs_directory_record_read_value_data",
626 fsapfs_test_directory_record_read_value_data );
627
628 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
629
630 return( EXIT_SUCCESS );
631
632 on_error:
633 return( EXIT_FAILURE );
634 }
635
636