dovecot-config.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import assert from 'node:assert/strict';
  2. import { readFileSync } from 'node:fs';
  3. import { test } from 'node:test';
  4. const compose = readFileSync(new URL('../docker-compose.yml', import.meta.url), 'utf8');
  5. const appService = compose.match(/^ app:\n[\s\S]*?(?=^ dovecot:)/m)?.[0] || '';
  6. const dovecotService = compose.match(/^ dovecot:\n[\s\S]*?(?=^ postfix:)/m)?.[0] || '';
  7. const envExample = readFileSync(new URL('../.env.example', import.meta.url), 'utf8');
  8. const readme = readFileSync(new URL('../README.md', import.meta.url), 'utf8');
  9. const maildirSyncSource = readFileSync(new URL('../src/maildir-sync.js', import.meta.url), 'utf8');
  10. const serverSource = readFileSync(new URL('../src/server.js', import.meta.url), 'utf8');
  11. const authConfig = readFileSync(new URL('../docker/dovecot/auth.conf', import.meta.url), 'utf8');
  12. const mailConfig = readFileSync(new URL('../docker/dovecot/mailhub.conf', import.meta.url), 'utf8');
  13. const sslConfig = readFileSync(new URL('../docker/dovecot/ssl.conf', import.meta.url), 'utf8');
  14. const authLua = readFileSync(new URL('../docker/dovecot/auth.lua', import.meta.url), 'utf8');
  15. test('Maildir reconciliation defaults to a five-minute polling interval', () => {
  16. assert.match(serverSource, /MAILDIR_SYNC_INTERVAL_MS \|\| 300000/);
  17. assert.match(maildirSyncSource, /intervalMs = 300_000/);
  18. assert.match(maildirSyncSource, /Number\(intervalMs\) \|\| 300_000/);
  19. assert.match(envExample, /^MAILDIR_SYNC_INTERVAL_MS=300000$/m);
  20. assert.match(readme, /MAILDIR_SYNC_INTERVAL_MS[^\n]*`300000` 毫秒(5 分钟)/);
  21. });
  22. test('Compose delegates public IMAP and POP3 ports to rootless Dovecot', () => {
  23. assert.match(compose, /image: dovecot\/dovecot:2\.4\.4/);
  24. for (const mapping of ['143:31143', '993:31993', '110:31110', '995:31995']) {
  25. assert.ok(compose.includes(mapping), `missing Dovecot port mapping ${mapping}`);
  26. }
  27. for (const oldMapping of ['143:143', '993:993', '110:110', '995:995']) {
  28. assert.equal(compose.includes(oldMapping), false, `app still owns ${oldMapping}`);
  29. }
  30. assert.match(compose, /dovecot_internal:\n\s+internal: true/);
  31. assert.match(dovecotService, /networks:\n\s+- dovecot_internal\n\s+- dovecot_public/);
  32. assert.match(compose, /^ dovecot_public:\s*$/m);
  33. assert.doesNotMatch(compose, /dovecot_public:\n\s+internal: true/);
  34. assert.match(appService, /networks:\n\s+- mailhub\n\s+- dovecot_internal/);
  35. assert.doesNotMatch(appService, /dovecot_public/);
  36. assert.match(compose, /file: \.\/data\/secrets\/dovecot_auth_secret/);
  37. assert.match(compose, /MAIL_ACCESS_BACKEND: dovecot/);
  38. assert.match(compose, /MAILDIR_ROOT: \/data\/maildir/);
  39. assert.match(compose, /test -r \/run\/secrets\/dovecot_auth_secret/);
  40. assert.match(compose, /test -s \/run\/secrets\/dovecot_auth_secret/);
  41. assert.match(compose, /test -w \/srv\/vmail/);
  42. assert.match(compose, /doveadm service status imap-login pop3-login/);
  43. });
  44. test('Dovecot uses Lua passdb, a static rootless userdb, and Maildir storage', () => {
  45. assert.match(authConfig, /passdb lua \{/);
  46. assert.match(authConfig, /SUBMISSION_TLS_CERT = %\{env:SUBMISSION_TLS_CERT\}/);
  47. assert.match(authConfig, /SUBMISSION_TLS_KEY = %\{env:SUBMISSION_TLS_KEY\}/);
  48. assert.match(authConfig, /lua_file = \/etc\/dovecot\/auth\.lua/);
  49. assert.match(authConfig, /userdb static \{/);
  50. assert.match(authConfig, /userdb static \{[\s\S]*allow_all_users = yes/);
  51. assert.match(authConfig, /uid = 1000/);
  52. assert.match(authConfig, /gid = 1000/);
  53. assert.match(authConfig, /home = \/srv\/vmail\/%\{user \| lower\}/);
  54. assert.match(mailConfig, /^protocols = imap pop3$/m);
  55. assert.match(mailConfig, /^mail_driver = maildir$/m);
  56. assert.match(mailConfig, /^mail_path = ~\/mail$/m);
  57. assert.match(mailConfig, /^mailbox_list_layout = maildir\+\+$/m);
  58. assert.match(mailConfig, /^mailbox_list_storage_escape_char = \^$/m);
  59. assert.match(mailConfig, /^mailbox_list_utf8 = no$/m);
  60. for (const [mailbox, specialUse] of [
  61. ['Archive', 'Archive'],
  62. ['Drafts', 'Drafts'],
  63. ['Junk', 'Junk'],
  64. ['Sent', 'Sent'],
  65. ['Trash', 'Trash']
  66. ]) {
  67. assert.match(
  68. mailConfig,
  69. new RegExp(`mailbox ${mailbox} \\{[\\s\\S]*?auto = subscribe[\\s\\S]*?special_use = \\\\${specialUse}`)
  70. );
  71. }
  72. assert.match(mailConfig, /service imap-login \{[\s\S]*chroot =/);
  73. assert.match(mailConfig, /service imap-login \{[\s\S]*inet_listener imaps \{[\s\S]*ssl = yes/);
  74. assert.match(mailConfig, /service pop3-login \{[\s\S]*inet_listener pop3s \{[\s\S]*ssl = yes/);
  75. assert.match(sslConfig, /^ssl_server_cert_file = \$ENV:SUBMISSION_TLS_CERT$/m);
  76. assert.match(sslConfig, /^ssl_server_key_file = \$ENV:SUBMISSION_TLS_KEY$/m);
  77. });
  78. test('Lua passdb sends both IMAP and POP3 to the private auth bridge', () => {
  79. assert.match(authLua, /http:\/\/app:3001\/internal\/dovecot\/auth/);
  80. assert.match(authLua, /\/run\/secrets\/dovecot_auth_secret/);
  81. assert.match(authLua, /first_nonempty_string\(request\.protocol, request\.service\)/);
  82. assert.match(authLua, /request\.remote_ip,[\s\S]*request\.real_remote_ip/);
  83. assert.match(authLua, /protocol ~= "imap" and protocol ~= "pop3"/);
  84. assert.match(authLua, /request_max_attempts = 1/);
  85. assert.match(authLua, /auto_retry = "no"/);
  86. assert.match(authLua, /request_absolute_timeout = "2s"/);
  87. assert.match(authLua, /status ~= 200[\s\S]*PASSDB_RESULT_INTERNAL_FAILURE/);
  88. assert.doesNotMatch(authLua, /status == (?:401|403|404)/);
  89. assert.match(
  90. authLua,
  91. /payload\.authenticated == false[\s\S]*PASSDB_RESULT_PASSWORD_MISMATCH[\s\S]*payload\.authenticated ~= true[\s\S]*PASSDB_RESULT_INTERNAL_FAILURE/
  92. );
  93. assert.match(authLua, /valid_user\(payload\.user\)/);
  94. assert.match(authLua, /PASSDB_RESULT_OK, \{ user = string\.lower\(payload\.user\) \}/);
  95. assert.doesNotMatch(authLua, /log_(?:debug|info|warning|error).*password/i);
  96. });