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