sssh-ssshd.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; sssh-ssshd.scm -- Communication between sssh and ssshd.
  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. (ice-9 popen)
  21. (ice-9 rdelim)
  22. (ice-9 regex)
  23. ;; Helper procedures
  24. (tests common))
  25. (test-begin "sssh-ssshd")
  26. ;;;
  27. (define *test-cmd* "uname --all")
  28. (define *srv-address* INADDR_LOOPBACK)
  29. (define *srv-port* 12600)
  30. (define *srv-pid-file* "ssshd.pid")
  31. (define *ssshd-cmd*
  32. (string-append
  33. %topbuilddir "/examples/ssshd.scm --detach"
  34. " --pid-file=" *srv-pid-file*
  35. " --port=" (number->string *srv-port*)
  36. " --rsakey=" %rsakey
  37. " --dsakey=" %dsakey))
  38. (define *sssh-cmd*
  39. (string-append
  40. %topbuilddir "/examples/sssh.scm"
  41. " --identity-file=" %rsakey
  42. " --port=" (number->string *srv-port*)
  43. " --known-hosts-file=" %knownhosts
  44. " " (inet-ntop AF_INET *srv-address*)
  45. " '" *test-cmd* "'"))
  46. (setenv "GUILE_LOAD_PATH" (string-append %topdir "/modules"))
  47. ;; We must unset `SSH_AUTH_SOCK' to prevent sssh from asking SSH agent
  48. ;; (if it is present) for keys.
  49. (unsetenv "SSH_AUTH_SOCK")
  50. (define ssshd-pid #f)
  51. ;;; Tests
  52. (test-assert "ssshd, start"
  53. (let ((*max-tries* 10))
  54. (system *ssshd-cmd*)
  55. (let wait-pid-file ((exists? #f)
  56. (sleep-time 1) ;s
  57. (try 1))
  58. (if exists?
  59. (let* ((p (open-input-file *srv-pid-file*))
  60. (pid (read-line p)))
  61. (set! ssshd-pid (string->number pid)))
  62. (if (<= try *max-tries*)
  63. (begin
  64. (sleep sleep-time)
  65. (wait-pid-file (file-exists? *srv-pid-file*)
  66. (1+ sleep-time)
  67. (1+ try)))
  68. (format #t "Couldn't read a PID file ~a in ~a tries.~%"
  69. *srv-pid-file* try))))
  70. (sleep 1)
  71. ssshd-pid))
  72. (test-assert "sssh, exec"
  73. (let ((output (read-line (open-input-pipe *test-cmd*)))
  74. (p (open-input-pipe *sssh-cmd*))
  75. (res #f))
  76. (let r ((l (read-line p)))
  77. (if (not (eof-object? l))
  78. (if (string=? output l)
  79. (set! res #t)
  80. (r (read-line p)))))
  81. ;; Cleanup
  82. (and ssshd-pid
  83. (kill ssshd-pid SIGTERM))
  84. (and (file-exists? *srv-pid-file*)
  85. (delete-file *srv-pid-file*))
  86. ;; Return the result
  87. res))
  88. (test-end "sssh-ssshd")
  89. (exit (= (test-runner-fail-count (test-runner-current)) 0))
  90. ;;; sssh-ssshd.scm ends here.