1 /*
2  * Library data_block_data_handle 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 <memory.h>
25 #include <types.h>
26 
27 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
28 #include <stdlib.h>
29 #endif
30 
31 #include "fsapfs_test_functions.h"
32 #include "fsapfs_test_libbfio.h"
33 #include "fsapfs_test_libcdata.h"
34 #include "fsapfs_test_libcerror.h"
35 #include "fsapfs_test_libfsapfs.h"
36 #include "fsapfs_test_macros.h"
37 #include "fsapfs_test_memory.h"
38 #include "fsapfs_test_unused.h"
39 
40 #include "../libfsapfs/libfsapfs_data_block_data_handle.h"
41 #include "../libfsapfs/libfsapfs_file_extent.h"
42 #include "../libfsapfs/libfsapfs_io_handle.h"
43 
44 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
45 
46 /* Tests the libfsapfs_data_block_data_handle_initialize function
47  * Returns 1 if successful or 0 if not
48  */
fsapfs_test_data_block_data_handle_initialize(void)49 int fsapfs_test_data_block_data_handle_initialize(
50      void )
51 {
52 	libcdata_array_t *file_extents                             = NULL;
53 	libcerror_error_t *error                                   = NULL;
54 	libfsapfs_data_block_data_handle_t *data_block_data_handle = NULL;
55 	libfsapfs_file_extent_t *file_extent                       = NULL;
56 	libfsapfs_io_handle_t *io_handle                           = NULL;
57 	int entry_index                                            = 0;
58 	int result                                                 = 0;
59 
60 #if defined( HAVE_FSAPFS_TEST_MEMORY )
61 	int number_of_malloc_fail_tests                            = 1;
62 	int number_of_memset_fail_tests                            = 1;
63 	int test_number                                            = 0;
64 #endif
65 
66 	/* Initialize test
67 	 */
68 	result = libfsapfs_io_handle_initialize(
69 	          &io_handle,
70 	          &error );
71 
72 	FSAPFS_TEST_ASSERT_EQUAL_INT(
73 	 "result",
74 	 result,
75 	 1 );
76 
77 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
78 	 "io_handle",
79 	 io_handle );
80 
81 	FSAPFS_TEST_ASSERT_IS_NULL(
82 	 "error",
83 	 error );
84 
85 	io_handle->block_size = 4096;
86 
87 	result = libcdata_array_initialize(
88 	          &file_extents,
89 	          0,
90 	          &error );
91 
92 	FSAPFS_TEST_ASSERT_EQUAL_INT(
93 	 "result",
94 	 result,
95 	 1 );
96 
97 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
98 	 "file_extents",
99 	 file_extents );
100 
101 	FSAPFS_TEST_ASSERT_IS_NULL(
102 	 "error",
103 	 error );
104 
105 	result = libfsapfs_file_extent_initialize(
106 	          &file_extent,
107 	          &error );
108 
109 	FSAPFS_TEST_ASSERT_EQUAL_INT(
110 	 "result",
111 	 result,
112 	 1 );
113 
114 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
115 	 "file_extent",
116 	 file_extent );
117 
118 	FSAPFS_TEST_ASSERT_IS_NULL(
119 	 "error",
120 	 error );
121 
122 	file_extent->physical_block_number = 1;
123 	file_extent->data_size             = 4096;
124 
125 	result = libcdata_array_append_entry(
126 	          file_extents,
127 	          &entry_index,
128 	          (intptr_t *) file_extent,
129 	          &error );
130 
131 	FSAPFS_TEST_ASSERT_EQUAL_INT(
132 	 "result",
133 	 result,
134 	 1 );
135 
136 	FSAPFS_TEST_ASSERT_IS_NULL(
137 	 "error",
138 	 error );
139 
140 	file_extent = NULL;
141 
142 	/* Test regular cases
143 	 */
144 	result = libfsapfs_data_block_data_handle_initialize(
145 	          &data_block_data_handle,
146 	          io_handle,
147 	          NULL,
148 	          file_extents,
149 	          0,
150 	          &error );
151 
152 	FSAPFS_TEST_ASSERT_EQUAL_INT(
153 	 "result",
154 	 result,
155 	 1 );
156 
157 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
158 	 "data_block_data_handle",
159 	 data_block_data_handle );
160 
161 	FSAPFS_TEST_ASSERT_IS_NULL(
162 	 "error",
163 	 error );
164 
165 	result = libfsapfs_data_block_data_handle_free(
166 	          &data_block_data_handle,
167 	          &error );
168 
169 	FSAPFS_TEST_ASSERT_EQUAL_INT(
170 	 "result",
171 	 result,
172 	 1 );
173 
174 	FSAPFS_TEST_ASSERT_IS_NULL(
175 	 "data_block_data_handle",
176 	 data_block_data_handle );
177 
178 	FSAPFS_TEST_ASSERT_IS_NULL(
179 	 "error",
180 	 error );
181 
182 	/* Test error cases
183 	 */
184 	result = libfsapfs_data_block_data_handle_initialize(
185 	          NULL,
186 	          io_handle,
187 	          NULL,
188 	          file_extents,
189 	          0,
190 	          &error );
191 
192 	FSAPFS_TEST_ASSERT_EQUAL_INT(
193 	 "result",
194 	 result,
195 	 -1 );
196 
197 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
198 	 "error",
199 	 error );
200 
201 	libcerror_error_free(
202 	 &error );
203 
204 	data_block_data_handle = (libfsapfs_data_block_data_handle_t *) 0x12345678UL;
205 
206 	result = libfsapfs_data_block_data_handle_initialize(
207 	          &data_block_data_handle,
208 	          io_handle,
209 	          NULL,
210 	          file_extents,
211 	          0,
212 	          &error );
213 
214 	data_block_data_handle = NULL;
215 
216 	FSAPFS_TEST_ASSERT_EQUAL_INT(
217 	 "result",
218 	 result,
219 	 -1 );
220 
221 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
222 	 "error",
223 	 error );
224 
225 	libcerror_error_free(
226 	 &error );
227 
228 	result = libfsapfs_data_block_data_handle_initialize(
229 	          &data_block_data_handle,
230 	          NULL,
231 	          NULL,
232 	          file_extents,
233 	          0,
234 	          &error );
235 
236 	FSAPFS_TEST_ASSERT_EQUAL_INT(
237 	 "result",
238 	 result,
239 	 -1 );
240 
241 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
242 	 "error",
243 	 error );
244 
245 	libcerror_error_free(
246 	 &error );
247 
248 	result = libfsapfs_data_block_data_handle_initialize(
249 	          &data_block_data_handle,
250 	          io_handle,
251 	          NULL,
252 	          NULL,
253 	          0,
254 	          &error );
255 
256 	FSAPFS_TEST_ASSERT_EQUAL_INT(
257 	 "result",
258 	 result,
259 	 -1 );
260 
261 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
262 	 "error",
263 	 error );
264 
265 	libcerror_error_free(
266 	 &error );
267 
268 #if defined( HAVE_FSAPFS_TEST_MEMORY )
269 
270 	for( test_number = 0;
271 	     test_number < number_of_malloc_fail_tests;
272 	     test_number++ )
273 	{
274 		/* Test libfsapfs_data_block_data_handle_initialize with malloc failing
275 		 */
276 		fsapfs_test_malloc_attempts_before_fail = test_number;
277 
278 		result = libfsapfs_data_block_data_handle_initialize(
279 		          &data_block_data_handle,
280 		          io_handle,
281 		          NULL,
282 		          file_extents,
283 		          0,
284 		          &error );
285 
286 		if( fsapfs_test_malloc_attempts_before_fail != -1 )
287 		{
288 			fsapfs_test_malloc_attempts_before_fail = -1;
289 
290 			if( data_block_data_handle != NULL )
291 			{
292 				libfsapfs_data_block_data_handle_free(
293 				 &data_block_data_handle,
294 				 NULL );
295 			}
296 		}
297 		else
298 		{
299 			FSAPFS_TEST_ASSERT_EQUAL_INT(
300 			 "result",
301 			 result,
302 			 -1 );
303 
304 			FSAPFS_TEST_ASSERT_IS_NULL(
305 			 "data_block_data_handle",
306 			 data_block_data_handle );
307 
308 			FSAPFS_TEST_ASSERT_IS_NOT_NULL(
309 			 "error",
310 			 error );
311 
312 			libcerror_error_free(
313 			 &error );
314 		}
315 	}
316 	for( test_number = 0;
317 	     test_number < number_of_memset_fail_tests;
318 	     test_number++ )
319 	{
320 		/* Test libfsapfs_data_block_data_handle_initialize with memset failing
321 		 */
322 		fsapfs_test_memset_attempts_before_fail = test_number;
323 
324 		result = libfsapfs_data_block_data_handle_initialize(
325 		          &data_block_data_handle,
326 		          io_handle,
327 		          NULL,
328 		          file_extents,
329 		          0,
330 		          &error );
331 
332 		if( fsapfs_test_memset_attempts_before_fail != -1 )
333 		{
334 			fsapfs_test_memset_attempts_before_fail = -1;
335 
336 			if( data_block_data_handle != NULL )
337 			{
338 				libfsapfs_data_block_data_handle_free(
339 				 &data_block_data_handle,
340 				 NULL );
341 			}
342 		}
343 		else
344 		{
345 			FSAPFS_TEST_ASSERT_EQUAL_INT(
346 			 "result",
347 			 result,
348 			 -1 );
349 
350 			FSAPFS_TEST_ASSERT_IS_NULL(
351 			 "data_block_data_handle",
352 			 data_block_data_handle );
353 
354 			FSAPFS_TEST_ASSERT_IS_NOT_NULL(
355 			 "error",
356 			 error );
357 
358 			libcerror_error_free(
359 			 &error );
360 		}
361 	}
362 #endif /* defined( HAVE_FSAPFS_TEST_MEMORY ) */
363 
364 	/* Clean up
365 	 */
366 	result = libcdata_array_free(
367 	          &file_extents,
368 	          (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_extent_free,
369 	          &error );
370 
371 	FSAPFS_TEST_ASSERT_EQUAL_INT(
372 	 "result",
373 	 result,
374 	 1 );
375 
376 	FSAPFS_TEST_ASSERT_IS_NULL(
377 	 "file_extents",
378 	 file_extents );
379 
380 	FSAPFS_TEST_ASSERT_IS_NULL(
381 	 "error",
382 	 error );
383 
384 	result = libfsapfs_io_handle_free(
385 	          &io_handle,
386 	          &error );
387 
388 	FSAPFS_TEST_ASSERT_EQUAL_INT(
389 	 "result",
390 	 result,
391 	 1 );
392 
393 	FSAPFS_TEST_ASSERT_IS_NULL(
394 	 "io_handle",
395 	 io_handle );
396 
397 	FSAPFS_TEST_ASSERT_IS_NULL(
398 	 "error",
399 	 error );
400 
401 	return( 1 );
402 
403 on_error:
404 	if( error != NULL )
405 	{
406 		libcerror_error_free(
407 		 &error );
408 	}
409 	if( data_block_data_handle != NULL )
410 	{
411 		libfsapfs_data_block_data_handle_free(
412 		 &data_block_data_handle,
413 		 NULL );
414 	}
415 	if( file_extent != NULL )
416 	{
417 		libfsapfs_file_extent_free(
418 		 &file_extent,
419 		 NULL );
420 	}
421 	if( file_extents != NULL )
422 	{
423 		libcdata_array_free(
424 		 &file_extents,
425 		 (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_extent_free,
426 		 NULL );
427 	}
428 	if( io_handle != NULL )
429 	{
430 		libfsapfs_io_handle_free(
431 		 &io_handle,
432 		 NULL );
433 	}
434 	return( 0 );
435 }
436 
437 /* Tests the libfsapfs_data_block_data_handle_free function
438  * Returns 1 if successful or 0 if not
439  */
fsapfs_test_data_block_data_handle_free(void)440 int fsapfs_test_data_block_data_handle_free(
441      void )
442 {
443 	libcerror_error_t *error = NULL;
444 	int result               = 0;
445 
446 	/* Test error cases
447 	 */
448 	result = libfsapfs_data_block_data_handle_free(
449 	          NULL,
450 	          &error );
451 
452 	FSAPFS_TEST_ASSERT_EQUAL_INT(
453 	 "result",
454 	 result,
455 	 -1 );
456 
457 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
458 	 "error",
459 	 error );
460 
461 	libcerror_error_free(
462 	 &error );
463 
464 	return( 1 );
465 
466 on_error:
467 	if( error != NULL )
468 	{
469 		libcerror_error_free(
470 		 &error );
471 	}
472 	return( 0 );
473 }
474 
475 /* Tests the libfsapfs_data_block_data_handle_read_segment_data function
476  * Returns 1 if successful or 0 if not
477  */
fsapfs_test_data_block_data_handle_read_segment_data(void)478 int fsapfs_test_data_block_data_handle_read_segment_data(
479      void )
480 {
481 	uint8_t segment_data[ 16 ];
482 
483 	uint8_t expected_segment_data[ 16 ] = {
484 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
485 
486 	libbfio_handle_t *file_io_handle                           = NULL;
487 	libcdata_array_t *file_extents                             = NULL;
488 	libcerror_error_t *error                                   = NULL;
489 	libfsapfs_data_block_data_handle_t *data_block_data_handle = NULL;
490 	libfsapfs_file_extent_t *file_extent                       = NULL;
491 	libfsapfs_io_handle_t *io_handle                           = NULL;
492 	uint8_t *data_block_data                                   = NULL;
493 	size_t data_offset                                         = 0;
494 	ssize_t read_count                                         = 0;
495 	off64_t offset                                             = 0;
496 	uint8_t byte_value                                         = 0;
497 	int entry_index                                            = 0;
498 	int result                                                 = 0;
499 
500 	/* Initialize test
501 	 */
502 	result = libfsapfs_io_handle_initialize(
503 	          &io_handle,
504 	          &error );
505 
506 	FSAPFS_TEST_ASSERT_EQUAL_INT(
507 	 "result",
508 	 result,
509 	 1 );
510 
511 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
512 	 "io_handle",
513 	 io_handle );
514 
515 	FSAPFS_TEST_ASSERT_IS_NULL(
516 	 "error",
517 	 error );
518 
519 	io_handle->block_size = 4096;
520 
521 	result = libcdata_array_initialize(
522 	          &file_extents,
523 	          0,
524 	          &error );
525 
526 	FSAPFS_TEST_ASSERT_EQUAL_INT(
527 	 "result",
528 	 result,
529 	 1 );
530 
531 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
532 	 "file_extents",
533 	 file_extents );
534 
535 	FSAPFS_TEST_ASSERT_IS_NULL(
536 	 "error",
537 	 error );
538 
539 	result = libfsapfs_file_extent_initialize(
540 	          &file_extent,
541 	          &error );
542 
543 	FSAPFS_TEST_ASSERT_EQUAL_INT(
544 	 "result",
545 	 result,
546 	 1 );
547 
548 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
549 	 "file_extent",
550 	 file_extent );
551 
552 	FSAPFS_TEST_ASSERT_IS_NULL(
553 	 "error",
554 	 error );
555 
556 	file_extent->physical_block_number = 1;
557 	file_extent->data_size             = 4096;
558 
559 	result = libcdata_array_append_entry(
560 	          file_extents,
561 	          &entry_index,
562 	          (intptr_t *) file_extent,
563 	          &error );
564 
565 	FSAPFS_TEST_ASSERT_EQUAL_INT(
566 	 "result",
567 	 result,
568 	 1 );
569 
570 	FSAPFS_TEST_ASSERT_IS_NULL(
571 	 "error",
572 	 error );
573 
574 	file_extent = NULL;
575 
576 	result = libfsapfs_data_block_data_handle_initialize(
577 	          &data_block_data_handle,
578 	          io_handle,
579 	          NULL,
580 	          file_extents,
581 	          0,
582 	          &error );
583 
584 	FSAPFS_TEST_ASSERT_EQUAL_INT(
585 	 "result",
586 	 result,
587 	 1 );
588 
589 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
590 	 "data_block_data_handle",
591 	 data_block_data_handle );
592 
593 	FSAPFS_TEST_ASSERT_IS_NULL(
594 	 "error",
595 	 error );
596 
597 	/* Initialize file IO handle
598 	 */
599 	data_block_data = (uint8_t *) memory_allocate(
600 	                               8192 );
601 
602 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
603 	 "data_block_data",
604 	 data_block_data );
605 
606 	for( data_offset = 0;
607 	     data_offset < 8192;
608 	     data_offset++ )
609 	{
610 		data_block_data[ data_offset ] = byte_value;
611 
612 		if( byte_value < 15 )
613 		{
614 			byte_value++;
615 		}
616 		else
617 		{
618 			byte_value = 0;
619 		}
620 	}
621 	result = fsapfs_test_open_file_io_handle(
622 	          &file_io_handle,
623 	          data_block_data,
624 	          8192,
625 	          &error );
626 
627 	FSAPFS_TEST_ASSERT_EQUAL_INT(
628 	 "result",
629 	 result,
630 	 1 );
631 
632 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
633 	 "file_io_handle",
634 	 file_io_handle );
635 
636 	FSAPFS_TEST_ASSERT_IS_NULL(
637 	 "error",
638 	 error );
639 
640 	/* Test regular cases
641 	 */
642 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
643 	              data_block_data_handle,
644 	              file_io_handle,
645 	              0,
646 	              0,
647 	              segment_data,
648 	              12,
649 	              0,
650 	              0,
651 	              &error );
652 
653 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
654 	 "read_count",
655 	 read_count,
656 	 (ssize_t) 12 );
657 
658 	FSAPFS_TEST_ASSERT_IS_NULL(
659 	 "error",
660 	 error );
661 
662 	result = memory_compare(
663 	          segment_data,
664 	          expected_segment_data,
665 	          12 );
666 
667 	FSAPFS_TEST_ASSERT_EQUAL_INT(
668 	 "result",
669 	 result,
670 	 0 );
671 
672 	/* Read buffer on last segment boundary
673 	 */
674 	offset = libfsapfs_data_block_data_handle_seek_segment_offset(
675 	          data_block_data_handle,
676 	          NULL,
677 	          0,
678 	          0,
679 	          4096 - 4,
680 	          &error );
681 
682 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
683 	 "offset",
684 	 offset,
685 	 (int64_t) 4096 - 4 );
686 
687 	FSAPFS_TEST_ASSERT_IS_NULL(
688 	 "error",
689 	 error );
690 
691 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
692 	              data_block_data_handle,
693 	              file_io_handle,
694 	              0,
695 	              0,
696 	              segment_data,
697 	              8,
698 	              0,
699 	              0,
700 	              &error );
701 
702 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
703 	 "read_count",
704 	 read_count,
705 	 (ssize_t) 4 );
706 
707 	FSAPFS_TEST_ASSERT_IS_NULL(
708 	 "error",
709 	 error );
710 
711 	result = memory_compare(
712 	          segment_data,
713 	          &( expected_segment_data[ 12 ] ),
714 	          4 );
715 
716 	FSAPFS_TEST_ASSERT_EQUAL_INT(
717 	 "result",
718 	 result,
719 	 0 );
720 
721 	/* Read buffer beyond last segment
722 	 */
723 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
724 	              data_block_data_handle,
725 	              file_io_handle,
726 	              0,
727 	              0,
728 	              segment_data,
729 	              16,
730 	              0,
731 	              0,
732 	              &error );
733 
734 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
735 	 "read_count",
736 	 read_count,
737 	 (ssize_t) 0 );
738 
739 	FSAPFS_TEST_ASSERT_IS_NULL(
740 	 "error",
741 	 error );
742 
743 	/* Clean up
744 	 */
745 	result = libfsapfs_data_block_data_handle_free(
746 	          &data_block_data_handle,
747 	          &error );
748 
749 	FSAPFS_TEST_ASSERT_EQUAL_INT(
750 	 "result",
751 	 result,
752 	 1 );
753 
754 	FSAPFS_TEST_ASSERT_IS_NULL(
755 	 "data_block_data_handle",
756 	 data_block_data_handle );
757 
758 	FSAPFS_TEST_ASSERT_IS_NULL(
759 	 "error",
760 	 error );
761 
762 	/* Create a new data block data handle to prevent the data block cache
763 	 * affecting the tests.
764 	 */
765 	result = libfsapfs_data_block_data_handle_initialize(
766 	          &data_block_data_handle,
767 	          io_handle,
768 	          NULL,
769 	          file_extents,
770 	          0,
771 	          &error );
772 
773 	FSAPFS_TEST_ASSERT_EQUAL_INT(
774 	 "result",
775 	 result,
776 	 1 );
777 
778 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
779 	 "data_block_data_handle",
780 	 data_block_data_handle );
781 
782 	FSAPFS_TEST_ASSERT_IS_NULL(
783 	 "error",
784 	 error );
785 
786 	/* Test error cases
787 	 */
788 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
789 	              NULL,
790 	              file_io_handle,
791 	              0,
792 	              0,
793 	              segment_data,
794 	              16,
795 	              0,
796 	              0,
797 	              &error );
798 
799 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
800 	 "read_count",
801 	 read_count,
802 	 (ssize_t) -1 );
803 
804 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
805 	 "error",
806 	 error );
807 
808 	libcerror_error_free(
809 	 &error );
810 
811 	data_block_data_handle->current_offset = -1;
812 
813 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
814 	              data_block_data_handle,
815 	              file_io_handle,
816 	              0,
817 	              0,
818 	              segment_data,
819 	              16,
820 	              0,
821 	              0,
822 	              &error );
823 
824 	data_block_data_handle->current_offset = 0;
825 
826 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
827 	 "read_count",
828 	 read_count,
829 	 (ssize_t) -1 );
830 
831 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
832 	 "error",
833 	 error );
834 
835 	libcerror_error_free(
836 	 &error );
837 
838 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
839 	              data_block_data_handle,
840 	              NULL,
841 	              0,
842 	              0,
843 	              segment_data,
844 	              16,
845 	              0,
846 	              0,
847 	              &error );
848 
849 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
850 	 "read_count",
851 	 read_count,
852 	 (ssize_t) -1 );
853 
854 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
855 	 "error",
856 	 error );
857 
858 	libcerror_error_free(
859 	 &error );
860 
861 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
862 	              data_block_data_handle,
863 	              file_io_handle,
864 	              -1,
865 	              0,
866 	              segment_data,
867 	              16,
868 	              0,
869 	              0,
870 	              &error );
871 
872 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
873 	 "read_count",
874 	 read_count,
875 	 (ssize_t) -1 );
876 
877 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
878 	 "error",
879 	 error );
880 
881 	libcerror_error_free(
882 	 &error );
883 
884 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
885 	              data_block_data_handle,
886 	              file_io_handle,
887 	              0,
888 	              0,
889 	              NULL,
890 	              16,
891 	              0,
892 	              0,
893 	              &error );
894 
895 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
896 	 "read_count",
897 	 read_count,
898 	 (ssize_t) -1 );
899 
900 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
901 	 "error",
902 	 error );
903 
904 	libcerror_error_free(
905 	 &error );
906 
907 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
908 	              data_block_data_handle,
909 	              file_io_handle,
910 	              0,
911 	              0,
912 	              segment_data,
913 	              (size_t) SSIZE_MAX + 1,
914 	              0,
915 	              0,
916 	              &error );
917 
918 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
919 	 "read_count",
920 	 read_count,
921 	 (ssize_t) -1 );
922 
923 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
924 	 "error",
925 	 error );
926 
927 	libcerror_error_free(
928 	 &error );
929 
930 #if defined( HAVE_FSAPFS_TEST_MEMORY )
931 #if defined( OPTIMIZATION_DISABLED )
932 	/* Test libfsapfs_data_block_data_handle_read_segment_data with memcpy failing
933 	 */
934 	fsapfs_test_memcpy_attempts_before_fail = 0;
935 
936 	read_count = libfsapfs_data_block_data_handle_read_segment_data(
937 	              data_block_data_handle,
938 	              file_io_handle,
939 	              0,
940 	              0,
941 	              segment_data,
942 	              12,
943 	              0,
944 	              0,
945 	              &error );
946 
947 	if( fsapfs_test_memcpy_attempts_before_fail != -1 )
948 	{
949 		fsapfs_test_memcpy_attempts_before_fail = -1;
950 	}
951 	else
952 	{
953 		FSAPFS_TEST_ASSERT_EQUAL_INT64(
954 		 "read_count",
955 		 read_count,
956 		 (ssize_t) -1 );
957 
958 		FSAPFS_TEST_ASSERT_IS_NOT_NULL(
959 		 "error",
960 		 error );
961 
962 		libcerror_error_free(
963 		 &error );
964 	}
965 #endif /* defined( OPTIMIZATION_DISABLED ) */
966 #endif /* defined( HAVE_FSAPFS_TEST_MEMORY ) */
967 
968 	/* Clean up file IO handle
969 	 */
970 	result = libbfio_handle_free(
971 	          &file_io_handle,
972 	          &error );
973 
974 	FSAPFS_TEST_ASSERT_EQUAL_INT(
975 	 "result",
976 	 result,
977 	 1 );
978 
979 	FSAPFS_TEST_ASSERT_IS_NULL(
980 	 "file_io_handle",
981 	 file_io_handle );
982 
983 	FSAPFS_TEST_ASSERT_IS_NULL(
984 	 "error",
985 	 error );
986 
987 	memory_free(
988 	 data_block_data );
989 
990 	data_block_data = NULL;
991 
992 	/* Clean up
993 	 */
994 	result = libfsapfs_data_block_data_handle_free(
995 	          &data_block_data_handle,
996 	          &error );
997 
998 	FSAPFS_TEST_ASSERT_EQUAL_INT(
999 	 "result",
1000 	 result,
1001 	 1 );
1002 
1003 	FSAPFS_TEST_ASSERT_IS_NULL(
1004 	 "data_block_data_handle",
1005 	 data_block_data_handle );
1006 
1007 	FSAPFS_TEST_ASSERT_IS_NULL(
1008 	 "error",
1009 	 error );
1010 
1011 	result = libcdata_array_free(
1012 	          &file_extents,
1013 	          (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_extent_free,
1014 	          &error );
1015 
1016 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1017 	 "result",
1018 	 result,
1019 	 1 );
1020 
1021 	FSAPFS_TEST_ASSERT_IS_NULL(
1022 	 "file_extents",
1023 	 file_extents );
1024 
1025 	FSAPFS_TEST_ASSERT_IS_NULL(
1026 	 "error",
1027 	 error );
1028 
1029 	result = libfsapfs_io_handle_free(
1030 	          &io_handle,
1031 	          &error );
1032 
1033 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1034 	 "result",
1035 	 result,
1036 	 1 );
1037 
1038 	FSAPFS_TEST_ASSERT_IS_NULL(
1039 	 "io_handle",
1040 	 io_handle );
1041 
1042 	FSAPFS_TEST_ASSERT_IS_NULL(
1043 	 "error",
1044 	 error );
1045 
1046 	return( 1 );
1047 
1048 on_error:
1049 	if( error != NULL )
1050 	{
1051 		libcerror_error_free(
1052 		 &error );
1053 	}
1054 	if( file_io_handle != NULL )
1055 	{
1056 		libbfio_handle_free(
1057 		 &file_io_handle,
1058 		 NULL );
1059 	}
1060 	if( data_block_data != NULL )
1061 	{
1062 		memory_free(
1063 		 data_block_data );
1064 	}
1065 	if( data_block_data_handle != NULL )
1066 	{
1067 		libfsapfs_data_block_data_handle_free(
1068 		 &data_block_data_handle,
1069 		 NULL );
1070 	}
1071 	if( file_extent != NULL )
1072 	{
1073 		libfsapfs_file_extent_free(
1074 		 &file_extent,
1075 		 NULL );
1076 	}
1077 	if( file_extents != NULL )
1078 	{
1079 		libcdata_array_free(
1080 		 &file_extents,
1081 		 (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_extent_free,
1082 		 NULL );
1083 	}
1084 	if( io_handle != NULL )
1085 	{
1086 		libfsapfs_io_handle_free(
1087 		 &io_handle,
1088 		 NULL );
1089 	}
1090 	return( 0 );
1091 }
1092 
1093 /* Tests the libfsapfs_data_block_data_handle_seek_segment_offset function
1094  * Returns 1 if successful or 0 if not
1095  */
fsapfs_test_data_block_data_handle_seek_segment_offset(void)1096 int fsapfs_test_data_block_data_handle_seek_segment_offset(
1097      void )
1098 {
1099 	libcdata_array_t *file_extents                             = NULL;
1100 	libcerror_error_t *error                                   = NULL;
1101 	libfsapfs_data_block_data_handle_t *data_block_data_handle = NULL;
1102 	libfsapfs_file_extent_t *file_extent                       = NULL;
1103 	libfsapfs_io_handle_t *io_handle                           = NULL;
1104 	off64_t offset                                             = 0;
1105 	int entry_index                                            = 0;
1106 	int result                                                 = 0;
1107 
1108 	/* Initialize test
1109 	 */
1110 	result = libfsapfs_io_handle_initialize(
1111 	          &io_handle,
1112 	          &error );
1113 
1114 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1115 	 "result",
1116 	 result,
1117 	 1 );
1118 
1119 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1120 	 "io_handle",
1121 	 io_handle );
1122 
1123 	FSAPFS_TEST_ASSERT_IS_NULL(
1124 	 "error",
1125 	 error );
1126 
1127 	io_handle->block_size = 4096;
1128 
1129 	result = libcdata_array_initialize(
1130 	          &file_extents,
1131 	          0,
1132 	          &error );
1133 
1134 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1135 	 "result",
1136 	 result,
1137 	 1 );
1138 
1139 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1140 	 "file_extents",
1141 	 file_extents );
1142 
1143 	FSAPFS_TEST_ASSERT_IS_NULL(
1144 	 "error",
1145 	 error );
1146 
1147 	result = libfsapfs_file_extent_initialize(
1148 	          &file_extent,
1149 	          &error );
1150 
1151 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1152 	 "result",
1153 	 result,
1154 	 1 );
1155 
1156 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1157 	 "file_extent",
1158 	 file_extent );
1159 
1160 	FSAPFS_TEST_ASSERT_IS_NULL(
1161 	 "error",
1162 	 error );
1163 
1164 	file_extent->physical_block_number = 1;
1165 	file_extent->data_size             = 4096;
1166 
1167 	result = libcdata_array_append_entry(
1168 	          file_extents,
1169 	          &entry_index,
1170 	          (intptr_t *) file_extent,
1171 	          &error );
1172 
1173 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1174 	 "result",
1175 	 result,
1176 	 1 );
1177 
1178 	FSAPFS_TEST_ASSERT_IS_NULL(
1179 	 "error",
1180 	 error );
1181 
1182 	file_extent = NULL;
1183 
1184 	result = libfsapfs_data_block_data_handle_initialize(
1185 	          &data_block_data_handle,
1186 	          io_handle,
1187 	          NULL,
1188 	          file_extents,
1189 	          0,
1190 	          &error );
1191 
1192 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1193 	 "result",
1194 	 result,
1195 	 1 );
1196 
1197 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1198 	 "data_block_data_handle",
1199 	 data_block_data_handle );
1200 
1201 	FSAPFS_TEST_ASSERT_IS_NULL(
1202 	 "error",
1203 	 error );
1204 
1205 	/* Test regular cases
1206 	 */
1207 	offset = libfsapfs_data_block_data_handle_seek_segment_offset(
1208 	          data_block_data_handle,
1209 	          NULL,
1210 	          0,
1211 	          0,
1212 	          0,
1213 	          &error );
1214 
1215 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
1216 	 "offset",
1217 	 offset,
1218 	 (int64_t) 0 );
1219 
1220 	FSAPFS_TEST_ASSERT_IS_NULL(
1221 	 "error",
1222 	 error );
1223 
1224 	/* Test error cases
1225 	 */
1226 	offset = libfsapfs_data_block_data_handle_seek_segment_offset(
1227 	          NULL,
1228 	          NULL,
1229 	          0,
1230 	          0,
1231 	          0,
1232 	          &error );
1233 
1234 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
1235 	 "offset",
1236 	 offset,
1237 	 (int64_t) -1 );
1238 
1239 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1240 	 "error",
1241 	 error );
1242 
1243 	libcerror_error_free(
1244 	 &error );
1245 
1246 	offset = libfsapfs_data_block_data_handle_seek_segment_offset(
1247 	          data_block_data_handle,
1248 	          NULL,
1249 	          -1,
1250 	          0,
1251 	          0,
1252 	          &error );
1253 
1254 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
1255 	 "offset",
1256 	 offset,
1257 	 (int64_t) -1 );
1258 
1259 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1260 	 "error",
1261 	 error );
1262 
1263 	libcerror_error_free(
1264 	 &error );
1265 
1266 	offset = libfsapfs_data_block_data_handle_seek_segment_offset(
1267 	          data_block_data_handle,
1268 	          NULL,
1269 	          0,
1270 	          0,
1271 	          -1,
1272 	          &error );
1273 
1274 	FSAPFS_TEST_ASSERT_EQUAL_INT64(
1275 	 "offset",
1276 	 offset,
1277 	 (int64_t) -1 );
1278 
1279 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
1280 	 "error",
1281 	 error );
1282 
1283 	libcerror_error_free(
1284 	 &error );
1285 
1286 	/* Clean up
1287 	 */
1288 	result = libfsapfs_data_block_data_handle_free(
1289 	          &data_block_data_handle,
1290 	          &error );
1291 
1292 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1293 	 "result",
1294 	 result,
1295 	 1 );
1296 
1297 	FSAPFS_TEST_ASSERT_IS_NULL(
1298 	 "data_block_data_handle",
1299 	 data_block_data_handle );
1300 
1301 	FSAPFS_TEST_ASSERT_IS_NULL(
1302 	 "error",
1303 	 error );
1304 
1305 	result = libcdata_array_free(
1306 	          &file_extents,
1307 	          (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_extent_free,
1308 	          &error );
1309 
1310 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1311 	 "result",
1312 	 result,
1313 	 1 );
1314 
1315 	FSAPFS_TEST_ASSERT_IS_NULL(
1316 	 "file_extents",
1317 	 file_extents );
1318 
1319 	FSAPFS_TEST_ASSERT_IS_NULL(
1320 	 "error",
1321 	 error );
1322 
1323 	result = libfsapfs_io_handle_free(
1324 	          &io_handle,
1325 	          &error );
1326 
1327 	FSAPFS_TEST_ASSERT_EQUAL_INT(
1328 	 "result",
1329 	 result,
1330 	 1 );
1331 
1332 	FSAPFS_TEST_ASSERT_IS_NULL(
1333 	 "io_handle",
1334 	 io_handle );
1335 
1336 	FSAPFS_TEST_ASSERT_IS_NULL(
1337 	 "error",
1338 	 error );
1339 
1340 	return( 1 );
1341 
1342 on_error:
1343 	if( error != NULL )
1344 	{
1345 		libcerror_error_free(
1346 		 &error );
1347 	}
1348 	if( data_block_data_handle != NULL )
1349 	{
1350 		libfsapfs_data_block_data_handle_free(
1351 		 &data_block_data_handle,
1352 		 NULL );
1353 	}
1354 	if( file_extent != NULL )
1355 	{
1356 		libfsapfs_file_extent_free(
1357 		 &file_extent,
1358 		 NULL );
1359 	}
1360 	if( file_extents != NULL )
1361 	{
1362 		libcdata_array_free(
1363 		 &file_extents,
1364 		 (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_extent_free,
1365 		 NULL );
1366 	}
1367 	if( io_handle != NULL )
1368 	{
1369 		libfsapfs_io_handle_free(
1370 		 &io_handle,
1371 		 NULL );
1372 	}
1373 	return( 0 );
1374 }
1375 
1376 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
1377 
1378 /* The main program
1379  */
1380 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSAPFS_TEST_ATTRIBUTE_UNUSED)1381 int wmain(
1382      int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
1383      wchar_t * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
1384 #else
1385 int main(
1386      int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
1387      char * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
1388 #endif
1389 {
1390 	FSAPFS_TEST_UNREFERENCED_PARAMETER( argc )
1391 	FSAPFS_TEST_UNREFERENCED_PARAMETER( argv )
1392 
1393 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
1394 
1395 	FSAPFS_TEST_RUN(
1396 	 "libfsapfs_data_block_data_handle_initialize",
1397 	 fsapfs_test_data_block_data_handle_initialize );
1398 
1399 	FSAPFS_TEST_RUN(
1400 	 "libfsapfs_data_block_data_handle_free",
1401 	 fsapfs_test_data_block_data_handle_free );
1402 
1403 	FSAPFS_TEST_RUN(
1404 	 "libfsapfs_data_block_data_handle_read_segment_data",
1405 	 fsapfs_test_data_block_data_handle_read_segment_data );
1406 
1407 	FSAPFS_TEST_RUN(
1408 	 "libfsapfs_data_block_data_handle_seek_segment_offset",
1409 	 fsapfs_test_data_block_data_handle_seek_segment_offset );
1410 
1411 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
1412 
1413 	return( EXIT_SUCCESS );
1414 
1415 on_error:
1416 	return( EXIT_FAILURE );
1417 }
1418 
1419