tc_exim4_live.rb 12.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
require 'test/unit'
require 'time'
require 'net/smtp'
require 'symbiosis/domain'
require 'symbiosis/domain/mailbox'

class TestEximLive < Test::Unit::TestCase

  def setup
    @domain = Symbiosis::Domain.new()
    @domain.create

13
14
15
    # Make sure the FQDN is created too.
    Symbiosis::Domain.new(Symbiosis::Host.fqdn)

16
17
    @mailbox = @domain.create_mailbox("test")
    @mailbox.encrypt_password = false
18
19
    @mailbox_password = Symbiosis::Utils.random_string
    @mailbox.password = @mailbox_password
20
21

    @mailbox_crypt = @domain.create_mailbox("te-s.t_crypt")
22
23
    @mailbox_crypt_password = Symbiosis::Utils.random_string
    @mailbox_crypt.password = @mailbox_crypt_password
24
25

    @ssl_ctx = OpenSSL::SSL::SSLContext.new("TLSv1_client")
Paul Cammish's avatar
Paul Cammish committed
26
    @ssl_ctx.ssl_version = :TLSv1_2
27
28
29
30
31
32
33
    @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  def teardown
    @domain.destroy unless $DEBUG
  end

34
35
36
37
38
39
40
41
42
43
44
  def do_skip(msg)
    if self.respond_to?(:skip)
      skip msg
    elsif self.respond_to?(:omit)
      omit msg
    else
      puts "Skipping #{self.method_name} -- #{msg}"
    end
    return nil
  end

45
46
47
  def fetch_test_user
    test_user = nil
    begin
48
      test_user = Etc.getpwnam("sympl-test")
49
50
51
52
53
54
55
    rescue ArgumentError
      # do nothing
    end
    test_user
  end

  def fetch_hostname
56
    Symbiosis::Host.fqdn
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
  end

  def test_smtp_capabilities
    smtp = Net::SMTP.new('localhost', 25) 
    smtp.debug_output = $stdout if $DEBUG

    smtp.start do 
      assert(smtp.capable_starttls?,"STARTTLS is not advertised on port 25")
      assert(!smtp.capable_plain_auth?, "AUTH PLAIN advertised without TLS")
      assert(!smtp.capable_login_auth?, "AUTH LOGIN advertised without TLS")
    end

    smtp.enable_starttls( @ssl_ctx )
    smtp.start do 
        assert(smtp.capable_plain_auth?, "AUTH PLAIN not advertised after STARTTLS")
        assert(smtp.capable_login_auth?, "AUTH LOGIN not advertised after STARTTLS")
    end
  end

  def test_smtp_auth
    smtp = Net::SMTP.new('localhost', 25) 
    smtp.debug_output = $stdout if $DEBUG
    smtp.enable_starttls( @ssl_ctx )

    smtp.start do
      assert_nothing_raised("AUTH PLAIN failed for a user with plaintext password") do
83
        smtp.auth_login(@mailbox.username, @mailbox_password)
84
85
86
87
88
      end
    end

    smtp.start do
      assert_nothing_raised("AUTH LOGIN failed for a user with plaintext password") do
89
        smtp.auth_login(@mailbox.username, @mailbox_password)
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
      end
    end
  end

  def test_smtp_auth_local_user
    test_user = fetch_test_user
    return do_skip "No test user" if test_user.nil?

    hostname = fetch_hostname
    username = test_user.name + "@" + hostname
    password = Symbiosis::Utils.random_string

    File.open(File.join(test_user.dir,".password"),"w+"){|fh| fh.puts password}

    smtp = Net::SMTP.new('localhost', 25) 
    smtp.debug_output = $stdout if $DEBUG
    smtp.enable_starttls( @ssl_ctx )

    smtp.start do
      assert_nothing_raised("AUTH PLAIN failed for the local user") do
        smtp.auth_plain(username, password)
      end
    end

    smtp.start do
      assert_nothing_raised("AUTH LOGIN failed for the local user") do
        smtp.auth_login(username, password)
      end
    end
119
120
121
122
123
124
125


    smtp.start do
      assert_nothing_raised("AUTH PLAIN failed for the local user (with no domain)") do
        smtp.auth_plain(test_user.name, password)
      end
    end
126
127
128
129
130
131
132
133
134
  end

  def test_smtp_auth_crypt
    smtp = Net::SMTP.new('localhost', 25) 
    smtp.debug_output = $stdout if $DEBUG
    smtp.enable_starttls( @ssl_ctx )

    smtp.start do
      assert_nothing_raised("AUTH PLAIN failed for a user with crypt'd password") do
135
        smtp.auth_login(@mailbox_crypt.username, @mailbox_crypt_password)
136
137
138
139
140
      end
    end

    smtp.start do
      assert_nothing_raised("AUTH PLAIN failed for a user with crypt'd password") do
141
        smtp.auth_login(@mailbox_crypt.username, @mailbox_crypt_password)
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
      end
    end
  end

  def test_ratelimiting
    smtp = Net::SMTP.new('localhost', 25)
    smtp.debug_output = $stdout if $DEBUG
    smtp.enable_starttls( @ssl_ctx )
    msg =<<EOF
Return-Path: #{@mailbox_crypt.username}
Envelope-To: #{@mailbox.username}
Date: #{Time.now.rfc2822}
From: #{@mailbox_crypt.username}
To: #{@mailbox.username}

Testing 1.2.3..
--
159
Sympl Test
160
161
162
163
164
EOF


    smtp.start do
      assert_nothing_raised("AUTH PLAIN failed for a user with crypt'd password") do
165
        smtp.auth_login(@mailbox_crypt.username, @mailbox_crypt_password)
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
      end

      assert_nothing_raised do
        3.times do
          smtp.send_message msg, @mailbox_crypt.username, @mailbox.username
        end
      end

      Symbiosis::Utils.set_param("mailbox-ratelimit","2",@domain.config_dir)

      assert_raise(Net::SMTPFatalError) do
        3.times do
          smtp.send_message msg, @mailbox_crypt.username, @mailbox.username
        end
      end

    end
  ensure
    10.times do
      break if `/usr/sbin/exiqgrep -i -f '#{@mailbox_crypt.username}'`.length == 0
      sleep 1
    end
  end

#  def do_test_deliver(mailbox)
#    sender_address = "postmaster@#{mailbox.domain.name}"
#    rcpt_address   = mailbox.username
#    msg =<<EOF
#Return-Path: #{sender_address}
#Envelope-To: #{rcpt_address}
#Date: #{Time.now.rfc2822}
#From: #{sender_address}
#To: #{rcpt_address}
#
#Testing 1.2.3..
#--
202
#Sympl Test
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
#EOF
#
#    do_dovecot_delivery(sender_address, rcpt_address, msg, 0, mailbox)
#
#    new_files = Dir.glob(File.join(mailbox.maildir, "new", "*")).length
#    assert_equal(1, new_files, "Found #{new_files} messages in Maildir/new rather than just 1")
#  end
#  
#  def do_setup_local_mailbox(test_user)
#    hostname = fetch_hostname
#    mailbox = Symbiosis::Domains.find_mailbox(test_user.name + "@" + hostname)
#
#    #
#    # AWOOGA.

#    #
#    FileUtils.rm_rf(mailbox.maildir)
#
#    return mailbox
#  end
#
#  def test_deliver
#    do_test_deliver(@mailbox)
#  end
#
#  def test_deliver_local_user
#    test_user = fetch_test_user
#    return do_skip "No test user" if test_user.nil?
#    mailbox = do_setup_local_mailbox(test_user)
#
#    do_test_deliver(mailbox)
#  end
#
#  def test_smtp_quotas
#    #
#    # SMTP quotas are done in units of kibibytes, apparently.
#    #
#    @mailbox.quota = "50ki"
#    assert_equal(51200, @mailbox.quota, "Mailbox quota not set correctly.")
#
#    quotaroot = nil
#    quota = nil
#
#    assert_nothing_raised do
#      smtp = Net::SMTP.new('localhost', 25, false)
248
#      smtp.authenticate('LOGIN', @mailbox.username, @mailbox_password)
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#
#      #
#      # Now check the quotaroot.
#      #
#      qra = smtp.getquotaroot("INBOX")
#      quotaroot = qra.find{|q| q.is_a?(Net::SMTP::MailboxQuotaRoot)}
#
#      #
#      # And the quotas.
#      #
#      quota = qra.find{|q| q.is_a?(Net::SMTP::MailboxQuota)}
#
#      smtp.logout
#      smtp.disconnect unless smtp.disconnected?
#    end
#
#    assert_equal("INBOX", quotaroot.mailbox, "Quota root returned the wrong mailbox.")
266
267
#    assert_equal(["Sympl mailbox quota"], quotaroot.quotaroots, "Quota root returned the wrong set of quota roots.")
#    assert_equal("Sympl mailbox quota", quota.mailbox)
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
#    assert_equal(0, quota.usage.to_i)
#    assert_equal(50, quota.quota.to_i)
#  end
#
#  def do_test_deliver_with_quotas(mailbox)
#    #
#    # SMTP quotas are done in units of kibibytes, apparently.
#    #
#    mailbox.quota = "50ki"
#    assert_equal(51200, mailbox.quota, "Mailbox quota not set correctly.")
#
#    #
#    # An SMTP/POP3 login should trigger this normally.
#    #
#    @mailbox.rebuild_maildirsize
#
#    sender_address = "postmaster@#{mailbox.domain.to_s}"
#    rcpt_address   = mailbox.username
#    msg =<<EOF
#Return-Path: #{sender_address}
#Envelope-To: #{rcpt_address}
#Date: #{Time.now.rfc2822}
#From: #{sender_address}
#To: #{rcpt_address}
#
#Testing 1.2.3..
#--
295
#Sympl Test
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
#EOF
#
#    #
#    # A small message should go through just fine
#    #
#    do_dovecot_delivery(sender_address, rcpt_address, msg)
#
#    #
#    # Make sure we've got the right number of messages in new/
#    #
#    new_files = Dir.glob(File.join(mailbox.maildir, "new", "*")).length
#    assert_equal(1, new_files, "Found #{new_files} messages in Maildir/new rather than just 1")
#
#    #
#    # Now make our message unfeasably long
#    #
#    msg += "x"*mailbox.quota
#
#    #
#    # Deliver should return a temporary failure message.
#    #
#    do_dovecot_delivery(sender_address, rcpt_address, msg, 75)
#
#    #
#    # And nothing should be delivered.
#    #
#    new_files = Dir.glob(File.join(mailbox.maildir, "new", "*")).length
#    assert_equal(1, new_files, "Found #{new_files} messages in Maildir/new rather than just 1")
#  end
#  
#  def test_deliver_with_quotas
#    do_test_deliver_with_quotas(@mailbox)
#  end
#
#  def do_test_deliver_with_sieve(mailbox)
#    sieve =<<EOF
#require "fileinto";
#
#fileinto "testing";
#stop;
#EOF
#
#    # Write the file.
#    Symbiosis::Utils.set_param(mailbox.dot + "sieve", sieve, mailbox.directory)
#
#    sender_address = "postmaster@#{mailbox.domain.to_s}"
#    rcpt_address   = mailbox.username
#    msg =<<EOF
#Return-Path: #{sender_address}
#Envelope-To: #{rcpt_address}
#Date: #{Time.now.rfc2822}
#From: #{sender_address}
#To: #{rcpt_address}
#
#Testing 1.2.3..
#--
352
#Sympl Test
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
#EOF
#
#    #
#    # Now deliver our message
#    #
#    do_dovecot_delivery(sender_address, rcpt_address, msg, 0, mailbox)
#
#    #
#    # And nothing should be delivered to the inbox.
#    #
#    new_files = Dir.glob(File.join(mailbox.maildir, "new", "*")).length
#    assert_equal(0, new_files, "Found #{new_files} messages in Maildir/new rather than 0")
#
#    #
#    # It should be delivered to the "testing" box.
#    #
#    new_files = Dir.glob(File.join(mailbox.maildir, ".testing", "new", "*")).length
#    assert_equal(1, new_files, "Found #{new_files} messages in Maildir/.testing/new rather than just 1")
#  end
#
#  def test_deliver_with_sieve
#    @mailbox.create
#    do_test_deliver_with_sieve(@mailbox)
#  end
#
#  def test_deliver_with_sieve_for_local_users
#    test_user = fetch_test_user
#    return do_skip "No test user" if test_user.nil?
#    mailbox = do_setup_local_mailbox(test_user)
#    sieve_file = File.join(mailbox.directory, ".sieve")
#
#    do_test_deliver_with_sieve(mailbox)
#  ensure
#    File.unlink(sieve_file) if sieve_file and File.exist?(sieve_file)
#  end
#
#  def do_test_deliver_with_sieve_and_quota(mailbox)
#    mailbox.create
#    sieve =<<EOF
#require "fileinto";
#
#fileinto "testing";
#stop;
#EOF
#
#    # Write the file.
#    Symbiosis::Utils.set_param(mailbox.dot + "sieve", sieve, mailbox.directory)
#
#    sender_address = "postmaster@#{mailbox.domain.to_s}"
#    rcpt_address   = mailbox.username
#    msg =<<EOF
#Return-Path: #{sender_address}
#Envelope-To: #{rcpt_address}
#Date: #{Time.now.rfc2822}
#From: #{sender_address}
#To: #{rcpt_address}
#
#Testing 1.2.3..
#--
412
#Sympl Test
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
#EOF
#
#    #
#    # Now create a quota 
#    #
#    mailbox.quota = "5ki"
#    mailbox.rebuild_maildirsize
#    
#    # 
#    # And make our message VERY long.
#    # 
#    msg += "x"*mailbox.quota
#
#    #
#    # And try to deliver again, this should temp fail.
#    #
#    do_dovecot_delivery(sender_address, rcpt_address, msg, 75, mailbox)
#
#    #
#    # And nothing should be delivered.
#    #
#    new_files = Dir.glob(File.join(mailbox.maildir, ".testing", "new", "*")).length
#    assert_equal(0, new_files, "Found #{new_files} messages in Maildir/.testing/new rather than just 1 after the quota has been exceeded.")
#  end
#
#  def test_deliver_with_sieve_and_quota
#    do_test_deliver_with_sieve_and_quota(@mailbox)
#  end
#
#  #
#  # This is fugly, but required to drop privileges properly.
#  #
#  def do_dovecot_delivery(sender_address, rcpt_address, msg, expected_code=0, mailbox = @mailbox)
#    fork do 
#      #
#      # Drop privileges
#      #
#      if 0 == Process.uid
#        Process::Sys.setgid(mailbox.gid)
#        Process::Sys.setuid(mailbox.uid)
#      end
#
#      ENV.keys.each do |k|
#        ENV[k] = nil
#      end
#      
#      ENV['HOME'] = mailbox.directory
#      ENV['USER'] = mailbox.username
#
#      cmd = "/usr/lib/dovecot/deliver -e -k -f \"#{sender_address}\" -d \"#{rcpt_address}\""
#
#      IO.popen(cmd,"w+") do |pipe|
#        pipe.puts msg
#      end
#
#      exit $?.exitstatus
#    end
#
#    Process.wait
#
#    assert_equal(expected_code, $?.exitstatus, "Dovecot deliver failed with the wrong exit code (#{$?.to_i})")
#  end
#
#end
#

end