diff options
author | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-25 15:02:05 +0000 |
---|---|---|
committer | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-25 15:02:05 +0000 |
commit | 0dc342de848a642ecce8db697b8fecd83a63e117 (patch) | |
tree | 2b7ed4724aff1f86073e4740134bda9c4aac1a39 /trunk/test/net | |
parent | ef70cf7138ab8034b5b806f466e4b484b24f0f88 (diff) |
added tag v1_9_0_4
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_9_0_4@18845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'trunk/test/net')
-rw-r--r-- | trunk/test/net/http/test_http.rb | 320 | ||||
-rw-r--r-- | trunk/test/net/http/test_httpheader.rb | 317 | ||||
-rw-r--r-- | trunk/test/net/http/test_https.rb | 97 | ||||
-rw-r--r-- | trunk/test/net/http/test_https_proxy.rb | 37 | ||||
-rw-r--r-- | trunk/test/net/http/utils.rb | 96 | ||||
-rw-r--r-- | trunk/test/net/imap/cacert.pem | 62 | ||||
-rw-r--r-- | trunk/test/net/imap/server.crt | 12 | ||||
-rw-r--r-- | trunk/test/net/imap/server.key | 15 | ||||
-rw-r--r-- | trunk/test/net/imap/test_imap.rb | 195 |
9 files changed, 1151 insertions, 0 deletions
diff --git a/trunk/test/net/http/test_http.rb b/trunk/test/net/http/test_http.rb new file mode 100644 index 0000000000..50d1fe2eae --- /dev/null +++ b/trunk/test/net/http/test_http.rb @@ -0,0 +1,320 @@ +# $Id$ + +require 'test/unit' +require 'net/http' +require 'stringio' +require File.expand_path("utils", File.dirname(__FILE__)) + +module TestNetHTTP_version_1_1_methods + + def test_s_get + assert_equal $test_net_http_data, + Net::HTTP.get(config('host'), '/', config('port')) + end + + def test_head + start {|http| + res = http.head('/') + assert_kind_of Net::HTTPResponse, res + assert_equal $test_net_http_data_type, res['Content-Type'] + assert_equal $test_net_http_data.size, res['Content-Length'].to_i + } + end + + def test_get + start {|http| + _test_get__get http + _test_get__iter http + _test_get__chunked http + } + end + + def _test_get__get(http) + res, body = http.get('/') + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + assert_kind_of String, body + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + assert_equal $test_net_http_data_type, res['Content-Type'] + assert_equal $test_net_http_data.size, body.size + assert_equal $test_net_http_data, body + assert_equal $test_net_http_data.size, res.body.size + assert_equal $test_net_http_data, res.body + end + + def _test_get__iter(http) + buf = '' + res, body = http.get('/') {|s| buf << s } + assert_kind_of Net::HTTPResponse, res + # assert_kind_of String, res.body + # assert_kind_of String, body + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + assert_equal $test_net_http_data_type, res['Content-Type'] + assert_equal $test_net_http_data.size, buf.size + assert_equal $test_net_http_data, buf + # assert_equal $test_net_http_data.size, res.body.size + # assert_equal $test_net_http_data, res.body + end + + def _test_get__chunked(http) + buf = '' + res, body = http.get('/') {|s| buf << s } + assert_kind_of Net::HTTPResponse, res + # assert_kind_of String, res.body + # assert_kind_of String, body + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + assert_equal $test_net_http_data_type, res['Content-Type'] + assert_equal $test_net_http_data.size, buf.size + assert_equal $test_net_http_data, buf + # assert_equal $test_net_http_data.size, res.body.size + # assert_equal $test_net_http_data, res.body + end + + def test_get__break + i = 0 + start {|http| + http.get('/') do |str| + i += 1 + break + end + } + assert_equal 1, i + end + + def test_get__implicit_start + res, body = new().get('/') + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, body + assert_kind_of String, res.body + assert_not_nil res['content-length'] + assert_equal $test_net_http_data_type, res['Content-Type'] + assert_equal $test_net_http_data.size, res.body.size + assert_equal $test_net_http_data, res.body + end + + def test_get2 + start {|http| + http.get2('/') {|res| + assert_kind_of Net::HTTPResponse, res + assert_kind_of Net::HTTPResponse, res.header + assert_not_nil res['content-length'] + assert_equal $test_net_http_data_type, res['Content-Type'] + assert_kind_of String, res.body + assert_kind_of String, res.entity + assert_equal $test_net_http_data.size, res.body.size + assert_equal $test_net_http_data, res.body + assert_equal $test_net_http_data, res.entity + } + } + end + + def test_post + start {|http| + _test_post__base http + _test_post__file http + } + end + + def _test_post__base(http) + uheader = {} + uheader['Accept'] = 'application/octet-stream' + data = 'post data' + res, body = http.post('/', data) + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, body + assert_kind_of String, res.body + assert_equal data, body + assert_equal data, res.body + assert_equal data, res.entity + end + + def _test_post__file(http) + data = 'post data' + f = StringIO.new + http.post('/', data, nil, f) + assert_equal data, f.string + end + + def test_s_post_form + res = Net::HTTP.post_form( + URI.parse("http://#{config('host')}:#{config('port')}/"), + "a" => "x") + assert_equal ["a=x"], res.body.split(/[;&]/).sort + + res = Net::HTTP.post_form( + URI.parse("http://#{config('host')}:#{config('port')}/"), + "a" => "x", + "b" => "y") + assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort + + res = Net::HTTP.post_form( + URI.parse("http://#{config('host')}:#{config('port')}/"), + "a" => ["x1", "x2"], + "b" => "y") + assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort + end + +end + + +module TestNetHTTP_version_1_2_methods + + def test_request + start {|http| + _test_request__GET http + _test_request__file http + # _test_request__range http # WEBrick does not support Range: header. + _test_request__HEAD http + _test_request__POST http + _test_request__stream_body http + } + end + + def _test_request__GET(http) + req = Net::HTTP::Get.new('/') + http.request(req) {|res| + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + assert_equal $test_net_http_data.size, res.body.size + assert_equal $test_net_http_data, res.body + } + end + + def _test_request__file(http) + req = Net::HTTP::Get.new('/') + http.request(req) {|res| + assert_kind_of Net::HTTPResponse, res + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + f = StringIO.new("".force_encoding("ASCII-8BIT")) + res.read_body f + assert_equal $test_net_http_data.bytesize, f.string.bytesize + assert_equal $test_net_http_data.encoding, f.string.encoding + assert_equal $test_net_http_data, f.string + } + end + + def _test_request__range(http) + req = Net::HTTP::Get.new('/') + req['range'] = 'bytes=0-5' + assert_equal $test_net_http_data[0,6], http.request(req).body + end + + def _test_request__HEAD(http) + req = Net::HTTP::Head.new('/') + http.request(req) {|res| + assert_kind_of Net::HTTPResponse, res + assert_not_nil res['content-length'] + assert_equal $test_net_http_data.size, res['content-length'].to_i + assert_nil res.body + } + end + + def _test_request__POST(http) + data = 'post data' + req = Net::HTTP::Post.new('/') + req['Accept'] = $test_net_http_data_type + http.request(req, data) {|res| + assert_kind_of Net::HTTPResponse, res + assert_equal data.size, res['content-length'].to_i + assert_kind_of String, res.body + assert_equal data, res.body + } + end + + def _test_request__stream_body(http) + req = Net::HTTP::Post.new('/') + data = $test_net_http_data + req.content_length = data.size + req.body_stream = StringIO.new(data) + res = http.request(req) + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + assert_equal data.size, res.body.size + assert_equal data, res.body + end + + def test_send_request + start {|http| + _test_send_request__GET http + _test_send_request__POST http + } + end + + def _test_send_request__GET(http) + res = http.send_request('GET', '/') + assert_kind_of Net::HTTPResponse, res + assert_equal $test_net_http_data.size, res['content-length'].to_i + assert_kind_of String, res.body + assert_equal $test_net_http_data, res.body + end + + def _test_send_request__POST(http) + data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa' + res = http.send_request('POST', '/', data) + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + assert_equal data.size, res.body.size + assert_equal data, res.body + end +end + +class TestNetHTTP_version_1_1 < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'port' => 10081, + 'proxy_host' => nil, + 'proxy_port' => nil, + } + + include TestNetHTTPUtils + include TestNetHTTP_version_1_1_methods + + def new + Net::HTTP.version_1_1 + super + end +end + +class TestNetHTTP_v1_2 < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'port' => 10081, + 'proxy_host' => nil, + 'proxy_port' => nil, + } + + include TestNetHTTPUtils + include TestNetHTTP_version_1_1_methods + include TestNetHTTP_version_1_2_methods + + def new + Net::HTTP.version_1_2 + super + end +end + +=begin +class TestNetHTTP_proxy < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'port' => 10081, + 'proxy_host' => '127.0.0.1', + 'proxy_port' => 10082, + } + + include TestNetHTTPUtils + include TestNetHTTP_version_1_1_methods + include TestNetHTTP_version_1_2_methods + + def new + Net::HTTP.version_1_2 + super + end +end +=end diff --git a/trunk/test/net/http/test_httpheader.rb b/trunk/test/net/http/test_httpheader.rb new file mode 100644 index 0000000000..3ba1b217c0 --- /dev/null +++ b/trunk/test/net/http/test_httpheader.rb @@ -0,0 +1,317 @@ +require 'net/http' +require 'test/unit' + +class HTTPHeaderTest < Test::Unit::TestCase + + class C + include Net::HTTPHeader + def initialize + initialize_http_header({}) + end + attr_accessor :body + end + + def setup + @c = C.new + end + + def test_size + assert_equal 0, @c.size + @c['a'] = 'a' + assert_equal 1, @c.size + @c['b'] = 'b' + assert_equal 2, @c.size + @c['b'] = 'b' + assert_equal 2, @c.size + @c['c'] = 'c' + assert_equal 3, @c.size + end + + def test_ASET + @c['My-Header'] = 'test string' + @c['my-Header'] = 'test string' + @c['My-header'] = 'test string' + @c['my-header'] = 'test string' + @c['MY-HEADER'] = 'test string' + assert_equal 1, @c.size + + @c['AaA'] = 'aaa' + @c['aaA'] = 'aaa' + @c['AAa'] = 'aaa' + assert_equal 2, @c.length + end + + def test_AREF + @c['My-Header'] = 'test string' + assert_equal 'test string', @c['my-header'] + assert_equal 'test string', @c['MY-header'] + assert_equal 'test string', @c['my-HEADER'] + + @c['Next-Header'] = 'next string' + assert_equal 'next string', @c['next-header'] + end + + def test_add_field + @c.add_field 'My-Header', 'a' + assert_equal 'a', @c['My-Header'] + assert_equal ['a'], @c.get_fields('My-Header') + @c.add_field 'My-Header', 'b' + assert_equal 'a, b', @c['My-Header'] + assert_equal ['a', 'b'], @c.get_fields('My-Header') + @c.add_field 'My-Header', 'c' + assert_equal 'a, b, c', @c['My-Header'] + assert_equal ['a', 'b', 'c'], @c.get_fields('My-Header') + @c.add_field 'My-Header', 'd, d' + assert_equal 'a, b, c, d, d', @c['My-Header'] + assert_equal ['a', 'b', 'c', 'd, d'], @c.get_fields('My-Header') + end + + def test_get_fields + @c['My-Header'] = 'test string' + assert_equal ['test string'], @c.get_fields('my-header') + assert_equal ['test string'], @c.get_fields('My-header') + assert_equal ['test string'], @c.get_fields('my-Header') + + assert_nil @c.get_fields('not-found') + assert_nil @c.get_fields('Not-Found') + + @c.get_fields('my-header').push 'junk' + assert_equal ['test string'], @c.get_fields('my-header') + @c.get_fields('my-header').clear + assert_equal ['test string'], @c.get_fields('my-header') + end + + def test_delete + @c['My-Header'] = 'test' + assert_equal 'test', @c['My-Header'] + assert_nil @c['not-found'] + @c.delete 'My-Header' + assert_nil @c['My-Header'] + assert_nil @c['not-found'] + @c.delete 'My-Header' + @c.delete 'My-Header' + assert_nil @c['My-Header'] + assert_nil @c['not-found'] + end + + def test_each + @c['My-Header'] = 'test' + @c.each do |k, v| + assert_equal 'my-header', k + assert_equal 'test', v + end + @c.each do |k, v| + assert_equal 'my-header', k + assert_equal 'test', v + end + end + + def test_each_key + @c['My-Header'] = 'test' + @c.each_key do |k| + assert_equal 'my-header', k + end + @c.each_key do |k| + assert_equal 'my-header', k + end + end + + def test_each_value + @c['My-Header'] = 'test' + @c.each_value do |v| + assert_equal 'test', v + end + @c.each_value do |v| + assert_equal 'test', v + end + end + + def test_canonical_each + @c['my-header'] = ['a', 'b'] + @c.canonical_each do |k,v| + assert_equal 'My-Header', k + assert_equal 'a, b', v + end + end + + def test_each_capitalized + @c['my-header'] = ['a', 'b'] + @c.each_capitalized do |k,v| + assert_equal 'My-Header', k + assert_equal 'a, b', v + end + end + + def test_key? + @c['My-Header'] = 'test' + assert_equal true, @c.key?('My-Header') + assert_equal true, @c.key?('my-header') + assert_equal false, @c.key?('Not-Found') + assert_equal false, @c.key?('not-found') + assert_equal false, @c.key?('') + assert_equal false, @c.key?('x' * 1024) + end + + def test_to_hash + end + + def test_range + try_range(1..5, '1-5') + try_range(234..567, '234-567') + try_range(-5..-1, '-5') + try_range(1..-1, '1-') + end + + def try_range(r, s) + @c['range'] = "bytes=#{s}" + assert_equal r, Array(@c.range)[0] + end + + def test_range= + @c.range = 0..499 + assert_equal 'bytes=0-499', @c['range'] + @c.range = 0...500 + assert_equal 'bytes=0-499', @c['range'] + @c.range = 300 + assert_equal 'bytes=0-299', @c['range'] + @c.range = -400 + assert_equal 'bytes=-400', @c['range'] + @c.set_range 0, 500 + assert_equal 'bytes=0-499', @c['range'] + end + + def test_content_range + end + + def test_range_length + @c['Content-Range'] = "bytes 0-499/1000" + assert_equal 500, @c.range_length + @c['Content-Range'] = "bytes 1-500/1000" + assert_equal 500, @c.range_length + @c['Content-Range'] = "bytes 1-1/1000" + assert_equal 1, @c.range_length + end + + def test_chunked? + try_chunked true, 'chunked' + try_chunked true, ' chunked ' + try_chunked true, '(OK)chunked' + + try_chunked false, 'not-chunked' + try_chunked false, 'chunked-but-not-chunked' + end + + def try_chunked(bool, str) + @c['transfer-encoding'] = str + assert_equal bool, @c.chunked? + end + + def test_content_length + @c.delete('content-length') + assert_nil @c['content-length'] + + try_content_length 500, '500' + try_content_length 10000_0000_0000, '1000000000000' + try_content_length 123, ' 123' + try_content_length 1, '1 23' + try_content_length 500, '(OK)500' + assert_raises(Net::HTTPHeaderSyntaxError, 'here is no digit, but') { + @c['content-length'] = 'no digit' + @c.content_length + } + end + + def try_content_length(len, str) + @c['content-length'] = str + assert_equal len, @c.content_length + end + + def test_content_length= + @c.content_length = 0 + assert_equal 0, @c.content_length + @c.content_length = 1 + assert_equal 1, @c.content_length + @c.content_length = 999 + assert_equal 999, @c.content_length + @c.content_length = 10000000000000 + assert_equal 10000000000000, @c.content_length + end + + def test_content_type + assert_nil @c.content_type + @c.content_type = 'text/html' + assert_equal 'text/html', @c.content_type + @c.content_type = 'application/pdf' + assert_equal 'application/pdf', @c.content_type + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal 'text/html', @c.content_type + @c.content_type = 'text' + assert_equal 'text', @c.content_type + end + + def test_main_type + assert_nil @c.main_type + @c.content_type = 'text/html' + assert_equal 'text', @c.main_type + @c.content_type = 'application/pdf' + assert_equal 'application', @c.main_type + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal 'text', @c.main_type + @c.content_type = 'text' + assert_equal 'text', @c.main_type + end + + def test_sub_type + assert_nil @c.sub_type + @c.content_type = 'text/html' + assert_equal 'html', @c.sub_type + @c.content_type = 'application/pdf' + assert_equal 'pdf', @c.sub_type + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal 'html', @c.sub_type + @c.content_type = 'text' + assert_nil @c.sub_type + end + + def test_type_params + assert_equal({}, @c.type_params) + @c.content_type = 'text/html' + assert_equal({}, @c.type_params) + @c.content_type = 'application/pdf' + assert_equal({}, @c.type_params) + @c.set_content_type 'text/html', {'charset' => 'iso-2022-jp'} + assert_equal({'charset' => 'iso-2022-jp'}, @c.type_params) + @c.content_type = 'text' + assert_equal({}, @c.type_params) + end + + def test_set_content_type + end + + def test_form_data= + @c.form_data = {"cmd"=>"search", "q"=>"ruby", "max"=>"50"} + assert_equal 'application/x-www-form-urlencoded', @c.content_type + assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort + end + + def test_set_form_data + @c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>"50" + assert_equal 'application/x-www-form-urlencoded', @c.content_type + assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort + + @c.set_form_data "cmd"=>"search", "q"=>"ruby", "max"=>50 + assert_equal 'application/x-www-form-urlencoded', @c.content_type + assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split('&').sort + + @c.set_form_data({"cmd"=>"search", "q"=>"ruby", "max"=>"50"}, ';') + assert_equal 'application/x-www-form-urlencoded', @c.content_type + assert_equal %w( cmd=search max=50 q=ruby ), @c.body.split(';').sort + end + + def test_basic_auth + end + + def test_proxy_basic_auth + end + +end diff --git a/trunk/test/net/http/test_https.rb b/trunk/test/net/http/test_https.rb new file mode 100644 index 0000000000..adb50c58fb --- /dev/null +++ b/trunk/test/net/http/test_https.rb @@ -0,0 +1,97 @@ +require "test/unit" +begin + require 'net/https' + require 'stringio' + require File.expand_path("../../openssl/utils", File.dirname(__FILE__)) + require File.expand_path("utils", File.dirname(__FILE__)) +rescue LoadError + # should skip this test +end + +class TestNetHTTPS < Test::Unit::TestCase + include TestNetHTTPUtils + + subject = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=localhost") + exts = [ + ["keyUsage", "keyEncipherment,digitalSignature", true], + ] + key = OpenSSL::TestUtils::TEST_KEY_RSA1024 + cert = OpenSSL::TestUtils.issue_cert( + subject, key, 1, Time.now, Time.now + 3600, exts, + nil, nil, OpenSSL::Digest::SHA1.new + ) + + CONFIG = { + 'host' => '127.0.0.1', + 'port' => 10081, + 'proxy_host' => nil, + 'proxy_port' => nil, + 'ssl_enable' => true, + 'ssl_certificate' => cert, + 'ssl_private_key' => key, + } + + def test_get + http = Net::HTTP.new("localhost", config("port")) + http.use_ssl = true + http.verify_callback = Proc.new do |preverify_ok, store_ctx| + store_ctx.current_cert.to_der == config('ssl_certificate').to_der + end + http.request_get("/") {|res| + assert_equal($test_net_http_data, res.body) + } + end + + def test_post + http = Net::HTTP.new("localhost", config("port")) + http.use_ssl = true + http.verify_callback = Proc.new do |preverify_ok, store_ctx| + store_ctx.current_cert.to_der == config('ssl_certificate').to_der + end + data = config('ssl_private_key').to_der + http.request_post("/", data) {|res| + assert_equal(data, res.body) + } + end + + if ENV["RUBY_OPENSSL_TEST_ALL"] + def test_verify + http = Net::HTTP.new("ssl.netlab.jp", 443) + http.use_ssl = true + assert( + (http.request_head("/"){|res| } rescue false), + "The system may not have default CA certificate store." + ) + end + end + + def test_verify_none + http = Net::HTTP.new("localhost", config("port")) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.request_get("/") {|res| + assert_equal($test_net_http_data, res.body) + } + end + + def test_certificate_verify_failure + http = Net::HTTP.new("localhost", config("port")) + http.use_ssl = true + ex = assert_raise(OpenSSL::SSL::SSLError){ + http.request_get("/") {|res| } + } + assert_match(/certificate verify failed/, ex.message) + end + + def test_identity_verify_failure + http = Net::HTTP.new("127.0.0.1", config("port")) + http.use_ssl = true + http.verify_callback = Proc.new do |preverify_ok, store_ctx| + store_ctx.current_cert.to_der == config('ssl_certificate').to_der + end + ex = assert_raise(OpenSSL::SSL::SSLError){ + http.request_get("/") {|res| } + } + assert_match(/hostname was not match/, ex.message) + end +end if defined?(OpenSSL) diff --git a/trunk/test/net/http/test_https_proxy.rb b/trunk/test/net/http/test_https_proxy.rb new file mode 100644 index 0000000000..4f26342339 --- /dev/null +++ b/trunk/test/net/http/test_https_proxy.rb @@ -0,0 +1,37 @@ +begin + require 'net/https' +rescue LoadError +end +require 'test/unit' + +class HTTPSProxyTest < Test::Unit::TestCase + def test_https_proxy_authentication + t = nil + TCPServer.open("127.0.0.1", 0) {|serv| + _, port, _, _ = serv.addr + t = Thread.new { + proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password') + http = proxy.new("foo.example.org", 8000) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + begin + http.start + rescue EOFError + end + } + sock = serv.accept + proxy_request = sock.gets("\r\n\r\n") + assert_equal( + "CONNECT foo.example.org:8000 HTTP/1.1\r\n" + + "Host: foo.example.org:8000\r\n" + + "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" + + "\r\n", + proxy_request, + "[ruby-dev:25673]") + sock.close + } + ensure + t.join if t + end +end if defined?(OpenSSL) + diff --git a/trunk/test/net/http/utils.rb b/trunk/test/net/http/utils.rb new file mode 100644 index 0000000000..e685a8ff61 --- /dev/null +++ b/trunk/test/net/http/utils.rb @@ -0,0 +1,96 @@ +require 'webrick' +begin + require "webrick/https" +rescue LoadError + # SSL features cannot be tested +end +require 'webrick/httpservlet/abstract' + +module TestNetHTTPUtils + def start(&block) + new().start(&block) + end + + def new + klass = Net::HTTP::Proxy(config('proxy_host'), config('proxy_port')) + http = klass.new(config('host'), config('port')) + http.set_debug_output logfile() + http + end + + def config(key) + self.class::CONFIG[key] + end + + def logfile + $DEBUG ? $stderr : NullWriter.new + end + + def setup + spawn_server + end + + def teardown + @server.shutdown + until @server.status == :Stop + sleep 0.1 + end + # resume global state + Net::HTTP.version_1_2 + end + + def spawn_server + server_config = { + :BindAddress => config('host'), + :Port => config('port'), + :Logger => WEBrick::Log.new(NullWriter.new), + :AccessLog => [], + :ShutdownSocketWithoutClose => true, + :ServerType => Thread, + } + if defined?(OpenSSL) and config('ssl_enable') + server_config.update({ + :SSLEnable => true, + :SSLCertificate => config('ssl_certificate'), + :SSLPrivateKey => config('ssl_private_key'), + }) + end + @server = WEBrick::HTTPServer.new(server_config) + @server.mount('/', Servlet) + @server.start + n_try_max = 5 + begin + TCPSocket.open(config('host'), config('port')).close + rescue Errno::ECONNREFUSED + sleep 0.2 + n_try_max -= 1 + raise 'cannot spawn server; give up' if n_try_max < 0 + retry + end + end + + $test_net_http = nil + $test_net_http_data = (0...256).to_a.map {|i| i.chr }.join('') * 64 + $test_net_http_data.force_encoding("ASCII-8BIT") + $test_net_http_data_type = 'application/octet-stream' + + class Servlet < WEBrick::HTTPServlet::AbstractServlet + def do_GET(req, res) + res['Content-Type'] = $test_net_http_data_type + res.body = $test_net_http_data + end + + # echo server + def do_POST(req, res) + res['Content-Type'] = req['Content-Type'] + res.body = req.body + end + end + + class NullWriter + def <<(s) end + def puts(*args) end + def print(*args) end + def printf(*args) end + end +end diff --git a/trunk/test/net/imap/cacert.pem b/trunk/test/net/imap/cacert.pem new file mode 100644 index 0000000000..8a48e07a90 --- /dev/null +++ b/trunk/test/net/imap/cacert.pem @@ -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----- diff --git a/trunk/test/net/imap/server.crt b/trunk/test/net/imap/server.crt new file mode 100644 index 0000000000..77803ee385 --- /dev/null +++ b/trunk/test/net/imap/server.crt @@ -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----- diff --git a/trunk/test/net/imap/server.key b/trunk/test/net/imap/server.key new file mode 100644 index 0000000000..7c57546ece --- /dev/null +++ b/trunk/test/net/imap/server.key @@ -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----- diff --git a/trunk/test/net/imap/test_imap.rb b/trunk/test/net/imap/test_imap.rb new file mode 100644 index 0000000000..cff09d3cbc --- /dev/null +++ b/trunk/test/net/imap/test_imap.rb @@ -0,0 +1,195 @@ +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) + assert_equal("&,yH,Iv8j-".force_encoding("UTF-8"), s) + end + + def test_decode_utf7 + s = Net::IMAP.decode_utf7("&,yH,Iv8j-") + utf8 = "\357\274\241\357\274\242\357\274\243".force_encoding("UTF-8") + assert_equal(utf8, s) + end + + def test_imaps_unknown_ca + if defined?(OpenSSL) + assert_raise(OpenSSL::SSL::SSLError) do + imaps_test do |port| + Net::IMAP.new("localhost", + :port => port, + :ssl => true) + end + end + end + end + + def test_imaps_with_ca_file + if defined?(OpenSSL) + assert_nothing_raised do + imaps_test do |port| + Net::IMAP.new("localhost", + :port => port, + :ssl => { :ca_file => CA_FILE }) + end + end + end + end + + def test_imaps_verify_none + if defined?(OpenSSL) + assert_nothing_raised do + imaps_test do |port| + Net::IMAP.new("localhost", + :port => port, + :ssl => { :verify_mode => OpenSSL::SSL::VERIFY_NONE }) + end + end + end + end + + def test_imaps_post_connection_check + if defined?(OpenSSL) + 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 + end + + def test_starttls + imap = nil + if defined?(OpenSSL) + starttls_test do |port| + imap = Net::IMAP.new("localhost", :port => port) + imap.starttls(:ca_file => CA_FILE) + imap + end + end + ensure + if imap && !imap.disconnected? + imap.disconnect + end + end + + def test_unexpected_eof + server = TCPServer.new(0) + port = server.addr[1] + Thread.start do + begin + sock = 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 + begin + imap = Net::IMAP.new("localhost", :port => port) + assert_raise(EOFError) do + imap.logout + end + ensure + imap.disconnect if imap + end + ensure + server.close + 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 + begin + imap = yield(port) + imap.logout + ensure + imap.disconnect if imap + end + 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 + begin + imap = yield(port) + imap.logout + ensure + imap.disconnect if imap + end + ensure + server.close + end + end +end |