1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
3 ;;; Copyright © 2017 Carlo Zancanaro <carlo@zancanaro.id.au>
4 ;;; Copyright © 2017, 2020 Ludovic Courtès <ludo@gnu.org>
5 ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
6 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
7 ;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
8 ;;; Copyright © 2019, 2020 Tobias Geerinckx-Rice <me@tobias.gr>
10 ;;; This file is part of GNU Guix.
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25 (define-module (gnu tests mail)
26 #:use-module (gnu tests)
27 #:use-module (gnu packages mail)
28 #:use-module (gnu system)
29 #:use-module (gnu system accounts)
30 #:use-module (gnu system shadow)
31 #:use-module (gnu system vm)
32 #:use-module (gnu services)
33 #:use-module (gnu services base)
34 #:use-module (gnu services getmail)
35 #:use-module (gnu services mail)
36 #:use-module (gnu services networking)
37 #:use-module (guix gexp)
38 #:use-module (guix store)
39 #:use-module (ice-9 ftw)
40 #:export (%test-opensmtpd
46 (simple-operating-system
47 (service dhcp-client-service-type)
48 (service opensmtpd-service-type
49 (opensmtpd-configuration
51 (plain-file "smtpd.conf" "
54 match from any for local action inbound
57 (define (run-opensmtpd-test)
58 "Return a test of an OS running OpenSMTPD service."
61 (operating-system (marionette-operating-system
63 #:imported-modules '((gnu services herd))))
64 (port-forwardings '((1025 . 25)))))
67 (with-imported-modules '((gnu build marionette))
69 (use-modules (rnrs base)
73 (gnu build marionette))
76 (make-marionette '(#$vm)))
78 (define (read-reply-code port)
79 "Read a SMTP reply from PORT and return its reply code."
80 (let* ((line (read-line port))
81 (mo (string-match "([0-9]+)([ -]).*" line))
82 (code (string->number (match:substring mo 1)))
83 (finished? (string= " " (match:substring mo 2))))
86 (read-reply-code port))))
91 (test-begin "opensmptd")
93 (test-assert "service is running"
96 (use-modules (gnu services herd))
97 (start-service 'smtpd))
100 (test-assert "mbox is empty"
102 '(and (file-exists? "/var/mail")
103 (not (file-exists? "/var/mail/root")))
106 (test-eq "accept an email"
108 (let* ((smtp (socket AF_INET SOCK_STREAM 0))
109 (addr (make-socket-address AF_INET INADDR_LOOPBACK 1025)))
112 (read-reply-code smtp) ;220
114 (write-line "EHLO somehost" smtp)
115 (read-reply-code smtp) ;250
117 (write-line "MAIL FROM: <someone>" smtp)
118 (read-reply-code smtp) ;250
119 ;; Set recipient email.
120 (write-line "RCPT TO: <root>" smtp)
121 (read-reply-code smtp) ;250
123 (write-line "DATA" smtp)
124 (read-reply-code smtp) ;354
125 (write-line "Subject: Hello" smtp)
127 (write-line "Nice to meet you!" smtp)
128 (write-line "." smtp)
129 (read-reply-code smtp) ;250
131 (write-line "QUIT" smtp)
132 (read-reply-code smtp) ;221
136 (test-assert "mail arrived"
139 (use-modules (ice-9 popen)
142 (define (queue-empty?)
146 (string-append #$(file-append opensmtpd "/sbin/smtpctl")
151 (file-exists? "/var/mail/root")
152 (begin (sleep 1) (wait)))))
156 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
158 (gexp->derivation "opensmtpd-test" test))
160 (define %test-opensmtpd
163 (description "Send an email to a running OpenSMTPD server.")
164 (value (run-opensmtpd-test))))
168 (simple-operating-system
169 (service dhcp-client-service-type)
170 (service mail-aliases-service-type '())
171 (service exim-service-type
174 (plain-file "exim.conf" "
175 primary_hostname = komputilo
176 domainlist local_domains = @
177 domainlist relay_to_domains =
178 hostlist relay_from_hosts = localhost
182 acl_smtp_rcpt = acl_check_rcpt
183 acl_smtp_data = acl_check_data
193 (define (run-exim-test)
194 "Return a test of an OS running an Exim service."
197 (operating-system (marionette-operating-system
199 #:imported-modules '((gnu services herd))))
200 (port-forwardings '((1025 . 25)))))
203 (with-imported-modules '((gnu build marionette)
206 (use-modules (rnrs base)
211 (gnu build marionette))
214 (make-marionette '(#$vm)))
216 (define (read-reply-code port)
217 "Read a SMTP reply from PORT and return its reply code."
218 (let* ((line (read-line port))
219 (mo (string-match "([0-9]+)([ -]).*" line))
220 (code (string->number (match:substring mo 1)))
221 (finished? (string= " " (match:substring mo 2))))
224 (read-reply-code port))))
226 (define smtp (socket AF_INET SOCK_STREAM 0))
227 (define addr (make-socket-address AF_INET INADDR_LOOPBACK 1025))
234 (test-assert "service is running"
237 (use-modules (gnu services herd))
238 (start-service 'exim))
241 (sleep 1) ;; give the service time to start talking
245 (test-eq "greeting received"
246 220 (read-reply-code smtp))
248 (write-line "EHLO somehost" smtp)
249 (test-eq "greeting successful"
250 250 (read-reply-code smtp))
252 (write-line "MAIL FROM: test@example.com" smtp)
253 (test-eq "sender set"
254 250 (read-reply-code smtp)) ;250
255 ;; Set recipient email.
256 (write-line "RCPT TO: root@komputilo" smtp)
257 (test-eq "recipient set"
258 250 (read-reply-code smtp)) ;250
260 (write-line "DATA" smtp)
261 (test-eq "data begun"
262 354 (read-reply-code smtp)) ;354
263 (write-line "Subject: Hello" smtp)
265 (write-line "Nice to meet you!" smtp)
266 (write-line "." smtp)
267 (test-eq "message sent"
268 250 (read-reply-code smtp)) ;250
270 (write-line "QUIT" smtp)
271 (test-eq "quit successful"
272 221 (read-reply-code smtp)) ;221
275 (test-eq "the email is received"
279 (use-modules (ice-9 ftw))
280 (length (scandir "/var/spool/exim/msglog"
281 (lambda (x) (not (string-prefix? "." x))))))
285 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
287 (gexp->derivation "exim-test" test))
292 (description "Send an email to a running an Exim server.")
293 (value (run-exim-test))))
296 (simple-operating-system
297 (service dhcp-client-service-type)
298 (dovecot-service #:config
299 (dovecot-configuration
300 (disable-plaintext-auth? #f)
302 (auth-mechanisms '("anonymous"))
303 (auth-anonymous-username "alice")
305 (string-append "maildir:~/Maildir"
306 ":INBOX=~/Maildir/INBOX"
309 (define (run-dovecot-test)
310 "Return a test of an OS running Dovecot service."
313 (operating-system (marionette-operating-system
315 #:imported-modules '((gnu services herd))))
316 (port-forwardings '((8143 . 143)))))
319 (with-imported-modules '((gnu build marionette))
321 (use-modules (gnu build marionette)
329 (make-marionette '(#$vm)))
331 (define* (message-length message #:key (encoding "iso-8859-1"))
332 (bytevector-length (string->bytevector message encoding)))
334 (define message "From: test@example.com\n\
335 Subject: Hello Nice to meet you!")
340 (test-begin "dovecot")
342 ;; Wait for dovecot to be up and running.
343 (test-assert "dovecot running"
346 (use-modules (gnu services herd))
347 (start-service 'dovecot))
350 ;; Check Dovecot service's PID.
351 (test-assert "service process id"
353 (number->string (wait-for-file "/var/run/dovecot/master.pid"
355 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
358 (test-assert "accept an email"
359 (let ((imap (socket AF_INET SOCK_STREAM 0))
360 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
365 (write-line "a AUTHENTICATE ANONYMOUS" imap)
367 (write-line "c2lyaGM=" imap)
369 ;; Create a TESTBOX mailbox
370 (write-line "a CREATE TESTBOX" imap)
372 ;; Append a message to a TESTBOX mailbox
373 (write-line (format #f "a APPEND TESTBOX {~a}"
374 (number->string (message-length message)))
377 (write-line message imap)
380 (write-line "a LOGOUT" imap)
384 (test-equal "mail arrived"
388 (use-modules (ice-9 ftw)
390 (let ((TESTBOX/new "/home/alice/Maildir/TESTBOX/new/"))
391 (match (scandir TESTBOX/new)
392 (("." ".." message-file)
393 (call-with-input-file
394 (string-append TESTBOX/new message-file)
399 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
401 (gexp->derivation "dovecot-test" test))
403 (define %test-dovecot
406 (description "Connect to a running Dovecot server.")
407 (value (run-dovecot-test))))
411 (inherit (simple-operating-system))
413 ;; Set a password for the user account; the test needs it.
414 (users (cons (user-account
416 (password (crypt "testpass" "$6$abc"))
417 (comment "Bob's sister")
419 (supplementary-groups '("wheel" "audio" "video")))
420 %base-user-accounts))
422 (services (cons* (service dhcp-client-service-type)
423 (service dovecot-service-type
424 (dovecot-configuration
425 (disable-plaintext-auth? #f)
427 (auth-mechanisms '("anonymous" "plain"))
428 (auth-anonymous-username "alice")
430 (string-append "maildir:~/Maildir"
431 ":INBOX=~/Maildir/INBOX"
433 (service getmail-service-type
435 (getmail-configuration
438 (directory "/var/lib/getmail/alice")
441 (getmail-configuration-file
443 (getmail-retriever-configuration
444 (type "SimpleIMAPRetriever")
449 '((password . "testpass")
450 (mailboxes . ("TESTBOX"))))))
452 (getmail-destination-configuration
454 (path "/home/alice/TestMaildir/")))
456 (getmail-options-configuration
460 (define (run-getmail-test)
461 "Return a test of an OS running Getmail service."
464 (operating-system (marionette-operating-system
466 #:imported-modules '((gnu services herd))))
467 (port-forwardings '((8143 . 143)))))
470 (with-imported-modules '((gnu build marionette))
472 (use-modules (gnu build marionette)
480 (make-marionette '(#$vm)))
482 (define* (message-length message #:key (encoding "iso-8859-1"))
483 (bytevector-length (string->bytevector message encoding)))
485 (define message "From: test@example.com\n\
486 Subject: Hello Nice to meet you!")
491 (test-begin "getmail")
493 ;; Wait for dovecot to be up and running.
494 (test-assert "dovecot running"
497 (use-modules (gnu services herd))
498 (start-service 'dovecot))
501 ;; Wait for getmail to be up and running.
502 (test-assert "getmail-test running"
504 '(let* ((pw (getpw "alice"))
505 (uid (passwd:uid pw))
506 (gid (passwd:gid pw)))
507 (use-modules (gnu services herd))
513 '("/home/alice/TestMaildir"
514 "/home/alice/TestMaildir/cur"
515 "/home/alice/TestMaildir/new"
516 "/home/alice/TestMaildir/tmp"
517 "/home/alice/TestMaildir/TESTBOX"
518 "/home/alice/TestMaildir/TESTBOX/cur"
519 "/home/alice/TestMaildir/TESTBOX/new"
520 "/home/alice/TestMaildir/TESTBOX/tmp"))
522 (start-service 'getmail-test))
525 ;; Check Dovecot service's PID.
526 (test-assert "service process id"
528 (number->string (wait-for-file "/var/run/dovecot/master.pid"
530 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
533 (test-assert "accept an email"
534 (let ((imap (socket AF_INET SOCK_STREAM 0))
535 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
540 (write-line "a AUTHENTICATE ANONYMOUS" imap)
542 (write-line "c2lyaGM=" imap)
544 ;; Create a TESTBOX mailbox
545 (write-line "a CREATE TESTBOX" imap)
547 ;; Append a message to a TESTBOX mailbox
548 (write-line (format #f "a APPEND TESTBOX {~a}"
549 (number->string (message-length message)))
552 (write-line message imap)
555 (write-line "a LOGOUT" imap)
561 (test-assert "mail arrived"
565 (use-modules (ice-9 ftw)
567 (let ((TESTBOX/new "/home/alice/TestMaildir/new/"))
568 (match (scandir TESTBOX/new)
569 (("." ".." message-file)
570 (call-with-input-file
571 (string-append TESTBOX/new message-file)
577 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
579 (gexp->derivation "getmail-test" test))
581 (define %test-getmail
584 (description "Connect to a running Getmail server.")
585 (value (run-getmail-test))))