Debian Codegraph @

tests/vhdi_test_support.c

raw symbols SCIP
1 /*
2 * Library support functions test program
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
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 <narrow_string.h>
25 #include <system_string.h>
26 #include <types.h>
27 #include <wide_string.h>
28
29 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
30 #include <stdlib.h>
31 #endif
32
33 #include "vhdi_test_functions.h"
34 #include "vhdi_test_getopt.h"
35 #include "vhdi_test_libbfio.h"
36 #include "vhdi_test_libcerror.h"
37 #include "vhdi_test_libvhdi.h"
38 #include "vhdi_test_macros.h"
39 #include "vhdi_test_memory.h"
40 #include "vhdi_test_unused.h"
41
42 #if !defined( LIBVHDI_HAVE_BFIO )
43
44 LIBVHDI_EXTERN \
45 int libvhdi_check_file_signature_file_io_handle(
46 libbfio_handle_t *file_io_handle,
47 libcerror_error_t **error );
48
49 #endif /* !defined( LIBVHDI_HAVE_BFIO ) */
50
51 /* Tests the libvhdi_get_version function
52 * Returns 1 if successful or 0 if not
53 */
54 int vhdi_test_get_version(
55 void )
56 {
57 const char *version_string = NULL;
58 int result = 0;
59
60 version_string = libvhdi_get_version();
61
62 result = narrow_string_compare(
63 version_string,
64 LIBVHDI_VERSION_STRING,
65 9 );
66
67 VHDI_TEST_ASSERT_EQUAL_INT(
68 "result",
69 result,
70 0 );
71
72 return( 1 );
73
74 on_error:
75 return( 0 );
76 }
77
78 /* Tests the libvhdi_get_access_flags_read function
79 * Returns 1 if successful or 0 if not
80 */
81 int vhdi_test_get_access_flags_read(
82 void )
83 {
84 int access_flags = 0;
85
86 access_flags = libvhdi_get_access_flags_read();
87
88 VHDI_TEST_ASSERT_EQUAL_INT(
89 "access_flags",
90 access_flags,
91 LIBVHDI_ACCESS_FLAG_READ );
92
93 return( 1 );
94
95 on_error:
96 return( 0 );
97 }
98
99 /* Tests the libvhdi_get_codepage function
100 * Returns 1 if successful or 0 if not
101 */
102 int vhdi_test_get_codepage(
103 void )
104 {
105 libcerror_error_t *error = NULL;
106 int codepage = 0;
107 int result = 0;
108
109 result = libvhdi_get_codepage(
110 &codepage,
111 &error );
112
113 VHDI_TEST_ASSERT_EQUAL_INT(
114 "result",
115 result,
116 1 );
117
118 VHDI_TEST_ASSERT_IS_NULL(
119 "error",
120 error );
121
122 /* Test error cases
123 */
124 result = libvhdi_get_codepage(
125 NULL,
126 &error );
127
128 VHDI_TEST_ASSERT_EQUAL_INT(
129 "result",
130 result,
131 -1 );
132
133 VHDI_TEST_ASSERT_IS_NOT_NULL(
134 "error",
135 error );
136
137 libcerror_error_free(
138 &error );
139
140 return( 1 );
141
142 on_error:
143 if( error != NULL )
144 {
145 libcerror_error_free(
146 &error );
147 }
148 return( 0 );
149 }
150
151 /* Tests the libvhdi_set_codepage function
152 * Returns 1 if successful or 0 if not
153 */
154 int vhdi_test_set_codepage(
155 void )
156 {
157 libcerror_error_t *error = NULL;
158 int result = 0;
159
160 result = libvhdi_set_codepage(
161 0,
162 &error );
163
164 VHDI_TEST_ASSERT_EQUAL_INT(
165 "result",
166 result,
167 1 );
168
169 VHDI_TEST_ASSERT_IS_NULL(
170 "error",
171 error );
172
173 /* Test error cases
174 */
175 result = libvhdi_set_codepage(
176 -1,
177 &error );
178
179 VHDI_TEST_ASSERT_EQUAL_INT(
180 "result",
181 result,
182 -1 );
183
184 VHDI_TEST_ASSERT_IS_NOT_NULL(
185 "error",
186 error );
187
188 libcerror_error_free(
189 &error );
190
191 return( 1 );
192
193 on_error:
194 if( error != NULL )
195 {
196 libcerror_error_free(
197 &error );
198 }
199 return( 0 );
200 }
201
202 /* Tests the libvhdi_check_file_signature function
203 * Returns 1 if successful or 0 if not
204 */
205 int vhdi_test_check_file_signature(
206 const system_character_t *source )
207 {
208 char narrow_source[ 256 ];
209
210 libcerror_error_t *error = NULL;
211 int result = 0;
212
213 if( source != NULL )
214 {
215 /* Initialize test
216 */
217 result = vhdi_test_get_narrow_source(
218 source,
219 narrow_source,
220 256,
221 &error );
222
223 VHDI_TEST_ASSERT_EQUAL_INT(
224 "result",
225 result,
226 1 );
227
228 VHDI_TEST_ASSERT_IS_NULL(
229 "error",
230 error );
231
232 /* Test check file signature
233 */
234 result = libvhdi_check_file_signature(
235 narrow_source,
236 &error );
237
238 VHDI_TEST_ASSERT_EQUAL_INT(
239 "result",
240 result,
241 1 );
242
243 VHDI_TEST_ASSERT_IS_NULL(
244 "error",
245 error );
246 }
247 /* Test error cases
248 */
249 result = libvhdi_check_file_signature(
250 NULL,
251 &error );
252
253 VHDI_TEST_ASSERT_EQUAL_INT(
254 "result",
255 result,
256 -1 );
257
258 VHDI_TEST_ASSERT_IS_NOT_NULL(
259 "error",
260 error );
261
262 libcerror_error_free(
263 &error );
264
265 result = libvhdi_check_file_signature(
266 "",
267 &error );
268
269 VHDI_TEST_ASSERT_EQUAL_INT(
270 "result",
271 result,
272 -1 );
273
274 VHDI_TEST_ASSERT_IS_NOT_NULL(
275 "error",
276 error );
277
278 libcerror_error_free(
279 &error );
280
281 if( source != NULL )
282 {
283 #if defined( HAVE_VHDI_TEST_MEMORY )
284
285 /* Test libvhdi_check_file_signature with malloc failing in libbfio_file_initialize
286 */
287 vhdi_test_malloc_attempts_before_fail = 0;
288
289 result = libvhdi_check_file_signature(
290 narrow_source,
291 &error );
292
293 if( vhdi_test_malloc_attempts_before_fail != -1 )
294 {
295 vhdi_test_malloc_attempts_before_fail = -1;
296 }
297 else
298 {
299 VHDI_TEST_ASSERT_EQUAL_INT(
300 "result",
301 result,
302 -1 );
303
304 VHDI_TEST_ASSERT_IS_NOT_NULL(
305 "error",
306 error );
307
308 libcerror_error_free(
309 &error );
310 }
311 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
312 }
313 return( 1 );
314
315 on_error:
316 if( error != NULL )
317 {
318 libcerror_error_free(
319 &error );
320 }
321 return( 0 );
322 }
323
324 #if defined( HAVE_WIDE_CHARACTER_TYPE )
325
326 /* Tests the libvhdi_check_file_signature_wide function
327 * Returns 1 if successful or 0 if not
328 */
329 int vhdi_test_check_file_signature_wide(
330 const system_character_t *source )
331 {
332 wchar_t wide_source[ 256 ];
333
334 libcerror_error_t *error = NULL;
335 int result = 0;
336
337 if( source != NULL )
338 {
339 /* Initialize test
340 */
341 result = vhdi_test_get_wide_source(
342 source,
343 wide_source,
344 256,
345 &error );
346
347 VHDI_TEST_ASSERT_EQUAL_INT(
348 "result",
349 result,
350 1 );
351
352 VHDI_TEST_ASSERT_IS_NULL(
353 "error",
354 error );
355
356 /* Test check file signature
357 */
358 result = libvhdi_check_file_signature_wide(
359 wide_source,
360 &error );
361
362 VHDI_TEST_ASSERT_EQUAL_INT(
363 "result",
364 result,
365 1 );
366
367 VHDI_TEST_ASSERT_IS_NULL(
368 "error",
369 error );
370 }
371 /* Test error cases
372 */
373 result = libvhdi_check_file_signature_wide(
374 NULL,
375 &error );
376
377 VHDI_TEST_ASSERT_EQUAL_INT(
378 "result",
379 result,
380 -1 );
381
382 VHDI_TEST_ASSERT_IS_NOT_NULL(
383 "error",
384 error );
385
386 libcerror_error_free(
387 &error );
388
389 result = libvhdi_check_file_signature_wide(
390 L"",
391 &error );
392
393 VHDI_TEST_ASSERT_EQUAL_INT(
394 "result",
395 result,
396 -1 );
397
398 VHDI_TEST_ASSERT_IS_NOT_NULL(
399 "error",
400 error );
401
402 libcerror_error_free(
403 &error );
404
405 if( source != NULL )
406 {
407 #if defined( HAVE_VHDI_TEST_MEMORY )
408
409 /* Test libvhdi_check_file_signature_wide with malloc failing in libbfio_file_initialize
410 */
411 vhdi_test_malloc_attempts_before_fail = 0;
412
413 result = libvhdi_check_file_signature_wide(
414 wide_source,
415 &error );
416
417 if( vhdi_test_malloc_attempts_before_fail != -1 )
418 {
419 vhdi_test_malloc_attempts_before_fail = -1;
420 }
421 else
422 {
423 VHDI_TEST_ASSERT_EQUAL_INT(
424 "result",
425 result,
426 -1 );
427
428 VHDI_TEST_ASSERT_IS_NOT_NULL(
429 "error",
430 error );
431
432 libcerror_error_free(
433 &error );
434 }
435 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
436 }
437 return( 1 );
438
439 on_error:
440 if( error != NULL )
441 {
442 libcerror_error_free(
443 &error );
444 }
445 return( 0 );
446 }
447
448 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
449
450 /* Tests the libvhdi_check_file_signature_file_io_handle function
451 * Returns 1 if successful or 0 if not
452 */
453 int vhdi_test_check_file_signature_file_io_handle(
454 const system_character_t *source )
455 {
456 uint8_t empty_block[ 8192 ];
457
458 libbfio_handle_t *file_io_handle = NULL;
459 libcerror_error_t *error = NULL;
460 void *memset_result = NULL;
461 size_t source_length = 0;
462 int result = 0;
463
464 /* Initialize test
465 */
466 memset_result = memory_set(
467 empty_block,
468 0,
469 sizeof( uint8_t ) * 8192 );
470
471 VHDI_TEST_ASSERT_IS_NOT_NULL(
472 "memset_result",
473 memset_result );
474
475 if( source != NULL )
476 {
477 /* Initialize test
478 */
479 result = libbfio_file_initialize(
480 &file_io_handle,
481 &error );
482
483 VHDI_TEST_ASSERT_EQUAL_INT(
484 "result",
485 result,
486 1 );
487
488 VHDI_TEST_ASSERT_IS_NOT_NULL(
489 "file_io_handle",
490 file_io_handle );
491
492 VHDI_TEST_ASSERT_IS_NULL(
493 "error",
494 error );
495
496 source_length = system_string_length(
497 source );
498
499 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
500 result = libbfio_file_set_name_wide(
501 file_io_handle,
502 source,
503 source_length,
504 &error );
505 #else
506 result = libbfio_file_set_name(
507 file_io_handle,
508 source,
509 source_length,
510 &error );
511 #endif
512 VHDI_TEST_ASSERT_EQUAL_INT(
513 "result",
514 result,
515 1 );
516
517 VHDI_TEST_ASSERT_IS_NULL(
518 "error",
519 error );
520
521 result = libbfio_handle_open(
522 file_io_handle,
523 LIBBFIO_OPEN_READ,
524 &error );
525
526 VHDI_TEST_ASSERT_EQUAL_INT(
527 "result",
528 result,
529 1 );
530
531 VHDI_TEST_ASSERT_IS_NULL(
532 "error",
533 error );
534
535 /* Test check file signature
536 */
537 result = libvhdi_check_file_signature_file_io_handle(
538 file_io_handle,
539 &error );
540
541 VHDI_TEST_ASSERT_EQUAL_INT(
542 "result",
543 result,
544 1 );
545
546 VHDI_TEST_ASSERT_IS_NULL(
547 "error",
548 error );
549 }
550 /* Test error cases
551 */
552 result = libvhdi_check_file_signature_file_io_handle(
553 NULL,
554 &error );
555
556 VHDI_TEST_ASSERT_EQUAL_INT(
557 "result",
558 result,
559 -1 );
560
561 VHDI_TEST_ASSERT_IS_NOT_NULL(
562 "error",
563 error );
564
565 libcerror_error_free(
566 &error );
567
568 /* Clean up
569 */
570 if( source != NULL )
571 {
572 result = libbfio_handle_close(
573 file_io_handle,
574 &error );
575
576 VHDI_TEST_ASSERT_EQUAL_INT(
577 "result",
578 result,
579 0 );
580
581 VHDI_TEST_ASSERT_IS_NULL(
582 "error",
583 error );
584
585 result = libbfio_handle_free(
586 &file_io_handle,
587 &error );
588
589 VHDI_TEST_ASSERT_EQUAL_INT(
590 "result",
591 result,
592 1 );
593
594 VHDI_TEST_ASSERT_IS_NULL(
595 "file_io_handle",
596 file_io_handle );
597
598 VHDI_TEST_ASSERT_IS_NULL(
599 "error",
600 error );
601 }
602 /* Test check file signature with data too small
603 */
604 result = vhdi_test_open_file_io_handle(
605 &file_io_handle,
606 empty_block,
607 sizeof( uint8_t ) * 1,
608 &error );
609
610 VHDI_TEST_ASSERT_EQUAL_INT(
611 "result",
612 result,
613 1 );
614
615 VHDI_TEST_ASSERT_IS_NOT_NULL(
616 "file_io_handle",
617 file_io_handle );
618
619 VHDI_TEST_ASSERT_IS_NULL(
620 "error",
621 error );
622
623 result = libvhdi_check_file_signature_file_io_handle(
624 file_io_handle,
625 &error );
626
627 VHDI_TEST_ASSERT_EQUAL_INT(
628 "result",
629 result,
630 0 );
631
632 VHDI_TEST_ASSERT_IS_NULL(
633 "error",
634 error );
635
636 result = vhdi_test_close_file_io_handle(
637 &file_io_handle,
638 &error );
639
640 VHDI_TEST_ASSERT_EQUAL_INT(
641 "result",
642 result,
643 0 );
644
645 VHDI_TEST_ASSERT_IS_NULL(
646 "error",
647 error );
648
649 /* Test check file signature with empty block
650 */
651 result = vhdi_test_open_file_io_handle(
652 &file_io_handle,
653 empty_block,
654 sizeof( uint8_t ) * 8192,
655 &error );
656
657 VHDI_TEST_ASSERT_EQUAL_INT(
658 "result",
659 result,
660 1 );
661
662 VHDI_TEST_ASSERT_IS_NOT_NULL(
663 "file_io_handle",
664 file_io_handle );
665
666 VHDI_TEST_ASSERT_IS_NULL(
667 "error",
668 error );
669
670 result = libvhdi_check_file_signature_file_io_handle(
671 file_io_handle,
672 &error );
673
674 VHDI_TEST_ASSERT_EQUAL_INT(
675 "result",
676 result,
677 0 );
678
679 VHDI_TEST_ASSERT_IS_NULL(
680 "error",
681 error );
682
683 result = vhdi_test_close_file_io_handle(
684 &file_io_handle,
685 &error );
686
687 VHDI_TEST_ASSERT_EQUAL_INT(
688 "result",
689 result,
690 0 );
691
692 VHDI_TEST_ASSERT_IS_NULL(
693 "error",
694 error );
695
696 return( 1 );
697
698 on_error:
699 if( error != NULL )
700 {
701 libcerror_error_free(
702 &error );
703 }
704 if( file_io_handle != NULL )
705 {
706 libbfio_handle_free(
707 &file_io_handle,
708 NULL );
709 }
710 return( 0 );
711 }
712
713 /* The main program
714 */
715 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
716 int wmain(
717 int argc,
718 wchar_t * const argv[] )
719 #else
720 int main(
721 int argc,
722 char * const argv[] )
723 #endif
724 {
725 system_character_t *source = NULL;
726 system_integer_t option = 0;
727
728 while( ( option = vhdi_test_getopt(
729 argc,
730 argv,
731 _SYSTEM_STRING( "" ) ) ) != (system_integer_t) -1 )
732 {
733 switch( option )
734 {
735 case (system_integer_t) '?':
736 default:
737 fprintf(
738 stderr,
739 "Invalid argument: %" PRIs_SYSTEM ".\n",
740 argv[ optind - 1 ] );
741
742 return( EXIT_FAILURE );
743 }
744 }
745 if( optind < argc )
746 {
747 source = argv[ optind ];
748 }
749 VHDI_TEST_RUN(
750 "libvhdi_get_version",
751 vhdi_test_get_version );
752
753 VHDI_TEST_RUN(
754 "libvhdi_get_access_flags_read",
755 vhdi_test_get_access_flags_read );
756
757 VHDI_TEST_RUN(
758 "libvhdi_get_codepage",
759 vhdi_test_get_codepage );
760
761 VHDI_TEST_RUN(
762 "libvhdi_set_codepage",
763 vhdi_test_set_codepage );
764
765 #if !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 )
766
767 VHDI_TEST_RUN_WITH_ARGS(
768 "libvhdi_check_file_signature",
769 vhdi_test_check_file_signature,
770 source );
771
772 #if defined( HAVE_WIDE_CHARACTER_TYPE )
773
774 VHDI_TEST_RUN_WITH_ARGS(
775 "libvhdi_check_file_signature_wide",
776 vhdi_test_check_file_signature_wide,
777 source );
778
779 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
780
781 VHDI_TEST_RUN_WITH_ARGS(
782 "libvhdi_check_file_signature_file_io_handle",
783 vhdi_test_check_file_signature_file_io_handle,
784 source );
785
786 #endif /* !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 ) */
787
788 return( EXIT_SUCCESS );
789
790 on_error:
791 return( EXIT_FAILURE );
792 }
793