popen.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ;;; popen.scm -- Remote pipes testing.
  2. ;; Copyright (C) 2015, 2016 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. (ice-9 rdelim)
  21. (tests common)
  22. (ssh channel)
  23. (ssh session)
  24. (ssh auth)
  25. (ssh log)
  26. (ssh popen))
  27. (set-log-verbosity! 'functions)
  28. (test-begin-with-log "popen")
  29. ;;; Helper procedures.
  30. (define (call-with-connected-session/popen proc)
  31. "Make a session for a channel test."
  32. (call-with-connected-session
  33. (lambda (session)
  34. (authenticate-server session)
  35. (userauth-none! session)
  36. (proc session))))
  37. (define (response=? channel string)
  38. "Read a line from a CHANNEL, check if the line is equal to a STRING."
  39. (string=? (read-line channel) string))
  40. (define (input-only? port)
  41. (and (input-port? port)
  42. (not (output-port? port))))
  43. (define (output-only? port)
  44. (and (output-port? port)
  45. (not (input-port? port))))
  46. ;;;
  47. (test-assert-with-log "open-remote-pipe, OPEN_READ"
  48. (run-client-test
  49. (lambda (server)
  50. (start-server/exec server (lambda () #t)))
  51. (lambda ()
  52. (sleep 1)
  53. (call-with-connected-session/shell
  54. (lambda (session)
  55. (let ((channel (open-remote-pipe session "ping" OPEN_READ)))
  56. (and (input-only? channel)
  57. (poll channel (lambda args (response=? channel "pong"))))))))))
  58. (test-assert-with-log "open-remote-pipe, OPEN_PTY_READ"
  59. (run-client-test
  60. (lambda (server)
  61. (start-server/exec server (const #t)))
  62. (lambda ()
  63. (call-with-connected-session/shell
  64. (lambda (session)
  65. (let* ((OPEN_PTY_READ (string-append OPEN_PTY OPEN_READ))
  66. (channel (open-remote-pipe session "ping" OPEN_PTY_READ)))
  67. (format-log/scm 'nolog "open-remote-pipe, OPEN_PTY_READ" "channel: ~A" channel)
  68. (and (input-only? channel)
  69. (poll channel (lambda args (response=? channel "pong"))))))))))
  70. (test-assert-with-log "open-remote-pipe, OPEN_BOTH"
  71. (run-client-test
  72. (lambda (server)
  73. (start-server/exec server (const #t)))
  74. (lambda ()
  75. (call-with-connected-session/shell
  76. (lambda (session)
  77. (let ((channel (open-remote-pipe session "ping" OPEN_BOTH)))
  78. (format-log/scm 'nolog "open-remote-pipe, OPEN_BOTH" "channel: ~A" channel)
  79. (and (input-port? channel)
  80. (output-port? channel)
  81. (poll channel (lambda args (response=? channel "pong"))))))))))
  82. (test-assert-with-log "open-remote-pipe*"
  83. (run-client-test
  84. (lambda (server)
  85. (start-server/exec server (const #t)))
  86. (lambda ()
  87. (call-with-connected-session/shell
  88. (lambda (session)
  89. (let ((channel (open-remote-pipe* session OPEN_READ "ping")))
  90. (format-log/scm 'nolog "open-remote-pipe*" "channel: ~A" channel)
  91. (and (input-only? channel)
  92. (poll channel (lambda args (response=? channel "pong"))))))))))
  93. (test-assert-with-log "open-remote-input-pipe"
  94. (run-client-test
  95. (lambda (server)
  96. (start-server/exec server (const #t)))
  97. (lambda ()
  98. (call-with-connected-session/shell
  99. (lambda (session)
  100. (let ((channel (open-remote-input-pipe session "ping")))
  101. (format-log/scm 'nolog "open-remote-input-pipe" "channel: ~A" channel)
  102. (and (input-only? channel)
  103. (poll channel (lambda args (response=? channel "pong"))))))))))
  104. (test-assert-with-log "open-remote-output-pipe"
  105. (run-client-test
  106. (lambda (server)
  107. (start-server/exec server (const #t)))
  108. (lambda ()
  109. (call-with-connected-session/shell
  110. (lambda (session)
  111. (let ((channel (open-remote-output-pipe session "ping")))
  112. (format-log/scm 'nolog "open-remote-output-pipe" "channel: ~A" channel)
  113. (output-only? channel)))))))
  114. (test-end "popen")
  115. (exit (= (test-runner-fail-count (test-runner-current)) 0))
  116. ;;; popen.scm ends here.