forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: add tool for GPG test key generation
This tool can be utilized in unit tests whenever a valid GPG key without a passphrase is being required. See ElektraInitiative#1981 for full discussion.
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
find_package (LibGpgme QUIET) | ||
if (NOT LIBGPGME_FOUND) | ||
remove_tool (elektra-gpg-testkey "libgpgme not found") | ||
else () | ||
include_directories (${LIBGPGME_INCLUDE_DIRS}) | ||
add_executable (elektra-gpg-testkey elektra-gpg-testkey.c) | ||
target_link_libraries (elektra-gpg-testkey ${LIBGPGME_LIBRARIES}) | ||
install (TARGETS elektra-gpg-testkey DESTINATION bin) | ||
endif () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @file | ||
* | ||
* @brief tool for creating and finding the GPG test key to be used in Elektra's unit tests. | ||
* | ||
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org) | ||
* | ||
*/ | ||
|
||
#include <gpgme.h> | ||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
gpgme_error_t err; | ||
gpgme_ctx_t ctx; | ||
gpgme_key_t key; | ||
gpgme_genkey_result_t res; | ||
|
||
gpgme_check_version (NULL); | ||
err = gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP); | ||
if (err) | ||
{ | ||
fprintf (stderr, "gpgme version check failed: %s\n",gpgme_strerror (err)); | ||
return -1; | ||
} | ||
|
||
// setup gpgme context | ||
err = gpgme_new (&ctx); | ||
if (err) | ||
{ | ||
fprintf (stderr, "gpgme error: %s\n", gpgme_strerror (err)); | ||
return -1; | ||
} | ||
|
||
// look for the elektra key | ||
err = gpgme_op_keylist_start(ctx,"elektra",1 /* secret keys only! */); | ||
if(err) | ||
{ | ||
fprintf (stderr, "gpgme error: %s\n", gpgme_strerror (err)); | ||
goto cleanup; | ||
} | ||
|
||
err = gpgme_op_keylist_next(ctx,&key); | ||
if(err && gpg_err_code(err) != GPG_ERR_EOF) | ||
{ | ||
fprintf (stderr, "gpgme error: %s\n", gpgme_strerror (err)); | ||
goto cleanup; | ||
} | ||
|
||
if(err && gpg_err_code(err) == GPG_ERR_EOF) | ||
{ | ||
// create the elektra demo key | ||
err = gpgme_op_createkey(ctx, "elektra testkey", "default", 0, 0, NULL, GPGME_CREATE_SIGN | GPGME_CREATE_ENCR | GPGME_CREATE_NOEXPIRE | GPGME_CREATE_NOPASSWD ); | ||
res = gpgme_op_genkey_result(ctx); | ||
fprintf(stdout, "%s", res->fpr); | ||
} | ||
else | ||
{ | ||
// display the key ID | ||
fprintf(stdout, "%s", key->subkeys->fpr); | ||
gpgme_key_release(key); | ||
gpgme_op_keylist_end(ctx); | ||
} | ||
|
||
gpgme_release(ctx); | ||
return 0; | ||
|
||
cleanup: | ||
gpgme_release(ctx); | ||
return -1; | ||
} |