Debian Codegraph @

tests/vhdi_test_metadata_table_header.c

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