forked from clj-commons/clj-ssh
/
ssh_test.clj
426 lines (386 loc) · 15.7 KB
/
ssh_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
(ns clj-ssh.ssh-test
(:use [clj-ssh.ssh] :reload-all)
(:use [clojure.test])
(:import com.jcraft.jsch.JSch))
;; clojure.contrib 1.1/1.2 compatability
(try
(use '[clojure.contrib.io :as io])
(catch Exception e
(use '[clojure.contrib.duck-streams :as io])))
(defmacro with-private-vars [[ns fns] & tests]
"Refers private fns from ns and runs tests in context. From users mailing
list, Alan Dipert and MeikelBrandmeyer."
`(let ~(reduce #(conj %1 %2 `@(ns-resolve '~ns '~%2)) [] fns)
~@tests))
;; TEST SETUP
;;
;; The tests assume the following setup
;;
;; ssh-keygen -f ~/.ssh/clj_ssh -t rsa -C "key for test clj-ssh" -N ""
;; ssh-keygen -f ~/.ssh/clj_ssh_pp -t rsa -C "key for test clj-ssh" -N "clj-ssh"
;; cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak
;; echo "from=\"localhost\" $(cat ~/.ssh/clj_ssh.pub)" >> ~/.ssh/authorized_keys
;; echo "from=\"localhost\" $(cat ~/.ssh/clj_ssh_pp.pub)" >> ~/.ssh/authorized_keys
(defn private-key-path
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh"))
(defn public-key-path
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh.pub"))
(defn encrypted-private-key-path
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh_pp"))
(defn encrypted-public-key-path
[] (str (. System getProperty "user.home") "/.ssh/clj_ssh_pp.pub"))
(defn username
[] (or (. System getProperty "ssh.username")
(. System getProperty "user.name")))
(defn cwd
[] (. System getProperty "user.dir"))
(defn home
[] (. System getProperty "user.home"))
(defn with-test-private-key-path
[f]
(binding [clj-ssh.ssh/*default-identity* (private-key-path)]
(f)))
(use-fixtures :once with-test-private-key-path)
(with-private-vars [clj-ssh.ssh [file-path camelize]]
(deftest file-path-test
(is (= "abc" (file-path "abc")))
(is (= "abc" (file-path (java.io.File. "abc")))))
(deftest camelize-test
(is (= "StrictHostKeyChecking" (camelize "strict-host-key-checking")))))
;; (deftest default-identity-test
;; (is (= (str (. System getProperty "user.home") "/.ssh/id_rsa")
;; (.getPath (default-identity)))))
(deftest default-session-options-test
(let [old *default-session-options*
new {:strict-host-key-checking :no}]
(default-session-options new)
(is (= new *default-session-options*))
(default-session-options old)
(is (= old *default-session-options*))))
(deftest ssh-agent?-test
(is (ssh-agent? (JSch.)))
(is (not (ssh-agent? "i'm not an ssh-agent"))))
(deftest create-ssh-agent-test
(is (ssh-agent? (create-ssh-agent false)))
(is (ssh-agent? (create-ssh-agent (private-key-path)))))
(deftest with-ssh-agent-test
(with-ssh-agent []
(is (ssh-agent? *ssh-agent*))
(is (= 1 (count (.getIdentityNames *ssh-agent*)))))
(with-ssh-agent [false]
(is (ssh-agent? *ssh-agent*))
(is (= 0 (count (.getIdentityNames *ssh-agent*)))))
(with-ssh-agent [(private-key-path)]
(is (ssh-agent? *ssh-agent*)))
(let [agent (create-ssh-agent)]
(with-ssh-agent [agent]
(is (= *ssh-agent* agent)))))
(deftest make-identity-test
(with-ssh-agent [false]
(let [path (private-key-path)
identity (make-identity path (str path ".pub"))]
(is (instance? com.jcraft.jsch.Identity identity))
(is (not (.isEncrypted identity)))
(add-identity identity)
(is (= 1 (count (.getIdentityNames *ssh-agent*)))))
(let [path (encrypted-private-key-path)
identity (make-identity path (str path ".pub"))]
(is (instance? com.jcraft.jsch.Identity identity))
(is (.isEncrypted identity))
(add-identity identity "clj-ssh")
(is (= 2 (count (.getIdentityNames *ssh-agent*)))))))
(deftest add-identity-test
(let [key (private-key-path)]
(with-ssh-agent [false]
(add-identity key)
(is (= 1 (count (.getIdentityNames *ssh-agent*))))
(add-identity *ssh-agent* key)))
(testing "passing byte arrays"
(with-ssh-agent [false]
(add-identity
*ssh-agent*
"name"
(.getBytes (slurp (private-key-path)))
(.getBytes (slurp (public-key-path)))
nil)
(is (= 1 (count (.getIdentityNames *ssh-agent*))))
(is (= "name" (first (.getIdentityNames *ssh-agent*)))))))
(deftest has-identity?-test
(let [key (private-key-path)]
(with-ssh-agent [false]
(is (not (has-identity? key)))
(is (not (has-identity? *ssh-agent* key)))
(add-identity key)
(is (= 1 (count (.getIdentityNames *ssh-agent*))))
(is (has-identity? key))
(is (has-identity? *ssh-agent* key))
(add-identity *ssh-agent* key))))
(with-private-vars [clj-ssh.ssh [session-impl]]
(deftest session-impl-test
(with-ssh-agent []
(let [session
(session-impl *ssh-agent* "somehost" (username) nil nil {})]
(is (instance? com.jcraft.jsch.Session session))
(is (not (connected? session))))
(let [session
(session-impl *ssh-agent* "localhost" (username) nil nil
{:strict-host-key-checking :no})]
(is (instance? com.jcraft.jsch.Session session))
(is (not (connected? session)))
(is (= "no" (.getConfig session "StrictHostKeyChecking")))))))
(deftest session-test
(with-ssh-agent []
(let [session (session *ssh-agent* "localhost"
:username (username) :port 22)]
(is (instance? com.jcraft.jsch.Session session))
(is (not (connected? session)))))
(with-ssh-agent []
(let [session (session "localhost" :username (username))]
(is (instance? com.jcraft.jsch.Session session))
(is (not (connected? session)))))
(with-ssh-agent []
(let [session (session *ssh-agent* "localhost" :username (username))]
(is (instance? com.jcraft.jsch.Session session))
(is (not (connected? session))))))
(deftest session-connect-test
(with-ssh-agent [false]
(add-identity (private-key-path))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(is (instance? com.jcraft.jsch.Session session))
(is (not (connected? session)))
(connect session)
(is (connected? session))
(disconnect session)
(is (not (connected? session))))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(is (connected? session)))
(is (not (connected? session)))))
(with-ssh-agent [false]
(add-identity (encrypted-private-key-path) "clj-ssh")
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(is (instance? com.jcraft.jsch.Session session))
(is (not (connected? session)))
(connect session)
(is (connected? session))
(disconnect session)
(is (not (connected? session))))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(is (connected? session)))
(is (not (connected? session))))))
(deftest open-shell-channel-test
(with-ssh-agent [false]
(add-identity (private-key-path))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(let [shell (open-channel session :shell)
os (java.io.ByteArrayOutputStream.)]
(.setInputStream
shell
(java.io.ByteArrayInputStream. (.getBytes "ls /;exit 0;\n"))
false)
(.setOutputStream shell os)
(with-connection shell
(while (connected? shell)
(Thread/sleep 1000))
(is (.contains (str os) "bin"))))))))
(deftest ssh-shell-test
(with-ssh-agent [false]
(add-identity (private-key-path))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(let [result (ssh-shell session "echo hello" "UTF-8" {})]
(is (= 0 (first result)))
(is (.contains (second result) "hello")))
(let [result (ssh-shell session "echo hello" :bytes {})]
(is (= 0 (first result)))
(is (.contains (String. (second result)) "hello")))
(let [result (ssh-shell session "echo hello;exit 1" "UTF-8" {})]
(is (= 1 (first result)))
(is (.contains (second result) "hello")))
(let [result (ssh-shell session "exit $(tty -s)" "UTF-8" {:pty true})]
(is (= 0 (first result))))
(let [result (ssh-shell session "exit $(tty -s)" "UTF-8" {:pty nil})]
(is (= 1 (first result))))))))
(deftest ssh-exec-test
(with-ssh-agent [false]
(add-identity (private-key-path))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(let [result (ssh-exec session "/bin/bash -c 'ls /'" nil "UTF-8" {})]
(is (= 0 (first result)))
(is (.contains (second result) "bin"))
(is (= "" (last result))))
(let [result (ssh-exec session "/bin/bash -c 'lsxxxxx /'" nil "UTF-8" {})]
(is (not (= 0 (first result))))
(is (= "" (second result)))
(is (.contains (last result) "command not found")))))))
(deftest ssh-test
(with-ssh-agent [false]
(add-identity (private-key-path))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(let [result (ssh session :in "echo hello")]
(is (= 0 (first result)))
(is (.contains (second result) "hello")))
(let [result (ssh session "/bin/bash -c 'ls /'")]
(is (= 0 (first result)))
(is (.contains (second result) "bin"))
(is (= "" (last result))))))
(let [result (ssh "localhost" :in "echo hello" :username (username))]
(is (= 0 (first result)))
(is (.contains (second result) "hello")))
(let [result (ssh "localhost" "/bin/bash -c 'ls /'" :username (username))]
(is (= 0 (first result)))
(is (.contains (second result) "bin"))
(is (= "" (last result))))
(let [result (ssh "localhost" :in "tty -s" :pty true :username (username))]
(is (= 0 (first result))))
(let [result (ssh "localhost" :in "tty -s" :pty false :username (username))]
(is (= 1 (first result)))))
(with-default-session-options {:strict-host-key-checking :no}
(with-default-identity (private-key-path)
(let [result (ssh "localhost" :in "echo hello" :username (username))]
(is (= 0 (first result)))
(is (.contains (second result) "hello")))
(let [result (ssh "localhost" :in "echo hello" :return-map true
:username (username))]
(is (= 0 (result :exit)))
(is (.contains (result :out) "hello")))
(let [result (ssh "localhost" "/bin/bash -c 'ls /'" :username (username))]
(is (= 0 (first result)))
(is (.contains (second result) "bin"))
(is (= "" (last result))))
(let [result (ssh "localhost" "/bin/bash -c 'ls /'" :return-map true
:username (username))]
(is (= 0 (result :exit)))
(is (.contains (result :out) "bin"))
(is (= "" (result :err)))))))
(deftest ssh-sftp-cmd-test
(with-ssh-agent [false]
(add-identity (private-key-path))
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(let [channel (ssh-sftp session)
dir (ssh-sftp-cmd channel :ls ["/"] {})]
(ssh-sftp-cmd channel :cd ["/"] {})
(is (= "/" (ssh-sftp-cmd channel :pwd [] {})))
;; value equality comparison on lsentry is borked
(is (= (map str dir)
(map str (ssh-sftp-cmd channel :ls [] {})))))))))
(defn sftp-monitor
"Create a SFTP progress monitor"
[]
(let [operation (atom nil)
source (atom nil)
destination (atom nil)
number (atom nil)
done (atom false)
progress (atom 0)
continue (atom true)]
[ (proxy [com.jcraft.jsch.SftpProgressMonitor] []
(init [op src dest max]
(do
(reset! operation op)
(reset! source src)
(reset! destination dest)
(reset! number max)
(reset! done false)))
(count [n]
(reset! progress n)
@continue)
(end []
(reset! done true)))
[operation source destination number done progress continue]]))
(defn sftp-monitor-done [state]
@(nth state 4))
(defn test-sftp-with [channel]
(let [dir (sftp channel :ls "/")]
(sftp channel :cd "/")
(is (= "/" (sftp channel :pwd)))
;; value equality comparison on lsentry is borked
(is (= (map str dir)
(map str (sftp channel :ls))))
(let [tmpfile1 (java.io.File/createTempFile "clj-ssh" "test")
tmpfile2 (java.io.File/createTempFile "clj-ssh" "test")
file1 (.getPath tmpfile1)
file2 (.getPath tmpfile2)
content "content"
content2 "content2"]
(try
(.setWritable tmpfile1 true false)
(.setWritable tmpfile2 true false)
(io/copy content tmpfile1)
(sftp channel :put file1 file2)
(is (= content (slurp file2)))
(io/copy content2 tmpfile2)
(sftp channel :get file2 file1)
(is (= content2 (slurp file1)))
(sftp
channel :put (java.io.ByteArrayInputStream. (.getBytes content)) file1)
(is (= content (slurp file1)))
(let [[monitor state] (sftp-monitor)]
(sftp channel :put (java.io.ByteArrayInputStream. (.getBytes content))
file2 :with-monitor monitor)
(is (sftp-monitor-done state)))
(is (= content (slurp file2)))
(finally
(.delete tmpfile1)
(.delete tmpfile2))))))
(defn test-sftp-transient-with [channel & options]
(let [dir (sftp channel :ls "/")]
(apply sftp channel :cd "/" options)
(is (not= "/" (sftp channel :pwd)))
(let [tmpfile1 (java.io.File/createTempFile "clj-ssh" "test")
tmpfile2 (java.io.File/createTempFile "clj-ssh" "test")
file1 (.getPath tmpfile1)
file2 (.getPath tmpfile2)
content "content"
content2 "othercontent"]
(try
(.setWritable tmpfile1 true false)
(.setWritable tmpfile2 true false)
(io/copy content tmpfile1)
(apply sftp channel :put file1 file2 options)
(is (= content (slurp file2)))
(io/copy content2 tmpfile2)
(apply sftp channel :get file2 file1 options)
(is (= content2 (slurp file1)))
(apply sftp channel
:put (java.io.ByteArrayInputStream. (.getBytes content))
file1
options)
(is (= content (slurp file1)))
(let [[monitor state] (sftp-monitor)]
(apply sftp channel
:put (java.io.ByteArrayInputStream. (.getBytes content))
file2 :with-monitor monitor options)
(is (sftp-monitor-done state)))
(is (= content (slurp file2)))
(finally
(.delete tmpfile1)
(.delete tmpfile2))))))
(deftest sftp-session-test
(with-default-identity (private-key-path)
(with-ssh-agent []
(let [session (session "localhost" :username (username)
:strict-host-key-checking :no)]
(with-connection session
(let [channel (ssh-sftp session)]
(with-connection channel
(test-sftp-with channel)))
(test-sftp-transient-with session))))))
(deftest sftp-hostnametest
(with-default-identity (private-key-path)
(with-default-session-options {:strict-host-key-checking :no}
(test-sftp-transient-with "localhost" :username (username)))))