Debian Codegraph @

tests/vhdi_test_region_table.c

raw symbols SCIP
1 /*
2 * Library region_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 <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.h"
38
39 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
40
41 /* Tests the libvhdi_region_table_initialize function
42 * Returns 1 if successful or 0 if not
43 */
44 int vhdi_test_region_table_initialize(
45 void )
46 {
47 libcerror_error_t *error = NULL;
48 libvhdi_region_table_t *region_table = NULL;
49 int result = 0;
50
51 #if defined( HAVE_VHDI_TEST_MEMORY )
52 int number_of_malloc_fail_tests = 1;
53 int number_of_memset_fail_tests = 1;
54 int test_number = 0;
55 #endif
56
57 /* Test regular cases
58 */
59 result = libvhdi_region_table_initialize(
60 &region_table,
61 &error );
62
63 VHDI_TEST_ASSERT_EQUAL_INT(
64 "result",
65 result,
66 1 );
67
68 VHDI_TEST_ASSERT_IS_NOT_NULL(
69 "region_table",
70 region_table );
71
72 VHDI_TEST_ASSERT_IS_NULL(
73 "error",
74 error );
75
76 result = libvhdi_region_table_free(
77 &region_table,
78 &error );
79
80 VHDI_TEST_ASSERT_EQUAL_INT(
81 "result",
82 result,
83 1 );
84
85 VHDI_TEST_ASSERT_IS_NULL(
86 "region_table",
87 region_table );
88
89 VHDI_TEST_ASSERT_IS_NULL(
90 "error",
91 error );
92
93 /* Test error cases
94 */
95 result = libvhdi_region_table_initialize(
96 NULL,
97 &error );
98
99 VHDI_TEST_ASSERT_EQUAL_INT(
100 "result",
101 result,
102 -1 );
103
104 VHDI_TEST_ASSERT_IS_NOT_NULL(
105 "error",
106 error );
107
108 libcerror_error_free(
109 &error );
110
111 region_table = (libvhdi_region_table_t *) 0x12345678UL;
112
113 result = libvhdi_region_table_initialize(
114 &region_table,
115 &error );
116
117 region_table = NULL;
118
119 VHDI_TEST_ASSERT_EQUAL_INT(
120 "result",
121 result,
122 -1 );
123
124 VHDI_TEST_ASSERT_IS_NOT_NULL(
125 "error",
126 error );
127
128 libcerror_error_free(
129 &error );
130
131 #if defined( HAVE_VHDI_TEST_MEMORY )
132
133 for( test_number = 0;
134 test_number < number_of_malloc_fail_tests;
135 test_number++ )
136 {
137 /* Test libvhdi_region_table_initialize with malloc failing
138 */
139 vhdi_test_malloc_attempts_before_fail = test_number;
140
141 result = libvhdi_region_table_initialize(
142 &region_table,
143 &error );
144
145 if( vhdi_test_malloc_attempts_before_fail != -1 )
146 {
147 vhdi_test_malloc_attempts_before_fail = -1;
148
149 if( region_table != NULL )
150 {
151 libvhdi_region_table_free(
152 &region_table,
153 NULL );
154 }
155 }
156 else
157 {
158 VHDI_TEST_ASSERT_EQUAL_INT(
159 "result",
160 result,
161 -1 );
162
163 VHDI_TEST_ASSERT_IS_NULL(
164 "region_table",
165 region_table );
166
167 VHDI_TEST_ASSERT_IS_NOT_NULL(
168 "error",
169 error );
170
171 libcerror_error_free(
172 &error );
173 }
174 }
175 for( test_number = 0;
176 test_number < number_of_memset_fail_tests;
177 test_number++ )
178 {
179 /* Test libvhdi_region_table_initialize with memset failing
180 */
181 vhdi_test_memset_attempts_before_fail = test_number;
182
183 result = libvhdi_region_table_initialize(
184 &region_table,
185 &error );
186
187 if( vhdi_test_memset_attempts_before_fail != -1 )
188 {
189 vhdi_test_memset_attempts_before_fail = -1;
190
191 if( region_table != NULL )
192 {
193 libvhdi_region_table_free(
194 &region_table,
195 NULL );
196 }
197 }
198 else
199 {
200 VHDI_TEST_ASSERT_EQUAL_INT(
201 "result",
202 result,
203 -1 );
204
205 VHDI_TEST_ASSERT_IS_NULL(
206 "region_table",
207 region_table );
208
209 VHDI_TEST_ASSERT_IS_NOT_NULL(
210 "error",
211 error );
212
213 libcerror_error_free(
214 &error );
215 }
216 }
217 #endif /* defined( HAVE_VHDI_TEST_MEMORY ) */
218
219 return( 1 );
220
221 on_error:
222 if( error != NULL )
223 {
224 libcerror_error_free(
225 &error );
226 }
227 if( region_table != NULL )
228 {
229 libvhdi_region_table_free(
230 &region_table,
231 NULL );
232 }
233 return( 0 );
234 }
235
236 /* Tests the libvhdi_region_table_free function
237 * Returns 1 if successful or 0 if not
238 */
239 int vhdi_test_region_table_free(
240 void )
241 {
242 libcerror_error_t *error = NULL;
243 int result = 0;
244
245 /* Test error cases
246 */
247 result = libvhdi_region_table_free(
248 NULL,
249 &error );
250
251 VHDI_TEST_ASSERT_EQUAL_INT(
252 "result",
253 result,
254 -1 );
255
256 VHDI_TEST_ASSERT_IS_NOT_NULL(
257 "error",
258 error );
259
260 libcerror_error_free(
261 &error );
262
263 return( 1 );
264
265 on_error:
266 if( error != NULL )
267 {
268 libcerror_error_free(
269 &error );
270 }
271 return( 0 );
272 }
273
274 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
275
276 /* The main program
277 */
278 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
279 int wmain(
280 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
281 wchar_t * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
282 #else
283 int main(
284 int argc VHDI_TEST_ATTRIBUTE_UNUSED,
285 char * const argv[] VHDI_TEST_ATTRIBUTE_UNUSED )
286 #endif
287 {
288 VHDI_TEST_UNREFERENCED_PARAMETER( argc )
289 VHDI_TEST_UNREFERENCED_PARAMETER( argv )
290
291 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
292
293 VHDI_TEST_RUN(
294 "libvhdi_region_table_initialize",
295 vhdi_test_region_table_initialize );
296
297 VHDI_TEST_RUN(
298 "libvhdi_region_table_free",
299 vhdi_test_region_table_free );
300
301 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
302
303 return( EXIT_SUCCESS );
304
305 #if defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT )
306
307 on_error:
308 return( EXIT_FAILURE );
309
310 #endif /* defined( __GNUC__ ) && !defined( LIBVHDI_DLL_IMPORT ) */
311 }
312