session.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; session.scm -- Testing of session procedures without a connection.
  2. ;; Copyright (C) 2014, 2015 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  3. ;;
  4. ;; This file is a part of Guile-SSH.
  5. ;;
  6. ;; Guile-SSH is free software: you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation, either version 3 of the
  9. ;; License, or (at your option) any later version.
  10. ;;
  11. ;; Guile-SSH is distributed in the hope that it will be useful, but
  12. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;; General Public License for more details.
  15. ;;
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
  18. (add-to-load-path (getenv "abs_top_srcdir"))
  19. (use-modules (srfi srfi-64)
  20. (ssh session)
  21. ;; Helper procedures
  22. (tests common))
  23. (test-begin-with-log "session")
  24. ;;;
  25. (test-assert "%make-session"
  26. (%make-session))
  27. (test-assert-with-log "%make-session, gc test"
  28. (let ((max-sessions 1000))
  29. (do ((idx 1 (+ idx 1)))
  30. ((> idx max-sessions))
  31. (when (zero? (euclidean-remainder idx 100))
  32. (format-log/scm 'nolog "" (format #f "~d / ~d sessions created ..."
  33. idx max-sessions)))
  34. (%make-session))
  35. #t))
  36. (test-assert "session?"
  37. (let ((session (%make-session))
  38. (x "string"))
  39. (and (session? session)
  40. (not (session? x)))))
  41. (test-assert "comparison of sessions"
  42. (let ((s1 (%make-session))
  43. (s2 (%make-session)))
  44. (and (equal? s1 s1)
  45. (not (equal? s1 s2)))))
  46. (test-assert "session-set!, valid values"
  47. (let ((session (%make-session))
  48. (options `((host "localhost")
  49. (port 22)
  50. (bindaddr "127.0.0.1")
  51. (user "Random J. User")
  52. (timeout 15) ;seconds
  53. (timeout-usec 15000) ;milliseconds
  54. (ssh1 #f #t)
  55. (ssh2 #f #t)
  56. (log-verbosity nolog rare protocol packet functions
  57. nolog)
  58. (compression "yes" "no")
  59. (compression-level 1 2 3 4 5 6 7 8 9)
  60. (callbacks ((user-data . "hello")
  61. (global-request-callback . ,(const #f))))))
  62. (res #t))
  63. (for-each
  64. (lambda (opt)
  65. (for-each
  66. (lambda (val)
  67. (session-set! session (car opt) val))
  68. (cdr opt)))
  69. options)
  70. res))
  71. (test-assert "session-set!, invalid values"
  72. (let ((session (%make-session))
  73. (options '((host 12345 #t)
  74. (port "string" -22)
  75. (bindaddr 12345 -12345)
  76. (user 12345 -12345)
  77. (timeout "string" -15)
  78. (timeout-usec "string" -15000)
  79. (ssh1 12345 "string")
  80. (ssh2 12345 "string")
  81. (log-verbosity "string" -1 0 1 2 3 4 5)
  82. (compression 12345)
  83. (compression-level -1 0 10)
  84. (callbacks "not a list"
  85. ((global-request-callback . #f)))))
  86. (res #t))
  87. (for-each
  88. (lambda (opt)
  89. (for-each
  90. (lambda (val)
  91. (catch #t
  92. (lambda ()
  93. (session-set! session (car opt) val)
  94. (let* ((r (test-runner-current))
  95. (l (test-runner-aux-value r)))
  96. (format l " opt: ~a, val: ~a -- passed mistakenly~%"
  97. (car opt) val)
  98. (set! res #f)))
  99. (const #t)))
  100. (cdr opt)))
  101. options)
  102. res))
  103. (test-assert "session-get"
  104. (let* ((host "example.com")
  105. (port 12345)
  106. (user "alice")
  107. (proxycommand "test")
  108. (callbacks '((user-data . "test")))
  109. (session (make-session #:host host
  110. #:port port
  111. #:user user
  112. #:identity %rsakey
  113. #:proxycommand proxycommand
  114. #:callbacks callbacks)))
  115. (and (string=? (session-get session 'host) host)
  116. (= (session-get session 'port) port)
  117. (string=? (session-get session 'user) user)
  118. (string=? (session-get session 'identity) %rsakey)
  119. (string=? (session-get session 'proxycommand) proxycommand)
  120. (equal? (session-get session 'callbacks) callbacks)
  121. ;; Make sure that default callbacks value is '#f'.
  122. (equal? (session-get (%make-session) 'callbacks) #f))))
  123. (test-assert "session-parse-config!"
  124. (let ((session (make-session #:host "example")))
  125. (session-parse-config! session %config)
  126. (format (current-error-port) "session: ~a~%" session)
  127. (and (string=? (session-get session 'host) "example.org")
  128. (string=? (session-get session 'user) "alice")
  129. (= (session-get session 'port) 2222))))
  130. (test-assert "make-session"
  131. (make-session #:host "localhost"
  132. #:port 22
  133. #:user "Random J. User"))
  134. (test-equal-with-log "blocking-flush!"
  135. 'ok
  136. (blocking-flush! (%make-session) 15))
  137. (test-assert "connected?, check that we are not connected"
  138. (let ((session (%make-session)))
  139. (not (connected? session))))
  140. (test-end "session")
  141. (exit (= (test-runner-fail-count (test-runner-current)) 0))
  142. ;;; session.scm ends here.