Debian Codegraph @

tests/vhdi_test_region_table_header.c

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