Debian Codegraph @

tests/vhdi_test_file_footer.c

raw symbols SCIP
1 /*
2 * Library file_footer type test program
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
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 <byte_stream.h>
24 #include <file_stream.h>
25 #include <types.h>
26
27 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
28 #include <stdlib.h>
29 #endif
30
31 #include "vhdi_test_functions.h"
32 #include "vhdi_test_libbfio.h"
33 #include "vhdi_test_libcerror.h"
34 #include "vhdi_test_libvhdi.h"
35 #include "vhdi_test_macros.h"
36 #include "vhdi_test_memory.h"
37 #include "vhdi_test_unused.h"
38
39 #include "../libvhdi/libvhdi_file_footer.h"
40
41 uint8_t vhdi_test_file_footer_data1[ 512 ] = {
42 0x63, 0x6f, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x78, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x19, 0x22, 0x15, 0xf0, 0x76, 0x70, 0x63, 0x20,
44 0x00, 0x01, 0x00, 0x00, 0x57, 0x69, 0x32, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10, 0x3f, 0x00, 0x00, 0x00, 0x04,
46 0xff, 0xff, 0xf0, 0x23, 0x40, 0xd5, 0x6d, 0x36, 0x9a, 0xb8, 0x4f, 0xe4, 0xa3, 0x1a, 0x12, 0x1f,
47 0xe2, 0xbf, 0x15, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
74
75 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
76
77 /* Tests the libvhdi_file_footer_initialize function
78 * Returns 1 if successful or 0 if not
79 */
80 int vhdi_test_file_footer_initialize(
81 void )
82 {
83 libcerror_error_t *error = NULL;
84 libvhdi_file_footer_t *file_footer = NULL;
85 int result = 0;
86
87 #if defined( HAVE_VHDI_TEST_MEMORY )
88 int number_of_malloc_fail_tests = 1;
89 int number_of_memset_fail_tests = 1;
90 int test_number = 0;
91 #endif
92
93 /* Test regular cases
94 */
95 result = libvhdi_file_footer_initialize(
96 &file_footer,
97 &error );
98
99 VHDI_TEST_ASSERT_EQUAL_INT(
100 "result",
101 result,
102 1 );
103
104 VHDI_TEST_ASSERT_IS_NOT_NULL(
105 "file_footer",
106 file_footer );
107
108 VHDI_TEST_ASSERT_IS_NULL(
109 "error",
110 error );
111
112 result = libvhdi_file_footer_free(
113 &file_footer,
114 &error );
115
116 VHDI_TEST_ASSERT_EQUAL_INT(
117 "result",
118 result,
119 1 );
120
121 VHDI_TEST_ASSERT_IS_NULL(
122 "file_footer",
123 file_footer );
124
125 VHDI_TEST_ASSERT_IS_NULL(
126 "error",
127 error );
128
129 /* Test error cases
130 */
131 result = libvhdi_file_footer_initialize(
132 NULL,
133 &error );
134
135 VHDI_TEST_ASSERT_EQUAL_INT(
136 "result",
137 result,
138 -1 );
139
140 VHDI_TEST_ASSERT_IS_NOT_NULL(
141 "error",
142 error );
143
144 libcerror_error_free(
145 &error );
146
147 file_footer = (libvhdi_file_footer_t *) 0x12345678UL;
148
149 result = libvhdi_file_footer_initialize(
150 &file_footer,
151 &error );
152
153 file_footer = NULL;
154
155 VHDI_TEST_ASSERT_EQUAL_INT(
156 "result",
157 result,
158 -1 );
159
160 VHDI_TEST_ASSERT_IS_NOT_NULL(
161 "error",
162 error );
163
164 libcerror_error_free(
165 &error );
166
167 #if defined( HAVE_VHDI_TEST_MEMORY )
168
169 for( test_number = 0;
170 test_number < number_of_malloc_fail_tests;
171 test_number++ )
172 {
173 /* Test libvhdi_file_footer_initialize with malloc failing
174 */
175 vhdi_test_malloc_attempts_before_fail = test_number;
176
177 result = libvhdi_file_footer_initialize(
178 &file_footer,
179 &error );
180
181 if( vhdi_test_malloc_attempts_before_fail != -1 )
182 {
183 vhdi_test_malloc_attempts_before_fail = -1;
184
185 if( file_footer != NULL )
186 {
187 libvhdi_file_footer_free(
188 &file_footer,
189 NULL );
190 }
191 }
192 else
193 {
194 VHDI_TEST_ASSERT_EQUAL_INT(
195 "result",
196 result,
197 -1 );
198
199 VHDI_TEST_ASSERT_IS_NULL(
200 "file_footer",
201 file_footer );
202
203 VHDI_TEST_ASSERT_IS_NOT_NULL(
204 "error",
205 error );
206
207 libcerror_error_free(
208 &error );
209 }
210 }
211 for( test_number = 0;
212 test_number < number_of_memset_fail_tests;
213 test_number++ )
214 {
215 /* Test libvhdi_file_footer_initialize with memset failing
216 */
217 vhdi_test_memset_attempts_before_fail = test_number;
218
219 result = libvhdi_file_footer_initialize(
220 &file_footer,
221 &error );
222
223 if( vhdi_test_memset_attempts_before_fail != -1 )
224 {
225 vhdi_test_memset_attempts_before_fail = -1;
226
227 if( file_footer != NULL )
228 {
229 libvhdi_file_footer_free(
230 &file_footer,
231 NULL );
232 }
233 }
234 else
235 {
236 VHDI_TEST_ASSERT_EQUAL_INT(
237 "result",
238 result,
239 -1 );
240
241 VHDI_TEST_ASSERT_IS_NULL(
242 "file_footer",
243 file_footer );
244
245 VHDI_TEST_ASSERT_IS_NOT_NULL(
246 "error",
247 error );
248
249 libcerror_error_free(
250 &error );
251 }
252 }
253 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
254
255 return( 1 );
256
257 on_error:
258 if( error != NULL )
259 {
260 libcerror_error_free(
261 &error );
262 }
263 if( file_footer != NULL )
264 {
265 libvhdi_file_footer_free(
266 &file_footer,
267 NULL );
268 }
269 return( 0 );
270 }
271
272 /* Tests the libvhdi_file_footer_free function
273 * Returns 1 if successful or 0 if not
274 */
275 int vhdi_test_file_footer_free(
276 void )
277 {
278 libcerror_error_t *error = NULL;
279 int result = 0;
280
281 /* Test error cases
282 */
283 result = libvhdi_file_footer_free(
284 NULL,
285 &error );
286
287 VHDI_TEST_ASSERT_EQUAL_INT(
288 "result",
289 result,
290 -1 );
291
292 VHDI_TEST_ASSERT_IS_NOT_NULL(
293 "error",
294 error );
295
296 libcerror_error_free(
297 &error );
298
299 return( 1 );
300
301 on_error:
302 if( error != NULL )
303 {
304 libcerror_error_free(
305 &error );
306 }
307 return( 0 );
308 }
309
310 /* Tests the libvhdi_file_footer_read_data function
311 * Returns 1 if successful or 0 if not
312 */
313 int vhdi_test_file_footer_read_data(
314 void )
315 {
316 libcerror_error_t *error = NULL;
317 libvhdi_file_footer_t *file_footer = NULL;
318 int result = 0;
319
320 /* Initialize test
321 */
322 result = libvhdi_file_footer_initialize(
323 &file_footer,
324 &error );
325
326 VHDI_TEST_ASSERT_EQUAL_INT(
327 "result",
328 result,
329 1 );
330
331 VHDI_TEST_ASSERT_IS_NOT_NULL(
332 "file_footer",
333 file_footer );
334
335 VHDI_TEST_ASSERT_IS_NULL(
336 "error",
337 error );
338
339 /* Test regular cases
340 */
341 result = libvhdi_file_footer_read_data(
342 file_footer,
343 vhdi_test_file_footer_data1,
344 512,
345 &error );
346
347 VHDI_TEST_ASSERT_EQUAL_INT(
348 "result",
349 result,
350 1 );
351
352 VHDI_TEST_ASSERT_IS_NULL(
353 "error",
354 error );
355
356 /* Test error cases
357 */
358 result = libvhdi_file_footer_read_data(
359 NULL,
360 vhdi_test_file_footer_data1,
361 512,
362 &error );
363
364 VHDI_TEST_ASSERT_EQUAL_INT(
365 "result",
366 result,
367 -1 );
368
369 VHDI_TEST_ASSERT_IS_NOT_NULL(
370 "error",
371 error );
372
373 libcerror_error_free(
374 &error );
375
376 result = libvhdi_file_footer_read_data(
377 file_footer,
378 NULL,
379 512,
380 &error );
381
382 VHDI_TEST_ASSERT_EQUAL_INT(
383 "result",
384 result,
385 -1 );
386
387 VHDI_TEST_ASSERT_IS_NOT_NULL(
388 "error",
389 error );
390
391 libcerror_error_free(
392 &error );
393
394 result = libvhdi_file_footer_read_data(
395 file_footer,
396 vhdi_test_file_footer_data1,
397 (size_t) SSIZE_MAX + 1,
398 &error );
399
400 VHDI_TEST_ASSERT_EQUAL_INT(
401 "result",
402 result,
403 -1 );
404
405 VHDI_TEST_ASSERT_IS_NOT_NULL(
406 "error",
407 error );
408
409 libcerror_error_free(
410 &error );
411
412 result = libvhdi_file_footer_read_data(
413 file_footer,
414 vhdi_test_file_footer_data1,
415 0,
416 &error );
417
418 VHDI_TEST_ASSERT_EQUAL_INT(
419 "result",
420 result,
421 -1 );
422
423 VHDI_TEST_ASSERT_IS_NOT_NULL(
424 "error",
425 error );
426
427 libcerror_error_free(
428 &error );
429
430 #if defined( HAVE_VHDI_TEST_MEMORY )
431
432 /* Test libvhdi_file_footer_read_data with memcpy failing
433 */
434 vhdi_test_memcpy_attempts_before_fail = 0;
435
436 result = libvhdi_file_footer_read_data(
437 file_footer,
438 vhdi_test_file_footer_data1,
439 512,
440 &error );
441
442 if( vhdi_test_memcpy_attempts_before_fail != -1 )
443 {
444 vhdi_test_memcpy_attempts_before_fail = -1;
445 }
446 else
447 {
448 VHDI_TEST_ASSERT_EQUAL_INT(
449 "result",
450 result,
451 -1 );
452
453 VHDI_TEST_ASSERT_IS_NOT_NULL(
454 "error",
455 error );
456
457 libcerror_error_free(
458 &error );
459 }
460 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
461
462 /* Test error case where signature is invalid
463 */
464 byte_stream_copy_from_uint64_big_endian(
465 vhdi_test_file_footer_data1,
466 0xffffffffffffffffUL );
467
468 result = libvhdi_file_footer_read_data(
469 file_footer,
470 vhdi_test_file_footer_data1,
471 512,
472 &error );
473
474 byte_stream_copy_from_uint64_big_endian(
475 vhdi_test_file_footer_data1,
476 0x636f6e6563746978UL );
477
478 VHDI_TEST_ASSERT_EQUAL_INT(
479 "result",
480 result,
481 -1 );
482
483 VHDI_TEST_ASSERT_IS_NOT_NULL(
484 "error",
485 error );
486
487 libcerror_error_free(
488 &error );
489
490 /* Test error case where format version is invalid
491 */
492 byte_stream_copy_from_uint32_big_endian(
493 &( vhdi_test_file_footer_data1[ 12 ] ),
494 0xffffffffUL );
495
496 result = libvhdi_file_footer_read_data(
497 file_footer,
498 vhdi_test_file_footer_data1,
499 512,
500 &error );
501
502 byte_stream_copy_from_uint32_big_endian(
503 &( vhdi_test_file_footer_data1[ 12 ] ),
504 0x00010000UL );
505
506 VHDI_TEST_ASSERT_EQUAL_INT(
507 "result",
508 result,
509 -1 );
510
511 VHDI_TEST_ASSERT_IS_NOT_NULL(
512 "error",
513 error );
514
515 libcerror_error_free(
516 &error );
517
518 /* Test error case where disk type is invalid
519 */
520 /* TODO implement */
521
522 /* Clean up
523 */
524 result = libvhdi_file_footer_free(
525 &file_footer,
526 &error );
527
528 VHDI_TEST_ASSERT_EQUAL_INT(
529 "result",
530 result,
531 1 );
532
533 VHDI_TEST_ASSERT_IS_NULL(
534 "file_footer",
535 file_footer );
536
537 VHDI_TEST_ASSERT_IS_NULL(
538 "error",
539 error );
540
541 return( 1 );
542
543 on_error:
544 if( error != NULL )
545 {
546 libcerror_error_free(
547 &error );
548 }
549 if( file_footer != NULL )
550 {
551 libvhdi_file_footer_free(
552 &file_footer,
553 NULL );
554 }
555 return( 0 );
556 }
557
558 /* Tests the libvhdi_file_footer_read_file_io_handle function
559 * Returns 1 if successful or 0 if not
560 */
561 int vhdi_test_file_footer_read_file_io_handle(
562 void )
563 {
564 libbfio_handle_t *file_io_handle = NULL;
565 libcerror_error_t *error = NULL;
566 libvhdi_file_footer_t *file_footer = NULL;
567 int result = 0;
568
569 /* Initialize test
570 */
571 result = libvhdi_file_footer_initialize(
572 &file_footer,
573 &error );
574
575 VHDI_TEST_ASSERT_EQUAL_INT(
576 "result",
577 result,
578 1 );
579
580 VHDI_TEST_ASSERT_IS_NOT_NULL(
581 "file_footer",
582 file_footer );
583
584 VHDI_TEST_ASSERT_IS_NULL(
585 "error",
586 error );
587
588 /* Initialize file IO handle
589 */
590 result = vhdi_test_open_file_io_handle(
591 &file_io_handle,
592 vhdi_test_file_footer_data1,
593 512,
594 &error );
595
596 VHDI_TEST_ASSERT_EQUAL_INT(
597 "result",
598 result,
599 1 );
600
601 VHDI_TEST_ASSERT_IS_NOT_NULL(
602 "file_io_handle",
603 file_io_handle );
604
605 VHDI_TEST_ASSERT_IS_NULL(
606 "error",
607 error );
608
609 /* Test regular cases
610 */
611 result = libvhdi_file_footer_read_file_io_handle(
612 file_footer,
613 file_io_handle,
614 0,
615 &error );
616
617 VHDI_TEST_ASSERT_EQUAL_INT(
618 "result",
619 result,
620 1 );
621
622 VHDI_TEST_ASSERT_IS_NULL(
623 "error",
624 error );
625
626 /* Test error cases
627 */
628 result = libvhdi_file_footer_read_file_io_handle(
629 NULL,
630 file_io_handle,
631 0,
632 &error );
633
634 VHDI_TEST_ASSERT_EQUAL_INT(
635 "result",
636 result,
637 -1 );
638
639 VHDI_TEST_ASSERT_IS_NOT_NULL(
640 "error",
641 error );
642
643 libcerror_error_free(
644 &error );
645
646 result = libvhdi_file_footer_read_file_io_handle(
647 file_footer,
648 NULL,
649 0,
650 &error );
651
652 VHDI_TEST_ASSERT_EQUAL_INT(
653 "result",
654 result,
655 -1 );
656
657 VHDI_TEST_ASSERT_IS_NOT_NULL(
658 "error",
659 error );
660
661 libcerror_error_free(
662 &error );
663
664 result = libvhdi_file_footer_read_file_io_handle(
665 file_footer,
666 file_io_handle,
667 -1,
668 &error );
669
670 VHDI_TEST_ASSERT_EQUAL_INT(
671 "result",
672 result,
673 -1 );
674
675 VHDI_TEST_ASSERT_IS_NOT_NULL(
676 "error",
677 error );
678
679 libcerror_error_free(
680 &error );
681
682 /* Test data invalid
683 */
684 byte_stream_copy_from_uint64_big_endian(
685 vhdi_test_file_footer_data1,
686 0xffffffffffffffffUL );
687
688 result = libvhdi_file_footer_read_file_io_handle(
689 file_footer,
690 file_io_handle,
691 0,
692 &error );
693
694 byte_stream_copy_from_uint64_big_endian(
695 vhdi_test_file_footer_data1,
696 0x636f6e6563746978UL );
697
698 VHDI_TEST_ASSERT_EQUAL_INT(
699 "result",
700 result,
701 -1 );
702
703 VHDI_TEST_ASSERT_IS_NOT_NULL(
704 "error",
705 error );
706
707 libcerror_error_free(
708 &error );
709
710 /* Clean up file IO handle
711 */
712 result = vhdi_test_close_file_io_handle(
713 &file_io_handle,
714 &error );
715
716 VHDI_TEST_ASSERT_EQUAL_INT(
717 "result",
718 result,
719 0 );
720
721 VHDI_TEST_ASSERT_IS_NULL(
722 "error",
723 error );
724
725 /* Test data too small
726 */
727 result = vhdi_test_open_file_io_handle(
728 &file_io_handle,
729 vhdi_test_file_footer_data1,
730 8,
731 &error );
732
733 VHDI_TEST_ASSERT_EQUAL_INT(
734 "result",
735 result,
736 1 );
737
738 VHDI_TEST_ASSERT_IS_NOT_NULL(
739 "file_io_handle",
740 file_io_handle );
741
742 VHDI_TEST_ASSERT_IS_NULL(
743 "error",
744 error );
745
746 result = libvhdi_file_footer_read_file_io_handle(
747 file_footer,
748 file_io_handle,
749 0,
750 &error );
751
752 VHDI_TEST_ASSERT_EQUAL_INT(
753 "result",
754 result,
755 -1 );
756
757 VHDI_TEST_ASSERT_IS_NOT_NULL(
758 "error",
759 error );
760
761 libcerror_error_free(
762 &error );
763
764 result = vhdi_test_close_file_io_handle(
765 &file_io_handle,
766 &error );
767
768 VHDI_TEST_ASSERT_EQUAL_INT(
769 "result",
770 result,
771 0 );
772
773 VHDI_TEST_ASSERT_IS_NULL(
774 "error",
775 error );
776
777 /* Clean up
778 */
779 result = libvhdi_file_footer_free(
780 &file_footer,
781 &error );
782
783 VHDI_TEST_ASSERT_EQUAL_INT(
784 "result",
785 result,
786 1 );
787
788 VHDI_TEST_ASSERT_IS_NULL(
789 "file_footer",
790 file_footer );
791
792 VHDI_TEST_ASSERT_IS_NULL(
793 "error",
794 error );
795
796 return( 1 );
797
798 on_error:
799 if( error != NULL )
800 {
801 libcerror_error_free(
802 &error );
803 }
804 if( file_io_handle != NULL )
805 {
806 libbfio_handle_free(
807 &file_io_handle,
808 NULL );
809 }
810 if( file_footer != NULL )
811 {
812 libvhdi_file_footer_free(
813 &file_footer,
814 NULL );
815 }
816 return( 0 );
817 }
818
819 /* Tests the libvhdi_file_footer_get_format_version function
820 * Returns 1 if successful or 0 if not
821 */
822 int vhdi_test_file_footer_get_format_version(
823 libvhdi_file_footer_t *file_footer )
824 {
825 libcerror_error_t *error = NULL;
826 uint16_t major_version = 0;
827 uint16_t minor_version = 0;
828 int result = 0;
829
830 /* Test regular cases
831 */
832 result = libvhdi_file_footer_get_format_version(
833 file_footer,
834 &major_version,
835 &minor_version,
836 &error );
837
838 VHDI_TEST_ASSERT_EQUAL_INT(
839 "result",
840 result,
841 1 );
842
843 VHDI_TEST_ASSERT_IS_NULL(
844 "error",
845 error );
846
847 /* Test error cases
848 */
849 result = libvhdi_file_footer_get_format_version(
850 NULL,
851 &major_version,
852 &minor_version,
853 &error );
854
855 VHDI_TEST_ASSERT_EQUAL_INT(
856 "result",
857 result,
858 -1 );
859
860 VHDI_TEST_ASSERT_IS_NOT_NULL(
861 "error",
862 error );
863
864 libcerror_error_free(
865 &error );
866
867 result = libvhdi_file_footer_get_format_version(
868 file_footer,
869 NULL,
870 &minor_version,
871 &error );
872
873 VHDI_TEST_ASSERT_EQUAL_INT(
874 "result",
875 result,
876 -1 );
877
878 VHDI_TEST_ASSERT_IS_NOT_NULL(
879 "error",
880 error );
881
882 libcerror_error_free(
883 &error );
884
885 result = libvhdi_file_footer_get_format_version(
886 file_footer,
887 &major_version,
888 NULL,
889 &error );
890
891 VHDI_TEST_ASSERT_EQUAL_INT(
892 "result",
893 result,
894 -1 );
895
896 VHDI_TEST_ASSERT_IS_NOT_NULL(
897 "error",
898 error );
899
900 libcerror_error_free(
901 &error );
902
903 return( 1 );
904
905 on_error:
906 if( error != NULL )
907 {
908 libcerror_error_free(
909 &error );
910 }
911 return( 0 );
912 }
913
914 /* Tests the libvhdi_file_footer_get_identifier function
915 * Returns 1 if successful or 0 if not
916 */
917 int vhdi_test_file_footer_get_identifier(
918 libvhdi_file_footer_t *file_footer )
919 {
920 uint8_t guid_data[ 16 ];
921
922 libcerror_error_t *error = NULL;
923 int result = 0;
924
925 /* Test regular cases
926 */
927 result = libvhdi_file_footer_get_identifier(
928 file_footer,
929 guid_data,
930 16,
931 &error );
932
933 VHDI_TEST_ASSERT_EQUAL_INT(
934 "result",
935 result,
936 1 );
937
938 VHDI_TEST_ASSERT_IS_NULL(
939 "error",
940 error );
941
942 /* Test error cases
943 */
944 result = libvhdi_file_footer_get_identifier(
945 NULL,
946 guid_data,
947 16,
948 &error );
949
950 VHDI_TEST_ASSERT_EQUAL_INT(
951 "result",
952 result,
953 -1 );
954
955 VHDI_TEST_ASSERT_IS_NOT_NULL(
956 "error",
957 error );
958
959 libcerror_error_free(
960 &error );
961
962 result = libvhdi_file_footer_get_identifier(
963 file_footer,
964 NULL,
965 16,
966 &error );
967
968 VHDI_TEST_ASSERT_EQUAL_INT(
969 "result",
970 result,
971 -1 );
972
973 VHDI_TEST_ASSERT_IS_NOT_NULL(
974 "error",
975 error );
976
977 libcerror_error_free(
978 &error );
979
980 result = libvhdi_file_footer_get_identifier(
981 file_footer,
982 guid_data,
983 0,
984 &error );
985
986 VHDI_TEST_ASSERT_EQUAL_INT(
987 "result",
988 result,
989 -1 );
990
991 VHDI_TEST_ASSERT_IS_NOT_NULL(
992 "error",
993 error );
994
995 libcerror_error_free(
996 &error );
997
998 result = libvhdi_file_footer_get_identifier(
999 file_footer,
1000 guid_data,
1001 (size_t) SSIZE_MAX + 1,
1002 &error );
1003
1004 VHDI_TEST_ASSERT_EQUAL_INT(
1005 "result",
1006 result,
1007 -1 );
1008
1009 VHDI_TEST_ASSERT_IS_NOT_NULL(
1010 "error",
1011 error );
1012
1013 libcerror_error_free(
1014 &error );
1015
1016 #if defined( HAVE_VHDI_TEST_MEMORY )
1017
1018 /* Test libvhdi_file_footer_get_identifier with memcpy failing
1019 */
1020 vhdi_test_memcpy_attempts_before_fail = 0;
1021
1022 result = libvhdi_file_footer_get_identifier(
1023 file_footer,
1024 guid_data,
1025 16,
1026 &error );
1027
1028 if( vhdi_test_memcpy_attempts_before_fail != -1 )
1029 {
1030 vhdi_test_memcpy_attempts_before_fail = -1;
1031 }
1032 else
1033 {
1034 VHDI_TEST_ASSERT_EQUAL_INT(
1035 "result",
1036 result,
1037 -1 );
1038
1039 VHDI_TEST_ASSERT_IS_NOT_NULL(
1040 "error",
1041 error );
1042
1043 libcerror_error_free(
1044 &error );
1045 }
1046 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
1047
1048 return( 1 );
1049
1050 on_error:
1051 if( error != NULL )
1052 {
1053 libcerror_error_free(
1054 &error );
1055 }
1056 return( 0 );
1057 }
1058
1059 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
1060
1061 /* The main program
1062 */
1063 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
1064 int wmain(
1065 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
1066 wchar_t * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
1067 #else
1068 int main(
1069 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
1070 char * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
1071 #endif
1072 {
1073 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
1074 #if !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 )
1075
1076 libcerror_error_t *error = NULL;
1077 libvhdi_file_footer_t *file_footer = NULL;
1078 int result = 0;
1079
1080 #endif /* !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 ) */
1081 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
1082
1083 VHDI_TEST_UNREFERENCED_PARAMETER( argc )
1084 VHDI_TEST_UNREFERENCED_PARAMETER( argv )
1085
1086 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
1087
1088 VHDI_TEST_RUN(
1089 "libvhdi_file_footer_initialize",
1090 vhdi_test_file_footer_initialize );
1091
1092 VHDI_TEST_RUN(
1093 "libvhdi_file_footer_free",
1094 vhdi_test_file_footer_free );
1095
1096 VHDI_TEST_RUN(
1097 "libvhdi_file_footer_read_data",
1098 vhdi_test_file_footer_read_data );
1099
1100 VHDI_TEST_RUN(
1101 "libvhdi_file_footer_read_file_io_handle",
1102 vhdi_test_file_footer_read_file_io_handle );
1103
1104 #if !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 )
1105
1106 /* Initialize file_footer for tests
1107 */
1108 result = libvhdi_file_footer_initialize(
1109 &file_footer,
1110 &error );
1111
1112 VHDI_TEST_ASSERT_EQUAL_INT(
1113 "result",
1114 result,
1115 1 );
1116
1117 VHDI_TEST_ASSERT_IS_NOT_NULL(
1118 "file_footer",
1119 file_footer );
1120
1121 VHDI_TEST_ASSERT_IS_NULL(
1122 "error",
1123 error );
1124
1125 result = libvhdi_file_footer_read_data(
1126 file_footer,
1127 vhdi_test_file_footer_data1,
1128 512,
1129 &error );
1130
1131 VHDI_TEST_ASSERT_EQUAL_INT(
1132 "result",
1133 result,
1134 1 );
1135
1136 VHDI_TEST_ASSERT_IS_NULL(
1137 "error",
1138 error );
1139
1140 /* Run tests
1141 */
1142 VHDI_TEST_RUN_WITH_ARGS(
1143 "libvhdi_file_footer_get_format_version",
1144 vhdi_test_file_footer_get_format_version,
1145 file_footer );
1146
1147 VHDI_TEST_RUN_WITH_ARGS(
1148 "libvhdi_file_footer_get_identifier",
1149 vhdi_test_file_footer_get_identifier,
1150 file_footer );
1151
1152 /* Clean up
1153 */
1154 result = libvhdi_file_footer_free(
1155 &file_footer,
1156 &error );
1157
1158 VHDI_TEST_ASSERT_EQUAL_INT(
1159 "result",
1160 result,
1161 1 );
1162
1163 VHDI_TEST_ASSERT_IS_NULL(
1164 "file_footer",
1165 file_footer );
1166
1167 VHDI_TEST_ASSERT_IS_NULL(
1168 "error",
1169 error );
1170
1171 #endif /* !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 ) */
1172 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
1173
1174 return( EXIT_SUCCESS );
1175
1176 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
1177
1178 on_error:
1179 #if !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 )
1180 if( error != NULL )
1181 {
1182 libcerror_error_free(
1183 &error );
1184 }
1185 if( file_footer != NULL )
1186 {
1187 libvhdi_file_footer_free(
1188 &file_footer,
1189 NULL );
1190 }
1191 #endif /* !defined( __BORLANDC__ ) || ( __BORLANDC__ >= 0x0560 ) */
1192
1193 return( EXIT_FAILURE );
1194
1195 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
1196 }
1197