Debian Codegraph @

tests/vhdi_test_file.c

raw symbols SCIP
1 /*
2 * Library file type 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_getopt.h"
34 #include "vhdi_test_libcerror.h"
35 #include "vhdi_test_libclocale.h"
36 #include "vhdi_test_libcpath.h"
37 #include "vhdi_test_libuna.h"
38 #include "vhdi_test_libvhdi.h"
39 #include "vhdi_test_macros.h"
40 #include "vhdi_test_memory.h"
41
42 #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) && SIZEOF_WCHAR_T != 2 && SIZEOF_WCHAR_T != 4
43 #error Unsupported size of wchar_t
44 #endif
45
46 /* Define to make vhdi_test_file generate verbose output
47 #define VHDI_TEST_FILE_VERBOSE
48 */
49
50 /* Retrieves source as a narrow string
51 * Returns 1 if successful or -1 on error
52 */
53 int vhdi_test_file_get_narrow_source(
54 const system_character_t *source,
55 char *narrow_string,
56 size_t narrow_string_size,
57 libcerror_error_t **error )
58 {
59 static char *function = "vhdi_test_file_get_narrow_source";
60 size_t narrow_source_size = 0;
61 size_t source_length = 0;
62
63 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
64 int result = 0;
65 #endif
66
67 if( source == NULL )
68 {
69 libcerror_error_set(
70 error,
71 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
72 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
73 "%s: invalid source.",
74 function );
75
76 return( -1 );
77 }
78 if( narrow_string == NULL )
79 {
80 libcerror_error_set(
81 error,
82 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
83 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
84 "%s: invalid narrow string.",
85 function );
86
87 return( -1 );
88 }
89 if( narrow_string_size > (size_t) SSIZE_MAX )
90 {
91 libcerror_error_set(
92 error,
93 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
94 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
95 "%s: invalid narrow string size value exceeds maximum.",
96 function );
97
98 return( -1 );
99 }
100 source_length = system_string_length(
101 source );
102
103 if( source_length > (size_t) ( SSIZE_MAX - 1 ) )
104 {
105 libcerror_error_set(
106 error,
107 LIBCERROR_ERROR_DOMAIN_RUNTIME,
108 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
109 "%s: invalid source length value out of bounds.",
110 function );
111
112 return( -1 );
113 }
114 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
115 if( libclocale_codepage == 0 )
116 {
117 #if SIZEOF_WCHAR_T == 4
118 result = libuna_utf8_string_size_from_utf32(
119 (libuna_utf32_character_t *) source,
120 source_length + 1,
121 &narrow_source_size,
122 error );
123 #elif SIZEOF_WCHAR_T == 2
124 result = libuna_utf8_string_size_from_utf16(
125 (libuna_utf16_character_t *) source,
126 source_length + 1,
127 &narrow_source_size,
128 error );
129 #endif
130 }
131 else
132 {
133 #if SIZEOF_WCHAR_T == 4
134 result = libuna_byte_stream_size_from_utf32(
135 (libuna_utf32_character_t *) source,
136 source_length + 1,
137 libclocale_codepage,
138 &narrow_source_size,
139 error );
140 #elif SIZEOF_WCHAR_T == 2
141 result = libuna_byte_stream_size_from_utf16(
142 (libuna_utf16_character_t *) source,
143 source_length + 1,
144 libclocale_codepage,
145 &narrow_source_size,
146 error );
147 #endif
148 }
149 if( result != 1 )
150 {
151 libcerror_error_set(
152 error,
153 LIBCERROR_ERROR_DOMAIN_CONVERSION,
154 LIBCERROR_CONVERSION_ERROR_GENERIC,
155 "%s: unable to determine narrow string size.",
156 function );
157
158 return( -1 );
159 }
160 #else
161 narrow_source_size = source_length + 1;
162
163 #endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
164
165 if( narrow_string_size < narrow_source_size )
166 {
167 libcerror_error_set(
168 error,
169 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
170 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
171 "%s: narrow string too small.",
172 function );
173
174 return( -1 );
175 }
176 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
177 if( libclocale_codepage == 0 )
178 {
179 #if SIZEOF_WCHAR_T == 4
180 result = libuna_utf8_string_copy_from_utf32(
181 (libuna_utf8_character_t *) narrow_string,
182 narrow_string_size,
183 (libuna_utf32_character_t *) source,
184 source_length + 1,
185 error );
186 #elif SIZEOF_WCHAR_T == 2
187 result = libuna_utf8_string_copy_from_utf16(
188 (libuna_utf8_character_t *) narrow_string,
189 narrow_string_size,
190 (libuna_utf16_character_t *) source,
191 source_length + 1,
192 error );
193 #endif
194 }
195 else
196 {
197 #if SIZEOF_WCHAR_T == 4
198 result = libuna_byte_stream_copy_from_utf32(
199 (uint8_t *) narrow_string,
200 narrow_string_size,
201 libclocale_codepage,
202 (libuna_utf32_character_t *) source,
203 source_length + 1,
204 error );
205 #elif SIZEOF_WCHAR_T == 2
206 result = libuna_byte_stream_copy_from_utf16(
207 (uint8_t *) narrow_string,
208 narrow_string_size,
209 libclocale_codepage,
210 (libuna_utf16_character_t *) source,
211 source_length + 1,
212 error );
213 #endif
214 }
215 if( result != 1 )
216 {
217 libcerror_error_set(
218 error,
219 LIBCERROR_ERROR_DOMAIN_CONVERSION,
220 LIBCERROR_CONVERSION_ERROR_GENERIC,
221 "%s: unable to set narrow string.",
222 function );
223
224 return( -1 );
225 }
226 #else
227 if( system_string_copy(
228 narrow_string,
229 source,
230 source_length ) == NULL )
231 {
232 libcerror_error_set(
233 error,
234 LIBCERROR_ERROR_DOMAIN_MEMORY,
235 LIBCERROR_MEMORY_ERROR_COPY_FAILED,
236 "%s: unable to set narrow string.",
237 function );
238
239 return( -1 );
240 }
241 narrow_string[ source_length ] = 0;
242
243 #endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
244
245 return( 1 );
246 }
247
248 #if defined( HAVE_WIDE_CHARACTER_TYPE )
249
250 /* Retrieves source as a wide string
251 * Returns 1 if successful or -1 on error
252 */
253 int vhdi_test_file_get_wide_source(
254 const system_character_t *source,
255 wchar_t *wide_string,
256 size_t wide_string_size,
257 libcerror_error_t **error )
258 {
259 static char *function = "vhdi_test_file_get_wide_source";
260 size_t source_length = 0;
261 size_t wide_source_size = 0;
262
263 #if !defined( HAVE_WIDE_SYSTEM_CHARACTER )
264 int result = 0;
265 #endif
266
267 if( source == NULL )
268 {
269 libcerror_error_set(
270 error,
271 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
272 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
273 "%s: invalid source.",
274 function );
275
276 return( -1 );
277 }
278 if( wide_string == NULL )
279 {
280 libcerror_error_set(
281 error,
282 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
283 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
284 "%s: invalid wide string.",
285 function );
286
287 return( -1 );
288 }
289 if( wide_string_size > (size_t) SSIZE_MAX )
290 {
291 libcerror_error_set(
292 error,
293 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
294 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
295 "%s: invalid wide string size value exceeds maximum.",
296 function );
297
298 return( -1 );
299 }
300 source_length = system_string_length(
301 source );
302
303 if( source_length > (size_t) ( SSIZE_MAX - 1 ) )
304 {
305 libcerror_error_set(
306 error,
307 LIBCERROR_ERROR_DOMAIN_RUNTIME,
308 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
309 "%s: invalid source length value out of bounds.",
310 function );
311
312 return( -1 );
313 }
314 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
315 wide_source_size = source_length + 1;
316 #else
317 if( libclocale_codepage == 0 )
318 {
319 #if SIZEOF_WCHAR_T == 4
320 result = libuna_utf32_string_size_from_utf8(
321 (libuna_utf8_character_t *) source,
322 source_length + 1,
323 &wide_source_size,
324 error );
325 #elif SIZEOF_WCHAR_T == 2
326 result = libuna_utf16_string_size_from_utf8(
327 (libuna_utf8_character_t *) source,
328 source_length + 1,
329 &wide_source_size,
330 error );
331 #endif
332 }
333 else
334 {
335 #if SIZEOF_WCHAR_T == 4
336 result = libuna_utf32_string_size_from_byte_stream(
337 (uint8_t *) source,
338 source_length + 1,
339 libclocale_codepage,
340 &wide_source_size,
341 error );
342 #elif SIZEOF_WCHAR_T == 2
343 result = libuna_utf16_string_size_from_byte_stream(
344 (uint8_t *) source,
345 source_length + 1,
346 libclocale_codepage,
347 &wide_source_size,
348 error );
349 #endif
350 }
351 if( result != 1 )
352 {
353 libcerror_error_set(
354 error,
355 LIBCERROR_ERROR_DOMAIN_CONVERSION,
356 LIBCERROR_CONVERSION_ERROR_GENERIC,
357 "%s: unable to determine wide string size.",
358 function );
359
360 return( -1 );
361 }
362
363 #endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
364
365 if( wide_string_size < wide_source_size )
366 {
367 libcerror_error_set(
368 error,
369 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
370 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
371 "%s: wide string too small.",
372 function );
373
374 return( -1 );
375 }
376 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
377 if( system_string_copy(
378 wide_string,
379 source,
380 source_length ) == NULL )
381 {
382 libcerror_error_set(
383 error,
384 LIBCERROR_ERROR_DOMAIN_MEMORY,
385 LIBCERROR_MEMORY_ERROR_COPY_FAILED,
386 "%s: unable to set wide string.",
387 function );
388
389 return( -1 );
390 }
391 wide_string[ source_length ] = 0;
392 #else
393 if( libclocale_codepage == 0 )
394 {
395 #if SIZEOF_WCHAR_T == 4
396 result = libuna_utf32_string_copy_from_utf8(
397 (libuna_utf32_character_t *) wide_string,
398 wide_string_size,
399 (libuna_utf8_character_t *) source,
400 source_length + 1,
401 error );
402 #elif SIZEOF_WCHAR_T == 2
403 result = libuna_utf16_string_copy_from_utf8(
404 (libuna_utf16_character_t *) wide_string,
405 wide_string_size,
406 (libuna_utf8_character_t *) source,
407 source_length + 1,
408 error );
409 #endif
410 }
411 else
412 {
413 #if SIZEOF_WCHAR_T == 4
414 result = libuna_utf32_string_copy_from_byte_stream(
415 (libuna_utf32_character_t *) wide_string,
416 wide_string_size,
417 (uint8_t *) source,
418 source_length + 1,
419 libclocale_codepage,
420 error );
421 #elif SIZEOF_WCHAR_T == 2
422 result = libuna_utf16_string_copy_from_byte_stream(
423 (libuna_utf16_character_t *) wide_string,
424 wide_string_size,
425 (uint8_t *) source,
426 source_length + 1,
427 libclocale_codepage,
428 error );
429 #endif
430 }
431 if( result != 1 )
432 {
433 libcerror_error_set(
434 error,
435 LIBCERROR_ERROR_DOMAIN_CONVERSION,
436 LIBCERROR_CONVERSION_ERROR_GENERIC,
437 "%s: unable to set wide string.",
438 function );
439
440 return( -1 );
441 }
442
443 #endif /* defined( HAVE_WIDE_SYSTEM_CHARACTER ) */
444
445 return( 1 );
446 }
447
448 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
449
450 /* Creates and opens a source file
451 * Returns 1 if successful or -1 on error
452 */
453 int vhdi_test_file_open_source(
454 libvhdi_file_t **file,
455 const system_character_t *source,
456 libcerror_error_t **error )
457 {
458 static char *function = "vhdi_test_file_open_source";
459 int result = 0;
460
461 if( file == NULL )
462 {
463 libcerror_error_set(
464 error,
465 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
466 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
467 "%s: invalid file.",
468 function );
469
470 return( -1 );
471 }
472 if( source == NULL )
473 {
474 libcerror_error_set(
475 error,
476 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
477 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
478 "%s: invalid source.",
479 function );
480
481 return( -1 );
482 }
483 if( libvhdi_file_initialize(
484 file,
485 error ) != 1 )
486 {
487 libcerror_error_set(
488 error,
489 LIBCERROR_ERROR_DOMAIN_RUNTIME,
490 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
491 "%s: unable to initialize file.",
492 function );
493
494 goto on_error;
495 }
496 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
497 result = libvhdi_file_open_wide(
498 *file,
499 source,
500 LIBVHDI_OPEN_READ,
501 error );
502 #else
503 result = libvhdi_file_open(
504 *file,
505 source,
506 LIBVHDI_OPEN_READ,
507 error );
508 #endif
509 if( result != 1 )
510 {
511 libcerror_error_set(
512 error,
513 LIBCERROR_ERROR_DOMAIN_IO,
514 LIBCERROR_IO_ERROR_OPEN_FAILED,
515 "%s: unable to open file.",
516 function );
517
518 goto on_error;
519 }
520 return( 1 );
521
522 on_error:
523 if( *file != NULL )
524 {
525 libvhdi_file_free(
526 file,
527 NULL );
528 }
529 return( -1 );
530 }
531
532 /* Opens the parent input file
533 * Returns 1 if successful, 0 if no parent file or -1 on error
534 */
535 int vhdi_test_file_open_parent_file(
536 libvhdi_file_t **parent_file,
537 const system_character_t *source,
538 libvhdi_file_t *file,
539 libcerror_error_t **error )
540 {
541 uint8_t guid[ 16 ];
542
543 system_character_t *basename_end = NULL;
544 system_character_t *parent_basename_end = NULL;
545 system_character_t *parent_filename = NULL;
546 system_character_t *parent_path = NULL;
547 static char *function = "vhdi_test_file_open_parent_file";
548 size_t basename_length = 0;
549 size_t parent_basename_length = 0;
550 size_t parent_filename_size = 0;
551 size_t parent_path_size = 0;
552 size_t source_length = 0;
553 int result = 0;
554
555 if( parent_file == NULL )
556 {
557 libcerror_error_set(
558 error,
559 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
560 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
561 "%s: invalid parent file.",
562 function );
563
564 return( -1 );
565 }
566 result = libvhdi_file_get_parent_identifier(
567 file,
568 guid,
569 16,
570 error );
571
572 if( result == -1 )
573 {
574 libcerror_error_set(
575 error,
576 LIBCERROR_ERROR_DOMAIN_RUNTIME,
577 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
578 "%s: unable to retrieve parent content identifier.",
579 function );
580
581 goto on_error;
582 }
583 else if( result != 1 )
584 {
585 return( 0 );
586 }
587 source_length = system_string_length(
588 source );
589
590 basename_end = system_string_search_character_reverse(
591 source,
592 (system_character_t) LIBCPATH_SEPARATOR,
593 source_length + 1 );
594
595 if( basename_end != NULL )
596 {
597 basename_length = (size_t) ( basename_end - source );
598 }
599 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
600 result = libvhdi_file_get_utf16_parent_filename_size(
601 file,
602 &parent_filename_size,
603 error );
604 #else
605 result = libvhdi_file_get_utf8_parent_filename_size(
606 file,
607 &parent_filename_size,
608 error );
609 #endif
610 if( result != 1 )
611 {
612 libcerror_error_set(
613 error,
614 LIBCERROR_ERROR_DOMAIN_RUNTIME,
615 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
616 "%s: unable to retrieve parent filename size.",
617 function );
618
619 goto on_error;
620 }
621 if( parent_filename_size == 0 )
622 {
623 libcerror_error_set(
624 error,
625 LIBCERROR_ERROR_DOMAIN_RUNTIME,
626 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
627 "%s: missing parent filename.",
628 function );
629
630 goto on_error;
631 }
632 if( parent_filename_size > (size_t) ( SSIZE_MAX / sizeof( system_character_t ) ) )
633 {
634 libcerror_error_set(
635 error,
636 LIBCERROR_ERROR_DOMAIN_RUNTIME,
637 LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
638 "%s: invalid parent filename size value exceeds maximum.",
639 function );
640
641 goto on_error;
642 }
643 parent_filename = system_string_allocate(
644 parent_filename_size );
645
646 if( parent_filename == NULL )
647 {
648 libcerror_error_set(
649 error,
650 LIBCERROR_ERROR_DOMAIN_MEMORY,
651 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
652 "%s: unable to create parent filename string.",
653 function );
654
655 goto on_error;
656 }
657 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
658 result = libvhdi_file_get_utf16_parent_filename(
659 file,
660 (uint16_t *) parent_filename,
661 parent_filename_size,
662 error );
663 #else
664 result = libvhdi_file_get_utf8_parent_filename(
665 file,
666 (uint8_t *) parent_filename,
667 parent_filename_size,
668 error );
669 #endif
670 if( result != 1 )
671 {
672 libcerror_error_set(
673 error,
674 LIBCERROR_ERROR_DOMAIN_RUNTIME,
675 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
676 "%s: unable to retrieve parent filename.",
677 function );
678
679 goto on_error;
680 }
681 parent_basename_end = system_string_search_character_reverse(
682 parent_filename,
683 (system_character_t) '\\',
684 parent_filename_size );
685
686 if( parent_basename_end != NULL )
687 {
688 parent_basename_length = (size_t) ( parent_basename_end - parent_filename ) + 1;
689 }
690 if( basename_length == 0 )
691 {
692 parent_path = &( parent_filename[ parent_basename_length ] );
693 parent_path_size = parent_filename_size - ( parent_basename_length + 1 );
694 }
695 else
696 {
697 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
698 if( libcpath_path_join_wide(
699 &parent_path,
700 &parent_path_size,
701 source,
702 basename_length,
703 &( parent_filename[ parent_basename_length ] ),
704 parent_filename_size - ( parent_basename_length + 1 ),
705 error ) != 1 )
706 #else
707 if( libcpath_path_join(
708 &parent_path,
709 &parent_path_size,
710 source,
711 basename_length,
712 &( parent_filename[ parent_basename_length ] ),
713 parent_filename_size - ( parent_basename_length + 1 ),
714 error ) != 1 )
715 #endif
716 {
717 libcerror_error_set(
718 error,
719 LIBCERROR_ERROR_DOMAIN_RUNTIME,
720 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
721 "%s: unable to create parent path.",
722 function );
723
724 goto on_error;
725 }
726 }
727 if( libvhdi_file_initialize(
728 parent_file,
729 error ) != 1 )
730 {
731 libcerror_error_set(
732 error,
733 LIBCERROR_ERROR_DOMAIN_RUNTIME,
734 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
735 "%s: unable to initialize parent input file.",
736 function );
737
738 goto on_error;
739 }
740 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
741 if( libvhdi_file_open_wide(
742 *parent_file,
743 parent_path,
744 LIBVHDI_OPEN_READ,
745 error ) != 1 )
746 #else
747 if( libvhdi_file_open(
748 *parent_file,
749 parent_path,
750 LIBVHDI_OPEN_READ,
751 error ) != 1 )
752 #endif
753 {
754 libcerror_error_set(
755 error,
756 LIBCERROR_ERROR_DOMAIN_IO,
757 LIBCERROR_IO_ERROR_OPEN_FAILED,
758 "%s: unable to open parent input file: %" PRIs_SYSTEM ".",
759 function,
760 parent_path );
761
762 goto on_error;
763 }
764 if( libvhdi_file_set_parent_file(
765 file,
766 *parent_file,
767 error ) != 1 )
768 {
769 libcerror_error_set(
770 error,
771 LIBCERROR_ERROR_DOMAIN_RUNTIME,
772 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
773 "%s: unable to set parent input file.",
774 function );
775
776 goto on_error;
777 }
778 if( ( basename_length != 0 )
779 && ( parent_path != NULL ) )
780 {
781 memory_free(
782 parent_path );
783
784 parent_path = NULL;
785 }
786 if( parent_filename != NULL )
787 {
788 memory_free(
789 parent_filename );
790
791 parent_filename = NULL;
792 }
793 return( 1 );
794
795 on_error:
796 if( *parent_file != NULL )
797 {
798 libvhdi_file_free(
799 parent_file,
800 NULL );
801 }
802 if( ( basename_length != 0 )
803 && ( parent_path != NULL ) )
804 {
805 memory_free(
806 parent_path );
807 }
808 if( parent_filename != NULL )
809 {
810 memory_free(
811 parent_filename );
812 }
813 return( -1 );
814 }
815
816 /* Closes and frees a source file
817 * Returns 1 if successful or -1 on error
818 */
819 int vhdi_test_file_close_source(
820 libvhdi_file_t **file,
821 libcerror_error_t **error )
822 {
823 static char *function = "vhdi_test_file_close_source";
824 int result = 0;
825
826 if( file == NULL )
827 {
828 libcerror_error_set(
829 error,
830 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
831 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
832 "%s: invalid file.",
833 function );
834
835 return( -1 );
836 }
837 if( libvhdi_file_close(
838 *file,
839 error ) != 0 )
840 {
841 libcerror_error_set(
842 error,
843 LIBCERROR_ERROR_DOMAIN_IO,
844 LIBCERROR_IO_ERROR_CLOSE_FAILED,
845 "%s: unable to close file.",
846 function );
847
848 result = -1;
849 }
850 if( libvhdi_file_free(
851 file,
852 error ) != 1 )
853 {
854 libcerror_error_set(
855 error,
856 LIBCERROR_ERROR_DOMAIN_RUNTIME,
857 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
858 "%s: unable to free file.",
859 function );
860
861 result = -1;
862 }
863 return( result );
864 }
865
866 /* Tests the libvhdi_file_initialize function
867 * Returns 1 if successful or 0 if not
868 */
869 int vhdi_test_file_initialize(
870 void )
871 {
872 libcerror_error_t *error = NULL;
873 libvhdi_file_t *file = NULL;
874 int result = 0;
875
876 #if defined( HAVE_VHDI_TEST_MEMORY )
877 int number_of_malloc_fail_tests = 1;
878 int number_of_memset_fail_tests = 1;
879 int test_number = 0;
880 #endif
881
882 /* Test regular cases
883 */
884 result = libvhdi_file_initialize(
885 &file,
886 &error );
887
888 VHDI_TEST_ASSERT_EQUAL_INT(
889 "result",
890 result,
891 1 );
892
893 VHDI_TEST_ASSERT_IS_NOT_NULL(
894 "file",
895 file );
896
897 VHDI_TEST_ASSERT_IS_NULL(
898 "error",
899 error );
900
901 result = libvhdi_file_free(
902 &file,
903 &error );
904
905 VHDI_TEST_ASSERT_EQUAL_INT(
906 "result",
907 result,
908 1 );
909
910 VHDI_TEST_ASSERT_IS_NULL(
911 "file",
912 file );
913
914 VHDI_TEST_ASSERT_IS_NULL(
915 "error",
916 error );
917
918 /* Test error cases
919 */
920 result = libvhdi_file_initialize(
921 NULL,
922 &error );
923
924 VHDI_TEST_ASSERT_EQUAL_INT(
925 "result",
926 result,
927 -1 );
928
929 VHDI_TEST_ASSERT_IS_NOT_NULL(
930 "error",
931 error );
932
933 libcerror_error_free(
934 &error );
935
936 file = (libvhdi_file_t *) 0x12345678UL;
937
938 result = libvhdi_file_initialize(
939 &file,
940 &error );
941
942 file = NULL;
943
944 VHDI_TEST_ASSERT_EQUAL_INT(
945 "result",
946 result,
947 -1 );
948
949 VHDI_TEST_ASSERT_IS_NOT_NULL(
950 "error",
951 error );
952
953 libcerror_error_free(
954 &error );
955
956 #if defined( HAVE_VHDI_TEST_MEMORY )
957
958 for( test_number = 0;
959 test_number < number_of_malloc_fail_tests;
960 test_number++ )
961 {
962 /* Test libvhdi_file_initialize with malloc failing
963 */
964 vhdi_test_malloc_attempts_before_fail = test_number;
965
966 result = libvhdi_file_initialize(
967 &file,
968 &error );
969
970 if( vhdi_test_malloc_attempts_before_fail != -1 )
971 {
972 vhdi_test_malloc_attempts_before_fail = -1;
973
974 if( file != NULL )
975 {
976 libvhdi_file_free(
977 &file,
978 NULL );
979 }
980 }
981 else
982 {
983 VHDI_TEST_ASSERT_EQUAL_INT(
984 "result",
985 result,
986 -1 );
987
988 VHDI_TEST_ASSERT_IS_NULL(
989 "file",
990 file );
991
992 VHDI_TEST_ASSERT_IS_NOT_NULL(
993 "error",
994 error );
995
996 libcerror_error_free(
997 &error );
998 }
999 }
1000 for( test_number = 0;
1001 test_number < number_of_memset_fail_tests;
1002 test_number++ )
1003 {
1004 /* Test libvhdi_file_initialize with memset failing
1005 */
1006 vhdi_test_memset_attempts_before_fail = test_number;
1007
1008 result = libvhdi_file_initialize(
1009 &file,
1010 &error );
1011
1012 if( vhdi_test_memset_attempts_before_fail != -1 )
1013 {
1014 vhdi_test_memset_attempts_before_fail = -1;
1015
1016 if( file != NULL )
1017 {
1018 libvhdi_file_free(
1019 &file,
1020 NULL );
1021 }
1022 }
1023 else
1024 {
1025 VHDI_TEST_ASSERT_EQUAL_INT(
1026 "result",
1027 result,
1028 -1 );
1029
1030 VHDI_TEST_ASSERT_IS_NULL(
1031 "file",
1032 file );
1033
1034 VHDI_TEST_ASSERT_IS_NOT_NULL(
1035 "error",
1036 error );
1037
1038 libcerror_error_free(
1039 &error );
1040 }
1041 }
1042 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
1043
1044 return( 1 );
1045
1046 on_error:
1047 if( error != NULL )
1048 {
1049 libcerror_error_free(
1050 &error );
1051 }
1052 if( file != NULL )
1053 {
1054 libvhdi_file_free(
1055 &file,
1056 NULL );
1057 }
1058 return( 0 );
1059 }
1060
1061 /* Tests the libvhdi_file_free function
1062 * Returns 1 if successful or 0 if not
1063 */
1064 int vhdi_test_file_free(
1065 void )
1066 {
1067 libcerror_error_t *error = NULL;
1068 int result = 0;
1069
1070 /* Test error cases
1071 */
1072 result = libvhdi_file_free(
1073 NULL,
1074 &error );
1075
1076 VHDI_TEST_ASSERT_EQUAL_INT(
1077 "result",
1078 result,
1079 -1 );
1080
1081 VHDI_TEST_ASSERT_IS_NOT_NULL(
1082 "error",
1083 error );
1084
1085 libcerror_error_free(
1086 &error );
1087
1088 return( 1 );
1089
1090 on_error:
1091 if( error != NULL )
1092 {
1093 libcerror_error_free(
1094 &error );
1095 }
1096 return( 0 );
1097 }
1098
1099 /* Tests the libvhdi_file_open function
1100 * Returns 1 if successful or 0 if not
1101 */
1102 int vhdi_test_file_open(
1103 const system_character_t *source )
1104 {
1105 char narrow_source[ 256 ];
1106
1107 libcerror_error_t *error = NULL;
1108 libvhdi_file_t *file = NULL;
1109 int result = 0;
1110
1111 /* Initialize test
1112 */
1113 result = vhdi_test_file_get_narrow_source(
1114 source,
1115 narrow_source,
1116 256,
1117 &error );
1118
1119 VHDI_TEST_ASSERT_EQUAL_INT(
1120 "result",
1121 result,
1122 1 );
1123
1124 VHDI_TEST_ASSERT_IS_NULL(
1125 "error",
1126 error );
1127
1128 result = libvhdi_file_initialize(
1129 &file,
1130 &error );
1131
1132 VHDI_TEST_ASSERT_EQUAL_INT(
1133 "result",
1134 result,
1135 1 );
1136
1137 VHDI_TEST_ASSERT_IS_NOT_NULL(
1138 "file",
1139 file );
1140
1141 VHDI_TEST_ASSERT_IS_NULL(
1142 "error",
1143 error );
1144
1145 /* Test open
1146 */
1147 result = libvhdi_file_open(
1148 file,
1149 narrow_source,
1150 LIBVHDI_OPEN_READ,
1151 &error );
1152
1153 VHDI_TEST_ASSERT_EQUAL_INT(
1154 "result",
1155 result,
1156 1 );
1157
1158 VHDI_TEST_ASSERT_IS_NULL(
1159 "error",
1160 error );
1161
1162 /* Test error cases
1163 */
1164 result = libvhdi_file_open(
1165 file,
1166 narrow_source,
1167 LIBVHDI_OPEN_READ,
1168 &error );
1169
1170 VHDI_TEST_ASSERT_EQUAL_INT(
1171 "result",
1172 result,
1173 -1 );
1174
1175 VHDI_TEST_ASSERT_IS_NOT_NULL(
1176 "error",
1177 error );
1178
1179 libcerror_error_free(
1180 &error );
1181
1182 /* Clean up
1183 */
1184 result = libvhdi_file_free(
1185 &file,
1186 &error );
1187
1188 VHDI_TEST_ASSERT_EQUAL_INT(
1189 "result",
1190 result,
1191 1 );
1192
1193 VHDI_TEST_ASSERT_IS_NULL(
1194 "file",
1195 file );
1196
1197 VHDI_TEST_ASSERT_IS_NULL(
1198 "error",
1199 error );
1200
1201 return( 1 );
1202
1203 on_error:
1204 if( error != NULL )
1205 {
1206 libcerror_error_free(
1207 &error );
1208 }
1209 if( file != NULL )
1210 {
1211 libvhdi_file_free(
1212 &file,
1213 NULL );
1214 }
1215 return( 0 );
1216 }
1217
1218 #if defined( HAVE_WIDE_CHARACTER_TYPE )
1219
1220 /* Tests the libvhdi_file_open_wide function
1221 * Returns 1 if successful or 0 if not
1222 */
1223 int vhdi_test_file_open_wide(
1224 const system_character_t *source )
1225 {
1226 wchar_t wide_source[ 256 ];
1227
1228 libcerror_error_t *error = NULL;
1229 libvhdi_file_t *file = NULL;
1230 int result = 0;
1231
1232 /* Initialize test
1233 */
1234 result = vhdi_test_file_get_wide_source(
1235 source,
1236 wide_source,
1237 256,
1238 &error );
1239
1240 VHDI_TEST_ASSERT_EQUAL_INT(
1241 "result",
1242 result,
1243 1 );
1244
1245 VHDI_TEST_ASSERT_IS_NULL(
1246 "error",
1247 error );
1248
1249 result = libvhdi_file_initialize(
1250 &file,
1251 &error );
1252
1253 VHDI_TEST_ASSERT_EQUAL_INT(
1254 "result",
1255 result,
1256 1 );
1257
1258 VHDI_TEST_ASSERT_IS_NOT_NULL(
1259 "file",
1260 file );
1261
1262 VHDI_TEST_ASSERT_IS_NULL(
1263 "error",
1264 error );
1265
1266 /* Test open
1267 */
1268 result = libvhdi_file_open_wide(
1269 file,
1270 wide_source,
1271 LIBVHDI_OPEN_READ,
1272 &error );
1273
1274 VHDI_TEST_ASSERT_EQUAL_INT(
1275 "result",
1276 result,
1277 1 );
1278
1279 VHDI_TEST_ASSERT_IS_NULL(
1280 "error",
1281 error );
1282
1283 /* Test error cases
1284 */
1285 result = libvhdi_file_open_wide(
1286 file,
1287 wide_source,
1288 LIBVHDI_OPEN_READ,
1289 &error );
1290
1291 VHDI_TEST_ASSERT_EQUAL_INT(
1292 "result",
1293 result,
1294 -1 );
1295
1296 VHDI_TEST_ASSERT_IS_NOT_NULL(
1297 "error",
1298 error );
1299
1300 libcerror_error_free(
1301 &error );
1302
1303 /* Clean up
1304 */
1305 result = libvhdi_file_free(
1306 &file,
1307 &error );
1308
1309 VHDI_TEST_ASSERT_EQUAL_INT(
1310 "result",
1311 result,
1312 1 );
1313
1314 VHDI_TEST_ASSERT_IS_NULL(
1315 "file",
1316 file );
1317
1318 VHDI_TEST_ASSERT_IS_NULL(
1319 "error",
1320 error );
1321
1322 return( 1 );
1323
1324 on_error:
1325 if( error != NULL )
1326 {
1327 libcerror_error_free(
1328 &error );
1329 }
1330 if( file != NULL )
1331 {
1332 libvhdi_file_free(
1333 &file,
1334 NULL );
1335 }
1336 return( 0 );
1337 }
1338
1339 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
1340
1341 /* Tests the libvhdi_file_close function
1342 * Returns 1 if successful or 0 if not
1343 */
1344 int vhdi_test_file_close(
1345 void )
1346 {
1347 libcerror_error_t *error = NULL;
1348 int result = 0;
1349
1350 /* Test error cases
1351 */
1352 result = libvhdi_file_close(
1353 NULL,
1354 &error );
1355
1356 VHDI_TEST_ASSERT_EQUAL_INT(
1357 "result",
1358 result,
1359 -1 );
1360
1361 VHDI_TEST_ASSERT_IS_NOT_NULL(
1362 "error",
1363 error );
1364
1365 libcerror_error_free(
1366 &error );
1367
1368 return( 1 );
1369
1370 on_error:
1371 if( error != NULL )
1372 {
1373 libcerror_error_free(
1374 &error );
1375 }
1376 return( 0 );
1377 }
1378
1379 /* Tests the libvhdi_file_open and libvhdi_file_close functions
1380 * Returns 1 if successful or 0 if not
1381 */
1382 int vhdi_test_file_open_close(
1383 const system_character_t *source )
1384 {
1385 libcerror_error_t *error = NULL;
1386 libvhdi_file_t *file = NULL;
1387 int result = 0;
1388
1389 /* Initialize test
1390 */
1391 result = libvhdi_file_initialize(
1392 &file,
1393 &error );
1394
1395 VHDI_TEST_ASSERT_EQUAL_INT(
1396 "result",
1397 result,
1398 1 );
1399
1400 VHDI_TEST_ASSERT_IS_NOT_NULL(
1401 "file",
1402 file );
1403
1404 VHDI_TEST_ASSERT_IS_NULL(
1405 "error",
1406 error );
1407
1408 /* Test open and close
1409 */
1410 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1411 result = libvhdi_file_open_wide(
1412 file,
1413 source,
1414 LIBVHDI_OPEN_READ,
1415 &error );
1416 #else
1417 result = libvhdi_file_open(
1418 file,
1419 source,
1420 LIBVHDI_OPEN_READ,
1421 &error );
1422 #endif
1423
1424 VHDI_TEST_ASSERT_EQUAL_INT(
1425 "result",
1426 result,
1427 1 );
1428
1429 VHDI_TEST_ASSERT_IS_NULL(
1430 "error",
1431 error );
1432
1433 result = libvhdi_file_close(
1434 file,
1435 &error );
1436
1437 VHDI_TEST_ASSERT_EQUAL_INT(
1438 "result",
1439 result,
1440 0 );
1441
1442 VHDI_TEST_ASSERT_IS_NULL(
1443 "error",
1444 error );
1445
1446 /* Test open and close a second time to validate clean up on close
1447 */
1448 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1449 result = libvhdi_file_open_wide(
1450 file,
1451 source,
1452 LIBVHDI_OPEN_READ,
1453 &error );
1454 #else
1455 result = libvhdi_file_open(
1456 file,
1457 source,
1458 LIBVHDI_OPEN_READ,
1459 &error );
1460 #endif
1461
1462 VHDI_TEST_ASSERT_EQUAL_INT(
1463 "result",
1464 result,
1465 1 );
1466
1467 VHDI_TEST_ASSERT_IS_NULL(
1468 "error",
1469 error );
1470
1471 result = libvhdi_file_close(
1472 file,
1473 &error );
1474
1475 VHDI_TEST_ASSERT_EQUAL_INT(
1476 "result",
1477 result,
1478 0 );
1479
1480 VHDI_TEST_ASSERT_IS_NULL(
1481 "error",
1482 error );
1483
1484 /* Clean up
1485 */
1486 result = libvhdi_file_free(
1487 &file,
1488 &error );
1489
1490 VHDI_TEST_ASSERT_EQUAL_INT(
1491 "result",
1492 result,
1493 1 );
1494
1495 VHDI_TEST_ASSERT_IS_NULL(
1496 "file",
1497 file );
1498
1499 VHDI_TEST_ASSERT_IS_NULL(
1500 "error",
1501 error );
1502
1503 return( 1 );
1504
1505 on_error:
1506 if( error != NULL )
1507 {
1508 libcerror_error_free(
1509 &error );
1510 }
1511 if( file != NULL )
1512 {
1513 libvhdi_file_free(
1514 &file,
1515 NULL );
1516 }
1517 return( 0 );
1518 }
1519
1520 /* Tests the libvhdi_file_signal_abort function
1521 * Returns 1 if successful or 0 if not
1522 */
1523 int vhdi_test_file_signal_abort(
1524 libvhdi_file_t *file )
1525 {
1526 libcerror_error_t *error = NULL;
1527 int result = 0;
1528
1529 /* Test regular cases
1530 */
1531 result = libvhdi_file_signal_abort(
1532 file,
1533 &error );
1534
1535 VHDI_TEST_ASSERT_EQUAL_INT(
1536 "result",
1537 result,
1538 1 );
1539
1540 VHDI_TEST_ASSERT_IS_NULL(
1541 "error",
1542 error );
1543
1544 /* Test error cases
1545 */
1546 result = libvhdi_file_signal_abort(
1547 NULL,
1548 &error );
1549
1550 VHDI_TEST_ASSERT_EQUAL_INT(
1551 "result",
1552 result,
1553 -1 );
1554
1555 VHDI_TEST_ASSERT_IS_NOT_NULL(
1556 "error",
1557 error );
1558
1559 libcerror_error_free(
1560 &error );
1561
1562 return( 1 );
1563
1564 on_error:
1565 if( error != NULL )
1566 {
1567 libcerror_error_free(
1568 &error );
1569 }
1570 return( 0 );
1571 }
1572
1573 /* Tests the libvhdi_file_read_buffer function
1574 * Returns 1 if successful or 0 if not
1575 */
1576 int vhdi_test_file_read_buffer(
1577 libvhdi_file_t *file )
1578 {
1579 uint8_t buffer[ 16 ];
1580
1581 libcerror_error_t *error = NULL;
1582 size64_t size = 0;
1583 ssize_t read_count = 0;
1584 off64_t offset = 0;
1585
1586 /* Determine size
1587 */
1588 offset = libvhdi_file_seek_offset(
1589 file,
1590 0,
1591 SEEK_END,
1592 &error );
1593
1594 VHDI_TEST_ASSERT_NOT_EQUAL_INT64(
1595 "offset",
1596 offset,
1597 (int64_t) -1 );
1598
1599 VHDI_TEST_ASSERT_IS_NULL(
1600 "error",
1601 error );
1602
1603 size = (size64_t) offset;
1604
1605 /* Reset offset to 0
1606 */
1607 offset = libvhdi_file_seek_offset(
1608 file,
1609 0,
1610 SEEK_SET,
1611 &error );
1612
1613 VHDI_TEST_ASSERT_EQUAL_INT64(
1614 "offset",
1615 offset,
1616 (int64_t) 0 );
1617
1618 VHDI_TEST_ASSERT_IS_NULL(
1619 "error",
1620 error );
1621
1622 /* Test regular cases
1623 */
1624 if( size > 16 )
1625 {
1626 read_count = libvhdi_file_read_buffer(
1627 file,
1628 buffer,
1629 16,
1630 &error );
1631
1632 VHDI_TEST_ASSERT_EQUAL_SSIZE(
1633 "read_count",
1634 read_count,
1635 (ssize_t) 16 );
1636
1637 VHDI_TEST_ASSERT_IS_NULL(
1638 "error",
1639 error );
1640 }
1641 /* TODO read on size boundary */
1642 /* TODO read beyond size boundary */
1643
1644 /* Reset offset to 0
1645 */
1646 offset = libvhdi_file_seek_offset(
1647 file,
1648 0,
1649 SEEK_SET,
1650 &error );
1651
1652 VHDI_TEST_ASSERT_EQUAL_INT64(
1653 "offset",
1654 offset,
1655 (int64_t) 0 );
1656
1657 VHDI_TEST_ASSERT_IS_NULL(
1658 "error",
1659 error );
1660
1661 /* Test error cases
1662 */
1663 read_count = libvhdi_file_read_buffer(
1664 NULL,
1665 buffer,
1666 16,
1667 &error );
1668
1669 VHDI_TEST_ASSERT_EQUAL_SSIZE(
1670 "read_count",
1671 read_count,
1672 (ssize_t) -1 );
1673
1674 VHDI_TEST_ASSERT_IS_NOT_NULL(
1675 "error",
1676 error );
1677
1678 libcerror_error_free(
1679 &error );
1680
1681 read_count = libvhdi_file_read_buffer(
1682 file,
1683 NULL,
1684 16,
1685 &error );
1686
1687 VHDI_TEST_ASSERT_EQUAL_SSIZE(
1688 "read_count",
1689 read_count,
1690 (ssize_t) -1 );
1691
1692 VHDI_TEST_ASSERT_IS_NOT_NULL(
1693 "error",
1694 error );
1695
1696 libcerror_error_free(
1697 &error );
1698
1699 read_count = libvhdi_file_read_buffer(
1700 file,
1701 buffer,
1702 (size_t) SSIZE_MAX + 1,
1703 &error );
1704
1705 VHDI_TEST_ASSERT_EQUAL_SSIZE(
1706 "read_count",
1707 read_count,
1708 (ssize_t) -1 );
1709
1710 VHDI_TEST_ASSERT_IS_NOT_NULL(
1711 "error",
1712 error );
1713
1714 libcerror_error_free(
1715 &error );
1716
1717 return( 1 );
1718
1719 on_error:
1720 if( error != NULL )
1721 {
1722 libcerror_error_free(
1723 &error );
1724 }
1725 return( 0 );
1726 }
1727
1728 /* Tests the libvhdi_file_seek_offset function
1729 * Returns 1 if successful or 0 if not
1730 */
1731 int vhdi_test_file_seek_offset(
1732 libvhdi_file_t *file )
1733 {
1734 libcerror_error_t *error = NULL;
1735 size64_t size = 0;
1736 off64_t offset = 0;
1737
1738 /* Test regular cases
1739 */
1740 offset = libvhdi_file_seek_offset(
1741 file,
1742 0,
1743 SEEK_END,
1744 &error );
1745
1746 VHDI_TEST_ASSERT_NOT_EQUAL_INT64(
1747 "offset",
1748 offset,
1749 (int64_t) -1 );
1750
1751 VHDI_TEST_ASSERT_IS_NULL(
1752 "error",
1753 error );
1754
1755 size = (size64_t) offset;
1756
1757 offset = libvhdi_file_seek_offset(
1758 file,
1759 1024,
1760 SEEK_SET,
1761 &error );
1762
1763 VHDI_TEST_ASSERT_EQUAL_INT64(
1764 "offset",
1765 offset,
1766 (int64_t) 1024 );
1767
1768 VHDI_TEST_ASSERT_IS_NULL(
1769 "error",
1770 error );
1771
1772 offset = libvhdi_file_seek_offset(
1773 file,
1774 -512,
1775 SEEK_CUR,
1776 &error );
1777
1778 VHDI_TEST_ASSERT_EQUAL_INT64(
1779 "offset",
1780 offset,
1781 (int64_t) 512 );
1782
1783 VHDI_TEST_ASSERT_IS_NULL(
1784 "error",
1785 error );
1786
1787 offset = libvhdi_file_seek_offset(
1788 file,
1789 (off64_t) ( size + 512 ),
1790 SEEK_SET,
1791 &error );
1792
1793 VHDI_TEST_ASSERT_EQUAL_INT64(
1794 "offset",
1795 offset,
1796 (int64_t) ( size + 512 ) );
1797
1798 VHDI_TEST_ASSERT_IS_NULL(
1799 "error",
1800 error );
1801
1802 /* Reset offset to 0
1803 */
1804 offset = libvhdi_file_seek_offset(
1805 file,
1806 0,
1807 SEEK_SET,
1808 &error );
1809
1810 VHDI_TEST_ASSERT_EQUAL_INT64(
1811 "offset",
1812 offset,
1813 (int64_t) 0 );
1814
1815 VHDI_TEST_ASSERT_IS_NULL(
1816 "error",
1817 error );
1818
1819 /* Test error cases
1820 */
1821 offset = libvhdi_file_seek_offset(
1822 NULL,
1823 0,
1824 SEEK_SET,
1825 &error );
1826
1827 VHDI_TEST_ASSERT_EQUAL_INT64(
1828 "offset",
1829 offset,
1830 (int64_t) -1 );
1831
1832 VHDI_TEST_ASSERT_IS_NOT_NULL(
1833 "error",
1834 error );
1835
1836 libcerror_error_free(
1837 &error );
1838
1839 offset = libvhdi_file_seek_offset(
1840 file,
1841 -1,
1842 SEEK_SET,
1843 &error );
1844
1845 VHDI_TEST_ASSERT_EQUAL_INT64(
1846 "offset",
1847 offset,
1848 (int64_t) -1 );
1849
1850 VHDI_TEST_ASSERT_IS_NOT_NULL(
1851 "error",
1852 error );
1853
1854 libcerror_error_free(
1855 &error );
1856
1857 offset = libvhdi_file_seek_offset(
1858 file,
1859 -1,
1860 SEEK_CUR,
1861 &error );
1862
1863 VHDI_TEST_ASSERT_EQUAL_INT64(
1864 "offset",
1865 offset,
1866 (int64_t) -1 );
1867
1868 VHDI_TEST_ASSERT_IS_NOT_NULL(
1869 "error",
1870 error );
1871
1872 offset = libvhdi_file_seek_offset(
1873 file,
1874 (off64_t) ( -1 * ( size + 1 ) ),
1875 SEEK_END,
1876 &error );
1877
1878 VHDI_TEST_ASSERT_EQUAL_INT64(
1879 "offset",
1880 offset,
1881 (int64_t) -1 );
1882
1883 VHDI_TEST_ASSERT_IS_NOT_NULL(
1884 "error",
1885 error );
1886
1887 libcerror_error_free(
1888 &error );
1889
1890 return( 1 );
1891
1892 on_error:
1893 if( error != NULL )
1894 {
1895 libcerror_error_free(
1896 &error );
1897 }
1898 return( 0 );
1899 }
1900
1901 /* Tests the libvhdi_file_get_offset function
1902 * Returns 1 if successful or 0 if not
1903 */
1904 int vhdi_test_file_get_offset(
1905 libvhdi_file_t *file )
1906 {
1907 libcerror_error_t *error = NULL;
1908 off64_t offset = 0;
1909 int result = 0;
1910
1911 /* Test regular cases
1912 */
1913 result = libvhdi_file_get_offset(
1914 file,
1915 &offset,
1916 &error );
1917
1918 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
1919 "result",
1920 result,
1921 -1 );
1922
1923 VHDI_TEST_ASSERT_IS_NULL(
1924 "error",
1925 error );
1926
1927 /* Test error cases
1928 */
1929 result = libvhdi_file_get_offset(
1930 NULL,
1931 &offset,
1932 &error );
1933
1934 VHDI_TEST_ASSERT_EQUAL_INT(
1935 "result",
1936 result,
1937 -1 );
1938
1939 VHDI_TEST_ASSERT_IS_NOT_NULL(
1940 "error",
1941 error );
1942
1943 libcerror_error_free(
1944 &error );
1945
1946 result = libvhdi_file_get_offset(
1947 file,
1948 NULL,
1949 &error );
1950
1951 VHDI_TEST_ASSERT_EQUAL_INT(
1952 "result",
1953 result,
1954 -1 );
1955
1956 VHDI_TEST_ASSERT_IS_NOT_NULL(
1957 "error",
1958 error );
1959
1960 libcerror_error_free(
1961 &error );
1962
1963 return( 1 );
1964
1965 on_error:
1966 if( error != NULL )
1967 {
1968 libcerror_error_free(
1969 &error );
1970 }
1971 return( 0 );
1972 }
1973
1974 /* Tests the libvhdi_file_get_media_size function
1975 * Returns 1 if successful or 0 if not
1976 */
1977 int vhdi_test_file_get_media_size(
1978 libvhdi_file_t *file )
1979 {
1980 libcerror_error_t *error = NULL;
1981 size64_t media_size = 0;
1982 int result = 0;
1983
1984 /* Test regular cases
1985 */
1986 result = libvhdi_file_get_media_size(
1987 file,
1988 &media_size,
1989 &error );
1990
1991 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
1992 "result",
1993 result,
1994 -1 );
1995
1996 VHDI_TEST_ASSERT_IS_NULL(
1997 "error",
1998 error );
1999
2000 /* Test error cases
2001 */
2002 result = libvhdi_file_get_media_size(
2003 NULL,
2004 &media_size,
2005 &error );
2006
2007 VHDI_TEST_ASSERT_EQUAL_INT(
2008 "result",
2009 result,
2010 -1 );
2011
2012 VHDI_TEST_ASSERT_IS_NOT_NULL(
2013 "error",
2014 error );
2015
2016 libcerror_error_free(
2017 &error );
2018
2019 result = libvhdi_file_get_media_size(
2020 file,
2021 NULL,
2022 &error );
2023
2024 VHDI_TEST_ASSERT_EQUAL_INT(
2025 "result",
2026 result,
2027 -1 );
2028
2029 VHDI_TEST_ASSERT_IS_NOT_NULL(
2030 "error",
2031 error );
2032
2033 libcerror_error_free(
2034 &error );
2035
2036 return( 1 );
2037
2038 on_error:
2039 if( error != NULL )
2040 {
2041 libcerror_error_free(
2042 &error );
2043 }
2044 return( 0 );
2045 }
2046
2047 /* Tests the libvhdi_file_get_disk_type function
2048 * Returns 1 if successful or 0 if not
2049 */
2050 int vhdi_test_file_get_disk_type(
2051 libvhdi_file_t *file )
2052 {
2053 libcerror_error_t *error = NULL;
2054 uint32_t disk_type = 0;
2055 int result = 0;
2056
2057 /* Test regular cases
2058 */
2059 result = libvhdi_file_get_disk_type(
2060 file,
2061 &disk_type,
2062 &error );
2063
2064 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2065 "result",
2066 result,
2067 -1 );
2068
2069 VHDI_TEST_ASSERT_IS_NULL(
2070 "error",
2071 error );
2072
2073 /* Test error cases
2074 */
2075 result = libvhdi_file_get_disk_type(
2076 NULL,
2077 &disk_type,
2078 &error );
2079
2080 VHDI_TEST_ASSERT_EQUAL_INT(
2081 "result",
2082 result,
2083 -1 );
2084
2085 VHDI_TEST_ASSERT_IS_NOT_NULL(
2086 "error",
2087 error );
2088
2089 libcerror_error_free(
2090 &error );
2091
2092 result = libvhdi_file_get_disk_type(
2093 file,
2094 NULL,
2095 &error );
2096
2097 VHDI_TEST_ASSERT_EQUAL_INT(
2098 "result",
2099 result,
2100 -1 );
2101
2102 VHDI_TEST_ASSERT_IS_NOT_NULL(
2103 "error",
2104 error );
2105
2106 libcerror_error_free(
2107 &error );
2108
2109 return( 1 );
2110
2111 on_error:
2112 if( error != NULL )
2113 {
2114 libcerror_error_free(
2115 &error );
2116 }
2117 return( 0 );
2118 }
2119
2120 /* Tests the libvhdi_file_get_identifier function
2121 * Returns 1 if successful or 0 if not
2122 */
2123 int vhdi_test_file_get_identifier(
2124 libvhdi_file_t *file )
2125 {
2126 uint8_t guid_data[ 16 ];
2127
2128 libcerror_error_t *error = NULL;
2129 int result = 0;
2130
2131 /* Test regular cases
2132 */
2133 result = libvhdi_file_get_identifier(
2134 file,
2135 guid_data,
2136 16,
2137 &error );
2138
2139 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2140 "result",
2141 result,
2142 -1 );
2143
2144 VHDI_TEST_ASSERT_IS_NULL(
2145 "error",
2146 error );
2147
2148 /* Test error cases
2149 */
2150 result = libvhdi_file_get_identifier(
2151 NULL,
2152 guid_data,
2153 16,
2154 &error );
2155
2156 VHDI_TEST_ASSERT_EQUAL_INT(
2157 "result",
2158 result,
2159 -1 );
2160
2161 VHDI_TEST_ASSERT_IS_NOT_NULL(
2162 "error",
2163 error );
2164
2165 libcerror_error_free(
2166 &error );
2167
2168 result = libvhdi_file_get_identifier(
2169 file,
2170 NULL,
2171 16,
2172 &error );
2173
2174 VHDI_TEST_ASSERT_EQUAL_INT(
2175 "result",
2176 result,
2177 -1 );
2178
2179 VHDI_TEST_ASSERT_IS_NOT_NULL(
2180 "error",
2181 error );
2182
2183 libcerror_error_free(
2184 &error );
2185
2186 result = libvhdi_file_get_identifier(
2187 file,
2188 guid_data,
2189 0,
2190 &error );
2191
2192 VHDI_TEST_ASSERT_EQUAL_INT(
2193 "result",
2194 result,
2195 -1 );
2196
2197 VHDI_TEST_ASSERT_IS_NOT_NULL(
2198 "error",
2199 error );
2200
2201 libcerror_error_free(
2202 &error );
2203
2204 /* TODO fix
2205 result = libvhdi_file_get_identifier(
2206 file,
2207 guid_data,
2208 (size_t) SSIZE_MAX + 1,
2209 &error );
2210
2211 VHDI_TEST_ASSERT_EQUAL_INT(
2212 "result",
2213 result,
2214 -1 );
2215
2216 VHDI_TEST_ASSERT_IS_NOT_NULL(
2217 "error",
2218 error );
2219
2220 libcerror_error_free(
2221 &error );
2222 */
2223
2224 return( 1 );
2225
2226 on_error:
2227 if( error != NULL )
2228 {
2229 libcerror_error_free(
2230 &error );
2231 }
2232 return( 0 );
2233 }
2234
2235 /* Tests the libvhdi_file_get_parent_identifier function
2236 * Returns 1 if successful or 0 if not
2237 */
2238 int vhdi_test_file_get_parent_identifier(
2239 libvhdi_file_t *file )
2240 {
2241 uint8_t guid_data[ 16 ];
2242
2243 libcerror_error_t *error = NULL;
2244 int result = 0;
2245
2246 /* Test regular cases
2247 */
2248 result = libvhdi_file_get_parent_identifier(
2249 file,
2250 guid_data,
2251 16,
2252 &error );
2253
2254 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2255 "result",
2256 result,
2257 -1 );
2258
2259 VHDI_TEST_ASSERT_IS_NULL(
2260 "error",
2261 error );
2262
2263 /* Test error cases
2264 */
2265 result = libvhdi_file_get_parent_identifier(
2266 NULL,
2267 guid_data,
2268 16,
2269 &error );
2270
2271 VHDI_TEST_ASSERT_EQUAL_INT(
2272 "result",
2273 result,
2274 -1 );
2275
2276 VHDI_TEST_ASSERT_IS_NOT_NULL(
2277 "error",
2278 error );
2279
2280 libcerror_error_free(
2281 &error );
2282
2283 /* TODO fix
2284 result = libvhdi_file_get_parent_identifier(
2285 file,
2286 NULL,
2287 16,
2288 &error );
2289
2290 VHDI_TEST_ASSERT_EQUAL_INT(
2291 "result",
2292 result,
2293 -1 );
2294
2295 VHDI_TEST_ASSERT_IS_NOT_NULL(
2296 "error",
2297 error );
2298
2299 libcerror_error_free(
2300 &error );
2301
2302 result = libvhdi_file_get_parent_identifier(
2303 file,
2304 guid_data,
2305 0,
2306 &error );
2307
2308 VHDI_TEST_ASSERT_EQUAL_INT(
2309 "result",
2310 result,
2311 -1 );
2312
2313 VHDI_TEST_ASSERT_IS_NOT_NULL(
2314 "error",
2315 error );
2316
2317 libcerror_error_free(
2318 &error );
2319
2320 result = libvhdi_file_get_parent_identifier(
2321 file,
2322 guid_data,
2323 (size_t) SSIZE_MAX + 1,
2324 &error );
2325
2326 VHDI_TEST_ASSERT_EQUAL_INT(
2327 "result",
2328 result,
2329 -1 );
2330
2331 VHDI_TEST_ASSERT_IS_NOT_NULL(
2332 "error",
2333 error );
2334
2335 libcerror_error_free(
2336 &error );
2337 */
2338
2339 return( 1 );
2340
2341 on_error:
2342 if( error != NULL )
2343 {
2344 libcerror_error_free(
2345 &error );
2346 }
2347 return( 0 );
2348 }
2349
2350 /* Tests the libvhdi_file_get_utf8_parent_filename_size function
2351 * Returns 1 if successful or 0 if not
2352 */
2353 int vhdi_test_file_get_utf8_parent_filename_size(
2354 libvhdi_file_t *file )
2355 {
2356 libcerror_error_t *error = NULL;
2357 size_t utf8_parent_filename_size = 0;
2358 int result = 0;
2359 int utf8_parent_filename_size_is_set = 0;
2360
2361 /* Test regular cases
2362 */
2363 result = libvhdi_file_get_utf8_parent_filename_size(
2364 file,
2365 &utf8_parent_filename_size,
2366 &error );
2367
2368 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2369 "result",
2370 result,
2371 -1 );
2372
2373 VHDI_TEST_ASSERT_IS_NULL(
2374 "error",
2375 error );
2376
2377 utf8_parent_filename_size_is_set = result;
2378
2379 /* Test error cases
2380 */
2381 result = libvhdi_file_get_utf8_parent_filename_size(
2382 NULL,
2383 &utf8_parent_filename_size,
2384 &error );
2385
2386 VHDI_TEST_ASSERT_EQUAL_INT(
2387 "result",
2388 result,
2389 -1 );
2390
2391 VHDI_TEST_ASSERT_IS_NOT_NULL(
2392 "error",
2393 error );
2394
2395 libcerror_error_free(
2396 &error );
2397
2398 if( utf8_parent_filename_size_is_set != 0 )
2399 {
2400 result = libvhdi_file_get_utf8_parent_filename_size(
2401 file,
2402 NULL,
2403 &error );
2404
2405 VHDI_TEST_ASSERT_EQUAL_INT(
2406 "result",
2407 result,
2408 -1 );
2409
2410 VHDI_TEST_ASSERT_IS_NOT_NULL(
2411 "error",
2412 error );
2413
2414 libcerror_error_free(
2415 &error );
2416 }
2417 return( 1 );
2418
2419 on_error:
2420 if( error != NULL )
2421 {
2422 libcerror_error_free(
2423 &error );
2424 }
2425 return( 0 );
2426 }
2427
2428 /* Tests the libvhdi_file_get_utf8_parent_filename function
2429 * Returns 1 if successful or 0 if not
2430 */
2431 int vhdi_test_file_get_utf8_parent_filename(
2432 libvhdi_file_t *file )
2433 {
2434 uint8_t utf8_parent_filename[ 512 ];
2435
2436 libcerror_error_t *error = NULL;
2437 int result = 0;
2438 int utf8_parent_filename_is_set = 0;
2439
2440 /* Test regular cases
2441 */
2442 result = libvhdi_file_get_utf8_parent_filename(
2443 file,
2444 utf8_parent_filename,
2445 512,
2446 &error );
2447
2448 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2449 "result",
2450 result,
2451 -1 );
2452
2453 VHDI_TEST_ASSERT_IS_NULL(
2454 "error",
2455 error );
2456
2457 utf8_parent_filename_is_set = result;
2458
2459 /* Test error cases
2460 */
2461 result = libvhdi_file_get_utf8_parent_filename(
2462 NULL,
2463 utf8_parent_filename,
2464 512,
2465 &error );
2466
2467 VHDI_TEST_ASSERT_EQUAL_INT(
2468 "result",
2469 result,
2470 -1 );
2471
2472 VHDI_TEST_ASSERT_IS_NOT_NULL(
2473 "error",
2474 error );
2475
2476 libcerror_error_free(
2477 &error );
2478
2479 if( utf8_parent_filename_is_set != 0 )
2480 {
2481 result = libvhdi_file_get_utf8_parent_filename(
2482 file,
2483 NULL,
2484 512,
2485 &error );
2486
2487 VHDI_TEST_ASSERT_EQUAL_INT(
2488 "result",
2489 result,
2490 -1 );
2491
2492 VHDI_TEST_ASSERT_IS_NOT_NULL(
2493 "error",
2494 error );
2495
2496 libcerror_error_free(
2497 &error );
2498
2499 result = libvhdi_file_get_utf8_parent_filename(
2500 file,
2501 utf8_parent_filename,
2502 0,
2503 &error );
2504
2505 VHDI_TEST_ASSERT_EQUAL_INT(
2506 "result",
2507 result,
2508 -1 );
2509
2510 VHDI_TEST_ASSERT_IS_NOT_NULL(
2511 "error",
2512 error );
2513
2514 libcerror_error_free(
2515 &error );
2516
2517 result = libvhdi_file_get_utf8_parent_filename(
2518 file,
2519 utf8_parent_filename,
2520 (size_t) SSIZE_MAX + 1,
2521 &error );
2522
2523 VHDI_TEST_ASSERT_EQUAL_INT(
2524 "result",
2525 result,
2526 -1 );
2527
2528 VHDI_TEST_ASSERT_IS_NOT_NULL(
2529 "error",
2530 error );
2531
2532 libcerror_error_free(
2533 &error );
2534 }
2535 return( 1 );
2536
2537 on_error:
2538 if( error != NULL )
2539 {
2540 libcerror_error_free(
2541 &error );
2542 }
2543 return( 0 );
2544 }
2545
2546 /* Tests the libvhdi_file_get_utf16_parent_filename_size function
2547 * Returns 1 if successful or 0 if not
2548 */
2549 int vhdi_test_file_get_utf16_parent_filename_size(
2550 libvhdi_file_t *file )
2551 {
2552 libcerror_error_t *error = NULL;
2553 size_t utf16_parent_filename_size = 0;
2554 int result = 0;
2555 int utf16_parent_filename_size_is_set = 0;
2556
2557 /* Test regular cases
2558 */
2559 result = libvhdi_file_get_utf16_parent_filename_size(
2560 file,
2561 &utf16_parent_filename_size,
2562 &error );
2563
2564 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2565 "result",
2566 result,
2567 -1 );
2568
2569 VHDI_TEST_ASSERT_IS_NULL(
2570 "error",
2571 error );
2572
2573 utf16_parent_filename_size_is_set = result;
2574
2575 /* Test error cases
2576 */
2577 result = libvhdi_file_get_utf16_parent_filename_size(
2578 NULL,
2579 &utf16_parent_filename_size,
2580 &error );
2581
2582 VHDI_TEST_ASSERT_EQUAL_INT(
2583 "result",
2584 result,
2585 -1 );
2586
2587 VHDI_TEST_ASSERT_IS_NOT_NULL(
2588 "error",
2589 error );
2590
2591 libcerror_error_free(
2592 &error );
2593
2594 if( utf16_parent_filename_size_is_set != 0 )
2595 {
2596 result = libvhdi_file_get_utf16_parent_filename_size(
2597 file,
2598 NULL,
2599 &error );
2600
2601 VHDI_TEST_ASSERT_EQUAL_INT(
2602 "result",
2603 result,
2604 -1 );
2605
2606 VHDI_TEST_ASSERT_IS_NOT_NULL(
2607 "error",
2608 error );
2609
2610 libcerror_error_free(
2611 &error );
2612 }
2613 return( 1 );
2614
2615 on_error:
2616 if( error != NULL )
2617 {
2618 libcerror_error_free(
2619 &error );
2620 }
2621 return( 0 );
2622 }
2623
2624 /* Tests the libvhdi_file_get_utf16_parent_filename function
2625 * Returns 1 if successful or 0 if not
2626 */
2627 int vhdi_test_file_get_utf16_parent_filename(
2628 libvhdi_file_t *file )
2629 {
2630 uint16_t utf16_parent_filename[ 512 ];
2631
2632 libcerror_error_t *error = NULL;
2633 int result = 0;
2634 int utf16_parent_filename_is_set = 0;
2635
2636 /* Test regular cases
2637 */
2638 result = libvhdi_file_get_utf16_parent_filename(
2639 file,
2640 utf16_parent_filename,
2641 512,
2642 &error );
2643
2644 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2645 "result",
2646 result,
2647 -1 );
2648
2649 VHDI_TEST_ASSERT_IS_NULL(
2650 "error",
2651 error );
2652
2653 utf16_parent_filename_is_set = result;
2654
2655 /* Test error cases
2656 */
2657 result = libvhdi_file_get_utf16_parent_filename(
2658 NULL,
2659 utf16_parent_filename,
2660 512,
2661 &error );
2662
2663 VHDI_TEST_ASSERT_EQUAL_INT(
2664 "result",
2665 result,
2666 -1 );
2667
2668 VHDI_TEST_ASSERT_IS_NOT_NULL(
2669 "error",
2670 error );
2671
2672 libcerror_error_free(
2673 &error );
2674
2675 if( utf16_parent_filename_is_set != 0 )
2676 {
2677 result = libvhdi_file_get_utf16_parent_filename(
2678 file,
2679 NULL,
2680 512,
2681 &error );
2682
2683 VHDI_TEST_ASSERT_EQUAL_INT(
2684 "result",
2685 result,
2686 -1 );
2687
2688 VHDI_TEST_ASSERT_IS_NOT_NULL(
2689 "error",
2690 error );
2691
2692 libcerror_error_free(
2693 &error );
2694
2695 result = libvhdi_file_get_utf16_parent_filename(
2696 file,
2697 utf16_parent_filename,
2698 0,
2699 &error );
2700
2701 VHDI_TEST_ASSERT_EQUAL_INT(
2702 "result",
2703 result,
2704 -1 );
2705
2706 VHDI_TEST_ASSERT_IS_NOT_NULL(
2707 "error",
2708 error );
2709
2710 libcerror_error_free(
2711 &error );
2712
2713 result = libvhdi_file_get_utf16_parent_filename(
2714 file,
2715 utf16_parent_filename,
2716 (size_t) SSIZE_MAX + 1,
2717 &error );
2718
2719 VHDI_TEST_ASSERT_EQUAL_INT(
2720 "result",
2721 result,
2722 -1 );
2723
2724 VHDI_TEST_ASSERT_IS_NOT_NULL(
2725 "error",
2726 error );
2727
2728 libcerror_error_free(
2729 &error );
2730 }
2731 return( 1 );
2732
2733 on_error:
2734 if( error != NULL )
2735 {
2736 libcerror_error_free(
2737 &error );
2738 }
2739 return( 0 );
2740 }
2741
2742 /* The main program
2743 */
2744 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
2745 int wmain(
2746 int argc,
2747 wchar_t * const argv[] )
2748 #else
2749 int main(
2750 int argc,
2751 char * const argv[] )
2752 #endif
2753 {
2754 libcerror_error_t *error = NULL;
2755 libvhdi_file_t *file = NULL;
2756 libvhdi_file_t *parent_file = NULL;
2757 system_character_t *source = NULL;
2758 system_integer_t option = 0;
2759 int result = 0;
2760
2761 while( ( option = vhdi_test_getopt(
2762 argc,
2763 argv,
2764 _SYSTEM_STRING( "" ) ) ) != (system_integer_t) -1 )
2765 {
2766 switch( option )
2767 {
2768 case (system_integer_t) '?':
2769 default:
2770 fprintf(
2771 stderr,
2772 "Invalid argument: %" PRIs_SYSTEM ".\n",
2773 argv[ optind - 1 ] );
2774
2775 return( EXIT_FAILURE );
2776 }
2777 }
2778 if( optind < argc )
2779 {
2780 source = argv[ optind ];
2781 }
2782 #if defined( HAVE_DEBUG_OUTPUT ) && defined( VHDI_TEST_FILE_VERBOSE )
2783 libvhdi_notify_set_verbose(
2784 1 );
2785 libvhdi_notify_set_stream(
2786 stderr,
2787 NULL );
2788 #endif
2789
2790 VHDI_TEST_RUN(
2791 "libvhdi_file_initialize",
2792 vhdi_test_file_initialize );
2793
2794 VHDI_TEST_RUN(
2795 "libvhdi_file_free",
2796 vhdi_test_file_free );
2797
2798 #if !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 )
2799 if( source != NULL )
2800 {
2801 VHDI_TEST_RUN_WITH_ARGS(
2802 "libvhdi_file_open",
2803 vhdi_test_file_open,
2804 source );
2805
2806 #if defined( HAVE_WIDE_CHARACTER_TYPE )
2807
2808 VHDI_TEST_RUN_WITH_ARGS(
2809 "libvhdi_file_open_wide",
2810 vhdi_test_file_open_wide,
2811 source );
2812
2813 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
2814
2815 #if defined( LIBVHDI_HAVE_BFIO )
2816
2817 /* TODO add test for libvhdi_file_open_file_io_handle */
2818
2819 #endif /* defined( LIBVHDI_HAVE_BFIO ) */
2820
2821 VHDI_TEST_RUN(
2822 "libvhdi_file_close",
2823 vhdi_test_file_close );
2824
2825 VHDI_TEST_RUN_WITH_ARGS(
2826 "libvhdi_file_open_close",
2827 vhdi_test_file_open_close,
2828 source );
2829
2830 /* Initialize test
2831 */
2832 result = vhdi_test_file_open_source(
2833 &file,
2834 source,
2835 &error );
2836
2837 VHDI_TEST_ASSERT_EQUAL_INT(
2838 "result",
2839 result,
2840 1 );
2841
2842 VHDI_TEST_ASSERT_IS_NOT_NULL(
2843 "file",
2844 file );
2845
2846 VHDI_TEST_ASSERT_IS_NULL(
2847 "error",
2848 error );
2849
2850 result = vhdi_test_file_open_parent_file(
2851 &parent_file,
2852 source,
2853 file,
2854 &error );
2855
2856 VHDI_TEST_ASSERT_NOT_EQUAL_INT(
2857 "result",
2858 result,
2859 -1 );
2860
2861 VHDI_TEST_ASSERT_IS_NULL(
2862 "error",
2863 error );
2864
2865 VHDI_TEST_RUN_WITH_ARGS(
2866 "libvhdi_file_signal_abort",
2867 vhdi_test_file_signal_abort,
2868 file );
2869
2870 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
2871
2872 /* TODO: add tests for libvhdi_internal_file_open_read */
2873
2874 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
2875
2876 VHDI_TEST_RUN_WITH_ARGS(
2877 "libvhdi_file_read_buffer",
2878 vhdi_test_file_read_buffer,
2879 file );
2880
2881 /* TODO: add tests for libvhdi_file_read_buffer_at_offset */
2882
2883 /* TODO: add tests for libvhdi_file_write_buffer */
2884
2885 /* TODO: add tests for libvhdi_file_write_buffer_at_offset */
2886
2887 VHDI_TEST_RUN_WITH_ARGS(
2888 "libvhdi_file_seek_offset",
2889 vhdi_test_file_seek_offset,
2890 file );
2891
2892 VHDI_TEST_RUN_WITH_ARGS(
2893 "libvhdi_file_get_offset",
2894 vhdi_test_file_get_offset,
2895 file );
2896
2897 /* TODO: add tests for libvhdi_file_set_parent_file */
2898
2899 VHDI_TEST_RUN_WITH_ARGS(
2900 "libvhdi_file_get_media_size",
2901 vhdi_test_file_get_media_size,
2902 file );
2903
2904 /* TODO: add tests for libvhdi_file_get_format_version */
2905
2906 VHDI_TEST_RUN_WITH_ARGS(
2907 "libvhdi_file_get_disk_type",
2908 vhdi_test_file_get_disk_type,
2909 file );
2910
2911 VHDI_TEST_RUN_WITH_ARGS(
2912 "libvhdi_file_get_identifier",
2913 vhdi_test_file_get_identifier,
2914 file );
2915
2916 VHDI_TEST_RUN_WITH_ARGS(
2917 "libvhdi_file_get_parent_identifier",
2918 vhdi_test_file_get_parent_identifier,
2919 file );
2920
2921 VHDI_TEST_RUN_WITH_ARGS(
2922 "libvhdi_file_get_utf8_parent_filename_size",
2923 vhdi_test_file_get_utf8_parent_filename_size,
2924 file );
2925
2926 VHDI_TEST_RUN_WITH_ARGS(
2927 "libvhdi_file_get_utf8_parent_filename",
2928 vhdi_test_file_get_utf8_parent_filename,
2929 file );
2930
2931 VHDI_TEST_RUN_WITH_ARGS(
2932 "libvhdi_file_get_utf16_parent_filename_size",
2933 vhdi_test_file_get_utf16_parent_filename_size,
2934 file );
2935
2936 VHDI_TEST_RUN_WITH_ARGS(
2937 "libvhdi_file_get_utf16_parent_filename",
2938 vhdi_test_file_get_utf16_parent_filename,
2939 file );
2940
2941 /* Clean up
2942 */
2943 result = vhdi_test_file_close_source(
2944 &file,
2945 &error );
2946
2947 VHDI_TEST_ASSERT_EQUAL_INT(
2948 "result",
2949 result,
2950 0 );
2951
2952 VHDI_TEST_ASSERT_IS_NULL(
2953 "file",
2954 file );
2955
2956 VHDI_TEST_ASSERT_IS_NULL(
2957 "error",
2958 error );
2959
2960 if( parent_file != NULL )
2961 {
2962 result = vhdi_test_file_close_source(
2963 &parent_file,
2964 &error );
2965
2966 VHDI_TEST_ASSERT_EQUAL_INT(
2967 "result",
2968 result,
2969 0 );
2970
2971 VHDI_TEST_ASSERT_IS_NULL(
2972 "error",
2973 error );
2974 }
2975 }
2976 #endif /* !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 ) */
2977
2978 return( EXIT_SUCCESS );
2979
2980 on_error:
2981 if( error != NULL )
2982 {
2983 libcerror_error_free(
2984 &error );
2985 }
2986 if( file != NULL )
2987 {
2988 vhdi_test_file_close_source(
2989 &file,
2990 NULL );
2991 }
2992 if( parent_file != NULL )
2993 {
2994 vhdi_test_file_close_source(
2995 &parent_file,
2996 NULL );
2997 }
2998 return( EXIT_FAILURE );
2999 }
3000