From: Michael Tremer Date: Sat, 20 Aug 2022 15:25:19 +0000 (+0000) Subject: tests: Add tests to generate keys X-Git-Tag: 0.9.28~413 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf4c22f287d25789d9a37e70bda84cad0216a2c9;p=pakfire.git tests: Add tests to generate keys Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index fbfdeba48..6bd55e074 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -418,8 +418,13 @@ static int pakfire_key_write_to_keystore(struct pakfire_key* key) { PAKFIRE_EXPORT int pakfire_key_generate(struct pakfire_key** key, struct pakfire* pakfire, const char* algo, const char* userid) { - // Reset key - *key = NULL; + int r; + + // Check input + if (!algo || !userid) { + errno = EINVAL; + return 1; + } // Fetch GPGME context gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); @@ -439,6 +444,15 @@ PAKFIRE_EXPORT int pakfire_key_generate(struct pakfire_key** key, struct pakfire algo, 0, 0, NULL, flags); if (error != GPG_ERR_NO_ERROR) { + switch (error) { + case GPG_ERR_INV_VALUE: + errno = EINVAL; + break; + + default: + break; + } + ERROR(pakfire, "%s\n", gpgme_strerror(error)); return 1; } @@ -447,7 +461,7 @@ PAKFIRE_EXPORT int pakfire_key_generate(struct pakfire_key** key, struct pakfire gpgme_genkey_result_t result = gpgme_op_genkey_result(gpgctx); // Retrieve the key by its fingerprint - int r = pakfire_find_key(key, pakfire, result->fpr); + r = pakfire_find_key(key, pakfire, result->fpr); if (r) return r; diff --git a/tests/libpakfire/key.c b/tests/libpakfire/key.c index 145a8cca6..5678c9213 100644 --- a/tests/libpakfire/key.c +++ b/tests/libpakfire/key.c @@ -53,6 +53,61 @@ FAIL: return r; } +static int test_generate_key(const struct test* t, const char* algo, const char* userid) { + struct pakfire_key* key = NULL; + char* dump = NULL; + int r = EXIT_FAILURE; + + // Generate a new key + ASSERT_SUCCESS(pakfire_key_generate(&key, t->pakfire, algo, userid)); + + // Export the key + ASSERT_SUCCESS(pakfire_key_export(key, stdout, PAKFIRE_KEY_EXPORT_MODE_SECRET)); + + // Dump the key details + ASSERT(dump = pakfire_key_dump(key)); + printf("%s\n", dump); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (key) + pakfire_key_unref(key); + if (dump) + free(dump); + + return r; +} + +static int test_generate(const struct test* t) { + struct pakfire_key* key = NULL; + int r = EXIT_FAILURE; + + // Try to call pakfire_key_generate() with some invalid inputs + ASSERT_ERRNO(pakfire_key_generate(&key, t->pakfire, NULL, NULL), EINVAL); + ASSERT_ERRNO(pakfire_key_generate(&key, t->pakfire, "rsa2048", NULL), EINVAL); + ASSERT_ERRNO(pakfire_key_generate(&key, t->pakfire, NULL, "Invalid Test Key"), EINVAL); + + // Generate a new key using RSA2048 + ASSERT_SUCCESS(test_generate_key(t, "rsa2048", "Test Key #1")); + + // Generate a new key using ed25519 + ASSERT_SUCCESS(test_generate_key(t, "ed25519", "Test Key #2")); + + // Try to generate a key with the same ID + ASSERT_ERRNO(pakfire_key_generate(&key, t->pakfire, "rsa2048", "Test Key #1"), EINVAL); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (key) + pakfire_key_unref(key); + + return r; +} + static int test_import_export(const struct test* t) { // Try to delete the key just in case it // has been imported before @@ -99,6 +154,7 @@ static int test_import_export(const struct test* t) { int main(int argc, const char* argv[]) { testsuite_add_test(test_init); + testsuite_add_test(test_generate); testsuite_add_test(test_import_export); return testsuite_run(argc, argv);