Skip to content

Commit

Permalink
Merge branch 'dry-test-session-handling'
Browse files Browse the repository at this point in the history
  • Loading branch information
conormcd committed Apr 9, 2016
2 parents e5cb1d8 + 9eafd48 commit 94f89ce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 79 deletions.
29 changes: 8 additions & 21 deletions test/clj_libssh2/test_agent.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))
32 changes: 12 additions & 20 deletions test/clj_libssh2/test_authentication.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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"}}))))
45 changes: 9 additions & 36 deletions test/clj_libssh2/test_known_hosts.clj
Original file line number Diff line number Diff line change
@@ -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})))
25 changes: 23 additions & 2 deletions test/clj_libssh2/test_utils.clj
Original file line number Diff line number Diff line change
@@ -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]))
Expand Down

0 comments on commit 94f89ce

Please sign in to comment.