services: hurd-vm: Resurrect system-test by using raw disk-image.
[jackhill/guix/guix.git] / gnu / tests / mail.scm
index 312df9b..a50fb1d 100644 (file)
@@ -1,7 +1,11 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
 ;;; Copyright © 2017 Carlo Zancanaro <carlo@zancanaro.id.au>
-;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2017, 2020 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
+;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
+;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
+;;; Copyright © 2019, 2020 Tobias Geerinckx-Rice <me@tobias.gr>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
 
 (define-module (gnu tests mail)
   #:use-module (gnu tests)
+  #:use-module (gnu packages mail)
   #:use-module (gnu system)
+  #:use-module (gnu system accounts)
+  #:use-module (gnu system shadow)
   #:use-module (gnu system vm)
   #:use-module (gnu services)
+  #:use-module (gnu services base)
+  #:use-module (gnu services getmail)
   #:use-module (gnu services mail)
   #:use-module (gnu services networking)
   #:use-module (guix gexp)
   #:use-module (guix store)
   #:use-module (ice-9 ftw)
   #:export (%test-opensmtpd
-            %test-exim))
+            %test-exim
+            %test-dovecot
+            %test-getmail))
 
 (define %opensmtpd-os
   (simple-operating-system
-   (dhcp-client-service)
+   (service dhcp-client-service-type)
    (service opensmtpd-service-type
             (opensmtpd-configuration
              (config-file
               (plain-file "smtpd.conf" "
 listen on 0.0.0.0
-accept from any for local deliver to mbox
+action inbound mbox
+match from any for local action inbound
 "))))))
 
 (define (run-opensmtpd-test)
@@ -82,14 +94,13 @@ accept from any for local deliver to mbox
             (marionette-eval
              '(begin
                 (use-modules (gnu services herd))
-                (start-service 'smtpd)
-                #t)
+                (start-service 'smtpd))
              marionette))
 
           (test-assert "mbox is empty"
             (marionette-eval
-             '(and (file-exists? "/var/mail")
-                   (not (file-exists? "/var/mail/root")))
+             '(and (file-exists? "/var/spool/mail")
+                   (not (file-exists? "/var/spool/mail/root")))
              marionette))
 
           (test-eq "accept an email"
@@ -129,14 +140,21 @@ accept from any for local deliver to mbox
                              (ice-9 rdelim))
 
                 (define (queue-empty?)
-                  (eof-object?
-                   (read-line
-                    (open-input-pipe "smtpctl show queue"))))
-
-                (let wait ()
-                  (if (queue-empty?)
-                      (file-exists? "/var/mail/root")
-                      (begin (sleep 1) (wait)))))
+                  (let* ((pipe (open-pipe* OPEN_READ
+                                           #$(file-append opensmtpd
+                                                          "/sbin/smtpctl")
+                                           "show" "queue"))
+                         (line (read-line pipe)))
+                    (close-pipe pipe)
+                    (eof-object? line)))
+
+                (let wait ((n 20))
+                  (cond ((queue-empty?)
+                         (file-exists? "/var/spool/mail/root"))
+                        ((zero? n)
+                         (error "root mailbox didn't show up"))
+                        (else
+                         (sleep 1) (wait (- n 1))))))
              marionette))
 
           (test-end)
@@ -153,7 +171,7 @@ accept from any for local deliver to mbox
 
 (define %exim-os
   (simple-operating-system
-   (dhcp-client-service)
+   (service dhcp-client-service-type)
    (service mail-aliases-service-type '())
    (service exim-service-type
             (exim-configuration
@@ -222,8 +240,7 @@ acl_check_data:
             (marionette-eval
              '(begin
                 (use-modules (gnu services herd))
-                (start-service 'exim)
-                #t)
+                (start-service 'exim))
              marionette))
 
           (sleep 1) ;; give the service time to start talking
@@ -279,3 +296,295 @@ acl_check_data:
    (name "exim")
    (description "Send an email to a running an Exim server.")
    (value (run-exim-test))))
+
+(define %dovecot-os
+  (simple-operating-system
+   (service dhcp-client-service-type)
+   (dovecot-service #:config
+                    (dovecot-configuration
+                     (disable-plaintext-auth? #f)
+                     (ssl? "no")
+                     (auth-mechanisms '("anonymous"))
+                     (auth-anonymous-username "alice")
+                     (mail-location
+                      (string-append "maildir:~/Maildir"
+                                     ":INBOX=~/Maildir/INBOX"
+                                     ":LAYOUT=fs"))))))
+
+(define (run-dovecot-test)
+  "Return a test of an OS running Dovecot service."
+  (define vm
+    (virtual-machine
+     (operating-system (marionette-operating-system
+                        %dovecot-os
+                        #:imported-modules '((gnu services herd))))
+     (port-forwardings '((8143 . 143)))))
+
+  (define test
+    (with-imported-modules '((gnu build marionette))
+      #~(begin
+          (use-modules (gnu build marionette)
+                       (ice-9 iconv)
+                       (ice-9 rdelim)
+                       (rnrs base)
+                       (rnrs bytevectors)
+                       (srfi srfi-64))
+
+          (define marionette
+            (make-marionette '(#$vm)))
+
+          (define* (message-length message #:key (encoding "iso-8859-1"))
+            (bytevector-length (string->bytevector message encoding)))
+
+          (define message "From: test@example.com\n\
+Subject: Hello Nice to meet you!")
+
+          (mkdir #$output)
+          (chdir #$output)
+
+          (test-begin "dovecot")
+
+          ;; Wait for dovecot to be up and running.
+          (test-assert "dovecot running"
+            (marionette-eval
+             '(begin
+                (use-modules (gnu services herd))
+                (start-service 'dovecot))
+             marionette))
+
+          ;; Check Dovecot service's PID.
+          (test-assert "service process id"
+            (let ((pid
+                   (number->string (wait-for-file "/var/run/dovecot/master.pid"
+                                                  marionette))))
+              (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
+                               marionette)))
+
+          (test-assert "accept an email"
+            (let ((imap (socket AF_INET SOCK_STREAM 0))
+                  (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
+              (connect imap addr)
+              ;; Be greeted.
+              (read-line imap) ;OK
+              ;; Authenticate
+              (write-line "a AUTHENTICATE ANONYMOUS" imap)
+              (read-line imap) ;+
+              (write-line "c2lyaGM=" imap)
+              (read-line imap) ;OK
+              ;; Create a TESTBOX mailbox
+              (write-line "a CREATE TESTBOX" imap)
+              (read-line imap) ;OK
+              ;; Append a message to a TESTBOX mailbox
+              (write-line (format #f "a APPEND TESTBOX {~a}"
+                                  (number->string (message-length message)))
+                          imap)
+              (read-line imap) ;+
+              (write-line message imap)
+              (read-line imap) ;OK
+              ;; Logout
+              (write-line "a LOGOUT" imap)
+              (close imap)
+              #t))
+
+          (test-equal "mail arrived"
+            message
+            (marionette-eval
+             '(begin
+                (use-modules (ice-9 ftw)
+                             (ice-9 match))
+                (let ((TESTBOX/new "/home/alice/Maildir/TESTBOX/new/"))
+                  (match (scandir TESTBOX/new)
+                    (("." ".." message-file)
+                     (call-with-input-file
+                         (string-append TESTBOX/new message-file)
+                       get-string-all)))))
+             marionette))
+
+          (test-end)
+          (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
+
+  (gexp->derivation "dovecot-test" test))
+
+(define %test-dovecot
+  (system-test
+   (name "dovecot")
+   (description "Connect to a running Dovecot server.")
+   (value (run-dovecot-test))))
+
+(define %getmail-os
+  (operating-system
+    (inherit (simple-operating-system))
+
+    ;; Set a password for the user account; the test needs it.
+    (users (cons (user-account
+                  (name "alice")
+                  (password (crypt "testpass" "$6$abc"))
+                  (comment "Bob's sister")
+                  (group "users")
+                  (supplementary-groups '("wheel" "audio" "video")))
+                 %base-user-accounts))
+
+    (services (cons* (service dhcp-client-service-type)
+                     (service dovecot-service-type
+                              (dovecot-configuration
+                               (disable-plaintext-auth? #f)
+                               (ssl? "no")
+                               (auth-mechanisms '("anonymous" "plain"))
+                               (auth-anonymous-username "alice")
+                               (mail-location
+                                (string-append "maildir:~/Maildir"
+                                               ":INBOX=~/Maildir/INBOX"
+                                               ":LAYOUT=fs"))))
+                     (service getmail-service-type
+                              (list
+                               (getmail-configuration
+                                (name 'test)
+                                (user "alice")
+                                (directory "/var/lib/getmail/alice")
+                                (idle '("TESTBOX"))
+                                (rcfile
+                                 (getmail-configuration-file
+                                  (retriever
+                                   (getmail-retriever-configuration
+                                    (type "SimpleIMAPRetriever")
+                                    (server "localhost")
+                                    (username "alice")
+                                    (port 143)
+                                    (extra-parameters
+                                     '((password . "testpass")
+                                       (mailboxes . ("TESTBOX"))))))
+                                  (destination
+                                   (getmail-destination-configuration
+                                    (type "Maildir")
+                                    (path "/home/alice/TestMaildir/")))
+                                  (options
+                                   (getmail-options-configuration
+                                    (read-all #f))))))))
+                     %base-services))))
+
+(define (run-getmail-test)
+  "Return a test of an OS running Getmail service."
+  (define vm
+    (virtual-machine
+     (operating-system (marionette-operating-system
+                        %getmail-os
+                        #:imported-modules '((gnu services herd))))
+     (port-forwardings '((8143 . 143)))))
+
+  (define test
+    (with-imported-modules '((gnu build marionette))
+      #~(begin
+          (use-modules (gnu build marionette)
+                       (ice-9 iconv)
+                       (ice-9 rdelim)
+                       (rnrs base)
+                       (rnrs bytevectors)
+                       (srfi srfi-64))
+
+          (define marionette
+            (make-marionette '(#$vm)))
+
+          (define* (message-length message #:key (encoding "iso-8859-1"))
+            (bytevector-length (string->bytevector message encoding)))
+
+          (define message "From: test@example.com\n\
+Subject: Hello Nice to meet you!")
+
+          (mkdir #$output)
+          (chdir #$output)
+
+          (test-begin "getmail")
+
+          ;; Wait for dovecot to be up and running.
+          (test-assert "dovecot running"
+            (marionette-eval
+             '(begin
+                (use-modules (gnu services herd))
+                (start-service 'dovecot))
+             marionette))
+
+          ;; Wait for getmail to be up and running.
+          (test-assert "getmail-test running"
+            (marionette-eval
+             '(let* ((pw (getpw "alice"))
+                     (uid (passwd:uid pw))
+                     (gid (passwd:gid pw)))
+                (use-modules (gnu services herd))
+
+                (for-each
+                 (lambda (dir)
+                   (mkdir dir)
+                   (chown dir uid gid))
+                 '("/home/alice/TestMaildir"
+                   "/home/alice/TestMaildir/cur"
+                   "/home/alice/TestMaildir/new"
+                   "/home/alice/TestMaildir/tmp"
+                   "/home/alice/TestMaildir/TESTBOX"
+                   "/home/alice/TestMaildir/TESTBOX/cur"
+                   "/home/alice/TestMaildir/TESTBOX/new"
+                   "/home/alice/TestMaildir/TESTBOX/tmp"))
+
+                (start-service 'getmail-test))
+             marionette))
+
+          ;; Check Dovecot service's PID.
+          (test-assert "service process id"
+            (let ((pid
+                   (number->string (wait-for-file "/var/run/dovecot/master.pid"
+                                                  marionette))))
+              (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
+                               marionette)))
+
+          (test-assert "accept an email"
+            (let ((imap (socket AF_INET SOCK_STREAM 0))
+                  (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
+              (connect imap addr)
+              ;; Be greeted.
+              (read-line imap) ;OK
+              ;; Authenticate
+              (write-line "a AUTHENTICATE ANONYMOUS" imap)
+              (read-line imap) ;+
+              (write-line "c2lyaGM=" imap)
+              (read-line imap) ;OK
+              ;; Create a TESTBOX mailbox
+              (write-line "a CREATE TESTBOX" imap)
+              (read-line imap) ;OK
+              ;; Append a message to a TESTBOX mailbox
+              (write-line (format #f "a APPEND TESTBOX {~a}"
+                                  (number->string (message-length message)))
+                          imap)
+              (read-line imap) ;+
+              (write-line message imap)
+              (read-line imap) ;OK
+              ;; Logout
+              (write-line "a LOGOUT" imap)
+              (close imap)
+              #t))
+
+          (sleep 1)
+
+          (test-assert "mail arrived"
+            (string-contains
+             (marionette-eval
+              '(begin
+                 (use-modules (ice-9 ftw)
+                              (ice-9 match))
+                 (let ((TESTBOX/new "/home/alice/TestMaildir/new/"))
+                   (match (scandir TESTBOX/new)
+                     (("." ".." message-file)
+                      (call-with-input-file
+                          (string-append TESTBOX/new message-file)
+                        get-string-all)))))
+              marionette)
+             message))
+
+          (test-end)
+          (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
+
+  (gexp->derivation "getmail-test" test))
+
+(define %test-getmail
+  (system-test
+   (name "getmail")
+   (description "Connect to a running Getmail server.")
+   (value (run-getmail-test))))