1 /*
2  * Library signature_table type test program
3  *
4  * Copyright (C) 2014-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 "sigscan_test_libcerror.h"
31 #include "sigscan_test_libsigscan.h"
32 #include "sigscan_test_macros.h"
33 #include "sigscan_test_memory.h"
34 #include "sigscan_test_unused.h"
35 
36 #include "../libsigscan/libsigscan_signature_table.h"
37 
38 #if defined( __GNUC__ ) && !defined( LIBSIGSCAN_DLL_IMPORT )
39 
40 /* Tests the libsigscan_signature_table_initialize function
41  * Returns 1 if successful or 0 if not
42  */
sigscan_test_signature_table_initialize(void)43 int sigscan_test_signature_table_initialize(
44      void )
45 {
46 	libcerror_error_t *error                      = NULL;
47 	libsigscan_signature_table_t *signature_table = NULL;
48 	int result                                    = 0;
49 
50 #if defined( HAVE_SIGSCAN_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 = libsigscan_signature_table_initialize(
59 	          &signature_table,
60 	          &error );
61 
62 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
63 	 "result",
64 	 result,
65 	 1 );
66 
67 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
68 	 "signature_table",
69 	 signature_table );
70 
71 	SIGSCAN_TEST_ASSERT_IS_NULL(
72 	 "error",
73 	 error );
74 
75 	result = libsigscan_signature_table_free(
76 	          &signature_table,
77 	          &error );
78 
79 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
80 	 "result",
81 	 result,
82 	 1 );
83 
84 	SIGSCAN_TEST_ASSERT_IS_NULL(
85 	 "signature_table",
86 	 signature_table );
87 
88 	SIGSCAN_TEST_ASSERT_IS_NULL(
89 	 "error",
90 	 error );
91 
92 	/* Test error cases
93 	 */
94 	result = libsigscan_signature_table_initialize(
95 	          NULL,
96 	          &error );
97 
98 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
99 	 "result",
100 	 result,
101 	 -1 );
102 
103 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
104 	 "error",
105 	 error );
106 
107 	libcerror_error_free(
108 	 &error );
109 
110 	signature_table = (libsigscan_signature_table_t *) 0x12345678UL;
111 
112 	result = libsigscan_signature_table_initialize(
113 	          &signature_table,
114 	          &error );
115 
116 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
117 	 "result",
118 	 result,
119 	 -1 );
120 
121 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
122 	 "error",
123 	 error );
124 
125 	libcerror_error_free(
126 	 &error );
127 
128 	signature_table = NULL;
129 
130 #if defined( HAVE_SIGSCAN_TEST_MEMORY )
131 
132 	for( test_number = 0;
133 	     test_number < number_of_malloc_fail_tests;
134 	     test_number++ )
135 	{
136 		/* Test libsigscan_signature_table_initialize with malloc failing
137 		 */
138 		sigscan_test_malloc_attempts_before_fail = test_number;
139 
140 		result = libsigscan_signature_table_initialize(
141 		          &signature_table,
142 		          &error );
143 
144 		if( sigscan_test_malloc_attempts_before_fail != -1 )
145 		{
146 			sigscan_test_malloc_attempts_before_fail = -1;
147 
148 			if( signature_table != NULL )
149 			{
150 				libsigscan_signature_table_free(
151 				 &signature_table,
152 				 NULL );
153 			}
154 		}
155 		else
156 		{
157 			SIGSCAN_TEST_ASSERT_EQUAL_INT(
158 			 "result",
159 			 result,
160 			 -1 );
161 
162 			SIGSCAN_TEST_ASSERT_IS_NULL(
163 			 "signature_table",
164 			 signature_table );
165 
166 			SIGSCAN_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 libsigscan_signature_table_initialize with memset failing
179 		 */
180 		sigscan_test_memset_attempts_before_fail = test_number;
181 
182 		result = libsigscan_signature_table_initialize(
183 		          &signature_table,
184 		          &error );
185 
186 		if( sigscan_test_memset_attempts_before_fail != -1 )
187 		{
188 			sigscan_test_memset_attempts_before_fail = -1;
189 
190 			if( signature_table != NULL )
191 			{
192 				libsigscan_signature_table_free(
193 				 &signature_table,
194 				 NULL );
195 			}
196 		}
197 		else
198 		{
199 			SIGSCAN_TEST_ASSERT_EQUAL_INT(
200 			 "result",
201 			 result,
202 			 -1 );
203 
204 			SIGSCAN_TEST_ASSERT_IS_NULL(
205 			 "signature_table",
206 			 signature_table );
207 
208 			SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
209 			 "error",
210 			 error );
211 
212 			libcerror_error_free(
213 			 &error );
214 		}
215 	}
216 #endif /* defined( HAVE_SIGSCAN_TEST_MEMORY ) */
217 
218 	return( 1 );
219 
220 on_error:
221 	if( error != NULL )
222 	{
223 		libcerror_error_free(
224 		 &error );
225 	}
226 	if( signature_table != NULL )
227 	{
228 		libsigscan_signature_table_free(
229 		 &signature_table,
230 		 NULL );
231 	}
232 	return( 0 );
233 }
234 
235 /* Tests the libsigscan_signature_table_free function
236  * Returns 1 if successful or 0 if not
237  */
sigscan_test_signature_table_free(void)238 int sigscan_test_signature_table_free(
239      void )
240 {
241 	libcerror_error_t *error = NULL;
242 	int result               = 0;
243 
244 	/* Test error cases
245 	 */
246 	result = libsigscan_signature_table_free(
247 	          NULL,
248 	          &error );
249 
250 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
251 	 "result",
252 	 result,
253 	 -1 );
254 
255 	SIGSCAN_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 /* Tests the libsigscan_signature_table_get_number_of_byte_value_groups function
274  * Returns 1 if successful or 0 if not
275  */
sigscan_test_signature_table_get_number_of_byte_value_groups(void)276 int sigscan_test_signature_table_get_number_of_byte_value_groups(
277      void )
278 {
279 	libcerror_error_t *error                      = NULL;
280 	libsigscan_signature_table_t *signature_table = NULL;
281 	int number_of_byte_value_groups               = 0;
282 	int number_of_byte_value_groups_is_set        = 0;
283 	int result                                    = 0;
284 
285 	/* Initialize test
286 	 */
287 	result = libsigscan_signature_table_initialize(
288 	          &signature_table,
289 	          &error );
290 
291 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
292 	 "result",
293 	 result,
294 	 1 );
295 
296 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
297 	 "signature_table",
298 	 signature_table );
299 
300 	SIGSCAN_TEST_ASSERT_IS_NULL(
301 	 "error",
302 	 error );
303 
304 	/* Test regular cases
305 	 */
306 	result = libsigscan_signature_table_get_number_of_byte_value_groups(
307 	          signature_table,
308 	          &number_of_byte_value_groups,
309 	          &error );
310 
311 	SIGSCAN_TEST_ASSERT_NOT_EQUAL_INT(
312 	 "result",
313 	 result,
314 	 -1 );
315 
316 	SIGSCAN_TEST_ASSERT_IS_NULL(
317 	 "error",
318 	 error );
319 
320 	number_of_byte_value_groups_is_set = result;
321 
322 	/* Test error cases
323 	 */
324 	result = libsigscan_signature_table_get_number_of_byte_value_groups(
325 	          NULL,
326 	          &number_of_byte_value_groups,
327 	          &error );
328 
329 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
330 	 "result",
331 	 result,
332 	 -1 );
333 
334 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
335 	 "error",
336 	 error );
337 
338 	libcerror_error_free(
339 	 &error );
340 
341 	if( number_of_byte_value_groups_is_set != 0 )
342 	{
343 		result = libsigscan_signature_table_get_number_of_byte_value_groups(
344 		          signature_table,
345 		          NULL,
346 		          &error );
347 
348 		SIGSCAN_TEST_ASSERT_EQUAL_INT(
349 		 "result",
350 		 result,
351 		 -1 );
352 
353 		SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
354 		 "error",
355 		 error );
356 
357 		libcerror_error_free(
358 		 &error );
359 	}
360 	/* Clean up
361 	 */
362 	result = libsigscan_signature_table_free(
363 	          &signature_table,
364 	          &error );
365 
366 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
367 	 "result",
368 	 result,
369 	 1 );
370 
371 	SIGSCAN_TEST_ASSERT_IS_NULL(
372 	 "signature_table",
373 	 signature_table );
374 
375 	SIGSCAN_TEST_ASSERT_IS_NULL(
376 	 "error",
377 	 error );
378 
379 	return( 1 );
380 
381 on_error:
382 	if( error != NULL )
383 	{
384 		libcerror_error_free(
385 		 &error );
386 	}
387 	if( signature_table != NULL )
388 	{
389 		libsigscan_signature_table_free(
390 		 &signature_table,
391 		 NULL );
392 	}
393 	return( 0 );
394 }
395 
396 /* Tests the libsigscan_signature_table_get_number_of_signatures function
397  * Returns 1 if successful or 0 if not
398  */
sigscan_test_signature_table_get_number_of_signatures(void)399 int sigscan_test_signature_table_get_number_of_signatures(
400      void )
401 {
402 	libcerror_error_t *error                      = NULL;
403 	libsigscan_signature_table_t *signature_table = NULL;
404 	int number_of_signatures                      = 0;
405 	int number_of_signatures_is_set               = 0;
406 	int result                                    = 0;
407 
408 	/* Initialize test
409 	 */
410 	result = libsigscan_signature_table_initialize(
411 	          &signature_table,
412 	          &error );
413 
414 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
415 	 "result",
416 	 result,
417 	 1 );
418 
419 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
420 	 "signature_table",
421 	 signature_table );
422 
423 	SIGSCAN_TEST_ASSERT_IS_NULL(
424 	 "error",
425 	 error );
426 
427 	/* Test regular cases
428 	 */
429 	result = libsigscan_signature_table_get_number_of_signatures(
430 	          signature_table,
431 	          &number_of_signatures,
432 	          &error );
433 
434 	SIGSCAN_TEST_ASSERT_NOT_EQUAL_INT(
435 	 "result",
436 	 result,
437 	 -1 );
438 
439 	SIGSCAN_TEST_ASSERT_IS_NULL(
440 	 "error",
441 	 error );
442 
443 	number_of_signatures_is_set = result;
444 
445 	/* Test error cases
446 	 */
447 	result = libsigscan_signature_table_get_number_of_signatures(
448 	          NULL,
449 	          &number_of_signatures,
450 	          &error );
451 
452 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
453 	 "result",
454 	 result,
455 	 -1 );
456 
457 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
458 	 "error",
459 	 error );
460 
461 	libcerror_error_free(
462 	 &error );
463 
464 	if( number_of_signatures_is_set != 0 )
465 	{
466 		result = libsigscan_signature_table_get_number_of_signatures(
467 		          signature_table,
468 		          NULL,
469 		          &error );
470 
471 		SIGSCAN_TEST_ASSERT_EQUAL_INT(
472 		 "result",
473 		 result,
474 		 -1 );
475 
476 		SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
477 		 "error",
478 		 error );
479 
480 		libcerror_error_free(
481 		 &error );
482 	}
483 	/* Clean up
484 	 */
485 	result = libsigscan_signature_table_free(
486 	          &signature_table,
487 	          &error );
488 
489 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
490 	 "result",
491 	 result,
492 	 1 );
493 
494 	SIGSCAN_TEST_ASSERT_IS_NULL(
495 	 "signature_table",
496 	 signature_table );
497 
498 	SIGSCAN_TEST_ASSERT_IS_NULL(
499 	 "error",
500 	 error );
501 
502 	return( 1 );
503 
504 on_error:
505 	if( error != NULL )
506 	{
507 		libcerror_error_free(
508 		 &error );
509 	}
510 	if( signature_table != NULL )
511 	{
512 		libsigscan_signature_table_free(
513 		 &signature_table,
514 		 NULL );
515 	}
516 	return( 0 );
517 }
518 
519 /* Tests the libsigscan_signature_table_get_signatures_list_clone function
520  * Returns 1 if successful or 0 if not
521  */
sigscan_test_signature_table_get_signatures_list_clone(void)522 int sigscan_test_signature_table_get_signatures_list_clone(
523      void )
524 {
525 	libcerror_error_t *error                      = NULL;
526 	libsigscan_signature_table_t *signature_table = NULL;
527 	libcdata_list_t *signatures_list_clone        = 0;
528 	int result                                    = 0;
529 	int signatures_list_clone_is_set              = 0;
530 
531 	/* Initialize test
532 	 */
533 	result = libsigscan_signature_table_initialize(
534 	          &signature_table,
535 	          &error );
536 
537 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
538 	 "result",
539 	 result,
540 	 1 );
541 
542 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
543 	 "signature_table",
544 	 signature_table );
545 
546 	SIGSCAN_TEST_ASSERT_IS_NULL(
547 	 "error",
548 	 error );
549 
550 	/* Test regular cases
551 	 */
552 	result = libsigscan_signature_table_get_signatures_list_clone(
553 	          signature_table,
554 	          &signatures_list_clone,
555 	          &error );
556 
557 	SIGSCAN_TEST_ASSERT_NOT_EQUAL_INT(
558 	 "result",
559 	 result,
560 	 -1 );
561 
562 	SIGSCAN_TEST_ASSERT_IS_NULL(
563 	 "error",
564 	 error );
565 
566 	signatures_list_clone_is_set = result;
567 
568 	/* Test error cases
569 	 */
570 	result = libsigscan_signature_table_get_signatures_list_clone(
571 	          NULL,
572 	          &signatures_list_clone,
573 	          &error );
574 
575 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
576 	 "result",
577 	 result,
578 	 -1 );
579 
580 	SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
581 	 "error",
582 	 error );
583 
584 	libcerror_error_free(
585 	 &error );
586 
587 	if( signatures_list_clone_is_set != 0 )
588 	{
589 		result = libsigscan_signature_table_get_signatures_list_clone(
590 		          signature_table,
591 		          NULL,
592 		          &error );
593 
594 		SIGSCAN_TEST_ASSERT_EQUAL_INT(
595 		 "result",
596 		 result,
597 		 -1 );
598 
599 		SIGSCAN_TEST_ASSERT_IS_NOT_NULL(
600 		 "error",
601 		 error );
602 
603 		libcerror_error_free(
604 		 &error );
605 	}
606 	/* Clean up
607 	 */
608 	result = libsigscan_signature_table_free_signatures_list_clone(
609 	          &signatures_list_clone,
610 	          &error );
611 
612 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
613 	 "result",
614 	 result,
615 	 1 );
616 
617 	SIGSCAN_TEST_ASSERT_IS_NULL(
618 	 "signatures_list_clone",
619 	 signatures_list_clone );
620 
621 	SIGSCAN_TEST_ASSERT_IS_NULL(
622 	 "error",
623 	 error );
624 
625 	result = libsigscan_signature_table_free(
626 	          &signature_table,
627 	          &error );
628 
629 	SIGSCAN_TEST_ASSERT_EQUAL_INT(
630 	 "result",
631 	 result,
632 	 1 );
633 
634 	SIGSCAN_TEST_ASSERT_IS_NULL(
635 	 "signature_table",
636 	 signature_table );
637 
638 	SIGSCAN_TEST_ASSERT_IS_NULL(
639 	 "error",
640 	 error );
641 
642 	return( 1 );
643 
644 on_error:
645 	if( error != NULL )
646 	{
647 		libcerror_error_free(
648 		 &error );
649 	}
650 	if( signatures_list_clone != NULL )
651 	{
652 		libsigscan_signature_table_free_signatures_list_clone(
653 		 &signatures_list_clone,
654 		 NULL );
655 	}
656 	if( signature_table != NULL )
657 	{
658 		libsigscan_signature_table_free(
659 		 &signature_table,
660 		 NULL );
661 	}
662 	return( 0 );
663 }
664 
665 #endif /* defined( __GNUC__ ) && !defined( LIBSIGSCAN_DLL_IMPORT ) */
666 
667 /* The main program
668  */
669 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc SIGSCAN_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]SIGSCAN_TEST_ATTRIBUTE_UNUSED)670 int wmain(
671      int argc SIGSCAN_TEST_ATTRIBUTE_UNUSED,
672      wchar_t * const argv[] SIGSCAN_TEST_ATTRIBUTE_UNUSED )
673 #else
674 int main(
675      int argc SIGSCAN_TEST_ATTRIBUTE_UNUSED,
676      char * const argv[] SIGSCAN_TEST_ATTRIBUTE_UNUSED )
677 #endif
678 {
679 	SIGSCAN_TEST_UNREFERENCED_PARAMETER( argc )
680 	SIGSCAN_TEST_UNREFERENCED_PARAMETER( argv )
681 
682 #if defined( __GNUC__ ) && !defined( LIBSIGSCAN_DLL_IMPORT )
683 
684 	SIGSCAN_TEST_RUN(
685 	 "libsigscan_signature_table_initialize",
686 	 sigscan_test_signature_table_initialize );
687 
688 	SIGSCAN_TEST_RUN(
689 	 "libsigscan_signature_table_free",
690 	 sigscan_test_signature_table_free );
691 
692 	/* TODO: add tests for libsigscan_signature_table_fill */
693 
694 	SIGSCAN_TEST_RUN(
695 	 "libsigscan_signature_table_get_number_of_byte_value_groups",
696 	 sigscan_test_signature_table_get_number_of_byte_value_groups );
697 
698 	/* TODO: add tests for libsigscan_signature_table_get_byte_value_group_by_index */
699 
700 	/* TODO: add tests for libsigscan_signature_table_get_byte_value_group_by_offset */
701 
702 	SIGSCAN_TEST_RUN(
703 	 "libsigscan_signature_table_get_number_of_signatures",
704 	 sigscan_test_signature_table_get_number_of_signatures );
705 
706 	SIGSCAN_TEST_RUN(
707 	 "libsigscan_signature_table_get_signatures_list_clone",
708 	 sigscan_test_signature_table_get_signatures_list_clone );
709 
710 	/* TODO: add tests for libsigscan_signature_table_insert_signature */
711 
712 #endif /* defined( __GNUC__ ) && !defined( LIBSIGSCAN_DLL_IMPORT ) */
713 
714 	return( EXIT_SUCCESS );
715 
716 on_error:
717 	return( EXIT_FAILURE );
718 }
719 
720