mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* test/net/imap/test_imap.rb: added tests for SSL.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14496 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d43090fb41
commit
018bdcadac
5 changed files with 220 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
|||
Sun Dec 23 00:48:05 2007 Shugo Maeda <shugo@ruby-lang.org>
|
||||
|
||||
* test/net/imap/test_imap.rb: added tests for SSL.
|
||||
|
||||
Sat Dec 22 21:10:53 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* ext/syck/rubyext.c (syck_genericresolver_node_import): should
|
||||
|
|
62
test/net/imap/cacert.pem
Normal file
62
test/net/imap/cacert.pem
Normal file
|
@ -0,0 +1,62 @@
|
|||
Certificate:
|
||||
Data:
|
||||
Version: 3 (0x2)
|
||||
Serial Number:
|
||||
d2:9c:5c:81:a3:b0:3b:ec
|
||||
Signature Algorithm: sha1WithRSAEncryption
|
||||
Issuer: C=JP, ST=Shimane, O=Ruby Core Team, CN=Ruby Test CA
|
||||
Validity
|
||||
Not Before: Dec 22 08:35:19 2007 GMT
|
||||
Not After : Dec 21 08:35:19 2010 GMT
|
||||
Subject: C=JP, ST=Shimane, O=Ruby Core Team, CN=Ruby Test CA
|
||||
Subject Public Key Info:
|
||||
Public Key Algorithm: rsaEncryption
|
||||
RSA Public Key: (1024 bit)
|
||||
Modulus (1024 bit):
|
||||
00:ee:b1:1a:06:25:1f:29:80:a1:59:ef:0f:0d:8b:
|
||||
5a:88:40:73:56:95:d9:db:b4:71:3c:36:7f:b4:fc:
|
||||
68:51:54:8b:8d:9e:41:27:fb:31:d1:7a:6c:5c:21:
|
||||
30:3d:22:f6:7a:92:c3:fa:b3:85:36:44:76:22:6c:
|
||||
08:82:78:89:7d:c3:19:d4:ec:7d:05:5b:75:cd:38:
|
||||
1c:89:03:ef:02:aa:7e:6d:5b:36:ea:ad:e2:0b:ec:
|
||||
4b:e2:36:bf:c0:49:44:93:a6:cc:da:40:be:5f:c0:
|
||||
75:3b:be:c0:28:db:42:57:90:3b:83:af:2d:60:9a:
|
||||
c6:b1:bc:5c:68:12:2d:70:25
|
||||
Exponent: 65537 (0x10001)
|
||||
X509v3 extensions:
|
||||
X509v3 Subject Key Identifier:
|
||||
AE:F5:39:FE:D2:35:A2:2C:1E:CB:0E:08:4C:0E:5A:DC:44:99:E2:9C
|
||||
X509v3 Authority Key Identifier:
|
||||
keyid:AE:F5:39:FE:D2:35:A2:2C:1E:CB:0E:08:4C:0E:5A:DC:44:99:E2:9C
|
||||
DirName:/C=JP/ST=Shimane/O=Ruby Core Team/CN=Ruby Test CA
|
||||
serial:D2:9C:5C:81:A3:B0:3B:EC
|
||||
|
||||
X509v3 Basic Constraints:
|
||||
CA:TRUE
|
||||
Signature Algorithm: sha1WithRSAEncryption
|
||||
9e:28:89:81:83:55:1b:38:cb:85:86:ec:ee:cc:f4:cf:f0:47:
|
||||
61:72:07:f2:2e:ca:67:f7:c8:29:b5:3c:34:d2:cf:c2:f7:9c:
|
||||
4d:ce:8d:37:24:57:85:c1:0e:27:86:81:15:99:ec:af:05:ae:
|
||||
09:96:f6:f7:5a:c0:d2:a4:82:ed:d6:d3:26:e9:3d:50:50:0c:
|
||||
71:c2:6e:9a:50:30:73:8a:94:0d:c1:9c:1a:da:76:80:b8:8b:
|
||||
46:ec:10:93:6c:8d:cb:ab:eb:12:5d:e0:6f:1a:5a:ac:5c:34:
|
||||
12:df:7b:a7:dc:b6:0f:d6:3e:ac:2f:69:db:11:aa:1d:c4:90:
|
||||
f6:fa
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIICzjCCAjegAwIBAgIJANKcXIGjsDvsMA0GCSqGSIb3DQEBBQUAME8xCzAJBgNV
|
||||
BAYTAkpQMRAwDgYDVQQIEwdTaGltYW5lMRcwFQYDVQQKEw5SdWJ5IENvcmUgVGVh
|
||||
bTEVMBMGA1UEAxMMUnVieSBUZXN0IENBMB4XDTA3MTIyMjA4MzUxOVoXDTEwMTIy
|
||||
MTA4MzUxOVowTzELMAkGA1UEBhMCSlAxEDAOBgNVBAgTB1NoaW1hbmUxFzAVBgNV
|
||||
BAoTDlJ1YnkgQ29yZSBUZWFtMRUwEwYDVQQDEwxSdWJ5IFRlc3QgQ0EwgZ8wDQYJ
|
||||
KoZIhvcNAQEBBQADgY0AMIGJAoGBAO6xGgYlHymAoVnvDw2LWohAc1aV2du0cTw2
|
||||
f7T8aFFUi42eQSf7MdF6bFwhMD0i9nqSw/qzhTZEdiJsCIJ4iX3DGdTsfQVbdc04
|
||||
HIkD7wKqfm1bNuqt4gvsS+I2v8BJRJOmzNpAvl/AdTu+wCjbQleQO4OvLWCaxrG8
|
||||
XGgSLXAlAgMBAAGjgbEwga4wHQYDVR0OBBYEFK71Of7SNaIsHssOCEwOWtxEmeKc
|
||||
MH8GA1UdIwR4MHaAFK71Of7SNaIsHssOCEwOWtxEmeKcoVOkUTBPMQswCQYDVQQG
|
||||
EwJKUDEQMA4GA1UECBMHU2hpbWFuZTEXMBUGA1UEChMOUnVieSBDb3JlIFRlYW0x
|
||||
FTATBgNVBAMTDFJ1YnkgVGVzdCBDQYIJANKcXIGjsDvsMAwGA1UdEwQFMAMBAf8w
|
||||
DQYJKoZIhvcNAQEFBQADgYEAniiJgYNVGzjLhYbs7sz0z/BHYXIH8i7KZ/fIKbU8
|
||||
NNLPwvecTc6NNyRXhcEOJ4aBFZnsrwWuCZb291rA0qSC7dbTJuk9UFAMccJumlAw
|
||||
c4qUDcGcGtp2gLiLRuwQk2yNy6vrEl3gbxparFw0Et97p9y2D9Y+rC9p2xGqHcSQ
|
||||
9vo=
|
||||
-----END CERTIFICATE-----
|
12
test/net/imap/server.crt
Normal file
12
test/net/imap/server.crt
Normal file
|
@ -0,0 +1,12 @@
|
|||
-----BEGIN CERTIFICATE-----
|
||||
MIIB0jCCATsCAQIwDQYJKoZIhvcNAQEFBQAwTzELMAkGA1UEBhMCSlAxEDAOBgNV
|
||||
BAgTB1NoaW1hbmUxFzAVBgNVBAoTDlJ1YnkgQ29yZSBUZWFtMRUwEwYDVQQDEwxS
|
||||
dWJ5IFRlc3QgQ0EwHhcNMDcxMjIyMDg0MDA1WhcNMTcxMjE5MDg0MDA1WjAUMRIw
|
||||
EAYDVQQDEwlsb2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANt1
|
||||
0EXesd+/caAOsKXmvPQcneUlZ2TFe8vxr8a+mqrqfg/MBa/vQGkGsskTnX7rogbi
|
||||
6n0Hx8eZx/vVuOtjd2IrGBLDU1jQ9cdADAHRJoI0Fgnj3GX03LtdpUFg56l0utdM
|
||||
tqOcxYyJr8vonwX+6v5kJL/n7eP20PzW6/wGghD7AgMBAAEwDQYJKoZIhvcNAQEF
|
||||
BQADgYEA2zl2U40/ogkC3k1ccoO4n/szaGW/wfxFREjn9BCvSB8iGRYNCGO/MdZ6
|
||||
maaSuAPdIw6RdFncL6dC3xf5IrriCyOmdZM/O8hgC/sNOnWtRAA/+XJqs0UnZ/A2
|
||||
f06bSqE1BTSiUU5wY4UraN2aDgnbFonCrY5RipZgLYaWyEbRtF8=
|
||||
-----END CERTIFICATE-----
|
15
test/net/imap/server.key
Normal file
15
test/net/imap/server.key
Normal file
|
@ -0,0 +1,15 @@
|
|||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXQIBAAKBgQDbddBF3rHfv3GgDrCl5rz0HJ3lJWdkxXvL8a/Gvpqq6n4PzAWv
|
||||
70BpBrLJE51+66IG4up9B8fHmcf71bjrY3diKxgSw1NY0PXHQAwB0SaCNBYJ49xl
|
||||
9Ny7XaVBYOepdLrXTLajnMWMia/L6J8F/ur+ZCS/5+3j9tD81uv8BoIQ+wIDAQAB
|
||||
AoGAGtYHR+P5gFDaxiXFuCPFC1zMeg7e29XCU6gURIteQnQ2QhxCvcbV64HkLu51
|
||||
HeYWhB0Pa4aeCWxmpgb2e+JH4MEoIjeJSGyZQeqwkQLgWJDdvkgWx5am58QzA60I
|
||||
ipkZ9QHcPffSs5RiGx4yfr58KqAmwFphGCY8W7v4LqaENdECQQD9H5VTW9g4gj1c
|
||||
j3uNYvSI/D7a9P7gfI+ziczuwMm5xsBx3D/t5TAr3SJKNne3sl1E6ZERCUbzxf+C
|
||||
k58EiHx1AkEA3fRLGqDOq7EcQhbjTcA/v/t5MwlGEUsS9+XrqOWn50YuoIwRZJ3v
|
||||
qHRQzfQfFNklGtfBvwQ4md3irXjMeGVprwJBAMEAuwiDiHuV+xm/ofKtmE13IKot
|
||||
ksYy1BOOp/8IawhHXueyi+BmF/PqOkIiA+jCjNGF0oIN89beizPSQbbgJx0CQG/K
|
||||
qL1bu1ys0y/SeWBi8XkP/0aeaCUzq/UiYCTsrzoEll2UzvnftqMhGsXxLGqCyHaR
|
||||
r2s3hA6zvIVlL4+AfM8CQQClq+WDrC5VKciLYakZNWJjV1m+H2Ut/0fXdUjKHajE
|
||||
FWLcsrOhADf6bkTb71GwPxnKRkkRmud5upP0ZYYTqM4X
|
||||
-----END RSA PRIVATE KEY-----
|
|
@ -2,6 +2,10 @@ require "net/imap"
|
|||
require "test/unit"
|
||||
|
||||
class IMAPTest < Test::Unit::TestCase
|
||||
CA_FILE = File.expand_path("cacert.pem", File.dirname(__FILE__))
|
||||
SERVER_KEY = File.expand_path("server.key", File.dirname(__FILE__))
|
||||
SERVER_CERT = File.expand_path("server.crt", File.dirname(__FILE__))
|
||||
|
||||
def test_encode_utf7
|
||||
utf8 = "\357\274\241\357\274\242\357\274\243".force_encoding("UTF-8")
|
||||
s = Net::IMAP.encode_utf7(utf8)
|
||||
|
@ -13,4 +17,127 @@ class IMAPTest < Test::Unit::TestCase
|
|||
utf8 = "\357\274\241\357\274\242\357\274\243".force_encoding("UTF-8")
|
||||
assert_equal(utf8, s)
|
||||
end
|
||||
|
||||
def test_imaps_unknown_ca
|
||||
assert_raise(OpenSSL::SSL::SSLError) do
|
||||
imaps_test do |port|
|
||||
Net::IMAP.new("localhost",
|
||||
:port => port,
|
||||
:ssl => true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_imaps_with_ca_file
|
||||
assert_nothing_raised do
|
||||
imaps_test do |port|
|
||||
Net::IMAP.new("localhost",
|
||||
:port => port,
|
||||
:ssl => { :ca_file => CA_FILE })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_imaps_verify_none
|
||||
assert_nothing_raised do
|
||||
imaps_test do |port|
|
||||
Net::IMAP.new("localhost",
|
||||
:port => port,
|
||||
:ssl => { :verify_mode => OpenSSL::SSL::VERIFY_NONE })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_imaps_post_connection_check
|
||||
assert_raise(OpenSSL::SSL::SSLError) do
|
||||
imaps_test do |port|
|
||||
Net::IMAP.new("127.0.0.1",
|
||||
:port => port,
|
||||
:ssl => { :ca_file => CA_FILE })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_starttls
|
||||
starttls_test do |port|
|
||||
imap = Net::IMAP.new("localhost", :port => port)
|
||||
imap.starttls(:ca_file => CA_FILE)
|
||||
imap
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def imaps_test
|
||||
server = TCPServer.new(0)
|
||||
port = server.addr[1]
|
||||
ctx = OpenSSL::SSL::SSLContext.new
|
||||
ctx.ca_file = CA_FILE
|
||||
ctx.key = File.open(SERVER_KEY) { |f|
|
||||
OpenSSL::PKey::RSA.new(f)
|
||||
}
|
||||
ctx.cert = File.open(SERVER_CERT) { |f|
|
||||
OpenSSL::X509::Certificate.new(f)
|
||||
}
|
||||
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.gets
|
||||
sock.print("* BYE terminating connection\r\n")
|
||||
sock.print("RUBY0001 OK LOGOUT completed\r\n")
|
||||
ensure
|
||||
sock.close
|
||||
end
|
||||
rescue
|
||||
end
|
||||
end
|
||||
begin
|
||||
imap = yield(port)
|
||||
imap.logout
|
||||
imap.disconnect
|
||||
ensure
|
||||
ssl_server.close
|
||||
end
|
||||
end
|
||||
|
||||
def starttls_test
|
||||
server = TCPServer.new(0)
|
||||
port = server.addr[1]
|
||||
Thread.start do
|
||||
begin
|
||||
sock = server.accept
|
||||
sock.print("* OK test server\r\n")
|
||||
sock.gets
|
||||
sock.print("RUBY0001 OK completed\r\n")
|
||||
ctx = OpenSSL::SSL::SSLContext.new
|
||||
ctx.ca_file = CA_FILE
|
||||
ctx.key = File.open(SERVER_KEY) { |f|
|
||||
OpenSSL::PKey::RSA.new(f)
|
||||
}
|
||||
ctx.cert = File.open(SERVER_CERT) { |f|
|
||||
OpenSSL::X509::Certificate.new(f)
|
||||
}
|
||||
sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
||||
begin
|
||||
sock.accept
|
||||
sock.gets
|
||||
sock.print("* BYE terminating connection\r\n")
|
||||
sock.print("RUBY0002 OK LOGOUT completed\r\n")
|
||||
ensure
|
||||
sock.close
|
||||
end
|
||||
rescue
|
||||
end
|
||||
end
|
||||
begin
|
||||
imap = yield(port)
|
||||
imap.logout
|
||||
imap.disconnect
|
||||
ensure
|
||||
server.close
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue