mail.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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, 2021 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>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  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.
  16. ;;;
  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.
  21. ;;;
  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/>.
  24. (define-module (gnu tests mail)
  25. #:use-module (gnu tests)
  26. #:use-module (gnu packages mail)
  27. #:use-module (gnu system)
  28. #:use-module (gnu system accounts)
  29. #:use-module (gnu system shadow)
  30. #:use-module (gnu system vm)
  31. #:use-module (gnu services)
  32. #:use-module (gnu services base)
  33. #:use-module (gnu services getmail)
  34. #:use-module (gnu services mail)
  35. #:use-module (gnu services networking)
  36. #:use-module (guix gexp)
  37. #:use-module (guix store)
  38. #:use-module (ice-9 ftw)
  39. #:export (%test-opensmtpd
  40. %test-exim
  41. %test-dovecot
  42. %test-getmail))
  43. (define %opensmtpd-os
  44. (simple-operating-system
  45. (service dhcp-client-service-type)
  46. (service opensmtpd-service-type
  47. (opensmtpd-configuration
  48. (config-file
  49. (plain-file "smtpd.conf" "
  50. listen on 0.0.0.0
  51. action inbound mbox
  52. match from any for local action inbound
  53. "))))))
  54. (define (run-opensmtpd-test)
  55. "Return a test of an OS running OpenSMTPD service."
  56. (define vm
  57. (virtual-machine
  58. (operating-system (marionette-operating-system
  59. %opensmtpd-os
  60. #:imported-modules '((gnu services herd))))
  61. (port-forwardings '((1025 . 25)))))
  62. (define test
  63. (with-imported-modules '((gnu build marionette))
  64. #~(begin
  65. (use-modules (rnrs base)
  66. (srfi srfi-64)
  67. (ice-9 rdelim)
  68. (ice-9 regex)
  69. (gnu build marionette))
  70. (define marionette
  71. (make-marionette '(#$vm)))
  72. (define (read-reply-code port)
  73. "Read a SMTP reply from PORT and return its reply code."
  74. (let* ((line (read-line port))
  75. (mo (string-match "([0-9]+)([ -]).*" line))
  76. (code (string->number (match:substring mo 1)))
  77. (finished? (string= " " (match:substring mo 2))))
  78. (if finished?
  79. code
  80. (read-reply-code port))))
  81. (test-runner-current (system-test-runner #$output))
  82. (test-begin "opensmptd")
  83. (test-assert "service is running"
  84. (marionette-eval
  85. '(begin
  86. (use-modules (gnu services herd))
  87. (start-service 'smtpd))
  88. marionette))
  89. (test-assert "mbox is empty"
  90. (marionette-eval
  91. '(and (file-exists? "/var/spool/mail")
  92. (not (file-exists? "/var/spool/mail/root")))
  93. marionette))
  94. (test-eq "accept an email"
  95. #t
  96. (let* ((smtp (socket AF_INET SOCK_STREAM 0))
  97. (addr (make-socket-address AF_INET INADDR_LOOPBACK 1025)))
  98. (connect smtp addr)
  99. ;; Be greeted.
  100. (read-reply-code smtp) ;220
  101. ;; Greet the server.
  102. (write-line "EHLO somehost" smtp)
  103. (read-reply-code smtp) ;250
  104. ;; Set sender email.
  105. (write-line "MAIL FROM: <someone>" smtp)
  106. (read-reply-code smtp) ;250
  107. ;; Set recipient email.
  108. (write-line "RCPT TO: <root>" smtp)
  109. (read-reply-code smtp) ;250
  110. ;; Send message.
  111. (write-line "DATA" smtp)
  112. (read-reply-code smtp) ;354
  113. (write-line "Subject: Hello" smtp)
  114. (newline smtp)
  115. (write-line "Nice to meet you!" smtp)
  116. (write-line "." smtp)
  117. (read-reply-code smtp) ;250
  118. ;; Say goodbye.
  119. (write-line "QUIT" smtp)
  120. (read-reply-code smtp) ;221
  121. (close smtp)
  122. #t))
  123. (test-assert "mail arrived"
  124. (marionette-eval
  125. '(begin
  126. (use-modules (ice-9 popen)
  127. (ice-9 rdelim))
  128. (define (queue-empty?)
  129. (let* ((pipe (open-pipe* OPEN_READ
  130. #$(file-append opensmtpd
  131. "/sbin/smtpctl")
  132. "show" "queue"))
  133. (line (read-line pipe)))
  134. (close-pipe pipe)
  135. (eof-object? line)))
  136. (let wait ((n 20))
  137. (cond ((queue-empty?)
  138. (file-exists? "/var/spool/mail/root"))
  139. ((zero? n)
  140. (error "root mailbox didn't show up"))
  141. (else
  142. (sleep 1) (wait (- n 1))))))
  143. marionette))
  144. (test-end))))
  145. (gexp->derivation "opensmtpd-test" test))
  146. (define %test-opensmtpd
  147. (system-test
  148. (name "opensmtpd")
  149. (description "Send an email to a running OpenSMTPD server.")
  150. (value (run-opensmtpd-test))))
  151. (define %exim-os
  152. (simple-operating-system
  153. (service dhcp-client-service-type)
  154. (service mail-aliases-service-type '())
  155. (service exim-service-type
  156. (exim-configuration
  157. (config-file
  158. (plain-file "exim.conf" "
  159. primary_hostname = komputilo
  160. domainlist local_domains = @
  161. domainlist relay_to_domains =
  162. hostlist relay_from_hosts = localhost
  163. never_users =
  164. acl_smtp_rcpt = acl_check_rcpt
  165. acl_smtp_data = acl_check_data
  166. begin acl
  167. acl_check_rcpt:
  168. accept
  169. acl_check_data:
  170. accept
  171. "))))))
  172. (define (run-exim-test)
  173. "Return a test of an OS running an Exim service."
  174. (define vm
  175. (virtual-machine
  176. (operating-system (marionette-operating-system
  177. %exim-os
  178. #:imported-modules '((gnu services herd))))
  179. (port-forwardings '((1025 . 25)))))
  180. (define test
  181. (with-imported-modules '((gnu build marionette))
  182. #~(begin
  183. (use-modules (rnrs base)
  184. (srfi srfi-64)
  185. (ice-9 ftw)
  186. (ice-9 rdelim)
  187. (ice-9 regex)
  188. (gnu build marionette))
  189. (define marionette
  190. (make-marionette '(#$vm)))
  191. (define (read-reply-code port)
  192. "Read a SMTP reply from PORT and return its reply code."
  193. (let* ((line (read-line port))
  194. (mo (string-match "([0-9]+)([ -]).*" line))
  195. (code (string->number (match:substring mo 1)))
  196. (finished? (string= " " (match:substring mo 2))))
  197. (if finished?
  198. code
  199. (read-reply-code port))))
  200. (define smtp (socket AF_INET SOCK_STREAM 0))
  201. (define addr (make-socket-address AF_INET INADDR_LOOPBACK 1025))
  202. (test-runner-current (system-test-runner #$output))
  203. (test-begin "exim")
  204. (test-assert "service is running"
  205. (marionette-eval
  206. '(begin
  207. (use-modules (gnu services herd))
  208. (start-service 'exim))
  209. marionette))
  210. (sleep 1) ;; give the service time to start talking
  211. (connect smtp addr)
  212. ;; Be greeted.
  213. (test-eq "greeting received"
  214. 220 (read-reply-code smtp))
  215. ;; Greet the server.
  216. (write-line "EHLO somehost" smtp)
  217. (test-eq "greeting successful"
  218. 250 (read-reply-code smtp))
  219. ;; Set sender email.
  220. (write-line "MAIL FROM: test@example.com" smtp)
  221. (test-eq "sender set"
  222. 250 (read-reply-code smtp)) ;250
  223. ;; Set recipient email.
  224. (write-line "RCPT TO: root@komputilo" smtp)
  225. (test-eq "recipient set"
  226. 250 (read-reply-code smtp)) ;250
  227. ;; Send message.
  228. (write-line "DATA" smtp)
  229. (test-eq "data begun"
  230. 354 (read-reply-code smtp)) ;354
  231. (write-line "Subject: Hello" smtp)
  232. (newline smtp)
  233. (write-line "Nice to meet you!" smtp)
  234. (write-line "." smtp)
  235. (test-eq "message sent"
  236. 250 (read-reply-code smtp)) ;250
  237. ;; Say goodbye.
  238. (write-line "QUIT" smtp)
  239. (test-eq "quit successful"
  240. 221 (read-reply-code smtp)) ;221
  241. (close smtp)
  242. (test-eq "the email is received"
  243. 1
  244. (marionette-eval
  245. '(begin
  246. (use-modules (ice-9 ftw))
  247. (length (scandir "/var/spool/exim/msglog"
  248. (lambda (x) (not (string-prefix? "." x))))))
  249. marionette))
  250. (test-end))))
  251. (gexp->derivation "exim-test" test))
  252. (define %test-exim
  253. (system-test
  254. (name "exim")
  255. (description "Send an email to a running an Exim server.")
  256. (value (run-exim-test))))
  257. (define %dovecot-os
  258. (simple-operating-system
  259. (service dhcp-client-service-type)
  260. (service dovecot-service-type
  261. (dovecot-configuration
  262. (disable-plaintext-auth? #f)
  263. (ssl? "no")
  264. (auth-mechanisms '("anonymous"))
  265. (auth-anonymous-username "alice")
  266. (mail-location
  267. (string-append "maildir:~/Maildir"
  268. ":INBOX=~/Maildir/INBOX"
  269. ":LAYOUT=fs"))))))
  270. (define (run-dovecot-test)
  271. "Return a test of an OS running Dovecot service."
  272. (define vm
  273. (virtual-machine
  274. (operating-system (marionette-operating-system
  275. %dovecot-os
  276. #:imported-modules '((gnu services herd))))
  277. (port-forwardings '((8143 . 143)))))
  278. (define test
  279. (with-imported-modules '((gnu build marionette))
  280. #~(begin
  281. (use-modules (gnu build marionette)
  282. (ice-9 iconv)
  283. (ice-9 rdelim)
  284. (rnrs base)
  285. (rnrs bytevectors)
  286. (srfi srfi-64))
  287. (define marionette
  288. (make-marionette '(#$vm)))
  289. (define* (message-length message #:key (encoding "iso-8859-1"))
  290. (bytevector-length (string->bytevector message encoding)))
  291. (define message "From: test@example.com\n\
  292. Subject: Hello Nice to meet you!")
  293. (test-runner-current (system-test-runner #$output))
  294. (test-begin "dovecot")
  295. ;; Wait for dovecot to be up and running.
  296. (test-assert "dovecot running"
  297. (marionette-eval
  298. '(begin
  299. (use-modules (gnu services herd))
  300. (start-service 'dovecot))
  301. marionette))
  302. ;; Check Dovecot service's PID.
  303. (test-assert "service process id"
  304. (let ((pid
  305. (number->string (wait-for-file "/var/run/dovecot/master.pid"
  306. marionette))))
  307. (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
  308. marionette)))
  309. (test-assert "accept an email"
  310. (let ((imap (socket AF_INET SOCK_STREAM 0))
  311. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
  312. (connect imap addr)
  313. ;; Be greeted.
  314. (read-line imap) ;OK
  315. ;; Authenticate
  316. (write-line "a AUTHENTICATE ANONYMOUS" imap)
  317. (read-line imap) ;+
  318. (write-line "c2lyaGM=" imap)
  319. (read-line imap) ;OK
  320. ;; Create a TESTBOX mailbox
  321. (write-line "a CREATE TESTBOX" imap)
  322. (read-line imap) ;OK
  323. ;; Append a message to a TESTBOX mailbox
  324. (write-line (format #f "a APPEND TESTBOX {~a}"
  325. (number->string (message-length message)))
  326. imap)
  327. (read-line imap) ;+
  328. (write-line message imap)
  329. (read-line imap) ;OK
  330. ;; Logout
  331. (write-line "a LOGOUT" imap)
  332. (close imap)
  333. #t))
  334. (test-equal "mail arrived"
  335. message
  336. (marionette-eval
  337. '(begin
  338. (use-modules (ice-9 ftw)
  339. (ice-9 match)
  340. (rnrs io ports))
  341. (let ((TESTBOX/new "/home/alice/Maildir/TESTBOX/new/"))
  342. (match (scandir TESTBOX/new)
  343. (("." ".." message-file)
  344. (call-with-input-file
  345. (string-append TESTBOX/new message-file)
  346. get-string-all)))))
  347. marionette))
  348. (test-end))))
  349. (gexp->derivation "dovecot-test" test))
  350. (define %test-dovecot
  351. (system-test
  352. (name "dovecot")
  353. (description "Connect to a running Dovecot server.")
  354. (value (run-dovecot-test))))
  355. (define %getmail-os
  356. (operating-system
  357. (inherit (simple-operating-system))
  358. ;; Set a password for the user account; the test needs it.
  359. (users (cons (user-account
  360. (name "alice")
  361. (password (crypt "testpass" "$6$abc"))
  362. (comment "Bob's sister")
  363. (group "users")
  364. (supplementary-groups '("wheel" "audio" "video")))
  365. %base-user-accounts))
  366. (services (cons* (service dhcp-client-service-type)
  367. (service dovecot-service-type
  368. (dovecot-configuration
  369. (disable-plaintext-auth? #f)
  370. (ssl? "no")
  371. (auth-mechanisms '("anonymous" "plain"))
  372. (auth-anonymous-username "alice")
  373. (mail-location
  374. (string-append "maildir:~/Maildir"
  375. ":INBOX=~/Maildir/INBOX"
  376. ":LAYOUT=fs"))))
  377. (service getmail-service-type
  378. (list
  379. (getmail-configuration
  380. (name 'test)
  381. (user "alice")
  382. (directory "/var/lib/getmail/alice")
  383. (idle '("TESTBOX"))
  384. (rcfile
  385. (getmail-configuration-file
  386. (retriever
  387. (getmail-retriever-configuration
  388. (type "SimpleIMAPRetriever")
  389. (server "localhost")
  390. (username "alice")
  391. (port 143)
  392. (extra-parameters
  393. '((password . "testpass")
  394. (mailboxes . ("TESTBOX"))))))
  395. (destination
  396. (getmail-destination-configuration
  397. (type "Maildir")
  398. (path "/home/alice/TestMaildir/")))
  399. (options
  400. (getmail-options-configuration
  401. (read-all #f))))))))
  402. %base-services))))
  403. (define (run-getmail-test)
  404. "Return a test of an OS running Getmail service."
  405. (define vm
  406. (virtual-machine
  407. (operating-system (marionette-operating-system
  408. %getmail-os
  409. #:imported-modules '((gnu services herd))))
  410. (port-forwardings '((8143 . 143)))))
  411. (define test
  412. (with-imported-modules '((gnu build marionette))
  413. #~(begin
  414. (use-modules (gnu build marionette)
  415. (ice-9 iconv)
  416. (ice-9 rdelim)
  417. (rnrs base)
  418. (rnrs bytevectors)
  419. (srfi srfi-64))
  420. (define marionette
  421. (make-marionette '(#$vm)))
  422. (define* (message-length message #:key (encoding "iso-8859-1"))
  423. (bytevector-length (string->bytevector message encoding)))
  424. (define message "From: test@example.com\n\
  425. Subject: Hello Nice to meet you!")
  426. (test-runner-current (system-test-runner #$output))
  427. (test-begin "getmail")
  428. ;; Wait for dovecot to be up and running.
  429. (test-assert "dovecot running"
  430. (marionette-eval
  431. '(begin
  432. (use-modules (gnu services herd))
  433. (start-service 'dovecot))
  434. marionette))
  435. ;; Wait for getmail to be up and running.
  436. (test-assert "getmail-test running"
  437. (marionette-eval
  438. '(let* ((pw (getpw "alice"))
  439. (uid (passwd:uid pw))
  440. (gid (passwd:gid pw)))
  441. (use-modules (gnu services herd))
  442. (for-each
  443. (lambda (dir)
  444. (mkdir dir)
  445. (chown dir uid gid))
  446. '("/home/alice/TestMaildir"
  447. "/home/alice/TestMaildir/cur"
  448. "/home/alice/TestMaildir/new"
  449. "/home/alice/TestMaildir/tmp"
  450. "/home/alice/TestMaildir/TESTBOX"
  451. "/home/alice/TestMaildir/TESTBOX/cur"
  452. "/home/alice/TestMaildir/TESTBOX/new"
  453. "/home/alice/TestMaildir/TESTBOX/tmp"))
  454. (start-service 'getmail-test))
  455. marionette))
  456. ;; Check Dovecot service's PID.
  457. (test-assert "service process id"
  458. (let ((pid
  459. (number->string (wait-for-file "/var/run/dovecot/master.pid"
  460. marionette))))
  461. (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
  462. marionette)))
  463. (test-assert "accept an email"
  464. (let ((imap (socket AF_INET SOCK_STREAM 0))
  465. (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
  466. (connect imap addr)
  467. ;; Be greeted.
  468. (read-line imap) ;OK
  469. ;; Authenticate
  470. (write-line "a AUTHENTICATE ANONYMOUS" imap)
  471. (read-line imap) ;+
  472. (write-line "c2lyaGM=" imap)
  473. (read-line imap) ;OK
  474. ;; Create a TESTBOX mailbox
  475. (write-line "a CREATE TESTBOX" imap)
  476. (read-line imap) ;OK
  477. ;; Append a message to a TESTBOX mailbox
  478. (write-line (format #f "a APPEND TESTBOX {~a}"
  479. (number->string (message-length message)))
  480. imap)
  481. (read-line imap) ;+
  482. (write-line message imap)
  483. (read-line imap) ;OK
  484. ;; Logout
  485. (write-line "a LOGOUT" imap)
  486. (close imap)
  487. #t))
  488. (sleep 1)
  489. (test-assert "mail arrived"
  490. (string-contains
  491. (marionette-eval
  492. '(begin
  493. (use-modules (ice-9 ftw)
  494. (ice-9 match)
  495. (rnrs io ports))
  496. (let ((TESTBOX/new "/home/alice/TestMaildir/new/"))
  497. (match (scandir TESTBOX/new)
  498. (("." ".." message-file)
  499. (call-with-input-file
  500. (string-append TESTBOX/new message-file)
  501. get-string-all)))))
  502. marionette)
  503. message))
  504. (test-end))))
  505. (gexp->derivation "getmail-test" test))
  506. (define %test-getmail
  507. (system-test
  508. (name "getmail")
  509. (description "Connect to a running Getmail server.")
  510. (value (run-getmail-test))))