]> git.ipfire.org Git - pakfire.git/commitdiff
tests: Add tests to generate keys
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 20 Aug 2022 15:25:19 +0000 (15:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 20 Aug 2022 15:25:19 +0000 (15:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/key.c
tests/libpakfire/key.c

index fbfdeba482bb2c55c154b19fc6e5f5b216f51523..6bd55e0748c9b13ec8a5228ef90cdb78dcf691ad 100644 (file)
@@ -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;
 
index 145a8cca6fb506ee788cd0a6cc0233363a113c09..5678c92134fa33258bce1b49e6c8e1ca093d447d 100644 (file)
@@ -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);