Lines Matching full:test

3  * Runtime test cases for CONFIG_FORTIFY_SOURCE. For additional memcpy()
44 #include <kunit/test.h>
45 #include <kunit/test-bug.h>
84 static void fortify_test_known_sizes(struct kunit *test) in fortify_test_known_sizes() argument
86 KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8); in fortify_test_known_sizes()
87 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10); in fortify_test_known_sizes()
88 KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11); in fortify_test_known_sizes()
90 KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX); in fortify_test_known_sizes()
92 KUNIT_EXPECT_EQ(test, __compiletime_strlen(test->name), SIZE_MAX); in fortify_test_known_sizes()
117 static void fortify_test_control_flow_split(struct kunit *test) in fortify_test_control_flow_split() argument
119 KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX); in fortify_test_control_flow_split()
122 #define KUNIT_EXPECT_BOS(test, p, expected, name) \ argument
123 KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \
128 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \ argument
130 KUNIT_EXPECT_EQ(test, expected, expected)
132 #define KUNIT_EXPECT_BDOS(test, p, expected, name) \ argument
133 KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \
142 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \
143 KUNIT_EXPECT_BOS(test, p, expected, #alloc); \
144 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
152 KUNIT_EXPECT_TRUE_MSG(test, p != NULL, #alloc " failed?!\n"); \
153 KUNIT_EXPECT_BOS(test, p, SIZE_MAX, #alloc); \
154 KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \
176 kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() support\n")
182 * Expected size is "size" in each test, before it is then \
183 * internally incremented in each test. Requires we disable \
188 KUNIT_EXPECT_NE(test, size, unknown_size); \
193 static void fortify_test_alloc_size_##allocator##_const(struct kunit *test) \
197 static void fortify_test_alloc_size_##allocator##_dynamic(struct kunit *test) \
239 KUNIT_EXPECT_TRUE(test, orig != NULL); \
244 KUNIT_EXPECT_TRUE(test, orig != NULL); \
249 KUNIT_EXPECT_TRUE(test, orig != NULL); \
307 KUNIT_EXPECT_TRUE(test, orig != NULL); \
316 const char dev_name[] = "fortify-test"; \
322 dev = kunit_device_register(test, dev_name); \
323 KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), \
324 "Cannot register test device\n"); \
344 KUNIT_EXPECT_TRUE(test, orig != NULL); \
356 kunit_device_unregister(test, dev); \
373 KUNIT_EXPECT_EQ(test, __builtin_constant_p(len), 0); \
380 static void fortify_test_realloc_size(struct kunit *test) in fortify_test_realloc_size() argument
400 static void fortify_test_strlen(struct kunit *test) in fortify_test_strlen() argument
409 KUNIT_EXPECT_EQ(test, pad.buf[end], '\0'); in fortify_test_strlen()
410 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strlen()
413 KUNIT_EXPECT_EQ(test, strlen(pad.buf), end); in fortify_test_strlen()
414 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strlen()
419 KUNIT_EXPECT_EQ(test, strlen(pad.buf), end); in fortify_test_strlen()
420 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_strlen()
423 static void fortify_test_strnlen(struct kunit *test) in fortify_test_strnlen() argument
432 KUNIT_EXPECT_EQ(test, pad.buf[end], '\0'); in fortify_test_strnlen()
433 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strnlen()
436 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf)), end); in fortify_test_strnlen()
437 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strnlen()
439 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, sizeof(pad.buf) / 2), in fortify_test_strnlen()
441 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strnlen()
447 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 1), end); in fortify_test_strnlen()
448 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_strnlen()
449 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end + 2), end); in fortify_test_strnlen()
450 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_strnlen()
453 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end), end); in fortify_test_strnlen()
454 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_strnlen()
457 KUNIT_EXPECT_EQ(test, strnlen(pad.buf, end), end); in fortify_test_strnlen()
458 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_strnlen()
461 static void fortify_test_strcpy(struct kunit *test) in fortify_test_strcpy() argument
472 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); in fortify_test_strcpy()
473 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcpy()
474 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcpy()
475 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strcpy()
476 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strcpy()
479 KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src) in fortify_test_strcpy()
481 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strcpy()
482 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strcpy()
484 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcpy()
485 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcpy()
486 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strcpy()
490 KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src) in fortify_test_strcpy()
492 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strcpy()
493 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); in fortify_test_strcpy()
495 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcpy()
496 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcpy()
497 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcpy()
499 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strcpy()
503 KUNIT_ASSERT_TRUE(test, strcpy(pad.buf, src) in fortify_test_strcpy()
509 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_strcpy()
510 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); in fortify_test_strcpy()
512 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcpy()
513 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcpy()
514 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcpy()
516 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strcpy()
519 static void fortify_test_strncpy(struct kunit *test) in fortify_test_strncpy() argument
525 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); in fortify_test_strncpy()
526 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncpy()
527 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
528 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncpy()
529 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncpy()
532 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, in fortify_test_strncpy()
535 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strncpy()
537 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncpy()
538 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
539 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncpy()
542 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, in fortify_test_strncpy()
545 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strncpy()
547 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncpy()
548 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
549 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
551 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncpy()
554 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, in fortify_test_strncpy()
558 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); in fortify_test_strncpy()
559 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncpy()
560 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
561 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
563 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncpy()
566 KUNIT_ASSERT_TRUE(test, strncpy(pad.buf, src, in fortify_test_strncpy()
570 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); in fortify_test_strncpy()
571 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncpy()
572 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
573 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncpy()
575 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncpy()
578 static void fortify_test_strscpy(struct kunit *test) in fortify_test_strscpy() argument
584 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); in fortify_test_strscpy()
585 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strscpy()
586 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
587 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strscpy()
588 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strscpy()
591 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, in fortify_test_strscpy()
594 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strscpy()
596 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strscpy()
597 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
598 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strscpy()
601 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, in fortify_test_strscpy()
604 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strscpy()
606 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strscpy()
607 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
608 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
611 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, in fortify_test_strscpy()
615 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); in fortify_test_strscpy()
616 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strscpy()
617 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
618 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
620 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strscpy()
623 KUNIT_ASSERT_EQ(test, strscpy(pad.buf, src, in fortify_test_strscpy()
627 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); in fortify_test_strscpy()
628 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strscpy()
629 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
630 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strscpy()
632 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strscpy()
635 static void fortify_test_strcat(struct kunit *test) in fortify_test_strcat() argument
648 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); in fortify_test_strcat()
649 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcat()
650 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcat()
651 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strcat()
652 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strcat()
655 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, src) == pad.buf); in fortify_test_strcat()
656 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strcat()
658 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, src) == pad.buf); in fortify_test_strcat()
659 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strcat()
661 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcat()
662 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcat()
663 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strcat()
666 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, one) == pad.buf); in fortify_test_strcat()
667 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strcat()
669 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcat()
670 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcat()
671 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strcat()
674 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, one) == pad.buf); in fortify_test_strcat()
675 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); in fortify_test_strcat()
677 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcat()
678 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcat()
679 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strcat()
680 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strcat()
683 KUNIT_ASSERT_TRUE(test, strcat(pad.buf, two) == pad.buf); in fortify_test_strcat()
684 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); in fortify_test_strcat()
686 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strcat()
687 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strcat()
688 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strcat()
689 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strcat()
692 static void fortify_test_strncat(struct kunit *test) in fortify_test_strncat() argument
704 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); in fortify_test_strncat()
705 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncat()
706 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncat()
707 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncat()
708 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncat()
711 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, partial) == pad.buf); in fortify_test_strncat()
712 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strncat()
713 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strncat()
715 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, partial) == pad.buf); in fortify_test_strncat()
716 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strncat()
717 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strncat()
719 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncat()
720 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncat()
721 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncat()
724 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf); in fortify_test_strncat()
725 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strncat()
726 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strncat()
728 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncat()
729 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncat()
730 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncat()
733 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf); in fortify_test_strncat()
734 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strncat()
735 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); in fortify_test_strncat()
737 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncat()
738 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncat()
739 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncat()
740 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncat()
743 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 2) == pad.buf); in fortify_test_strncat()
744 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strncat()
745 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); in fortify_test_strncat()
747 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncat()
748 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncat()
749 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncat()
750 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncat()
754 KUNIT_ASSERT_TRUE(test, strncat(pad.buf, src, 1) == pad.buf); in fortify_test_strncat()
756 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_strncat()
757 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 3); in fortify_test_strncat()
758 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strncat()
759 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strncat()
760 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strncat()
762 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strncat()
765 static void fortify_test_strlcat(struct kunit *test) in fortify_test_strlcat() argument
778 KUNIT_EXPECT_EQ(test, pad.bytes_before, 0); in fortify_test_strlcat()
779 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strlcat()
780 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strlcat()
781 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strlcat()
782 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strlcat()
785 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len), partial); in fortify_test_strlcat()
786 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strlcat()
787 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strlcat()
789 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len), partial * 2); in fortify_test_strlcat()
790 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strlcat()
791 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strlcat()
793 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strlcat()
794 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strlcat()
795 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strlcat()
798 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "Q", len), partial * 2 + 1); in fortify_test_strlcat()
799 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strlcat()
800 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); in fortify_test_strlcat()
802 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strlcat()
803 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strlcat()
804 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strlcat()
807 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "V", len * 2), len); in fortify_test_strlcat()
808 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strlcat()
809 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); in fortify_test_strlcat()
811 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strlcat()
812 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strlcat()
813 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strlcat()
814 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strlcat()
817 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "QQ", len * 2), len + 1); in fortify_test_strlcat()
818 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_strlcat()
819 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); in fortify_test_strlcat()
821 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strlcat()
822 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strlcat()
823 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strlcat()
824 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strlcat()
828 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, "TT", len * 2), len + 2); in fortify_test_strlcat()
830 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_strlcat()
831 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); in fortify_test_strlcat()
832 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strlcat()
833 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 2], '\0'); in fortify_test_strlcat()
834 KUNIT_EXPECT_NE(test, pad.buf[sizeof(pad.buf) - 3], '\0'); in fortify_test_strlcat()
836 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strlcat()
841 KUNIT_ASSERT_EQ(test, strlcat(pad.buf, src, len * 3), len - 1 + sizeof(src)); in fortify_test_strlcat()
843 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 3); in fortify_test_strlcat()
844 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 3); in fortify_test_strlcat()
845 KUNIT_EXPECT_EQ(test, pad.buf[sizeof(pad.buf) - 1], '\0'); in fortify_test_strlcat()
847 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); in fortify_test_strlcat()
858 static void fortify_test_##memfunc(struct kunit *test) \
867 KUNIT_ASSERT_EQ(test, srcA[0], 'A'); \
869 KUNIT_ASSERT_EQ(test, srcB[0], 'B'); \
872 KUNIT_EXPECT_EQ(test, pad.buf[0], '\0'); \
873 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
874 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
876 KUNIT_EXPECT_EQ(test, pad.buf[0], '\0'); \
877 KUNIT_EXPECT_EQ(test, pad.buf[1], 'B'); \
878 KUNIT_EXPECT_EQ(test, pad.buf[2], '\0'); \
879 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
880 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
882 KUNIT_EXPECT_EQ(test, pad.buf[0], 'A'); \
883 KUNIT_EXPECT_EQ(test, pad.buf[1], 'B'); \
884 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
885 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
887 KUNIT_EXPECT_EQ(test, pad.buf[1], 'A'); \
888 KUNIT_EXPECT_EQ(test, pad.buf[len - 1], '\0'); \
889 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
890 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
892 KUNIT_EXPECT_EQ(test, pad.buf[1], 'A'); \
893 KUNIT_EXPECT_EQ(test, pad.buf[len - 1], 'A'); \
894 KUNIT_EXPECT_EQ(test, pad.bytes_after, 0); \
895 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
896 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
898 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
899 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); \
901 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
902 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 2); \
908 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
909 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
911 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
912 KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); \
917 static void fortify_test_memscan(struct kunit *test) in __fortify_test()
924 KUNIT_ASSERT_PTR_EQ(test, memscan(haystack, needle, len), in __fortify_test()
926 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in __fortify_test()
928 KUNIT_ASSERT_PTR_EQ(test, memscan(haystack, needle, len + 1), in __fortify_test()
930 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in __fortify_test()
931 KUNIT_ASSERT_PTR_EQ(test, memscan(haystack, needle, len * 2), in __fortify_test()
933 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in __fortify_test()
936 static void fortify_test_memchr(struct kunit *test) in fortify_test_memchr() argument
943 KUNIT_ASSERT_PTR_EQ(test, memchr(haystack, needle, len), in fortify_test_memchr()
945 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_memchr()
947 KUNIT_ASSERT_PTR_EQ(test, memchr(haystack, needle, len + 1), in fortify_test_memchr()
949 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_memchr()
950 KUNIT_ASSERT_PTR_EQ(test, memchr(haystack, needle, len * 2), in fortify_test_memchr()
952 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_memchr()
955 static void fortify_test_memchr_inv(struct kunit *test) in fortify_test_memchr_inv() argument
963 KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len), in fortify_test_memchr_inv()
965 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_memchr_inv()
967 KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len + 1), in fortify_test_memchr_inv()
969 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_memchr_inv()
970 KUNIT_ASSERT_PTR_EQ(test, memchr_inv(haystack, needle, len * 2), in fortify_test_memchr_inv()
972 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_memchr_inv()
975 static void fortify_test_memcmp(struct kunit *test) in fortify_test_memcmp() argument
983 KUNIT_ASSERT_EQ(test, memcmp(one, two, one_len), 0); in fortify_test_memcmp()
984 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_memcmp()
986 KUNIT_ASSERT_LT(test, memcmp(one, two, one_len + 1), 0); in fortify_test_memcmp()
987 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_memcmp()
990 KUNIT_ASSERT_EQ(test, memcmp(one, two, one_len + 2), INT_MIN); in fortify_test_memcmp()
991 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_memcmp()
993 KUNIT_ASSERT_EQ(test, memcmp(two, one, two_len + 2), INT_MIN); in fortify_test_memcmp()
994 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_memcmp()
997 static void fortify_test_kmemdup(struct kunit *test) in fortify_test_kmemdup() argument
1005 KUNIT_EXPECT_NOT_NULL(test, copy); in fortify_test_kmemdup()
1006 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_kmemdup()
1011 KUNIT_EXPECT_NOT_NULL(test, copy); in fortify_test_kmemdup()
1012 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_kmemdup()
1017 KUNIT_EXPECT_NOT_NULL(test, copy); in fortify_test_kmemdup()
1018 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); in fortify_test_kmemdup()
1023 KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR); in fortify_test_kmemdup()
1024 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); in fortify_test_kmemdup()
1029 KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR); in fortify_test_kmemdup()
1030 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2); in fortify_test_kmemdup()
1035 KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR); in fortify_test_kmemdup()
1036 KUNIT_EXPECT_EQ(test, fortify_read_overflows, 3); in fortify_test_kmemdup()
1040 static int fortify_test_init(struct kunit *test) in fortify_test_init() argument
1043 kunit_skip(test, "Not built with CONFIG_FORTIFY_SOURCE=y"); in fortify_test_init()
1046 kunit_add_named_resource(test, NULL, NULL, &read_resource, in fortify_test_init()
1050 kunit_add_named_resource(test, NULL, NULL, &write_resource, in fortify_test_init()
1095 MODULE_DESCRIPTION("Runtime test cases for CONFIG_FORTIFY_SOURCE");