;;; 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 (ice-9 ftw)
#:export (%test-opensmtpd
%test-exim
- %test-dovecot))
+ %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)
(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"
(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)
(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
(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
(define %dovecot-os
(simple-operating-system
- (dhcp-client-service)
+ (service dhcp-client-service-type)
(dovecot-service #:config
(dovecot-configuration
(disable-plaintext-auth? #f)
(test-begin "dovecot")
;; Wait for dovecot to be up and running.
- (test-eq "dovecot running"
- 'running!
+ (test-assert "dovecot running"
(marionette-eval
'(begin
(use-modules (gnu services herd))
- (start-service 'dovecot)
- 'running!)
+ (start-service 'dovecot))
marionette))
;; Check Dovecot service's PID.
(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))))