aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_dauth_userdigest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/test_dauth_userdigest.c')
-rw-r--r--src/microhttpd/test_dauth_userdigest.c201
1 files changed, 201 insertions, 0 deletions
diff --git a/src/microhttpd/test_dauth_userdigest.c b/src/microhttpd/test_dauth_userdigest.c
index 10bf0515..6b15a282 100644
--- a/src/microhttpd/test_dauth_userdigest.c
+++ b/src/microhttpd/test_dauth_userdigest.c
@@ -123,6 +123,54 @@ static const struct data_sha256 sha256_tests[] = {
123 0xcb, 0x14, 0x9c, 0x54, 0xf3, 0x7c, 0xff, 0x37}} 123 0xcb, 0x14, 0x9c, 0x54, 0xf3, 0x7c, 0xff, 0x37}}
124}; 124};
125 125
126struct data_sha512_256
127{
128 unsigned int line_num;
129 const char *const username;
130 const char *const realm;
131 const char *const password;
132 const uint8_t hash[MHD_SHA512_256_DIGEST_SIZE];
133};
134
135static const struct data_sha512_256 sha512_256_tests[] = {
136 {__LINE__,
137 "u", "r", "p",
138 {0xd5, 0xe8, 0xe7, 0x3b, 0xa3, 0x47, 0xb9, 0xad, 0xf0, 0xe4, 0x7a, 0x9a,
139 0xce, 0x43, 0xb7, 0x08, 0x2a, 0xbc, 0x8d, 0x27, 0x27, 0x2e, 0x38, 0x7d,
140 0x1d, 0x9c, 0xe2, 0x44, 0x25, 0x68, 0x74, 0x04}},
141 {__LINE__,
142 "testuser", "testrealm", "testpass",
143 {0x41, 0x7d, 0xf9, 0x60, 0x7c, 0xc9, 0x60, 0x28, 0x44, 0x74, 0x75, 0xf7,
144 0x7b, 0x78, 0xe7, 0x60, 0xec, 0x9a, 0xe1, 0x62, 0xd4, 0x95, 0x82, 0x61,
145 0x68, 0xa7, 0x94, 0xe8, 0x3b, 0xdf, 0x8d, 0x59}},
146 {__LINE__, /* Values from testcurl/test_digestauth2.c */
147 "test_user", "TestRealm", "test pass",
148 {0xe7, 0xa1, 0x9e, 0x27, 0xf6, 0x73, 0x88, 0xb2, 0xde, 0xa4, 0xe2, 0x66,
149 0xc5, 0x16, 0x37, 0x17, 0x4d, 0x29, 0xcc, 0xa3, 0xc1, 0xf5, 0xb2, 0x49,
150 0x20, 0xc1, 0x05, 0xc9, 0x20, 0x13, 0x3c, 0x3d}},
151 {__LINE__,
152 "Mufasa", "myhost@testrealm.com", "CircleOfLife",
153 {0x44, 0xbc, 0xd2, 0xb1, 0x1f, 0x6f, 0x7d, 0xd3, 0xae, 0xa6, 0x66, 0x8a,
154 0x24, 0x84, 0x4b, 0x87, 0x7d, 0xe1, 0x80, 0x24, 0x9a, 0x26, 0x6b, 0xe6,
155 0xdb, 0x7f, 0xe3, 0xc8, 0x7a, 0xf9, 0x75, 0x64}},
156 {__LINE__,
157 "Mufasa", "myhost@example.com", "Circle Of Life",
158 {0xd5, 0xf6, 0x25, 0x7c, 0x64, 0xe4, 0x01, 0xd2, 0x87, 0xd5, 0xaa, 0x19,
159 0xae, 0xf0, 0xa2, 0xa2, 0xce, 0x4e, 0x5d, 0xfc, 0x77, 0x70, 0x0b, 0x72,
160 0x90, 0x43, 0x96, 0xd2, 0x95, 0x6e, 0x83, 0x0a}},
161 {__LINE__,
162 "Mufasa", "http-auth@example.org", "Circle of Life",
163 {0xfb, 0x17, 0x4f, 0x5c, 0x3c, 0x78, 0x02, 0x72, 0x15, 0x17, 0xca, 0xe1,
164 0x3b, 0x98, 0xe2, 0xb8, 0xda, 0xe2, 0xe0, 0x11, 0x8c, 0xb7, 0x05, 0xd9,
165 0x4e, 0xe2, 0x99, 0x46, 0x31, 0x92, 0x04, 0xce}},
166 {__LINE__,
167 "J" "\xC3\xA4" "s" "\xC3\xB8" "n Doe" /* "Jäsøn Doe" */,
168 "api@example.org", "Secret, or not?",
169 {0x2d, 0x3d, 0x9f, 0x12, 0xc9, 0xf3, 0xd3, 0x00, 0x11, 0x25, 0x9d, 0xc5,
170 0xfe, 0xce, 0xe0, 0x05, 0xae, 0x24, 0xde, 0x40, 0xe3, 0xe1, 0xf6, 0x18,
171 0x06, 0xd0, 0x3e, 0x65, 0xf1, 0xe6, 0x02, 0x4f}}
172};
173
126 174
127/* 175/*
128 * Helper functions 176 * Helper functions
@@ -413,6 +461,156 @@ test_sha256_failure (void)
413} 461}
414 462
415 463
464static unsigned int
465check_sha512_256 (const struct data_sha512_256 *const data)
466{
467 static const enum MHD_DigestAuthAlgo3 algo3 =
468 MHD_DIGEST_AUTH_ALGO3_SHA512_256;
469 uint8_t hash_bin[MHD_SHA512_256_DIGEST_SIZE];
470 char hash_hex[MHD_SHA512_256_DIGEST_SIZE * 2 + 1];
471 char expected_hex[MHD_SHA512_256_DIGEST_SIZE * 2 + 1];
472 const char *func_name;
473 unsigned int failed = 0;
474
475 func_name = "MHD_digest_auth_calc_userdigest";
476 if (MHD_YES != MHD_digest_auth_calc_userdigest (algo3,
477 data->username,
478 data->realm,
479 data->password,
480 hash_bin, sizeof(hash_bin)))
481 {
482 failed++;
483 fprintf (stderr,
484 "FAILED: %s() has not returned MHD_YES.\n",
485 func_name);
486 }
487 else if (0 != memcmp (hash_bin, data->hash, sizeof(data->hash)))
488 {
489 failed++;
490 bin2hex (hash_bin, sizeof(hash_bin), hash_hex);
491 bin2hex (data->hash, sizeof(data->hash), expected_hex);
492 fprintf (stderr,
493 "FAILED: %s() produced wrong hash. "
494 "Calculated digest %s, expected digest %s.\n",
495 func_name,
496 hash_hex, expected_hex);
497 }
498
499 if (failed)
500 {
501 fprintf (stderr,
502 "The check failed for data located at line: %u.\n",
503 data->line_num);
504 fflush (stderr);
505 }
506 else if (verbose)
507 {
508 printf ("PASSED: check for data at line: %u.\n",
509 data->line_num);
510 }
511 return failed ? 1 : 0;
512}
513
514
515static unsigned int
516test_sha512_256 (void)
517{
518 unsigned int num_failed = 0;
519 size_t i;
520
521 for (i = 0; i < sizeof(sha512_256_tests) / sizeof(sha512_256_tests[0]); i++)
522 num_failed += check_sha512_256 (sha512_256_tests + i);
523 return num_failed;
524}
525
526
527static unsigned int
528test_sha512_256_failure (void)
529{
530 static const enum MHD_DigestAuthAlgo3 algo3 =
531 MHD_DIGEST_AUTH_ALGO3_SHA512_256;
532 static const enum MHD_FEATURE feature = MHD_FEATURE_DIGEST_AUTH_SHA512_256;
533 uint8_t hash_bin[MHD_SHA512_256_DIGEST_SIZE];
534 char hash_hex[MHD_SHA512_256_DIGEST_SIZE * 2 + 1];
535 const char *func_name;
536 unsigned int failed = 0;
537
538 func_name = "MHD_digest_auth_calc_userhash";
539 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
540 "u", "r",
541 hash_bin, sizeof(hash_bin) - 1))
542 {
543 failed++;
544 fprintf (stderr,
545 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
546 func_name, (unsigned) __LINE__);
547 }
548 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
549 "u", "r",
550 hash_bin, 0))
551 {
552 failed++;
553 fprintf (stderr,
554 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
555 func_name, (unsigned) __LINE__);
556 }
557 if (MHD_NO == MHD_is_feature_supported (feature))
558 {
559 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
560 "u", "r",
561 hash_bin, sizeof(hash_bin)))
562 {
563 failed++;
564 fprintf (stderr,
565 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
566 func_name, (unsigned) __LINE__);
567 }
568 }
569
570 func_name = "MHD_digest_auth_calc_userhash_hex";
571 if (MHD_NO !=
572 MHD_digest_auth_calc_userhash_hex (algo3,
573 "u", "r",
574 hash_hex, sizeof(hash_hex) - 1))
575 {
576 failed++;
577 fprintf (stderr,
578 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
579 func_name, (unsigned) __LINE__);
580 }
581 if (MHD_NO !=
582 MHD_digest_auth_calc_userhash_hex (algo3,
583 "u", "r",
584 hash_hex, 0))
585 {
586 failed++;
587 fprintf (stderr,
588 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
589 func_name, (unsigned) __LINE__);
590 }
591 if (MHD_NO == MHD_is_feature_supported (feature))
592 {
593 if (MHD_NO !=
594 MHD_digest_auth_calc_userhash_hex (algo3,
595 "u", "r",
596 hash_hex, sizeof(hash_hex)))
597 {
598 failed++;
599 fprintf (stderr,
600 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
601 func_name, (unsigned) __LINE__);
602 }
603 }
604
605 if (! failed && verbose)
606 {
607 printf ("PASSED: all checks with expected MHD_NO result near line: %u.\n",
608 (unsigned) __LINE__);
609 }
610 return failed ? 1 : 0;
611}
612
613
416int 614int
417main (int argc, char *argv[]) 615main (int argc, char *argv[])
418{ 616{
@@ -427,6 +625,9 @@ main (int argc, char *argv[])
427 if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA256)) 625 if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA256))
428 num_failed += test_sha256 (); 626 num_failed += test_sha256 ();
429 num_failed += test_sha256_failure (); 627 num_failed += test_sha256_failure ();
628 if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA512_256))
629 num_failed += test_sha512_256 ();
630 num_failed += test_sha512_256_failure ();
430 631
431 return num_failed ? 1 : 0; 632 return num_failed ? 1 : 0;
432} 633}