diff options
Diffstat (limited to 'test/uri')
-rw-r--r-- | test/uri/test_common.rb | 111 | ||||
-rw-r--r-- | test/uri/test_ftp.rb | 10 | ||||
-rw-r--r-- | test/uri/test_generic.rb | 24 | ||||
-rw-r--r-- | test/uri/test_http.rb | 8 | ||||
-rw-r--r-- | test/uri/test_ldap.rb | 14 | ||||
-rw-r--r-- | test/uri/test_parser.rb | 46 | ||||
-rw-r--r-- | test/uri/test_ws.rb | 8 | ||||
-rw-r--r-- | test/uri/test_wss.rb | 65 |
8 files changed, 243 insertions, 43 deletions
diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb index 5e30cda41d..1df19e6968 100644 --- a/test/uri/test_common.rb +++ b/test/uri/test_common.rb @@ -3,10 +3,7 @@ require 'test/unit' require 'envutil' require 'uri' -module URI - - -class TestCommon < Test::Unit::TestCase +class URI::TestCommon < Test::Unit::TestCase def setup end @@ -41,18 +38,52 @@ class TestCommon < Test::Unit::TestCase RUBY end + DEFAULT_SCHEMES = ["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort.freeze + def test_register_scheme - assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS"].sort, URI.scheme_list.keys.sort) + assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort) foobar = Class.new(URI::Generic) URI.register_scheme 'FOOBAR', foobar begin - assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "FOOBAR"].sort, URI.scheme_list.keys.sort) + assert_include(URI.scheme_list.keys, "FOOBAR") + assert_equal foobar, URI.parse('foobar://localhost').class ensure URI.const_get(:Schemes).send(:remove_const, :FOOBAR) end - assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS"].sort, URI.scheme_list.keys.sort) + assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort) + end + + def test_register_scheme_lowercase + assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort) + + foobar = Class.new(URI::Generic) + URI.register_scheme 'foobarlower', foobar + begin + assert_include(URI.scheme_list.keys, "FOOBARLOWER") + assert_equal foobar, URI.parse('foobarlower://localhost').class + ensure + URI.const_get(:Schemes).send(:remove_const, :FOOBARLOWER) + end + + assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort) + end + + def test_register_scheme_with_symbols + # Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml + some_uri_class = Class.new(URI::Generic) + assert_raise(NameError) { URI.register_scheme 'ms-search', some_uri_class } + assert_raise(NameError) { URI.register_scheme 'microsoft.windows.camera', some_uri_class } + assert_raise(NameError) { URI.register_scheme 'coaps+ws', some_uri_class } + + ms_search_class = Class.new(URI::Generic) + URI.register_scheme 'MS_SEARCH', ms_search_class + begin + assert_equal URI::Generic, URI.parse('ms-search://localhost').class + ensure + URI.const_get(:Schemes).send(:remove_const, :MS_SEARCH) + end end def test_regexp @@ -78,6 +109,17 @@ class TestCommon < Test::Unit::TestCase assert_raise(NoMethodError) { Object.new.URI("http://www.ruby-lang.org/") } end + def test_parse_timeout + pre = ->(n) { + 'https://example.com/dir/' + 'a' * (n * 100) + '/##.jpg' + } + assert_linear_performance((1..3).map {|i| 10**i}, rehearsal: 1000, pre: pre) do |uri| + assert_raise(URI::InvalidURIError) do + URI.parse(uri) + end + end + end + def test_encode_www_form_component assert_equal("%00+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \ "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E", @@ -130,6 +172,58 @@ class TestCommon < Test::Unit::TestCase assert_nothing_raised(ArgumentError){URI.decode_www_form_component("x"*(1024*1024))} end + def test_encode_uri_component + assert_equal("%00%20%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \ + "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E", + URI.encode_uri_component("\x00 !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~")) + assert_equal("%95A", URI.encode_uri_component( + "\x95\x41".force_encoding(Encoding::Shift_JIS))) + assert_equal("0B", URI.encode_uri_component( + "\x30\x42".force_encoding(Encoding::UTF_16BE))) + assert_equal("%1B%24B%24%22%1B%28B", URI.encode_uri_component( + "\e$B$\"\e(B".force_encoding(Encoding::ISO_2022_JP))) + + assert_equal("%E3%81%82", URI.encode_uri_component( + "\u3042", Encoding::ASCII_8BIT)) + assert_equal("%82%A0", URI.encode_uri_component( + "\u3042", Encoding::Windows_31J)) + assert_equal("%E3%81%82", URI.encode_uri_component( + "\u3042", Encoding::UTF_8)) + + assert_equal("%82%A0", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::ASCII_8BIT)) + assert_equal("%A4%A2", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::EUC_JP)) + assert_equal("%E3%81%82", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::UTF_8)) + assert_equal("B0", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::UTF_16LE)) + assert_equal("%26%23730%3B", URI.encode_uri_component( + "\u02DA", Encoding::WINDOWS_1252)) + + # invalid + assert_equal("%EF%BF%BD%EF%BF%BD", URI.encode_uri_component( + "\xE3\x81\xFF", "utf-8")) + assert_equal("%E6%9F%8A%EF%BF%BD%EF%BF%BD", URI.encode_uri_component( + "\x95\x41\xff\xff".force_encoding(Encoding::Shift_JIS), "utf-8")) + end + + def test_decode_uri_component + assert_equal(" +!\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~", + URI.decode_uri_component( + "%20+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \ + "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E")) + assert_equal("\xA1\xA2".force_encoding(Encoding::EUC_JP), + URI.decode_uri_component("%A1%A2", "EUC-JP")) + assert_equal("\xE3\x81\x82\xE3\x81\x82".force_encoding("UTF-8"), + URI.decode_uri_component("\xE3\x81\x82%E3%81%82".force_encoding("UTF-8"))) + + assert_raise(ArgumentError){URI.decode_uri_component("%")} + assert_raise(ArgumentError){URI.decode_uri_component("%a")} + assert_raise(ArgumentError){URI.decode_uri_component("x%a_")} + assert_nothing_raised(ArgumentError){URI.decode_uri_component("x"*(1024*1024))} + end + def test_encode_www_form assert_equal("a=1", URI.encode_www_form("a" => "1")) assert_equal("a=1", URI.encode_www_form(a: 1)) @@ -195,6 +289,3 @@ class TestCommon < Test::Unit::TestCase private def s(str) str.force_encoding(Encoding::Windows_31J); end end - - -end diff --git a/test/uri/test_ftp.rb b/test/uri/test_ftp.rb index 0eec984db8..f45bb0667c 100644 --- a/test/uri/test_ftp.rb +++ b/test/uri/test_ftp.rb @@ -2,10 +2,7 @@ require 'test/unit' require 'uri/ftp' -module URI - - -class TestFTP < Test::Unit::TestCase +class URI::TestFTP < Test::Unit::TestCase def setup end @@ -29,7 +26,7 @@ class TestFTP < Test::Unit::TestCase end def test_parse_invalid - assert_raise(InvalidURIError){URI.parse('ftp:example')} + assert_raise(URI::InvalidURIError) {URI.parse('ftp:example')} end def test_paths @@ -62,6 +59,3 @@ class TestFTP < Test::Unit::TestCase end end end - - -end diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index fdb405e396..8209363b82 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -24,7 +24,19 @@ class URI::TestGeneric < Test::Unit::TestCase assert_equal "file:///foo", URI("file:///foo").to_s assert_equal "postgres:///foo", URI("postgres:///foo").to_s - assert_equal "http:/foo", URI("http:///foo").to_s + assert_equal "http:///foo", URI("http:///foo").to_s + assert_equal "http:/foo", URI("http:/foo").to_s + + uri = URI('rel_path') + assert_equal "rel_path", uri.to_s + uri.scheme = 'http' + assert_equal "http:rel_path", uri.to_s + uri.host = 'h' + assert_equal "http://h/rel_path", uri.to_s + uri.port = 8080 + assert_equal "http://h:8080/rel_path", uri.to_s + uri.host = nil + assert_equal "http::8080/rel_path", uri.to_s end def test_parse @@ -157,6 +169,12 @@ class URI::TestGeneric < Test::Unit::TestCase assert_equal(nil, url.user) assert_equal(nil, url.password) assert_equal(nil, url.userinfo) + + # sec-156615 + url = URI.parse('http:////example.com') + # must be empty string to identify as path-abempty, not path-absolute + assert_equal('', url.host) + assert_equal('http:////example.com', url.to_s) end def test_parse_scheme_with_symbols @@ -970,6 +988,10 @@ class URI::TestGeneric < Test::Unit::TestCase end end + def test_split + assert_equal [nil, nil, nil, nil, nil, "", nil, nil, nil], URI.split("//") + end + class CaseInsensitiveEnv def initialize(h={}) @h = {} diff --git a/test/uri/test_http.rb b/test/uri/test_http.rb index 748e90a322..e937b1a26b 100644 --- a/test/uri/test_http.rb +++ b/test/uri/test_http.rb @@ -3,10 +3,7 @@ require 'test/unit' require 'uri/http' require 'uri/https' -module URI - - -class TestHTTP < Test::Unit::TestCase +class URI::TestHTTP < Test::Unit::TestCase def setup end @@ -82,6 +79,3 @@ class TestHTTP < Test::Unit::TestCase assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin) end end - - -end diff --git a/test/uri/test_ldap.rb b/test/uri/test_ldap.rb index 64845e487a..9c4506a357 100644 --- a/test/uri/test_ldap.rb +++ b/test/uri/test_ldap.rb @@ -2,10 +2,7 @@ require 'test/unit' require 'uri/ldap' -module URI - - -class TestLDAP < Test::Unit::TestCase +class URI::TestLDAP < Test::Unit::TestCase def setup end @@ -39,7 +36,7 @@ class TestLDAP < Test::Unit::TestCase # from RFC2255, section 6. { 'ldap:///o=University%20of%20Michigan,c=US' => - ['ldap', nil, URI::LDAP::DEFAULT_PORT, + ['ldap', '', URI::LDAP::DEFAULT_PORT, 'o=University%20of%20Michigan,c=US', nil, nil, nil, nil], @@ -74,12 +71,12 @@ class TestLDAP < Test::Unit::TestCase nil, '(int=%5c00%5c00%5c00%5c04)', nil, nil], 'ldap:///??sub??bindname=cn=Manager%2co=Foo' => - ['ldap', nil, URI::LDAP::DEFAULT_PORT, + ['ldap', '', URI::LDAP::DEFAULT_PORT, '', nil, 'sub', nil, 'bindname=cn=Manager%2co=Foo'], 'ldap:///??sub??!bindname=cn=Manager%2co=Foo' => - ['ldap', nil, URI::LDAP::DEFAULT_PORT, + ['ldap', '', URI::LDAP::DEFAULT_PORT, '', nil, 'sub', nil, '!bindname=cn=Manager%2co=Foo'], }.each do |url2, ary| @@ -100,6 +97,3 @@ class TestLDAP < Test::Unit::TestCase assert_raise(URI::InvalidURIError) {URI.parse("ldap:https://example.com")} end end - - -end diff --git a/test/uri/test_parser.rb b/test/uri/test_parser.rb index 03de137788..75c02fe65b 100644 --- a/test/uri/test_parser.rb +++ b/test/uri/test_parser.rb @@ -50,6 +50,15 @@ class URI::TestParser < Test::Unit::TestCase assert_raise(URI::InvalidURIError) { URI.parse('https://www.example.com/search?q=%XX') } end + def test_parse_auth + str = "http://al%40ice:p%40s%25sword@example.com/dir%2Fname/subdir?foo=bar%40example.com" + uri = URI.parse(str) + assert_equal "al%40ice", uri.user + assert_equal "p%40s%25sword", uri.password + assert_equal "al@ice", uri.decoded_user + assert_equal "p@s%sword", uri.decoded_password + end + def test_raise_bad_uri_for_integer assert_raise(URI::InvalidURIError) do URI.parse(1) @@ -63,4 +72,41 @@ class URI::TestParser < Test::Unit::TestCase assert_equal("\u3042", p1.unescape('%e3%81%82'.force_encoding(Encoding::US_ASCII))) assert_equal("\xe3\x83\x90\xe3\x83\x90", p1.unescape("\xe3\x83\x90%e3%83%90")) end + + def test_split + assert_equal(["http", nil, "example.com", nil, nil, "", nil, nil, nil], URI.split("http://example.com")) + assert_equal(["http", nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("http://[0::0]")) + assert_equal([nil, nil, "example.com", nil, nil, "", nil, nil, nil], URI.split("//example.com")) + assert_equal([nil, nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("//[0::0]")) + + assert_equal(["a", nil, nil, nil, nil, "", nil, nil, nil], URI.split("a:")) + assert_raise(URI::InvalidURIError) do + URI.parse("::") + end + assert_raise(URI::InvalidURIError) do + URI.parse("foo@example:foo") + end + end + + def test_rfc2822_parse_relative_uri + pre = ->(length) { + " " * length + "\0" + } + parser = URI::RFC2396_Parser.new + assert_linear_performance((1..5).map {|i| 10**i}, pre: pre) do |uri| + assert_raise(URI::InvalidURIError) do + parser.split(uri) + end + end + end + + def test_rfc3986_port_check + pre = ->(length) {"\t" * length + "a"} + uri = URI.parse("http://my.example.com") + assert_linear_performance((1..5).map {|i| 10**i}, pre: pre) do |port| + assert_raise(URI::InvalidComponentError) do + uri.port = port + end + end + end end diff --git a/test/uri/test_ws.rb b/test/uri/test_ws.rb index 17acb0d9f2..f3918f617c 100644 --- a/test/uri/test_ws.rb +++ b/test/uri/test_ws.rb @@ -3,10 +3,7 @@ require 'test/unit' require 'uri/http' require 'uri/ws' -module URI - - -class TestWS < Test::Unit::TestCase +class URI::TestWS < Test::Unit::TestCase def setup end @@ -66,6 +63,3 @@ class TestWS < Test::Unit::TestCase end end end - - -end diff --git a/test/uri/test_wss.rb b/test/uri/test_wss.rb new file mode 100644 index 0000000000..13a2583059 --- /dev/null +++ b/test/uri/test_wss.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: false +require 'test/unit' +require 'uri/https' +require 'uri/wss' + +class URI::TestWSS < Test::Unit::TestCase + def setup + end + + def teardown + end + + def uri_to_ary(uri) + uri.class.component.collect {|c| uri.send(c)} + end + + def test_build + u = URI::WSS.build(host: 'www.example.com', path: '/foo/bar') + assert_kind_of(URI::WSS, u) + end + + def test_parse + u = URI.parse('wss://a') + assert_kind_of(URI::WSS, u) + assert_equal(['wss', + nil, 'a', URI::HTTPS.default_port, + '', nil], uri_to_ary(u)) + end + + def test_normalize + host = 'aBcD' + u1 = URI.parse('wss://' + host + '/eFg?HiJ') + u2 = URI.parse('wss://' + host.downcase + '/eFg?HiJ') + assert(u1.normalize.host == 'abcd') + assert(u1.normalize.path == u1.path) + assert(u1.normalize == u2.normalize) + assert(!u1.normalize.host.equal?(u1.host)) + assert( u2.normalize.host.equal?(u2.host)) + + assert_equal('wss://abc/', URI.parse('wss://abc').normalize.to_s) + end + + def test_equal + assert(URI.parse('wss://abc') == URI.parse('wss://ABC')) + assert(URI.parse('wss://abc/def') == URI.parse('wss://ABC/def')) + assert(URI.parse('wss://abc/def') != URI.parse('wss://ABC/DEF')) + end + + def test_request_uri + assert_equal('/', URI.parse('wss://a.b.c/').request_uri) + assert_equal('/?abc=def', URI.parse('wss://a.b.c/?abc=def').request_uri) + assert_equal('/', URI.parse('wss://a.b.c').request_uri) + assert_equal('/?abc=def', URI.parse('wss://a.b.c?abc=def').request_uri) + assert_equal(nil, URI.parse('wss:foo').request_uri) + end + + def test_select + assert_equal(['wss', 'a.b.c', 443], URI.parse('wss://a.b.c/').select(:scheme, :host, :port)) + u = URI.parse('wss://a.b.c/') + assert_equal(uri_to_ary(u), u.select(*u.component)) + assert_raise(ArgumentError) do + u.select(:scheme, :host, :not_exist, :port) + end + end +end |