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