An SMTP client and server library for Zig implementing RFC 5321.
0

Configure Feed

Select the types of activity you want to include in your feed.

zig-smtp / nix / interop-test.nix
11 kB 327 lines
1# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us> 2# SPDX-License-Identifier: MIT 3 4# NixOS VM test exercising zsmtp against third-party implementations: 5# - zsmtp client -> Postfix (25/465) and Exim (2625/2626): plaintext, 6# STARTTLS, and implicit TLS against each, verified by checking local 7# delivery to alice's mailbox 8# - swaks -> zsmtp server: plaintext and STARTTLS, verified by checking 9# the received message in the server's journal 10 11{ 12 testers, 13 callPackage, 14 runCommand, 15 openssl, 16}: 17let 18 zsmtp = callPackage ../package.nix { }; 19 20 snakeoil = 21 runCommand "zsmtp-test-cert" 22 { 23 nativeBuildInputs = [ openssl ]; 24 } 25 '' 26 mkdir -p $out 27 openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 \ 28 -keyout $out/key.pem -out $out/cert.pem -days 36500 -nodes \ 29 -subj "/CN=localhost" \ 30 -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" 31 ''; 32in 33testers.runNixOSTest { 34 name = "zsmtp-interop"; 35 36 nodes.machine = 37 { pkgs, ... }: 38 { 39 environment.systemPackages = [ 40 zsmtp 41 pkgs.swaks 42 ]; 43 44 users.users.alice.isNormalUser = true; 45 46 services.postfix = { 47 enable = true; 48 # Implicit-TLS smtpd on port 465; allow loopback without SASL. 49 enableSubmissions = true; 50 submissionsOptions = { 51 smtpd_client_restrictions = "permit_mynetworks,reject"; 52 }; 53 settings.main = { 54 mydestination = [ 55 "localhost" 56 "$myhostname" 57 ]; 58 smtpd_tls_security_level = "may"; 59 smtpd_tls_chain_files = [ 60 "${snakeoil}/key.pem" 61 "${snakeoil}/cert.pem" 62 ]; 63 }; 64 }; 65 66 # Exim: plaintext + STARTTLS on 2625, implicit TLS on 2626, delivering 67 # to /var/spool/exim-mail/<user>. 68 services.exim = { 69 enable = true; 70 config = '' 71 primary_hostname = machine.test 72 qualify_domain = localhost 73 local_interfaces = 127.0.0.1 74 daemon_smtp_ports = 2625 : 2626 75 tls_on_connect_ports = 2626 76 tls_advertise_hosts = * 77 tls_certificate = ${snakeoil}/cert.pem 78 tls_privatekey = ${snakeoil}/key.pem 79 acl_smtp_rcpt = acl_rcpt 80 81 begin acl 82 83 acl_rcpt: 84 accept 85 86 begin routers 87 88 local_users: 89 driver = accept 90 local_parts = alice : bob 91 transport = local_delivery 92 93 begin transports 94 95 local_delivery: 96 driver = appendfile 97 file = /var/spool/exim-mail/$local_part_data 98 user = exim 99 delivery_date_add 100 envelope_to_add 101 return_path_add 102 103 begin authenticators 104 105 plain_server: 106 driver = plaintext 107 public_name = PLAIN 108 server_prompts = : 109 server_condition = ''${if and{{eq{$auth2}{alice}}{eq{$auth3}{secret}}}} 110 server_set_id = $auth2 111 112 login_server: 113 driver = plaintext 114 public_name = LOGIN 115 server_prompts = Username:: : Password:: 116 server_condition = ''${if and{{eq{$auth1}{alice}}{eq{$auth2}{secret}}}} 117 server_set_id = $auth1 118 ''; 119 }; 120 121 systemd.tmpfiles.rules = [ 122 "d /var/spool/exim-mail 0755 exim exim -" 123 ]; 124 125 systemd.services.zsmtp-server = { 126 description = "zsmtp debug server (plaintext)"; 127 wantedBy = [ "multi-user.target" ]; 128 serviceConfig = { 129 ExecStart = "${zsmtp}/bin/zsmtp serve 2525"; 130 DynamicUser = true; 131 }; 132 }; 133 134 systemd.services.zsmtp-server-tls = { 135 description = "zsmtp debug server (STARTTLS)"; 136 wantedBy = [ "multi-user.target" ]; 137 serviceConfig = { 138 ExecStart = "${zsmtp}/bin/zsmtp serve --tls-cert ${snakeoil}/cert.pem --tls-key ${snakeoil}/key.pem 2526"; 139 DynamicUser = true; 140 }; 141 }; 142 143 systemd.services.zsmtp-server-tlsc = { 144 description = "zsmtp debug server (implicit TLS)"; 145 wantedBy = [ "multi-user.target" ]; 146 serviceConfig = { 147 ExecStart = "${zsmtp}/bin/zsmtp serve --tls-cert ${snakeoil}/cert.pem --tls-key ${snakeoil}/key.pem --implicit-tls 2528"; 148 DynamicUser = true; 149 }; 150 }; 151 152 systemd.services.zsmtp-server-auth = { 153 description = "zsmtp debug server (authentication required)"; 154 wantedBy = [ "multi-user.target" ]; 155 serviceConfig = { 156 ExecStart = "${zsmtp}/bin/zsmtp serve --auth alice:secret 2527"; 157 DynamicUser = true; 158 }; 159 }; 160 }; 161 162 testScript = '' 163 machine.wait_for_unit("postfix.service") 164 machine.wait_for_open_port(25) 165 machine.wait_for_open_port(465) 166 machine.wait_for_unit("exim.service") 167 machine.wait_for_open_port(2625) 168 machine.wait_for_open_port(2626) 169 machine.wait_for_unit("zsmtp-server.service") 170 machine.wait_for_unit("zsmtp-server-tls.service") 171 machine.wait_for_unit("zsmtp-server-tlsc.service") 172 machine.wait_for_unit("zsmtp-server-auth.service") 173 machine.wait_for_open_port(2525) 174 machine.wait_for_open_port(2526) 175 machine.wait_for_open_port(2527) 176 machine.wait_for_open_port(2528) 177 178 179 def deliver(flags, port, needle, mailbox): 180 machine.succeed( 181 f"printf 'Subject: interop\\r\\n\\r\\n{needle}\\r\\n'" 182 f" | zsmtp send {flags} 127.0.0.1 {port}" 183 " bob@example.com alice@localhost" 184 ) 185 machine.wait_until_succeeds(f"grep -r '{needle}' {mailbox}", timeout=60) 186 187 188 servers = { 189 "postfix": (25, 465, "/var/spool/mail/alice/"), 190 "exim": (2625, 2626, "/var/spool/exim-mail/alice"), 191 } 192 193 for name, (port, tls_port, mailbox) in servers.items(): 194 with subtest(f"zsmtp client to {name}, plaintext"): 195 deliver("", port, f"zsmtp to {name} plain", mailbox) 196 197 with subtest(f"zsmtp client to {name}, STARTTLS"): 198 deliver( 199 "--starttls --insecure", port, f"zsmtp to {name} starttls", mailbox 200 ) 201 202 with subtest(f"zsmtp client to {name}, implicit TLS"): 203 deliver("--tls --insecure", tls_port, f"zsmtp to {name} smtps", mailbox) 204 205 for name, (port, tls_port, mailbox) in servers.items(): 206 with subtest(f"zsmtp client to {name}, CHUNKING"): 207 deliver("--chunking", port, f"zsmtp to {name} chunked", mailbox) 208 209 with subtest("zsmtp client to postfix, SMTPUTF8"): 210 machine.succeed( 211 "printf 'Subject: interop\\r\\n\\r\\nzsmtp to postfix utf8\\r\\n'" 212 " | zsmtp send --smtputf8 127.0.0.1 25" 213 " 'böb@example.com' alice@localhost" 214 ) 215 machine.wait_until_succeeds( 216 "grep -r 'zsmtp to postfix utf8' /var/spool/mail/alice/", timeout=60 217 ) 218 219 # Exim advertises only PLAIN and LOGIN, both of which put the password 220 # on the wire, so the client refuses them over this plaintext loopback 221 # connection unless it is told to allow it. 222 with subtest("zsmtp client to exim, AUTH PLAIN"): 223 deliver( 224 "--allow-cleartext-auth --user alice --password secret --auth-method plain", 225 2625, 226 "zsmtp to exim auth plain", 227 "/var/spool/exim-mail/alice", 228 ) 229 230 with subtest("zsmtp client to exim, AUTH LOGIN"): 231 deliver( 232 "--allow-cleartext-auth --user alice --password secret --auth-method login", 233 2625, 234 "zsmtp to exim auth login", 235 "/var/spool/exim-mail/alice", 236 ) 237 238 with subtest("zsmtp client to exim, wrong password is rejected"): 239 machine.fail( 240 "printf 'Subject: interop\\r\\n\\r\\nnope\\r\\n'" 241 " | zsmtp send --allow-cleartext-auth --user alice --password wrong" 242 " 127.0.0.1 2625 bob@example.com alice@localhost" 243 ) 244 245 # The refusal itself, which is what stops a password reaching the network 246 # by accident: the same delivery without the opt-in must not go through. 247 with subtest("zsmtp client refuses cleartext AUTH without the opt-in"): 248 machine.fail( 249 "printf 'Subject: interop\\r\\n\\r\\nnope\\r\\n'" 250 " | zsmtp send --user alice --password secret 127.0.0.1 2625" 251 " bob@example.com alice@localhost" 252 ) 253 # ...and over STARTTLS it goes through with no opt-in at all. 254 deliver( 255 "--starttls --insecure --user alice --password secret", 256 2625, 257 "zsmtp to exim auth over starttls", 258 "/var/spool/exim-mail/alice", 259 ) 260 261 with subtest("swaks to zsmtp server, AUTH PLAIN"): 262 machine.succeed( 263 "swaks --server 127.0.0.1:2527 --auth PLAIN --auth-user alice" 264 " --auth-password secret --from bob@example.com" 265 " --to alice@example.net --body 'swaks to zsmtp auth plain'" 266 ) 267 machine.wait_until_succeeds( 268 "journalctl -u zsmtp-server-auth | grep 'swaks to zsmtp auth plain'", 269 timeout=60, 270 ) 271 272 with subtest("swaks to zsmtp server, AUTH LOGIN"): 273 machine.succeed( 274 "swaks --server 127.0.0.1:2527 --auth LOGIN --auth-user alice" 275 " --auth-password secret --from bob@example.com" 276 " --to alice@example.net --body 'swaks to zsmtp auth login'" 277 ) 278 machine.wait_until_succeeds( 279 "journalctl -u zsmtp-server-auth | grep 'swaks to zsmtp auth login'", 280 timeout=60, 281 ) 282 283 with subtest("swaks to zsmtp server, wrong password is rejected"): 284 machine.fail( 285 "swaks --server 127.0.0.1:2527 --auth PLAIN --auth-user alice" 286 " --auth-password wrong --from bob@example.com" 287 " --to alice@example.net --body nope" 288 ) 289 290 with subtest("unauthenticated mail to auth-required server is rejected"): 291 machine.fail( 292 "printf 'Subject: interop\\r\\n\\r\\nnope\\r\\n'" 293 " | zsmtp send 127.0.0.1 2527 bob@example.com alice@example.net" 294 ) 295 296 with subtest("swaks to zsmtp server, plaintext"): 297 machine.succeed( 298 "swaks --server 127.0.0.1:2525 --from bob@example.com" 299 " --to alice@example.net --header 'Subject: swaks plain'" 300 " --body 'swaks to zsmtp plain'" 301 ) 302 machine.wait_until_succeeds( 303 "journalctl -u zsmtp-server | grep 'swaks to zsmtp plain'", timeout=60 304 ) 305 306 with subtest("swaks to zsmtp server, implicit TLS"): 307 machine.succeed( 308 "swaks --tlsc --server 127.0.0.1:2528 --from bob@example.com" 309 " --to alice@example.net --header 'Subject: swaks tlsc'" 310 " --body 'swaks to zsmtp implicit tls'" 311 ) 312 machine.wait_until_succeeds( 313 "journalctl -u zsmtp-server-tlsc | grep 'swaks to zsmtp implicit tls'", 314 timeout=60, 315 ) 316 317 with subtest("swaks to zsmtp server, STARTTLS"): 318 machine.succeed( 319 "swaks --tls --server 127.0.0.1:2526 --from bob@example.com" 320 " --to alice@example.net --header 'Subject: swaks starttls'" 321 " --body 'swaks to zsmtp starttls'" 322 ) 323 machine.wait_until_succeeds( 324 "journalctl -u zsmtp-server-tls | grep 'swaks to zsmtp starttls'", timeout=60 325 ) 326 ''; 327}