aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_pki.c
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2011-08-20 12:59:19 +0200
committerAndreas Schneider <asn@cryptomilk.org>2011-08-20 12:59:19 +0200
commit79029854f638839be3cfcf8f2194e69ea5e96559 (patch)
tree761fccaf701399c5021016bdff978bca9f8f74e4 /tests/unittests/torture_pki.c
parent743a00dad4098f473b23f3b04d9fe6fbf520c7c8 (diff)
downloadlibssh-79029854f638839be3cfcf8f2194e69ea5e96559.tar.gz
libssh-79029854f638839be3cfcf8f2194e69ea5e96559.tar.xz
libssh-79029854f638839be3cfcf8f2194e69ea5e96559.zip
tests: Added more pki tests.
Diffstat (limited to 'tests/unittests/torture_pki.c')
-rw-r--r--tests/unittests/torture_pki.c140
1 files changed, 137 insertions, 3 deletions
diff --git a/tests/unittests/torture_pki.c b/tests/unittests/torture_pki.c
index 6684cf3d..ef003f1c 100644
--- a/tests/unittests/torture_pki.c
+++ b/tests/unittests/torture_pki.c
@@ -364,7 +364,7 @@ static void torture_pki_publickey_rsa_base64(void **state)
ssh_key_free(key);
}
-static void torture_generate_pubkey_from_privkey(void **state) {
+static void torture_generate_pubkey_from_privkey_rsa(void **state) {
char pubkey_original[4096] = {0};
char pubkey_generated[4096] = {0};
ssh_key privkey;
@@ -373,7 +373,7 @@ static void torture_generate_pubkey_from_privkey(void **state) {
(void) state; /* unused */
- rc = torture_read_one_line(LIBSSH_DSA_TESTKEY ".pub",
+ rc = torture_read_one_line(LIBSSH_RSA_TESTKEY ".pub",
pubkey_original,
sizeof(pubkey_original));
assert_true(rc == 0);
@@ -381,6 +381,47 @@ static void torture_generate_pubkey_from_privkey(void **state) {
/* 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_RSA_TESTKEY,
+ NULL,
+ NULL,
+ NULL,
+ &privkey);
+ assert_true(rc == 0);
+
+ pubkey = ssh_pki_publickey_from_privatekey(privkey);
+ assert_true(pubkey != NULL);
+
+ rc = ssh_pki_export_pubkey_file(pubkey, LIBSSH_RSA_TESTKEY ".pub");
+ assert_true(rc == 0);
+
+ rc = torture_read_one_line(LIBSSH_RSA_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);
+}
+
+static void torture_generate_pubkey_from_privkey_dsa(void **state) {
+ char pubkey_original[4096] = {0};
+ char pubkey_generated[4096] = {0};
+ ssh_key privkey;
+ ssh_key pubkey;
+ int rc;
+
+ (void) state; /* unused */
+
+ 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_DSA_TESTKEY ".pub");
+
rc = ssh_pki_import_privkey_file(LIBSSH_DSA_TESTKEY,
NULL,
NULL,
@@ -405,6 +446,90 @@ static void torture_generate_pubkey_from_privkey(void **state) {
ssh_key_free(pubkey);
}
+static void torture_pki_duplicate_key_rsa(void **state)
+{
+ int rc;
+ char *b64_key;
+ char *b64_key_gen;
+ ssh_key pubkey;
+ ssh_key privkey;
+ ssh_key privkey_dup;
+
+ (void) state;
+
+ rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY ".pub", &pubkey);
+ assert_true(rc == 0);
+
+ rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key);
+ assert_true(rc == 0);
+ ssh_key_free(pubkey);
+
+ rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY,
+ NULL,
+ NULL,
+ NULL,
+ &privkey);
+ assert_true(rc == 0);
+
+ privkey_dup = ssh_key_dup(privkey);
+ assert_true(privkey_dup != NULL);
+
+ pubkey = ssh_pki_publickey_from_privatekey(privkey_dup);
+ assert_true(pubkey != NULL);
+
+ rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key_gen);
+ assert_true(rc == 0);
+
+ assert_string_equal(b64_key, b64_key_gen);
+
+ ssh_key_free(pubkey);
+ ssh_key_free(privkey);
+ ssh_string_free_char(b64_key);
+ ssh_string_free_char(b64_key_gen);
+}
+
+static void torture_pki_duplicate_key_dsa(void **state)
+{
+ int rc;
+ char *b64_key;
+ char *b64_key_gen;
+ ssh_key pubkey;
+ ssh_key privkey;
+ ssh_key privkey_dup;
+
+ (void) state;
+
+ rc = ssh_pki_import_pubkey_file(LIBSSH_DSA_TESTKEY ".pub", &pubkey);
+ assert_true(rc == 0);
+
+ rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key);
+ assert_true(rc == 0);
+ ssh_key_free(pubkey);
+
+ rc = ssh_pki_import_privkey_file(LIBSSH_DSA_TESTKEY,
+ NULL,
+ NULL,
+ NULL,
+ &privkey);
+ assert_true(rc == 0);
+
+ privkey_dup = ssh_key_dup(privkey);
+ assert_true(privkey_dup != NULL);
+
+ pubkey = ssh_pki_publickey_from_privatekey(privkey_dup);
+ assert_true(pubkey != NULL);
+
+ rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key_gen);
+ assert_true(rc == 0);
+
+ assert_string_equal(b64_key, b64_key_gen);
+
+ ssh_key_free(pubkey);
+ ssh_key_free(privkey);
+ ssh_string_free_char(b64_key);
+ ssh_string_free_char(b64_key_gen);
+}
+
int torture_run_tests(void) {
int rc;
const UnitTest tests[] = {
@@ -439,10 +564,19 @@ int torture_run_tests(void) {
setup_rsa_key,
teardown),
- unit_test_setup_teardown(torture_generate_pubkey_from_privkey,
+ unit_test_setup_teardown(torture_generate_pubkey_from_privkey_dsa,
setup_dsa_key,
teardown),
+ unit_test_setup_teardown(torture_generate_pubkey_from_privkey_rsa,
+ setup_rsa_key,
+ teardown),
+ unit_test_setup_teardown(torture_pki_duplicate_key_rsa,
+ setup_rsa_key,
+ teardown),
+ unit_test_setup_teardown(torture_pki_duplicate_key_dsa,
+ setup_dsa_key,
+ teardown),
};