@c The GNU Free Documentation License. @center Version 1.3, 3 November 2008 @c This file is intended to be included within another document, @c hence no sectioning command or @node. @display Copyright @copyright{} 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc. @uref{https://fsf.org/} Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. @end display @enumerate 0 @item PREAMBLE The purpose of this License is to make a manual, textbook, or other functional and useful document @dfn{free} in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others. This License is a kind of ``copyleft'', which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software. We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference. @item APPLICABILITY AND DEFINITIONS This License applies to any manual or other work, in any medium, that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions state
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Simon South <simon@simonsouth.net>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

;;;
;;; The Transmission Daemon service.
;;;

(define-module (gnu tests file-sharing)
  #:use-module (gnu packages bittorrent)
  #:use-module (gnu services)
  #:use-module (gnu services file-sharing)
  #:use-module (gnu services networking)
  #:use-module (gnu system vm)
  #:use-module (gnu tests)
  #:use-module (guix gexp)
  #:export (%test-transmission-daemon))

(define %transmission-daemon-user "transmission")
(define %transmission-daemon-group "transmission")

(define %transmission-daemon-config-dir "/var/lib/transmission-daemon")
(define %transmission-daemon-watch-dir
  (string-append %transmission-daemon-config-dir "/watch"))
(define %transmission-daemon-incomplete-dir
  (string-append %transmission-daemon-config-dir "/incomplete"))

(define %transmission-daemon-settings-file
  (string-append %transmission-daemon-config-dir "/settings.json"))

(define %transmission-daemon-peer-port 51000) ; default is 51413

(define %transmission-daemon-rpc-port 9999)   ; default is 9091
(define %transmission-daemon-rpc-username "test-username")
(define %transmission-daemon-rpc-password "test-password")

(define %transmission-daemon-test-configuration
  (transmission-daemon-configuration
   (incomplete-dir-enabled? #t)
   (incomplete-dir %transmission-daemon-incomplete-dir)

   (watch-dir-enabled? #t)
   (watch-dir %transmission-daemon-watch-dir)

   (peer-port-random-on-start? #f)
   (peer-port %transmission-daemon-peer-port)

   (rpc-enabled? #t)
   (rpc-port %transmission-daemon-rpc-port)
   (rpc-whitelist-enabled? #f)
   (rpc-authentication-required? #t)
   (rpc-username %transmission-daemon-rpc-username)
   (rpc-password (transmission-password-hash %transmission-daemon-rpc-password
                                             "yEK0q3.X"))))

(define (run-transmission-daemon-test)
  (define os
    (marionette-operating-system
     (simple-operating-system
      (service dhcp-client-service-type)
      (service transmission-daemon-service-type
               %transmission-daemon-test-configuration))
     #:imported-modules '((gnu services herd)
                          (json parser))
     #:requirements '(transmission-daemon)))

  (define test
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (gnu build marionette)
                       (srfi srfi-64))

          (define marionette
            (make-marionette
             (list #$(virtual-machine
                      (operating-system os)
                      (port-forwardings
                       `((9091 . ,%transmission-daemon-rpc-port)))))))

          (test-runner-current (system-test-runner #$output))
          (test-begin "transmission-daemon")

          ;; Make sure the "transmission" user and group have been created.
          (test-assert "\"transmission\" user exists"
            (marionette-eval
             '(begin
                (getpwnam #$%transmission-daemon-user)
                #t)
             marionette))
          (test-assert "\"transmission\" group exists"
            (marionette-eval
             '(begin
                (getgrnam #$%transmission-daemon-group)
                #t)
             marionette))

          ;; Make sure Transmission Daemon's configuration directory has been
          ;; created with the correct ownership and permissions.
          (test-assert "configuration directory exists"
            (marionette-eval
             '(eq? (stat:type (stat #$%transmission-daemon-config-dir))
                   'directory)
             marionette))
          (test-assert "configuration directory has correct ownership"
            (marionette-eval
             '(let ((config-dir (stat #$%transmission-daemon-config-dir))
                    (transmission-user (getpwnam #$%transmission-daemon-user)))
                (and (eqv? (stat:uid config-dir)
                           (passwd:uid transmission-user))
                     (eqv? (stat:gid config-dir)
                           (passwd:gid transmission-user))))
             marionette))
          (test-assert "configuration directory has expected permissions"
            (marionette-eval
             '(eqv? (stat:perms (stat #$%transmission-daemon-config-dir))
                    #o750)
             marionette))

          ;; Make sure the incomplete-downloads and watch directories have been
          ;; created with the correct ownership and permissions.
          (test-assert "incomplete-downloads directory exists"
            (marionette-eval
             '(eq? (stat:type (stat #$%transmission-daemon-incomplete-dir))
                   'directory)
             marionette))
          (test-assert "incomplete-downloads directory has correct ownership"
            (marionette-eval
             '(let ((incomplete-dir
                     (stat #$%transmission-daemon-incomplete-dir))
                    (transmission-user
                     (getpwnam #$%transmission-daemon-user)))
                (and (eqv? (stat:uid incomplete-dir)
                           (passwd:uid transmission-user))
                     (eqv? (stat:gid incomplete-dir)
                           (passwd:gid transmission-user))))
             marionette))
          (test-assert
              "incomplete-downloads directory has expected permissions"
            (marionette-eval
             '(eqv? (stat:perms (stat #$%transmission-daemon-incomplete-dir))
                    #o750)
             marionette))

          (test-assert "watch directory exists"
            (marionette-eval
             '(eq? (stat:type (stat #$%transmission-daemon-watch-dir))
                   'directory)
             marionette))
          (test-assert "watch directory has correct ownership"
            (marionette-eval
             '(let ((watch-dir (stat #$%transmission-daemon-watch-dir))
                    (transmission-user (getpwnam #$%transmission-daemon-user)))
                (and (eqv? (stat:uid watch-dir)
                           (passwd:uid transmission-user))
                     (eqv? (stat:gid watch-dir)
                           (passwd:gid transmission-user))))
             marionette))
          (test-assert "watch directory has expected permissions"
            (marionette-eval
             '(eqv? (stat:perms (stat #$%transmission-daemon-watch-dir))
                    #o770)
             marionette))

          ;; Make sure the settings file has been created and appears valid.
          (test-assert "settings file exists"
            (marionette-eval
             '(file-exists? #$%transmission-daemon-settings-file)
             marionette))
          (test-assert "settings file is valid JSON"
            (marionette-eval
             '(begin
                (use-modules (json parser))
                (with-input-from-file #$%transmission-daemon-settings-file
                  (lambda ()
                    (json->scm)))
                #t)
             marionette))
          (test-assert "settings file contains a non-empty JSON object"
            (marionette-eval
             '(begin
                (use-modules (json parser)
                             (srfi srfi-1))
                (let ((settings (with-input-from-file
                                    #$%transmission-daemon-settings-file
                                  (lambda ()
                                    (json->scm)))))
                  (and (list? settings)
                       (not (null? settings))
                       (every pair? settings))))
             marionette))

          ;; Make sure Transmission Daemon is running.
          (test-assert "transmission-daemon is running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (live-service-running
                 (find (lambda (live-service)
                         (memq 'transmission-daemon
                               (live-service-provision live-service)))
                       (current-services))))
             marionette))

          ;; Make sure the daemon is listening for peer connections.
          (test-assert "transmission-daemon is listening for peers"
            (wait-for-tcp-port #$%transmission-daemon-peer-port marionette))

          ;; Make sure the daemon is listening for RPC-client connections.
          (test-assert "transmission-daemon is listening for RPC clients"
            (wait-for-tcp-port #$%transmission-daemon-rpc-port marionette))

          ;; Make sure the RPC-authentication settings are honored.
          (test-assert "transmission-daemon requires RPC authentication"
            (let ((transmission-remote
                   (string-append #+transmission "/bin/transmission-remote")))
              (with-error-to-port (%make-void-port "w")
                (lambda ()
                  (not (zero? (system* transmission-remote
                                       "--session-info")))))))
          (test-assert "transmission-daemon rejects incorrect RPC credentials"
            (let ((transmission-remote
                   (string-append #+transmission "/bin/transmission-remote"))
                  (wrong-auth-string
                   (string-append #$%transmission-daemon-rpc-username
                                  ":"
                                  "wrong-"
                                  #$%transmission-daemon-rpc-password)))
              (with-error-to-port (%make-void-port "w")
                (lambda ()
                  (not (zero? (system* transmission-remote
                                       "--auth" wrong-auth-string
                                       "--session-info")))))))
          (test-assert "transmission-daemon accepts correct RPC credentials"
            (let ((transmission-remote
                   (string-append #+transmission "/bin/transmission-remote"))
                  (auth-string
                   (string-append #$%transmission-daemon-rpc-username
                                  ":"
                                  #$%transmission-daemon-rpc-password)))
              (with-output-to-port (%make-void-port "w")
                (lambda ()
                  (zero? (system* transmission-remote
                                  "--auth" auth-string
                                  "--session-info"))))))

          (test-end))))

  (gexp->derivation "transmission-daemon-test" test))

(define %test-transmission-daemon
  (system-test
   (name "transmission-daemon")
   (description "Test a running Transmission Daemon service.")
   (value (run-transmission-daemon-test))))
for modified versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice, and that you preserve all their Warranty Disclaimers. The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work. In the combination, you must combine any sections Entitled ``History'' in the various original documents, forming one section Entitled ``History''; likewise combine any sections Entitled ``Acknowledgements'', and any sections Entitled ``Dedications''. You must delete all sections Entitled ``Endorsements.'' @item COLLECTIONS OF DOCUMENTS You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects. You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document. @item AGGREGATION WITH INDEPENDENT WORKS A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, is called an ``aggregate'' if the copyright resulting from the compilation is not used to limit the legal rights of the compilation's users beyond what the individual works permit. When the Document is included in an aggregate, this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document. If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Document's Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate. @item TRANSLATION Translation is considered a kind of modification, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License and the original versions of those notices and disclaimers. In case of a disagreement between the translation and the original version of this License or a notice or disclaimer, the original version will prevail. If a section in the Document is Entitled ``Acknowledgements'', ``Dedications'', or ``History'', the requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual title. @item TERMINATION You may not copy, modify, sublicense, or distribute the Document except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and will automatically terminate your rights under this License. However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, receipt of a copy of some or all of the same material does not give you any rights to use it. @item FUTURE REVISIONS OF THIS LICENSE The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See @uref{https://www.gnu.org/copyleft/}. Each version of the License is given a distinguishing version number. If the Document specifies that a particular numbered version of this License ``or any later version'' applies to it, you have the option of following the terms and conditions either of that specified version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. If the Document specifies that a proxy can decide which future versions of this License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Document. @item RELICENSING ``Massive Multiauthor Collaboration Site'' (or ``MMC Site'') means any World Wide Web server that publishes copyrightable works and also provides prominent facilities for anybody to edit those works. A public wiki that anybody can edit is an example of such a server. A ``Massive Multiauthor Collaboration'' (or ``MMC'') contained in the site means any set of copyrightable works thus published on the MMC site. ``CC-BY-SA'' means the Creative Commons Attribution-Share Alike 3.0 license published by Creative Commons Corporation, a not-for-profit corporation with a principal place of business in San Francisco, California, as well as future copyleft versions of that license published by that same organization. ``Incorporate'' means to publish or republish a Document, in whole or in part, as part of another Document. An MMC is ``eligible for relicensing'' if it is licensed under this License, and if all works that were first published under this License somewhere other than this MMC, and subsequently incorporated in whole or in part into the MMC, (1) had no cover texts or invariant sections, and (2) were thus incorporated prior to November 1, 2008. The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is eligible for relicensing. @end enumerate @page @heading ADDENDUM: How to use this License for your documents To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page: @smallexample @group Copyright (C) @var{year} @var{your name}. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License''. @end group @end smallexample If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the ``with@dots{}Texts.''@: line with this: @smallexample @group with the Invariant Sections being @var{list their titles}, with the Front-Cover Texts being @var{list}, and with the Back-Cover Texts being @var{list}. @end group @end smallexample If you have Invariant Sections without Cover Texts, or some other combination of the three, merge those two alternatives to suit the situation. If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software. @c Local Variables: @c ispell-local-pdict: "ispell-dict" @c End: