messaging.scm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  3. ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu tests messaging)
  20. #:use-module (gnu tests)
  21. #:use-module (gnu system)
  22. #:use-module (gnu system vm)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services messaging)
  25. #:use-module (gnu services networking)
  26. #:use-module (gnu packages messaging)
  27. #:use-module (guix gexp)
  28. #:use-module (guix store)
  29. #:export (%test-prosody))
  30. (define (run-xmpp-test name xmpp-service pid-file create-account)
  31. "Run a test of an OS running XMPP-SERVICE, which writes its PID to PID-FILE."
  32. (define os
  33. (marionette-operating-system
  34. (simple-operating-system (dhcp-client-service)
  35. xmpp-service)
  36. #:imported-modules '((gnu services herd))))
  37. (define port 15222)
  38. (define vm
  39. (virtual-machine
  40. (operating-system os)
  41. (port-forwardings `((,port . 5222)))))
  42. (define username "alice")
  43. (define server "localhost")
  44. (define jid (string-append username "@" server))
  45. (define password "correct horse battery staple")
  46. (define message "hello world")
  47. (define witness "/tmp/freetalk-witness")
  48. (define script.ft
  49. (scheme-file
  50. "script.ft"
  51. #~(begin
  52. (define (handle-received-message time from nickname message)
  53. (define (touch file-name)
  54. (call-with-output-file file-name (const #t)))
  55. (when (equal? message #$message)
  56. (touch #$witness)))
  57. (add-hook! ft-message-receive-hook handle-received-message)
  58. (ft-set-jid! #$jid)
  59. (ft-set-password! #$password)
  60. (ft-set-server! #$server)
  61. (ft-set-port! #$port)
  62. (ft-set-sslconn! #f)
  63. (ft-connect-blocking)
  64. (ft-send-message #$jid #$message)
  65. (ft-set-daemon)
  66. (ft-main-loop))))
  67. (define test
  68. (with-imported-modules '((gnu build marionette))
  69. #~(begin
  70. (use-modules (gnu build marionette)
  71. (srfi srfi-64))
  72. (define marionette
  73. (make-marionette (list #$vm)))
  74. (define (host-wait-for-file file)
  75. ;; Wait until FILE exists in the host.
  76. (let loop ((i 60))
  77. (cond ((file-exists? file)
  78. #t)
  79. ((> i 0)
  80. (begin
  81. (sleep 1))
  82. (loop (- i 1)))
  83. (else
  84. (error "file didn't show up" file)))))
  85. (mkdir #$output)
  86. (chdir #$output)
  87. (test-begin "xmpp")
  88. ;; Wait for XMPP service to be up and running.
  89. (test-eq "service running"
  90. 'running!
  91. (marionette-eval
  92. '(begin
  93. (use-modules (gnu services herd))
  94. (start-service 'xmpp-daemon)
  95. 'running!)
  96. marionette))
  97. ;; Check XMPP service's PID.
  98. (test-assert "service process id"
  99. (let ((pid (number->string (wait-for-file #$pid-file
  100. marionette))))
  101. (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
  102. marionette)))
  103. ;; Alice sends an XMPP message to herself, with Freetalk.
  104. (test-assert "client-to-server communication"
  105. (let ((freetalk-bin (string-append #$freetalk "/bin/freetalk")))
  106. (marionette-eval '(system* #$create-account #$jid #$password)
  107. marionette)
  108. ;; Freetalk requires write access to $HOME.
  109. (setenv "HOME" "/tmp")
  110. (system* freetalk-bin "-s" #$script.ft)
  111. (host-wait-for-file #$witness)))
  112. (test-end)
  113. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  114. (gexp->derivation name test))
  115. (define %create-prosody-account
  116. (program-file
  117. "create-account"
  118. #~(begin
  119. (use-modules (ice-9 match))
  120. (match (command-line)
  121. ((command jid password)
  122. (let ((password-input (format #f "\"~a~%~a\"" password password))
  123. (prosodyctl #$(file-append prosody "/bin/prosodyctl")))
  124. (system (string-join
  125. `("echo" ,password-input "|" ,prosodyctl "adduser" ,jid)
  126. " "))))))))
  127. (define %test-prosody
  128. (let* ((config (prosody-configuration
  129. (disable-sasl-mechanisms '())
  130. (virtualhosts
  131. (list
  132. (virtualhost-configuration
  133. (domain "localhost")))))))
  134. (system-test
  135. (name "prosody")
  136. (description "Connect to a running Prosody daemon.")
  137. (value (run-xmpp-test name
  138. (service prosody-service-type config)
  139. (prosody-configuration-pidfile config)
  140. %create-prosody-account)))))