Skip to content

Commit 8e44703

Browse files
committed
Agent tests
1 parent a6916aa commit 8e44703

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/clj_libssh2/agent.clj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
(throw (Exception. "Failed to initialize agent.")))
2727
(try
2828
(handle-errors session (libssh2-agent/connect ssh-agent))
29-
(loop [previous nil]
30-
(when-let [id (get-identity session ssh-agent previous)]
31-
(when (not= 0 (libssh2-agent/userauth ssh-agent username id))
32-
(recur id))))
29+
(when-not (loop [success false
30+
previous nil]
31+
(if success
32+
success
33+
(if-let [id (get-identity session ssh-agent previous)]
34+
(recur
35+
(= 0 (libssh2-agent/userauth ssh-agent username id))
36+
id)
37+
false)))
38+
(throw (Exception. "Failed to authenticate with the agent.")))
3339
(finally
3440
(handle-errors session (libssh2-agent/disconnect ssh-agent))
3541
(libssh2-agent/free ssh-agent)))))

test/clj_libssh2/test_agent.clj

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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))))))

test/script/setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ generate_user_keys() {
6767
launch_ssh_agent() {
6868
require_executable ssh-agent
6969
eval "$(ssh-agent -s -a "${TMP_DIR}/agent.sock")" > /dev/null
70+
ssh-add -D > /dev/null
7071
}
7172

7273
launch_sshd() {

0 commit comments

Comments
 (0)