1 /*
2 * Library profiler 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_profiler.h"
37
38 #if defined( HAVE_PROFILER )
39
40 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
41
42 /* Tests the libfsapfs_profiler_initialize function
43 * Returns 1 if successful or 0 if not
44 */
fsapfs_test_profiler_initialize(void)45 int fsapfs_test_profiler_initialize(
46 void )
47 {
48 libcerror_error_t *error = NULL;
49 libfsapfs_profiler_t *profiler = NULL;
50 int result = 0;
51
52 #if defined( HAVE_FSAPFS_TEST_MEMORY )
53 int number_of_malloc_fail_tests = 1;
54 int number_of_memset_fail_tests = 1;
55 int test_number = 0;
56 #endif
57
58 /* Test regular cases
59 */
60 result = libfsapfs_profiler_initialize(
61 &profiler,
62 &error );
63
64 FSAPFS_TEST_ASSERT_EQUAL_INT(
65 "result",
66 result,
67 1 );
68
69 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
70 "profiler",
71 profiler );
72
73 FSAPFS_TEST_ASSERT_IS_NULL(
74 "error",
75 error );
76
77 result = libfsapfs_profiler_free(
78 &profiler,
79 &error );
80
81 FSAPFS_TEST_ASSERT_EQUAL_INT(
82 "result",
83 result,
84 1 );
85
86 FSAPFS_TEST_ASSERT_IS_NULL(
87 "profiler",
88 profiler );
89
90 FSAPFS_TEST_ASSERT_IS_NULL(
91 "error",
92 error );
93
94 /* Test error cases
95 */
96 result = libfsapfs_profiler_initialize(
97 NULL,
98 &error );
99
100 FSAPFS_TEST_ASSERT_EQUAL_INT(
101 "result",
102 result,
103 -1 );
104
105 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
106 "error",
107 error );
108
109 libcerror_error_free(
110 &error );
111
112 profiler = (libfsapfs_profiler_t *) 0x12345678UL;
113
114 result = libfsapfs_profiler_initialize(
115 &profiler,
116 &error );
117
118 profiler = NULL;
119
120 FSAPFS_TEST_ASSERT_EQUAL_INT(
121 "result",
122 result,
123 -1 );
124
125 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
126 "error",
127 error );
128
129 libcerror_error_free(
130 &error );
131
132 #if defined( HAVE_FSAPFS_TEST_MEMORY )
133
134 for( test_number = 0;
135 test_number < number_of_malloc_fail_tests;
136 test_number++ )
137 {
138 /* Test libfsapfs_profiler_initialize with malloc failing
139 */
140 fsapfs_test_malloc_attempts_before_fail = test_number;
141
142 result = libfsapfs_profiler_initialize(
143 &profiler,
144 &error );
145
146 if( fsapfs_test_malloc_attempts_before_fail != -1 )
147 {
148 fsapfs_test_malloc_attempts_before_fail = -1;
149
150 if( profiler != NULL )
151 {
152 libfsapfs_profiler_free(
153 &profiler,
154 NULL );
155 }
156 }
157 else
158 {
159 FSAPFS_TEST_ASSERT_EQUAL_INT(
160 "result",
161 result,
162 -1 );
163
164 FSAPFS_TEST_ASSERT_IS_NULL(
165 "profiler",
166 profiler );
167
168 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
169 "error",
170 error );
171
172 libcerror_error_free(
173 &error );
174 }
175 }
176 for( test_number = 0;
177 test_number < number_of_memset_fail_tests;
178 test_number++ )
179 {
180 /* Test libfsapfs_profiler_initialize with memset failing
181 */
182 fsapfs_test_memset_attempts_before_fail = test_number;
183
184 result = libfsapfs_profiler_initialize(
185 &profiler,
186 &error );
187
188 if( fsapfs_test_memset_attempts_before_fail != -1 )
189 {
190 fsapfs_test_memset_attempts_before_fail = -1;
191
192 if( profiler != NULL )
193 {
194 libfsapfs_profiler_free(
195 &profiler,
196 NULL );
197 }
198 }
199 else
200 {
201 FSAPFS_TEST_ASSERT_EQUAL_INT(
202 "result",
203 result,
204 -1 );
205
206 FSAPFS_TEST_ASSERT_IS_NULL(
207 "profiler",
208 profiler );
209
210 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
211 "error",
212 error );
213
214 libcerror_error_free(
215 &error );
216 }
217 }
218 #endif /* defined( HAVE_FSAPFS_TEST_MEMORY ) */
219
220 return( 1 );
221
222 on_error:
223 if( error != NULL )
224 {
225 libcerror_error_free(
226 &error );
227 }
228 if( profiler != NULL )
229 {
230 libfsapfs_profiler_free(
231 &profiler,
232 NULL );
233 }
234 return( 0 );
235 }
236
237 /* Tests the libfsapfs_profiler_free function
238 * Returns 1 if successful or 0 if not
239 */
fsapfs_test_profiler_free(void)240 int fsapfs_test_profiler_free(
241 void )
242 {
243 libcerror_error_t *error = NULL;
244 int result = 0;
245
246 /* Test error cases
247 */
248 result = libfsapfs_profiler_free(
249 NULL,
250 &error );
251
252 FSAPFS_TEST_ASSERT_EQUAL_INT(
253 "result",
254 result,
255 -1 );
256
257 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
258 "error",
259 error );
260
261 libcerror_error_free(
262 &error );
263
264 return( 1 );
265
266 on_error:
267 if( error != NULL )
268 {
269 libcerror_error_free(
270 &error );
271 }
272 return( 0 );
273 }
274
275 /* Tests the libfsapfs_profiler_close function
276 * Returns 1 if successful or 0 if not
277 */
fsapfs_test_profiler_close(void)278 int fsapfs_test_profiler_close(
279 void )
280 {
281 libcerror_error_t *error = NULL;
282 int result = 0;
283
284 /* Test error cases
285 */
286 result = libfsapfs_profiler_close(
287 NULL,
288 &error );
289
290 FSAPFS_TEST_ASSERT_EQUAL_INT(
291 "result",
292 result,
293 -1 );
294
295 FSAPFS_TEST_ASSERT_IS_NOT_NULL(
296 "error",
297 error );
298
299 libcerror_error_free(
300 &error );
301
302 return( 1 );
303
304 on_error:
305 if( error != NULL )
306 {
307 libcerror_error_free(
308 &error );
309 }
310 return( 0 );
311 }
312
313 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
314
315 #endif /* defined( HAVE_PROFILER ) */
316
317 /* The main program
318 */
319 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSAPFS_TEST_ATTRIBUTE_UNUSED)320 int wmain(
321 int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
322 wchar_t * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
323 #else
324 int main(
325 int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
326 char * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
327 #endif
328 {
329 FSAPFS_TEST_UNREFERENCED_PARAMETER( argc )
330 FSAPFS_TEST_UNREFERENCED_PARAMETER( argv )
331
332 #if defined( HAVE_PROFILER )
333
334 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
335
336 FSAPFS_TEST_RUN(
337 "libfsapfs_profiler_initialize",
338 fsapfs_test_profiler_initialize );
339
340 FSAPFS_TEST_RUN(
341 "libfsapfs_profiler_free",
342 fsapfs_test_profiler_free );
343
344 /* TODO: add tests for libfsapfs_profiler_open */
345
346 FSAPFS_TEST_RUN(
347 "libfsapfs_profiler_close",
348 fsapfs_test_profiler_close );
349
350 /* TODO: add tests for libfsapfs_profiler_start_timing */
351
352 /* TODO: add tests for libfsapfs_profiler_stop_timing */
353
354 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
355
356 #endif /* defined( HAVE_PROFILER ) */
357
358 return( EXIT_SUCCESS );
359
360 #if defined( HAVE_PROFILER )
361
362 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
363
364 on_error:
365 return( EXIT_FAILURE );
366
367 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
368
369 #endif /* defined( HAVE_PROFILER ) */
370 }
371
372