diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/net/smtp/test_smtp.rb | 94 |
1 files changed, 88 insertions, 6 deletions
diff --git a/test/net/smtp/test_smtp.rb b/test/net/smtp/test_smtp.rb index 90c92e06f8..507caee025 100644 --- a/test/net/smtp/test_smtp.rb +++ b/test/net/smtp/test_smtp.rb @@ -184,17 +184,99 @@ module Net end end + def test_start + port = fake_server_start + smtp = Net::SMTP.start('localhost', port) + smtp.quit + end + + def test_start_with_position_argument + port = fake_server_start(helo: 'myname', user: 'account', password: 'password') + smtp = Net::SMTP.start('localhost', port, 'myname', 'account', 'password', :plain) + smtp.quit + end + + def test_start_with_keyword_argument + port = fake_server_start(helo: 'myname', user: 'account', password: 'password') + smtp = Net::SMTP.start('localhost', port, helo: 'myname', user: 'account', secret: 'password', authtype: :plain) + smtp.quit + end + + def test_start_password_is_secret + port = fake_server_start(helo: 'myname', user: 'account', password: 'password') + smtp = Net::SMTP.start('localhost', port, helo: 'myname', user: 'account', password: 'password', authtype: :plain) + smtp.quit + end + + def test_start_invalid_number_of_arguments + err = assert_raise ArgumentError do + Net::SMTP.start('localhost', 25, 'myname', 'account', 'password', :plain, :invalid_arg) + end + assert_equal('wrong number of arguments (given 7, expected 1..6)', err.message) + end + + def test_start_instance + port = fake_server_start + smtp = Net::SMTP.new('localhost', port) + smtp.start + smtp.quit + end + + def test_start_instance_with_position_argument + port = fake_server_start(helo: 'myname', user: 'account', password: 'password') + smtp = Net::SMTP.new('localhost', port) + smtp.start('myname', 'account', 'password', :plain) + smtp.quit + end + + def test_start_instance_with_keyword_argument + port = fake_server_start(helo: 'myname', user: 'account', password: 'password') + smtp = Net::SMTP.new('localhost', port) + smtp.start(helo: 'myname', user: 'account', secret: 'password', authtype: :plain) + smtp.quit + end + + def test_start_instance_password_is_secret + port = fake_server_start(helo: 'myname', user: 'account', password: 'password') + smtp = Net::SMTP.new('localhost', port) + smtp.start(helo: 'myname', user: 'account', password: 'password', authtype: :plain) + smtp.quit + end + + def test_start_instance_invalid_number_of_arguments + smtp = Net::SMTP.new('localhost') + err = assert_raise ArgumentError do + smtp.start('myname', 'account', 'password', :plain, :invalid_arg) + end + assert_equal('wrong number of arguments (given 5, expected 0..4)', err.message) + end + private def accept(servers) - loop do - readable, = IO.select(servers.map(&:to_io)) - readable.each do |r| - sock, = r.accept_nonblock(exception: false) - next if sock == :wait_readable - return sock + Socket.accept_loop(servers) { |s, _| break s } + end + + def fake_server_start(helo: 'localhost', user: nil, password: nil) + servers = Socket.tcp_server_sockets('localhost', 0) + Thread.start do + Thread.current.abort_on_exception = true + sock = accept(servers) + sock.puts "220 ready\r\n" + assert_equal("EHLO #{helo}\r\n", sock.gets) + sock.puts "220-servername\r\n220 AUTH PLAIN\r\n" + if user + credential = ["\0#{user}\0#{password}"].pack('m0') + assert_equal("AUTH PLAIN #{credential}\r\n", sock.gets) + sock.puts "235 2.7.0 Authentication successful\r\n" end + assert_equal("QUIT\r\n", sock.gets) + sock.puts "221 2.0.0 Bye\r\n" + sock.close + servers.each(&:close) end + port = servers[0].local_address.ip_port + return port end end end |
