sha256c-test.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <crypt.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. static const struct
  5. {
  6. const char *salt;
  7. const char *input;
  8. const char *expected;
  9. } tests[] =
  10. {
  11. { "$5$saltstring", "Hello world!",
  12. "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" },
  13. { "$5$rounds=10000$saltstringsaltstring", "Hello world!",
  14. "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2."
  15. "opqey6IcA" },
  16. { "$5$rounds=5000$toolongsaltstring", "This is just a test",
  17. "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8"
  18. "mGRcvxa5" },
  19. { "$5$rounds=1400$anotherlongsaltstring",
  20. "a very much longer text to encrypt. This one even stretches over more"
  21. "than one line.",
  22. "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12"
  23. "oP84Bnq1" },
  24. { "$5$rounds=77777$short",
  25. "we have a short salt string but not a short password",
  26. "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/" },
  27. { "$5$rounds=123456$asaltof16chars..", "a short string",
  28. "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/"
  29. "cZKmF/wJvD" },
  30. { "$5$rounds=10$roundstoolow", "the minimum number is still observed",
  31. "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL97"
  32. "2bIC" },
  33. };
  34. #define ntests (sizeof (tests) / sizeof (tests[0]))
  35. static int
  36. do_test (void)
  37. {
  38. #if __UCLIBC_HAS_SHA256_CRYPT_IMPL__
  39. int result = 0;
  40. int i;
  41. for (i = 0; i < ntests; ++i)
  42. {
  43. char *cp = crypt (tests[i].input, tests[i].salt);
  44. if (strcmp (cp, tests[i].expected) != 0)
  45. {
  46. printf ("test %d: expected \"%s\", got \"%s\"\n",
  47. i, tests[i].expected, cp);
  48. result = 1;
  49. }
  50. }
  51. return result;
  52. #else
  53. return 23;
  54. #endif
  55. }
  56. #define TIMEOUT 6
  57. #define TEST_FUNCTION do_test ()
  58. #include "../test-skeleton.c"