1 /*
2 * Library strings_array 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 "evt_test_libcerror.h"
31 #include "evt_test_libevt.h"
32 #include "evt_test_macros.h"
33 #include "evt_test_memory.h"
34 #include "evt_test_unused.h"
35
36 #include "../libevt/libevt_strings_array.h"
37
38 uint8_t evt_test_strings_array_data1[ 28 ] = {
39 0x52, 0x00, 0x53, 0x00, 0x56, 0x00, 0x50, 0x00, 0x00, 0x00, 0x51, 0x00, 0x6f, 0x00, 0x53, 0x00,
40 0x20, 0x00, 0x52, 0x00, 0x53, 0x00, 0x56, 0x00, 0x50, 0x00, 0x00, 0x00 };
41
42 #if defined( __GNUC__ ) && !defined( LIBEVT_DLL_IMPORT )
43
44 /* Tests the libevt_strings_array_initialize function
45 * Returns 1 if successful or 0 if not
46 */
evt_test_strings_array_initialize(void)47 int evt_test_strings_array_initialize(
48 void )
49 {
50 libcerror_error_t *error = NULL;
51 libevt_strings_array_t *strings_array = NULL;
52 int result = 0;
53
54 #if defined( HAVE_EVT_TEST_MEMORY )
55 int number_of_malloc_fail_tests = 1;
56 int number_of_memset_fail_tests = 1;
57 int test_number = 0;
58 #endif
59
60 /* Test regular cases
61 */
62 result = libevt_strings_array_initialize(
63 &strings_array,
64 &error );
65
66 EVT_TEST_ASSERT_EQUAL_INT(
67 "result",
68 result,
69 1 );
70
71 EVT_TEST_ASSERT_IS_NOT_NULL(
72 "strings_array",
73 strings_array );
74
75 EVT_TEST_ASSERT_IS_NULL(
76 "error",
77 error );
78
79 result = libevt_strings_array_free(
80 &strings_array,
81 &error );
82
83 EVT_TEST_ASSERT_EQUAL_INT(
84 "result",
85 result,
86 1 );
87
88 EVT_TEST_ASSERT_IS_NULL(
89 "strings_array",
90 strings_array );
91
92 EVT_TEST_ASSERT_IS_NULL(
93 "error",
94 error );
95
96 /* Test error cases
97 */
98 result = libevt_strings_array_initialize(
99 NULL,
100 &error );
101
102 EVT_TEST_ASSERT_EQUAL_INT(
103 "result",
104 result,
105 -1 );
106
107 EVT_TEST_ASSERT_IS_NOT_NULL(
108 "error",
109 error );
110
111 libcerror_error_free(
112 &error );
113
114 strings_array = (libevt_strings_array_t *) 0x12345678UL;
115
116 result = libevt_strings_array_initialize(
117 &strings_array,
118 &error );
119
120 strings_array = NULL;
121
122 EVT_TEST_ASSERT_EQUAL_INT(
123 "result",
124 result,
125 -1 );
126
127 EVT_TEST_ASSERT_IS_NOT_NULL(
128 "error",
129 error );
130
131 libcerror_error_free(
132 &error );
133
134 #if defined( HAVE_EVT_TEST_MEMORY )
135
136 for( test_number = 0;
137 test_number < number_of_malloc_fail_tests;
138 test_number++ )
139 {
140 /* Test libevt_strings_array_initialize with malloc failing
141 */
142 evt_test_malloc_attempts_before_fail = test_number;
143
144 result = libevt_strings_array_initialize(
145 &strings_array,
146 &error );
147
148 if( evt_test_malloc_attempts_before_fail != -1 )
149 {
150 evt_test_malloc_attempts_before_fail = -1;
151
152 if( strings_array != NULL )
153 {
154 libevt_strings_array_free(
155 &strings_array,
156 NULL );
157 }
158 }
159 else
160 {
161 EVT_TEST_ASSERT_EQUAL_INT(
162 "result",
163 result,
164 -1 );
165
166 EVT_TEST_ASSERT_IS_NULL(
167 "strings_array",
168 strings_array );
169
170 EVT_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 libevt_strings_array_initialize with memset failing
183 */
184 evt_test_memset_attempts_before_fail = test_number;
185
186 result = libevt_strings_array_initialize(
187 &strings_array,
188 &error );
189
190 if( evt_test_memset_attempts_before_fail != -1 )
191 {
192 evt_test_memset_attempts_before_fail = -1;
193
194 if( strings_array != NULL )
195 {
196 libevt_strings_array_free(
197 &strings_array,
198 NULL );
199 }
200 }
201 else
202 {
203 EVT_TEST_ASSERT_EQUAL_INT(
204 "result",
205 result,
206 -1 );
207
208 EVT_TEST_ASSERT_IS_NULL(
209 "strings_array",
210 strings_array );
211
212 EVT_TEST_ASSERT_IS_NOT_NULL(
213 "error",
214 error );
215
216 libcerror_error_free(
217 &error );
218 }
219 }
220 #endif /* defined( HAVE_EVT_TEST_MEMORY ) */
221
222 return( 1 );
223
224 on_error:
225 if( error != NULL )
226 {
227 libcerror_error_free(
228 &error );
229 }
230 if( strings_array != NULL )
231 {
232 libevt_strings_array_free(
233 &strings_array,
234 NULL );
235 }
236 return( 0 );
237 }
238
239 /* Tests the libevt_strings_array_free function
240 * Returns 1 if successful or 0 if not
241 */
evt_test_strings_array_free(void)242 int evt_test_strings_array_free(
243 void )
244 {
245 libcerror_error_t *error = NULL;
246 int result = 0;
247
248 /* Test error cases
249 */
250 result = libevt_strings_array_free(
251 NULL,
252 &error );
253
254 EVT_TEST_ASSERT_EQUAL_INT(
255 "result",
256 result,
257 -1 );
258
259 EVT_TEST_ASSERT_IS_NOT_NULL(
260 "error",
261 error );
262
263 libcerror_error_free(
264 &error );
265
266 return( 1 );
267
268 on_error:
269 if( error != NULL )
270 {
271 libcerror_error_free(
272 &error );
273 }
274 return( 0 );
275 }
276
277 /* Tests the libevt_strings_array_read_data function
278 * Returns 1 if successful or 0 if not
279 */
evt_test_strings_array_read_data(void)280 int evt_test_strings_array_read_data(
281 void )
282 {
283 libcerror_error_t *error = NULL;
284 libevt_strings_array_t *strings_array = NULL;
285 int result = 0;
286
287 /* Initialize test
288 */
289 result = libevt_strings_array_initialize(
290 &strings_array,
291 &error );
292
293 EVT_TEST_ASSERT_EQUAL_INT(
294 "result",
295 result,
296 1 );
297
298 EVT_TEST_ASSERT_IS_NOT_NULL(
299 "strings_array",
300 strings_array );
301
302 EVT_TEST_ASSERT_IS_NULL(
303 "error",
304 error );
305
306 /* Test regular cases
307 */
308 result = libevt_strings_array_read_data(
309 strings_array,
310 evt_test_strings_array_data1,
311 28,
312 &error );
313
314 EVT_TEST_ASSERT_EQUAL_INT(
315 "result",
316 result,
317 1 );
318
319 EVT_TEST_ASSERT_IS_NULL(
320 "error",
321 error );
322
323 /* Test error cases
324 */
325 result = libevt_strings_array_read_data(
326 strings_array,
327 evt_test_strings_array_data1,
328 28,
329 &error );
330
331 EVT_TEST_ASSERT_EQUAL_INT(
332 "result",
333 result,
334 -1 );
335
336 EVT_TEST_ASSERT_IS_NOT_NULL(
337 "error",
338 error );
339
340 libcerror_error_free(
341 &error );
342
343 /* Clean up
344 */
345 result = libevt_strings_array_free(
346 &strings_array,
347 &error );
348
349 EVT_TEST_ASSERT_EQUAL_INT(
350 "result",
351 result,
352 1 );
353
354 EVT_TEST_ASSERT_IS_NULL(
355 "strings_array",
356 strings_array );
357
358 EVT_TEST_ASSERT_IS_NULL(
359 "error",
360 error );
361
362 /* Initialize test
363 */
364 result = libevt_strings_array_initialize(
365 &strings_array,
366 &error );
367
368 EVT_TEST_ASSERT_EQUAL_INT(
369 "result",
370 result,
371 1 );
372
373 EVT_TEST_ASSERT_IS_NOT_NULL(
374 "strings_array",
375 strings_array );
376
377 EVT_TEST_ASSERT_IS_NULL(
378 "error",
379 error );
380
381 /* Test error cases
382 */
383 result = libevt_strings_array_read_data(
384 NULL,
385 evt_test_strings_array_data1,
386 28,
387 &error );
388
389 EVT_TEST_ASSERT_EQUAL_INT(
390 "result",
391 result,
392 -1 );
393
394 EVT_TEST_ASSERT_IS_NOT_NULL(
395 "error",
396 error );
397
398 libcerror_error_free(
399 &error );
400
401 result = libevt_strings_array_read_data(
402 strings_array,
403 NULL,
404 28,
405 &error );
406
407 EVT_TEST_ASSERT_EQUAL_INT(
408 "result",
409 result,
410 -1 );
411
412 EVT_TEST_ASSERT_IS_NOT_NULL(
413 "error",
414 error );
415
416 libcerror_error_free(
417 &error );
418
419 result = libevt_strings_array_read_data(
420 strings_array,
421 evt_test_strings_array_data1,
422 (size_t) SSIZE_MAX + 1,
423 &error );
424
425 EVT_TEST_ASSERT_EQUAL_INT(
426 "result",
427 result,
428 -1 );
429
430 EVT_TEST_ASSERT_IS_NOT_NULL(
431 "error",
432 error );
433
434 libcerror_error_free(
435 &error );
436
437 result = libevt_strings_array_read_data(
438 strings_array,
439 evt_test_strings_array_data1,
440 0,
441 &error );
442
443 EVT_TEST_ASSERT_EQUAL_INT(
444 "result",
445 result,
446 -1 );
447
448 EVT_TEST_ASSERT_IS_NOT_NULL(
449 "error",
450 error );
451
452 libcerror_error_free(
453 &error );
454
455 result = libevt_strings_array_read_data(
456 strings_array,
457 evt_test_strings_array_data1,
458 27,
459 &error );
460
461 EVT_TEST_ASSERT_EQUAL_INT(
462 "result",
463 result,
464 -1 );
465
466 EVT_TEST_ASSERT_IS_NOT_NULL(
467 "error",
468 error );
469
470 libcerror_error_free(
471 &error );
472
473 /* Clean up
474 */
475 result = libevt_strings_array_free(
476 &strings_array,
477 &error );
478
479 EVT_TEST_ASSERT_EQUAL_INT(
480 "result",
481 result,
482 1 );
483
484 EVT_TEST_ASSERT_IS_NULL(
485 "strings_array",
486 strings_array );
487
488 EVT_TEST_ASSERT_IS_NULL(
489 "error",
490 error );
491
492 return( 1 );
493
494 on_error:
495 if( error != NULL )
496 {
497 libcerror_error_free(
498 &error );
499 }
500 if( strings_array != NULL )
501 {
502 libevt_strings_array_free(
503 &strings_array,
504 NULL );
505 }
506 return( 0 );
507 }
508
509 #endif /* defined( __GNUC__ ) && !defined( LIBEVT_DLL_IMPORT ) */
510
511 /* The main program
512 */
513 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc EVT_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]EVT_TEST_ATTRIBUTE_UNUSED)514 int wmain(
515 int argc EVT_TEST_ATTRIBUTE_UNUSED,
516 wchar_t * const argv[] EVT_TEST_ATTRIBUTE_UNUSED )
517 #else
518 int main(
519 int argc EVT_TEST_ATTRIBUTE_UNUSED,
520 char * const argv[] EVT_TEST_ATTRIBUTE_UNUSED )
521 #endif
522 {
523 EVT_TEST_UNREFERENCED_PARAMETER( argc )
524 EVT_TEST_UNREFERENCED_PARAMETER( argv )
525
526 #if defined( __GNUC__ ) && !defined( LIBEVT_DLL_IMPORT )
527
528 EVT_TEST_RUN(
529 "libevt_strings_array_initialize",
530 evt_test_strings_array_initialize );
531
532 EVT_TEST_RUN(
533 "libevt_strings_array_free",
534 evt_test_strings_array_free );
535
536 EVT_TEST_RUN(
537 "libevt_strings_array_read_data",
538 evt_test_strings_array_read_data );
539
540 /* TODO add tests for libevt_strings_array_get_number_of_strings */
541
542 /* TODO add tests for libevt_strings_array_get_utf8_string_size */
543
544 /* TODO add tests for libevt_strings_array_get_utf8_string */
545
546 /* TODO add tests for libevt_strings_array_get_utf16_string_size */
547
548 /* TODO add tests for libevt_strings_array_get_utf16_string */
549
550 #endif /* defined( __GNUC__ ) && !defined( LIBEVT_DLL_IMPORT ) */
551
552 return( EXIT_SUCCESS );
553
554 on_error:
555 return( EXIT_FAILURE );
556 }
557
558