Debian Codegraph @

tests/vhdi_test_metadata_table_entry.c

raw symbols SCIP
1 /*
2 * Library metadata_table_entry 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_entry.h"
38
39 uint8_t vhdi_test_metadata_table_entry_data1[ 32 ] = {
40 0x37, 0x67, 0xa1, 0xca, 0x36, 0xfa, 0x43, 0x4d, 0xb3, 0xb6, 0x33, 0xf0, 0xaa, 0x44, 0xe7, 0x6b,
41 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
42
43 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
44
45 /* Tests the libvhdi_metadata_table_entry_initialize function
46 * Returns 1 if successful or 0 if not
47 */
48 int vhdi_test_metadata_table_entry_initialize(
49 void )
50 {
51 libcerror_error_t *error = NULL;
52 libvhdi_metadata_table_entry_t *metadata_table_entry = 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_entry_initialize(
64 &metadata_table_entry,
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_entry",
74 metadata_table_entry );
75
76 VHDI_TEST_ASSERT_IS_NULL(
77 "error",
78 error );
79
80 result = libvhdi_metadata_table_entry_free(
81 &metadata_table_entry,
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_entry",
91 metadata_table_entry );
92
93 VHDI_TEST_ASSERT_IS_NULL(
94 "error",
95 error );
96
97 /* Test error cases
98 */
99 result = libvhdi_metadata_table_entry_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_entry = (libvhdi_metadata_table_entry_t *) 0x12345678UL;
116
117 result = libvhdi_metadata_table_entry_initialize(
118 &metadata_table_entry,
119 &error );
120
121 metadata_table_entry = 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_entry_initialize with malloc failing
142 */
143 vhdi_test_malloc_attempts_before_fail = test_number;
144
145 result = libvhdi_metadata_table_entry_initialize(
146 &metadata_table_entry,
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_entry != NULL )
154 {
155 libvhdi_metadata_table_entry_free(
156 &metadata_table_entry,
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_entry",
169 metadata_table_entry );
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_entry_initialize with memset failing
184 */
185 vhdi_test_memset_attempts_before_fail = test_number;
186
187 result = libvhdi_metadata_table_entry_initialize(
188 &metadata_table_entry,
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_entry != NULL )
196 {
197 libvhdi_metadata_table_entry_free(
198 &metadata_table_entry,
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_entry",
211 metadata_table_entry );
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_entry != NULL )
232 {
233 libvhdi_metadata_table_entry_free(
234 &metadata_table_entry,
235 NULL );
236 }
237 return( 0 );
238 }
239
240 /* Tests the libvhdi_metadata_table_entry_free function
241 * Returns 1 if successful or 0 if not
242 */
243 int vhdi_test_metadata_table_entry_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_entry_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_entry_read_data function
279 * Returns 1 if successful or 0 if not
280 */
281 int vhdi_test_metadata_table_entry_read_data(
282 void )
283 {
284 libcerror_error_t *error = NULL;
285 libvhdi_metadata_table_entry_t *metadata_table_entry = NULL;
286 int result = 0;
287
288 /* Initialize test
289 */
290 result = libvhdi_metadata_table_entry_initialize(
291 &metadata_table_entry,
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_entry",
301 metadata_table_entry );
302
303 VHDI_TEST_ASSERT_IS_NULL(
304 "error",
305 error );
306
307 /* Test regular cases
308 */
309 result = libvhdi_metadata_table_entry_read_data(
310 metadata_table_entry,
311 vhdi_test_metadata_table_entry_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_entry_read_data(
327 NULL,
328 vhdi_test_metadata_table_entry_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_entry_read_data(
345 metadata_table_entry,
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_entry_read_data(
363 metadata_table_entry,
364 vhdi_test_metadata_table_entry_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_entry_read_data(
381 metadata_table_entry,
382 vhdi_test_metadata_table_entry_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 #if defined( HAVE_VHDI_TEST_MEMORY )
399
400 /* Test libvhdi_metadata_table_entry_read_data with memcpy failing
401 */
402 vhdi_test_memcpy_attempts_before_fail = 0;
403
404 result = libvhdi_metadata_table_entry_read_data(
405 metadata_table_entry,
406 vhdi_test_metadata_table_entry_data1,
407 32,
408 &error );
409
410 if( vhdi_test_memcpy_attempts_before_fail != -1 )
411 {
412 vhdi_test_memcpy_attempts_before_fail = -1;
413 }
414 else
415 {
416 VHDI_TEST_ASSERT_EQUAL_INT(
417 "result",
418 result,
419 -1 );
420
421 VHDI_TEST_ASSERT_IS_NOT_NULL(
422 "error",
423 error );
424
425 libcerror_error_free(
426 &error );
427 }
428 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
429
430 /* Clean up
431 */
432 result = libvhdi_metadata_table_entry_free(
433 &metadata_table_entry,
434 &error );
435
436 VHDI_TEST_ASSERT_EQUAL_INT(
437 "result",
438 result,
439 1 );
440
441 VHDI_TEST_ASSERT_IS_NULL(
442 "metadata_table_entry",
443 metadata_table_entry );
444
445 VHDI_TEST_ASSERT_IS_NULL(
446 "error",
447 error );
448
449 return( 1 );
450
451 on_error:
452 if( error != NULL )
453 {
454 libcerror_error_free(
455 &error );
456 }
457 if( metadata_table_entry != NULL )
458 {
459 libvhdi_metadata_table_entry_free(
460 &metadata_table_entry,
461 NULL );
462 }
463 return( 0 );
464 }
465
466 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
467
468 /* The main program
469 */
470 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
471 int wmain(
472 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
473 wchar_t * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
474 #else
475 int main(
476 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
477 char * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
478 #endif
479 {
480 VHDI_TEST_UNREFERENCED_PARAMETER( argc )
481 VHDI_TEST_UNREFERENCED_PARAMETER( argv )
482
483 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
484
485 VHDI_TEST_RUN(
486 "libvhdi_metadata_table_entry_initialize",
487 vhdi_test_metadata_table_entry_initialize );
488
489 VHDI_TEST_RUN(
490 "libvhdi_metadata_table_entry_free",
491 vhdi_test_metadata_table_entry_free );
492
493 VHDI_TEST_RUN(
494 "libvhdi_metadata_table_entry_read_data",
495 vhdi_test_metadata_table_entry_read_data );
496
497 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
498
499 return( EXIT_SUCCESS );
500
501 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
502
503 on_error:
504 return( EXIT_FAILURE );
505
506 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
507 }
508