Debian Codegraph @

tests/vhdi_test_block_descriptor.c

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