File: test_imaps.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (107 lines) | stat: -rw-r--r-- 2,796 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require "net/imap"
require "test/unit"

# This testcase is made for 1.8 based on test_imap.rb in CRuby 1.9
class IMAPTest < Test::Unit::TestCase
  CA_FILE = File.expand_path("fixture/imaps/cacert.pem", File.dirname(__FILE__))
  SERVER_KEY = File.expand_path("fixture/imaps/server.key", File.dirname(__FILE__))
  SERVER_CERT = File.expand_path("fixture/imaps/server.crt", File.dirname(__FILE__))

  SERVER_ADDR = "127.0.0.1"

  def setup
    @do_not_reverse_lookup = Socket.do_not_reverse_lookup
    Socket.do_not_reverse_lookup = true
  end

  def teardown
    Socket.do_not_reverse_lookup = @do_not_reverse_lookup
  end

  def test_imaps_unknown_ca
    assert_raise(OpenSSL::SSL::SSLError) do
      imaps_test do |port|
        Net::IMAP.new("localhost", port, true, nil, true)
      end
    end
  end

  def test_imaps_with_ca_file
    assert_nothing_raised do
      imaps_test do |port|
        Net::IMAP.new("localhost", port, true, CA_FILE, true)
      end
    end
  end

  def test_imaps_login
    assert_raises(Net::IMAP::ByeResponseError) do
      imaps_test do |port|
        imaps = Net::IMAP.new("localhost", port, true, CA_FILE, true)
        imaps.login('foo@bar.com', 'wrong password')
        imaps
      end
    end
  end

  def test_imaps_verify_none
    assert_nothing_raised do
      imaps_test do |port|
        Net::IMAP.new(SERVER_ADDR, port, true, nil, false)
      end
    end
  end

  def test_imaps_post_connection_check
    assert_raise(OpenSSL::SSL::SSLError) do
      imaps_test do |port|
        # SERVER_ADDR is different from the hostname in the certificate,
        # so the following code should raise a SSLError.
        Net::IMAP.new(SERVER_ADDR, port, true, CA_FILE, true)
      end
    end
  end

private

  def imaps_test
    server = create_tcp_server
    port = server.addr[1]
    ctx = OpenSSL::SSL::SSLContext.new
    ctx.ca_file = CA_FILE
    ctx.key = OpenSSL::PKey::RSA.new(File.read(SERVER_KEY))
    ctx.cert = OpenSSL::X509::Certificate.new(File.read(SERVER_CERT))
    ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx)
    Thread.start do
      begin
        sock = ssl_server.accept
        begin
          sock.print("* OK test server\r\n")
          sock.read(10) # emulates half-read for JRUBY-5200
          sock.print("* BYE terminating connection\r\n")
          sock.print("RUBY0001 OK LOGOUT completed\r\n")
          sock.gets
        ensure
          sock.close
        end
      rescue
      end
    end
    begin
      begin
        imap = yield(port)
        imap.logout if !imap.disconnected?
      ensure
        imap.disconnect if imap && !imap.disconnected?
      end
    rescue IOError
      # ignore
    ensure
      ssl_server.close
    end
  end

  def create_tcp_server
    return TCPServer.new(SERVER_ADDR, 0)
  end
end