messaging.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
  3. ;;; Copyright © 2017-2018, 2021-2022 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2018 Efraim Flashner <efraim@flashner.co.il>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu tests messaging)
  21. #:use-module (gnu tests)
  22. #:use-module (gnu system)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services messaging)
  26. #:use-module (gnu services networking)
  27. #:use-module (gnu packages messaging)
  28. #:use-module (guix gexp)
  29. #:use-module (guix store)
  30. #:use-module (guix modules)
  31. #:export (%test-prosody
  32. %test-bitlbee
  33. %test-quassel))
  34. (define (run-xmpp-test name xmpp-service pid-file create-account)
  35. "Run a test of an OS running XMPP-SERVICE, which writes its PID to PID-FILE."
  36. (define os
  37. (marionette-operating-system
  38. (simple-operating-system (service dhcp-client-service-type)
  39. xmpp-service)
  40. #:imported-modules '((gnu services herd))))
  41. (define port 15222)
  42. (define vm
  43. (virtual-machine
  44. (operating-system os)
  45. (port-forwardings `((,port . 5222)))))
  46. (define username "alice")
  47. (define server "localhost")
  48. (define jid (string-append username "@" server))
  49. (define password "correct horse battery staple")
  50. (define message "hello world")
  51. (define witness "/tmp/freetalk-witness")
  52. (define script.ft
  53. (scheme-file
  54. "script.ft"
  55. #~(begin
  56. (define (handle-received-message time from nickname message)
  57. (define (touch file-name)
  58. (call-with-output-file file-name (const #t)))
  59. (when (equal? message #$message)
  60. (touch #$witness)))
  61. (add-hook! ft-message-receive-hook handle-received-message)
  62. (ft-set-jid! #$jid)
  63. (ft-set-password! #$password)
  64. (ft-set-server! #$server)
  65. (ft-set-port! #$port)
  66. (ft-set-sslconn! #f)
  67. (ft-connect-blocking)
  68. (ft-send-message #$jid #$message)
  69. (ft-set-daemon)
  70. (ft-main-loop))))
  71. (define test
  72. (with-imported-modules '((gnu build marionette))
  73. #~(begin
  74. (use-modules (gnu build marionette)
  75. (srfi srfi-64))
  76. (define marionette
  77. (make-marionette (list #$vm)))
  78. (define (host-wait-for-file file)
  79. ;; Wait until FILE exists in the host.
  80. (let loop ((i 60))
  81. (cond ((file-exists? file)
  82. #t)
  83. ((> i 0)
  84. (begin
  85. (sleep 1))
  86. (loop (- i 1)))
  87. (else
  88. (error "file didn't show up" file)))))
  89. (test-runner-current (system-test-runner #$output))
  90. (test-begin "xmpp")
  91. ;; Wait for XMPP service to be up and running.
  92. (test-assert "service running"
  93. (marionette-eval
  94. '(begin
  95. (use-modules (gnu services herd))
  96. (start-service 'xmpp-daemon))
  97. marionette))
  98. ;; Check XMPP service's PID.
  99. (test-assert "service process id"
  100. (let ((pid (number->string (wait-for-file #$pid-file
  101. marionette))))
  102. (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
  103. marionette)))
  104. ;; Alice sends an XMPP message to herself, with Freetalk.
  105. (test-assert "client-to-server communication"
  106. (let ((freetalk-bin (string-append #$freetalk "/bin/freetalk")))
  107. (marionette-eval '(system* #$create-account #$jid #$password)
  108. marionette)
  109. ;; Freetalk requires write access to $HOME.
  110. (setenv "HOME" "/tmp")
  111. (system* freetalk-bin "-s" #$script.ft)
  112. (host-wait-for-file #$witness)))
  113. (test-end))))
  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)))))
  141. ;;;
  142. ;;; BitlBee.
  143. ;;;
  144. (define (run-bitlbee-test)
  145. (define os
  146. (marionette-operating-system
  147. (simple-operating-system (service dhcp-client-service-type)
  148. (service bitlbee-service-type
  149. (bitlbee-configuration
  150. (interface "0.0.0.0"))))
  151. #:imported-modules (source-module-closure
  152. '((gnu services herd)))))
  153. (define vm
  154. (virtual-machine
  155. (operating-system os)
  156. (port-forwardings `((6667 . 6667)))))
  157. (define test
  158. (with-imported-modules '((gnu build marionette))
  159. #~(begin
  160. (use-modules (ice-9 rdelim)
  161. (srfi srfi-64)
  162. (gnu build marionette))
  163. (define marionette
  164. (make-marionette (list #$vm)))
  165. (test-runner-current (system-test-runner #$output))
  166. (test-begin "bitlbee")
  167. (test-assert "service started"
  168. (marionette-eval
  169. '(begin
  170. (use-modules (gnu services herd))
  171. (start-service 'bitlbee))
  172. marionette))
  173. (test-assert "connect"
  174. (let* ((address (make-socket-address AF_INET INADDR_LOOPBACK
  175. 6667))
  176. (sock (socket AF_INET SOCK_STREAM 0)))
  177. (connect sock address)
  178. ;; See <https://tools.ietf.org/html/rfc1459>.
  179. (->bool (string-contains (pk 'message (read-line sock))
  180. "BitlBee"))))
  181. (test-end))))
  182. (gexp->derivation "bitlbee-test" test))
  183. (define %test-bitlbee
  184. (system-test
  185. (name "bitlbee")
  186. (description "Connect to a BitlBee IRC server.")
  187. (value (run-bitlbee-test))))
  188. (define (run-quassel-test)
  189. (define os
  190. (marionette-operating-system
  191. (simple-operating-system (service dhcp-client-service-type)
  192. (service quassel-service-type))
  193. #:imported-modules (source-module-closure
  194. '((gnu services herd)))))
  195. (define vm
  196. (virtual-machine
  197. (operating-system os)
  198. (port-forwardings `((4242 . 4242)))))
  199. (define test
  200. (with-imported-modules '((gnu build marionette))
  201. #~(begin
  202. (use-modules (srfi srfi-64)
  203. (gnu build marionette))
  204. (define marionette
  205. (make-marionette (list #$vm)))
  206. (test-runner-current (system-test-runner #$output))
  207. (test-begin "quassel")
  208. (test-assert "service started"
  209. (marionette-eval
  210. '(begin
  211. (use-modules (gnu services herd))
  212. (start-service 'quassel))
  213. marionette))
  214. (test-assert "certificate file"
  215. (marionette-eval
  216. '(file-exists? "/var/lib/quassel/quasselCert.pem")
  217. marionette))
  218. (test-end))))
  219. (gexp->derivation "quassel-test" test))
  220. (define %test-quassel
  221. (system-test
  222. (name "quassel")
  223. (description "Connect to a quassel IRC server.")
  224. (value (run-quassel-test))))