Debian Codegraph @

tests/vhdi_test_block_allocation_table.c

raw symbols SCIP
1 /*
2 * Library block_allocation_table 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 <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 "vhdi_test_libcerror.h"
31 #include "vhdi_test_libvhdi.h"
32 #include "vhdi_test_macros.h"
33 #include "vhdi_test_memory.h"
34 #include "vhdi_test_unused.h"
35
36 #include "../libvhdi/libvhdi_block_allocation_table.h"
37
38 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
39
40 /* Tests the libvhdi_block_allocation_table_initialize function
41 * Returns 1 if successful or 0 if not
42 */
43 int vhdi_test_block_allocation_table_initialize(
44 void )
45 {
46 libcerror_error_t *error = NULL;
47 libvhdi_block_allocation_table_t *block_allocation_table = NULL;
48 int result = 0;
49
50 #if defined( HAVE_VHDI_TEST_MEMORY )
51 int number_of_malloc_fail_tests = 1;
52 int number_of_memset_fail_tests = 1;
53 int test_number = 0;
54 #endif
55
56 /* Test regular cases
57 */
58 result = libvhdi_block_allocation_table_initialize(
59 &block_allocation_table,
60 1,
61 &error );
62
63 VHDI_TEST_ASSERT_EQUAL_INT(
64 "result",
65 result,
66 1 );
67
68 VHDI_TEST_ASSERT_IS_NOT_NULL(
69 "block_allocation_table",
70 block_allocation_table );
71
72 VHDI_TEST_ASSERT_IS_NULL(
73 "error",
74 error );
75
76 result = libvhdi_block_allocation_table_free(
77 &block_allocation_table,
78 &error );
79
80 VHDI_TEST_ASSERT_EQUAL_INT(
81 "result",
82 result,
83 1 );
84
85 VHDI_TEST_ASSERT_IS_NULL(
86 "block_allocation_table",
87 block_allocation_table );
88
89 VHDI_TEST_ASSERT_IS_NULL(
90 "error",
91 error );
92
93 /* Test error cases
94 */
95 result = libvhdi_block_allocation_table_initialize(
96 NULL,
97 1,
98 &error );
99
100 VHDI_TEST_ASSERT_EQUAL_INT(
101 "result",
102 result,
103 -1 );
104
105 VHDI_TEST_ASSERT_IS_NOT_NULL(
106 "error",
107 error );
108
109 libcerror_error_free(
110 &error );
111
112 block_allocation_table = (libvhdi_block_allocation_table_t *) 0x12345678UL;
113
114 result = libvhdi_block_allocation_table_initialize(
115 &block_allocation_table,
116 1,
117 &error );
118
119 block_allocation_table = NULL;
120
121 VHDI_TEST_ASSERT_EQUAL_INT(
122 "result",
123 result,
124 -1 );
125
126 VHDI_TEST_ASSERT_IS_NOT_NULL(
127 "error",
128 error );
129
130 libcerror_error_free(
131 &error );
132
133 #if defined( HAVE_VHDI_TEST_MEMORY )
134
135 for( test_number = 0;
136 test_number < number_of_malloc_fail_tests;
137 test_number++ )
138 {
139 /* Test libvhdi_block_allocation_table_initialize with malloc failing
140 */
141 vhdi_test_malloc_attempts_before_fail = test_number;
142
143 result = libvhdi_block_allocation_table_initialize(
144 &block_allocation_table,
145 1,
146 &error );
147
148 if( vhdi_test_malloc_attempts_before_fail != -1 )
149 {
150 vhdi_test_malloc_attempts_before_fail = -1;
151
152 if( block_allocation_table != NULL )
153 {
154 libvhdi_block_allocation_table_free(
155 &block_allocation_table,
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 "block_allocation_table",
168 block_allocation_table );
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_block_allocation_table_initialize with memset failing
183 */
184 vhdi_test_memset_attempts_before_fail = test_number;
185
186 result = libvhdi_block_allocation_table_initialize(
187 &block_allocation_table,
188 1,
189 &error );
190
191 if( vhdi_test_memset_attempts_before_fail != -1 )
192 {
193 vhdi_test_memset_attempts_before_fail = -1;
194
195 if( block_allocation_table != NULL )
196 {
197 libvhdi_block_allocation_table_free(
198 &block_allocation_table,
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 "block_allocation_table",
211 block_allocation_table );
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( block_allocation_table != NULL )
232 {
233 libvhdi_block_allocation_table_free(
234 &block_allocation_table,
235 NULL );
236 }
237 return( 0 );
238 }
239
240 /* Tests the libvhdi_block_allocation_table_free function
241 * Returns 1 if successful or 0 if not
242 */
243 int vhdi_test_block_allocation_table_free(
244 void )
245 {
246 libcerror_error_t *error = NULL;
247 int result = 0;
248
249 /* Test error cases
250 */
251 result = libvhdi_block_allocation_table_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 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
279
280 /* The main program
281 */
282 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
283 int wmain(
284 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
285 wchar_t * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
286 #else
287 int main(
288 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
289 char * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
290 #endif
291 {
292 VHDI_TEST_UNREFERENCED_PARAMETER( argc )
293 VHDI_TEST_UNREFERENCED_PARAMETER( argv )
294
295 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
296
297 VHDI_TEST_RUN(
298 "libvhdi_block_allocation_table_initialize",
299 vhdi_test_block_allocation_table_initialize );
300
301 VHDI_TEST_RUN(
302 "libvhdi_block_allocation_table_free",
303 vhdi_test_block_allocation_table_free );
304
305 /* TODO: add tests for libvhdi_block_allocation_table_read_element_data */
306
307 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
308
309 return( EXIT_SUCCESS );
310
311 on_error:
312 return( EXIT_FAILURE );
313 }
314