messaging.scm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
  3. ;;; Copyright © 2017, 2018 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. (mkdir #$output)
  90. (chdir #$output)
  91. (test-begin "xmpp")
  92. ;; Wait for XMPP service to be up and running.
  93. (test-assert "service running"
  94. (marionette-eval
  95. '(begin
  96. (use-modules (gnu services herd))
  97. (start-service 'xmpp-daemon))
  98. marionette))
  99. ;; Check XMPP service's PID.
  100. (test-assert "service process id"
  101. (let ((pid (number->string (wait-for-file #$pid-file
  102. marionette))))
  103. (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
  104. marionette)))
  105. ;; Alice sends an XMPP message to herself, with Freetalk.
  106. (test-assert "client-to-server communication"
  107. (let ((freetalk-bin (string-append #$freetalk "/bin/freetalk")))
  108. (marionette-eval '(system* #$create-account #$jid #$password)
  109. marionette)
  110. ;; Freetalk requires write access to $HOME.
  111. (setenv "HOME" "/tmp")
  112. (system* freetalk-bin "-s" #$script.ft)
  113. (host-wait-for-file #$witness)))
  114. (test-end)
  115. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  116. (gexp->derivation name test))
  117. (define %create-prosody-account
  118. (program-file
  119. "create-account"
  120. #~(begin
  121. (use-modules (ice-9 match))
  122. (match (command-line)
  123. ((command jid password)
  124. (let ((password-input (format #f "\"~a~%~a\"" password password))
  125. (prosodyctl #$(file-append prosody "/bin/prosodyctl")))
  126. (system (string-join
  127. `("echo" ,password-input "|" ,prosodyctl "adduser" ,jid)
  128. " "))))))))
  129. (define %test-prosody
  130. (let* ((config (prosody-configuration
  131. (disable-sasl-mechanisms '())
  132. (virtualhosts
  133. (list
  134. (virtualhost-configuration
  135. (domain "localhost")))))))
  136. (system-test
  137. (name "prosody")
  138. (description "Connect to a running Prosody daemon.")
  139. (value (run-xmpp-test name
  140. (service prosody-service-type config)
  141. (prosody-configuration-pidfile config)
  142. %create-prosody-account)))))
  143. ;;;
  144. ;;; BitlBee.
  145. ;;;
  146. (define (run-bitlbee-test)
  147. (define os
  148. (marionette-operating-system
  149. (simple-operating-system (service dhcp-client-service-type)
  150. (service bitlbee-service-type
  151. (bitlbee-configuration
  152. (interface "0.0.0.0"))))
  153. #:imported-modules (source-module-closure
  154. '((gnu services herd)))))
  155. (define vm
  156. (virtual-machine
  157. (operating-system os)
  158. (port-forwardings `((6667 . 6667)))))
  159. (define test
  160. (with-imported-modules '((gnu build marionette))
  161. #~(begin
  162. (use-modules (ice-9 rdelim)
  163. (srfi srfi-64)
  164. (gnu build marionette))
  165. (define marionette
  166. (make-marionette (list #$vm)))
  167. (mkdir #$output)
  168. (chdir #$output)
  169. (test-begin "bitlbee")
  170. (test-assert "service started"
  171. (marionette-eval
  172. '(begin
  173. (use-modules (gnu services herd))
  174. (start-service 'bitlbee))
  175. marionette))
  176. (test-equal "valid PID"
  177. #$(file-append bitlbee "/sbin/bitlbee")
  178. (marionette-eval
  179. '(begin
  180. (use-modules (srfi srfi-1)
  181. (gnu services herd))
  182. (let ((bitlbee
  183. (find (lambda (service)
  184. (equal? '(bitlbee)
  185. (live-service-provision service)))
  186. (current-services))))
  187. (and (pk 'bitlbee-service bitlbee)
  188. (let ((pid (live-service-running bitlbee)))
  189. (readlink (string-append "/proc/"
  190. (number->string pid)
  191. "/exe"))))))
  192. marionette))
  193. (test-assert "connect"
  194. (let* ((address (make-socket-address AF_INET INADDR_LOOPBACK
  195. 6667))
  196. (sock (socket AF_INET SOCK_STREAM 0)))
  197. (connect sock address)
  198. ;; See <https://tools.ietf.org/html/rfc1459>.
  199. (->bool (string-contains (pk 'message (read-line sock))
  200. "BitlBee"))))
  201. (test-end)
  202. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  203. (gexp->derivation "bitlbee-test" test))
  204. (define %test-bitlbee
  205. (system-test
  206. (name "bitlbee")
  207. (description "Connect to a BitlBee IRC server.")
  208. (value (run-bitlbee-test))))
  209. (define (run-quassel-test)
  210. (define os
  211. (marionette-operating-system
  212. (simple-operating-system (service dhcp-client-service-type)
  213. (service quassel-service-type))
  214. #:imported-modules (source-module-closure
  215. '((gnu services herd)))))
  216. (define vm
  217. (virtual-machine
  218. (operating-system os)
  219. (port-forwardings `((4242 . 4242)))))
  220. (define test
  221. (with-imported-modules '((gnu build marionette))
  222. #~(begin
  223. (use-modules (srfi srfi-64)
  224. (gnu build marionette))
  225. (define marionette
  226. (make-marionette (list #$vm)))
  227. (mkdir #$output)
  228. (chdir #$output)
  229. (test-begin "quassel")
  230. (test-assert "service started"
  231. (marionette-eval
  232. '(begin
  233. (use-modules (gnu services herd))
  234. (start-service 'quassel))
  235. marionette))
  236. (test-assert "certificate file"
  237. (marionette-eval
  238. '(file-exists? "/var/lib/quassel/quasselCert.pem")
  239. marionette))
  240. (test-end)
  241. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  242. (gexp->derivation "quassel-test" test))
  243. (define %test-quassel
  244. (system-test
  245. (name "quassel")
  246. (description "Connect to a quassel IRC server.")
  247. (value (run-quassel-test))))