common_test.c (1823B)
- // SPDX-FileCopyrightText: 2022 Haelwenn (lanodan) Monnier <contact+skeud@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #include "common.h"
- #include <stdio.h>
- #include <string.h> // strlen
- const char *argv0 = "common_test";
- int counter = 0;
- int err = 0;
- static void
- check_int(const char *name, int got, int exp)
- {
- int id = ++counter;
- if(got == exp)
- {
- printf("ok %d - %s ==> %d\n", id, name, exp);
- return;
- }
- err = 1;
- printf("not ok %d - %s ==> %d\n", id, name, exp);
- printf("# Got: %d\n", got);
- }
- int
- main(void)
- {
- int plan = 12;
- printf("1..%d\n", plan);
- #define CHECK_INT(func, exp) check_int(#func, func, exp)
- #define test_hash_match(exp, s1, s2) CHECK_INT(hash_match(s1, strlen0(s1), s2, strlen0(s2)), exp)
- test_hash_match(1, "foo", "foo");
- test_hash_match(0, "foo", "bar");
- test_hash_match(0, "foo", "fooo");
- test_hash_match(0, "fooo", "foo");
- #define test_crypt_check(exp, s1, s2) CHECK_INT(skeud_crypt_check(s1, s2), exp)
- 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 *sha256_hash =
- "$6$QUEEGuX9dkGlNkTP$IJwcvb6tpm63hoOfm9QJjEgjte/OpcQS3S43zDN95G3diJ5Xc/OlhhbCkUyV/"
- "A0ARhgQj2D/4m/DWhwvvs3A91";
- test_crypt_check(1, sha256_hash, "foobar");
- if(counter != plan)
- {
- fprintf(stderr, "common_test: error: Ran %d tests, expected %d\n", counter, plan);
- return 1;
- }
- return err;
- }