1import ./make-test.nix {
2 name = "dovecot";
3
4 machine = { pkgs, ... }: {
5 imports = [ common/user-account.nix ];
6 services.postfix.enable = true;
7 services.dovecot2.enable = true;
8 services.dovecot2.protocols = [ "imap" "pop3" ];
9 environment.systemPackages = let
10 sendTestMail = pkgs.writeScriptBin "send-testmail" ''
11 #!${pkgs.stdenv.shell}
12 exec sendmail -vt <<MAIL
13 From: root@localhost
14 To: alice@localhost
15 Subject: Very important!
16
17 Hello world!
18 MAIL
19 '';
20
21 sendTestMailViaDeliveryAgent = pkgs.writeScriptBin "send-lda" ''
22 #!${pkgs.stdenv.shell}
23
24 exec ${pkgs.dovecot}/libexec/dovecot/deliver -d bob <<MAIL
25 From: root@localhost
26 To: bob@localhost
27 Subject: Something else...
28
29 I'm running short of ideas!
30 MAIL
31 '';
32
33 testImap = pkgs.writeScriptBin "test-imap" ''
34 #!${pkgs.python3.interpreter}
35 import imaplib
36
37 with imaplib.IMAP4('localhost') as imap:
38 imap.login('alice', 'foobar')
39 imap.select()
40 status, refs = imap.search(None, 'ALL')
41 assert status == 'OK'
42 assert len(refs) == 1
43 status, msg = imap.fetch(refs[0], 'BODY[TEXT]')
44 assert status == 'OK'
45 assert msg[0][1].strip() == b'Hello world!'
46 '';
47
48 testPop = pkgs.writeScriptBin "test-pop" ''
49 #!${pkgs.python3.interpreter}
50 import poplib
51
52 pop = poplib.POP3('localhost')
53 try:
54 pop.user('bob')
55 pop.pass_('foobar')
56 assert len(pop.list()[1]) == 1
57 status, fullmail, size = pop.retr(1)
58 assert status.startswith(b'+OK ')
59 body = b"".join(fullmail[fullmail.index(b""):]).strip()
60 assert body == b"I'm running short of ideas!"
61 finally:
62 pop.quit()
63 '';
64
65 in [ sendTestMail sendTestMailViaDeliveryAgent testImap testPop ];
66 };
67
68 testScript = ''
69 $machine->waitForUnit('postfix.service');
70 $machine->waitForUnit('dovecot2.service');
71 $machine->succeed('send-testmail');
72 $machine->succeed('send-lda');
73 $machine->waitUntilFails('[ "$(postqueue -p)" != "Mail queue is empty" ]');
74 $machine->succeed('test-imap');
75 $machine->succeed('test-pop');
76 '';
77}