diff options
Diffstat (limited to 'src/microhttpd/test_dauth_userhash.c')
-rw-r--r-- | src/microhttpd/test_dauth_userhash.c | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/src/microhttpd/test_dauth_userhash.c b/src/microhttpd/test_dauth_userhash.c index 94230acd..633ab4b9 100644 --- a/src/microhttpd/test_dauth_userhash.c +++ b/src/microhttpd/test_dauth_userhash.c | |||
@@ -119,6 +119,53 @@ static const struct data_sha256 sha256_tests[] = { | |||
119 | 0xe9, 0x19, 0x1d, 0x8e, 0x94, 0x6f, 0xc4, 0xe7}} | 119 | 0xe9, 0x19, 0x1d, 0x8e, 0x94, 0x6f, 0xc4, 0xe7}} |
120 | }; | 120 | }; |
121 | 121 | ||
122 | struct data_sha512_256 | ||
123 | { | ||
124 | unsigned int line_num; | ||
125 | const char *const username; | ||
126 | const char *const realm; | ||
127 | const uint8_t hash[MHD_SHA512_256_DIGEST_SIZE]; | ||
128 | }; | ||
129 | |||
130 | |||
131 | static const struct data_sha512_256 sha512_256_tests[] = { | ||
132 | {__LINE__, | ||
133 | "u", "r", | ||
134 | {0xc7, 0x38, 0xf2, 0xad, 0x40, 0x1b, 0xc8, 0x7a, 0x71, 0xfe, 0x78, 0x09, | ||
135 | 0x60, 0x15, 0xc9, 0x7b, 0x9a, 0x26, 0xd5, 0x5f, 0x15, 0xe9, 0xf5, 0x0a, | ||
136 | 0xc3, 0xa6, 0xde, 0x73, 0xdd, 0xcd, 0x3d, 0x08}}, | ||
137 | {__LINE__, | ||
138 | "testuser", "testrealm", | ||
139 | {0x4f, 0x69, 0x1e, 0xe9, 0x50, 0x8a, 0xe4, 0x55, 0x21, 0x32, 0x9e, 0xcf, | ||
140 | 0xd4, 0x91, 0xf7, 0xe2, 0x77, 0x4b, 0x6f, 0xb8, 0x60, 0x2c, 0x14, 0x86, | ||
141 | 0xad, 0x94, 0x9d, 0x1c, 0x23, 0xd8, 0xa1, 0xf5}}, | ||
142 | {__LINE__, | ||
143 | "test_user", "TestRealm", /* Values from testcurl/test_digestauth2.c */ | ||
144 | {0x62, 0xe1, 0xac, 0x9f, 0x6c, 0xb1, 0xeb, 0x26, 0xaa, 0x75, 0xeb, 0x5d, | ||
145 | 0x46, 0xef, 0xcd, 0xc8, 0x9c, 0xcb, 0xa7, 0x81, 0xf0, 0xf9, 0xf7, 0x2f, | ||
146 | 0x6a, 0xfd, 0xb9, 0x42, 0x65, 0xd9, 0xa7, 0x9a}}, | ||
147 | {__LINE__, | ||
148 | "Mufasa", "myhost@testrealm.com", | ||
149 | {0xbd, 0x3e, 0xbc, 0x30, 0x10, 0x0b, 0x7c, 0xf1, 0x61, 0x45, 0x6c, 0xfe, | ||
150 | 0x64, 0x1c, 0x4c, 0xd2, 0x82, 0xe0, 0x62, 0x6e, 0x2c, 0x5e, 0x09, 0xc2, | ||
151 | 0x4c, 0x90, 0xb1, 0x60, 0x8a, 0xec, 0x28, 0x64}}, | ||
152 | {__LINE__, | ||
153 | "Mufasa", "myhost@example.com", | ||
154 | {0xea, 0x4b, 0x59, 0x37, 0xde, 0x2c, 0x4e, 0x9f, 0x16, 0xf9, 0x9c, 0x31, | ||
155 | 0x01, 0xb6, 0xdd, 0xf8, 0x8c, 0x85, 0xd7, 0xe8, 0xf1, 0x75, 0x90, 0xd0, | ||
156 | 0x63, 0x2a, 0x75, 0x75, 0xe4, 0x80, 0x13, 0x69}}, | ||
157 | {__LINE__, | ||
158 | "Mufasa", "http-auth@example.org", | ||
159 | {0xe2, 0xdf, 0xab, 0xd1, 0xa9, 0x6d, 0xdf, 0x86, 0x77, 0x10, 0xb6, 0x53, | ||
160 | 0xb6, 0xe6, 0x85, 0x7d, 0x1f, 0x14, 0x70, 0x86, 0xde, 0x7d, 0x7e, 0xf7, | ||
161 | 0x9d, 0xcd, 0x24, 0x98, 0x59, 0x87, 0x25, 0x70}}, | ||
162 | {__LINE__, | ||
163 | "J" "\xC3\xA4" "s" "\xC3\xB8" "n Doe" /* "Jäsøn Doe" */, "api@example.org", | ||
164 | {0x79, 0x32, 0x63, 0xca, 0xab, 0xb7, 0x07, 0xa5, 0x62, 0x11, 0x94, 0x0d, | ||
165 | 0x90, 0x41, 0x1e, 0xa4, 0xa5, 0x75, 0xad, 0xec, 0xcb, 0x7e, 0x36, 0x0a, | ||
166 | 0xeb, 0x62, 0x4e, 0xd0, 0x6e, 0xce, 0x9b, 0x0b}} | ||
167 | }; | ||
168 | |||
122 | 169 | ||
123 | /* | 170 | /* |
124 | * Helper functions | 171 | * Helper functions |
@@ -510,6 +557,189 @@ test_sha256_failure (void) | |||
510 | } | 557 | } |
511 | 558 | ||
512 | 559 | ||
560 | static unsigned int | ||
561 | check_sha512_256 (const struct data_sha512_256 *const data) | ||
562 | { | ||
563 | static const enum MHD_DigestAuthAlgo3 algo3 = | ||
564 | MHD_DIGEST_AUTH_ALGO3_SHA512_256; | ||
565 | uint8_t hash_bin[MHD_SHA512_256_DIGEST_SIZE]; | ||
566 | char hash_hex[MHD_SHA512_256_DIGEST_SIZE * 2 + 1]; | ||
567 | char expected_hex[MHD_SHA512_256_DIGEST_SIZE * 2 + 1]; | ||
568 | const char *func_name; | ||
569 | unsigned int failed = 0; | ||
570 | |||
571 | func_name = "MHD_digest_auth_calc_userhash"; | ||
572 | if (MHD_YES != MHD_digest_auth_calc_userhash (algo3, | ||
573 | data->username, data->realm, | ||
574 | hash_bin, sizeof(hash_bin))) | ||
575 | { | ||
576 | failed++; | ||
577 | fprintf (stderr, | ||
578 | "FAILED: %s() has not returned MHD_YES.\n", | ||
579 | func_name); | ||
580 | } | ||
581 | else if (0 != memcmp (hash_bin, data->hash, sizeof(data->hash))) | ||
582 | { | ||
583 | failed++; | ||
584 | bin2hex (hash_bin, sizeof(hash_bin), hash_hex); | ||
585 | bin2hex (data->hash, sizeof(data->hash), expected_hex); | ||
586 | fprintf (stderr, | ||
587 | "FAILED: %s() produced wrong hash. " | ||
588 | "Calculated digest %s, expected digest %s.\n", | ||
589 | func_name, | ||
590 | hash_hex, expected_hex); | ||
591 | } | ||
592 | |||
593 | func_name = "MHD_digest_auth_calc_userhash_hex"; | ||
594 | if (MHD_YES != | ||
595 | MHD_digest_auth_calc_userhash_hex (algo3, | ||
596 | data->username, data->realm, | ||
597 | hash_hex, sizeof(hash_hex))) | ||
598 | { | ||
599 | failed++; | ||
600 | fprintf (stderr, | ||
601 | "FAILED: %s() has not returned MHD_YES.\n", | ||
602 | func_name); | ||
603 | } | ||
604 | else if (sizeof(hash_hex) - 1 != strlen (hash_hex)) | ||
605 | { | ||
606 | failed++; | ||
607 | fprintf (stderr, | ||
608 | "FAILED: %s produced hash with wrong length. " | ||
609 | "Calculated length %u, expected digest %u.\n", | ||
610 | func_name, | ||
611 | (unsigned) strlen (hash_hex), | ||
612 | (unsigned) (sizeof(hash_hex) - 1)); | ||
613 | } | ||
614 | else | ||
615 | { | ||
616 | bin2hex (data->hash, sizeof(data->hash), expected_hex); | ||
617 | if (0 != memcmp (hash_hex, expected_hex, sizeof(hash_hex))) | ||
618 | { | ||
619 | failed++; | ||
620 | fprintf (stderr, | ||
621 | "FAILED: %s() produced wrong hash. " | ||
622 | "Calculated digest %s, expected digest %s.\n", | ||
623 | func_name, | ||
624 | hash_hex, expected_hex); | ||
625 | } | ||
626 | } | ||
627 | |||
628 | if (failed) | ||
629 | { | ||
630 | fprintf (stderr, | ||
631 | "The check failed for data located at line: %u.\n", | ||
632 | data->line_num); | ||
633 | fflush (stderr); | ||
634 | } | ||
635 | else if (verbose) | ||
636 | { | ||
637 | printf ("PASSED: check for data at line: %u.\n", | ||
638 | data->line_num); | ||
639 | } | ||
640 | return failed ? 1 : 0; | ||
641 | } | ||
642 | |||
643 | |||
644 | static unsigned int | ||
645 | test_sha512_256 (void) | ||
646 | { | ||
647 | unsigned int num_failed = 0; | ||
648 | size_t i; | ||
649 | |||
650 | for (i = 0; i < sizeof(sha512_256_tests) / sizeof(sha512_256_tests[0]); i++) | ||
651 | num_failed += check_sha512_256 (sha512_256_tests + i); | ||
652 | return num_failed; | ||
653 | } | ||
654 | |||
655 | |||
656 | static unsigned int | ||
657 | test_sha512_256_failure (void) | ||
658 | { | ||
659 | static const enum MHD_DigestAuthAlgo3 algo3 = | ||
660 | MHD_DIGEST_AUTH_ALGO3_SHA512_256; | ||
661 | static const enum MHD_FEATURE feature = MHD_FEATURE_DIGEST_AUTH_SHA512_256; | ||
662 | uint8_t hash_bin[MHD_SHA512_256_DIGEST_SIZE]; | ||
663 | char hash_hex[MHD_SHA512_256_DIGEST_SIZE * 2 + 1]; | ||
664 | const char *func_name; | ||
665 | unsigned int failed = 0; | ||
666 | |||
667 | func_name = "MHD_digest_auth_calc_userhash"; | ||
668 | if (MHD_NO != MHD_digest_auth_calc_userhash (algo3, | ||
669 | "u", "r", | ||
670 | hash_bin, sizeof(hash_bin) - 1)) | ||
671 | { | ||
672 | failed++; | ||
673 | fprintf (stderr, | ||
674 | "FAILED: %s() has not returned MHD_NO at line: %u.\n", | ||
675 | func_name, (unsigned) __LINE__); | ||
676 | } | ||
677 | if (MHD_NO != MHD_digest_auth_calc_userhash (algo3, | ||
678 | "u", "r", | ||
679 | hash_bin, 0)) | ||
680 | { | ||
681 | failed++; | ||
682 | fprintf (stderr, | ||
683 | "FAILED: %s() has not returned MHD_NO at line: %u.\n", | ||
684 | func_name, (unsigned) __LINE__); | ||
685 | } | ||
686 | if (MHD_NO == MHD_is_feature_supported (feature)) | ||
687 | { | ||
688 | if (MHD_NO != MHD_digest_auth_calc_userhash (algo3, | ||
689 | "u", "r", | ||
690 | hash_bin, sizeof(hash_bin))) | ||
691 | { | ||
692 | failed++; | ||
693 | fprintf (stderr, | ||
694 | "FAILED: %s() has not returned MHD_NO at line: %u.\n", | ||
695 | func_name, (unsigned) __LINE__); | ||
696 | } | ||
697 | } | ||
698 | |||
699 | func_name = "MHD_digest_auth_calc_userhash_hex"; | ||
700 | if (MHD_NO != | ||
701 | MHD_digest_auth_calc_userhash_hex (algo3, | ||
702 | "u", "r", | ||
703 | hash_hex, sizeof(hash_hex) - 1)) | ||
704 | { | ||
705 | failed++; | ||
706 | fprintf (stderr, | ||
707 | "FAILED: %s() has not returned MHD_NO at line: %u.\n", | ||
708 | func_name, (unsigned) __LINE__); | ||
709 | } | ||
710 | if (MHD_NO != | ||
711 | MHD_digest_auth_calc_userhash_hex (algo3, | ||
712 | "u", "r", | ||
713 | hash_hex, 0)) | ||
714 | { | ||
715 | failed++; | ||
716 | fprintf (stderr, | ||
717 | "FAILED: %s() has not returned MHD_NO at line: %u.\n", | ||
718 | func_name, (unsigned) __LINE__); | ||
719 | } | ||
720 | if (MHD_NO == MHD_is_feature_supported (feature)) | ||
721 | { | ||
722 | if (MHD_NO != | ||
723 | MHD_digest_auth_calc_userhash_hex (algo3, | ||
724 | "u", "r", | ||
725 | hash_hex, sizeof(hash_hex))) | ||
726 | { | ||
727 | failed++; | ||
728 | fprintf (stderr, | ||
729 | "FAILED: %s() has not returned MHD_NO at line: %u.\n", | ||
730 | func_name, (unsigned) __LINE__); | ||
731 | } | ||
732 | } | ||
733 | |||
734 | if (! failed && verbose) | ||
735 | { | ||
736 | printf ("PASSED: all checks with expected MHD_NO result near line: %u.\n", | ||
737 | (unsigned) __LINE__); | ||
738 | } | ||
739 | return failed ? 1 : 0; | ||
740 | } | ||
741 | |||
742 | |||
513 | int | 743 | int |
514 | main (int argc, char *argv[]) | 744 | main (int argc, char *argv[]) |
515 | { | 745 | { |
@@ -524,6 +754,9 @@ main (int argc, char *argv[]) | |||
524 | if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA256)) | 754 | if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA256)) |
525 | num_failed += test_sha256 (); | 755 | num_failed += test_sha256 (); |
526 | num_failed += test_sha256_failure (); | 756 | num_failed += test_sha256_failure (); |
757 | if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA512_256)) | ||
758 | num_failed += test_sha512_256 (); | ||
759 | num_failed += test_sha512_256_failure (); | ||
527 | 760 | ||
528 | return num_failed ? 1 : 0; | 761 | return num_failed ? 1 : 0; |
529 | } | 762 | } |