@@ -523,6 +523,23 @@ list, Alan Dipert and MeikelBrandmeyer."
523
523
(let [[priv pub] (generate-keypair :rsa 1024 " hello" )]
524
524
(add-identity *ssh-agent* " name" priv pub (.getBytes " hello" )))))
525
525
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
+
526
543
(deftest forward-local-port-test
527
544
(testing " minimal test"
528
545
(with-ssh-agent [false ]
@@ -531,13 +548,36 @@ list, Alan Dipert and MeikelBrandmeyer."
531
548
:strict-host-key-checking :no )]
532
549
(is (instance? com.jcraft.jsch.Session session))
533
550
(is (not (connected? session)))
551
+ (is (not (port-reachable? 2222 )))
534
552
(connect session)
535
553
(is (connected? session))
536
554
(forward-local-port session 2222 22 )
555
+ (is (port-reachable? 2222 ))
537
556
(unforward-local-port session 2222 )
538
557
(forward-local-port session 2222 22 " localhost" )
539
558
(unforward-local-port session 2222 )
540
559
(with-local-port-forward [session 2222 22 ]
541
- (is true ))
560
+ (is ( port-reachable? 2222 ) ))
542
561
(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