file-sharing.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Simon South <simon@simonsouth.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix 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
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. ;;;
  19. ;;; The Transmission Daemon service.
  20. ;;;
  21. (define-module (gnu tests file-sharing)
  22. #:use-module (gnu packages bittorrent)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services file-sharing)
  25. #:use-module (gnu services networking)
  26. #:use-module (gnu system vm)
  27. #:use-module (gnu tests)
  28. #:use-module (guix gexp)
  29. #:export (%test-transmission-daemon))
  30. (define %transmission-daemon-user "transmission")
  31. (define %transmission-daemon-group "transmission")
  32. (define %transmission-daemon-config-dir "/var/lib/transmission-daemon")
  33. (define %transmission-daemon-watch-dir
  34. (string-append %transmission-daemon-config-dir "/watch"))
  35. (define %transmission-daemon-incomplete-dir
  36. (string-append %transmission-daemon-config-dir "/incomplete"))
  37. (define %transmission-daemon-settings-file
  38. (string-append %transmission-daemon-config-dir "/settings.json"))
  39. (define %transmission-daemon-peer-port 51000) ; default is 51413
  40. (define %transmission-daemon-rpc-port 9999) ; default is 9091
  41. (define %transmission-daemon-rpc-username "test-username")
  42. (define %transmission-daemon-rpc-password "test-password")
  43. (define %transmission-daemon-test-configuration
  44. (transmission-daemon-configuration
  45. (incomplete-dir-enabled? #t)
  46. (incomplete-dir %transmission-daemon-incomplete-dir)
  47. (watch-dir-enabled? #t)
  48. (watch-dir %transmission-daemon-watch-dir)
  49. (peer-port-random-on-start? #f)
  50. (peer-port %transmission-daemon-peer-port)
  51. (rpc-enabled? #t)
  52. (rpc-port %transmission-daemon-rpc-port)
  53. (rpc-whitelist-enabled? #f)
  54. (rpc-authentication-required? #t)
  55. (rpc-username %transmission-daemon-rpc-username)
  56. (rpc-password (transmission-password-hash %transmission-daemon-rpc-password
  57. "yEK0q3.X"))))
  58. (define (run-transmission-daemon-test)
  59. (define os
  60. (marionette-operating-system
  61. (simple-operating-system
  62. (service dhcp-client-service-type)
  63. (service transmission-daemon-service-type
  64. %transmission-daemon-test-configuration))
  65. #:imported-modules '((gnu services herd)
  66. (json parser))
  67. #:requirements '(transmission-daemon)))
  68. (define test
  69. (with-imported-modules '((gnu build marionette))
  70. #~(begin
  71. (use-modules (gnu build marionette)
  72. (srfi srfi-64))
  73. (define marionette
  74. (make-marionette
  75. (list #$(virtual-machine
  76. (operating-system os)
  77. (port-forwardings
  78. `((9091 . ,%transmission-daemon-rpc-port)))))))
  79. (test-runner-current (system-test-runner #$output))
  80. (test-begin "transmission-daemon")
  81. ;; Make sure the "transmission" user and group have been created.
  82. (test-assert "\"transmission\" user exists"
  83. (marionette-eval
  84. '(begin
  85. (getpwnam #$%transmission-daemon-user)
  86. #t)
  87. marionette))
  88. (test-assert "\"transmission\" group exists"
  89. (marionette-eval
  90. '(begin
  91. (getgrnam #$%transmission-daemon-group)
  92. #t)
  93. marionette))
  94. ;; Make sure Transmission Daemon's configuration directory has been
  95. ;; created with the correct ownership and permissions.
  96. (test-assert "configuration directory exists"
  97. (marionette-eval
  98. '(eq? (stat:type (stat #$%transmission-daemon-config-dir))
  99. 'directory)
  100. marionette))
  101. (test-assert "configuration directory has correct ownership"
  102. (marionette-eval
  103. '(let ((config-dir (stat #$%transmission-daemon-config-dir))
  104. (transmission-user (getpwnam #$%transmission-daemon-user)))
  105. (and (eqv? (stat:uid config-dir)
  106. (passwd:uid transmission-user))
  107. (eqv? (stat:gid config-dir)
  108. (passwd:gid transmission-user))))
  109. marionette))
  110. (test-assert "configuration directory has expected permissions"
  111. (marionette-eval
  112. '(eqv? (stat:perms (stat #$%transmission-daemon-config-dir))
  113. #o750)
  114. marionette))
  115. ;; Make sure the incomplete-downloads and watch directories have been
  116. ;; created with the correct ownership and permissions.
  117. (test-assert "incomplete-downloads directory exists"
  118. (marionette-eval
  119. '(eq? (stat:type (stat #$%transmission-daemon-incomplete-dir))
  120. 'directory)
  121. marionette))
  122. (test-assert "incomplete-downloads directory has correct ownership"
  123. (marionette-eval
  124. '(let ((incomplete-dir
  125. (stat #$%transmission-daemon-incomplete-dir))
  126. (transmission-user
  127. (getpwnam #$%transmission-daemon-user)))
  128. (and (eqv? (stat:uid incomplete-dir)
  129. (passwd:uid transmission-user))
  130. (eqv? (stat:gid incomplete-dir)
  131. (passwd:gid transmission-user))))
  132. marionette))
  133. (test-assert
  134. "incomplete-downloads directory has expected permissions"
  135. (marionette-eval
  136. '(eqv? (stat:perms (stat #$%transmission-daemon-incomplete-dir))
  137. #o750)
  138. marionette))
  139. (test-assert "watch directory exists"
  140. (marionette-eval
  141. '(eq? (stat:type (stat #$%transmission-daemon-watch-dir))
  142. 'directory)
  143. marionette))
  144. (test-assert "watch directory has correct ownership"
  145. (marionette-eval
  146. '(let ((watch-dir (stat #$%transmission-daemon-watch-dir))
  147. (transmission-user (getpwnam #$%transmission-daemon-user)))
  148. (and (eqv? (stat:uid watch-dir)
  149. (passwd:uid transmission-user))
  150. (eqv? (stat:gid watch-dir)
  151. (passwd:gid transmission-user))))
  152. marionette))
  153. (test-assert "watch directory has expected permissions"
  154. (marionette-eval
  155. '(eqv? (stat:perms (stat #$%transmission-daemon-watch-dir))
  156. #o770)
  157. marionette))
  158. ;; Make sure the settings file has been created and appears valid.
  159. (test-assert "settings file exists"
  160. (marionette-eval
  161. '(file-exists? #$%transmission-daemon-settings-file)
  162. marionette))
  163. (test-assert "settings file is valid JSON"
  164. (marionette-eval
  165. '(begin
  166. (use-modules (json parser))
  167. (with-input-from-file #$%transmission-daemon-settings-file
  168. (lambda ()
  169. (json->scm)))
  170. #t)
  171. marionette))
  172. (test-assert "settings file contains a non-empty JSON object"
  173. (marionette-eval
  174. '(begin
  175. (use-modules (json parser)
  176. (srfi srfi-1))
  177. (let ((settings (with-input-from-file
  178. #$%transmission-daemon-settings-file
  179. (lambda ()
  180. (json->scm)))))
  181. (and (list? settings)
  182. (not (null? settings))
  183. (every pair? settings))))
  184. marionette))
  185. ;; Make sure Transmission Daemon is running.
  186. (test-assert "transmission-daemon is running"
  187. (marionette-eval
  188. '(begin
  189. (use-modules (gnu services herd))
  190. (live-service-running
  191. (find (lambda (live-service)
  192. (memq 'transmission-daemon
  193. (live-service-provision live-service)))
  194. (current-services))))
  195. marionette))
  196. ;; Make sure the daemon is listening for peer connections.
  197. (test-assert "transmission-daemon is listening for peers"
  198. (wait-for-tcp-port #$%transmission-daemon-peer-port marionette))
  199. ;; Make sure the daemon is listening for RPC-client connections.
  200. (test-assert "transmission-daemon is listening for RPC clients"
  201. (wait-for-tcp-port #$%transmission-daemon-rpc-port marionette))
  202. ;; Make sure the RPC-authentication settings are honored.
  203. (test-assert "transmission-daemon requires RPC authentication"
  204. (let ((transmission-remote
  205. (string-append #+transmission "/bin/transmission-remote")))
  206. (with-error-to-port (%make-void-port "w")
  207. (lambda ()
  208. (not (zero? (system* transmission-remote
  209. "--session-info")))))))
  210. (test-assert "transmission-daemon rejects incorrect RPC credentials"
  211. (let ((transmission-remote
  212. (string-append #+transmission "/bin/transmission-remote"))
  213. (wrong-auth-string
  214. (string-append #$%transmission-daemon-rpc-username
  215. ":"
  216. "wrong-"
  217. #$%transmission-daemon-rpc-password)))
  218. (with-error-to-port (%make-void-port "w")
  219. (lambda ()
  220. (not (zero? (system* transmission-remote
  221. "--auth" wrong-auth-string
  222. "--session-info")))))))
  223. (test-assert "transmission-daemon accepts correct RPC credentials"
  224. (let ((transmission-remote
  225. (string-append #+transmission "/bin/transmission-remote"))
  226. (auth-string
  227. (string-append #$%transmission-daemon-rpc-username
  228. ":"
  229. #$%transmission-daemon-rpc-password)))
  230. (with-output-to-port (%make-void-port "w")
  231. (lambda ()
  232. (zero? (system* transmission-remote
  233. "--auth" auth-string
  234. "--session-info"))))))
  235. (test-end))))
  236. (gexp->derivation "transmission-daemon-test" test))
  237. (define %test-transmission-daemon
  238. (system-test
  239. (name "transmission-daemon")
  240. (description "Test a running Transmission Daemon service.")
  241. (value (run-transmission-daemon-test))))