1 /*
2  * Library store_descriptor type test program
3  *
4  * Copyright (C) 2011-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <common.h>
23 #include <file_stream.h>
24 #include <types.h>
25 
26 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
27 #include <stdlib.h>
28 #endif
29 
30 #include "vshadow_test_libcerror.h"
31 #include "vshadow_test_libvshadow.h"
32 #include "vshadow_test_macros.h"
33 #include "vshadow_test_memory.h"
34 #include "vshadow_test_unused.h"
35 
36 #include "../libvshadow/libvshadow_store_descriptor.h"
37 
38 #if defined( __GNUC__ ) && !defined( LIBVSHADOW_DLL_IMPORT )
39 
40 /* Tests the libvshadow_store_descriptor_initialize function
41  * Returns 1 if successful or 0 if not
42  */
vshadow_test_store_descriptor_initialize(void)43 int vshadow_test_store_descriptor_initialize(
44      void )
45 {
46 	libcerror_error_t *error                        = NULL;
47 	libvshadow_store_descriptor_t *store_descriptor = NULL;
48 	int result                                      = 0;
49 
50 #if defined( HAVE_VSHADOW_TEST_MEMORY )
51 	int number_of_malloc_fail_tests                 = 1;
52 	int number_of_memset_fail_tests                 = 1;
53 	int test_number                                 = 0;
54 #endif
55 
56 	/* Test regular cases
57 	 */
58 	result = libvshadow_store_descriptor_initialize(
59 	          &store_descriptor,
60 	          &error );
61 
62 	VSHADOW_TEST_ASSERT_EQUAL_INT(
63 	 "result",
64 	 result,
65 	 1 );
66 
67 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
68 	 "store_descriptor",
69 	 store_descriptor );
70 
71 	VSHADOW_TEST_ASSERT_IS_NULL(
72 	 "error",
73 	 error );
74 
75 	result = libvshadow_store_descriptor_free(
76 	          &store_descriptor,
77 	          &error );
78 
79 	VSHADOW_TEST_ASSERT_EQUAL_INT(
80 	 "result",
81 	 result,
82 	 1 );
83 
84 	VSHADOW_TEST_ASSERT_IS_NULL(
85 	 "store_descriptor",
86 	 store_descriptor );
87 
88 	VSHADOW_TEST_ASSERT_IS_NULL(
89 	 "error",
90 	 error );
91 
92 	/* Test error cases
93 	 */
94 	result = libvshadow_store_descriptor_initialize(
95 	          NULL,
96 	          &error );
97 
98 	VSHADOW_TEST_ASSERT_EQUAL_INT(
99 	 "result",
100 	 result,
101 	 -1 );
102 
103 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
104 	 "error",
105 	 error );
106 
107 	libcerror_error_free(
108 	 &error );
109 
110 	store_descriptor = (libvshadow_store_descriptor_t *) 0x12345678UL;
111 
112 	result = libvshadow_store_descriptor_initialize(
113 	          &store_descriptor,
114 	          &error );
115 
116 	store_descriptor = NULL;
117 
118 	VSHADOW_TEST_ASSERT_EQUAL_INT(
119 	 "result",
120 	 result,
121 	 -1 );
122 
123 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
124 	 "error",
125 	 error );
126 
127 	libcerror_error_free(
128 	 &error );
129 
130 #if defined( HAVE_VSHADOW_TEST_MEMORY )
131 
132 	for( test_number = 0;
133 	     test_number < number_of_malloc_fail_tests;
134 	     test_number++ )
135 	{
136 		/* Test libvshadow_store_descriptor_initialize with malloc failing
137 		 */
138 		vshadow_test_malloc_attempts_before_fail = test_number;
139 
140 		result = libvshadow_store_descriptor_initialize(
141 		          &store_descriptor,
142 		          &error );
143 
144 		if( vshadow_test_malloc_attempts_before_fail != -1 )
145 		{
146 			vshadow_test_malloc_attempts_before_fail = -1;
147 
148 			if( store_descriptor != NULL )
149 			{
150 				libvshadow_store_descriptor_free(
151 				 &store_descriptor,
152 				 NULL );
153 			}
154 		}
155 		else
156 		{
157 			VSHADOW_TEST_ASSERT_EQUAL_INT(
158 			 "result",
159 			 result,
160 			 -1 );
161 
162 			VSHADOW_TEST_ASSERT_IS_NULL(
163 			 "store_descriptor",
164 			 store_descriptor );
165 
166 			VSHADOW_TEST_ASSERT_IS_NOT_NULL(
167 			 "error",
168 			 error );
169 
170 			libcerror_error_free(
171 			 &error );
172 		}
173 	}
174 	for( test_number = 0;
175 	     test_number < number_of_memset_fail_tests;
176 	     test_number++ )
177 	{
178 		/* Test libvshadow_store_descriptor_initialize with memset failing
179 		 */
180 		vshadow_test_memset_attempts_before_fail = test_number;
181 
182 		result = libvshadow_store_descriptor_initialize(
183 		          &store_descriptor,
184 		          &error );
185 
186 		if( vshadow_test_memset_attempts_before_fail != -1 )
187 		{
188 			vshadow_test_memset_attempts_before_fail = -1;
189 
190 			if( store_descriptor != NULL )
191 			{
192 				libvshadow_store_descriptor_free(
193 				 &store_descriptor,
194 				 NULL );
195 			}
196 		}
197 		else
198 		{
199 			VSHADOW_TEST_ASSERT_EQUAL_INT(
200 			 "result",
201 			 result,
202 			 -1 );
203 
204 			VSHADOW_TEST_ASSERT_IS_NULL(
205 			 "store_descriptor",
206 			 store_descriptor );
207 
208 			VSHADOW_TEST_ASSERT_IS_NOT_NULL(
209 			 "error",
210 			 error );
211 
212 			libcerror_error_free(
213 			 &error );
214 		}
215 	}
216 #endif /* defined( HAVE_VSHADOW_TEST_MEMORY ) */
217 
218 	return( 1 );
219 
220 on_error:
221 	if( error != NULL )
222 	{
223 		libcerror_error_free(
224 		 &error );
225 	}
226 	if( store_descriptor != NULL )
227 	{
228 		libvshadow_store_descriptor_free(
229 		 &store_descriptor,
230 		 NULL );
231 	}
232 	return( 0 );
233 }
234 
235 /* Tests the libvshadow_store_descriptor_free function
236  * Returns 1 if successful or 0 if not
237  */
vshadow_test_store_descriptor_free(void)238 int vshadow_test_store_descriptor_free(
239      void )
240 {
241 	libcerror_error_t *error = NULL;
242 	int result               = 0;
243 
244 	/* Test error cases
245 	 */
246 	result = libvshadow_store_descriptor_free(
247 	          NULL,
248 	          &error );
249 
250 	VSHADOW_TEST_ASSERT_EQUAL_INT(
251 	 "result",
252 	 result,
253 	 -1 );
254 
255 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
256 	 "error",
257 	 error );
258 
259 	libcerror_error_free(
260 	 &error );
261 
262 	return( 1 );
263 
264 on_error:
265 	if( error != NULL )
266 	{
267 		libcerror_error_free(
268 		 &error );
269 	}
270 	return( 0 );
271 }
272 
273 /* Tests the libvshadow_store_descriptor_get_volume_size function
274  * Returns 1 if successful or 0 if not
275  */
vshadow_test_store_descriptor_get_volume_size(void)276 int vshadow_test_store_descriptor_get_volume_size(
277      void )
278 {
279 	libcerror_error_t *error                        = NULL;
280 	libvshadow_store_descriptor_t *store_descriptor = NULL;
281 	size64_t volume_size                            = 0;
282 	int result                                      = 0;
283 	int volume_size_is_set                          = 0;
284 
285 	/* Initialize test
286 	 */
287 	result = libvshadow_store_descriptor_initialize(
288 	          &store_descriptor,
289 	          &error );
290 
291 	VSHADOW_TEST_ASSERT_EQUAL_INT(
292 	 "result",
293 	 result,
294 	 1 );
295 
296 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
297 	 "store_descriptor",
298 	 store_descriptor );
299 
300 	VSHADOW_TEST_ASSERT_IS_NULL(
301 	 "error",
302 	 error );
303 
304 	/* Test regular cases
305 	 */
306 	result = libvshadow_store_descriptor_get_volume_size(
307 	          store_descriptor,
308 	          &volume_size,
309 	          &error );
310 
311 	VSHADOW_TEST_ASSERT_NOT_EQUAL_INT(
312 	 "result",
313 	 result,
314 	 -1 );
315 
316 	VSHADOW_TEST_ASSERT_IS_NULL(
317 	 "error",
318 	 error );
319 
320 	volume_size_is_set = result;
321 
322 	/* Test error cases
323 	 */
324 	result = libvshadow_store_descriptor_get_volume_size(
325 	          NULL,
326 	          &volume_size,
327 	          &error );
328 
329 	VSHADOW_TEST_ASSERT_EQUAL_INT(
330 	 "result",
331 	 result,
332 	 -1 );
333 
334 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
335 	 "error",
336 	 error );
337 
338 	libcerror_error_free(
339 	 &error );
340 
341 	if( volume_size_is_set != 0 )
342 	{
343 		result = libvshadow_store_descriptor_get_volume_size(
344 		          store_descriptor,
345 		          NULL,
346 		          &error );
347 
348 		VSHADOW_TEST_ASSERT_EQUAL_INT(
349 		 "result",
350 		 result,
351 		 -1 );
352 
353 		VSHADOW_TEST_ASSERT_IS_NOT_NULL(
354 		 "error",
355 		 error );
356 
357 		libcerror_error_free(
358 		 &error );
359 	}
360 	/* Clean up
361 	 */
362 	result = libvshadow_store_descriptor_free(
363 	          &store_descriptor,
364 	          &error );
365 
366 	VSHADOW_TEST_ASSERT_EQUAL_INT(
367 	 "result",
368 	 result,
369 	 1 );
370 
371 	VSHADOW_TEST_ASSERT_IS_NULL(
372 	 "store_descriptor",
373 	 store_descriptor );
374 
375 	VSHADOW_TEST_ASSERT_IS_NULL(
376 	 "error",
377 	 error );
378 
379 	return( 1 );
380 
381 on_error:
382 	if( error != NULL )
383 	{
384 		libcerror_error_free(
385 		 &error );
386 	}
387 	if( store_descriptor != NULL )
388 	{
389 		libvshadow_store_descriptor_free(
390 		 &store_descriptor,
391 		 NULL );
392 	}
393 	return( 0 );
394 }
395 
396 /* Tests the libvshadow_store_descriptor_get_creation_time function
397  * Returns 1 if successful or 0 if not
398  */
vshadow_test_store_descriptor_get_creation_time(void)399 int vshadow_test_store_descriptor_get_creation_time(
400      void )
401 {
402 	libcerror_error_t *error                        = NULL;
403 	libvshadow_store_descriptor_t *store_descriptor = NULL;
404 	uint64_t creation_time                          = 0;
405 	int creation_time_is_set                        = 0;
406 	int result                                      = 0;
407 
408 	/* Initialize test
409 	 */
410 	result = libvshadow_store_descriptor_initialize(
411 	          &store_descriptor,
412 	          &error );
413 
414 	VSHADOW_TEST_ASSERT_EQUAL_INT(
415 	 "result",
416 	 result,
417 	 1 );
418 
419 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
420 	 "store_descriptor",
421 	 store_descriptor );
422 
423 	VSHADOW_TEST_ASSERT_IS_NULL(
424 	 "error",
425 	 error );
426 
427 	/* Test regular cases
428 	 */
429 	result = libvshadow_store_descriptor_get_creation_time(
430 	          store_descriptor,
431 	          &creation_time,
432 	          &error );
433 
434 	VSHADOW_TEST_ASSERT_NOT_EQUAL_INT(
435 	 "result",
436 	 result,
437 	 -1 );
438 
439 	VSHADOW_TEST_ASSERT_IS_NULL(
440 	 "error",
441 	 error );
442 
443 	creation_time_is_set = result;
444 
445 	/* Test error cases
446 	 */
447 	result = libvshadow_store_descriptor_get_creation_time(
448 	          NULL,
449 	          &creation_time,
450 	          &error );
451 
452 	VSHADOW_TEST_ASSERT_EQUAL_INT(
453 	 "result",
454 	 result,
455 	 -1 );
456 
457 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
458 	 "error",
459 	 error );
460 
461 	libcerror_error_free(
462 	 &error );
463 
464 	if( creation_time_is_set != 0 )
465 	{
466 		result = libvshadow_store_descriptor_get_creation_time(
467 		          store_descriptor,
468 		          NULL,
469 		          &error );
470 
471 		VSHADOW_TEST_ASSERT_EQUAL_INT(
472 		 "result",
473 		 result,
474 		 -1 );
475 
476 		VSHADOW_TEST_ASSERT_IS_NOT_NULL(
477 		 "error",
478 		 error );
479 
480 		libcerror_error_free(
481 		 &error );
482 	}
483 	/* Clean up
484 	 */
485 	result = libvshadow_store_descriptor_free(
486 	          &store_descriptor,
487 	          &error );
488 
489 	VSHADOW_TEST_ASSERT_EQUAL_INT(
490 	 "result",
491 	 result,
492 	 1 );
493 
494 	VSHADOW_TEST_ASSERT_IS_NULL(
495 	 "store_descriptor",
496 	 store_descriptor );
497 
498 	VSHADOW_TEST_ASSERT_IS_NULL(
499 	 "error",
500 	 error );
501 
502 	return( 1 );
503 
504 on_error:
505 	if( error != NULL )
506 	{
507 		libcerror_error_free(
508 		 &error );
509 	}
510 	if( store_descriptor != NULL )
511 	{
512 		libvshadow_store_descriptor_free(
513 		 &store_descriptor,
514 		 NULL );
515 	}
516 	return( 0 );
517 }
518 
519 /* Tests the libvshadow_store_descriptor_get_attribute_flags function
520  * Returns 1 if successful or 0 if not
521  */
vshadow_test_store_descriptor_get_attribute_flags(void)522 int vshadow_test_store_descriptor_get_attribute_flags(
523      void )
524 {
525 	libcerror_error_t *error                        = NULL;
526 	libvshadow_store_descriptor_t *store_descriptor = NULL;
527 	uint32_t attribute_flags                        = 0;
528 	int attribute_flags_is_set                      = 0;
529 	int result                                      = 0;
530 
531 	/* Initialize test
532 	 */
533 	result = libvshadow_store_descriptor_initialize(
534 	          &store_descriptor,
535 	          &error );
536 
537 	VSHADOW_TEST_ASSERT_EQUAL_INT(
538 	 "result",
539 	 result,
540 	 1 );
541 
542 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
543 	 "store_descriptor",
544 	 store_descriptor );
545 
546 	VSHADOW_TEST_ASSERT_IS_NULL(
547 	 "error",
548 	 error );
549 
550 	/* Test regular cases
551 	 */
552 	result = libvshadow_store_descriptor_get_attribute_flags(
553 	          store_descriptor,
554 	          &attribute_flags,
555 	          &error );
556 
557 	VSHADOW_TEST_ASSERT_NOT_EQUAL_INT(
558 	 "result",
559 	 result,
560 	 -1 );
561 
562 	VSHADOW_TEST_ASSERT_IS_NULL(
563 	 "error",
564 	 error );
565 
566 	attribute_flags_is_set = result;
567 
568 	/* Test error cases
569 	 */
570 	result = libvshadow_store_descriptor_get_attribute_flags(
571 	          NULL,
572 	          &attribute_flags,
573 	          &error );
574 
575 	VSHADOW_TEST_ASSERT_EQUAL_INT(
576 	 "result",
577 	 result,
578 	 -1 );
579 
580 	VSHADOW_TEST_ASSERT_IS_NOT_NULL(
581 	 "error",
582 	 error );
583 
584 	libcerror_error_free(
585 	 &error );
586 
587 	if( attribute_flags_is_set != 0 )
588 	{
589 		result = libvshadow_store_descriptor_get_attribute_flags(
590 		          store_descriptor,
591 		          NULL,
592 		          &error );
593 
594 		VSHADOW_TEST_ASSERT_EQUAL_INT(
595 		 "result",
596 		 result,
597 		 -1 );
598 
599 		VSHADOW_TEST_ASSERT_IS_NOT_NULL(
600 		 "error",
601 		 error );
602 
603 		libcerror_error_free(
604 		 &error );
605 	}
606 	/* Clean up
607 	 */
608 	result = libvshadow_store_descriptor_free(
609 	          &store_descriptor,
610 	          &error );
611 
612 	VSHADOW_TEST_ASSERT_EQUAL_INT(
613 	 "result",
614 	 result,
615 	 1 );
616 
617 	VSHADOW_TEST_ASSERT_IS_NULL(
618 	 "store_descriptor",
619 	 store_descriptor );
620 
621 	VSHADOW_TEST_ASSERT_IS_NULL(
622 	 "error",
623 	 error );
624 
625 	return( 1 );
626 
627 on_error:
628 	if( error != NULL )
629 	{
630 		libcerror_error_free(
631 		 &error );
632 	}
633 	if( store_descriptor != NULL )
634 	{
635 		libvshadow_store_descriptor_free(
636 		 &store_descriptor,
637 		 NULL );
638 	}
639 	return( 0 );
640 }
641 
642 #endif /* defined( __GNUC__ ) && !defined( LIBVSHADOW_DLL_IMPORT ) */
643 
644 /* The main program
645  */
646 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc VSHADOW_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]VSHADOW_TEST_ATTRIBUTE_UNUSED)647 int wmain(
648      int argc VSHADOW_TEST_ATTRIBUTE_UNUSED,
649      wchar_t * const argv[] VSHADOW_TEST_ATTRIBUTE_UNUSED )
650 #else
651 int main(
652      int argc VSHADOW_TEST_ATTRIBUTE_UNUSED,
653      char * const argv[] VSHADOW_TEST_ATTRIBUTE_UNUSED )
654 #endif
655 {
656 	VSHADOW_TEST_UNREFERENCED_PARAMETER( argc )
657 	VSHADOW_TEST_UNREFERENCED_PARAMETER( argv )
658 
659 #if defined( __GNUC__ ) && !defined( LIBVSHADOW_DLL_IMPORT )
660 
661 	VSHADOW_TEST_RUN(
662 	 "libvshadow_store_descriptor_initialize",
663 	 vshadow_test_store_descriptor_initialize );
664 
665 	VSHADOW_TEST_RUN(
666 	 "libvshadow_store_descriptor_free",
667 	 vshadow_test_store_descriptor_free );
668 
669 	/* TODO: add tests for libvshadow_store_descriptor_has_in_volume_data */
670 
671 	/* TODO: add tests for libvshadow_store_descriptor_compare_by_creation_time */
672 
673 	/* TODO: add tests for libvshadow_store_descriptor_compare_by_identifier */
674 
675 	/* TODO: add tests for libvshadow_store_descriptor_read_catalog_entry */
676 
677 	/* TODO: add tests for libvshadow_store_descriptor_read_store_header */
678 
679 	/* TODO: add tests for libvshadow_store_descriptor_read_store_bitmap */
680 
681 	/* TODO: add tests for libvshadow_store_descriptor_read_store_block_list */
682 
683 	/* TODO: add tests for libvshadow_store_descriptor_read_store_block_range_list */
684 
685 	/* TODO: add tests for libvshadow_store_descriptor_read_block_descriptors */
686 
687 	/* TODO: add tests for libvshadow_store_descriptor_read_buffer */
688 
689 	VSHADOW_TEST_RUN(
690 	 "libvshadow_store_descriptor_get_volume_size",
691 	 vshadow_test_store_descriptor_get_volume_size );
692 
693 	/* TODO: add tests for libvshadow_store_descriptor_get_identifier */
694 
695 	VSHADOW_TEST_RUN(
696 	 "libvshadow_store_descriptor_get_creation_time",
697 	 vshadow_test_store_descriptor_get_creation_time );
698 
699 	/* TODO: add tests for libvshadow_store_descriptor_get_copy_identifier */
700 
701 	/* TODO: add tests for libvshadow_store_descriptor_get_copy_set_identifier */
702 
703 	VSHADOW_TEST_RUN(
704 	 "libvshadow_store_descriptor_get_attribute_flags",
705 	 vshadow_test_store_descriptor_get_attribute_flags );
706 
707 	/* TODO: add tests for libvshadow_store_descriptor_get_number_of_blocks */
708 
709 	/* TODO: add tests for libvshadow_store_descriptor_get_block_descriptor_by_index */
710 
711 #endif /* defined( __GNUC__ ) && !defined( LIBVSHADOW_DLL_IMPORT ) */
712 
713 	return( EXIT_SUCCESS );
714 
715 on_error:
716 	return( EXIT_FAILURE );
717 }
718 
719