1 /*
2  * Library file 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 <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 "evtx_test_functions.h"
34 #include "evtx_test_getopt.h"
35 #include "evtx_test_libbfio.h"
36 #include "evtx_test_libcerror.h"
37 #include "evtx_test_libevtx.h"
38 #include "evtx_test_macros.h"
39 #include "evtx_test_memory.h"
40 #include "evtx_test_unused.h"
41 
42 #include "../libevtx/libevtx_file.h"
43 
44 #if !defined( LIBEVTX_HAVE_BFIO )
45 
46 LIBEVTX_EXTERN \
47 int libevtx_check_file_signature_file_io_handle(
48      libbfio_handle_t *file_io_handle,
49      libcerror_error_t **error );
50 
51 LIBEVTX_EXTERN \
52 int libevtx_file_open_file_io_handle(
53      libevtx_file_t *file,
54      libbfio_handle_t *file_io_handle,
55      int access_flags,
56      libevtx_error_t **error );
57 
58 #endif /* !defined( LIBEVTX_HAVE_BFIO ) */
59 
60 #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) && SIZEOF_WCHAR_T != 2 && SIZEOF_WCHAR_T != 4
61 #error Unsupported size of wchar_t
62 #endif
63 
64 /* Define to make evtx_test_file generate verbose output
65 #define EVTX_TEST_FILE_VERBOSE
66  */
67 
68 /* Creates and opens a source file
69  * Returns 1 if successful or -1 on error
70  */
evtx_test_file_open_source(libevtx_file_t ** file,libbfio_handle_t * file_io_handle,libcerror_error_t ** error)71 int evtx_test_file_open_source(
72      libevtx_file_t **file,
73      libbfio_handle_t *file_io_handle,
74      libcerror_error_t **error )
75 {
76 	static char *function = "evtx_test_file_open_source";
77 	int result            = 0;
78 
79 	if( file == NULL )
80 	{
81 		libcerror_error_set(
82 		 error,
83 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
84 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
85 		 "%s: invalid file.",
86 		 function );
87 
88 		return( -1 );
89 	}
90 	if( file_io_handle == NULL )
91 	{
92 		libcerror_error_set(
93 		 error,
94 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
95 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
96 		 "%s: invalid file IO handle.",
97 		 function );
98 
99 		return( -1 );
100 	}
101 	if( libevtx_file_initialize(
102 	     file,
103 	     error ) != 1 )
104 	{
105 		libcerror_error_set(
106 		 error,
107 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
108 		 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
109 		 "%s: unable to initialize file.",
110 		 function );
111 
112 		goto on_error;
113 	}
114 	result = libevtx_file_open_file_io_handle(
115 	          *file,
116 	          file_io_handle,
117 	          LIBEVTX_OPEN_READ,
118 	          error );
119 
120 	if( result != 1 )
121 	{
122 		libcerror_error_set(
123 		 error,
124 		 LIBCERROR_ERROR_DOMAIN_IO,
125 		 LIBCERROR_IO_ERROR_OPEN_FAILED,
126 		 "%s: unable to open file.",
127 		 function );
128 
129 		goto on_error;
130 	}
131 	return( 1 );
132 
133 on_error:
134 	if( *file != NULL )
135 	{
136 		libevtx_file_free(
137 		 file,
138 		 NULL );
139 	}
140 	return( -1 );
141 }
142 
143 /* Closes and frees a source file
144  * Returns 1 if successful or -1 on error
145  */
evtx_test_file_close_source(libevtx_file_t ** file,libcerror_error_t ** error)146 int evtx_test_file_close_source(
147      libevtx_file_t **file,
148      libcerror_error_t **error )
149 {
150 	static char *function = "evtx_test_file_close_source";
151 	int result            = 0;
152 
153 	if( file == NULL )
154 	{
155 		libcerror_error_set(
156 		 error,
157 		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158 		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159 		 "%s: invalid file.",
160 		 function );
161 
162 		return( -1 );
163 	}
164 	if( libevtx_file_close(
165 	     *file,
166 	     error ) != 0 )
167 	{
168 		libcerror_error_set(
169 		 error,
170 		 LIBCERROR_ERROR_DOMAIN_IO,
171 		 LIBCERROR_IO_ERROR_CLOSE_FAILED,
172 		 "%s: unable to close file.",
173 		 function );
174 
175 		result = -1;
176 	}
177 	if( libevtx_file_free(
178 	     file,
179 	     error ) != 1 )
180 	{
181 		libcerror_error_set(
182 		 error,
183 		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
184 		 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
185 		 "%s: unable to free file.",
186 		 function );
187 
188 		result = -1;
189 	}
190 	return( result );
191 }
192 
193 /* Tests the libevtx_file_initialize function
194  * Returns 1 if successful or 0 if not
195  */
evtx_test_file_initialize(void)196 int evtx_test_file_initialize(
197      void )
198 {
199 	libcerror_error_t *error        = NULL;
200 	libevtx_file_t *file            = NULL;
201 	int result                      = 0;
202 
203 #if defined( HAVE_EVTX_TEST_MEMORY )
204 	int number_of_malloc_fail_tests = 1;
205 	int number_of_memset_fail_tests = 1;
206 	int test_number                 = 0;
207 #endif
208 
209 	/* Test regular cases
210 	 */
211 	result = libevtx_file_initialize(
212 	          &file,
213 	          &error );
214 
215 	EVTX_TEST_ASSERT_EQUAL_INT(
216 	 "result",
217 	 result,
218 	 1 );
219 
220 	EVTX_TEST_ASSERT_IS_NOT_NULL(
221 	 "file",
222 	 file );
223 
224 	EVTX_TEST_ASSERT_IS_NULL(
225 	 "error",
226 	 error );
227 
228 	result = libevtx_file_free(
229 	          &file,
230 	          &error );
231 
232 	EVTX_TEST_ASSERT_EQUAL_INT(
233 	 "result",
234 	 result,
235 	 1 );
236 
237 	EVTX_TEST_ASSERT_IS_NULL(
238 	 "file",
239 	 file );
240 
241 	EVTX_TEST_ASSERT_IS_NULL(
242 	 "error",
243 	 error );
244 
245 	/* Test error cases
246 	 */
247 	result = libevtx_file_initialize(
248 	          NULL,
249 	          &error );
250 
251 	EVTX_TEST_ASSERT_EQUAL_INT(
252 	 "result",
253 	 result,
254 	 -1 );
255 
256 	EVTX_TEST_ASSERT_IS_NOT_NULL(
257 	 "error",
258 	 error );
259 
260 	libcerror_error_free(
261 	 &error );
262 
263 	file = (libevtx_file_t *) 0x12345678UL;
264 
265 	result = libevtx_file_initialize(
266 	          &file,
267 	          &error );
268 
269 	EVTX_TEST_ASSERT_EQUAL_INT(
270 	 "result",
271 	 result,
272 	 -1 );
273 
274 	EVTX_TEST_ASSERT_IS_NOT_NULL(
275 	 "error",
276 	 error );
277 
278 	libcerror_error_free(
279 	 &error );
280 
281 	file = NULL;
282 
283 #if defined( HAVE_EVTX_TEST_MEMORY )
284 
285 	for( test_number = 0;
286 	     test_number < number_of_malloc_fail_tests;
287 	     test_number++ )
288 	{
289 		/* Test libevtx_file_initialize with malloc failing
290 		 */
291 		evtx_test_malloc_attempts_before_fail = test_number;
292 
293 		result = libevtx_file_initialize(
294 		          &file,
295 		          &error );
296 
297 		if( evtx_test_malloc_attempts_before_fail != -1 )
298 		{
299 			evtx_test_malloc_attempts_before_fail = -1;
300 
301 			if( file != NULL )
302 			{
303 				libevtx_file_free(
304 				 &file,
305 				 NULL );
306 			}
307 		}
308 		else
309 		{
310 			EVTX_TEST_ASSERT_EQUAL_INT(
311 			 "result",
312 			 result,
313 			 -1 );
314 
315 			EVTX_TEST_ASSERT_IS_NULL(
316 			 "file",
317 			 file );
318 
319 			EVTX_TEST_ASSERT_IS_NOT_NULL(
320 			 "error",
321 			 error );
322 
323 			libcerror_error_free(
324 			 &error );
325 		}
326 	}
327 	for( test_number = 0;
328 	     test_number < number_of_memset_fail_tests;
329 	     test_number++ )
330 	{
331 		/* Test libevtx_file_initialize with memset failing
332 		 */
333 		evtx_test_memset_attempts_before_fail = test_number;
334 
335 		result = libevtx_file_initialize(
336 		          &file,
337 		          &error );
338 
339 		if( evtx_test_memset_attempts_before_fail != -1 )
340 		{
341 			evtx_test_memset_attempts_before_fail = -1;
342 
343 			if( file != NULL )
344 			{
345 				libevtx_file_free(
346 				 &file,
347 				 NULL );
348 			}
349 		}
350 		else
351 		{
352 			EVTX_TEST_ASSERT_EQUAL_INT(
353 			 "result",
354 			 result,
355 			 -1 );
356 
357 			EVTX_TEST_ASSERT_IS_NULL(
358 			 "file",
359 			 file );
360 
361 			EVTX_TEST_ASSERT_IS_NOT_NULL(
362 			 "error",
363 			 error );
364 
365 			libcerror_error_free(
366 			 &error );
367 		}
368 	}
369 #endif /* defined( HAVE_EVTX_TEST_MEMORY ) */
370 
371 	return( 1 );
372 
373 on_error:
374 	if( error != NULL )
375 	{
376 		libcerror_error_free(
377 		 &error );
378 	}
379 	if( file != NULL )
380 	{
381 		libevtx_file_free(
382 		 &file,
383 		 NULL );
384 	}
385 	return( 0 );
386 }
387 
388 /* Tests the libevtx_file_free function
389  * Returns 1 if successful or 0 if not
390  */
evtx_test_file_free(void)391 int evtx_test_file_free(
392      void )
393 {
394 	libcerror_error_t *error = NULL;
395 	int result               = 0;
396 
397 	/* Test error cases
398 	 */
399 	result = libevtx_file_free(
400 	          NULL,
401 	          &error );
402 
403 	EVTX_TEST_ASSERT_EQUAL_INT(
404 	 "result",
405 	 result,
406 	 -1 );
407 
408 	EVTX_TEST_ASSERT_IS_NOT_NULL(
409 	 "error",
410 	 error );
411 
412 	libcerror_error_free(
413 	 &error );
414 
415 	return( 1 );
416 
417 on_error:
418 	if( error != NULL )
419 	{
420 		libcerror_error_free(
421 		 &error );
422 	}
423 	return( 0 );
424 }
425 
426 /* Tests the libevtx_file_open function
427  * Returns 1 if successful or 0 if not
428  */
evtx_test_file_open(const system_character_t * source)429 int evtx_test_file_open(
430      const system_character_t *source )
431 {
432 	char narrow_source[ 256 ];
433 
434 	libcerror_error_t *error = NULL;
435 	libevtx_file_t *file     = NULL;
436 	int result               = 0;
437 
438 	/* Initialize test
439 	 */
440 	result = evtx_test_get_narrow_source(
441 	          source,
442 	          narrow_source,
443 	          256,
444 	          &error );
445 
446 	EVTX_TEST_ASSERT_EQUAL_INT(
447 	 "result",
448 	 result,
449 	 1 );
450 
451 	EVTX_TEST_ASSERT_IS_NULL(
452 	 "error",
453 	 error );
454 
455 	result = libevtx_file_initialize(
456 	          &file,
457 	          &error );
458 
459 	EVTX_TEST_ASSERT_EQUAL_INT(
460 	 "result",
461 	 result,
462 	 1 );
463 
464 	EVTX_TEST_ASSERT_IS_NOT_NULL(
465 	 "file",
466 	 file );
467 
468 	EVTX_TEST_ASSERT_IS_NULL(
469 	 "error",
470 	 error );
471 
472 	/* Test open
473 	 */
474 	result = libevtx_file_open(
475 	          file,
476 	          narrow_source,
477 	          LIBEVTX_OPEN_READ,
478 	          &error );
479 
480 	EVTX_TEST_ASSERT_EQUAL_INT(
481 	 "result",
482 	 result,
483 	 1 );
484 
485 	EVTX_TEST_ASSERT_IS_NULL(
486 	 "error",
487 	 error );
488 
489 	/* Test error cases
490 	 */
491 	result = libevtx_file_open(
492 	          NULL,
493 	          narrow_source,
494 	          LIBEVTX_OPEN_READ,
495 	          &error );
496 
497 	EVTX_TEST_ASSERT_EQUAL_INT(
498 	 "result",
499 	 result,
500 	 -1 );
501 
502 	EVTX_TEST_ASSERT_IS_NOT_NULL(
503 	 "error",
504 	 error );
505 
506 	libcerror_error_free(
507 	 &error );
508 
509 	result = libevtx_file_open(
510 	          file,
511 	          NULL,
512 	          LIBEVTX_OPEN_READ,
513 	          &error );
514 
515 	EVTX_TEST_ASSERT_EQUAL_INT(
516 	 "result",
517 	 result,
518 	 -1 );
519 
520 	EVTX_TEST_ASSERT_IS_NOT_NULL(
521 	 "error",
522 	 error );
523 
524 	libcerror_error_free(
525 	 &error );
526 
527 	result = libevtx_file_open(
528 	          file,
529 	          narrow_source,
530 	          -1,
531 	          &error );
532 
533 	EVTX_TEST_ASSERT_EQUAL_INT(
534 	 "result",
535 	 result,
536 	 -1 );
537 
538 	EVTX_TEST_ASSERT_IS_NOT_NULL(
539 	 "error",
540 	 error );
541 
542 	libcerror_error_free(
543 	 &error );
544 
545 	/* Test open when already opened
546 	 */
547 	result = libevtx_file_open(
548 	          file,
549 	          narrow_source,
550 	          LIBEVTX_OPEN_READ,
551 	          &error );
552 
553 	EVTX_TEST_ASSERT_EQUAL_INT(
554 	 "result",
555 	 result,
556 	 -1 );
557 
558 	EVTX_TEST_ASSERT_IS_NOT_NULL(
559 	 "error",
560 	 error );
561 
562 	libcerror_error_free(
563 	 &error );
564 
565 	/* Clean up
566 	 */
567 	result = libevtx_file_free(
568 	          &file,
569 	          &error );
570 
571 	EVTX_TEST_ASSERT_EQUAL_INT(
572 	 "result",
573 	 result,
574 	 1 );
575 
576 	EVTX_TEST_ASSERT_IS_NULL(
577 	 "file",
578 	 file );
579 
580 	EVTX_TEST_ASSERT_IS_NULL(
581 	 "error",
582 	 error );
583 
584 	return( 1 );
585 
586 on_error:
587 	if( error != NULL )
588 	{
589 		libcerror_error_free(
590 		 &error );
591 	}
592 	if( file != NULL )
593 	{
594 		libevtx_file_free(
595 		 &file,
596 		 NULL );
597 	}
598 	return( 0 );
599 }
600 
601 #if defined( HAVE_WIDE_CHARACTER_TYPE )
602 
603 /* Tests the libevtx_file_open_wide function
604  * Returns 1 if successful or 0 if not
605  */
evtx_test_file_open_wide(const system_character_t * source)606 int evtx_test_file_open_wide(
607      const system_character_t *source )
608 {
609 	wchar_t wide_source[ 256 ];
610 
611 	libcerror_error_t *error = NULL;
612 	libevtx_file_t *file     = NULL;
613 	int result               = 0;
614 
615 	/* Initialize test
616 	 */
617 	result = evtx_test_get_wide_source(
618 	          source,
619 	          wide_source,
620 	          256,
621 	          &error );
622 
623 	EVTX_TEST_ASSERT_EQUAL_INT(
624 	 "result",
625 	 result,
626 	 1 );
627 
628 	EVTX_TEST_ASSERT_IS_NULL(
629 	 "error",
630 	 error );
631 
632 	result = libevtx_file_initialize(
633 	          &file,
634 	          &error );
635 
636 	EVTX_TEST_ASSERT_EQUAL_INT(
637 	 "result",
638 	 result,
639 	 1 );
640 
641 	EVTX_TEST_ASSERT_IS_NOT_NULL(
642 	 "file",
643 	 file );
644 
645 	EVTX_TEST_ASSERT_IS_NULL(
646 	 "error",
647 	 error );
648 
649 	/* Test open
650 	 */
651 	result = libevtx_file_open_wide(
652 	          file,
653 	          wide_source,
654 	          LIBEVTX_OPEN_READ,
655 	          &error );
656 
657 	EVTX_TEST_ASSERT_EQUAL_INT(
658 	 "result",
659 	 result,
660 	 1 );
661 
662 	EVTX_TEST_ASSERT_IS_NULL(
663 	 "error",
664 	 error );
665 
666 	/* Test error cases
667 	 */
668 	result = libevtx_file_open_wide(
669 	          NULL,
670 	          wide_source,
671 	          LIBEVTX_OPEN_READ,
672 	          &error );
673 
674 	EVTX_TEST_ASSERT_EQUAL_INT(
675 	 "result",
676 	 result,
677 	 -1 );
678 
679 	EVTX_TEST_ASSERT_IS_NOT_NULL(
680 	 "error",
681 	 error );
682 
683 	libcerror_error_free(
684 	 &error );
685 
686 	result = libevtx_file_open_wide(
687 	          file,
688 	          NULL,
689 	          LIBEVTX_OPEN_READ,
690 	          &error );
691 
692 	EVTX_TEST_ASSERT_EQUAL_INT(
693 	 "result",
694 	 result,
695 	 -1 );
696 
697 	EVTX_TEST_ASSERT_IS_NOT_NULL(
698 	 "error",
699 	 error );
700 
701 	libcerror_error_free(
702 	 &error );
703 
704 	result = libevtx_file_open_wide(
705 	          file,
706 	          wide_source,
707 	          -1,
708 	          &error );
709 
710 	EVTX_TEST_ASSERT_EQUAL_INT(
711 	 "result",
712 	 result,
713 	 -1 );
714 
715 	EVTX_TEST_ASSERT_IS_NOT_NULL(
716 	 "error",
717 	 error );
718 
719 	libcerror_error_free(
720 	 &error );
721 
722 	/* Test open when already opened
723 	 */
724 	result = libevtx_file_open_wide(
725 	          file,
726 	          wide_source,
727 	          LIBEVTX_OPEN_READ,
728 	          &error );
729 
730 	EVTX_TEST_ASSERT_EQUAL_INT(
731 	 "result",
732 	 result,
733 	 -1 );
734 
735 	EVTX_TEST_ASSERT_IS_NOT_NULL(
736 	 "error",
737 	 error );
738 
739 	libcerror_error_free(
740 	 &error );
741 
742 	/* Clean up
743 	 */
744 	result = libevtx_file_free(
745 	          &file,
746 	          &error );
747 
748 	EVTX_TEST_ASSERT_EQUAL_INT(
749 	 "result",
750 	 result,
751 	 1 );
752 
753 	EVTX_TEST_ASSERT_IS_NULL(
754 	 "file",
755 	 file );
756 
757 	EVTX_TEST_ASSERT_IS_NULL(
758 	 "error",
759 	 error );
760 
761 	return( 1 );
762 
763 on_error:
764 	if( error != NULL )
765 	{
766 		libcerror_error_free(
767 		 &error );
768 	}
769 	if( file != NULL )
770 	{
771 		libevtx_file_free(
772 		 &file,
773 		 NULL );
774 	}
775 	return( 0 );
776 }
777 
778 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
779 
780 /* Tests the libevtx_file_open_file_io_handle function
781  * Returns 1 if successful or 0 if not
782  */
evtx_test_file_open_file_io_handle(const system_character_t * source)783 int evtx_test_file_open_file_io_handle(
784      const system_character_t *source )
785 {
786 	libbfio_handle_t *file_io_handle = NULL;
787 	libcerror_error_t *error         = NULL;
788 	libevtx_file_t *file             = NULL;
789 	size_t string_length             = 0;
790 	int result                       = 0;
791 
792 	/* Initialize test
793 	 */
794 	result = libbfio_file_initialize(
795 	          &file_io_handle,
796 	          &error );
797 
798 	EVTX_TEST_ASSERT_EQUAL_INT(
799 	 "result",
800 	 result,
801 	 1 );
802 
803         EVTX_TEST_ASSERT_IS_NOT_NULL(
804          "file_io_handle",
805          file_io_handle );
806 
807         EVTX_TEST_ASSERT_IS_NULL(
808          "error",
809          error );
810 
811 	string_length = system_string_length(
812 	                 source );
813 
814 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
815 	result = libbfio_file_set_name_wide(
816 	          file_io_handle,
817 	          source,
818 	          string_length,
819 	          &error );
820 #else
821 	result = libbfio_file_set_name(
822 	          file_io_handle,
823 	          source,
824 	          string_length,
825 	          &error );
826 #endif
827 	EVTX_TEST_ASSERT_EQUAL_INT(
828 	 "result",
829 	 result,
830 	 1 );
831 
832         EVTX_TEST_ASSERT_IS_NULL(
833          "error",
834          error );
835 
836 	result = libevtx_file_initialize(
837 	          &file,
838 	          &error );
839 
840 	EVTX_TEST_ASSERT_EQUAL_INT(
841 	 "result",
842 	 result,
843 	 1 );
844 
845 	EVTX_TEST_ASSERT_IS_NOT_NULL(
846 	 "file",
847 	 file );
848 
849 	EVTX_TEST_ASSERT_IS_NULL(
850 	 "error",
851 	 error );
852 
853 	/* Test open
854 	 */
855 	result = libevtx_file_open_file_io_handle(
856 	          file,
857 	          file_io_handle,
858 	          LIBEVTX_OPEN_READ,
859 	          &error );
860 
861 	EVTX_TEST_ASSERT_EQUAL_INT(
862 	 "result",
863 	 result,
864 	 1 );
865 
866 	EVTX_TEST_ASSERT_IS_NULL(
867 	 "error",
868 	 error );
869 
870 	/* Test error cases
871 	 */
872 	result = libevtx_file_open_file_io_handle(
873 	          NULL,
874 	          file_io_handle,
875 	          LIBEVTX_OPEN_READ,
876 	          &error );
877 
878 	EVTX_TEST_ASSERT_EQUAL_INT(
879 	 "result",
880 	 result,
881 	 -1 );
882 
883 	EVTX_TEST_ASSERT_IS_NOT_NULL(
884 	 "error",
885 	 error );
886 
887 	libcerror_error_free(
888 	 &error );
889 
890 	result = libevtx_file_open_file_io_handle(
891 	          file,
892 	          NULL,
893 	          LIBEVTX_OPEN_READ,
894 	          &error );
895 
896 	EVTX_TEST_ASSERT_EQUAL_INT(
897 	 "result",
898 	 result,
899 	 -1 );
900 
901 	EVTX_TEST_ASSERT_IS_NOT_NULL(
902 	 "error",
903 	 error );
904 
905 	libcerror_error_free(
906 	 &error );
907 
908 	result = libevtx_file_open_file_io_handle(
909 	          file,
910 	          file_io_handle,
911 	          -1,
912 	          &error );
913 
914 	EVTX_TEST_ASSERT_EQUAL_INT(
915 	 "result",
916 	 result,
917 	 -1 );
918 
919 	EVTX_TEST_ASSERT_IS_NOT_NULL(
920 	 "error",
921 	 error );
922 
923 	libcerror_error_free(
924 	 &error );
925 
926 	/* Test open when already opened
927 	 */
928 	result = libevtx_file_open_file_io_handle(
929 	          file,
930 	          file_io_handle,
931 	          LIBEVTX_OPEN_READ,
932 	          &error );
933 
934 	EVTX_TEST_ASSERT_EQUAL_INT(
935 	 "result",
936 	 result,
937 	 -1 );
938 
939 	EVTX_TEST_ASSERT_IS_NOT_NULL(
940 	 "error",
941 	 error );
942 
943 	libcerror_error_free(
944 	 &error );
945 
946 	/* Clean up
947 	 */
948 	result = libevtx_file_free(
949 	          &file,
950 	          &error );
951 
952 	EVTX_TEST_ASSERT_EQUAL_INT(
953 	 "result",
954 	 result,
955 	 1 );
956 
957 	EVTX_TEST_ASSERT_IS_NULL(
958 	 "file",
959 	 file );
960 
961 	EVTX_TEST_ASSERT_IS_NULL(
962 	 "error",
963 	 error );
964 
965 	result = libbfio_handle_free(
966 	          &file_io_handle,
967 	          &error );
968 
969 	EVTX_TEST_ASSERT_EQUAL_INT(
970 	 "result",
971 	 result,
972 	 1 );
973 
974 	EVTX_TEST_ASSERT_IS_NULL(
975          "file_io_handle",
976          file_io_handle );
977 
978         EVTX_TEST_ASSERT_IS_NULL(
979          "error",
980          error );
981 
982 	return( 1 );
983 
984 on_error:
985 	if( error != NULL )
986 	{
987 		libcerror_error_free(
988 		 &error );
989 	}
990 	if( file != NULL )
991 	{
992 		libevtx_file_free(
993 		 &file,
994 		 NULL );
995 	}
996 	if( file_io_handle != NULL )
997 	{
998 		libbfio_handle_free(
999 		 &file_io_handle,
1000 		 NULL );
1001 	}
1002 	return( 0 );
1003 }
1004 
1005 /* Tests the libevtx_file_close function
1006  * Returns 1 if successful or 0 if not
1007  */
evtx_test_file_close(void)1008 int evtx_test_file_close(
1009      void )
1010 {
1011 	libcerror_error_t *error = NULL;
1012 	int result               = 0;
1013 
1014 	/* Test error cases
1015 	 */
1016 	result = libevtx_file_close(
1017 	          NULL,
1018 	          &error );
1019 
1020 	EVTX_TEST_ASSERT_EQUAL_INT(
1021 	 "result",
1022 	 result,
1023 	 -1 );
1024 
1025 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1026 	 "error",
1027 	 error );
1028 
1029 	libcerror_error_free(
1030 	 &error );
1031 
1032 	return( 1 );
1033 
1034 on_error:
1035 	if( error != NULL )
1036 	{
1037 		libcerror_error_free(
1038 		 &error );
1039 	}
1040 	return( 0 );
1041 }
1042 
1043 /* Tests the libevtx_file_open and libevtx_file_close functions
1044  * Returns 1 if successful or 0 if not
1045  */
evtx_test_file_open_close(const system_character_t * source)1046 int evtx_test_file_open_close(
1047      const system_character_t *source )
1048 {
1049 	libcerror_error_t *error = NULL;
1050 	libevtx_file_t *file     = NULL;
1051 	int result               = 0;
1052 
1053 	/* Initialize test
1054 	 */
1055 	result = libevtx_file_initialize(
1056 	          &file,
1057 	          &error );
1058 
1059 	EVTX_TEST_ASSERT_EQUAL_INT(
1060 	 "result",
1061 	 result,
1062 	 1 );
1063 
1064 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1065 	 "file",
1066 	 file );
1067 
1068 	EVTX_TEST_ASSERT_IS_NULL(
1069 	 "error",
1070 	 error );
1071 
1072 	/* Test open and close
1073 	 */
1074 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1075 	result = libevtx_file_open_wide(
1076 	          file,
1077 	          source,
1078 	          LIBEVTX_OPEN_READ,
1079 	          &error );
1080 #else
1081 	result = libevtx_file_open(
1082 	          file,
1083 	          source,
1084 	          LIBEVTX_OPEN_READ,
1085 	          &error );
1086 #endif
1087 
1088 	EVTX_TEST_ASSERT_EQUAL_INT(
1089 	 "result",
1090 	 result,
1091 	 1 );
1092 
1093 	EVTX_TEST_ASSERT_IS_NULL(
1094 	 "error",
1095 	 error );
1096 
1097 	result = libevtx_file_close(
1098 	          file,
1099 	          &error );
1100 
1101 	EVTX_TEST_ASSERT_EQUAL_INT(
1102 	 "result",
1103 	 result,
1104 	 0 );
1105 
1106 	EVTX_TEST_ASSERT_IS_NULL(
1107 	 "error",
1108 	 error );
1109 
1110 	/* Test open and close a second time to validate clean up on close
1111 	 */
1112 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1113 	result = libevtx_file_open_wide(
1114 	          file,
1115 	          source,
1116 	          LIBEVTX_OPEN_READ,
1117 	          &error );
1118 #else
1119 	result = libevtx_file_open(
1120 	          file,
1121 	          source,
1122 	          LIBEVTX_OPEN_READ,
1123 	          &error );
1124 #endif
1125 
1126 	EVTX_TEST_ASSERT_EQUAL_INT(
1127 	 "result",
1128 	 result,
1129 	 1 );
1130 
1131 	EVTX_TEST_ASSERT_IS_NULL(
1132 	 "error",
1133 	 error );
1134 
1135 	result = libevtx_file_close(
1136 	          file,
1137 	          &error );
1138 
1139 	EVTX_TEST_ASSERT_EQUAL_INT(
1140 	 "result",
1141 	 result,
1142 	 0 );
1143 
1144 	EVTX_TEST_ASSERT_IS_NULL(
1145 	 "error",
1146 	 error );
1147 
1148 	/* Clean up
1149 	 */
1150 	result = libevtx_file_free(
1151 	          &file,
1152 	          &error );
1153 
1154 	EVTX_TEST_ASSERT_EQUAL_INT(
1155 	 "result",
1156 	 result,
1157 	 1 );
1158 
1159 	EVTX_TEST_ASSERT_IS_NULL(
1160 	 "file",
1161 	 file );
1162 
1163 	EVTX_TEST_ASSERT_IS_NULL(
1164 	 "error",
1165 	 error );
1166 
1167 	return( 1 );
1168 
1169 on_error:
1170 	if( error != NULL )
1171 	{
1172 		libcerror_error_free(
1173 		 &error );
1174 	}
1175 	if( file != NULL )
1176 	{
1177 		libevtx_file_free(
1178 		 &file,
1179 		 NULL );
1180 	}
1181 	return( 0 );
1182 }
1183 
1184 /* Tests the libevtx_file_signal_abort function
1185  * Returns 1 if successful or 0 if not
1186  */
evtx_test_file_signal_abort(libevtx_file_t * file)1187 int evtx_test_file_signal_abort(
1188      libevtx_file_t *file )
1189 {
1190 	libcerror_error_t *error = NULL;
1191 	int result               = 0;
1192 
1193 	/* Test regular cases
1194 	 */
1195 	result = libevtx_file_signal_abort(
1196 	          file,
1197 	          &error );
1198 
1199 	EVTX_TEST_ASSERT_EQUAL_INT(
1200 	 "result",
1201 	 result,
1202 	 1 );
1203 
1204 	EVTX_TEST_ASSERT_IS_NULL(
1205 	 "error",
1206 	 error );
1207 
1208 	/* Test error cases
1209 	 */
1210 	result = libevtx_file_signal_abort(
1211 	          NULL,
1212 	          &error );
1213 
1214 	EVTX_TEST_ASSERT_EQUAL_INT(
1215 	 "result",
1216 	 result,
1217 	 -1 );
1218 
1219 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1220 	 "error",
1221 	 error );
1222 
1223 	libcerror_error_free(
1224 	 &error );
1225 
1226 	return( 1 );
1227 
1228 on_error:
1229 	if( error != NULL )
1230 	{
1231 		libcerror_error_free(
1232 		 &error );
1233 	}
1234 	return( 0 );
1235 }
1236 
1237 /* Tests the libevtx_file_get_ascii_codepage function
1238  * Returns 1 if successful or 0 if not
1239  */
evtx_test_file_get_ascii_codepage(libevtx_file_t * file)1240 int evtx_test_file_get_ascii_codepage(
1241      libevtx_file_t *file )
1242 {
1243 	libcerror_error_t *error = NULL;
1244 	int ascii_codepage       = 0;
1245 	int result               = 0;
1246 
1247 	/* Test regular cases
1248 	 */
1249 	result = libevtx_file_get_ascii_codepage(
1250 	          file,
1251 	          &ascii_codepage,
1252 	          &error );
1253 
1254 	EVTX_TEST_ASSERT_EQUAL_INT(
1255 	 "result",
1256 	 result,
1257 	 1 );
1258 
1259 	EVTX_TEST_ASSERT_IS_NULL(
1260 	 "error",
1261 	 error );
1262 
1263 	/* Test error cases
1264 	 */
1265 	result = libevtx_file_get_ascii_codepage(
1266 	          NULL,
1267 	          &ascii_codepage,
1268 	          &error );
1269 
1270 	EVTX_TEST_ASSERT_EQUAL_INT(
1271 	 "result",
1272 	 result,
1273 	 -1 );
1274 
1275 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1276 	 "error",
1277 	 error );
1278 
1279 	libcerror_error_free(
1280 	 &error );
1281 
1282 	result = libevtx_file_get_ascii_codepage(
1283 	          file,
1284 	          NULL,
1285 	          &error );
1286 
1287 	EVTX_TEST_ASSERT_EQUAL_INT(
1288 	 "result",
1289 	 result,
1290 	 -1 );
1291 
1292 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1293 	 "error",
1294 	 error );
1295 
1296 	libcerror_error_free(
1297 	 &error );
1298 
1299 	return( 1 );
1300 
1301 on_error:
1302 	if( error != NULL )
1303 	{
1304 		libcerror_error_free(
1305 		 &error );
1306 	}
1307 	return( 0 );
1308 }
1309 
1310 /* Tests the libevtx_file_set_ascii_codepage function
1311  * Returns 1 if successful or 0 if not
1312  */
evtx_test_file_set_ascii_codepage(libevtx_file_t * file)1313 int evtx_test_file_set_ascii_codepage(
1314      libevtx_file_t *file )
1315 {
1316 	int supported_codepages[ 15 ] = {
1317 		LIBEVTX_CODEPAGE_ASCII,
1318 		LIBEVTX_CODEPAGE_WINDOWS_874,
1319 		LIBEVTX_CODEPAGE_WINDOWS_932,
1320 		LIBEVTX_CODEPAGE_WINDOWS_936,
1321 		LIBEVTX_CODEPAGE_WINDOWS_949,
1322 		LIBEVTX_CODEPAGE_WINDOWS_950,
1323 		LIBEVTX_CODEPAGE_WINDOWS_1250,
1324 		LIBEVTX_CODEPAGE_WINDOWS_1251,
1325 		LIBEVTX_CODEPAGE_WINDOWS_1252,
1326 		LIBEVTX_CODEPAGE_WINDOWS_1253,
1327 		LIBEVTX_CODEPAGE_WINDOWS_1254,
1328 		LIBEVTX_CODEPAGE_WINDOWS_1255,
1329 		LIBEVTX_CODEPAGE_WINDOWS_1256,
1330 		LIBEVTX_CODEPAGE_WINDOWS_1257,
1331 		LIBEVTX_CODEPAGE_WINDOWS_1258 };
1332 
1333 	int unsupported_codepages[ 17 ] = {
1334 		LIBEVTX_CODEPAGE_ISO_8859_1,
1335 		LIBEVTX_CODEPAGE_ISO_8859_2,
1336 		LIBEVTX_CODEPAGE_ISO_8859_3,
1337 		LIBEVTX_CODEPAGE_ISO_8859_4,
1338 		LIBEVTX_CODEPAGE_ISO_8859_5,
1339 		LIBEVTX_CODEPAGE_ISO_8859_6,
1340 		LIBEVTX_CODEPAGE_ISO_8859_7,
1341 		LIBEVTX_CODEPAGE_ISO_8859_8,
1342 		LIBEVTX_CODEPAGE_ISO_8859_9,
1343 		LIBEVTX_CODEPAGE_ISO_8859_10,
1344 		LIBEVTX_CODEPAGE_ISO_8859_11,
1345 		LIBEVTX_CODEPAGE_ISO_8859_13,
1346 		LIBEVTX_CODEPAGE_ISO_8859_14,
1347 		LIBEVTX_CODEPAGE_ISO_8859_15,
1348 		LIBEVTX_CODEPAGE_ISO_8859_16,
1349 		LIBEVTX_CODEPAGE_KOI8_R,
1350 		LIBEVTX_CODEPAGE_KOI8_U };
1351 
1352 	libcerror_error_t *error = NULL;
1353 	int codepage             = 0;
1354 	int index                = 0;
1355 	int result               = 0;
1356 
1357 	/* Test set ASCII codepage
1358 	 */
1359 	for( index = 0;
1360 	     index < 15;
1361 	     index++ )
1362 	{
1363 		codepage = supported_codepages[ index ];
1364 
1365 		result = libevtx_file_set_ascii_codepage(
1366 		          file,
1367 		          codepage,
1368 		          &error );
1369 
1370 		EVTX_TEST_ASSERT_EQUAL_INT(
1371 		 "result",
1372 		 result,
1373 		 1 );
1374 
1375 		EVTX_TEST_ASSERT_IS_NULL(
1376 		 "error",
1377 		 error );
1378 	}
1379 	/* Test error cases
1380 	 */
1381 	result = libevtx_file_set_ascii_codepage(
1382 	          NULL,
1383 	          LIBEVTX_CODEPAGE_ASCII,
1384 	          &error );
1385 
1386 	EVTX_TEST_ASSERT_EQUAL_INT(
1387 	 "result",
1388 	 result,
1389 	 -1 );
1390 
1391 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1392 	 "error",
1393 	 error );
1394 
1395 	libcerror_error_free(
1396 	 &error );
1397 
1398 	for( index = 0;
1399 	     index < 17;
1400 	     index++ )
1401 	{
1402 		codepage = unsupported_codepages[ index ];
1403 
1404 		result = libevtx_file_set_ascii_codepage(
1405 		          file,
1406 		          codepage,
1407 		          &error );
1408 
1409 		EVTX_TEST_ASSERT_EQUAL_INT(
1410 		 "result",
1411 		 result,
1412 		 -1 );
1413 
1414 		EVTX_TEST_ASSERT_IS_NOT_NULL(
1415 		 "error",
1416 		 error );
1417 
1418 		libcerror_error_free(
1419 		 &error );
1420 	}
1421 	/* Clean up
1422 	 */
1423 	result = libevtx_file_set_ascii_codepage(
1424 	          file,
1425 	          LIBEVTX_CODEPAGE_WINDOWS_1252,
1426 	          &error );
1427 
1428 	EVTX_TEST_ASSERT_EQUAL_INT(
1429 	 "result",
1430 	 result,
1431 	 1 );
1432 
1433 	EVTX_TEST_ASSERT_IS_NULL(
1434 	 "error",
1435 	 error );
1436 
1437 	return( 1 );
1438 
1439 on_error:
1440 	if( error != NULL )
1441 	{
1442 		libcerror_error_free(
1443 		 &error );
1444 	}
1445 	return( 0 );
1446 }
1447 
1448 /* Tests the libevtx_file_get_flags function
1449  * Returns 1 if successful or 0 if not
1450  */
evtx_test_file_get_flags(libevtx_file_t * file)1451 int evtx_test_file_get_flags(
1452      libevtx_file_t *file )
1453 {
1454 	libcerror_error_t *error = NULL;
1455 	uint32_t flags           = 0;
1456 	int result               = 0;
1457 
1458 	/* Test regular cases
1459 	 */
1460 	result = libevtx_file_get_flags(
1461 	          file,
1462 	          &flags,
1463 	          &error );
1464 
1465 	EVTX_TEST_ASSERT_EQUAL_INT(
1466 	 "result",
1467 	 result,
1468 	 1 );
1469 
1470 	EVTX_TEST_ASSERT_IS_NULL(
1471 	 "error",
1472 	 error );
1473 
1474 	/* Test error cases
1475 	 */
1476 	result = libevtx_file_get_flags(
1477 	          NULL,
1478 	          &flags,
1479 	          &error );
1480 
1481 	EVTX_TEST_ASSERT_EQUAL_INT(
1482 	 "result",
1483 	 result,
1484 	 -1 );
1485 
1486 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1487 	 "error",
1488 	 error );
1489 
1490 	libcerror_error_free(
1491 	 &error );
1492 
1493 	result = libevtx_file_get_flags(
1494 	          file,
1495 	          NULL,
1496 	          &error );
1497 
1498 	EVTX_TEST_ASSERT_EQUAL_INT(
1499 	 "result",
1500 	 result,
1501 	 -1 );
1502 
1503 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1504 	 "error",
1505 	 error );
1506 
1507 	libcerror_error_free(
1508 	 &error );
1509 
1510 	return( 1 );
1511 
1512 on_error:
1513 	if( error != NULL )
1514 	{
1515 		libcerror_error_free(
1516 		 &error );
1517 	}
1518 	return( 0 );
1519 }
1520 
1521 /* Tests the libevtx_file_get_number_of_records function
1522  * Returns 1 if successful or 0 if not
1523  */
evtx_test_file_get_number_of_records(libevtx_file_t * file)1524 int evtx_test_file_get_number_of_records(
1525      libevtx_file_t *file )
1526 {
1527 	libcerror_error_t *error = NULL;
1528 	int number_of_records    = 0;
1529 	int result               = 0;
1530 
1531 	/* Test regular cases
1532 	 */
1533 	result = libevtx_file_get_number_of_records(
1534 	          file,
1535 	          &number_of_records,
1536 	          &error );
1537 
1538 	EVTX_TEST_ASSERT_EQUAL_INT(
1539 	 "result",
1540 	 result,
1541 	 1 );
1542 
1543 	EVTX_TEST_ASSERT_IS_NULL(
1544 	 "error",
1545 	 error );
1546 
1547 	/* Test error cases
1548 	 */
1549 	result = libevtx_file_get_number_of_records(
1550 	          NULL,
1551 	          &number_of_records,
1552 	          &error );
1553 
1554 	EVTX_TEST_ASSERT_EQUAL_INT(
1555 	 "result",
1556 	 result,
1557 	 -1 );
1558 
1559 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1560 	 "error",
1561 	 error );
1562 
1563 	libcerror_error_free(
1564 	 &error );
1565 
1566 	result = libevtx_file_get_number_of_records(
1567 	          file,
1568 	          NULL,
1569 	          &error );
1570 
1571 	EVTX_TEST_ASSERT_EQUAL_INT(
1572 	 "result",
1573 	 result,
1574 	 -1 );
1575 
1576 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1577 	 "error",
1578 	 error );
1579 
1580 	libcerror_error_free(
1581 	 &error );
1582 
1583 	return( 1 );
1584 
1585 on_error:
1586 	if( error != NULL )
1587 	{
1588 		libcerror_error_free(
1589 		 &error );
1590 	}
1591 	return( 0 );
1592 }
1593 
1594 /* Tests the libevtx_file_get_record_by_index function
1595  * Returns 1 if successful or 0 if not
1596  */
evtx_test_file_get_record_by_index(libevtx_file_t * file)1597 int evtx_test_file_get_record_by_index(
1598      libevtx_file_t *file )
1599 {
1600 	libcerror_error_t *error = NULL;
1601 	libevtx_record_t *record = 0;
1602 	int number_of_records    = 0;
1603 	int result               = 0;
1604 
1605 	/* Initialize test
1606 	 */
1607 	result = libevtx_file_get_number_of_records(
1608 	          file,
1609 	          &number_of_records,
1610 	          &error );
1611 
1612 	EVTX_TEST_ASSERT_EQUAL_INT(
1613 	 "result",
1614 	 result,
1615 	 1 );
1616 
1617 	EVTX_TEST_ASSERT_IS_NULL(
1618 	 "error",
1619 	 error );
1620 
1621 	if( number_of_records == 0 )
1622 	{
1623 		return( 1 );
1624 	}
1625 	/* Test regular cases
1626 	 */
1627 	result = libevtx_file_get_record_by_index(
1628 	          file,
1629 	          0,
1630 	          &record,
1631 	          &error );
1632 
1633 	/* TODO: remove after troubleshooting failing tests */
1634 	EVTX_TEST_FPRINT_ERROR( error );
1635 
1636 	EVTX_TEST_ASSERT_EQUAL_INT(
1637 	 "result",
1638 	 result,
1639 	 1 );
1640 
1641 	EVTX_TEST_ASSERT_IS_NULL(
1642 	 "error",
1643 	 error );
1644 
1645 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1646 	 "record",
1647 	 record );
1648 
1649 	result = libevtx_record_free(
1650 	          &record,
1651 	          &error );
1652 
1653 	EVTX_TEST_ASSERT_EQUAL_INT(
1654 	 "result",
1655 	 result,
1656 	 1 );
1657 
1658 	EVTX_TEST_ASSERT_IS_NULL(
1659 	 "error",
1660 	 error );
1661 
1662 	/* Test error cases
1663 	 */
1664 	result = libevtx_file_get_record_by_index(
1665 	          NULL,
1666 	          0,
1667 	          &record,
1668 	          &error );
1669 
1670 	EVTX_TEST_ASSERT_EQUAL_INT(
1671 	 "result",
1672 	 result,
1673 	 -1 );
1674 
1675 	EVTX_TEST_ASSERT_IS_NULL(
1676 	 "record",
1677 	 record );
1678 
1679 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1680 	 "error",
1681 	 error );
1682 
1683 	libcerror_error_free(
1684 	 &error );
1685 
1686 	result = libevtx_file_get_record_by_index(
1687 	          file,
1688 	          -1,
1689 	          &record,
1690 	          &error );
1691 
1692 	EVTX_TEST_ASSERT_EQUAL_INT(
1693 	 "result",
1694 	 result,
1695 	 -1 );
1696 
1697 	EVTX_TEST_ASSERT_IS_NULL(
1698 	 "record",
1699 	 record );
1700 
1701 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1702 	 "error",
1703 	 error );
1704 
1705 	libcerror_error_free(
1706 	 &error );
1707 
1708 	result = libevtx_file_get_record_by_index(
1709 	          file,
1710 	          0,
1711 	          NULL,
1712 	          &error );
1713 
1714 	EVTX_TEST_ASSERT_EQUAL_INT(
1715 	 "result",
1716 	 result,
1717 	 -1 );
1718 
1719 	EVTX_TEST_ASSERT_IS_NULL(
1720 	 "record",
1721 	 record );
1722 
1723 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1724 	 "error",
1725 	 error );
1726 
1727 	libcerror_error_free(
1728 	 &error );
1729 
1730 	return( 1 );
1731 
1732 on_error:
1733 	if( error != NULL )
1734 	{
1735 		libcerror_error_free(
1736 		 &error );
1737 	}
1738 	return( 0 );
1739 }
1740 
1741 /* Tests the libevtx_file_get_number_of_recovered_records function
1742  * Returns 1 if successful or 0 if not
1743  */
evtx_test_file_get_number_of_recovered_records(libevtx_file_t * file)1744 int evtx_test_file_get_number_of_recovered_records(
1745      libevtx_file_t *file )
1746 {
1747 	libcerror_error_t *error        = NULL;
1748 	int number_of_recovered_records = 0;
1749 	int result                      = 0;
1750 
1751 	/* Test regular cases
1752 	 */
1753 	result = libevtx_file_get_number_of_recovered_records(
1754 	          file,
1755 	          &number_of_recovered_records,
1756 	          &error );
1757 
1758 	EVTX_TEST_ASSERT_EQUAL_INT(
1759 	 "result",
1760 	 result,
1761 	 1 );
1762 
1763 	EVTX_TEST_ASSERT_IS_NULL(
1764 	 "error",
1765 	 error );
1766 
1767 	/* Test error cases
1768 	 */
1769 	result = libevtx_file_get_number_of_recovered_records(
1770 	          NULL,
1771 	          &number_of_recovered_records,
1772 	          &error );
1773 
1774 	EVTX_TEST_ASSERT_EQUAL_INT(
1775 	 "result",
1776 	 result,
1777 	 -1 );
1778 
1779 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1780 	 "error",
1781 	 error );
1782 
1783 	libcerror_error_free(
1784 	 &error );
1785 
1786 	result = libevtx_file_get_number_of_recovered_records(
1787 	          file,
1788 	          NULL,
1789 	          &error );
1790 
1791 	EVTX_TEST_ASSERT_EQUAL_INT(
1792 	 "result",
1793 	 result,
1794 	 -1 );
1795 
1796 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1797 	 "error",
1798 	 error );
1799 
1800 	libcerror_error_free(
1801 	 &error );
1802 
1803 	return( 1 );
1804 
1805 on_error:
1806 	if( error != NULL )
1807 	{
1808 		libcerror_error_free(
1809 		 &error );
1810 	}
1811 	return( 0 );
1812 }
1813 
1814 /* Tests the libevtx_file_get_recovered_record_by_index function
1815  * Returns 1 if successful or 0 if not
1816  */
evtx_test_file_get_recovered_record_by_index(libevtx_file_t * file)1817 int evtx_test_file_get_recovered_record_by_index(
1818      libevtx_file_t *file )
1819 {
1820 	libcerror_error_t *error           = NULL;
1821 	libevtx_record_t *recovered_record = 0;
1822 	int number_of_recovered_records    = 0;
1823 	int result                         = 0;
1824 
1825 	/* Initialize test
1826 	 */
1827 	result = libevtx_file_get_number_of_recovered_records(
1828 	          file,
1829 	          &number_of_recovered_records,
1830 	          &error );
1831 
1832 	EVTX_TEST_ASSERT_EQUAL_INT(
1833 	 "result",
1834 	 result,
1835 	 1 );
1836 
1837 	EVTX_TEST_ASSERT_IS_NULL(
1838 	 "error",
1839 	 error );
1840 
1841 	if( number_of_recovered_records == 0 )
1842 	{
1843 		return( 1 );
1844 	}
1845 	/* Test regular cases
1846 	 */
1847 	result = libevtx_file_get_recovered_record_by_index(
1848 	          file,
1849 	          0,
1850 	          &recovered_record,
1851 	          &error );
1852 
1853 	EVTX_TEST_ASSERT_EQUAL_INT(
1854 	 "result",
1855 	 result,
1856 	 1 );
1857 
1858 	EVTX_TEST_ASSERT_IS_NULL(
1859 	 "error",
1860 	 error );
1861 
1862 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1863 	 "recovered_record",
1864 	 recovered_record );
1865 
1866 	result = libevtx_record_free(
1867 	          &recovered_record,
1868 	          &error );
1869 
1870 	EVTX_TEST_ASSERT_EQUAL_INT(
1871 	 "result",
1872 	 result,
1873 	 1 );
1874 
1875 	EVTX_TEST_ASSERT_IS_NULL(
1876 	 "error",
1877 	 error );
1878 
1879 	/* Test error cases
1880 	 */
1881 	result = libevtx_file_get_recovered_record_by_index(
1882 	          NULL,
1883 	          0,
1884 	          &recovered_record,
1885 	          &error );
1886 
1887 	EVTX_TEST_ASSERT_EQUAL_INT(
1888 	 "result",
1889 	 result,
1890 	 -1 );
1891 
1892 	EVTX_TEST_ASSERT_IS_NULL(
1893 	 "recovered_record",
1894 	 recovered_record );
1895 
1896 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1897 	 "error",
1898 	 error );
1899 
1900 	libcerror_error_free(
1901 	 &error );
1902 
1903 	result = libevtx_file_get_recovered_record_by_index(
1904 	          file,
1905 	          -1,
1906 	          &recovered_record,
1907 	          &error );
1908 
1909 	EVTX_TEST_ASSERT_EQUAL_INT(
1910 	 "result",
1911 	 result,
1912 	 -1 );
1913 
1914 	EVTX_TEST_ASSERT_IS_NULL(
1915 	 "recovered_record",
1916 	 recovered_record );
1917 
1918 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1919 	 "error",
1920 	 error );
1921 
1922 	libcerror_error_free(
1923 	 &error );
1924 
1925 	result = libevtx_file_get_recovered_record_by_index(
1926 	          file,
1927 	          0,
1928 	          NULL,
1929 	          &error );
1930 
1931 	EVTX_TEST_ASSERT_EQUAL_INT(
1932 	 "result",
1933 	 result,
1934 	 -1 );
1935 
1936 	EVTX_TEST_ASSERT_IS_NULL(
1937 	 "recovered_record",
1938 	 recovered_record );
1939 
1940 	EVTX_TEST_ASSERT_IS_NOT_NULL(
1941 	 "error",
1942 	 error );
1943 
1944 	libcerror_error_free(
1945 	 &error );
1946 
1947 	return( 1 );
1948 
1949 on_error:
1950 	if( error != NULL )
1951 	{
1952 		libcerror_error_free(
1953 		 &error );
1954 	}
1955 	return( 0 );
1956 }
1957 
1958 /* The main program
1959  */
1960 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc,wchar_t * const argv[])1961 int wmain(
1962      int argc,
1963      wchar_t * const argv[] )
1964 #else
1965 int main(
1966      int argc,
1967      char * const argv[] )
1968 #endif
1969 {
1970 	libbfio_handle_t *file_io_handle = NULL;
1971 	libcerror_error_t *error         = NULL;
1972 	libevtx_file_t *file             = NULL;
1973 	system_character_t *source       = NULL;
1974 	system_integer_t option          = 0;
1975 	size_t string_length             = 0;
1976 	int result                       = 0;
1977 
1978 	while( ( option = evtx_test_getopt(
1979 	                   argc,
1980 	                   argv,
1981 	                   _SYSTEM_STRING( "" ) ) ) != (system_integer_t) -1 )
1982 	{
1983 		switch( option )
1984 		{
1985 			case (system_integer_t) '?':
1986 			default:
1987 				fprintf(
1988 				 stderr,
1989 				 "Invalid argument: %" PRIs_SYSTEM ".\n",
1990 				 argv[ optind - 1 ] );
1991 
1992 				return( EXIT_FAILURE );
1993 		}
1994 	}
1995 	if( optind < argc )
1996 	{
1997 		source = argv[ optind ];
1998 	}
1999 #if defined( HAVE_DEBUG_OUTPUT ) && defined( EVTX_TEST_FILE_VERBOSE )
2000 	libevtx_notify_set_verbose(
2001 	 1 );
2002 	libevtx_notify_set_stream(
2003 	 stderr,
2004 	 NULL );
2005 #endif
2006 
2007 	EVTX_TEST_RUN(
2008 	 "libevtx_file_initialize",
2009 	 evtx_test_file_initialize );
2010 
2011 	EVTX_TEST_RUN(
2012 	 "libevtx_file_free",
2013 	 evtx_test_file_free );
2014 
2015 #if !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 )
2016 	if( source != NULL )
2017 	{
2018 		result = libbfio_file_initialize(
2019 		          &file_io_handle,
2020 		          &error );
2021 
2022 		EVTX_TEST_ASSERT_EQUAL_INT(
2023 		 "result",
2024 		 result,
2025 		 1 );
2026 
2027 	        EVTX_TEST_ASSERT_IS_NOT_NULL(
2028 	         "file_io_handle",
2029 	         file_io_handle );
2030 
2031 	        EVTX_TEST_ASSERT_IS_NULL(
2032 	         "error",
2033 	         error );
2034 
2035 		string_length = system_string_length(
2036 		                 source );
2037 
2038 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
2039 		result = libbfio_file_set_name_wide(
2040 		          file_io_handle,
2041 		          source,
2042 		          string_length,
2043 		          &error );
2044 #else
2045 		result = libbfio_file_set_name(
2046 		          file_io_handle,
2047 		          source,
2048 		          string_length,
2049 		          &error );
2050 #endif
2051 		EVTX_TEST_ASSERT_EQUAL_INT(
2052 		 "result",
2053 		 result,
2054 		 1 );
2055 
2056 	        EVTX_TEST_ASSERT_IS_NULL(
2057 	         "error",
2058 	         error );
2059 
2060 		result = libevtx_check_file_signature_file_io_handle(
2061 		          file_io_handle,
2062 		          &error );
2063 
2064 		EVTX_TEST_ASSERT_NOT_EQUAL_INT(
2065 		 "result",
2066 		 result,
2067 		 -1 );
2068 
2069 		EVTX_TEST_ASSERT_IS_NULL(
2070 		 "error",
2071 		 error );
2072 	}
2073 	if( result != 0 )
2074 	{
2075 		EVTX_TEST_RUN_WITH_ARGS(
2076 		 "libevtx_file_open",
2077 		 evtx_test_file_open,
2078 		 source );
2079 
2080 #if defined( HAVE_WIDE_CHARACTER_TYPE )
2081 
2082 		EVTX_TEST_RUN_WITH_ARGS(
2083 		 "libevtx_file_open_wide",
2084 		 evtx_test_file_open_wide,
2085 		 source );
2086 
2087 #endif /* defined( HAVE_WIDE_CHARACTER_TYPE ) */
2088 
2089 		EVTX_TEST_RUN_WITH_ARGS(
2090 		 "libevtx_file_open_file_io_handle",
2091 		 evtx_test_file_open_file_io_handle,
2092 		 source );
2093 
2094 		EVTX_TEST_RUN(
2095 		 "libevtx_file_close",
2096 		 evtx_test_file_close );
2097 
2098 		EVTX_TEST_RUN_WITH_ARGS(
2099 		 "libevtx_file_open_close",
2100 		 evtx_test_file_open_close,
2101 		 source );
2102 
2103 		/* Initialize file for tests
2104 		 */
2105 		result = evtx_test_file_open_source(
2106 		          &file,
2107 		          file_io_handle,
2108 		          &error );
2109 
2110 		EVTX_TEST_ASSERT_EQUAL_INT(
2111 		 "result",
2112 		 result,
2113 		 1 );
2114 
2115 		EVTX_TEST_ASSERT_IS_NOT_NULL(
2116 		 "file",
2117 		 file );
2118 
2119 		EVTX_TEST_ASSERT_IS_NULL(
2120 		 "error",
2121 		 error );
2122 
2123 		EVTX_TEST_RUN_WITH_ARGS(
2124 		 "libevtx_file_signal_abort",
2125 		 evtx_test_file_signal_abort,
2126 		 file );
2127 
2128 #if defined( __GNUC__ ) && !defined( LIBEVTX_DLL_IMPORT )
2129 
2130 		/* TODO: add tests for libevtx_file_open_read */
2131 
2132 #endif /* defined( __GNUC__ ) && !defined( LIBEVTX_DLL_IMPORT ) */
2133 
2134 		/* TODO: add tests for libevtx_file_is_corrupted */
2135 
2136 		EVTX_TEST_RUN_WITH_ARGS(
2137 		 "libevtx_file_get_ascii_codepage",
2138 		 evtx_test_file_get_ascii_codepage,
2139 		 file );
2140 
2141 		EVTX_TEST_RUN_WITH_ARGS(
2142 		 "libevtx_file_set_ascii_codepage",
2143 		 evtx_test_file_set_ascii_codepage,
2144 		 file );
2145 
2146 		/* TODO: add tests for libevtx_file_get_format_version */
2147 
2148 		/* TODO: add tests for libevtx_file_get_version */
2149 
2150 		EVTX_TEST_RUN_WITH_ARGS(
2151 		 "libevtx_file_get_flags",
2152 		 evtx_test_file_get_flags,
2153 		 file );
2154 
2155 		EVTX_TEST_RUN_WITH_ARGS(
2156 		 "libevtx_file_get_number_of_records",
2157 		 evtx_test_file_get_number_of_records,
2158 		 file );
2159 
2160 		EVTX_TEST_RUN_WITH_ARGS(
2161 		 "libevtx_file_get_record_by_index",
2162 		 evtx_test_file_get_record_by_index,
2163 		 file );
2164 
2165 		EVTX_TEST_RUN_WITH_ARGS(
2166 		 "libevtx_file_get_number_of_recovered_records",
2167 		 evtx_test_file_get_number_of_recovered_records,
2168 		 file );
2169 
2170 #if defined( TODO )
2171 
2172 		EVTX_TEST_RUN_WITH_ARGS(
2173 		 "libevtx_file_get_recovered_record_by_index",
2174 		 evtx_test_file_get_recovered_record_by_index,
2175 		 file );
2176 
2177 #endif /* defined( TODO ) */
2178 
2179 		/* Clean up
2180 		 */
2181 		result = evtx_test_file_close_source(
2182 		          &file,
2183 		          &error );
2184 
2185 		EVTX_TEST_ASSERT_EQUAL_INT(
2186 		 "result",
2187 		 result,
2188 		 0 );
2189 
2190 		EVTX_TEST_ASSERT_IS_NULL(
2191 		 "file",
2192 		 file );
2193 
2194 		EVTX_TEST_ASSERT_IS_NULL(
2195 		 "error",
2196 		 error );
2197 
2198 		result = libbfio_handle_free(
2199 		          &file_io_handle,
2200 		          &error );
2201 
2202 		EVTX_TEST_ASSERT_EQUAL_INT(
2203 		 "result",
2204 		 result,
2205 		 1 );
2206 
2207 		EVTX_TEST_ASSERT_IS_NULL(
2208 	         "file_io_handle",
2209 	         file_io_handle );
2210 
2211 	        EVTX_TEST_ASSERT_IS_NULL(
2212 	         "error",
2213 	         error );
2214 	}
2215 #endif /* !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 ) */
2216 
2217 	return( EXIT_SUCCESS );
2218 
2219 on_error:
2220 	if( error != NULL )
2221 	{
2222 		libcerror_error_free(
2223 		 &error );
2224 	}
2225 	if( file != NULL )
2226 	{
2227 		libevtx_file_free(
2228 		 &file,
2229 		 NULL );
2230 	}
2231 	if( file_io_handle != NULL )
2232 	{
2233 		libbfio_handle_free(
2234 		 &file_io_handle,
2235 		 NULL );
2236 	}
2237 	return( EXIT_FAILURE );
2238 }
2239 
2240