[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/4] tests: Add ssh server setup functionality


Add the ability to generate a keypair (RSA, but tests that focus on
what occurs over a connection may well not care) and an ssh_bind
easily.

Signed-off-by: Alan Dunn <amdunn@xxxxxxxxx>
---
 tests/torture.c               |   54 +++++++++++++++++++++++++++++++++++++++++
 tests/torture.h               |   15 ++++++++++++
 tests/unittests/torture_pki.c |    6 +----
 3 files changed, 70 insertions(+), 5 deletions(-)

diff --git a/tests/torture.c b/tests/torture.c
index fb4707a..ce26050 100644
--- a/tests/torture.c
+++ b/tests/torture.c
@@ -235,6 +235,60 @@ failed:
     return NULL;
 }
 
+int torture_setup_rsa_key() {
+    int rc;
+
+    unlink(LIBSSH_RSA_TESTKEY);
+    unlink(LIBSSH_RSA_TESTKEY ".pub");
+
+    rc = system("ssh-keygen -t rsa -q -N \"\" -f " LIBSSH_RSA_TESTKEY);
+    return rc;
+}
+
+#ifdef WITH_SERVER
+
+ssh_bind torture_ssh_bind(const char *addr,
+                          const unsigned int port,
+                          const char *private_key_file) {
+    int rc;
+    ssh_bind sshbind = NULL;
+
+    sshbind = ssh_bind_new();
+    if (sshbind == NULL) {
+        goto out;
+    }
+
+    rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, addr);
+    if (rc != 0) {
+        goto out_free;
+    }
+
+    rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port);
+    if (rc != 0) {
+        goto out_free;
+    }
+
+    rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY,
+                              private_key_file);
+    if (rc != 0) {
+        goto out_free;
+    }
+
+    rc = ssh_bind_listen(sshbind);
+    if (rc != SSH_OK) {
+        goto out_free;
+    }
+
+    goto out;
+ out_free:
+    ssh_bind_free(sshbind);
+    sshbind = NULL;
+ out:
+    return sshbind;
+}
+
+#endif
+
 #ifdef WITH_SFTP
 
 struct torture_sftp *torture_sftp_session(ssh_session session) {
diff --git a/tests/torture.h b/tests/torture.h
index fb7ac5a..d6e5e47 100644
--- a/tests/torture.h
+++ b/tests/torture.h
@@ -35,6 +35,7 @@
 #include <setjmp.h>
 
 #include "libssh/priv.h"
+#include "libssh/server.h"
 #include "libssh/sftp.h"
 
 #include <cmocka.h>
@@ -66,6 +67,20 @@ ssh_session torture_ssh_session(const char *host,
                                 const char *user,
                                 const char *password);
 
+#define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa"
+
+/*
+ * Set up an RSA key pair with private key at LIBSSH_RSA_TESTKEY and
+ * public key at the same filename extended with ".pub".
+ *
+ * Returns 0 on success.
+ */
+int torture_setup_rsa_key(void);
+
+ssh_bind torture_ssh_bind(const char *addr,
+                          const unsigned int port,
+                          const char *private_key_file);
+
 struct torture_sftp *torture_sftp_session(ssh_session session);
 void torture_sftp_close(struct torture_sftp *t);
 
diff --git a/tests/unittests/torture_pki.c b/tests/unittests/torture_pki.c
index 7eab95d..3672055 100644
--- a/tests/unittests/torture_pki.c
+++ b/tests/unittests/torture_pki.c
@@ -5,7 +5,6 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa"
 #define LIBSSH_DSA_TESTKEY "libssh_testkey.id_dsa"
 #define LIBSSH_ECDSA_TESTKEY "libssh_testkey.id_ecdsa"
 #define LIBSSH_PASSPHRASE "libssh-rocks"
@@ -16,10 +15,7 @@ static void setup_rsa_key(void **state) {
 
     (void) state; /* unused */
 
-    unlink(LIBSSH_RSA_TESTKEY);
-    unlink(LIBSSH_RSA_TESTKEY ".pub");
-
-    rc = system("ssh-keygen -t rsa -q -N \"\" -f " LIBSSH_RSA_TESTKEY);
+    rc = torture_setup_rsa_key();
     assert_true(rc == 0);
 }
 
-- 
1.7.9.5


References:
[PATCH 0/4] Fix incorrectly passed X11 request screen numberAlan Dunn <amdunn@xxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org