|
| 1 | +(ns clj-libssh2.test-agent |
| 2 | + (:require [clojure.test :refer :all] |
| 3 | + [clj-libssh2.libssh2 :as libssh2] |
| 4 | + [clj-libssh2.libssh2.agent :as libssh2-agent] |
| 5 | + [clj-libssh2.session :as session] |
| 6 | + [clj-libssh2.test-utils :as test])) |
| 7 | + |
| 8 | +(test/fixtures) |
| 9 | + |
| 10 | +(defn agent-session |
| 11 | + [] |
| 12 | + (session/open test/ssh-host |
| 13 | + test/ssh-port |
| 14 | + {:username (test/ssh-user) |
| 15 | + :agent true})) |
| 16 | + |
| 17 | +(defn open-and-close |
| 18 | + [] |
| 19 | + (is (= 0 (count @session/sessions))) |
| 20 | + (let [session (agent-session)] |
| 21 | + (is (= 1 (count @session/sessions))) |
| 22 | + (session/close session) |
| 23 | + (is (= 0 (count @session/sessions))))) |
| 24 | + |
| 25 | +(deftest agent-authentication-works |
| 26 | + (testing "A good session works" |
| 27 | + (open-and-close)) |
| 28 | + (testing "If no identities match, we get an exception" |
| 29 | + (with-redefs [libssh2-agent/userauth (constantly libssh2/ERROR_PUBLICKEY_UNVERIFIED)] |
| 30 | + (is (thrown? Exception (open-and-close))))) |
| 31 | + (testing "If there are no identities, we get an exception" |
| 32 | + (with-redefs [libssh2-agent/get-identity (constantly 1)] |
| 33 | + (is (thrown? Exception (open-and-close)))))) |
| 34 | + |
| 35 | +(deftest agent-authentication-throws-but-doesn't-crash |
| 36 | + (testing "when libssh2_agent_init fails" |
| 37 | + (with-redefs [libssh2-agent/init (constantly nil)] |
| 38 | + (is (thrown? Exception (open-and-close))))) |
| 39 | + (testing "when libssh2_agent_connect fails" |
| 40 | + (with-redefs [libssh2-agent/connect (constantly libssh2/ERROR_AGENT_PROTOCOL)] |
| 41 | + (is (thrown? Exception (open-and-close))))) |
| 42 | + (testing "when libssh2_agent_list_identities fails" |
| 43 | + (with-redefs [libssh2-agent/list-identities (constantly libssh2/ERROR_BAD_USE)] |
| 44 | + (is (thrown? Exception (open-and-close))))) |
| 45 | + (testing "when libssh2_agent_get_identity fails" |
| 46 | + (with-redefs [libssh2-agent/get-identity (constantly libssh2/ERROR_AGENT_PROTOCOL)] |
| 47 | + (is (thrown? Exception (open-and-close))))) |
| 48 | + (testing "when libssh2_agent_disconnect fails" |
| 49 | + (with-redefs [libssh2-agent/disconnect (constantly libssh2/ERROR_SOCKET_DISCONNECT)] |
| 50 | + (is (thrown? Exception (open-and-close)))))) |
0 commit comments