Debian Codegraph @

tests/vhdi_test_parent_locator_entry.c

raw symbols SCIP
1 /*
2 * Library parent_locator_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_parent_locator_entry.h"
38
39 uint8_t vhdi_test_parent_locator_entry_data1[ 12 ] = {
40 0x50, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x4c, 0x00 };
41
42 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
43
44 /* Tests the libvhdi_parent_locator_entry_initialize function
45 * Returns 1 if successful or 0 if not
46 */
47 int vhdi_test_parent_locator_entry_initialize(
48 void )
49 {
50 libcerror_error_t *error = NULL;
51 libvhdi_parent_locator_entry_t *parent_locator_entry = 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_parent_locator_entry_initialize(
63 &parent_locator_entry,
64 &error );
65
66 VHDI_TEST_ASSERT_EQUAL_INT(
67 "result",
68 result,
69 1 );
70
71 VHDI_TEST_ASSERT_IS_NOT_NULL(
72 "parent_locator_entry",
73 parent_locator_entry );
74
75 VHDI_TEST_ASSERT_IS_NULL(
76 "error",
77 error );
78
79 result = libvhdi_parent_locator_entry_free(
80 &parent_locator_entry,
81 &error );
82
83 VHDI_TEST_ASSERT_EQUAL_INT(
84 "result",
85 result,
86 1 );
87
88 VHDI_TEST_ASSERT_IS_NULL(
89 "parent_locator_entry",
90 parent_locator_entry );
91
92 VHDI_TEST_ASSERT_IS_NULL(
93 "error",
94 error );
95
96 /* Test error cases
97 */
98 result = libvhdi_parent_locator_entry_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 parent_locator_entry = (libvhdi_parent_locator_entry_t *) 0x12345678UL;
115
116 result = libvhdi_parent_locator_entry_initialize(
117 &parent_locator_entry,
118 &error );
119
120 parent_locator_entry = 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_parent_locator_entry_initialize with malloc failing
141 */
142 vhdi_test_malloc_attempts_before_fail = test_number;
143
144 result = libvhdi_parent_locator_entry_initialize(
145 &parent_locator_entry,
146 &error );
147
148 if( vhdi_test_malloc_attempts_before_fail != -1 )
149 {
150 vhdi_test_malloc_attempts_before_fail = -1;
151
152 if( parent_locator_entry != NULL )
153 {
154 libvhdi_parent_locator_entry_free(
155 &parent_locator_entry,
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 "parent_locator_entry",
168 parent_locator_entry );
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_parent_locator_entry_initialize with memset failing
183 */
184 vhdi_test_memset_attempts_before_fail = test_number;
185
186 result = libvhdi_parent_locator_entry_initialize(
187 &parent_locator_entry,
188 &error );
189
190 if( vhdi_test_memset_attempts_before_fail != -1 )
191 {
192 vhdi_test_memset_attempts_before_fail = -1;
193
194 if( parent_locator_entry != NULL )
195 {
196 libvhdi_parent_locator_entry_free(
197 &parent_locator_entry,
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 "parent_locator_entry",
210 parent_locator_entry );
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( parent_locator_entry != NULL )
231 {
232 libvhdi_parent_locator_entry_free(
233 &parent_locator_entry,
234 NULL );
235 }
236 return( 0 );
237 }
238
239 /* Tests the libvhdi_parent_locator_entry_free function
240 * Returns 1 if successful or 0 if not
241 */
242 int vhdi_test_parent_locator_entry_free(
243 void )
244 {
245 libcerror_error_t *error = NULL;
246 int result = 0;
247
248 /* Test error cases
249 */
250 result = libvhdi_parent_locator_entry_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_parent_locator_entry_read_data function
278 * Returns 1 if successful or 0 if not
279 */
280 int vhdi_test_parent_locator_entry_read_data(
281 void )
282 {
283 libcerror_error_t *error = NULL;
284 libvhdi_parent_locator_entry_t *parent_locator_entry = NULL;
285 int result = 0;
286
287 /* Initialize test
288 */
289 result = libvhdi_parent_locator_entry_initialize(
290 &parent_locator_entry,
291 &error );
292
293 VHDI_TEST_ASSERT_EQUAL_INT(
294 "result",
295 result,
296 1 );
297
298 VHDI_TEST_ASSERT_IS_NOT_NULL(
299 "parent_locator_entry",
300 parent_locator_entry );
301
302 VHDI_TEST_ASSERT_IS_NULL(
303 "error",
304 error );
305
306 /* Test regular cases
307 */
308 result = libvhdi_parent_locator_entry_read_data(
309 parent_locator_entry,
310 vhdi_test_parent_locator_entry_data1,
311 12,
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_parent_locator_entry_read_data(
326 NULL,
327 vhdi_test_parent_locator_entry_data1,
328 12,
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_parent_locator_entry_read_data(
344 parent_locator_entry,
345 NULL,
346 12,
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_parent_locator_entry_read_data(
362 parent_locator_entry,
363 vhdi_test_parent_locator_entry_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_parent_locator_entry_read_data(
380 parent_locator_entry,
381 vhdi_test_parent_locator_entry_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 /* Clean up
398 */
399 result = libvhdi_parent_locator_entry_free(
400 &parent_locator_entry,
401 &error );
402
403 VHDI_TEST_ASSERT_EQUAL_INT(
404 "result",
405 result,
406 1 );
407
408 VHDI_TEST_ASSERT_IS_NULL(
409 "parent_locator_entry",
410 parent_locator_entry );
411
412 VHDI_TEST_ASSERT_IS_NULL(
413 "error",
414 error );
415
416 return( 1 );
417
418 on_error:
419 if( error != NULL )
420 {
421 libcerror_error_free(
422 &error );
423 }
424 if( parent_locator_entry != NULL )
425 {
426 libvhdi_parent_locator_entry_free(
427 &parent_locator_entry,
428 NULL );
429 }
430 return( 0 );
431 }
432
433 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
434
435 /* The main program
436 */
437 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
438 int wmain(
439 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
440 wchar_t * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
441 #else
442 int main(
443 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
444 char * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
445 #endif
446 {
447 VHDI_TEST_UNREFERENCED_PARAMETER( argc )
448 VHDI_TEST_UNREFERENCED_PARAMETER( argv )
449
450 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
451
452 VHDI_TEST_RUN(
453 "libvhdi_parent_locator_entry_initialize",
454 vhdi_test_parent_locator_entry_initialize );
455
456 VHDI_TEST_RUN(
457 "libvhdi_parent_locator_entry_free",
458 vhdi_test_parent_locator_entry_free );
459
460 VHDI_TEST_RUN(
461 "libvhdi_parent_locator_entry_read_data",
462 vhdi_test_parent_locator_entry_read_data );
463
464 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
465
466 return( EXIT_SUCCESS );
467
468 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
469
470 on_error:
471 return( EXIT_FAILURE );
472
473 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
474 }
475