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/spool/mail")
103 (not (file-exists? "/var/spool/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?)
143 (let* ((pipe (open-pipe* OPEN_READ
144 #$(file-append opensmtpd
147 (line (read-line pipe)))
152 (cond ((queue-empty?)
153 (file-exists? "/var/spool/mail/root"))
155 (error "root mailbox didn't show up"))
157 (sleep 1) (wait (- n 1))))))
161 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
163 (gexp->derivation "opensmtpd-test" test))
165 (define %test-opensmtpd
168 (description "Send an email to a running OpenSMTPD server.")
169 (value (run-opensmtpd-test))))
173 (simple-operating-system
174 (service dhcp-client-service-type)
175 (service mail-aliases-service-type '())
176 (service exim-service-type
179 (plain-file "exim.conf" "
180 primary_hostname = komputilo
181 domainlist local_domains = @
182 domainlist relay_to_domains =
183 hostlist relay_from_hosts = localhost
187 acl_smtp_rcpt = acl_check_rcpt
188 acl_smtp_data = acl_check_data
198 (define (run-exim-test)
199 "Return a test of an OS running an Exim service."
202 (operating-system (marionette-operating-system
204 #:imported-modules '((gnu services herd))))
205 (port-forwardings '((1025 . 25)))))
208 (with-imported-modules '((gnu build marionette))
210 (use-modules (rnrs base)
215 (gnu build marionette))
218 (make-marionette '(#$vm)))
220 (define (read-reply-code port)
221 "Read a SMTP reply from PORT and return its reply code."
222 (let* ((line (read-line port))
223 (mo (string-match "([0-9]+)([ -]).*" line))
224 (code (string->number (match:substring mo 1)))
225 (finished? (string= " " (match:substring mo 2))))
228 (read-reply-code port))))
230 (define smtp (socket AF_INET SOCK_STREAM 0))
231 (define addr (make-socket-address AF_INET INADDR_LOOPBACK 1025))
238 (test-assert "service is running"
241 (use-modules (gnu services herd))
242 (start-service 'exim))
245 (sleep 1) ;; give the service time to start talking
249 (test-eq "greeting received"
250 220 (read-reply-code smtp))
252 (write-line "EHLO somehost" smtp)
253 (test-eq "greeting successful"
254 250 (read-reply-code smtp))
256 (write-line "MAIL FROM: test@example.com" smtp)
257 (test-eq "sender set"
258 250 (read-reply-code smtp)) ;250
259 ;; Set recipient email.
260 (write-line "RCPT TO: root@komputilo" smtp)
261 (test-eq "recipient set"
262 250 (read-reply-code smtp)) ;250
264 (write-line "DATA" smtp)
265 (test-eq "data begun"
266 354 (read-reply-code smtp)) ;354
267 (write-line "Subject: Hello" smtp)
269 (write-line "Nice to meet you!" smtp)
270 (write-line "." smtp)
271 (test-eq "message sent"
272 250 (read-reply-code smtp)) ;250
274 (write-line "QUIT" smtp)
275 (test-eq "quit successful"
276 221 (read-reply-code smtp)) ;221
279 (test-eq "the email is received"
283 (use-modules (ice-9 ftw))
284 (length (scandir "/var/spool/exim/msglog"
285 (lambda (x) (not (string-prefix? "." x))))))
289 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
291 (gexp->derivation "exim-test" test))
296 (description "Send an email to a running an Exim server.")
297 (value (run-exim-test))))
300 (simple-operating-system
301 (service dhcp-client-service-type)
302 (dovecot-service #:config
303 (dovecot-configuration
304 (disable-plaintext-auth? #f)
306 (auth-mechanisms '("anonymous"))
307 (auth-anonymous-username "alice")
309 (string-append "maildir:~/Maildir"
310 ":INBOX=~/Maildir/INBOX"
313 (define (run-dovecot-test)
314 "Return a test of an OS running Dovecot service."
317 (operating-system (marionette-operating-system
319 #:imported-modules '((gnu services herd))))
320 (port-forwardings '((8143 . 143)))))
323 (with-imported-modules '((gnu build marionette))
325 (use-modules (gnu build marionette)
333 (make-marionette '(#$vm)))
335 (define* (message-length message #:key (encoding "iso-8859-1"))
336 (bytevector-length (string->bytevector message encoding)))
338 (define message "From: test@example.com\n\
339 Subject: Hello Nice to meet you!")
344 (test-begin "dovecot")
346 ;; Wait for dovecot to be up and running.
347 (test-assert "dovecot running"
350 (use-modules (gnu services herd))
351 (start-service 'dovecot))
354 ;; Check Dovecot service's PID.
355 (test-assert "service process id"
357 (number->string (wait-for-file "/var/run/dovecot/master.pid"
359 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
362 (test-assert "accept an email"
363 (let ((imap (socket AF_INET SOCK_STREAM 0))
364 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
369 (write-line "a AUTHENTICATE ANONYMOUS" imap)
371 (write-line "c2lyaGM=" imap)
373 ;; Create a TESTBOX mailbox
374 (write-line "a CREATE TESTBOX" imap)
376 ;; Append a message to a TESTBOX mailbox
377 (write-line (format #f "a APPEND TESTBOX {~a}"
378 (number->string (message-length message)))
381 (write-line message imap)
384 (write-line "a LOGOUT" imap)
388 (test-equal "mail arrived"
392 (use-modules (ice-9 ftw)
394 (let ((TESTBOX/new "/home/alice/Maildir/TESTBOX/new/"))
395 (match (scandir TESTBOX/new)
396 (("." ".." message-file)
397 (call-with-input-file
398 (string-append TESTBOX/new message-file)
403 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
405 (gexp->derivation "dovecot-test" test))
407 (define %test-dovecot
410 (description "Connect to a running Dovecot server.")
411 (value (run-dovecot-test))))
415 (inherit (simple-operating-system))
417 ;; Set a password for the user account; the test needs it.
418 (users (cons (user-account
420 (password (crypt "testpass" "$6$abc"))
421 (comment "Bob's sister")
423 (supplementary-groups '("wheel" "audio" "video")))
424 %base-user-accounts))
426 (services (cons* (service dhcp-client-service-type)
427 (service dovecot-service-type
428 (dovecot-configuration
429 (disable-plaintext-auth? #f)
431 (auth-mechanisms '("anonymous" "plain"))
432 (auth-anonymous-username "alice")
434 (string-append "maildir:~/Maildir"
435 ":INBOX=~/Maildir/INBOX"
437 (service getmail-service-type
439 (getmail-configuration
442 (directory "/var/lib/getmail/alice")
445 (getmail-configuration-file
447 (getmail-retriever-configuration
448 (type "SimpleIMAPRetriever")
453 '((password . "testpass")
454 (mailboxes . ("TESTBOX"))))))
456 (getmail-destination-configuration
458 (path "/home/alice/TestMaildir/")))
460 (getmail-options-configuration
464 (define (run-getmail-test)
465 "Return a test of an OS running Getmail service."
468 (operating-system (marionette-operating-system
470 #:imported-modules '((gnu services herd))))
471 (port-forwardings '((8143 . 143)))))
474 (with-imported-modules '((gnu build marionette))
476 (use-modules (gnu build marionette)
484 (make-marionette '(#$vm)))
486 (define* (message-length message #:key (encoding "iso-8859-1"))
487 (bytevector-length (string->bytevector message encoding)))
489 (define message "From: test@example.com\n\
490 Subject: Hello Nice to meet you!")
495 (test-begin "getmail")
497 ;; Wait for dovecot to be up and running.
498 (test-assert "dovecot running"
501 (use-modules (gnu services herd))
502 (start-service 'dovecot))
505 ;; Wait for getmail to be up and running.
506 (test-assert "getmail-test running"
508 '(let* ((pw (getpw "alice"))
509 (uid (passwd:uid pw))
510 (gid (passwd:gid pw)))
511 (use-modules (gnu services herd))
517 '("/home/alice/TestMaildir"
518 "/home/alice/TestMaildir/cur"
519 "/home/alice/TestMaildir/new"
520 "/home/alice/TestMaildir/tmp"
521 "/home/alice/TestMaildir/TESTBOX"
522 "/home/alice/TestMaildir/TESTBOX/cur"
523 "/home/alice/TestMaildir/TESTBOX/new"
524 "/home/alice/TestMaildir/TESTBOX/tmp"))
526 (start-service 'getmail-test))
529 ;; Check Dovecot service's PID.
530 (test-assert "service process id"
532 (number->string (wait-for-file "/var/run/dovecot/master.pid"
534 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
537 (test-assert "accept an email"
538 (let ((imap (socket AF_INET SOCK_STREAM 0))
539 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
544 (write-line "a AUTHENTICATE ANONYMOUS" imap)
546 (write-line "c2lyaGM=" imap)
548 ;; Create a TESTBOX mailbox
549 (write-line "a CREATE TESTBOX" imap)
551 ;; Append a message to a TESTBOX mailbox
552 (write-line (format #f "a APPEND TESTBOX {~a}"
553 (number->string (message-length message)))
556 (write-line message imap)
559 (write-line "a LOGOUT" imap)
565 (test-assert "mail arrived"
569 (use-modules (ice-9 ftw)
571 (let ((TESTBOX/new "/home/alice/TestMaildir/new/"))
572 (match (scandir TESTBOX/new)
573 (("." ".." message-file)
574 (call-with-input-file
575 (string-append TESTBOX/new message-file)
581 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
583 (gexp->derivation "getmail-test" test))
585 (define %test-getmail
588 (description "Connect to a running Getmail server.")
589 (value (run-getmail-test))))