unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 1bac9f50a24e82ecc6e2816fdd7144ef3888f03e 32065 bytes (raw)
name: gnu/tests/mail.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
;;; Copyright © 2017 Carlo Zancanaro <carlo@zancanaro.id.au>
;;; Copyright © 2017, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
;;; Copyright © 2019, 2020 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu tests mail)
  #:use-module (gnu tests)
  #:use-module (gnu packages mail)
  #:use-module (gnu system)
  #:use-module (gnu system accounts)
  #:use-module (gnu system shadow)
  #:use-module (gnu system vm)
  #:use-module (gnu services)
  #:use-module (gnu services base)
  #:use-module (gnu services getmail)
  #:use-module (gnu services mail)
  #:use-module (gnu services networking)
  #:use-module (guix gexp)
  #:use-module (guix store)
  #:use-module (ice-9 ftw)
  #:use-module (srfi srfi-64)
  #:export (%test-opensmtpd
            %test-exim
            %test-dovecot
            %test-getmail))

(define %opensmtpd-os
  (simple-operating-system
   (service dhcp-client-service-type)
   (service opensmtpd-service-type
            (opensmtpd-configuration
             (config-file
              (plain-file "smtpd.conf" "
listen on 0.0.0.0
action inbound mbox
match from any for local action inbound
"))))))

(define (run-opensmtpd-test)
  "Return a test of an OS running OpenSMTPD service."
  (define vm
    (virtual-machine
     (operating-system (marionette-operating-system
                        %opensmtpd-os
                        #:imported-modules '((gnu services herd))))
     (port-forwardings '((1025 . 25)))))

  (define test
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (rnrs base)
                       (srfi srfi-64)
                       (ice-9 rdelim)
                       (ice-9 regex)
                       (gnu build marionette))

          (define marionette
            (make-marionette '(#$vm)))

          (define (read-reply-code port)
            "Read a SMTP reply from PORT and return its reply code."
            (let* ((line      (read-line port))
                   (mo        (string-match "([0-9]+)([ -]).*" line))
                   (code      (string->number (match:substring mo 1)))
                   (finished? (string= " " (match:substring mo 2))))
              (if finished?
                  code
                  (read-reply-code port))))

          (test-runner-current (system-test-runner #$output))
          (test-begin "opensmptd")

          (test-assert "service is running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (start-service 'smtpd))
             marionette))

          (test-assert "mbox is empty"
            (marionette-eval
             '(and (file-exists? "/var/spool/mail")
                   (not (file-exists? "/var/spool/mail/root")))
             marionette))

          (test-eq "accept an email"
            #t
            (let* ((smtp (socket AF_INET SOCK_STREAM 0))
                   (addr (make-socket-address AF_INET INADDR_LOOPBACK 1025)))
              (connect smtp addr)
              ;; Be greeted.
              (read-reply-code smtp)              ;220
              ;; Greet the server.
              (write-line "EHLO somehost" smtp)
              (read-reply-code smtp)              ;250
              ;; Set sender email.
              (write-line "MAIL FROM: <someone>" smtp)
              (read-reply-code smtp)              ;250
              ;; Set recipient email.
              (write-line "RCPT TO: <root>" smtp)
              (read-reply-code smtp)              ;250
              ;; Send message.
              (write-line "DATA" smtp)
              (read-reply-code smtp)              ;354
              (write-line "Subject: Hello" smtp)
              (newline smtp)
              (write-line "Nice to meet you!" smtp)
              (write-line "." smtp)
              (read-reply-code smtp)              ;250
              ;; Say goodbye.
              (write-line "QUIT" smtp)
              (read-reply-code smtp)              ;221
              (close smtp)
              #t))

          (test-assert "mail arrived"
            (marionette-eval
             '(begin
                (use-modules (ice-9 popen)
                             (ice-9 rdelim))

                (define (queue-empty?)
                  (let* ((pipe (open-pipe* OPEN_READ
                                           #$(file-append opensmtpd
                                                          "/sbin/smtpctl")
                                           "show" "queue"))
                         (line (read-line pipe)))
                    (close-pipe pipe)
                    (eof-object? line)))

                (let wait ((n 20))
                  (cond ((queue-empty?)
                         (file-exists? "/var/spool/mail/root"))
                        ((zero? n)
                         (error "root mailbox didn't show up"))
                        (else
                         (sleep 1) (wait (- n 1))))))
             marionette))

          (test-end))))

  (gexp->derivation "opensmtpd-test" test))

(define %test-opensmtpd
  (system-test
   (name "opensmtpd")
   (description "Send an email to a running OpenSMTPD server.")
   (value (run-opensmtpd-test))))

;; trying to create a bad record, should result in an error.
;; this function should be able return, instead it should throw an error
(define (create-bad-record record)
  ;; TODO why is this not working
  (with-output-to-port (%make-void-port "w")
    (lambda () (when record #f))))

;; if this caller function is reached, then trying to create the bad record
;; resulted in an error.  So return true.
(define (return-true error arg)
  #t)

;; two filters with the same name
(define (bad-interface1)
  (create-bad-record
   (opensmtpd-interface
    (interface "lo")
    (filters (list
              (opensmtpd-filter
               (name "dkimsign")
               (exec #t)
               (proc (list (file-append opensmtpd-filter-dkimsign "/libexec/opensmtpd/filter-dkimsign")
                           " -d gnucode.me -s 2021-09-22 -c relaxed/relaxed -k "
                           "rando string"
                           "/etc/dkim/private.key "
                           "user nobody group nogroup")))
              (opensmtpd-filter
               (name "dkimsign")
               (exec #t)
               (proc (list (file-append opensmtpd-filter-dkimsign "/libexec/opensmtpd/filter-dkimsign")
                           " -d gnucode.me -s 2021-09-22 -c relaxed/relaxed -k "
                           "/etc/dkim/private.key "
                           "user nobody group nogroup"))))))))

;; duplicate filter names
(define (bad-interface2)
  (create-bad-record
   (opensmtpd-interface
    (filters (list
              (opensmtpd-filter-phase
               (name "src")
               (phase "connect")
               (options
                (list
                 (opensmtpd-option
                  (option "fcrdns")
                  (not #t))))
               (decision "junk"))
              (opensmtpd-filter-phase
               (name "src")
               (phase "helo")
               (options
                (list
                 (opensmtpd-option
                  (option "rdns")
                  (not #t))))
               (decision "junk")))))))

 ;; improper phase name
(define (bad-filter-phase1)
    (create-bad-record
     (opensmtpd-filter-phase
      (name "filter")
      (phase "wrongString")
      (decision "bypass")
      (options
       (list
        (opensmtpd-option
         (option "auth")))))))

;; decision reject requires you to have a
;; corresponding fieldname 'message' with value of string.
(define (bad-filter-phase2)
  (create-bad-record
   (opensmtpd-interface
    (filters (list
              (opensmtpd-filter-phase
               (name "src")
               (phase "connect")
               (options
                (list
                 (opensmtpd-option
                  (option "src")
                  (data (opensmtpd-table
                         (name "src-table")
                         (data (list "cat" "hat")))))))
               (decision "reject")))))))

;; message needs to start with 4xx or 5xx
(define (bad-filter-phase3)
  (create-bad-record
   (opensmtpd-interface
    (filters (list
              (opensmtpd-filter-phase
               (name "src")
               (phase "connect")
               (options
                (list
                 (opensmtpd-option
                  (option "src")
                  (data (opensmtpd-table
                         (name "src-table")
                         (data (list "cat" "hat")))))))
               (decision "reject")
               (message "322 Bad data!")))))))

 ;; there needs to be a value here.  rewrite requires a value!
(define (bad-filter-phase4)
  (create-bad-record
   (opensmtpd-interface
    (filters
     (list
      (opensmtpd-filter-phase
       (name "noFRDNS")
       (phase "commit")
       (options (list (opensmtpd-option
                       (option "fcrdns")
                       (not #t))))
       (decision "rewrite"))
      )))))

;; fieldname 'decision' with value "junk" or "bypass", then fieldname 'message' and 'value'
;; must NOT be defined
(define (bad-filter-phase5)
  (create-bad-record
   (opensmtpd-interface
    (filters
     (list
      (opensmtpd-filter-phase
       (name "noFRDNS")
       (phase "commit")
       (options (list (opensmtpd-option
                       (option "fcrdns")
                       )))
       (decision "junk")
       (message "This is not a good email.")))))))

;; you cannot junk on phase commit.  You need to use an eariler phase.
(define (bad-filter-phase6)
  (create-bad-record
   (opensmtpd-interface
    (filters
     (list
      (opensmtpd-filter-phase
       (name "junk-after-commit")
       (options (list (opensmtpd-option
                       (option "fcrdns"))))
       (phase "commit")
       (decision "junk")))))))

;; TODO fix this test
;; two fcrdns options records
(define (bad-filter-phase7)
  (create-bad-record
   (opensmtpd-filter-phase
    (name "invalid-fcrdns")
    (phase "connect")
    (options
     (list (opensmtpd-option
            (option "fcrdns")
            (not #t))
           (opensmtpd-option
            (option "fcrdns")
            (not #f))))
    (decision "reject")
    (message "422 No valid fcrdns."))))

;; option src requires a table
;; TODO maybe check for other options requiring a table
(define (bad-filter-phase8)
  (create-bad-record
   (opensmtpd-filter-phase
    (name "filter")
    (phase "helo")
    (decision "bypass")
    (options
     (list
      (opensmtpd-option
       (option "src")))))))

;; option fcrdns cannot have data defined.
(define (bad-filter-phase9)
  (create-bad-record
   (opensmtpd-filter-phase
    (name "filter")
    (phase "helo")
    (decision "bypass")
    (options
     (list
      (opensmtpd-option
       (option "fcrdns")
       (data (opensmtpd-table
               (name "table")
               (data (list "hello" "cat"))))))))))


;; this should be (list ...) instead of '( ...)
(define (bad-match1)
  (create-bad-record
   (opensmtpd-match
    (options
     '((opensmtpd-option
        (option "for any"))))
    (action
     (opensmtpd-relay)))))


;; duplcate "for" options
(define (bad-match2)
  (create-bad-record
   (opensmtpd-match
            (options (list
                      (opensmtpd-option
                       (option "for any"))
                      (opensmtpd-option
                       (option "for local"))))
            (action
             (opensmtpd-relay
              (name "relay"))))))

;; duplicate froms
(define (bad-match3)
  (create-bad-record
   (opensmtpd-match
    (options (list
              (opensmtpd-option
               (option "from any"))
              (opensmtpd-option
               (option "from auth"))))
    (action
     (opensmtpd-relay
      (name "relay"))))))

;; rcpt-to must have a data field.
(define (bad-match4)
  (create-bad-record
   (opensmtpd-match
    (options (list
              (opensmtpd-option
               (option "rcpt-to"))))
    (action
     (opensmtpd-relay
      (name "relay"))))))

;; option 'tls' cannot have fieldname
;; 'data' defined.
(define (bad-match5)
  (create-bad-record
   (opensmtpd-match
    (options (list
              (opensmtpd-option
               (option "tls")
               (data "hello"))))
    (action
     (opensmtpd-relay
      (name "relay"))))))

;; for any cannot have data
;; or regex defined
(define (bad-match6)
  (create-bad-record
   (opensmtpd-match
    (options (list
              (opensmtpd-option
               (option "for any")
               (regex #t))))
    (action
     (opensmtpd-relay
      (name "relay"))))))

;; match needs an action
(define (bad-match7)
  (create-bad-record
   (opensmtpd-match
    (options (list
              (opensmtpd-option
               (option "from auth")))))))

(define (run-opensmtpd-record-sanitation-test)
                                        ;(with-output-to-port (%make-void-port "w")
                                        ;  (lambda ()
  (test-begin "run-opensmtpd-record-sanitation-test")

  ;; TODO fix me!
  (test-assert "Test <interface> fieldname 'filters' has two filters with the same name."
    (catch #t bad-interface1 return-true))

  (test-assert "Test <interface> cannot have two filters with the same name."
               (catch #t bad-interface2 return-true))

  (test-assert "Test <filter-phase> fieldname 'phase' the right string."
    (catch #t bad-filter-phase1 return-true))

  (test-assert "Test <filter-phase> fieldname 'decision' w/ value \"reject\" and \"disconnect\" requires a 'message'."
    (catch #t bad-filter-phase2 return-true))

  (test-assert (string-append  "Test <filter-phase> fieldname 'decision' "
                               "w/ value \"reject\" and \"disconnect\" requires a 'message'."
                               " The message must begin with 4xx or 5xx.")
    (catch #t bad-filter-phase3 return-true))

  (test-assert "Test <filter-phase> fieldname 'rewrite' requires fieldname 'value' to have a number."
    (catch #t bad-filter-phase4 return-true))

  (test-assert (string-append "Test <filter-phase> fieldname 'decision' with values 'junk' or 'bypass', "
                             "then fieldname 'message' and 'value' must be blank.")
    (catch #t bad-filter-phase5 return-true))

  (test-assert "You cannot junk an email on phase commit."
    (catch #t bad-filter-phase6 return-true))

  ;; TODO fix me!
  (test-assert "Test <filter-phase> has 2 duplicate options."
    (catch #t bad-filter-phase7 return-true))

  (test-assert "Test <filter-phase> option 'src' requires a table."
    (catch #t bad-filter-phase8 return-true))

  ;; TODO fix me!
  (test-assert "Test <filter-phase> option 'fcrdns' cannot have a table."
    (catch #t bad-filter-phase9 return-true))

  (test-assert "Test <opensmtpd-match> fieldname 'options' should not be quoted."
    (catch #t bad-match1 return-true))

  (test-assert "Test <opensmtpd-match> has duplicate 'for' options."
    (catch #t bad-match2 return-true))

  (test-assert "Test <opensmtpd-match> has duplicate 'from' options."
    (catch #t bad-match3 return-true))

  (test-assert "Test <opensmtpd-match> option 'rcpt' must have data."
    (catch #t bad-match4 return-true))

  (test-assert "Test <opensmtpd-match> option 'tls' cannot have fieldname 'data' defined."
    (catch #t bad-match5 return-true))

  (test-assert "Test <opensmtpd-match> option 'for any' cannot have fieldname 'data' defined."
    (catch #t bad-match6 return-true))

  (test-assert "Test <opensmtpd-match> needs fieldname 'action' needs to be defined."
    (catch #t bad-match7 return-true))

  (test-end "run-opensmtpd-record-sanitation-test"))

(define %test-opensmtpd-record-sanitation
  (system-test
   (name "opensmtpdRecordSanitation")
   (description
    (string-append "<opensmtpd> has numerous sanity checks.\n"
                   "This checks that invalid configurations, return an\n"
                   "appropriate error.\n"))
   (value (run-opensmtpd-record-sanitation-test))))


(define %exim-os
  (simple-operating-system
   (service dhcp-client-service-type)
   (service mail-aliases-service-type '())
   (service exim-service-type
            (exim-configuration
             (config-file
              (plain-file "exim.conf" "
primary_hostname = komputilo
domainlist local_domains = @
domainlist relay_to_domains =
hostlist   relay_from_hosts = localhost

never_users =

acl_smtp_rcpt = acl_check_rcpt
acl_smtp_data = acl_check_data

begin acl

acl_check_rcpt:
  accept
acl_check_data:
  accept
"))))))

(define (run-exim-test)
  "Return a test of an OS running an Exim service."
  (define vm
    (virtual-machine
     (operating-system (marionette-operating-system
                        %exim-os
                        #:imported-modules '((gnu services herd))))
     (port-forwardings '((1025 . 25)))))

  (define test
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (rnrs base)
                       (srfi srfi-64)
                       (ice-9 ftw)
                       (ice-9 rdelim)
                       (ice-9 regex)
                       (gnu build marionette))

          (define marionette
            (make-marionette '(#$vm)))

          (define (read-reply-code port)
            "Read a SMTP reply from PORT and return its reply code."
            (let* ((line      (read-line port))
                   (mo        (string-match "([0-9]+)([ -]).*" line))
                   (code      (string->number (match:substring mo 1)))
                   (finished? (string= " " (match:substring mo 2))))
              (if finished?
                  code
                  (read-reply-code port))))

          (define smtp (socket AF_INET SOCK_STREAM 0))
          (define addr (make-socket-address AF_INET INADDR_LOOPBACK 1025))

          (test-runner-current (system-test-runner #$output))
          (test-begin "exim")

          (test-assert "service is running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (start-service 'exim))
             marionette))

          (sleep 1) ;; give the service time to start talking

          (connect smtp addr)
          ;; Be greeted.
          (test-eq "greeting received"
            220 (read-reply-code smtp))
          ;; Greet the server.
          (write-line "EHLO somehost" smtp)
          (test-eq "greeting successful"
            250 (read-reply-code smtp))
          ;; Set sender email.
          (write-line "MAIL FROM: test@example.com" smtp)
          (test-eq "sender set"
            250 (read-reply-code smtp))           ;250
          ;; Set recipient email.
          (write-line "RCPT TO: root@komputilo" smtp)
          (test-eq "recipient set"
            250 (read-reply-code smtp))           ;250
          ;; Send message.
          (write-line "DATA" smtp)
          (test-eq "data begun"
            354 (read-reply-code smtp))           ;354
          (write-line "Subject: Hello" smtp)
          (newline smtp)
          (write-line "Nice to meet you!" smtp)
          (write-line "." smtp)
          (test-eq "message sent"
            250 (read-reply-code smtp))           ;250
          ;; Say goodbye.
          (write-line "QUIT" smtp)
          (test-eq "quit successful"
            221 (read-reply-code smtp))           ;221
          (close smtp)

          (test-eq "the email is received"
            1
            (marionette-eval
             '(begin
                (use-modules (ice-9 ftw))
                (length (scandir "/var/spool/exim/msglog"
                                 (lambda (x) (not (string-prefix? "." x))))))
             marionette))

          (test-end))))

  (gexp->derivation "exim-test" test))

(define %test-exim
  (system-test
   (name "exim")
   (description "Send an email to a running an Exim server.")
   (value (run-exim-test))))

(define %dovecot-os
  (simple-operating-system
   (service dhcp-client-service-type)
   (dovecot-service #:config
                    (dovecot-configuration
                     (disable-plaintext-auth? #f)
                     (ssl? "no")
                     (auth-mechanisms '("anonymous"))
                     (auth-anonymous-username "alice")
                     (mail-location
                      (string-append "maildir:~/Maildir"
                                     ":INBOX=~/Maildir/INBOX"
                                     ":LAYOUT=fs"))))))

(define (run-dovecot-test)
  "Return a test of an OS running Dovecot service."
  (define vm
    (virtual-machine
     (operating-system (marionette-operating-system
                        %dovecot-os
                        #:imported-modules '((gnu services herd))))
     (port-forwardings '((8143 . 143)))))

  (define test
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (gnu build marionette)
                       (ice-9 iconv)
                       (ice-9 rdelim)
                       (rnrs base)
                       (rnrs bytevectors)
                       (srfi srfi-64))

          (define marionette
            (make-marionette '(#$vm)))

          (define* (message-length message #:key (encoding "iso-8859-1"))
            (bytevector-length (string->bytevector message encoding)))

          (define message "From: test@example.com\n\
Subject: Hello Nice to meet you!")

          (test-runner-current (system-test-runner #$output))
          (test-begin "dovecot")

          ;; Wait for dovecot to be up and running.
          (test-assert "dovecot running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (start-service 'dovecot))
             marionette))

          ;; Check Dovecot service's PID.
          (test-assert "service process id"
            (let ((pid
                   (number->string (wait-for-file "/var/run/dovecot/master.pid"
                                                  marionette))))
              (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
                               marionette)))

          (test-assert "accept an email"
            (let ((imap (socket AF_INET SOCK_STREAM 0))
                  (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
              (connect imap addr)
              ;; Be greeted.
              (read-line imap) ;OK
              ;; Authenticate
              (write-line "a AUTHENTICATE ANONYMOUS" imap)
              (read-line imap) ;+
              (write-line "c2lyaGM=" imap)
              (read-line imap) ;OK
              ;; Create a TESTBOX mailbox
              (write-line "a CREATE TESTBOX" imap)
              (read-line imap) ;OK
              ;; Append a message to a TESTBOX mailbox
              (write-line (format #f "a APPEND TESTBOX {~a}"
                                  (number->string (message-length message)))
                          imap)
              (read-line imap) ;+
              (write-line message imap)
              (read-line imap) ;OK
              ;; Logout
              (write-line "a LOGOUT" imap)
              (close imap)
              #t))

          (test-equal "mail arrived"
            message
            (marionette-eval
             '(begin
                (use-modules (ice-9 ftw)
                             (ice-9 match))
                (let ((TESTBOX/new "/home/alice/Maildir/TESTBOX/new/"))
                  (match (scandir TESTBOX/new)
                    (("." ".." message-file)
                     (call-with-input-file
                         (string-append TESTBOX/new message-file)
                       get-string-all)))))
             marionette))

          (test-end))))

  (gexp->derivation "dovecot-test" test))

(define %test-dovecot
  (system-test
   (name "dovecot")
   (description "Connect to a running Dovecot server.")
   (value (run-dovecot-test))))

(define %getmail-os
  (operating-system
    (inherit (simple-operating-system))

    ;; Set a password for the user account; the test needs it.
    (users (cons (user-account
                  (name "alice")
                  (password (crypt "testpass" "$6$abc"))
                  (comment "Bob's sister")
                  (group "users")
                  (supplementary-groups '("wheel" "audio" "video")))
                 %base-user-accounts))

    (services (cons* (service dhcp-client-service-type)
                     (service dovecot-service-type
                              (dovecot-configuration
                               (disable-plaintext-auth? #f)
                               (ssl? "no")
                               (auth-mechanisms '("anonymous" "plain"))
                               (auth-anonymous-username "alice")
                               (mail-location
                                (string-append "maildir:~/Maildir"
                                               ":INBOX=~/Maildir/INBOX"
                                               ":LAYOUT=fs"))))
                     (service getmail-service-type
                              (list
                               (getmail-configuration
                                (name 'test)
                                (user "alice")
                                (directory "/var/lib/getmail/alice")
                                (idle '("TESTBOX"))
                                (rcfile
                                 (getmail-configuration-file
                                  (retriever
                                   (getmail-retriever-configuration
                                    (type "SimpleIMAPRetriever")
                                    (server "localhost")
                                    (username "alice")
                                    (port 143)
                                    (extra-parameters
                                     '((password . "testpass")
                                       (mailboxes . ("TESTBOX"))))))
                                  (destination
                                   (getmail-destination-configuration
                                    (type "Maildir")
                                    (path "/home/alice/TestMaildir/")))
                                  (options
                                   (getmail-options-configuration
                                    (read-all #f))))))))
                     %base-services))))

(define (run-getmail-test)
  "Return a test of an OS running Getmail service."
  (define vm
    (virtual-machine
     (operating-system (marionette-operating-system
                        %getmail-os
                        #:imported-modules '((gnu services herd))))
     (port-forwardings '((8143 . 143)))))

  (define test
    (with-imported-modules '((gnu build marionette))
      #~(begin
          (use-modules (gnu build marionette)
                       (ice-9 iconv)
                       (ice-9 rdelim)
                       (rnrs base)
                       (rnrs bytevectors)
                       (srfi srfi-64))

          (define marionette
            (make-marionette '(#$vm)))

          (define* (message-length message #:key (encoding "iso-8859-1"))
            (bytevector-length (string->bytevector message encoding)))

          (define message "From: test@example.com\n\
Subject: Hello Nice to meet you!")

          (test-runner-current (system-test-runner #$output))
          (test-begin "getmail")

          ;; Wait for dovecot to be up and running.
          (test-assert "dovecot running"
            (marionette-eval
             '(begin
                (use-modules (gnu services herd))
                (start-service 'dovecot))
             marionette))

          ;; Wait for getmail to be up and running.
          (test-assert "getmail-test running"
            (marionette-eval
             '(let* ((pw (getpw "alice"))
                     (uid (passwd:uid pw))
                     (gid (passwd:gid pw)))
                (use-modules (gnu services herd))

                (for-each
                 (lambda (dir)
                   (mkdir dir)
                   (chown dir uid gid))
                 '("/home/alice/TestMaildir"
                   "/home/alice/TestMaildir/cur"
                   "/home/alice/TestMaildir/new"
                   "/home/alice/TestMaildir/tmp"
                   "/home/alice/TestMaildir/TESTBOX"
                   "/home/alice/TestMaildir/TESTBOX/cur"
                   "/home/alice/TestMaildir/TESTBOX/new"
                   "/home/alice/TestMaildir/TESTBOX/tmp"))

                (start-service 'getmail-test))
             marionette))

          ;; Check Dovecot service's PID.
          (test-assert "service process id"
            (let ((pid
                   (number->string (wait-for-file "/var/run/dovecot/master.pid"
                                                  marionette))))
              (marionette-eval `(file-exists? (string-append "/proc/" ,pid))
                               marionette)))

          (test-assert "accept an email"
            (let ((imap (socket AF_INET SOCK_STREAM 0))
                  (addr (make-socket-address AF_INET INADDR_LOOPBACK 8143)))
              (connect imap addr)
              ;; Be greeted.
              (read-line imap) ;OK
              ;; Authenticate
              (write-line "a AUTHENTICATE ANONYMOUS" imap)
              (read-line imap) ;+
              (write-line "c2lyaGM=" imap)
              (read-line imap) ;OK
              ;; Create a TESTBOX mailbox
              (write-line "a CREATE TESTBOX" imap)
              (read-line imap) ;OK
              ;; Append a message to a TESTBOX mailbox
              (write-line (format #f "a APPEND TESTBOX {~a}"
                                  (number->string (message-length message)))
                          imap)
              (read-line imap) ;+
              (write-line message imap)
              (read-line imap) ;OK
              ;; Logout
              (write-line "a LOGOUT" imap)
              (close imap)
              #t))

          (sleep 1)

          (test-assert "mail arrived"
            (string-contains
             (marionette-eval
              '(begin
                 (use-modules (ice-9 ftw)
                              (ice-9 match))
                 (let ((TESTBOX/new "/home/alice/TestMaildir/new/"))
                   (match (scandir TESTBOX/new)
                     (("." ".." message-file)
                      (call-with-input-file
                          (string-append TESTBOX/new message-file)
                        get-string-all)))))
              marionette)
             message))

          (test-end))))

  (gexp->derivation "getmail-test" test))

(define %test-getmail
  (system-test
   (name "getmail")
   (description "Connect to a running Getmail server.")
   (value (run-getmail-test))))

debug log:

solving 1bac9f50a2 ...
found 1bac9f50a2 in https://yhetil.org/guix-patches/a8ebf5f4f792cb98b7691c40053f30e09386b177.1666632422.git.jbranso@dismail.de/
found f13751b72f in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 f13751b72fafdb37372de17b55001a112f9efc11	gnu/tests/mail.scm

applying [1/1] https://yhetil.org/guix-patches/a8ebf5f4f792cb98b7691c40053f30e09386b177.1666632422.git.jbranso@dismail.de/
diff --git a/gnu/tests/mail.scm b/gnu/tests/mail.scm
index f13751b72f..1bac9f50a2 100644

Checking patch gnu/tests/mail.scm...
Applied patch gnu/tests/mail.scm cleanly.

index at:
100644 1bac9f50a24e82ecc6e2816fdd7144ef3888f03e	gnu/tests/mail.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).