diff options
| author | Mattias Andrée <m@maandree.se> | 2026-05-21 18:33:57 +0200 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-05-21 18:33:57 +0200 |
| commit | 0d63bcee00754c81231b0292abe70420943ab545 (patch) | |
| tree | 9ad9f04e152b3efd559f9b2a867b3baf74167c71 /librecrypt_find_first_algorithm_.c | |
| parent | Add support for custom hash functions (diff) | |
| download | librecrypt-0d63bcee00754c81231b0292abe70420943ab545.tar.gz librecrypt-0d63bcee00754c81231b0292abe70420943ab545.tar.bz2 librecrypt-0d63bcee00754c81231b0292abe70420943ab545.tar.xz | |
Add some test code
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'librecrypt_find_first_algorithm_.c')
| -rw-r--r-- | librecrypt_find_first_algorithm_.c | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/librecrypt_find_first_algorithm_.c b/librecrypt_find_first_algorithm_.c index ff967c9..a339d93 100644 --- a/librecrypt_find_first_algorithm_.c +++ b/librecrypt_find_first_algorithm_.c @@ -15,7 +15,6 @@ librecrypt_find_first_algorithm_(const char *settings, size_t len, LIBRECRYPT_CO * priority as library-provided hash functions */ if (!ctx) goto library_provided; - /* TODO test */ for (i = 0u; i < ctx->nalgos; i++) { algo = &ctx->algos[i]; r = (*algo->is_algorithm)(settings, len); @@ -68,10 +67,50 @@ library_provided: } while (0) +static unsigned prio1; +static unsigned prio2; + +static unsigned +is_algorithm_x1(const char *settings, size_t len) +{ + if (len && settings[0u] == 'x') + return prio1; + return 0; +} + +static unsigned +is_algorithm_x2(const char *settings, size_t len) +{ + if (len && settings[0u] == 'x') + return prio2; + return 0; +} + +static unsigned +is_algorithm_y(const char *settings, size_t len) +{ + if (len && settings[0u] == 'y') + return prio2; + return 0; +} + +static unsigned +is_algorithm_argon2(const char *settings, size_t len) +{ + if (len > sizeof("$argon2") - 1u) + if (!strncmp(settings, "$argon2", sizeof("$argon2") - 1u)) + return prio1; + return 0; +} + + + int main(void) { const struct librecrypt_algorithm *algo; + struct librecrypt_algorithm custom[2]; + LIBRECRYPT_CONTEXT *ctx; SET_UP_ALARM(); INIT_RESOURCE_TEST(); @@ -84,6 +123,56 @@ main(void) IF__argon2id__SUPPORTED(CHECK("$argon2id$")); IF__argon2ds__SUPPORTED(CHECK("$argon2ds$")); + ctx = librecrypt_create_context(); + assert(ctx != NULL); + librecrypt_set_custom_algorithms(ctx, custom, ELEMSOF(custom)); + + prio1 = 1u; + prio2 = 2u; + custom[0].is_algorithm = &is_algorithm_x1; + custom[1].is_algorithm = &is_algorithm_y; + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]); + EXPECT(librecrypt_find_first_algorithm_("y", 1u, ctx) == &custom[1u]); + + prio1 = 1u; + prio2 = 1u; + custom[0].is_algorithm = &is_algorithm_x1; + custom[1].is_algorithm = &is_algorithm_y; + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]); + EXPECT(librecrypt_find_first_algorithm_("y", 1u, ctx) == &custom[1u]); + + prio1 = 1u; + prio2 = 1u; + custom[0].is_algorithm = &is_algorithm_x1; + custom[1].is_algorithm = &is_algorithm_x2; + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]); + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]); + + prio1 = 2u; + prio2 = 1u; + custom[0].is_algorithm = &is_algorithm_x1; + custom[1].is_algorithm = &is_algorithm_x2; + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]); + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]); + + prio1 = 1u; + prio2 = 2u; + custom[0].is_algorithm = &is_algorithm_x1; + custom[1].is_algorithm = &is_algorithm_x2; + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[1u]); + EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[1u]); + + librecrypt_set_custom_algorithms(ctx, custom, 1u); + custom[0].is_algorithm = &is_algorithm_argon2; + prio1 = UINT_MAX; + EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) == &custom[0u]); + prio1 = 1u; + EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) == &custom[0u]); + prio1 = 0u; + EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) != &custom[0u]); + + librecrypt_free_context(ctx); + STOP_RESOURCE_TEST(); return 0; } |
