1 /*
2  * Library chunk 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 "evtx_test_libcerror.h"
31 #include "evtx_test_libevtx.h"
32 #include "evtx_test_macros.h"
33 #include "evtx_test_memory.h"
34 #include "evtx_test_unused.h"
35 
36 #include "../libevtx/libevtx_chunk.h"
37 
38 #if defined( __GNUC__ ) && !defined( LIBEVTX_DLL_IMPORT )
39 
40 /* Tests the libevtx_chunk_initialize function
41  * Returns 1 if successful or 0 if not
42  */
evtx_test_chunk_initialize(void)43 int evtx_test_chunk_initialize(
44      void )
45 {
46 	libcerror_error_t *error        = NULL;
47 	libevtx_chunk_t *chunk          = NULL;
48 	int result                      = 0;
49 
50 #if defined( HAVE_EVTX_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 = libevtx_chunk_initialize(
59 	          &chunk,
60 	          &error );
61 
62 	EVTX_TEST_ASSERT_EQUAL_INT(
63 	 "result",
64 	 result,
65 	 1 );
66 
67 	EVTX_TEST_ASSERT_IS_NOT_NULL(
68 	 "chunk",
69 	 chunk );
70 
71 	EVTX_TEST_ASSERT_IS_NULL(
72 	 "error",
73 	 error );
74 
75 	result = libevtx_chunk_free(
76 	          &chunk,
77 	          &error );
78 
79 	EVTX_TEST_ASSERT_EQUAL_INT(
80 	 "result",
81 	 result,
82 	 1 );
83 
84 	EVTX_TEST_ASSERT_IS_NULL(
85 	 "chunk",
86 	 chunk );
87 
88 	EVTX_TEST_ASSERT_IS_NULL(
89 	 "error",
90 	 error );
91 
92 	/* Test error cases
93 	 */
94 	result = libevtx_chunk_initialize(
95 	          NULL,
96 	          &error );
97 
98 	EVTX_TEST_ASSERT_EQUAL_INT(
99 	 "result",
100 	 result,
101 	 -1 );
102 
103 	EVTX_TEST_ASSERT_IS_NOT_NULL(
104 	 "error",
105 	 error );
106 
107 	libcerror_error_free(
108 	 &error );
109 
110 	chunk = (libevtx_chunk_t *) 0x12345678UL;
111 
112 	result = libevtx_chunk_initialize(
113 	          &chunk,
114 	          &error );
115 
116 	EVTX_TEST_ASSERT_EQUAL_INT(
117 	 "result",
118 	 result,
119 	 -1 );
120 
121 	EVTX_TEST_ASSERT_IS_NOT_NULL(
122 	 "error",
123 	 error );
124 
125 	libcerror_error_free(
126 	 &error );
127 
128 	chunk = NULL;
129 
130 #if defined( HAVE_EVTX_TEST_MEMORY )
131 
132 	for( test_number = 0;
133 	     test_number < number_of_malloc_fail_tests;
134 	     test_number++ )
135 	{
136 		/* Test libevtx_chunk_initialize with malloc failing
137 		 */
138 		evtx_test_malloc_attempts_before_fail = test_number;
139 
140 		result = libevtx_chunk_initialize(
141 		          &chunk,
142 		          &error );
143 
144 		if( evtx_test_malloc_attempts_before_fail != -1 )
145 		{
146 			evtx_test_malloc_attempts_before_fail = -1;
147 
148 			if( chunk != NULL )
149 			{
150 				libevtx_chunk_free(
151 				 &chunk,
152 				 NULL );
153 			}
154 		}
155 		else
156 		{
157 			EVTX_TEST_ASSERT_EQUAL_INT(
158 			 "result",
159 			 result,
160 			 -1 );
161 
162 			EVTX_TEST_ASSERT_IS_NULL(
163 			 "chunk",
164 			 chunk );
165 
166 			EVTX_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 libevtx_chunk_initialize with memset failing
179 		 */
180 		evtx_test_memset_attempts_before_fail = test_number;
181 
182 		result = libevtx_chunk_initialize(
183 		          &chunk,
184 		          &error );
185 
186 		if( evtx_test_memset_attempts_before_fail != -1 )
187 		{
188 			evtx_test_memset_attempts_before_fail = -1;
189 
190 			if( chunk != NULL )
191 			{
192 				libevtx_chunk_free(
193 				 &chunk,
194 				 NULL );
195 			}
196 		}
197 		else
198 		{
199 			EVTX_TEST_ASSERT_EQUAL_INT(
200 			 "result",
201 			 result,
202 			 -1 );
203 
204 			EVTX_TEST_ASSERT_IS_NULL(
205 			 "chunk",
206 			 chunk );
207 
208 			EVTX_TEST_ASSERT_IS_NOT_NULL(
209 			 "error",
210 			 error );
211 
212 			libcerror_error_free(
213 			 &error );
214 		}
215 	}
216 #endif /* defined( HAVE_EVTX_TEST_MEMORY ) */
217 
218 	return( 1 );
219 
220 on_error:
221 	if( error != NULL )
222 	{
223 		libcerror_error_free(
224 		 &error );
225 	}
226 	if( chunk != NULL )
227 	{
228 		libevtx_chunk_free(
229 		 &chunk,
230 		 NULL );
231 	}
232 	return( 0 );
233 }
234 
235 /* Tests the libevtx_chunk_free function
236  * Returns 1 if successful or 0 if not
237  */
evtx_test_chunk_free(void)238 int evtx_test_chunk_free(
239      void )
240 {
241 	libcerror_error_t *error = NULL;
242 	int result               = 0;
243 
244 	/* Test error cases
245 	 */
246 	result = libevtx_chunk_free(
247 	          NULL,
248 	          &error );
249 
250 	EVTX_TEST_ASSERT_EQUAL_INT(
251 	 "result",
252 	 result,
253 	 -1 );
254 
255 	EVTX_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 libevtx_chunk_get_number_of_records function
274  * Returns 1 if successful or 0 if not
275  */
evtx_test_chunk_get_number_of_records(void)276 int evtx_test_chunk_get_number_of_records(
277      void )
278 {
279 	libcerror_error_t *error     = NULL;
280 	libevtx_chunk_t *chunk       = NULL;
281 	uint16_t number_of_records   = 0;
282 	int number_of_records_is_set = 0;
283 	int result                   = 0;
284 
285 	/* Initialize test
286 	 */
287 	result = libevtx_chunk_initialize(
288 	          &chunk,
289 	          &error );
290 
291 	EVTX_TEST_ASSERT_EQUAL_INT(
292 	 "result",
293 	 result,
294 	 1 );
295 
296 	EVTX_TEST_ASSERT_IS_NOT_NULL(
297 	 "chunk",
298 	 chunk );
299 
300 	EVTX_TEST_ASSERT_IS_NULL(
301 	 "error",
302 	 error );
303 
304 	/* Test regular cases
305 	 */
306 	result = libevtx_chunk_get_number_of_records(
307 	          chunk,
308 	          &number_of_records,
309 	          &error );
310 
311 	EVTX_TEST_ASSERT_NOT_EQUAL_INT(
312 	 "result",
313 	 result,
314 	 -1 );
315 
316 	EVTX_TEST_ASSERT_IS_NULL(
317 	 "error",
318 	 error );
319 
320 	number_of_records_is_set = result;
321 
322 	/* Test error cases
323 	 */
324 	result = libevtx_chunk_get_number_of_records(
325 	          NULL,
326 	          &number_of_records,
327 	          &error );
328 
329 	EVTX_TEST_ASSERT_EQUAL_INT(
330 	 "result",
331 	 result,
332 	 -1 );
333 
334 	EVTX_TEST_ASSERT_IS_NOT_NULL(
335 	 "error",
336 	 error );
337 
338 	libcerror_error_free(
339 	 &error );
340 
341 	if( number_of_records_is_set != 0 )
342 	{
343 		result = libevtx_chunk_get_number_of_records(
344 		          chunk,
345 		          NULL,
346 		          &error );
347 
348 		EVTX_TEST_ASSERT_EQUAL_INT(
349 		 "result",
350 		 result,
351 		 -1 );
352 
353 		EVTX_TEST_ASSERT_IS_NOT_NULL(
354 		 "error",
355 		 error );
356 
357 		libcerror_error_free(
358 		 &error );
359 	}
360 	/* Clean up
361 	 */
362 	result = libevtx_chunk_free(
363 	          &chunk,
364 	          &error );
365 
366 	EVTX_TEST_ASSERT_EQUAL_INT(
367 	 "result",
368 	 result,
369 	 1 );
370 
371 	EVTX_TEST_ASSERT_IS_NULL(
372 	 "chunk",
373 	 chunk );
374 
375 	EVTX_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( chunk != NULL )
388 	{
389 		libevtx_chunk_free(
390 		 &chunk,
391 		 NULL );
392 	}
393 	return( 0 );
394 }
395 
396 /* Tests the libevtx_chunk_get_number_of_recovered_records function
397  * Returns 1 if successful or 0 if not
398  */
evtx_test_chunk_get_number_of_recovered_records(void)399 int evtx_test_chunk_get_number_of_recovered_records(
400      void )
401 {
402 	libcerror_error_t *error               = NULL;
403 	libevtx_chunk_t *chunk                 = NULL;
404 	uint16_t number_of_recovered_records   = 0;
405 	int number_of_recovered_records_is_set = 0;
406 	int result                             = 0;
407 
408 	/* Initialize test
409 	 */
410 	result = libevtx_chunk_initialize(
411 	          &chunk,
412 	          &error );
413 
414 	EVTX_TEST_ASSERT_EQUAL_INT(
415 	 "result",
416 	 result,
417 	 1 );
418 
419 	EVTX_TEST_ASSERT_IS_NOT_NULL(
420 	 "chunk",
421 	 chunk );
422 
423 	EVTX_TEST_ASSERT_IS_NULL(
424 	 "error",
425 	 error );
426 
427 	/* Test regular cases
428 	 */
429 	result = libevtx_chunk_get_number_of_recovered_records(
430 	          chunk,
431 	          &number_of_recovered_records,
432 	          &error );
433 
434 	EVTX_TEST_ASSERT_NOT_EQUAL_INT(
435 	 "result",
436 	 result,
437 	 -1 );
438 
439 	EVTX_TEST_ASSERT_IS_NULL(
440 	 "error",
441 	 error );
442 
443 	number_of_recovered_records_is_set = result;
444 
445 	/* Test error cases
446 	 */
447 	result = libevtx_chunk_get_number_of_recovered_records(
448 	          NULL,
449 	          &number_of_recovered_records,
450 	          &error );
451 
452 	EVTX_TEST_ASSERT_EQUAL_INT(
453 	 "result",
454 	 result,
455 	 -1 );
456 
457 	EVTX_TEST_ASSERT_IS_NOT_NULL(
458 	 "error",
459 	 error );
460 
461 	libcerror_error_free(
462 	 &error );
463 
464 	if( number_of_recovered_records_is_set != 0 )
465 	{
466 		result = libevtx_chunk_get_number_of_recovered_records(
467 		          chunk,
468 		          NULL,
469 		          &error );
470 
471 		EVTX_TEST_ASSERT_EQUAL_INT(
472 		 "result",
473 		 result,
474 		 -1 );
475 
476 		EVTX_TEST_ASSERT_IS_NOT_NULL(
477 		 "error",
478 		 error );
479 
480 		libcerror_error_free(
481 		 &error );
482 	}
483 	/* Clean up
484 	 */
485 	result = libevtx_chunk_free(
486 	          &chunk,
487 	          &error );
488 
489 	EVTX_TEST_ASSERT_EQUAL_INT(
490 	 "result",
491 	 result,
492 	 1 );
493 
494 	EVTX_TEST_ASSERT_IS_NULL(
495 	 "chunk",
496 	 chunk );
497 
498 	EVTX_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( chunk != NULL )
511 	{
512 		libevtx_chunk_free(
513 		 &chunk,
514 		 NULL );
515 	}
516 	return( 0 );
517 }
518 
519 #endif /* defined( __GNUC__ ) && !defined( LIBEVTX_DLL_IMPORT ) */
520 
521 /* The main program
522  */
523 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc EVTX_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]EVTX_TEST_ATTRIBUTE_UNUSED)524 int wmain(
525      int argc EVTX_TEST_ATTRIBUTE_UNUSED,
526      wchar_t * const argv[] EVTX_TEST_ATTRIBUTE_UNUSED )
527 #else
528 int main(
529      int argc EVTX_TEST_ATTRIBUTE_UNUSED,
530      char * const argv[] EVTX_TEST_ATTRIBUTE_UNUSED )
531 #endif
532 {
533 	EVTX_TEST_UNREFERENCED_PARAMETER( argc )
534 	EVTX_TEST_UNREFERENCED_PARAMETER( argv )
535 
536 #if defined( __GNUC__ ) && !defined( LIBEVTX_DLL_IMPORT )
537 
538 	EVTX_TEST_RUN(
539 	 "libevtx_chunk_initialize",
540 	 evtx_test_chunk_initialize );
541 
542 	EVTX_TEST_RUN(
543 	 "libevtx_chunk_free",
544 	 evtx_test_chunk_free );
545 
546 	/* TODO: add tests for libevtx_chunk_read */
547 
548 	EVTX_TEST_RUN(
549 	 "libevtx_chunk_get_number_of_records",
550 	 evtx_test_chunk_get_number_of_records );
551 
552 	/* TODO: add tests for libevtx_chunk_get_record */
553 
554 	EVTX_TEST_RUN(
555 	 "libevtx_chunk_get_number_of_recovered_records",
556 	 evtx_test_chunk_get_number_of_recovered_records );
557 
558 	/* TODO: add tests for libevtx_chunk_get_recovered_record */
559 
560 #endif /* defined( __GNUC__ ) && !defined( LIBEVTX_DLL_IMPORT ) */
561 
562 	return( EXIT_SUCCESS );
563 
564 on_error:
565 	return( EXIT_FAILURE );
566 }
567 
568