file-sharing.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. (mkdir #$output)
  80. (chdir #$output)
  81. (test-begin "transmission-daemon")
  82. ;; Make sure the "transmission" user and group have been created.
  83. (test-assert "\"transmission\" user exists"
  84. (marionette-eval
  85. '(begin
  86. (getpwnam #$%transmission-daemon-user)
  87. #t)
  88. marionette))
  89. (test-assert "\"transmission\" group exists"
  90. (marionette-eval
  91. '(begin
  92. (getgrnam #$%transmission-daemon-group)
  93. #t)
  94. marionette))
  95. ;; Make sure Transmission Daemon's configuration directory has been
  96. ;; created with the correct ownership and permissions.
  97. (test-assert "configuration directory exists"
  98. (marionette-eval
  99. '(eq? (stat:type (stat #$%transmission-daemon-config-dir))
  100. 'directory)
  101. marionette))
  102. (test-assert "configuration directory has correct ownership"
  103. (marionette-eval
  104. '(let ((config-dir (stat #$%transmission-daemon-config-dir))
  105. (transmission-user (getpwnam #$%transmission-daemon-user)))
  106. (and (eqv? (stat:uid config-dir)
  107. (passwd:uid transmission-user))
  108. (eqv? (stat:gid config-dir)
  109. (passwd:gid transmission-user))))
  110. marionette))
  111. (test-assert "configuration directory has expected permissions"
  112. (marionette-eval
  113. '(eqv? (stat:perms (stat #$%transmission-daemon-config-dir))
  114. #o750)
  115. marionette))
  116. ;; Make sure the incomplete-downloads and watch directories have been
  117. ;; created with the correct ownership and permissions.
  118. (test-assert "incomplete-downloads directory exists"
  119. (marionette-eval
  120. '(eq? (stat:type (stat #$%transmission-daemon-incomplete-dir))
  121. 'directory)
  122. marionette))
  123. (test-assert "incomplete-downloads directory has correct ownership"
  124. (marionette-eval
  125. '(let ((incomplete-dir
  126. (stat #$%transmission-daemon-incomplete-dir))
  127. (transmission-user
  128. (getpwnam #$%transmission-daemon-user)))
  129. (and (eqv? (stat:uid incomplete-dir)
  130. (passwd:uid transmission-user))
  131. (eqv? (stat:gid incomplete-dir)
  132. (passwd:gid transmission-user))))
  133. marionette))
  134. (test-assert
  135. "incomplete-downloads directory has expected permissions"
  136. (marionette-eval
  137. '(eqv? (stat:perms (stat #$%transmission-daemon-incomplete-dir))
  138. #o750)
  139. marionette))
  140. (test-assert "watch directory exists"
  141. (marionette-eval
  142. '(eq? (stat:type (stat #$%transmission-daemon-watch-dir))
  143. 'directory)
  144. marionette))
  145. (test-assert "watch directory has correct ownership"
  146. (marionette-eval
  147. '(let ((watch-dir (stat #$%transmission-daemon-watch-dir))
  148. (transmission-user (getpwnam #$%transmission-daemon-user)))
  149. (and (eqv? (stat:uid watch-dir)
  150. (passwd:uid transmission-user))
  151. (eqv? (stat:gid watch-dir)
  152. (passwd:gid transmission-user))))
  153. marionette))
  154. (test-assert "watch directory has expected permissions"
  155. (marionette-eval
  156. '(eqv? (stat:perms (stat #$%transmission-daemon-watch-dir))
  157. #o770)
  158. marionette))
  159. ;; Make sure the settings file has been created and appears valid.
  160. (test-assert "settings file exists"
  161. (marionette-eval
  162. '(file-exists? #$%transmission-daemon-settings-file)
  163. marionette))
  164. (test-assert "settings file is valid JSON"
  165. (marionette-eval
  166. '(begin
  167. (use-modules (json parser))
  168. (with-input-from-file #$%transmission-daemon-settings-file
  169. (lambda ()
  170. (json->scm)))
  171. #t)
  172. marionette))
  173. (test-assert "settings file contains a non-empty JSON object"
  174. (marionette-eval
  175. '(begin
  176. (use-modules (json parser)
  177. (srfi srfi-1))
  178. (let ((settings (with-input-from-file
  179. #$%transmission-daemon-settings-file
  180. (lambda ()
  181. (json->scm)))))
  182. (and (list? settings)
  183. (not (null? settings))
  184. (every pair? settings))))
  185. marionette))
  186. ;; Make sure Transmission Daemon is running.
  187. (test-assert "transmission-daemon is running"
  188. (marionette-eval
  189. '(begin
  190. (use-modules (gnu services herd))
  191. (live-service-running
  192. (find (lambda (live-service)
  193. (memq 'transmission-daemon
  194. (live-service-provision live-service)))
  195. (current-services))))
  196. marionette))
  197. ;; Make sure the daemon is listening for peer connections.
  198. (test-assert "transmission-daemon is listening for peers"
  199. (wait-for-tcp-port #$%transmission-daemon-peer-port marionette))
  200. ;; Make sure the daemon is listening for RPC-client connections.
  201. (test-assert "transmission-daemon is listening for RPC clients"
  202. (wait-for-tcp-port #$%transmission-daemon-rpc-port marionette))
  203. ;; Make sure the RPC-authentication settings are honored.
  204. (test-assert "transmission-daemon requires RPC authentication"
  205. (let ((transmission-remote
  206. (string-append #+transmission "/bin/transmission-remote")))
  207. (with-error-to-port (%make-void-port "w")
  208. (lambda ()
  209. (not (zero? (system* transmission-remote
  210. "--session-info")))))))
  211. (test-assert "transmission-daemon rejects incorrect RPC credentials"
  212. (let ((transmission-remote
  213. (string-append #+transmission "/bin/transmission-remote"))
  214. (wrong-auth-string
  215. (string-append #$%transmission-daemon-rpc-username
  216. ":"
  217. "wrong-"
  218. #$%transmission-daemon-rpc-password)))
  219. (with-error-to-port (%make-void-port "w")
  220. (lambda ()
  221. (not (zero? (system* transmission-remote
  222. "--auth" wrong-auth-string
  223. "--session-info")))))))
  224. (test-assert "transmission-daemon accepts correct RPC credentials"
  225. (let ((transmission-remote
  226. (string-append #+transmission "/bin/transmission-remote"))
  227. (auth-string
  228. (string-append #$%transmission-daemon-rpc-username
  229. ":"
  230. #$%transmission-daemon-rpc-password)))
  231. (with-output-to-port (%make-void-port "w")
  232. (lambda ()
  233. (zero? (system* transmission-remote
  234. "--auth" auth-string
  235. "--session-info"))))))
  236. (test-end)
  237. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  238. (gexp->derivation "transmission-daemon-test" test))
  239. (define %test-transmission-daemon
  240. (system-test
  241. (name "transmission-daemon")
  242. (description "Test a running Transmission Daemon service.")
  243. (value (run-transmission-daemon-test))))