common_test.c (1967B)
- // SPDX-FileCopyrightText: 2022 Haelwenn (lanodan) Monnier <contact+skeud@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #include "common.h"
- #include <stdio.h>
- int counter = 0;
- int err = 0;
- static void
- test_hash_match(int ret, const char *s1, const char *s2)
- {
- int id = ++counter;
- int got = hash_match(s1, s2);
- if(got == ret)
- {
- printf("ok %d - hash_match(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
- return;
- }
- err = 1;
- printf("not ok %d - hash_match(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
- printf("# Got: %d\n", got);
- }
- static void
- test_crypt_check(int ret, const char *s1, const char *s2)
- {
- int id = ++counter;
- int got = skeud_crypt_check(s1, s2);
- if(got == ret)
- {
- printf("ok %d - skeud_crypt_check(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
- return;
- }
- err = 1;
- printf("not ok %d - skeud_crypt_check(\"%s\", \"%s\") == %d\n", id, s1, s2, ret);
- printf("# Got: %d\n", got);
- }
- int
- main(void)
- {
- int plan = 12;
- printf("1..%d\n", plan);
- test_hash_match(1, "foo", "foo");
- test_hash_match(0, "foo", "bar");
- test_hash_match(0, "foo", "fooo");
- test_hash_match(0, "fooo", "foo");
- test_crypt_check(0, NULL, "foobar");
- test_crypt_check(0, "", "foobar");
- test_crypt_check(0, "x", "foobar");
- test_crypt_check(0, "foobar", "foobar");
- // DES: openssl passwd -noverify
- test_crypt_check(1, "FmuFhHU.nJlkg", "foobar");
- // MD5: openssl passwd -1 -noverify
- test_crypt_check(1, "$1$L0.ptviH$oU/aJvI7BwUtWXzeJ3nGU.", "foobar");
- // SHA256: openssl passwd -5 -noverify
- test_crypt_check(1, "$5$8VryLuwDTzZ8MSZX$2UIaWB5LcMlhXv7UQIBcFeq8/Dr6PswXZP/SJ09L01B", "foobar");
- // SHA512: openssl passwd -6 -noverify
- const char *hash =
- "$6$QUEEGuX9dkGlNkTP$IJwcvb6tpm63hoOfm9QJjEgjte/OpcQS3S43zDN95G3diJ5Xc/OlhhbCkUyV/"
- "A0ARhgQj2D/4m/DWhwvvs3A91";
- test_crypt_check(1, hash, "foobar");
- if(counter != plan)
- {
- fprintf(stderr, "common_test: error: Ran %d tests, expected %d\n", counter, plan);
- return 1;
- }
- return err;
- }