Skip to content

Commit 94f89ce

Browse files
committed
Merge branch 'dry-test-session-handling'
2 parents e5cb1d8 + 9eafd48 commit 94f89ce

File tree

4 files changed

+52
-79
lines changed

4 files changed

+52
-79
lines changed

test/clj_libssh2/test_agent.clj

+8-21
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,33 @@
22
(:require [clojure.test :refer :all]
33
[clj-libssh2.libssh2 :as libssh2]
44
[clj-libssh2.libssh2.agent :as libssh2-agent]
5-
[clj-libssh2.session :as session]
65
[clj-libssh2.test-utils :as test]))
76

87
(test/fixtures)
98

10-
(defn agent-session
11-
[]
12-
(session/open test/ssh-host test/ssh-port {:username (test/ssh-user)} {}))
13-
14-
(defn open-and-close
15-
[]
16-
(is (= 0 (count @session/sessions)))
17-
(let [session (agent-session)]
18-
(is (= 1 (count @session/sessions)))
19-
(session/close session)
20-
(is (= 0 (count @session/sessions)))))
21-
229
(deftest agent-authentication-works
2310
(testing "A good session works"
24-
(open-and-close))
11+
(test/auth))
2512
(testing "If no identities match, we get an exception"
2613
(with-redefs [libssh2-agent/userauth (constantly libssh2/ERROR_PUBLICKEY_UNVERIFIED)]
27-
(is (thrown? Exception (open-and-close)))))
14+
(is (thrown? Exception (test/auth)))))
2815
(testing "If there are no identities, we get an exception"
2916
(with-redefs [libssh2-agent/get-identity (constantly 1)]
30-
(is (thrown? Exception (open-and-close))))))
17+
(is (thrown? Exception (test/auth))))))
3118

3219
(deftest agent-authentication-throws-but-doesn't-crash
3320
(testing "when libssh2_agent_init fails"
3421
(with-redefs [libssh2-agent/init (constantly nil)]
35-
(is (thrown? Exception (open-and-close)))))
22+
(is (thrown? Exception (test/auth)))))
3623
(testing "when libssh2_agent_connect fails"
3724
(with-redefs [libssh2-agent/connect (constantly libssh2/ERROR_AGENT_PROTOCOL)]
38-
(is (thrown? Exception (open-and-close)))))
25+
(is (thrown? Exception (test/auth)))))
3926
(testing "when libssh2_agent_list_identities fails"
4027
(with-redefs [libssh2-agent/list-identities (constantly libssh2/ERROR_BAD_USE)]
41-
(is (thrown? Exception (open-and-close)))))
28+
(is (thrown? Exception (test/auth)))))
4229
(testing "when libssh2_agent_get_identity fails"
4330
(with-redefs [libssh2-agent/get-identity (constantly libssh2/ERROR_AGENT_PROTOCOL)]
44-
(is (thrown? Exception (open-and-close)))))
31+
(is (thrown? Exception (test/auth)))))
4532
(testing "when libssh2_agent_disconnect fails"
4633
(with-redefs [libssh2-agent/disconnect (constantly libssh2/ERROR_SOCKET_DISCONNECT)]
47-
(is (thrown? Exception (open-and-close))))))
34+
(is (thrown? Exception (test/auth))))))

test/clj_libssh2/test_authentication.clj

+12-20
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22
(:require [clojure.test :refer :all]
33
[clj-libssh2.libssh2 :as libssh2]
44
[clj-libssh2.libssh2.userauth :as libssh2-userauth]
5-
[clj-libssh2.session :as session]
65
[clj-libssh2.test-utils :as test])
76
(:use clj-libssh2.authentication))
87

98
(test/fixtures)
109

11-
(defn auth
12-
[creds]
13-
(is (= 0 (count @session/sessions)))
14-
(let [session (session/open test/ssh-host test/ssh-port creds {})]
15-
(is (= 1 (count @session/sessions)))
16-
(session/close session))
17-
(is (= 0 (count @session/sessions))))
18-
1910
; This is more fully tested in clj-libssh2.test-agent
2011
(deftest agent-authentication-works
21-
(is (auth (->AgentCredentials (test/ssh-user)))))
12+
(is (test/auth {:credentials (->AgentCredentials (test/ssh-user))})))
2213

2314
(deftest key-authentication-works
2415
(let [user (test/ssh-user)
@@ -41,22 +32,22 @@
4132
bad-pubkey (->KeyCredentials user "" "/bad" (pubkey ""))]
4233
(testing "A passphrase-less key works"
4334
(is (valid? no-passphrase))
44-
(is (auth no-passphrase)))
35+
(is (test/auth {:credentials no-passphrase})))
4536
(testing "A key with a passphrase works"
4637
(is (valid? with-passphrase))
47-
(is (auth with-passphrase)))
38+
(is (test/auth {:credentials with-passphrase})))
4839
(testing "A valid but unauthorized key does not work"
4940
(is (valid? unauthorized))
50-
(is (thrown? Exception (auth unauthorized))))
41+
(is (thrown? Exception (test/auth {:credentials unauthorized}))))
5142
(testing "It fails if the private key file doesn't exist"
5243
(is (valid? bad-privkey))
53-
(is (thrown? Exception (auth bad-privkey))))
44+
(is (thrown? Exception (test/auth {:credentials bad-privkey}))))
5445
(testing "It fails if the public key file doesn't exist"
5546
(is (valid? bad-pubkey))
56-
(is (thrown? Exception (auth bad-pubkey))))
47+
(is (thrown? Exception (test/auth {:credentials bad-pubkey}))))
5748
(testing "It fails if the passphrase is incorrect"
5849
(is (valid? with-wrong-passphrase))
59-
(is (thrown? Exception (auth with-wrong-passphrase))))))
50+
(is (thrown? Exception (test/auth {:credentials with-wrong-passphrase}))))))
6051

6152
; We can't test this all the way without knowing a password on the local
6253
; machine. We can test with libssh2_userauth_password stubbed and some error
@@ -68,15 +59,16 @@
6859
(->PasswordCredentials (test/ssh-user) password))]
6960
(testing "A successful authentication returns true"
7061
(with-redefs [libssh2-userauth/password (constantly 0)]
71-
(is (auth (password-creds "doesn't matter")))))
62+
(is (test/auth {:credentials (password-creds "doesn't matter")}))))
7263
(testing "It fails to authenticate with the wrong password"
73-
(is (thrown? Exception (auth (password-creds "the wrong password")))))
64+
(is (thrown? Exception (test/auth {:credentials (password-creds "the wrong password")}))))
7465
(testing "A library error does not result in a crash"
7566
(with-redefs [libssh2-userauth/password (constantly libssh2/ERROR_ALLOC)]
76-
(is (thrown? Exception (auth (password-creds "doesn't matter"))))))))
67+
(is (thrown? Exception (test/auth {:credentials (password-creds "doesn't matter")})))))))
7768

7869
(deftest authenticating-with-a-map-fails-if-there's-no-equivalent-record
7970
(is (thrown-with-msg?
8071
Exception
8172
#"Failed to determine credentials type"
82-
(auth {:password "foo"}))))
73+
(test/auth {:credentials {:username nil
74+
:password "foo"}}))))

test/clj_libssh2/test_known_hosts.clj

+9-36
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
11
(ns clj-libssh2.test-known-hosts
22
(:require [clojure.test :refer :all]
3-
[clj-libssh2.session :as session]
43
[clj-libssh2.test-utils :as test]))
54

65
(test/fixtures)
76

8-
(defn- session-with-options
9-
[options]
10-
(session/open test/ssh-host
11-
test/ssh-port
12-
{:username (test/ssh-user)}
13-
options))
14-
157
(deftest by-default-we-don't-fail-if-the-host-is-unknown
16-
(is (= 0 (count @session/sessions)))
17-
(let [file (test/known-hosts-file :missing)
18-
session (session-with-options {:known-hosts-file file})]
19-
(is (= 1 (count @session/sessions)))
20-
(session/close session))
21-
(is (= 0 (count @session/sessions))))
8+
(test/auth {:known-hosts-file (test/known-hosts-file :missing)}))
229

2310
(deftest by-default-we-fail-if-the-host-is-different
24-
(is (= 0 (count @session/sessions)))
2511
(is (thrown? Exception
26-
(let [file (test/known-hosts-file :bad)
27-
session (session-with-options {:known-hosts-file file})]
28-
(session/close session))))
29-
(is (= 0 (count @session/sessions))))
12+
(test/auth {:known-hosts-file (test/known-hosts-file :bad)}))))
3013

3114
(deftest known-hosts-checking-works-when-the-host-is-known
32-
(is (= 0 (count @session/sessions)))
33-
(let [file (test/known-hosts-file :good)
34-
session (session-with-options {:fail-if-not-in-known-hosts true
35-
:fail-unless-known-hosts-matches true
36-
:known-hosts-file file})]
37-
(is (= 1 (count @session/sessions)))
38-
(session/close session))
39-
(is (= 0 (count @session/sessions))))
15+
(test/auth {:fail-if-not-in-known-hosts true
16+
:fail-unless-known-hosts-matches true
17+
:known-hosts-file (test/known-hosts-file :good)}))
4018

4119
(deftest known-host-checking-can-be-ignored
42-
(doseq [known-hosts-file [:good :bad :missing]]
43-
(is (= 0 (count @session/sessions)))
44-
(let [file (test/known-hosts-file known-hosts-file)
45-
session (session-with-options {:fail-if-not-in-known-hosts false
46-
:fail-unless-known-hosts-matches false
47-
:known-hosts-file file})]
48-
(is (= 1 (count @session/sessions)))
49-
(session/close session))
50-
(is (= 0 (count @session/sessions)))))
20+
(doseq [file (map test/known-hosts-file [:good :bad :missing])]
21+
(test/auth {:fail-if-not-in-known-hosts false
22+
:fail-unless-known-hosts-matches false
23+
:known-hosts-file file})))

test/clj_libssh2/test_utils.clj

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
(ns clj-libssh2.test-utils
22
(:require [clojure.java.shell :as sh]
33
[clojure.string :as str]
4-
[clojure.test :as test]
4+
[clojure.test :as test :refer [is]]
55
[clojure-test-junit-output.core :refer [with-junit-output]]
66
[net.n01se.clojure-jna :as jna]
7-
[clj-libssh2.logging :as logging])
7+
[clj-libssh2.logging :as logging]
8+
[clj-libssh2.session :as session])
89
(:import [java.io File]))
910

1011
(def ssh-host "127.0.0.1")
1112
(def ssh-port 2222)
1213
(defn ssh-user [] (System/getProperty "user.name"))
1314

15+
(defmacro with-test-session
16+
[session session-params & body]
17+
`(let [session-params# ~session-params
18+
params# (merge {:hostname (or (:hostname session-params# ssh-host))
19+
:port (or (:port session-params#) ssh-port)
20+
:credentials (merge {:username (ssh-user)}
21+
(:credentials session-params#))}
22+
(dissoc session-params# :hostname :port :credentials))]
23+
(session/with-session session# params#
24+
~@body)))
25+
26+
(defn auth
27+
([]
28+
(auth {}))
29+
([session-params]
30+
(is (= 0 (count @session/sessions)))
31+
(with-test-session session session-params
32+
(is (= 1 (count @session/sessions))))
33+
(is (= 0 (count @session/sessions)))))
34+
1435
(defn test-script
1536
[script]
1637
(str/join "/" [(System/getProperty "user.dir") "test/script" script]))

0 commit comments

Comments
 (0)