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 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 vm)
30 #:use-module (gnu services)
31 #:use-module (gnu services getmail)
32 #:use-module (gnu services mail)
33 #:use-module (gnu services networking)
34 #:use-module (guix gexp)
35 #:use-module (guix store)
36 #:use-module (ice-9 ftw)
37 #:export (%test-opensmtpd
43 (simple-operating-system
44 (service dhcp-client-service-type)
45 (service opensmtpd-service-type
46 (opensmtpd-configuration
48 (plain-file "smtpd.conf" "
51 match from any for local action inbound
54 (define (run-opensmtpd-test)
55 "Return a test of an OS running OpenSMTPD service."
58 (operating-system (marionette-operating-system
60 #:imported-modules '((gnu services herd))))
61 (port-forwardings '((1025 . 25)))))
64 (with-imported-modules '((gnu build marionette))
66 (use-modules (rnrs base)
70 (gnu build marionette))
73 (make-marionette '(#$vm)))
75 (define (read-reply-code port)
76 "Read a SMTP reply from PORT and return its reply code."
77 (let* ((line (read-line port))
78 (mo (string-match "([0-9]+)([ -]).*" line))
79 (code (string->number (match:substring mo 1)))
80 (finished? (string= " " (match:substring mo 2))))
83 (read-reply-code port))))
88 (test-begin "opensmptd")
90 (test-assert "service is running"
93 (use-modules (gnu services herd))
94 (start-service 'smtpd))
97 (test-assert "mbox is empty"
99 '(and (file-exists? "/var/mail")
100 (not (file-exists? "/var/mail/root")))
103 (test-eq "accept an email"
105 (let* ((smtp (socket AF_INET SOCK_STREAM 0))
106 (addr (make-socket-address AF_INET INADDR_LOOPBACK 1025)))
109 (read-reply-code smtp) ;220
111 (write-line "EHLO somehost" smtp)
112 (read-reply-code smtp) ;250
114 (write-line "MAIL FROM: <someone>" smtp)
115 (read-reply-code smtp) ;250
116 ;; Set recipient email.
117 (write-line "RCPT TO: <root>" smtp)
118 (read-reply-code smtp) ;250
120 (write-line "DATA" smtp)
121 (read-reply-code smtp) ;354
122 (write-line "Subject: Hello" smtp)
124 (write-line "Nice to meet you!" smtp)
125 (write-line "." smtp)
126 (read-reply-code smtp) ;250
128 (write-line "QUIT" smtp)
129 (read-reply-code smtp) ;221
133 (test-assert "mail arrived"
136 (use-modules (ice-9 popen)
139 (define (queue-empty?)
143 (string-append #$(file-append opensmtpd "/sbin/smtpctl")
148 (file-exists? "/var/mail/root")
149 (begin (sleep 1) (wait)))))
153 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
155 (gexp->derivation "opensmtpd-test" test))
157 (define %test-opensmtpd
160 (description "Send an email to a running OpenSMTPD server.")
161 (value (run-opensmtpd-test))))
165 (simple-operating-system
166 (service dhcp-client-service-type)
167 (service mail-aliases-service-type '())
168 (service exim-service-type
171 (plain-file "exim.conf" "
172 primary_hostname = komputilo
173 domainlist local_domains = @
174 domainlist relay_to_domains =
175 hostlist relay_from_hosts = localhost
179 acl_smtp_rcpt = acl_check_rcpt
180 acl_smtp_data = acl_check_data
190 (define (run-exim-test)
191 "Return a test of an OS running an Exim service."
194 (operating-system (marionette-operating-system
196 #:imported-modules '((gnu services herd))))
197 (port-forwardings '((1025 . 25)))))
200 (with-imported-modules '((gnu build marionette)
203 (use-modules (rnrs base)
208 (gnu build marionette))
211 (make-marionette '(#$vm)))
213 (define (read-reply-code port)
214 "Read a SMTP reply from PORT and return its reply code."
215 (let* ((line (read-line port))
216 (mo (string-match "([0-9]+)([ -]).*" line))
217 (code (string->number (match:substring mo 1)))
218 (finished? (string= " " (match:substring mo 2))))
221 (read-reply-code port))))
223 (define smtp (socket AF_INET SOCK_STREAM 0))
224 (define addr (make-socket-address AF_INET INADDR_LOOPBACK 1025))
231 (test-assert "service is running"
234 (use-modules (gnu services herd))
235 (start-service 'exim))
238 (sleep 1) ;; give the service time to start talking
242 (test-eq "greeting received"
243 220 (read-reply-code smtp))
245 (write-line "EHLO somehost" smtp)
246 (test-eq "greeting successful"
247 250 (read-reply-code smtp))
249 (write-line "MAIL FROM: test@example.com" smtp)
250 (test-eq "sender set"
251 250 (read-reply-code smtp)) ;250
252 ;; Set recipient email.
253 (write-line "RCPT TO: root@komputilo" smtp)
254 (test-eq "recipient set"
255 250 (read-reply-code smtp)) ;250
257 (write-line "DATA" smtp)
258 (test-eq "data begun"
259 354 (read-reply-code smtp)) ;354
260 (write-line "Subject: Hello" smtp)
262 (write-line "Nice to meet you!" smtp)
263 (write-line "." smtp)
264 (test-eq "message sent"
265 250 (read-reply-code smtp)) ;250
267 (write-line "QUIT" smtp)
268 (test-eq "quit successful"
269 221 (read-reply-code smtp)) ;221
272 (test-eq "the email is received"
276 (use-modules (ice-9 ftw))
277 (length (scandir "/var/spool/exim/msglog"
278 (lambda (x) (not (string-prefix? "." x))))))
282 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
284 (gexp->derivation "exim-test" test))
289 (description "Send an email to a running an Exim server.")
290 (value (run-exim-test))))
293 (simple-operating-system
294 (service dhcp-client-service-type)
295 (dovecot-service #:config
296 (dovecot-configuration
297 (disable-plaintext-auth? #f)
299 (auth-mechanisms '("anonymous"))
300 (auth-anonymous-username "alice")
302 (string-append "maildir:~/Maildir"
303 ":INBOX=~/Maildir/INBOX"
306 (define (run-dovecot-test)
307 "Return a test of an OS running Dovecot service."
310 (operating-system (marionette-operating-system
312 #:imported-modules '((gnu services herd))))
313 (port-forwardings '((8143 . 143)))))
316 (with-imported-modules '((gnu build marionette))
318 (use-modules (gnu build marionette)
326 (make-marionette '(#$vm)))
328 (define* (message-length message #:key (encoding "iso-8859-1"))
329 (bytevector-length (string->bytevector message encoding)))
331 (define message "From: test@example.com\n\
332 Subject: Hello Nice to meet you!")
337 (test-begin "dovecot")
339 ;; Wait for dovecot to be up and running.
340 (test-assert "dovecot running"
343 (use-modules (gnu services herd))
344 (start-service 'dovecot))
347 ;; Check Dovecot service's PID.
348 (test-assert "service process id"
350 (number->string (wait-for-file "/var/run/dovecot/master.pid"
352 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
355 (test-assert "accept an email"
356 (let ((imap (socket AF_INET SOCK_STREAM 0))
357 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
362 (write-line "a AUTHENTICATE ANONYMOUS" imap)
364 (write-line "c2lyaGM=" imap)
366 ;; Create a TESTBOX mailbox
367 (write-line "a CREATE TESTBOX" imap)
369 ;; Append a message to a TESTBOX mailbox
370 (write-line (format #f "a APPEND TESTBOX {~a}"
371 (number->string (message-length message)))
374 (write-line message imap)
377 (write-line "a LOGOUT" imap)
381 (test-equal "mail arrived"
385 (use-modules (ice-9 ftw)
387 (let ((TESTBOX/new "/home/alice/Maildir/TESTBOX/new/"))
388 (match (scandir TESTBOX/new)
389 (("." ".." message-file)
390 (call-with-input-file
391 (string-append TESTBOX/new message-file)
396 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
398 (gexp->derivation "dovecot-test" test))
400 (define %test-dovecot
403 (description "Connect to a running Dovecot server.")
404 (value (run-dovecot-test))))
407 (simple-operating-system
408 (service dhcp-client-service-type)
409 (service dovecot-service-type
410 (dovecot-configuration
411 (disable-plaintext-auth? #f)
413 (auth-mechanisms '("anonymous" "plain"))
414 (auth-anonymous-username "alice")
416 (string-append "maildir:~/Maildir"
417 ":INBOX=~/Maildir/INBOX"
419 (service getmail-service-type
421 (getmail-configuration
424 (directory "/var/lib/getmail/alice")
427 (getmail-configuration-file
429 (getmail-retriever-configuration
430 (type "SimpleIMAPRetriever")
435 '((password . "testpass")
436 (mailboxes . ("TESTBOX"))))))
438 (getmail-destination-configuration
440 (path "/home/alice/TestMaildir/")))
442 (getmail-options-configuration
443 (read-all #f))))))))))
445 (define (run-getmail-test)
446 "Return a test of an OS running Getmail service."
449 (operating-system (marionette-operating-system
451 #:imported-modules '((gnu services herd))))
452 (port-forwardings '((8143 . 143)))))
455 (with-imported-modules '((gnu build marionette))
457 (use-modules (gnu build marionette)
465 (make-marionette '(#$vm)))
467 (define* (message-length message #:key (encoding "iso-8859-1"))
468 (bytevector-length (string->bytevector message encoding)))
470 (define message "From: test@example.com\n\
471 Subject: Hello Nice to meet you!")
476 (test-begin "getmail")
478 ;; Wait for dovecot to be up and running.
479 (test-assert "dovecot running"
482 (use-modules (gnu services herd))
483 (start-service 'dovecot))
486 (test-assert "set password for alice"
488 '(system "echo -e \"testpass\ntestpass\" | passwd alice")
491 ;; Wait for getmail to be up and running.
492 (test-assert "getmail-test running"
494 '(let* ((pw (getpw "alice"))
495 (uid (passwd:uid pw))
496 (gid (passwd:gid pw)))
497 (use-modules (gnu services herd))
503 '("/home/alice/TestMaildir"
504 "/home/alice/TestMaildir/cur"
505 "/home/alice/TestMaildir/new"
506 "/home/alice/TestMaildir/tmp"
507 "/home/alice/TestMaildir/TESTBOX"
508 "/home/alice/TestMaildir/TESTBOX/cur"
509 "/home/alice/TestMaildir/TESTBOX/new"
510 "/home/alice/TestMaildir/TESTBOX/tmp"))
512 (start-service 'getmail-test))
515 ;; Check Dovecot service's PID.
516 (test-assert "service process id"
518 (number->string (wait-for-file "/var/run/dovecot/master.pid"
520 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
523 (test-assert "accept an email"
524 (let ((imap (socket AF_INET SOCK_STREAM 0))
525 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
530 (write-line "a AUTHENTICATE ANONYMOUS" imap)
532 (write-line "c2lyaGM=" imap)
534 ;; Create a TESTBOX mailbox
535 (write-line "a CREATE TESTBOX" imap)
537 ;; Append a message to a TESTBOX mailbox
538 (write-line (format #f "a APPEND TESTBOX {~a}"
539 (number->string (message-length message)))
542 (write-line message imap)
545 (write-line "a LOGOUT" imap)
551 (test-assert "mail arrived"
555 (use-modules (ice-9 ftw)
557 (let ((TESTBOX/new "/home/alice/TestMaildir/new/"))
558 (match (scandir TESTBOX/new)
559 (("." ".." message-file)
560 (call-with-input-file
561 (string-append TESTBOX/new message-file)
567 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
569 (gexp->derivation "getmail-test" test))
571 (define %test-getmail
574 (description "Connect to a running Getmail server.")
575 (value (run-getmail-test))))