Skip to content

Commit 6985fb7

Browse files
committed
Add test for forward-remote-port
1 parent 0740694 commit 6985fb7

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/clj_ssh/ssh.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,16 @@ keys. All other option key pairs will be passed as SSH config options."
404404
([session remote-port local-port local-host]
405405
(.setPortForwardingR session remote-port local-host local-port))
406406
([session remote-port local-port]
407-
(forward-remote-port session local-port remote-port "localhost")))
407+
(forward-remote-port session remote-port local-port "localhost")))
408408

409409
(defn unforward-remote-port
410410
"Remove remote port forwarding"
411411
[session remote-port]
412412
(.delPortForwardingR session remote-port))
413413

414414
(defmacro with-remote-port-forward
415-
"Creates a context in which a remote SSH tunnel is established for the session.
416-
(Use before the connection is opened.)"
415+
"Creates a context in which a remote SSH tunnel is established for the
416+
session. (Use after the connection is opened.)"
417417
[[session remote-port local-port & [local-host & _]] & body]
418418
`(try
419419
(forward-remote-port
@@ -436,7 +436,7 @@ keys. All other option key pairs will be passed as SSH config options."
436436

437437
(defmacro with-local-port-forward
438438
"Creates a context in which a local SSH tunnel is established for the session.
439-
(Use before the connection is opened.)"
439+
(Use after the connection is opened.)"
440440
[[session local-port remote-port & [remote-host & _]] & body]
441441
`(try
442442
(forward-local-port

test/clj_ssh/ssh_test.clj

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,23 @@ list, Alan Dipert and MeikelBrandmeyer."
523523
(let [[priv pub] (generate-keypair :rsa 1024 "hello")]
524524
(add-identity *ssh-agent* "name" priv pub (.getBytes "hello")))))
525525

526+
(defn port-reachable?
527+
([ip port timeout]
528+
(let [socket (doto (java.net.Socket.)
529+
(.setReuseAddress false)
530+
(.setSoLinger false 1)
531+
(.setSoTimeout timeout))]
532+
(try
533+
(.connect socket (java.net.InetSocketAddress. ip port))
534+
true
535+
(catch java.io.IOException _)
536+
(finally
537+
(try (.close socket) (catch java.io.IOException _))))))
538+
([ip port]
539+
(port-reachable? ip port 2000))
540+
([port]
541+
(port-reachable? "localhost" port)))
542+
526543
(deftest forward-local-port-test
527544
(testing "minimal test"
528545
(with-ssh-agent [false]
@@ -531,13 +548,36 @@ list, Alan Dipert and MeikelBrandmeyer."
531548
:strict-host-key-checking :no)]
532549
(is (instance? com.jcraft.jsch.Session session))
533550
(is (not (connected? session)))
551+
(is (not (port-reachable? 2222)))
534552
(connect session)
535553
(is (connected? session))
536554
(forward-local-port session 2222 22)
555+
(is (port-reachable? 2222))
537556
(unforward-local-port session 2222)
538557
(forward-local-port session 2222 22 "localhost")
539558
(unforward-local-port session 2222)
540559
(with-local-port-forward [session 2222 22]
541-
(is true))
560+
(is (port-reachable? 2222)))
542561
(with-local-port-forward [session 2222 22 "localhost"]
543-
(is true))))))
562+
(is (port-reachable? 2222)))))))
563+
564+
(deftest forward-remote-port-test
565+
(testing "minimal test"
566+
(with-ssh-agent [false]
567+
(add-identity (private-key-path))
568+
(let [session (session "localhost" :username (username)
569+
:strict-host-key-checking :no)]
570+
(is (instance? com.jcraft.jsch.Session session))
571+
(is (not (connected? session)))
572+
(is (not (port-reachable? 2222)))
573+
(connect session)
574+
(is (connected? session))
575+
(forward-remote-port session 2222 22)
576+
(is (port-reachable? 2222))
577+
(unforward-remote-port session 2222)
578+
(forward-remote-port session 2222 22 "localhost")
579+
(unforward-remote-port session 2222)
580+
(with-remote-port-forward [session 2222 22]
581+
(is (port-reachable? 2222)))
582+
(with-remote-port-forward [session 2222 22 "localhost"]
583+
(is (port-reachable? 2222)))))))

0 commit comments

Comments
 (0)