From 9eafd48355191758ee313ad709e5e6789ae39ea2 Mon Sep 17 00:00:00 2001 From: Conor McDermottroe Date: Sat, 9 Apr 2016 13:51:09 +0100 Subject: [PATCH] Reduce duplication in test session handling. Reduce repetition and be better about closing connections when exceptions are thrown. Fixes #9 --- test/clj_libssh2/test_agent.clj | 29 +++++---------- test/clj_libssh2/test_authentication.clj | 32 +++++++---------- test/clj_libssh2/test_known_hosts.clj | 45 +++++------------------- test/clj_libssh2/test_utils.clj | 25 +++++++++++-- 4 files changed, 52 insertions(+), 79 deletions(-) diff --git a/test/clj_libssh2/test_agent.clj b/test/clj_libssh2/test_agent.clj index 6eb93dc..99873ab 100644 --- a/test/clj_libssh2/test_agent.clj +++ b/test/clj_libssh2/test_agent.clj @@ -2,46 +2,33 @@ (:require [clojure.test :refer :all] [clj-libssh2.libssh2 :as libssh2] [clj-libssh2.libssh2.agent :as libssh2-agent] - [clj-libssh2.session :as session] [clj-libssh2.test-utils :as test])) (test/fixtures) -(defn agent-session - [] - (session/open test/ssh-host test/ssh-port {:username (test/ssh-user)} {})) - -(defn open-and-close - [] - (is (= 0 (count @session/sessions))) - (let [session (agent-session)] - (is (= 1 (count @session/sessions))) - (session/close session) - (is (= 0 (count @session/sessions))))) - (deftest agent-authentication-works (testing "A good session works" - (open-and-close)) + (test/auth)) (testing "If no identities match, we get an exception" (with-redefs [libssh2-agent/userauth (constantly libssh2/ERROR_PUBLICKEY_UNVERIFIED)] - (is (thrown? Exception (open-and-close))))) + (is (thrown? Exception (test/auth))))) (testing "If there are no identities, we get an exception" (with-redefs [libssh2-agent/get-identity (constantly 1)] - (is (thrown? Exception (open-and-close)))))) + (is (thrown? Exception (test/auth)))))) (deftest agent-authentication-throws-but-doesn't-crash (testing "when libssh2_agent_init fails" (with-redefs [libssh2-agent/init (constantly nil)] - (is (thrown? Exception (open-and-close))))) + (is (thrown? Exception (test/auth))))) (testing "when libssh2_agent_connect fails" (with-redefs [libssh2-agent/connect (constantly libssh2/ERROR_AGENT_PROTOCOL)] - (is (thrown? Exception (open-and-close))))) + (is (thrown? Exception (test/auth))))) (testing "when libssh2_agent_list_identities fails" (with-redefs [libssh2-agent/list-identities (constantly libssh2/ERROR_BAD_USE)] - (is (thrown? Exception (open-and-close))))) + (is (thrown? Exception (test/auth))))) (testing "when libssh2_agent_get_identity fails" (with-redefs [libssh2-agent/get-identity (constantly libssh2/ERROR_AGENT_PROTOCOL)] - (is (thrown? Exception (open-and-close))))) + (is (thrown? Exception (test/auth))))) (testing "when libssh2_agent_disconnect fails" (with-redefs [libssh2-agent/disconnect (constantly libssh2/ERROR_SOCKET_DISCONNECT)] - (is (thrown? Exception (open-and-close)))))) + (is (thrown? Exception (test/auth)))))) diff --git a/test/clj_libssh2/test_authentication.clj b/test/clj_libssh2/test_authentication.clj index 33df888..cfb1024 100644 --- a/test/clj_libssh2/test_authentication.clj +++ b/test/clj_libssh2/test_authentication.clj @@ -2,23 +2,14 @@ (:require [clojure.test :refer :all] [clj-libssh2.libssh2 :as libssh2] [clj-libssh2.libssh2.userauth :as libssh2-userauth] - [clj-libssh2.session :as session] [clj-libssh2.test-utils :as test]) (:use clj-libssh2.authentication)) (test/fixtures) -(defn auth - [creds] - (is (= 0 (count @session/sessions))) - (let [session (session/open test/ssh-host test/ssh-port creds {})] - (is (= 1 (count @session/sessions))) - (session/close session)) - (is (= 0 (count @session/sessions)))) - ; This is more fully tested in clj-libssh2.test-agent (deftest agent-authentication-works - (is (auth (->AgentCredentials (test/ssh-user))))) + (is (test/auth {:credentials (->AgentCredentials (test/ssh-user))}))) (deftest key-authentication-works (let [user (test/ssh-user) @@ -41,22 +32,22 @@ bad-pubkey (->KeyCredentials user "" "/bad" (pubkey ""))] (testing "A passphrase-less key works" (is (valid? no-passphrase)) - (is (auth no-passphrase))) + (is (test/auth {:credentials no-passphrase}))) (testing "A key with a passphrase works" (is (valid? with-passphrase)) - (is (auth with-passphrase))) + (is (test/auth {:credentials with-passphrase}))) (testing "A valid but unauthorized key does not work" (is (valid? unauthorized)) - (is (thrown? Exception (auth unauthorized)))) + (is (thrown? Exception (test/auth {:credentials unauthorized})))) (testing "It fails if the private key file doesn't exist" (is (valid? bad-privkey)) - (is (thrown? Exception (auth bad-privkey)))) + (is (thrown? Exception (test/auth {:credentials bad-privkey})))) (testing "It fails if the public key file doesn't exist" (is (valid? bad-pubkey)) - (is (thrown? Exception (auth bad-pubkey)))) + (is (thrown? Exception (test/auth {:credentials bad-pubkey})))) (testing "It fails if the passphrase is incorrect" (is (valid? with-wrong-passphrase)) - (is (thrown? Exception (auth with-wrong-passphrase)))))) + (is (thrown? Exception (test/auth {:credentials with-wrong-passphrase})))))) ; We can't test this all the way without knowing a password on the local ; machine. We can test with libssh2_userauth_password stubbed and some error @@ -68,15 +59,16 @@ (->PasswordCredentials (test/ssh-user) password))] (testing "A successful authentication returns true" (with-redefs [libssh2-userauth/password (constantly 0)] - (is (auth (password-creds "doesn't matter"))))) + (is (test/auth {:credentials (password-creds "doesn't matter")})))) (testing "It fails to authenticate with the wrong password" - (is (thrown? Exception (auth (password-creds "the wrong password"))))) + (is (thrown? Exception (test/auth {:credentials (password-creds "the wrong password")})))) (testing "A library error does not result in a crash" (with-redefs [libssh2-userauth/password (constantly libssh2/ERROR_ALLOC)] - (is (thrown? Exception (auth (password-creds "doesn't matter")))))))) + (is (thrown? Exception (test/auth {:credentials (password-creds "doesn't matter")}))))))) (deftest authenticating-with-a-map-fails-if-there's-no-equivalent-record (is (thrown-with-msg? Exception #"Failed to determine credentials type" - (auth {:password "foo"})))) + (test/auth {:credentials {:username nil + :password "foo"}})))) diff --git a/test/clj_libssh2/test_known_hosts.clj b/test/clj_libssh2/test_known_hosts.clj index 2746d0c..da8810f 100644 --- a/test/clj_libssh2/test_known_hosts.clj +++ b/test/clj_libssh2/test_known_hosts.clj @@ -1,50 +1,23 @@ (ns clj-libssh2.test-known-hosts (:require [clojure.test :refer :all] - [clj-libssh2.session :as session] [clj-libssh2.test-utils :as test])) (test/fixtures) -(defn- session-with-options - [options] - (session/open test/ssh-host - test/ssh-port - {:username (test/ssh-user)} - options)) - (deftest by-default-we-don't-fail-if-the-host-is-unknown - (is (= 0 (count @session/sessions))) - (let [file (test/known-hosts-file :missing) - session (session-with-options {:known-hosts-file file})] - (is (= 1 (count @session/sessions))) - (session/close session)) - (is (= 0 (count @session/sessions)))) + (test/auth {:known-hosts-file (test/known-hosts-file :missing)})) (deftest by-default-we-fail-if-the-host-is-different - (is (= 0 (count @session/sessions))) (is (thrown? Exception - (let [file (test/known-hosts-file :bad) - session (session-with-options {:known-hosts-file file})] - (session/close session)))) - (is (= 0 (count @session/sessions)))) + (test/auth {:known-hosts-file (test/known-hosts-file :bad)})))) (deftest known-hosts-checking-works-when-the-host-is-known - (is (= 0 (count @session/sessions))) - (let [file (test/known-hosts-file :good) - session (session-with-options {:fail-if-not-in-known-hosts true - :fail-unless-known-hosts-matches true - :known-hosts-file file})] - (is (= 1 (count @session/sessions))) - (session/close session)) - (is (= 0 (count @session/sessions)))) + (test/auth {:fail-if-not-in-known-hosts true + :fail-unless-known-hosts-matches true + :known-hosts-file (test/known-hosts-file :good)})) (deftest known-host-checking-can-be-ignored - (doseq [known-hosts-file [:good :bad :missing]] - (is (= 0 (count @session/sessions))) - (let [file (test/known-hosts-file known-hosts-file) - session (session-with-options {:fail-if-not-in-known-hosts false - :fail-unless-known-hosts-matches false - :known-hosts-file file})] - (is (= 1 (count @session/sessions))) - (session/close session)) - (is (= 0 (count @session/sessions))))) + (doseq [file (map test/known-hosts-file [:good :bad :missing])] + (test/auth {:fail-if-not-in-known-hosts false + :fail-unless-known-hosts-matches false + :known-hosts-file file}))) diff --git a/test/clj_libssh2/test_utils.clj b/test/clj_libssh2/test_utils.clj index 893f03b..289d1a0 100644 --- a/test/clj_libssh2/test_utils.clj +++ b/test/clj_libssh2/test_utils.clj @@ -1,16 +1,37 @@ (ns clj-libssh2.test-utils (:require [clojure.java.shell :as sh] [clojure.string :as str] - [clojure.test :as test] + [clojure.test :as test :refer [is]] [clojure-test-junit-output.core :refer [with-junit-output]] [net.n01se.clojure-jna :as jna] - [clj-libssh2.logging :as logging]) + [clj-libssh2.logging :as logging] + [clj-libssh2.session :as session]) (:import [java.io File])) (def ssh-host "127.0.0.1") (def ssh-port 2222) (defn ssh-user [] (System/getProperty "user.name")) +(defmacro with-test-session + [session session-params & body] + `(let [session-params# ~session-params + params# (merge {:hostname (or (:hostname session-params# ssh-host)) + :port (or (:port session-params#) ssh-port) + :credentials (merge {:username (ssh-user)} + (:credentials session-params#))} + (dissoc session-params# :hostname :port :credentials))] + (session/with-session session# params# + ~@body))) + +(defn auth + ([] + (auth {})) + ([session-params] + (is (= 0 (count @session/sessions))) + (with-test-session session session-params + (is (= 1 (count @session/sessions)))) + (is (= 0 (count @session/sessions))))) + (defn test-script [script] (str/join "/" [(System/getProperty "user.dir") "test/script" script]))