aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2011-08-16 23:03:12 +0200
committerAndreas Schneider <asn@cryptomilk.org>2011-08-16 23:06:13 +0200
commit2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4 (patch)
tree363d4a5ce5b06828e2f3c659fd48005c594256af /tests/unittests
parentc39181437b10fb49cd236f625c599c01af41f1b2 (diff)
downloadlibssh-2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4.tar.gz
libssh-2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4.tar.xz
libssh-2615f8bc949ff87eba3051ad9eb0f1e41cd8afb4.zip
tests: Added test for pubkey from privkey.
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/torture_pki.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/unittests/torture_pki.c b/tests/unittests/torture_pki.c
index 643fc120..3d6da3da 100644
--- a/tests/unittests/torture_pki.c
+++ b/tests/unittests/torture_pki.c
@@ -93,6 +93,26 @@ static char *read_file(const char *filename) {
return key;
}
+static int torture_read_one_line(const char *filename, char *buffer, size_t len) {
+ FILE *fp;
+ size_t rc;
+
+ fp = fopen(filename, "r");
+ if (fp == NULL) {
+ return -1;
+ }
+
+ rc = fread(buffer, len, 1, fp);
+ if (rc != 0 || ferror(fp)) {
+ fclose(fp);
+ return -1;
+ }
+
+ fclose(fp);
+
+ return 0;
+}
+
static void torture_pki_import_privkey_base64_RSA(void **state) {
ssh_session session = *state;
int rc;
@@ -333,6 +353,45 @@ static void torture_pki_publickey_rsa_base64(void **state)
ssh_key_free(key);
}
+static void torture_generate_pubkey_from_privkey(void **state) {
+ char pubkey_original[4096] = {0};
+ char pubkey_generated[4096] = {0};
+ ssh_key privkey;
+ ssh_key pubkey;
+ int rc;
+
+ rc = torture_read_one_line(LIBSSH_DSA_TESTKEY ".pub",
+ pubkey_original,
+ sizeof(pubkey_original));
+ assert_true(rc == 0);
+
+ /* remove the public key, generate it from the private key and write it. */
+ unlink(LIBSSH_RSA_TESTKEY ".pub");
+
+ rc = ssh_pki_import_privkey_file(LIBSSH_DSA_TESTKEY,
+ NULL,
+ NULL,
+ NULL,
+ &privkey);
+ assert_true(rc == 0);
+
+ pubkey = ssh_pki_publickey_from_privatekey(privkey);
+ assert_true(pubkey != NULL);
+
+ rc = ssh_pki_export_publickey_file(pubkey, LIBSSH_DSA_TESTKEY ".pub");
+ assert_true(rc == 0);
+
+ rc = torture_read_one_line(LIBSSH_DSA_TESTKEY ".pub",
+ pubkey_generated,
+ sizeof(pubkey_generated));
+ assert_true(rc == 0);
+
+ assert_string_equal(pubkey_original, pubkey_generated);
+
+ ssh_key_free(privkey);
+ ssh_key_free(pubkey);
+}
+
int torture_run_tests(void) {
int rc;
const UnitTest tests[] = {
@@ -367,6 +426,9 @@ int torture_run_tests(void) {
setup_rsa_key,
teardown),
+ unit_test_setup_teardown(torture_generate_pubkey_from_privkey,
+ setup_dsa_key,
+ teardown),
};