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 (1823B)


  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. #include <string.h> // strlen
  6. const char *argv0 = "common_test";
  7. int counter = 0;
  8. int err = 0;
  9. static void
  10. check_int(const char *name, int got, int exp)
  11. {
  12. int id = ++counter;
  13. if(got == exp)
  14. {
  15. printf("ok %d - %s ==> %d\n", id, name, exp);
  16. return;
  17. }
  18. err = 1;
  19. printf("not ok %d - %s ==> %d\n", id, name, exp);
  20. printf("# Got: %d\n", got);
  21. }
  22. int
  23. main(void)
  24. {
  25. int plan = 12;
  26. printf("1..%d\n", plan);
  27. #define CHECK_INT(func, exp) check_int(#func, func, exp)
  28. #define test_hash_match(exp, s1, s2) CHECK_INT(hash_match(s1, strlen0(s1), s2, strlen0(s2)), exp)
  29. test_hash_match(1, "foo", "foo");
  30. test_hash_match(0, "foo", "bar");
  31. test_hash_match(0, "foo", "fooo");
  32. test_hash_match(0, "fooo", "foo");
  33. #define test_crypt_check(exp, s1, s2) CHECK_INT(skeud_crypt_check(s1, s2), exp)
  34. test_crypt_check(0, NULL, "foobar");
  35. test_crypt_check(0, "", "foobar");
  36. test_crypt_check(0, "x", "foobar");
  37. test_crypt_check(0, "foobar", "foobar");
  38. // DES: openssl passwd -noverify
  39. test_crypt_check(1, "FmuFhHU.nJlkg", "foobar");
  40. // MD5: openssl passwd -1 -noverify
  41. test_crypt_check(1, "$1$L0.ptviH$oU/aJvI7BwUtWXzeJ3nGU.", "foobar");
  42. // SHA256: openssl passwd -5 -noverify
  43. test_crypt_check(1, "$5$8VryLuwDTzZ8MSZX$2UIaWB5LcMlhXv7UQIBcFeq8/Dr6PswXZP/SJ09L01B", "foobar");
  44. // SHA512: openssl passwd -6 -noverify
  45. const char *sha256_hash =
  46. "$6$QUEEGuX9dkGlNkTP$IJwcvb6tpm63hoOfm9QJjEgjte/OpcQS3S43zDN95G3diJ5Xc/OlhhbCkUyV/"
  47. "A0ARhgQj2D/4m/DWhwvvs3A91";
  48. test_crypt_check(1, sha256_hash, "foobar");
  49. if(counter != plan)
  50. {
  51. fprintf(stderr, "common_test: error: Ran %d tests, expected %d\n", counter, plan);
  52. return 1;
  53. }
  54. return err;
  55. }