logo

skeud

Simple and portable utilities to deal with user accounts (su, login)git clone https://anongit.hacktivis.me/git/skeud.git/

common_test.c (1967B)


  1. // SPDX-FileCopyrightText: 2022 Haelwenn (lanodan) Monnier <contact+skeud@hacktivis.me>
  2. // SPDX-License-Identifier: MPL-2.0
  3. #include "common.h"
  4. #include <stdio.h>
  5. int counter = 0;
  6. int err = 0;
  7. static void
  8. test_hash_match(int ret, const char *s1, const char *s2)
  9. {
  10. int id = ++counter;
  11. int got = hash_match(s1, s2);
  12. if(got == ret)
  13. {
  14. printf("ok %d - hash_match(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
  15. return;
  16. }
  17. err = 1;
  18. printf("not ok %d - hash_match(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
  19. printf("# Got: %d\n", got);
  20. }
  21. static void
  22. test_crypt_check(int ret, const char *s1, const char *s2)
  23. {
  24. int id = ++counter;
  25. int got = skeud_crypt_check(s1, s2);
  26. if(got == ret)
  27. {
  28. printf("ok %d - skeud_crypt_check(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
  29. return;
  30. }
  31. err = 1;
  32. printf("not ok %d - skeud_crypt_check(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
  33. printf("# Got: %d\n", got);
  34. }
  35. int
  36. main(void)
  37. {
  38. int plan = 12;
  39. printf("1..%d\n", plan);
  40. test_hash_match(1, "foo", "foo");
  41. test_hash_match(0, "foo", "bar");
  42. test_hash_match(0, "foo", "fooo");
  43. test_hash_match(0, "fooo", "foo");
  44. test_crypt_check(0, NULL, "foobar");
  45. test_crypt_check(0, "", "foobar");
  46. test_crypt_check(0, "x", "foobar");
  47. test_crypt_check(0, "foobar", "foobar");
  48. // DES: openssl passwd -noverify
  49. test_crypt_check(1, "FmuFhHU.nJlkg", "foobar");
  50. // MD5: openssl passwd -1 -noverify
  51. test_crypt_check(1, "$1$L0.ptviH$oU/aJvI7BwUtWXzeJ3nGU.", "foobar");
  52. // SHA256: openssl passwd -5 -noverify
  53. test_crypt_check(1, "$5$8VryLuwDTzZ8MSZX$2UIaWB5LcMlhXv7UQIBcFeq8/Dr6PswXZP/SJ09L01B", "foobar");
  54. // SHA512: openssl passwd -6 -noverify
  55. const char *hash =
  56. "$6$QUEEGuX9dkGlNkTP$IJwcvb6tpm63hoOfm9QJjEgjte/OpcQS3S43zDN95G3diJ5Xc/OlhhbCkUyV/"
  57. "A0ARhgQj2D/4m/DWhwvvs3A91";
  58. test_crypt_check(1, hash, "foobar");
  59. if(counter != plan)
  60. {
  61. fprintf(stderr, "common_test: error: Ran %d tests, expected %d\n", counter, plan);
  62. return 1;
  63. }
  64. return err;
  65. }