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>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu tests mail)
24 #:use-module (gnu tests)
25 #:use-module (gnu system)
26 #:use-module (gnu system vm)
27 #:use-module (gnu services)
28 #:use-module (gnu services mail)
29 #:use-module (gnu services networking)
30 #:use-module (guix gexp)
31 #:use-module (guix store)
32 #:use-module (ice-9 ftw)
33 #:export (%test-opensmtpd
38 (simple-operating-system
39 (service dhcp-client-service-type)
40 (service opensmtpd-service-type
41 (opensmtpd-configuration
43 (plain-file "smtpd.conf" "
45 accept from any for local deliver to mbox
48 (define (run-opensmtpd-test)
49 "Return a test of an OS running OpenSMTPD service."
52 (operating-system (marionette-operating-system
54 #:imported-modules '((gnu services herd))))
55 (port-forwardings '((1025 . 25)))))
58 (with-imported-modules '((gnu build marionette))
60 (use-modules (rnrs base)
64 (gnu build marionette))
67 (make-marionette '(#$vm)))
69 (define (read-reply-code port)
70 "Read a SMTP reply from PORT and return its reply code."
71 (let* ((line (read-line port))
72 (mo (string-match "([0-9]+)([ -]).*" line))
73 (code (string->number (match:substring mo 1)))
74 (finished? (string= " " (match:substring mo 2))))
77 (read-reply-code port))))
82 (test-begin "opensmptd")
84 (test-assert "service is running"
87 (use-modules (gnu services herd))
88 (start-service 'smtpd))
91 (test-assert "mbox is empty"
93 '(and (file-exists? "/var/mail")
94 (not (file-exists? "/var/mail/root")))
97 (test-eq "accept an email"
99 (let* ((smtp (socket AF_INET SOCK_STREAM 0))
100 (addr (make-socket-address AF_INET INADDR_LOOPBACK 1025)))
103 (read-reply-code smtp) ;220
105 (write-line "EHLO somehost" smtp)
106 (read-reply-code smtp) ;250
108 (write-line "MAIL FROM: <someone>" smtp)
109 (read-reply-code smtp) ;250
110 ;; Set recipient email.
111 (write-line "RCPT TO: <root>" smtp)
112 (read-reply-code smtp) ;250
114 (write-line "DATA" smtp)
115 (read-reply-code smtp) ;354
116 (write-line "Subject: Hello" smtp)
118 (write-line "Nice to meet you!" smtp)
119 (write-line "." smtp)
120 (read-reply-code smtp) ;250
122 (write-line "QUIT" smtp)
123 (read-reply-code smtp) ;221
127 (test-assert "mail arrived"
130 (use-modules (ice-9 popen)
133 (define (queue-empty?)
136 (open-input-pipe "smtpctl show queue"))))
140 (file-exists? "/var/mail/root")
141 (begin (sleep 1) (wait)))))
145 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
147 (gexp->derivation "opensmtpd-test" test))
149 (define %test-opensmtpd
152 (description "Send an email to a running OpenSMTPD server.")
153 (value (run-opensmtpd-test))))
157 (simple-operating-system
158 (service dhcp-client-service-type)
159 (service mail-aliases-service-type '())
160 (service exim-service-type
163 (plain-file "exim.conf" "
164 primary_hostname = komputilo
165 domainlist local_domains = @
166 domainlist relay_to_domains =
167 hostlist relay_from_hosts = localhost
171 acl_smtp_rcpt = acl_check_rcpt
172 acl_smtp_data = acl_check_data
182 (define (run-exim-test)
183 "Return a test of an OS running an Exim service."
186 (operating-system (marionette-operating-system
188 #:imported-modules '((gnu services herd))))
189 (port-forwardings '((1025 . 25)))))
192 (with-imported-modules '((gnu build marionette)
195 (use-modules (rnrs base)
200 (gnu build marionette))
203 (make-marionette '(#$vm)))
205 (define (read-reply-code port)
206 "Read a SMTP reply from PORT and return its reply code."
207 (let* ((line (read-line port))
208 (mo (string-match "([0-9]+)([ -]).*" line))
209 (code (string->number (match:substring mo 1)))
210 (finished? (string= " " (match:substring mo 2))))
213 (read-reply-code port))))
215 (define smtp (socket AF_INET SOCK_STREAM 0))
216 (define addr (make-socket-address AF_INET INADDR_LOOPBACK 1025))
223 (test-assert "service is running"
226 (use-modules (gnu services herd))
227 (start-service 'exim))
230 (sleep 1) ;; give the service time to start talking
234 (test-eq "greeting received"
235 220 (read-reply-code smtp))
237 (write-line "EHLO somehost" smtp)
238 (test-eq "greeting successful"
239 250 (read-reply-code smtp))
241 (write-line "MAIL FROM: test@example.com" smtp)
242 (test-eq "sender set"
243 250 (read-reply-code smtp)) ;250
244 ;; Set recipient email.
245 (write-line "RCPT TO: root@komputilo" smtp)
246 (test-eq "recipient set"
247 250 (read-reply-code smtp)) ;250
249 (write-line "DATA" smtp)
250 (test-eq "data begun"
251 354 (read-reply-code smtp)) ;354
252 (write-line "Subject: Hello" smtp)
254 (write-line "Nice to meet you!" smtp)
255 (write-line "." smtp)
256 (test-eq "message sent"
257 250 (read-reply-code smtp)) ;250
259 (write-line "QUIT" smtp)
260 (test-eq "quit successful"
261 221 (read-reply-code smtp)) ;221
264 (test-eq "the email is received"
268 (use-modules (ice-9 ftw))
269 (length (scandir "/var/spool/exim/msglog"
270 (lambda (x) (not (string-prefix? "." x))))))
274 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
276 (gexp->derivation "exim-test" test))
281 (description "Send an email to a running an Exim server.")
282 (value (run-exim-test))))
285 (simple-operating-system
286 (service dhcp-client-service-type)
287 (dovecot-service #:config
288 (dovecot-configuration
289 (disable-plaintext-auth? #f)
291 (auth-mechanisms '("anonymous"))
292 (auth-anonymous-username "alice")
294 (string-append "maildir:~/Maildir"
295 ":INBOX=~/Maildir/INBOX"
298 (define (run-dovecot-test)
299 "Return a test of an OS running Dovecot service."
302 (operating-system (marionette-operating-system
304 #:imported-modules '((gnu services herd))))
305 (port-forwardings '((8143 . 143)))))
308 (with-imported-modules '((gnu build marionette))
310 (use-modules (gnu build marionette)
318 (make-marionette '(#$vm)))
320 (define* (message-length message #:key (encoding "iso-8859-1"))
321 (bytevector-length (string->bytevector message encoding)))
323 (define message "From: test@example.com\n\
324 Subject: Hello Nice to meet you!")
329 (test-begin "dovecot")
331 ;; Wait for dovecot to be up and running.
332 (test-assert "dovecot running"
335 (use-modules (gnu services herd))
336 (start-service 'dovecot))
339 ;; Check Dovecot service's PID.
340 (test-assert "service process id"
342 (number->string (wait-for-file "/var/run/dovecot/master.pid"
344 (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
347 (test-assert "accept an email"
348 (let ((imap (socket AF_INET SOCK_STREAM 0))
349 (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
354 (write-line "a AUTHENTICATE ANONYMOUS" imap)
356 (write-line "c2lyaGM=" imap)
358 ;; Create a TESTBOX mailbox
359 (write-line "a CREATE TESTBOX" imap)
361 ;; Append a message to a TESTBOX mailbox
362 (write-line (format #f "a APPEND TESTBOX {~a}"
363 (number->string (message-length message)))
366 (write-line message imap)
369 (write-line "a LOGOUT" imap)
373 (test-equal "mail arrived"
377 (use-modules (ice-9 ftw)
379 (let ((TESTBOX/new "/home/alice/Maildir/TESTBOX/new/"))
380 (match (scandir TESTBOX/new)
381 (("." ".." message-file)
382 (call-with-input-file
383 (string-append TESTBOX/new message-file)
388 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
390 (gexp->derivation "dovecot-test" test))
392 (define %test-dovecot
395 (description "Connect to a running Dovecot server.")
396 (value (run-dovecot-test))))