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);
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;
}
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;
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
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);