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