1 /*
2  * Library container 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 <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 "fsapfs_test_functions.h"
34 #include "fsapfs_test_getopt.h"
35 #include "fsapfs_test_libbfio.h"
36 #include "fsapfs_test_libcerror.h"
37 #include "fsapfs_test_libclocale.h"
38 #include "fsapfs_test_libfsapfs.h"
39 #include "fsapfs_test_libuna.h"
40 #include "fsapfs_test_macros.h"
41 #include "fsapfs_test_memory.h"
42 
43 #include "../libfsapfs/libfsapfs_container.h"
44 
45 #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) && SIZEOF_WCHAR_T != 2 && SIZEOF_WCHAR_T != 4
46 #error Unsupported size of wchar_t
47 #endif
48 
49 /* Define to make fsapfs_test_container generate verbose output
50 #define FSAPFS_TEST_CONTAINER_VERBOSE
51  */
52 
53 #if !defined( LIBFSAPFS_HAVE_BFIO )
54 
55 LIBFSAPFS_EXTERN \
56 int libfsapfs_check_container_signature_file_io_handle(
57      libbfio_handle_t *file_io_handle,
58      libcerror_error_t **error );
59 
60 LIBFSAPFS_EXTERN \
61 int libfsapfs_container_open_file_io_handle(
62      libfsapfs_container_t *container,
63      libbfio_handle_t *file_io_handle,
64      int access_flags,
65      libfsapfs_error_t **error );
66 
67 #endif /* !defined( LIBFSAPFS_HAVE_BFIO ) */
68 
69 /* Creates and opens a source container
70  * Returns 1 if successful or -1 on error
71  */
fsapfs_test_container_open_source(libfsapfs_container_t ** container,libbfio_handle_t * file_io_handle,libcerror_error_t ** error)72 int fsapfs_test_container_open_source(
73      libfsapfs_container_t **container,
74      libbfio_handle_t *file_io_handle,
75      libcerror_error_t **error )
76 {
77 	static char *function = "fsapfs_test_container_open_source";
78 	int result            = 0;
79 
80 	if( container == NULL )
81 	{
82 		libcerror_error_set(
83 		 error,
84 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
85 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
86 		 "%s: invalid container.",
87 		 function );
88 
89 		return( -1 );
90 	}
91 	if( file_io_handle == NULL )
92 	{
93 		libcerror_error_set(
94 		 error,
95 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
96 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
97 		 "%s: invalid file IO handle.",
98 		 function );
99 
100 		return( -1 );
101 	}
102 	if( libfsapfs_container_initialize(
103 	     container,
104 	     error ) != 1 )
105 	{
106 		libcerror_error_set(
107 		 error,
108 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
109 		 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
110 		 "%s: unable to initialize container.",
111 		 function );
112 
113 		goto on_error;
114 	}
115 	result = libfsapfs_container_open_file_io_handle(
116 	          *container,
117 	          file_io_handle,
118 	          LIBFSAPFS_OPEN_READ,
119 	          error );
120 
121 	if( result != 1 )
122 	{
123 		libcerror_error_set(
124 		 error,
125 		 LIBCERROR_ERROR_DOMAIN_IO,
126 		 LIBCERROR_IO_ERROR_OPEN_FAILED,
127 		 "%s: unable to open container.",
128 		 function );
129 
130 		goto on_error;
131 	}
132 	return( 1 );
133 
134 on_error:
135 	if( *container != NULL )
136 	{
137 		libfsapfs_container_free(
138 		 container,
139 		 NULL );
140 	}
141 	return( -1 );
142 }
143 
144 /* Closes and frees a source container
145  * Returns 1 if successful or -1 on error
146  */
fsapfs_test_container_close_source(libfsapfs_container_t ** container,libcerror_error_t ** error)147 int fsapfs_test_container_close_source(
148      libfsapfs_container_t **container,
149      libcerror_error_t **error )
150 {
151 	static char *function = "fsapfs_test_container_close_source";
152 	int result            = 0;
153 
154 	if( container == NULL )
155 	{
156 		libcerror_error_set(
157 		 error,
158 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160 		 "%s: invalid container.",
161 		 function );
162 
163 		return( -1 );
164 	}
165 	if( libfsapfs_container_close(
166 	     *container,
167 	     error ) != 0 )
168 	{
169 		libcerror_error_set(
170 		 error,
171 		 LIBCERROR_ERROR_DOMAIN_IO,
172 		 LIBCERROR_IO_ERROR_CLOSE_FAILED,
173 		 "%s: unable to close container.",
174 		 function );
175 
176 		result = -1;
177 	}
178 	if( libfsapfs_container_free(
179 	     container,
180 	     error ) != 1 )
181 	{
182 		libcerror_error_set(
183 		 error,
184 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
185 		 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
186 		 "%s: unable to free container.",
187 		 function );
188 
189 		result = -1;
190 	}
191 	return( result );
192 }
193 
194 /* Tests the libfsapfs_container_initialize function
195  * Returns 1 if successful or 0 if not
196  */
fsapfs_test_container_initialize(void)197 int fsapfs_test_container_initialize(
198      void )
199 {
200 	libcerror_error_t *error         = NULL;
201 	libfsapfs_container_t *container = NULL;
202 	int result                       = 0;
203 
204 #if defined( HAVE_FSAPFS_TEST_MEMORY )
205 	int number_of_malloc_fail_tests  = 1;
206 	int number_of_memset_fail_tests  = 1;
207 	int test_number                  = 0;
208 #endif
209 
210 	/* Test regular cases
211 	 */
212 	result = libfsapfs_container_initialize(
213 	          &container,
214 	          &error );
215 
216 	FSAPFS_TEST_ASSERT_EQUAL_INT(
217 	 "result",
218 	 result,
219 	 1 );
220 
221 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
222 	 "container",
223 	 container );
224 
225 	FSAPFS_TEST_ASSERT_IS_NULL(
226 	 "error",
227 	 error );
228 
229 	result = libfsapfs_container_free(
230 	          &container,
231 	          &error );
232 
233 	FSAPFS_TEST_ASSERT_EQUAL_INT(
234 	 "result",
235 	 result,
236 	 1 );
237 
238 	FSAPFS_TEST_ASSERT_IS_NULL(
239 	 "container",
240 	 container );
241 
242 	FSAPFS_TEST_ASSERT_IS_NULL(
243 	 "error",
244 	 error );
245 
246 	/* Test error cases
247 	 */
248 	result = libfsapfs_container_initialize(
249 	          NULL,
250 	          &error );
251 
252 	FSAPFS_TEST_ASSERT_EQUAL_INT(
253 	 "result",
254 	 result,
255 	 -1 );
256 
257 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
258 	 "error",
259 	 error );
260 
261 	libcerror_error_free(
262 	 &error );
263 
264 	container = (libfsapfs_container_t *) 0x12345678UL;
265 
266 	result = libfsapfs_container_initialize(
267 	          &container,
268 	          &error );
269 
270 	FSAPFS_TEST_ASSERT_EQUAL_INT(
271 	 "result",
272 	 result,
273 	 -1 );
274 
275 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
276 	 "error",
277 	 error );
278 
279 	libcerror_error_free(
280 	 &error );
281 
282 	container = NULL;
283 
284 #if defined( HAVE_FSAPFS_TEST_MEMORY )
285 
286 	for( test_number = 0;
287 	     test_number < number_of_malloc_fail_tests;
288 	     test_number++ )
289 	{
290 		/* Test libfsapfs_container_initialize with malloc failing
291 		 */
292 		fsapfs_test_malloc_attempts_before_fail = test_number;
293 
294 		result = libfsapfs_container_initialize(
295 		          &container,
296 		          &error );
297 
298 		if( fsapfs_test_malloc_attempts_before_fail != -1 )
299 		{
300 			fsapfs_test_malloc_attempts_before_fail = -1;
301 
302 			if( container != NULL )
303 			{
304 				libfsapfs_container_free(
305 				 &container,
306 				 NULL );
307 			}
308 		}
309 		else
310 		{
311 			FSAPFS_TEST_ASSERT_EQUAL_INT(
312 			 "result",
313 			 result,
314 			 -1 );
315 
316 			FSAPFS_TEST_ASSERT_IS_NULL(
317 			 "container",
318 			 container );
319 
320 			FSAPFS_TEST_ASSERT_IS_NOT_NULL(
321 			 "error",
322 			 error );
323 
324 			libcerror_error_free(
325 			 &error );
326 		}
327 	}
328 	for( test_number = 0;
329 	     test_number < number_of_memset_fail_tests;
330 	     test_number++ )
331 	{
332 		/* Test libfsapfs_container_initialize with memset failing
333 		 */
334 		fsapfs_test_memset_attempts_before_fail = test_number;
335 
336 		result = libfsapfs_container_initialize(
337 		          &container,
338 		          &error );
339 
340 		if( fsapfs_test_memset_attempts_before_fail != -1 )
341 		{
342 			fsapfs_test_memset_attempts_before_fail = -1;
343 
344 			if( container != NULL )
345 			{
346 				libfsapfs_container_free(
347 				 &container,
348 				 NULL );
349 			}
350 		}
351 		else
352 		{
353 			FSAPFS_TEST_ASSERT_EQUAL_INT(
354 			 "result",
355 			 result,
356 			 -1 );
357 
358 			FSAPFS_TEST_ASSERT_IS_NULL(
359 			 "container",
360 			 container );
361 
362 			FSAPFS_TEST_ASSERT_IS_NOT_NULL(
363 			 "error",
364 			 error );
365 
366 			libcerror_error_free(
367 			 &error );
368 		}
369 	}
370 #endif /* defined( HAVE_FSAPFS_TEST_MEMORY ) */
371 
372 	return( 1 );
373 
374 on_error:
375 	if( error != NULL )
376 	{
377 		libcerror_error_free(
378 		 &error );
379 	}
380 	if( container != NULL )
381 	{
382 		libfsapfs_container_free(
383 		 &container,
384 		 NULL );
385 	}
386 	return( 0 );
387 }
388 
389 /* Tests the libfsapfs_container_free function
390  * Returns 1 if successful or 0 if not
391  */
fsapfs_test_container_free(void)392 int fsapfs_test_container_free(
393      void )
394 {
395 	libcerror_error_t *error = NULL;
396 	int result               = 0;
397 
398 	/* Test error cases
399 	 */
400 	result = libfsapfs_container_free(
401 	          NULL,
402 	          &error );
403 
404 	FSAPFS_TEST_ASSERT_EQUAL_INT(
405 	 "result",
406 	 result,
407 	 -1 );
408 
409 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
410 	 "error",
411 	 error );
412 
413 	libcerror_error_free(
414 	 &error );
415 
416 	return( 1 );
417 
418 on_error:
419 	if( error != NULL )
420 	{
421 		libcerror_error_free(
422 		 &error );
423 	}
424 	return( 0 );
425 }
426 
427 /* Tests the libfsapfs_container_open function
428  * Returns 1 if successful or 0 if not
429  */
fsapfs_test_container_open(const system_character_t * source)430 int fsapfs_test_container_open(
431      const system_character_t *source )
432 {
433 	char narrow_source[ 256 ];
434 
435 	libcerror_error_t *error         = NULL;
436 	libfsapfs_container_t *container = NULL;
437 	int result                       = 0;
438 
439 	/* Initialize test
440 	 */
441 	result = fsapfs_test_get_narrow_source(
442 	          source,
443 	          narrow_source,
444 	          256,
445 	          &error );
446 
447 	FSAPFS_TEST_ASSERT_EQUAL_INT(
448 	 "result",
449 	 result,
450 	 1 );
451 
452 	FSAPFS_TEST_ASSERT_IS_NULL(
453 	 "error",
454 	 error );
455 
456 	result = libfsapfs_container_initialize(
457 	          &container,
458 	          &error );
459 
460 	FSAPFS_TEST_ASSERT_EQUAL_INT(
461 	 "result",
462 	 result,
463 	 1 );
464 
465 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
466 	 "container",
467 	 container );
468 
469 	FSAPFS_TEST_ASSERT_IS_NULL(
470 	 "error",
471 	 error );
472 
473 	/* Test open
474 	 */
475 	result = libfsapfs_container_open(
476 	          container,
477 	          narrow_source,
478 	          LIBFSAPFS_OPEN_READ,
479 	          &error );
480 
481 	FSAPFS_TEST_ASSERT_EQUAL_INT(
482 	 "result",
483 	 result,
484 	 1 );
485 
486 	FSAPFS_TEST_ASSERT_IS_NULL(
487 	 "error",
488 	 error );
489 
490 	/* Test error cases
491 	 */
492 	result = libfsapfs_container_open(
493 	          container,
494 	          narrow_source,
495 	          LIBFSAPFS_OPEN_READ,
496 	          &error );
497 
498 	FSAPFS_TEST_ASSERT_EQUAL_INT(
499 	 "result",
500 	 result,
501 	 -1 );
502 
503 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
504 	 "error",
505 	 error );
506 
507 	libcerror_error_free(
508 	 &error );
509 
510 	/* Clean up
511 	 */
512 	result = libfsapfs_container_free(
513 	          &container,
514 	          &error );
515 
516 	FSAPFS_TEST_ASSERT_EQUAL_INT(
517 	 "result",
518 	 result,
519 	 1 );
520 
521 	FSAPFS_TEST_ASSERT_IS_NULL(
522 	 "container",
523 	 container );
524 
525 	FSAPFS_TEST_ASSERT_IS_NULL(
526 	 "error",
527 	 error );
528 
529 	return( 1 );
530 
531 on_error:
532 	if( error != NULL )
533 	{
534 		libcerror_error_free(
535 		 &error );
536 	}
537 	if( container != NULL )
538 	{
539 		libfsapfs_container_free(
540 		 &container,
541 		 NULL );
542 	}
543 	return( 0 );
544 }
545 
546 #if defined( HAVE_WIDE_CHARACTER_TYPE )
547 
548 /* Tests the libfsapfs_container_open_wide function
549  * Returns 1 if successful or 0 if not
550  */
fsapfs_test_container_open_wide(const system_character_t * source)551 int fsapfs_test_container_open_wide(
552      const system_character_t *source )
553 {
554 	wchar_t wide_source[ 256 ];
555 
556 	libcerror_error_t *error         = NULL;
557 	libfsapfs_container_t *container = NULL;
558 	int result                       = 0;
559 
560 	/* Initialize test
561 	 */
562 	result = fsapfs_test_get_wide_source(
563 	          source,
564 	          wide_source,
565 	          256,
566 	          &error );
567 
568 	FSAPFS_TEST_ASSERT_EQUAL_INT(
569 	 "result",
570 	 result,
571 	 1 );
572 
573 	FSAPFS_TEST_ASSERT_IS_NULL(
574 	 "error",
575 	 error );
576 
577 	result = libfsapfs_container_initialize(
578 	          &container,
579 	          &error );
580 
581 	FSAPFS_TEST_ASSERT_EQUAL_INT(
582 	 "result",
583 	 result,
584 	 1 );
585 
586 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
587 	 "container",
588 	 container );
589 
590 	FSAPFS_TEST_ASSERT_IS_NULL(
591 	 "error",
592 	 error );
593 
594 	/* Test open
595 	 */
596 	result = libfsapfs_container_open_wide(
597 	          container,
598 	          wide_source,
599 	          LIBFSAPFS_OPEN_READ,
600 	          &error );
601 
602 	FSAPFS_TEST_ASSERT_EQUAL_INT(
603 	 "result",
604 	 result,
605 	 1 );
606 
607 	FSAPFS_TEST_ASSERT_IS_NULL(
608 	 "error",
609 	 error );
610 
611 	/* Test error cases
612 	 */
613 	result = libfsapfs_container_open_wide(
614 	          container,
615 	          wide_source,
616 	          LIBFSAPFS_OPEN_READ,
617 	          &error );
618 
619 	FSAPFS_TEST_ASSERT_EQUAL_INT(
620 	 "result",
621 	 result,
622 	 -1 );
623 
624 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
625 	 "error",
626 	 error );
627 
628 	libcerror_error_free(
629 	 &error );
630 
631 	/* Clean up
632 	 */
633 	result = libfsapfs_container_free(
634 	          &container,
635 	          &error );
636 
637 	FSAPFS_TEST_ASSERT_EQUAL_INT(
638 	 "result",
639 	 result,
640 	 1 );
641 
642 	FSAPFS_TEST_ASSERT_IS_NULL(
643 	 "container",
644 	 container );
645 
646 	FSAPFS_TEST_ASSERT_IS_NULL(
647 	 "error",
648 	 error );
649 
650 	return( 1 );
651 
652 on_error:
653 	if( error != NULL )
654 	{
655 		libcerror_error_free(
656 		 &error );
657 	}
658 	if( container != NULL )
659 	{
660 		libfsapfs_container_free(
661 		 &container,
662 		 NULL );
663 	}
664 	return( 0 );
665 }
666 
667 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
668 
669 /* Tests the libfsapfs_container_open_file_io_handle function
670  * Returns 1 if successful or 0 if not
671  */
fsapfs_test_container_open_file_io_handle(const system_character_t * source)672 int fsapfs_test_container_open_file_io_handle(
673      const system_character_t *source )
674 {
675 	libbfio_handle_t *file_io_handle = NULL;
676 	libcerror_error_t *error         = NULL;
677 	libfsapfs_container_t *container = NULL;
678 	size_t string_length             = 0;
679 	int result                       = 0;
680 
681 	/* Initialize test
682 	 */
683 	result = libbfio_file_initialize(
684 	          &file_io_handle,
685 	          &error );
686 
687 	FSAPFS_TEST_ASSERT_EQUAL_INT(
688 	 "result",
689 	 result,
690 	 1 );
691 
692         FSAPFS_TEST_ASSERT_IS_NOT_NULL(
693          "file_io_handle",
694          file_io_handle );
695 
696         FSAPFS_TEST_ASSERT_IS_NULL(
697          "error",
698          error );
699 
700 	string_length = system_string_length(
701 	                 source );
702 
703 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
704 	result = libbfio_file_set_name_wide(
705 	          file_io_handle,
706 	          source,
707 	          string_length,
708 	          &error );
709 #else
710 	result = libbfio_file_set_name(
711 	          file_io_handle,
712 	          source,
713 	          string_length,
714 	          &error );
715 #endif
716 	FSAPFS_TEST_ASSERT_EQUAL_INT(
717 	 "result",
718 	 result,
719 	 1 );
720 
721         FSAPFS_TEST_ASSERT_IS_NULL(
722          "error",
723          error );
724 
725 	result = libfsapfs_container_initialize(
726 	          &container,
727 	          &error );
728 
729 	FSAPFS_TEST_ASSERT_EQUAL_INT(
730 	 "result",
731 	 result,
732 	 1 );
733 
734 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
735 	 "container",
736 	 container );
737 
738 	FSAPFS_TEST_ASSERT_IS_NULL(
739 	 "error",
740 	 error );
741 
742 	/* Test regular cases
743 	 */
744 	result = libfsapfs_container_open_file_io_handle(
745 	          container,
746 	          file_io_handle,
747 	          LIBFSAPFS_OPEN_READ,
748 	          &error );
749 
750 	FSAPFS_TEST_ASSERT_EQUAL_INT(
751 	 "result",
752 	 result,
753 	 1 );
754 
755 	FSAPFS_TEST_ASSERT_IS_NULL(
756 	 "error",
757 	 error );
758 
759 	/* Test error cases
760 	 */
761 	result = libfsapfs_container_open_file_io_handle(
762 	          NULL,
763 	          file_io_handle,
764 	          LIBFSAPFS_OPEN_READ,
765 	          &error );
766 
767 	FSAPFS_TEST_ASSERT_EQUAL_INT(
768 	 "result",
769 	 result,
770 	 -1 );
771 
772 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
773 	 "error",
774 	 error );
775 
776 	libcerror_error_free(
777 	 &error );
778 
779 	result = libfsapfs_container_open_file_io_handle(
780 	          container,
781 	          NULL,
782 	          LIBFSAPFS_OPEN_READ,
783 	          &error );
784 
785 	FSAPFS_TEST_ASSERT_EQUAL_INT(
786 	 "result",
787 	 result,
788 	 -1 );
789 
790 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
791 	 "error",
792 	 error );
793 
794 	libcerror_error_free(
795 	 &error );
796 
797 	result = libfsapfs_container_open_file_io_handle(
798 	          container,
799 	          file_io_handle,
800 	          -1,
801 	          &error );
802 
803 	FSAPFS_TEST_ASSERT_EQUAL_INT(
804 	 "result",
805 	 result,
806 	 -1 );
807 
808 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
809 	 "error",
810 	 error );
811 
812 	libcerror_error_free(
813 	 &error );
814 
815 	/* Test open when already opened
816 	 */
817 	result = libfsapfs_container_open_file_io_handle(
818 	          container,
819 	          file_io_handle,
820 	          LIBFSAPFS_OPEN_READ,
821 	          &error );
822 
823 	FSAPFS_TEST_ASSERT_EQUAL_INT(
824 	 "result",
825 	 result,
826 	 -1 );
827 
828 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
829 	 "error",
830 	 error );
831 
832 	libcerror_error_free(
833 	 &error );
834 
835 	/* Clean up
836 	 */
837 	result = libfsapfs_container_free(
838 	          &container,
839 	          &error );
840 
841 	FSAPFS_TEST_ASSERT_EQUAL_INT(
842 	 "result",
843 	 result,
844 	 1 );
845 
846 	FSAPFS_TEST_ASSERT_IS_NULL(
847 	 "container",
848 	 container );
849 
850 	FSAPFS_TEST_ASSERT_IS_NULL(
851 	 "error",
852 	 error );
853 
854 	result = libbfio_handle_free(
855 	          &file_io_handle,
856 	          &error );
857 
858 	FSAPFS_TEST_ASSERT_EQUAL_INT(
859 	 "result",
860 	 result,
861 	 1 );
862 
863 	FSAPFS_TEST_ASSERT_IS_NULL(
864          "file_io_handle",
865          file_io_handle );
866 
867         FSAPFS_TEST_ASSERT_IS_NULL(
868          "error",
869          error );
870 
871 	return( 1 );
872 
873 on_error:
874 	if( error != NULL )
875 	{
876 		libcerror_error_free(
877 		 &error );
878 	}
879 	if( container != NULL )
880 	{
881 		libfsapfs_container_free(
882 		 &container,
883 		 NULL );
884 	}
885 	if( file_io_handle != NULL )
886 	{
887 		libbfio_handle_free(
888 		 &file_io_handle,
889 		 NULL );
890 	}
891 	return( 0 );
892 }
893 
894 /* Tests the libfsapfs_container_close function
895  * Returns 1 if successful or 0 if not
896  */
fsapfs_test_container_close(void)897 int fsapfs_test_container_close(
898      void )
899 {
900 	libcerror_error_t *error = NULL;
901 	int result               = 0;
902 
903 	/* Test error cases
904 	 */
905 	result = libfsapfs_container_close(
906 	          NULL,
907 	          &error );
908 
909 	FSAPFS_TEST_ASSERT_EQUAL_INT(
910 	 "result",
911 	 result,
912 	 -1 );
913 
914 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
915 	 "error",
916 	 error );
917 
918 	libcerror_error_free(
919 	 &error );
920 
921 	return( 1 );
922 
923 on_error:
924 	if( error != NULL )
925 	{
926 		libcerror_error_free(
927 		 &error );
928 	}
929 	return( 0 );
930 }
931 
932 /* Tests the libfsapfs_container_open and libfsapfs_container_close functions
933  * Returns 1 if successful or 0 if not
934  */
fsapfs_test_container_open_close(const system_character_t * source)935 int fsapfs_test_container_open_close(
936      const system_character_t *source )
937 {
938 	libcerror_error_t *error         = NULL;
939 	libfsapfs_container_t *container = NULL;
940 	int result                       = 0;
941 
942 	/* Initialize test
943 	 */
944 	result = libfsapfs_container_initialize(
945 	          &container,
946 	          &error );
947 
948 	FSAPFS_TEST_ASSERT_EQUAL_INT(
949 	 "result",
950 	 result,
951 	 1 );
952 
953 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
954 	 "container",
955 	 container );
956 
957 	FSAPFS_TEST_ASSERT_IS_NULL(
958 	 "error",
959 	 error );
960 
961 	/* Test open and close
962 	 */
963 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
964 	result = libfsapfs_container_open_wide(
965 	          container,
966 	          source,
967 	          LIBFSAPFS_OPEN_READ,
968 	          &error );
969 #else
970 	result = libfsapfs_container_open(
971 	          container,
972 	          source,
973 	          LIBFSAPFS_OPEN_READ,
974 	          &error );
975 #endif
976 
977 	FSAPFS_TEST_ASSERT_EQUAL_INT(
978 	 "result",
979 	 result,
980 	 1 );
981 
982 	FSAPFS_TEST_ASSERT_IS_NULL(
983 	 "error",
984 	 error );
985 
986 	result = libfsapfs_container_close(
987 	          container,
988 	          &error );
989 
990 	FSAPFS_TEST_ASSERT_EQUAL_INT(
991 	 "result",
992 	 result,
993 	 0 );
994 
995 	FSAPFS_TEST_ASSERT_IS_NULL(
996 	 "error",
997 	 error );
998 
999 	/* Test open and close a second time to validate clean up on close
1000 	 */
1001 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1002 	result = libfsapfs_container_open_wide(
1003 	          container,
1004 	          source,
1005 	          LIBFSAPFS_OPEN_READ,
1006 	          &error );
1007 #else
1008 	result = libfsapfs_container_open(
1009 	          container,
1010 	          source,
1011 	          LIBFSAPFS_OPEN_READ,
1012 	          &error );
1013 #endif
1014 
1015 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1016 	 "result",
1017 	 result,
1018 	 1 );
1019 
1020 	FSAPFS_TEST_ASSERT_IS_NULL(
1021 	 "error",
1022 	 error );
1023 
1024 	result = libfsapfs_container_close(
1025 	          container,
1026 	          &error );
1027 
1028 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1029 	 "result",
1030 	 result,
1031 	 0 );
1032 
1033 	FSAPFS_TEST_ASSERT_IS_NULL(
1034 	 "error",
1035 	 error );
1036 
1037 	/* Clean up
1038 	 */
1039 	result = libfsapfs_container_free(
1040 	          &container,
1041 	          &error );
1042 
1043 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1044 	 "result",
1045 	 result,
1046 	 1 );
1047 
1048 	FSAPFS_TEST_ASSERT_IS_NULL(
1049 	 "container",
1050 	 container );
1051 
1052 	FSAPFS_TEST_ASSERT_IS_NULL(
1053 	 "error",
1054 	 error );
1055 
1056 	return( 1 );
1057 
1058 on_error:
1059 	if( error != NULL )
1060 	{
1061 		libcerror_error_free(
1062 		 &error );
1063 	}
1064 	if( container != NULL )
1065 	{
1066 		libfsapfs_container_free(
1067 		 &container,
1068 		 NULL );
1069 	}
1070 	return( 0 );
1071 }
1072 
1073 /* Tests the libfsapfs_container_signal_abort function
1074  * Returns 1 if successful or 0 if not
1075  */
fsapfs_test_container_signal_abort(libfsapfs_container_t * container)1076 int fsapfs_test_container_signal_abort(
1077      libfsapfs_container_t *container )
1078 {
1079 	libcerror_error_t *error = NULL;
1080 	int result               = 0;
1081 
1082 	/* Test regular cases
1083 	 */
1084 	result = libfsapfs_container_signal_abort(
1085 	          container,
1086 	          &error );
1087 
1088 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1089 	 "result",
1090 	 result,
1091 	 1 );
1092 
1093 	FSAPFS_TEST_ASSERT_IS_NULL(
1094 	 "error",
1095 	 error );
1096 
1097 	/* Test error cases
1098 	 */
1099 	result = libfsapfs_container_signal_abort(
1100 	          NULL,
1101 	          &error );
1102 
1103 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1104 	 "result",
1105 	 result,
1106 	 -1 );
1107 
1108 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1109 	 "error",
1110 	 error );
1111 
1112 	libcerror_error_free(
1113 	 &error );
1114 
1115 	return( 1 );
1116 
1117 on_error:
1118 	if( error != NULL )
1119 	{
1120 		libcerror_error_free(
1121 		 &error );
1122 	}
1123 	return( 0 );
1124 }
1125 
1126 /* Tests the libfsapfs_container_get_size function
1127  * Returns 1 if successful or 0 if not
1128  */
fsapfs_test_container_get_size(libfsapfs_container_t * container)1129 int fsapfs_test_container_get_size(
1130      libfsapfs_container_t *container )
1131 {
1132 	libcerror_error_t *error = NULL;
1133 	size64_t size            = 0;
1134 	int result               = 0;
1135 
1136 	/* Test regular cases
1137 	 */
1138 	result = libfsapfs_container_get_size(
1139 	          container,
1140 	          &size,
1141 	          &error );
1142 
1143 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1144 	 "result",
1145 	 result,
1146 	 1 );
1147 
1148 	FSAPFS_TEST_ASSERT_IS_NULL(
1149 	 "error",
1150 	 error );
1151 
1152 	/* Test error cases
1153 	 */
1154 	result = libfsapfs_container_get_size(
1155 	          NULL,
1156 	          &size,
1157 	          &error );
1158 
1159 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1160 	 "result",
1161 	 result,
1162 	 -1 );
1163 
1164 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1165 	 "error",
1166 	 error );
1167 
1168 	libcerror_error_free(
1169 	 &error );
1170 
1171 	result = libfsapfs_container_get_size(
1172 	          container,
1173 	          NULL,
1174 	          &error );
1175 
1176 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1177 	 "result",
1178 	 result,
1179 	 -1 );
1180 
1181 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1182 	 "error",
1183 	 error );
1184 
1185 	libcerror_error_free(
1186 	 &error );
1187 
1188 	return( 1 );
1189 
1190 on_error:
1191 	if( error != NULL )
1192 	{
1193 		libcerror_error_free(
1194 		 &error );
1195 	}
1196 	return( 0 );
1197 }
1198 
1199 /* Tests the libfsapfs_container_get_number_of_volumes function
1200  * Returns 1 if successful or 0 if not
1201  */
fsapfs_test_container_get_number_of_volumes(libfsapfs_container_t * container)1202 int fsapfs_test_container_get_number_of_volumes(
1203      libfsapfs_container_t *container )
1204 {
1205 	libcerror_error_t *error = NULL;
1206 	int number_of_volumes    = 0;
1207 	int result               = 0;
1208 
1209 	/* Test regular cases
1210 	 */
1211 	result = libfsapfs_container_get_number_of_volumes(
1212 	          container,
1213 	          &number_of_volumes,
1214 	          &error );
1215 
1216 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1217 	 "result",
1218 	 result,
1219 	 1 );
1220 
1221 	FSAPFS_TEST_ASSERT_IS_NULL(
1222 	 "error",
1223 	 error );
1224 
1225 	/* Test error cases
1226 	 */
1227 	result = libfsapfs_container_get_number_of_volumes(
1228 	          NULL,
1229 	          &number_of_volumes,
1230 	          &error );
1231 
1232 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1233 	 "result",
1234 	 result,
1235 	 -1 );
1236 
1237 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1238 	 "error",
1239 	 error );
1240 
1241 	libcerror_error_free(
1242 	 &error );
1243 
1244 	result = libfsapfs_container_get_number_of_volumes(
1245 	          container,
1246 	          NULL,
1247 	          &error );
1248 
1249 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1250 	 "result",
1251 	 result,
1252 	 -1 );
1253 
1254 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1255 	 "error",
1256 	 error );
1257 
1258 	libcerror_error_free(
1259 	 &error );
1260 
1261 	return( 1 );
1262 
1263 on_error:
1264 	if( error != NULL )
1265 	{
1266 		libcerror_error_free(
1267 		 &error );
1268 	}
1269 	return( 0 );
1270 }
1271 
1272 /* The main program
1273  */
1274 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc,wchar_t * const argv[])1275 int wmain(
1276      int argc,
1277      wchar_t * const argv[] )
1278 #else
1279 int main(
1280      int argc,
1281      char * const argv[] )
1282 #endif
1283 {
1284 	libbfio_handle_t *file_io_handle  = NULL;
1285 	libcerror_error_t *error          = NULL;
1286 	libfsapfs_container_t *container  = NULL;
1287 	system_character_t *option_offset = NULL;
1288 	system_character_t *source        = NULL;
1289 	system_integer_t option           = 0;
1290 	size_t string_length              = 0;
1291 	off64_t volume_offset             = 0;
1292 	int result                        = 0;
1293 
1294 	while( ( option = fsapfs_test_getopt(
1295 	                   argc,
1296 	                   argv,
1297 	                   _SYSTEM_STRING( "o:p:" ) ) ) != (system_integer_t) -1 )
1298 	{
1299 		switch( option )
1300 		{
1301 			case (system_integer_t) '?':
1302 			default:
1303 				fprintf(
1304 				 stderr,
1305 				 "Invalid argument: %" PRIs_SYSTEM ".\n",
1306 				 argv[ optind - 1 ] );
1307 
1308 				return( EXIT_FAILURE );
1309 
1310 			case (system_integer_t) 'o':
1311 				option_offset = optarg;
1312 
1313 				break;
1314 
1315 			case (system_integer_t) 'p':
1316 				break;
1317 		}
1318 	}
1319 	if( optind < argc )
1320 	{
1321 		source = argv[ optind ];
1322 	}
1323 	if( option_offset != NULL )
1324 	{
1325 		string_length = system_string_length(
1326 		                 option_offset );
1327 
1328 		result = fsapfs_test_system_string_copy_from_64_bit_in_decimal(
1329 		          option_offset,
1330 		          string_length + 1,
1331 		          (uint64_t *) &volume_offset,
1332 		          &error );
1333 
1334 		FSAPFS_TEST_ASSERT_EQUAL_INT(
1335 		 "result",
1336 		 result,
1337 		 1 );
1338 
1339 	        FSAPFS_TEST_ASSERT_IS_NULL(
1340 	         "error",
1341 	         error );
1342 	}
1343 #if defined( HAVE_DEBUG_OUTPUT ) && defined( FSAPFS_TEST_CONTAINER_VERBOSE )
1344 	libfsapfs_notify_set_verbose(
1345 	 1 );
1346 	libfsapfs_notify_set_stream(
1347 	 stderr,
1348 	 NULL );
1349 #endif
1350 
1351 	FSAPFS_TEST_RUN(
1352 	 "libfsapfs_container_initialize",
1353 	 fsapfs_test_container_initialize );
1354 
1355 	FSAPFS_TEST_RUN(
1356 	 "libfsapfs_container_free",
1357 	 fsapfs_test_container_free );
1358 
1359 #if !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 )
1360 	if( source != NULL )
1361 	{
1362 		result = libbfio_file_range_initialize(
1363 		          &file_io_handle,
1364 		          &error );
1365 
1366 		FSAPFS_TEST_ASSERT_EQUAL_INT(
1367 		 "result",
1368 		 result,
1369 		 1 );
1370 
1371 	        FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1372 	         "file_io_handle",
1373 	         file_io_handle );
1374 
1375 	        FSAPFS_TEST_ASSERT_IS_NULL(
1376 	         "error",
1377 	         error );
1378 
1379 		string_length = system_string_length(
1380 		                 source );
1381 
1382 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1383 		result = libbfio_file_range_set_name_wide(
1384 		          file_io_handle,
1385 		          source,
1386 		          string_length,
1387 		          &error );
1388 #else
1389 		result = libbfio_file_range_set_name(
1390 		          file_io_handle,
1391 		          source,
1392 		          string_length,
1393 		          &error );
1394 #endif
1395 		FSAPFS_TEST_ASSERT_EQUAL_INT(
1396 		 "result",
1397 		 result,
1398 		 1 );
1399 
1400 	        FSAPFS_TEST_ASSERT_IS_NULL(
1401 	         "error",
1402 	         error );
1403 
1404 		result = libbfio_file_range_set(
1405 		          file_io_handle,
1406 		          volume_offset,
1407 		          0,
1408 		          &error );
1409 
1410 		FSAPFS_TEST_ASSERT_EQUAL_INT(
1411 		 "result",
1412 		 result,
1413 		 1 );
1414 
1415 	        FSAPFS_TEST_ASSERT_IS_NULL(
1416 	         "error",
1417 	         error );
1418 
1419 		result = libfsapfs_check_container_signature_file_io_handle(
1420 		          file_io_handle,
1421 		          &error );
1422 
1423 		FSAPFS_TEST_ASSERT_NOT_EQUAL_INT(
1424 		 "result",
1425 		 result,
1426 		 -1 );
1427 
1428 		FSAPFS_TEST_ASSERT_IS_NULL(
1429 		 "error",
1430 		 error );
1431 	}
1432 	if( ( result != 0 )
1433 	 && ( volume_offset == 0 ) )
1434 	{
1435 		FSAPFS_TEST_RUN_WITH_ARGS(
1436 		 "libfsapfs_container_open",
1437 		 fsapfs_test_container_open,
1438 		 source );
1439 
1440 #if defined( HAVE_WIDE_CHARACTER_TYPE )
1441 
1442 		FSAPFS_TEST_RUN_WITH_ARGS(
1443 		 "libfsapfs_container_open_wide",
1444 		 fsapfs_test_container_open_wide,
1445 		 source );
1446 
1447 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
1448 
1449 		FSAPFS_TEST_RUN_WITH_ARGS(
1450 		 "libfsapfs_container_open_file_io_handle",
1451 		 fsapfs_test_container_open_file_io_handle,
1452 		 source );
1453 
1454 		FSAPFS_TEST_RUN(
1455 		 "libfsapfs_container_close",
1456 		 fsapfs_test_container_close );
1457 
1458 		FSAPFS_TEST_RUN_WITH_ARGS(
1459 		 "libfsapfs_container_open_close",
1460 		 fsapfs_test_container_open_close,
1461 		 source );
1462 	}
1463 	if( result != 0 )
1464 	{
1465 		/* Initialize test
1466 		 */
1467 		result = fsapfs_test_container_open_source(
1468 		          &container,
1469 		          file_io_handle,
1470 		          &error );
1471 
1472 		FSAPFS_TEST_ASSERT_EQUAL_INT(
1473 		 "result",
1474 		 result,
1475 		 1 );
1476 
1477 	        FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1478 	         "container",
1479 	         container );
1480 
1481 	        FSAPFS_TEST_ASSERT_IS_NULL(
1482 	         "error",
1483 	         error );
1484 
1485 		FSAPFS_TEST_RUN_WITH_ARGS(
1486 		 "libfsapfs_container_signal_abort",
1487 		 fsapfs_test_container_signal_abort,
1488 		 container );
1489 
1490 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
1491 
1492 		/* TODO: add tests for libfsapfs_internal_container_open_read */
1493 
1494 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
1495 
1496 		FSAPFS_TEST_RUN_WITH_ARGS(
1497 		 "libfsapfs_container_get_size",
1498 		 fsapfs_test_container_get_size,
1499 		 container );
1500 
1501 		/* TODO: add tests for libfsapfs_container_get_identifier */
1502 
1503 		FSAPFS_TEST_RUN_WITH_ARGS(
1504 		 "libfsapfs_container_get_number_of_volumes",
1505 		 fsapfs_test_container_get_number_of_volumes,
1506 		 container );
1507 
1508 		/* TODO: add tests for libfsapfs_container_get_volume_by_index */
1509 
1510 		/* Clean up
1511 		 */
1512 		result = fsapfs_test_container_close_source(
1513 		          &container,
1514 		          &error );
1515 
1516 		FSAPFS_TEST_ASSERT_EQUAL_INT(
1517 		 "result",
1518 		 result,
1519 		 0 );
1520 
1521 		FSAPFS_TEST_ASSERT_IS_NULL(
1522 	         "container",
1523 	         container );
1524 
1525 	        FSAPFS_TEST_ASSERT_IS_NULL(
1526 	         "error",
1527 	         error );
1528 
1529 		result = libbfio_handle_free(
1530 		          &file_io_handle,
1531 		          &error );
1532 
1533 		FSAPFS_TEST_ASSERT_EQUAL_INT(
1534 		 "result",
1535 		 result,
1536 		 1 );
1537 
1538 		FSAPFS_TEST_ASSERT_IS_NULL(
1539 	         "file_io_handle",
1540 	         file_io_handle );
1541 
1542 	        FSAPFS_TEST_ASSERT_IS_NULL(
1543 	         "error",
1544 	         error );
1545 	}
1546 #endif /* !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 ) */
1547 
1548 	return( EXIT_SUCCESS );
1549 
1550 on_error:
1551 	if( error != NULL )
1552 	{
1553 		libcerror_error_free(
1554 		 &error );
1555 	}
1556 	if( container != NULL )
1557 	{
1558 		fsapfs_test_container_close_source(
1559 		 &container,
1560 		 NULL );
1561 	}
1562 	if( file_io_handle != NULL )
1563 	{
1564 		libbfio_handle_free(
1565 		 &file_io_handle,
1566 		 NULL );
1567 	}
1568 	return( EXIT_FAILURE );
1569 }
1570 
1571