diff options
Diffstat (limited to 'test')
42 files changed, 1249 insertions, 83 deletions
diff --git a/test/-ext-/debug/test_profile_frames.rb b/test/-ext-/debug/test_profile_frames.rb index 5ea506046e..0335267ee9 100644 --- a/test/-ext-/debug/test_profile_frames.rb +++ b/test/-ext-/debug/test_profile_frames.rb @@ -3,6 +3,16 @@ require 'test/unit' require '-test-/debug' class SampleClassForTestProfileFrames + class << self + attr_accessor :sample4 + end + + self.sample4 = Module.new do + def self.corge(block) + Sample2.new.baz(block) + end + end + class Sample2 def baz(block) instance_eval "def zab(block) block.call end" @@ -10,8 +20,16 @@ class SampleClassForTestProfileFrames end end + module Sample3 + class << self + def qux(block) + SampleClassForTestProfileFrames.sample4.corge(block) + end + end + end + def self.bar(block) - Sample2.new.baz(block) + Sample3.qux(block) end def foo(block) @@ -29,6 +47,8 @@ class TestProfileFrames < Test::Unit::TestCase "test_profile_frames", "zab", "baz", + "corge", + "qux", "bar", "foo", "test_profile_frames", @@ -37,6 +57,8 @@ class TestProfileFrames < Test::Unit::TestCase "test_profile_frames", "zab", "baz", + "corge", + "qux", "bar", "foo", "test_profile_frames", @@ -45,6 +67,8 @@ class TestProfileFrames < Test::Unit::TestCase "TestProfileFrames#test_profile_frames", "#{obj.inspect}.zab", "SampleClassForTestProfileFrames::Sample2#baz", + "#{SampleClassForTestProfileFrames.sample4.inspect}.corge", + "SampleClassForTestProfileFrames::Sample3.qux", "SampleClassForTestProfileFrames.bar", "SampleClassForTestProfileFrames#foo", "TestProfileFrames#test_profile_frames", @@ -53,17 +77,21 @@ class TestProfileFrames < Test::Unit::TestCase TestProfileFrames, obj, SampleClassForTestProfileFrames::Sample2, + SampleClassForTestProfileFrames.sample4, + SampleClassForTestProfileFrames::Sample3, SampleClassForTestProfileFrames, # singleton method SampleClassForTestProfileFrames, TestProfileFrames, ] singleton_method_p = [ - false, true, false, true, false, false, false, + false, true, false, true, true, true, false, false, false, ] method_names = [ "test_profile_frames", "zab", "baz", + "corge", + "qux", "bar", "foo", "test_profile_frames", @@ -72,14 +100,14 @@ class TestProfileFrames < Test::Unit::TestCase "TestProfileFrames#test_profile_frames", "#{obj.inspect}.zab", "SampleClassForTestProfileFrames::Sample2#baz", + "#{SampleClassForTestProfileFrames.sample4.inspect}.corge", + "SampleClassForTestProfileFrames::Sample3.qux", "SampleClassForTestProfileFrames.bar", "SampleClassForTestProfileFrames#foo", "TestProfileFrames#test_profile_frames", ] - paths = [ file=__FILE__, "(eval)", file, file, file, file ] - absolute_paths = [ file, nil, file, file, file, file ] - - # pp frames + paths = [ file=__FILE__, "(eval)", file, file, file, file, file, file ] + absolute_paths = [ file, nil, file, file, file, file, file, file ] assert_equal(labels.size, frames.size) diff --git a/test/cgi/test_cgi_cookie.rb b/test/cgi/test_cgi_cookie.rb index 115a57e4a1..985cc0d7a1 100644 --- a/test/cgi/test_cgi_cookie.rb +++ b/test/cgi/test_cgi_cookie.rb @@ -101,6 +101,11 @@ class CGICookieTest < Test::Unit::TestCase end end + def test_cgi_cookie_parse_not_decode_name + cookie_str = "%66oo=baz;foo=bar" + cookies = CGI::Cookie.parse(cookie_str) + assert_equal({"%66oo" => ["baz"], "foo" => ["bar"]}, cookies) + end def test_cgi_cookie_arrayinterface cookie = CGI::Cookie.new('name1', 'a', 'b', 'c') diff --git a/test/coverage/test_coverage.rb b/test/coverage/test_coverage.rb index 30523c341a..1625d48845 100644 --- a/test/coverage/test_coverage.rb +++ b/test/coverage/test_coverage.rb @@ -696,4 +696,16 @@ class TestCoverage < Test::Unit::TestCase } } end + + def test_stop_wrong_peephole_optimization + result = { + :lines => [1, 1, 1, nil] + } + assert_coverage(<<~"end;", { lines: true }, result) + raise if 1 == 2 + while true + break + end + end; + end end diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index e17fd3eb25..d1163b29cb 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'test/unit' require 'date' +require 'timeout' class TestDateParse < Test::Unit::TestCase @@ -121,8 +122,6 @@ class TestDateParse < Test::Unit::TestCase [['S40.05.23T23:55:21-09:00',false],[1965,5,23,23,55,21,'-09:00',-9*3600,nil], __LINE__], [['H11.05.23 23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil], __LINE__], [['H11.05.23T23:55:21Z',false],[1999,5,23,23,55,21,'Z',0,nil], __LINE__], - [['H31.04.30 23:55:21Z',false],[2019,4,30,23,55,21,'Z',0,nil], __LINE__], - [['H31.04.30T23:55:21Z',false],[2019,4,30,23,55,21,'Z',0,nil], __LINE__], # ofx date [['19990523235521',false],[1999,5,23,23,55,21,nil,nil,nil], __LINE__], @@ -418,14 +417,7 @@ class TestDateParse < Test::Unit::TestCase a[1] = -1 a[2] = h[:yday] end - l = format('<failed at line %d>', l) - assert_equal(y, a, l) - if y[6] - h = Date._parse(x[0].dup.taint, *x[1..-1]) - assert_equal(y[6], h[:zone], l) - assert_equal(y[6].encoding, h[:zone].encoding, l) - assert_predicate(h[:zone], :tainted?, l) - end + assert_equal(y, a, format('<failed at line %d>', l)) end end @@ -832,6 +824,13 @@ class TestDateParse < Test::Unit::TestCase h = Date._iso8601('') assert_equal({}, h) + + h = Date._iso8601(nil) + assert_equal({}, h) + + h = Date._iso8601('01-02-03T04:05:06Z'.to_sym) + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) end def test__rfc3339 @@ -847,6 +846,13 @@ class TestDateParse < Test::Unit::TestCase h = Date._rfc3339('') assert_equal({}, h) + + h = Date._rfc3339(nil) + assert_equal({}, h) + + h = Date._rfc3339('2001-02-03T04:05:06Z'.to_sym) + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) end def test__xmlschema @@ -929,6 +935,13 @@ class TestDateParse < Test::Unit::TestCase h = Date._xmlschema('') assert_equal({}, h) + + h = Date._xmlschema(nil) + assert_equal({}, h) + + h = Date._xmlschema('2001-02-03'.to_sym) + assert_equal([2001, 2, 3, nil, nil, nil, nil], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) end def test__rfc2822 @@ -961,6 +974,13 @@ class TestDateParse < Test::Unit::TestCase h = Date._rfc2822('') assert_equal({}, h) + + h = Date._rfc2822(nil) + assert_equal({}, h) + + h = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 UT'.to_sym) + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) end def test__httpdate @@ -981,6 +1001,13 @@ class TestDateParse < Test::Unit::TestCase h = Date._httpdate('') assert_equal({}, h) + + h = Date._httpdate(nil) + assert_equal({}, h) + + h = Date._httpdate('Sat, 03 Feb 2001 04:05:06 GMT'.to_sym) + assert_equal([2001, 2, 3, 4, 5, 6, 0], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) end def test__jisx0301 @@ -1057,6 +1084,13 @@ class TestDateParse < Test::Unit::TestCase h = Date._jisx0301('') assert_equal({}, h) + + h = Date._jisx0301(nil) + assert_equal({}, h) + + h = Date._jisx0301('H13.02.03T04:05:06.07+0100'.to_sym) + assert_equal([2001, 2, 3, 4, 5, 6, 3600], + h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) end def test_iso8601 @@ -1201,16 +1235,34 @@ class TestDateParse < Test::Unit::TestCase s0 = s.dup assert_not_equal({}, Date._jisx0301(s)) assert_equal(s0, s) - - s = 'H31.04.30T04:05:06,07Z' - s0 = s.dup - assert_not_equal({}, Date._jisx0301(s)) - assert_equal(s0, s) - - s = 'H31.05.01T04:05:06,07Z' - s0 = s.dup - assert_not_equal({}, Date._jisx0301(s)) - assert_equal(s0, s) end + def test_length_limit + assert_raise(ArgumentError) { Date._parse("1" * 1000) } + assert_raise(ArgumentError) { Date._iso8601("1" * 1000) } + assert_raise(ArgumentError) { Date._rfc3339("1" * 1000) } + assert_raise(ArgumentError) { Date._xmlschema("1" * 1000) } + assert_raise(ArgumentError) { Date._rfc2822("1" * 1000) } + assert_raise(ArgumentError) { Date._rfc822("1" * 1000) } + assert_raise(ArgumentError) { Date._jisx0301("1" * 1000) } + + assert_raise(ArgumentError) { Date.parse("1" * 1000) } + assert_raise(ArgumentError) { Date.iso8601("1" * 1000) } + assert_raise(ArgumentError) { Date.rfc3339("1" * 1000) } + assert_raise(ArgumentError) { Date.xmlschema("1" * 1000) } + assert_raise(ArgumentError) { Date.rfc2822("1" * 1000) } + assert_raise(ArgumentError) { Date.rfc822("1" * 1000) } + assert_raise(ArgumentError) { Date.jisx0301("1" * 1000) } + + assert_raise(ArgumentError) { DateTime.parse("1" * 1000) } + assert_raise(ArgumentError) { DateTime.iso8601("1" * 1000) } + assert_raise(ArgumentError) { DateTime.rfc3339("1" * 1000) } + assert_raise(ArgumentError) { DateTime.xmlschema("1" * 1000) } + assert_raise(ArgumentError) { DateTime.rfc2822("1" * 1000) } + assert_raise(ArgumentError) { DateTime.rfc822("1" * 1000) } + assert_raise(ArgumentError) { DateTime.jisx0301("1" * 1000) } + + assert_raise(ArgumentError) { Date._parse("Jan " + "9" * 1000000) } + assert_raise(Timeout::Error) { Timeout.timeout(1) { Date._parse("Jan " + "9" * 1000000, limit: nil) } } + end end diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb index a5219644bb..b3fe7774ed 100644 --- a/test/net/ftp/test_ftp.rb +++ b/test/net/ftp/test_ftp.rb @@ -61,7 +61,7 @@ class FTPTest < Test::Unit::TestCase end def test_parse227 - ftp = Net::FTP.new + ftp = Net::FTP.new(nil, use_pasv_ip: true) host, port = ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1,12,34)") assert_equal("192.168.0.1", host) assert_equal(3106, port) @@ -80,6 +80,14 @@ class FTPTest < Test::Unit::TestCase assert_raise(Net::FTPProtoError) do ftp.send(:parse227, "227 ) foo bar (") end + + ftp = Net::FTP.new + sock = OpenStruct.new + sock.remote_address = OpenStruct.new + sock.remote_address.ip_address = "10.0.0.1" + ftp.instance_variable_set(:@bare_sock, sock) + host, port = ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1,12,34)") + assert_equal("10.0.0.1", host) end def test_parse228 @@ -2360,10 +2368,155 @@ EOF end end + def test_ignore_pasv_ip + commands = [] + binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3 + server = create_ftp_server(nil, "127.0.0.1") { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("331 Please specify the password.\r\n") + commands.push(sock.gets) + sock.print("230 Login successful.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + line = sock.gets + commands.push(line) + data_server = TCPServer.new("127.0.0.1", 0) + port = data_server.local_address.ip_port + sock.printf("227 Entering Passive Mode (999,0,0,1,%s).\r\n", + port.divmod(256).join(",")) + commands.push(sock.gets) + sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n") + conn = data_server.accept + binary_data.scan(/.{1,1024}/nm) do |s| + conn.print(s) + end + conn.shutdown(Socket::SHUT_WR) + conn.read + conn.close + data_server.close + sock.print("226 Transfer complete.\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.passive = true + ftp.read_timeout *= 5 if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # for --jit-wait + ftp.connect("127.0.0.1", server.port) + ftp.login + assert_match(/\AUSER /, commands.shift) + assert_match(/\APASS /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + buf = ftp.getbinaryfile("foo", nil) + assert_equal(binary_data, buf) + assert_equal(Encoding::ASCII_8BIT, buf.encoding) + assert_equal("PASV\r\n", commands.shift) + assert_equal("RETR foo\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + + def test_use_pasv_ip + commands = [] + binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3 + server = create_ftp_server(nil, "127.0.0.1") { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("331 Please specify the password.\r\n") + commands.push(sock.gets) + sock.print("230 Login successful.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + line = sock.gets + commands.push(line) + data_server = TCPServer.new("127.0.0.1", 0) + port = data_server.local_address.ip_port + sock.printf("227 Entering Passive Mode (127,0,0,1,%s).\r\n", + port.divmod(256).join(",")) + commands.push(sock.gets) + sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n") + conn = data_server.accept + binary_data.scan(/.{1,1024}/nm) do |s| + conn.print(s) + end + conn.shutdown(Socket::SHUT_WR) + conn.read + conn.close + data_server.close + sock.print("226 Transfer complete.\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.passive = true + ftp.use_pasv_ip = true + ftp.read_timeout *= 5 if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # for --jit-wait + ftp.connect("127.0.0.1", server.port) + ftp.login + assert_match(/\AUSER /, commands.shift) + assert_match(/\APASS /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + buf = ftp.getbinaryfile("foo", nil) + assert_equal(binary_data, buf) + assert_equal(Encoding::ASCII_8BIT, buf.encoding) + assert_equal("PASV\r\n", commands.shift) + assert_equal("RETR foo\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + + def test_use_pasv_invalid_ip + commands = [] + binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3 + server = create_ftp_server(nil, "127.0.0.1") { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("331 Please specify the password.\r\n") + commands.push(sock.gets) + sock.print("230 Login successful.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + line = sock.gets + commands.push(line) + sock.print("227 Entering Passive Mode (999,0,0,1,48,57).\r\n") + commands.push(sock.gets) + } + begin + begin + ftp = Net::FTP.new + ftp.passive = true + ftp.use_pasv_ip = true + ftp.read_timeout *= 5 if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # for --jit-wait + ftp.connect("127.0.0.1", server.port) + ftp.login + assert_match(/\AUSER /, commands.shift) + assert_match(/\APASS /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + assert_raise(SocketError) do + ftp.getbinaryfile("foo", nil) + end + ensure + ftp.close if ftp + end + ensure + server.close + end + end + private - def create_ftp_server(sleep_time = nil) - server = TCPServer.new(SERVER_ADDR, 0) + def create_ftp_server(sleep_time = nil, addr = SERVER_ADDR) + server = TCPServer.new(addr, 0) @thread = Thread.start do if sleep_time sleep(sleep_time) diff --git a/test/net/http/test_https.rb b/test/net/http/test_https.rb index c1d486470a..784f002c22 100644 --- a/test/net/http/test_https.rb +++ b/test/net/http/test_https.rb @@ -44,8 +44,10 @@ class TestNetHTTPS < Test::Unit::TestCase http.request_get("/") {|res| assert_equal($test_net_http_data, res.body) } - assert_equal(CA_CERT.to_der, certs[0].to_der) - assert_equal(SERVER_CERT.to_der, certs[1].to_der) + # TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility + certs.zip([CA_CERT, SERVER_CERT]) do |actual, expected| + assert_equal(expected.to_der, actual.to_der) + end rescue SystemCallError skip $! end diff --git a/test/net/imap/test_imap.rb b/test/net/imap/test_imap.rb index 936f4e0f42..81928cb8fe 100644 --- a/test/net/imap/test_imap.rb +++ b/test/net/imap/test_imap.rb @@ -127,6 +127,24 @@ class IMAPTest < Test::Unit::TestCase imap.disconnect end end + + def test_starttls_stripping + starttls_stripping_test do |port| + imap = Net::IMAP.new("localhost", :port => port) + assert_raise(Net::IMAP::UnknownResponseError) do + imap.starttls(:ca_file => CA_FILE) + end + imap + end + end + end + + def start_server + th = Thread.new do + yield + end + @threads << th + sleep 0.1 until th.stop? end def test_unexpected_eof @@ -762,6 +780,27 @@ EOF end end + def starttls_stripping_test + server = create_tcp_server + port = server.addr[1] + start_server do + sock = server.accept + begin + sock.print("* OK test server\r\n") + sock.gets + sock.print("RUBY0001 BUG unhandled command\r\n") + ensure + sock.close + server.close + end + end + begin + imap = yield(port) + ensure + imap.disconnect if imap && !imap.disconnected? + end + end + def create_tcp_server return TCPServer.new(server_addr, 0) end diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 947170203a..f139b90efe 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -98,6 +98,8 @@ class TestObjSpace < Test::Unit::TestCase res = ObjectSpace.count_imemo_objects assert_not_empty(res) assert_not_nil(res[:imemo_cref]) + assert_not_empty res.inspect + arg = {} res = ObjectSpace.count_imemo_objects(arg) assert_not_empty(res) diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index f8e4937802..750fabf039 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -824,7 +824,7 @@ class TestPathname < Test::Unit::TestCase old = path.lstat.mode begin path.lchmod(0444) - rescue NotImplementedError + rescue NotImplementedError, Errno::EOPNOTSUPP next end assert_equal(0444, path.lstat.mode & 0777) diff --git a/test/rdoc/test_rdoc_rdoc.rb b/test/rdoc/test_rdoc_rdoc.rb index 3bce54b243..123b1a4f87 100644 --- a/test/rdoc/test_rdoc_rdoc.rb +++ b/test/rdoc/test_rdoc_rdoc.rb @@ -420,6 +420,18 @@ class TestRDocRDoc < RDoc::TestCase end end + def test_remove_unparseable_CVE_2021_31799 + skip 'for Un*x platforms' if Gem.win_platform? + temp_dir do + file_list = ['| touch evil.txt && echo tags'] + file_list.each do |f| + FileUtils.touch f + end + assert_equal file_list, @rdoc.remove_unparseable(file_list) + assert_equal file_list, Dir.children('.') + end + end + def test_setup_output_dir Dir.mktmpdir {|d| path = File.join d, 'testdir' diff --git a/test/resolv/test_dns.rb b/test/resolv/test_dns.rb index 669d86bd83..c49c152208 100644 --- a/test/resolv/test_dns.rb +++ b/test/resolv/test_dns.rb @@ -128,6 +128,119 @@ class TestResolvDNS < Test::Unit::TestCase } end + def test_query_ipv4_duplicate_responses + begin + OpenSSL + rescue LoadError + skip 'autoload problem. see [ruby-dev:45021][Bug #5786]' + end if defined?(OpenSSL) + + with_udp('127.0.0.1', 0) {|u| + _, server_port, _, server_address = u.addr + begin + client_thread = Thread.new { + Resolv::DNS.open(:nameserver_port => [[server_address, server_port]], :search => ['bad1.com', 'bad2.com', 'good.com'], ndots: 5) {|dns| + dns.getaddress("example") + } + } + server_thread = Thread.new { + 3.times do + msg, (_, client_port, _, client_address) = Timeout.timeout(5) {u.recvfrom(4096)} + id, flags, qdcount, ancount, nscount, arcount = msg.unpack("nnnnnn") + + qr = (flags & 0x8000) >> 15 + opcode = (flags & 0x7800) >> 11 + aa = (flags & 0x0400) >> 10 + tc = (flags & 0x0200) >> 9 + rd = (flags & 0x0100) >> 8 + ra = (flags & 0x0080) >> 7 + z = (flags & 0x0070) >> 4 + rcode = flags & 0x000f + rest = msg[12..-1] + + questions = msg.bytes[12..-1] + labels = [] + idx = 0 + while idx < questions.length-5 + size = questions[idx] + labels << questions[idx+1..idx+size].pack('c*') + idx += size+1 + end + hostname = labels.join('.') + + if hostname == "example.good.com" + id = id + qr = 1 + opcode = opcode + aa = 0 + tc = 0 + rd = rd + ra = 1 + z = 0 + rcode = 0 + qdcount = 1 + ancount = 1 + nscount = 0 + arcount = 0 + word2 = (qr << 15) | + (opcode << 11) | + (aa << 10) | + (tc << 9) | + (rd << 8) | + (ra << 7) | + (z << 4) | + rcode + msg = [id, word2, qdcount, ancount, nscount, arcount].pack("nnnnnn") + msg << questions.pack('c*') + type = 1 + klass = 1 + ttl = 3600 + rdlength = 4 + rdata = [52,0,2,1].pack("CCCC") + rr = [0xc00c, type, klass, ttl, rdlength, rdata].pack("nnnNna*") + msg << rr + rdata = [52,0,2,2].pack("CCCC") + rr = [0xc00c, type, klass, ttl, rdlength, rdata].pack("nnnNna*") + msg << rr + + u.send(msg, 0, client_address, client_port) + else + id = id + qr = 1 + opcode = opcode + aa = 0 + tc = 0 + rd = rd + ra = 1 + z = 0 + rcode = 3 + qdcount = 1 + ancount = 0 + nscount = 0 + arcount = 0 + word2 = (qr << 15) | + (opcode << 11) | + (aa << 10) | + (tc << 9) | + (rd << 8) | + (ra << 7) | + (z << 4) | + rcode + msg = [id, word2, qdcount, ancount, nscount, arcount].pack("nnnnnn") + msg << questions.pack('c*') + + u.send(msg, 0, client_address, client_port) + u.send(msg, 0, client_address, client_port) + end + end + } + result, _ = assert_join_threads([client_thread, server_thread]) + assert_instance_of(Resolv::IPv4, result) + assert_equal("52.0.2.1", result.to_s) + end + } + end + def test_query_ipv4_address_timeout with_udp('127.0.0.1', 0) {|u| _, port , _, host = u.addr diff --git a/test/rexml/parse/test_document_type_declaration.rb b/test/rexml/parse/test_document_type_declaration.rb index 80f70888fb..55713909e7 100644 --- a/test/rexml/parse/test_document_type_declaration.rb +++ b/test/rexml/parse/test_document_type_declaration.rb @@ -5,17 +5,187 @@ require "rexml/document" module REXMLTests class TestParseDocumentTypeDeclaration < Test::Unit::TestCase private - def xml(internal_subset) - <<-XML -<!DOCTYPE r SYSTEM "urn:x-rexml:test" [ -#{internal_subset} -]> + def parse(doctype) + REXML::Document.new(<<-XML).doctype +#{doctype} <r/> XML end - def parse(internal_subset) - REXML::Document.new(xml(internal_subset)).doctype + class TestName < self + def test_valid + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r> + DOCTYPE + assert_equal("r", doctype.name) + end + + def test_garbage_plus_before_name_at_line_start + exception = assert_raise(REXML::ParseException) do + parse(<<-DOCTYPE) +<!DOCTYPE + +r SYSTEM "urn:x-rexml:test" [ +]> + DOCTYPE + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed DOCTYPE: invalid name +Line: 5 +Position: 51 +Last 80 unconsumed characters: ++ r SYSTEM "urn:x-rexml:test" [ ]> <r/> + DETAIL + end + end + + class TestExternalID < self + class TestSystem < self + def test_left_bracket_in_system_literal + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r SYSTEM "urn:x-rexml:[test" [ +]> + DOCTYPE + assert_equal([ + "r", + "SYSTEM", + nil, + "urn:x-rexml:[test", + ], + [ + doctype.name, + doctype.external_id, + doctype.public, + doctype.system, + ]) + end + + def test_greater_than_in_system_literal + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r SYSTEM "urn:x-rexml:>test" [ +]> + DOCTYPE + assert_equal([ + "r", + "SYSTEM", + nil, + "urn:x-rexml:>test", + ], + [ + doctype.name, + doctype.external_id, + doctype.public, + doctype.system, + ]) + end + + def test_no_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-DOCTYPE) +<!DOCTYPE r SYSTEM> + DOCTYPE + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed DOCTYPE: system literal is missing +Line: 3 +Position: 26 +Last 80 unconsumed characters: + SYSTEM> <r/> + DETAIL + end + + def test_garbage_after_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-DOCTYPE) +<!DOCTYPE r SYSTEM 'r.dtd'x'> + DOCTYPE + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed DOCTYPE: garbage after external ID +Line: 3 +Position: 36 +Last 80 unconsumed characters: +x'> <r/> + DETAIL + end + + def test_single_quote + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r SYSTEM 'r".dtd'> + DOCTYPE + assert_equal("r\".dtd", doctype.system) + end + + def test_double_quote + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r SYSTEM "r'.dtd"> + DOCTYPE + assert_equal("r'.dtd", doctype.system) + end + end + + class TestPublic < self + class TestPublicIDLiteral < self + def test_content_double_quote + exception = assert_raise(REXML::ParseException) do + parse(<<-DOCTYPE) +<!DOCTYPE r PUBLIC 'double quote " is invalid' "r.dtd"> + DOCTYPE + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed DOCTYPE: invalid public ID literal +Line: 3 +Position: 62 +Last 80 unconsumed characters: + PUBLIC 'double quote " is invalid' "r.dtd"> <r/> + DETAIL + end + + def test_single_quote + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r PUBLIC 'public-id-literal' "r.dtd"> + DOCTYPE + assert_equal("public-id-literal", doctype.public) + end + + def test_double_quote + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r PUBLIC "public'-id-literal" "r.dtd"> + DOCTYPE + assert_equal("public'-id-literal", doctype.public) + end + end + + class TestSystemLiteral < self + def test_garbage_after_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-DOCTYPE) +<!DOCTYPE r PUBLIC 'public-id-literal' 'system-literal'x'> + DOCTYPE + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed DOCTYPE: garbage after external ID +Line: 3 +Position: 65 +Last 80 unconsumed characters: +x'> <r/> + DETAIL + end + + def test_single_quote + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r PUBLIC "public-id-literal" 'system"-literal'> + DOCTYPE + assert_equal("system\"-literal", doctype.system) + end + + def test_double_quote + doctype = parse(<<-DOCTYPE) +<!DOCTYPE r PUBLIC "public-id-literal" "system'-literal"> + DOCTYPE + assert_equal("system'-literal", doctype.system) + end + end + end end class TestMixed < self @@ -45,6 +215,15 @@ module REXMLTests assert_equal([REXML::NotationDecl, REXML::AttlistDecl], doctype.children.collect(&:class)) end + + private + def parse(internal_subset) + super(<<-DOCTYPE) +<!DOCTYPE r SYSTEM "urn:x-rexml:test" [ +#{internal_subset} +]> + DOCTYPE + end end end end diff --git a/test/rexml/parse/test_element.rb b/test/rexml/parse/test_element.rb index aad915fe7b..7206fe595a 100644 --- a/test/rexml/parse/test_element.rb +++ b/test/rexml/parse/test_element.rb @@ -33,6 +33,32 @@ Last 80 unconsumed characters: DETAIL end + + def test_garbage_less_than_before_root_element_at_line_start + exception = assert_raise(REXML::ParseException) do + parse("<\n<x/>") + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +malformed XML: missing tag start +Line: 2 +Position: 6 +Last 80 unconsumed characters: +< <x/> + DETAIL + end + + def test_garbage_less_than_slash_before_end_tag_at_line_start + exception = assert_raise(REXML::ParseException) do + parse("<x></\n</x>") + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Missing end tag for 'x' +Line: 2 +Position: 10 +Last 80 unconsumed characters: +</ </x> + DETAIL + end end end end diff --git a/test/rexml/parse/test_notation_declaration.rb b/test/rexml/parse/test_notation_declaration.rb index 0d29f0d81f..19a0536d0a 100644 --- a/test/rexml/parse/test_notation_declaration.rb +++ b/test/rexml/parse/test_notation_declaration.rb @@ -23,10 +23,100 @@ module REXMLTests doctype = parse("<!NOTATION name PUBLIC 'urn:public-id'>") assert_equal("name", doctype.notation("name").name) end + + def test_no_name + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: name is missing +Line: 5 +Position: 72 +Last 80 unconsumed characters: + <!NOTATION> ]> <r/> + DETAIL + end + + def test_invalid_name + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION '> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: invalid name +Line: 5 +Position: 74 +Last 80 unconsumed characters: +'> ]> <r/> + DETAIL + end + + def test_no_id_type + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: invalid ID type +Line: 5 +Position: 77 +Last 80 unconsumed characters: +> ]> <r/> + DETAIL + end + + def test_invalid_id_type + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name INVALID> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: invalid ID type +Line: 5 +Position: 85 +Last 80 unconsumed characters: + INVALID> ]> <r/> + DETAIL + end end class TestExternalID < self class TestSystem < self + def test_no_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name SYSTEM> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: system literal is missing +Line: 5 +Position: 84 +Last 80 unconsumed characters: + SYSTEM> ]> <r/> + DETAIL + end + + def test_garbage_after_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name SYSTEM 'system-literal'x'> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: garbage before end > +Line: 5 +Position: 103 +Last 80 unconsumed characters: +x'> ]> <r/> + DETAIL + end + def test_single_quote doctype = parse(<<-INTERNAL_SUBSET) <!NOTATION name SYSTEM 'system-literal'> @@ -44,6 +134,21 @@ module REXMLTests class TestPublic < self class TestPublicIDLiteral < self + def test_content_double_quote + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name PUBLIC 'double quote " is invalid' "system-literal"> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: invalid public ID literal +Line: 5 +Position: 129 +Last 80 unconsumed characters: + PUBLIC 'double quote " is invalid' "system-literal"> ]> <r/> + DETAIL + end + def test_single_quote doctype = parse(<<-INTERNAL_SUBSET) <!NOTATION name PUBLIC 'public-id-literal' "system-literal"> @@ -60,6 +165,21 @@ module REXMLTests end class TestSystemLiteral < self + def test_garbage_after_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name PUBLIC 'public-id-literal' 'system-literal'x'> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: garbage before end > +Line: 5 +Position: 123 +Last 80 unconsumed characters: +x'> ]> <r/> + DETAIL + end + def test_single_quote doctype = parse(<<-INTERNAL_SUBSET) <!NOTATION name PUBLIC "public-id-literal" 'system-literal'> @@ -96,5 +216,66 @@ module REXMLTests end end end + + class TestPublicID < self + def test_no_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name PUBLIC> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: public ID literal is missing +Line: 5 +Position: 84 +Last 80 unconsumed characters: + PUBLIC> ]> <r/> + DETAIL + end + + def test_literal_content_double_quote + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name PUBLIC 'double quote " is invalid in PubidLiteral'> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: invalid public ID literal +Line: 5 +Position: 128 +Last 80 unconsumed characters: + PUBLIC 'double quote \" is invalid in PubidLiteral'> ]> <r/> + DETAIL + end + + def test_garbage_after_literal + exception = assert_raise(REXML::ParseException) do + parse(<<-INTERNAL_SUBSET) +<!NOTATION name PUBLIC 'public-id-literal'x'> + INTERNAL_SUBSET + end + assert_equal(<<-DETAIL.chomp, exception.to_s) +Malformed notation declaration: garbage before end > +Line: 5 +Position: 106 +Last 80 unconsumed characters: +x'> ]> <r/> + DETAIL + end + + def test_literal_single_quote + doctype = parse(<<-INTERNAL_SUBSET) +<!NOTATION name PUBLIC 'public-id-literal'> + INTERNAL_SUBSET + assert_equal("public-id-literal", doctype.notation("name").public) + end + + def test_literal_double_quote + doctype = parse(<<-INTERNAL_SUBSET) +<!NOTATION name PUBLIC "public-id-literal"> + INTERNAL_SUBSET + assert_equal("public-id-literal", doctype.notation("name").public) + end + end end end diff --git a/test/rexml/parse/test_processing_instruction.rb b/test/rexml/parse/test_processing_instruction.rb index a23513fc6e..f0c0c24e67 100644 --- a/test/rexml/parse/test_processing_instruction.rb +++ b/test/rexml/parse/test_processing_instruction.rb @@ -20,6 +20,25 @@ Last 80 unconsumed characters: <??> DETAIL end + + def test_garbage_text + # TODO: This should be parse error. + # Create test/parse/test_document.rb or something and move this to it. + doc = parse(<<-XML) +x<?x y +<!--?><?x -->?> +<r/> + XML + pi = doc.children[1] + assert_equal([ + "x", + "y\n<!--", + ], + [ + pi.target, + pi.content, + ]) + end end end end diff --git a/test/rexml/parser/test_ultra_light.rb b/test/rexml/parser/test_ultra_light.rb index 8f4a3980d5..44fd1d1ec0 100644 --- a/test/rexml/parser/test_ultra_light.rb +++ b/test/rexml/parser/test_ultra_light.rb @@ -16,7 +16,6 @@ class TestUltraLightParser < Test::Unit::TestCase nil, [:entitydecl, "name", "value"] ], - [:text, "\n"], [:start_element, :parent, "root", {}], [:text, "\n"], ], diff --git a/test/rexml/test_core.rb b/test/rexml/test_core.rb index 46036d7f12..ae528d75d3 100644 --- a/test/rexml/test_core.rb +++ b/test/rexml/test_core.rb @@ -1,4 +1,4 @@ -# coding: binary +# coding: utf-8 # frozen_string_literal: false require_relative "rexml_test_utils" @@ -995,7 +995,7 @@ EOL document.write(s) ## XML Doctype - str = '<!DOCTYPE foo "bar">' + str = '<!DOCTYPE foo SYSTEM "bar">' source = REXML::Source.new(str) doctype = REXML::DocType.new(source) document.add(doctype) diff --git a/test/rexml/test_doctype.rb b/test/rexml/test_doctype.rb index 7f42669170..915717de8e 100644 --- a/test/rexml/test_doctype.rb +++ b/test/rexml/test_doctype.rb @@ -18,12 +18,6 @@ module REXMLTests @doc_type_system = REXML::Document.new(xml_system).doctype @pubid = "TEST_ID" - xml_public = <<-XML - <!DOCTYPE root PUBLIC "#{@pubid}"> - <root/> - XML - @doc_type_public = REXML::Document.new(xml_public).doctype - xml_public_system = <<-XML <!DOCTYPE root PUBLIC "#{@pubid}" "#{@sysid}"> <root/> @@ -35,11 +29,9 @@ module REXMLTests assert_equal([ nil, @pubid, - @pubid, ], [ @doc_type_system.public, - @doc_type_public.public, @doc_type_public_system.public, ]) end @@ -58,12 +50,10 @@ module REXMLTests def test_system assert_equal([ @sysid, - nil, @sysid, ], [ @doc_type_system.system, - @doc_type_public.system, @doc_type_public_system.system, ]) end diff --git a/test/ripper/test_parser_events.rb b/test/ripper/test_parser_events.rb index 95ec661fcf..1f2f960020 100644 --- a/test/ripper/test_parser_events.rb +++ b/test/ripper/test_parser_events.rb @@ -1498,4 +1498,10 @@ class TestRipper::ParserEvents < Test::Unit::TestCase assert_warn("") {fmt, = warn("\r;")} assert_match(/encountered/, fmt) end + + def test_warn_mismatched_indentations + fmt, tokend, tokbeg, line = assert_warning("") {break warn("if true\n end\n")} + assert_match(/mismatched indentations/, fmt) + assert_equal(["if", "end", 1], [tokbeg, tokend, line]) + end end if ripper_test diff --git a/test/ruby/test_arithmetic_sequence.rb b/test/ruby/test_arithmetic_sequence.rb index 88fd3136cc..143906f4e2 100644 --- a/test/ruby/test_arithmetic_sequence.rb +++ b/test/ruby/test_arithmetic_sequence.rb @@ -254,6 +254,11 @@ class TestArithmeticSequence < Test::Unit::TestCase '[ruby-core:90648] [Bug #15444]') end + def test_last_bug17218 + seq = (1.0997r .. 1.1r).step(0.0001r) + assert_equal([1.0997r, 1.0998r, 1.0999r, 1.1r], seq.to_a, '[ruby-core:100312] [Bug #17218]') + end + def test_slice seq = 1.step(10, 2) assert_equal([[1, 3, 5], [7, 9]], seq.each_slice(3).to_a) diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb index 1a897ef502..3ba67e69a2 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -269,4 +269,15 @@ class TestAst < Test::Unit::TestCase assert_equal(:LIT, body.type) assert_equal([1], body.children) end + + def test_op_asgn2 + node = RubyVM::AbstractSyntaxTree.parse("struct.field += foo") + _, _, body = *node.children + assert_equal(:OP_ASGN2, body.type) + recv, _, mid, op, value = body.children + assert_equal(:VCALL, recv.type) + assert_equal(:field, mid) + assert_equal(:+, op) + assert_equal(:VCALL, value.type) + end end diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index ad2caeb8be..2ab1e7f266 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -232,6 +232,11 @@ class TestClass < Test::Unit::TestCase assert_raise(TypeError) { Class.allocate.superclass } bug6863 = '[ruby-core:47148]' assert_raise(TypeError, bug6863) { Class.new(Class.allocate) } + + allocator = Class.instance_method(:allocate) + assert_raise_with_message(TypeError, /prohibited/) { + allocator.bind(Rational).call + } end def test_nonascii_name @@ -427,6 +432,53 @@ class TestClass < Test::Unit::TestCase assert_equal(:foo, d.foo) end + def test_clone_singleton_class_exists + klass = Class.new do + def self.bar; :bar; end + end + + o = klass.new + o.singleton_class + clone = o.clone + + assert_empty(o.singleton_class.instance_methods(false)) + assert_empty(clone.singleton_class.instance_methods(false)) + assert_empty(o.singleton_class.singleton_class.instance_methods(false)) + assert_empty(clone.singleton_class.singleton_class.instance_methods(false)) + end + + def test_clone_when_singleton_class_of_singleton_class_exists + klass = Class.new do + def self.bar; :bar; end + end + + o = klass.new + o.singleton_class.singleton_class + clone = o.clone + + assert_empty(o.singleton_class.instance_methods(false)) + assert_empty(clone.singleton_class.instance_methods(false)) + assert_empty(o.singleton_class.singleton_class.instance_methods(false)) + assert_empty(clone.singleton_class.singleton_class.instance_methods(false)) + end + + def test_clone_when_method_exists_on_singleton_class_of_singleton_class + klass = Class.new do + def self.bar; :bar; end + end + + o = klass.new + o.singleton_class.singleton_class.define_method(:s2_method) { :s2 } + clone = o.clone + + assert_empty(o.singleton_class.instance_methods(false)) + assert_empty(clone.singleton_class.instance_methods(false)) + assert_equal(:s2, o.singleton_class.s2_method) + assert_equal(:s2, clone.singleton_class.s2_method) + assert_equal([:s2_method], o.singleton_class.singleton_class.instance_methods(false)) + assert_equal([:s2_method], clone.singleton_class.singleton_class.instance_methods(false)) + end + def test_singleton_class_p feature7609 = '[ruby-core:51087] [Feature #7609]' assert_predicate(self.singleton_class, :singleton_class?, feature7609) diff --git a/test/ruby/test_defined.rb b/test/ruby/test_defined.rb index 9976db3b6f..e1571d5714 100644 --- a/test/ruby/test_defined.rb +++ b/test/ruby/test_defined.rb @@ -23,40 +23,80 @@ class TestDefined < Test::Unit::TestCase return !defined?(yield) end - def test_defined + def test_defined_global_variable $x = nil assert(defined?($x)) # global variable assert_equal('global-variable', defined?($x))# returns description + end + def test_defined_local_variable assert_nil(defined?(foo)) # undefined foo=5 assert(defined?(foo)) # local variable + end + def test_defined_constant assert(defined?(Array)) # constant assert(defined?(::Array)) # toplevel constant assert(defined?(File::Constants)) # nested constant + end + + def test_defined_public_method assert(defined?(Object.new)) # method assert(defined?(Object::new)) # method + end + + def test_defined_private_method assert(!defined?(Object.print)) # private method + end + + def test_defined_operator assert(defined?(1 == 2)) # operator expression + end + def test_defined_protected_method f = Foo.new assert_nil(defined?(f.foo)) # protected method f.bar(f) { |v| assert(v) } + f.bar(Class.new(Foo).new) { |v| assert(v, "inherited protected method") } + end + + def test_defined_undefined_method + f = Foo.new assert_nil(defined?(f.quux)) # undefined method + end + + def test_defined_undefined_argument + f = Foo.new assert_nil(defined?(f.baz(x))) # undefined argument x = 0 assert(defined?(f.baz(x))) assert_nil(defined?(f.quux(x))) assert(defined?(print(x))) assert_nil(defined?(quux(x))) + end + + def test_defined_attrasgn + f = Foo.new assert(defined?(f.attr = 1)) f.attrasgn_test { |v| assert(v) } + end + + def test_defined_undef + x = Object.new + def x.foo; end + assert(defined?(x.foo)) + x.instance_eval {undef :foo} + assert(!defined?(x.foo), "undefed method should not be defined?") + end + def test_defined_yield assert(defined_test) # not iterator assert(!defined_test{}) # called as iterator + end + def test_defined_matchdata /a/ =~ '' assert_equal nil, defined?($&) assert_equal nil, defined?($`) @@ -85,12 +125,16 @@ class TestDefined < Test::Unit::TestCase assert_equal 'global-variable', defined?($+) assert_equal 'global-variable', defined?($1) assert_equal nil, defined?($2) + end + def test_defined_literal assert_equal("nil", defined?(nil)) assert_equal("true", defined?(true)) assert_equal("false", defined?(false)) assert_equal("expression", defined?(1)) + end + def test_defined_empty_paren_expr bug8224 = '[ruby-core:54024] [Bug #8224]' (1..3).each do |level| expr = "("*level+")"*level diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index 6343642ac1..e9fb2524f6 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -440,6 +440,8 @@ class TestEnv < Test::Unit::TestCase ENV["foo"] = "xxx" ENV.replace({"foo"=>"bar", "baz"=>"qux"}) check(ENV.to_hash.to_a, [%w(foo bar), %w(baz qux)]) + ENV.replace({"Foo"=>"Bar", "Baz"=>"Qux"}) + check(ENV.to_hash.to_a, [%w(Foo Bar), %w(Baz Qux)]) end def test_update diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index 05c74f6dcd..de249c9b7c 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -842,6 +842,26 @@ end.join } end + def test_cause_exception_in_cause_message + assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}") do |outs, errs, status| + begin; + exc = Class.new(StandardError) do + def initialize(obj, cnt) + super(obj) + @errcnt = cnt + end + def to_s + return super if @errcnt <= 0 + @errcnt -= 1 + raise "xxx" + end + end.new("ok", 10) + raise "[Bug #17033]", cause: exc + end; + assert_equal(1, errs.count {|m| m.include?("[Bug #17033]")}, proc {errs.pretty_inspect}) + end + end + def test_anonymous_message assert_in_out_err([], "raise Class.new(RuntimeError), 'foo'", [], /foo\n/) end diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 3453440694..0b2e4df05b 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -171,6 +171,24 @@ class TestFloat < Test::Unit::TestCase assert_raise(ArgumentError, n += z + "A") {Float(n)} assert_raise(ArgumentError, n += z + ".0") {Float(n)} end + + x = nil + 2000.times do + x = Float("0x"+"0"*30) + break unless x == 0.0 + end + assert_equal(0.0, x, ->{"%a" % x}) + x = nil + 2000.times do + begin + x = Float("0x1."+"0"*270) + rescue ArgumentError => e + raise unless /"0x1\.0{270}"/ =~ e.message + else + break + end + end + assert_nil(x, ->{"%a" % x}) end def test_divmod diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 69347b6b11..0f289d8ed9 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -595,6 +595,9 @@ class TestInteger < Test::Unit::TestCase failures << n unless root*root <= n && (root+1)*(root+1) > n end assert_empty(failures, bug13440) + + x = 0xffff_ffff_ffff_ffff + assert_equal(x, Integer.sqrt(x ** 2), "[ruby-core:95453]") end def test_fdiv diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index 84a5cfec66..8e2da53bdf 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -520,12 +520,6 @@ class TestKeywordArguments < Test::Unit::TestCase assert_equal(:ok, m.f(*a, **o), '[ruby-core:83638] [Bug #10856]') o = {a: 42} - assert_warning(/splat keyword/, 'splat to mandatory') do - assert_equal({a: 42}, m.f1(**o)) - end - assert_warning(/splat keyword/) do - assert_equal({a: 42}, m.f2(**o), '[ruby-core:82280] [Bug #13791]') - end assert_warning('', 'splat to kwrest') do assert_equal({a: 42}, m.f3(**o)) end diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index 3a7186584a..3bf25928c9 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -458,6 +458,14 @@ class TestMethod < Test::Unit::TestCase m.taint assert_predicate(m.inspect, :tainted?, "inspect result should be infected") + + bug15608 = '[ruby-core:91570] [Bug #15608]' + c4 = Class.new(c) + c4.class_eval { alias bar foo } + o = c4.new + o.singleton_class + m4 = o.method(:bar) + assert_equal("#<Method: #{c4.inspect}(#{c.inspect})#bar(foo)>", m4.inspect, bug15608) end def test_callee_top_level @@ -950,6 +958,99 @@ class TestMethod < Test::Unit::TestCase '[ruby-core:85231] [Bug #14421]' end + def test_super_method_alias + c0 = Class.new do + def m1 + [:C0_m1] + end + def m2 + [:C0_m2] + end + end + + c1 = Class.new(c0) do + def m1 + [:C1_m1] + super + end + alias m2 m1 + end + + c2 = Class.new(c1) do + def m2 + [:C2_m2] + super + end + end + o1 = c2.new + assert_equal([:C2_m2, :C1_m1, :C0_m1], o1.m2) + + m = o1.method(:m2) + assert_equal([:C2_m2, :C1_m1, :C0_m1], m.call) + + m = m.super_method + assert_equal([:C1_m1, :C0_m1], m.call) + + m = m.super_method + assert_equal([:C0_m1], m.call) + + assert_nil(m.super_method) + end + + def test_super_method_alias_to_prepended_module + m = Module.new do + def m1 + [:P_m1] + super + end + + def m2 + [:P_m2] + super + end + end + + c0 = Class.new do + def m1 + [:C0_m1] + end + end + + c1 = Class.new(c0) do + def m1 + [:C1_m1] + super + end + prepend m + alias m2 m1 + end + + o1 = c1.new + assert_equal([:P_m2, :P_m1, :C1_m1, :C0_m1], o1.m2) + + m = o1.method(:m2) + assert_equal([:P_m2, :P_m1, :C1_m1, :C0_m1], m.call) + + m = m.super_method + assert_equal([:P_m1, :C1_m1, :C0_m1], m.call) + + m = m.super_method + assert_equal([:C1_m1, :C0_m1], m.call) + + m = m.super_method + assert_equal([:C0_m1], m.call) + + assert_nil(m.super_method) + end + + # Bug 17780 + def test_super_method_module_alias + m = Module.new do + def foo + end + alias :f :foo + end + + method = m.instance_method(:f) + super_method = method.super_method + assert_nil(super_method) + end + def rest_parameter(*rest) rest end diff --git a/test/ruby/test_notimp.rb b/test/ruby/test_notimp.rb index ddebb657bf..daa5a82d7b 100644 --- a/test/ruby/test_notimp.rb +++ b/test/ruby/test_notimp.rb @@ -13,11 +13,11 @@ class TestNotImplement < Test::Unit::TestCase def test_respond_to_lchmod assert_include(File.methods, :lchmod) - if /linux/ =~ RUBY_PLATFORM - assert_equal(false, File.respond_to?(:lchmod)) - end - if /freebsd/ =~ RUBY_PLATFORM + case RUBY_PLATFORM + when /freebsd/, /linux-musl/ assert_equal(true, File.respond_to?(:lchmod)) + when /linux/ + assert_equal(false, File.respond_to?(:lchmod)) end end @@ -57,9 +57,14 @@ class TestNotImplement < Test::Unit::TestCase File.open(f, "w") {} File.symlink f, g newmode = 0444 - File.lchmod newmode, "#{d}/g" - snew = File.lstat(g) - assert_equal(newmode, snew.mode & 0777) + begin + File.lchmod newmode, "#{d}/g" + rescue Errno::EOPNOTSUPP + skip $! + else + snew = File.lstat(g) + assert_equal(newmode, snew.mode & 0777) + end } end end diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index a8cdf22a49..2957e1bfc8 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -598,6 +598,13 @@ class Rational_Test < Test::Unit::TestCase assert_nothing_raised(TypeError, '[Bug #5020] [ruby-dev:44088]') do Rational(1,2).coerce(Complex(1,1)) end + + assert_raise(ZeroDivisionError) do + 1 / 0r.coerce(0+0i)[0] + end + assert_raise(ZeroDivisionError) do + 1 / 0r.coerce(0.0+0i)[0] + end end class ObjectX diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 38147c7446..038c1c830f 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -218,6 +218,17 @@ class TestRegexp < Test::Unit::TestCase def test_assign_named_capture_to_reserved_word /(?<nil>.)/ =~ "a" assert_not_include(local_variables, :nil, "[ruby-dev:32675]") + + def (obj = Object.new).test(s, nil: :ng) + /(?<nil>.)/ =~ s + binding.local_variable_get(:nil) + end + assert_equal("b", obj.test("b")) + + tap do |nil: :ng| + /(?<nil>.)/ =~ "c" + assert_equal("c", binding.local_variable_get(:nil)) + end end def test_assign_named_capture_to_const diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 4e403a9ffd..ab462bddf4 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1265,6 +1265,15 @@ eom assert_nil obj.test end + def test_assignment_return_in_loop + obj = Object.new + def obj.test + x = nil + _y = (return until x unless x) + end + assert_nil obj.test, "[Bug #16695]" + end + def test_method_call_location line = __LINE__+5 e = assert_raise(NoMethodError) do @@ -1306,6 +1315,12 @@ eom assert_valid_syntax('obj::foo (1) {}') end + def test_value_expr_in_condition + mesg = /void value expression/ + assert_syntax_error("tap {a = (true ? next : break)}", mesg) + assert_valid_syntax("tap {a = (true ? true : break)}") + end + private def not_label(x) @result = x; @not_label ||= nil end diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index 2bd4bc8455..3b3711b805 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -851,6 +851,13 @@ class TestTime < Test::Unit::TestCase assert_equal(8192, Time.now.strftime('%8192z').size) end + def test_strftime_wide_precision + t2000 = get_t2000 + s = t2000.strftime("%28c") + assert_equal(28, s.size) + assert_equal(t2000.strftime("%c"), s.strip) + end + def test_strfimte_zoneoffset t2000 = get_t2000 t = t2000.getlocal("+09:00:00") @@ -1101,6 +1108,14 @@ class TestTime < Test::Unit::TestCase } end + def test_getlocal_utc_offset + t = Time.gm(2000) + assert_equal [00, 30, 21, 31, 12, 1999], t.getlocal("-02:30").to_a[0, 6] + assert_equal [00, 00, 9, 1, 1, 2000], t.getlocal("+09:00").to_a[0, 6] + assert_equal [20, 29, 21, 31, 12, 1999], t.getlocal("-02:30:40").to_a[0, 6] + assert_equal [35, 10, 9, 1, 1, 2000], t.getlocal("+09:10:35").to_a[0, 6] + end + def test_getlocal_nil now = Time.now now2 = nil diff --git a/test/ruby/test_time_tz.rb b/test/ruby/test_time_tz.rb index 21ac667281..9bba30e577 100644 --- a/test/ruby/test_time_tz.rb +++ b/test/ruby/test_time_tz.rb @@ -548,6 +548,8 @@ module TestTimeTZ::WithTZ m, s = (4000-utc_offset).divmod(60) h, m = m.divmod(60) assert_equal(time_class.utc(2018, 9, 1, 12+h, m, s), t) + assert_equal(6, t.wday) + assert_equal(244, t.yday) end def subtest_at(time_class, tz, tzarg, tzname, abbr, utc_offset) @@ -657,6 +659,16 @@ else @tz ||= TZInfo::Timezone.get(tzname) end end + + def test_fractional_second + x = Object.new + def x.local_to_utc(t); t + 8*3600; end + def x.utc_to_local(t); t - 8*3600; end + + t1 = Time.new(2020,11,11,12,13,14.124r, '-08:00') + t2 = Time.new(2020,11,11,12,13,14.124r, x) + assert_equal(t1, t2) + end end begin diff --git a/test/ruby/test_transcode.rb b/test/ruby/test_transcode.rb index 44d238ffd2..7f81cbf424 100644 --- a/test/ruby/test_transcode.rb +++ b/test/ruby/test_transcode.rb @@ -2161,6 +2161,14 @@ class TestTranscode < Test::Unit::TestCase assert_equal("U+3042", "\u{3042}".encode("US-ASCII", fallback: fallback.method(:escape))) end + def test_fallback_aref + fallback = Object.new + def fallback.[](x) + "U+%.4X" % x.unpack("U") + end + assert_equal("U+3042", "\u{3042}".encode("US-ASCII", fallback: fallback)) + end + bug8940 = '[ruby-core:57318] [Bug #8940]' %w[UTF-32 UTF-16].each do |enc| define_method("test_pseudo_encoding_inspect(#{enc})") do diff --git a/test/rubygems/test_bundled_ca.rb b/test/rubygems/test_bundled_ca.rb index 97a64af323..3f543d4acf 100644 --- a/test/rubygems/test_bundled_ca.rb +++ b/test/rubygems/test_bundled_ca.rb @@ -51,13 +51,16 @@ if ENV["CI"] || ENV["TEST_SSL"] assert_https('rubygems.org') end - def test_accessing_fastly - assert_https('rubygems.global.ssl.fastly.net') + def test_accessing_www_rubygems + assert_https('www.rubygems.org') end - def test_accessing_new_index - assert_https('fastly.rubygems.org') + def test_accessing_staging + assert_https('staging.rubygems.org') end + def test_accessing_new_index + assert_https('index.rubygems.org') + end end end diff --git a/test/test_tmpdir.rb b/test/test_tmpdir.rb index 1e633d233b..bede8c0ab4 100644 --- a/test/test_tmpdir.rb +++ b/test/test_tmpdir.rb @@ -65,22 +65,40 @@ class TestTmpdir < Test::Unit::TestCase } end - TRAVERSAL_PATH = Array.new(Dir.pwd.split('/').count, '..').join('/') + Dir.pwd + '/' - TRAVERSAL_PATH.delete!(':') if /mswin|mingw/ =~ RUBY_PLATFORM + def test_mktmpdir_mutate + bug16918 = '[ruby-core:98563]' + assert_nothing_raised(bug16918) do + assert_mktmpdir_traversal do |traversal_path| + Dir.mktmpdir(traversal_path + 'foo') do |actual| + actual << "foo" + end + end + end + end def test_mktmpdir_traversal - expect = Dir.glob(TRAVERSAL_PATH + '*').count - Dir.mktmpdir(TRAVERSAL_PATH + 'foo') do - actual = Dir.glob(TRAVERSAL_PATH + '*').count - assert_equal expect, actual + assert_mktmpdir_traversal do |traversal_path| + Dir.mktmpdir(traversal_path + 'foo') do |actual| + actual + end end end def test_mktmpdir_traversal_array - expect = Dir.glob(TRAVERSAL_PATH + '*').count - Dir.mktmpdir([TRAVERSAL_PATH, 'foo']) do - actual = Dir.glob(TRAVERSAL_PATH + '*').count - assert_equal expect, actual + assert_mktmpdir_traversal do |traversal_path| + Dir.mktmpdir([traversal_path, 'foo']) do |actual| + actual + end + end + end + + def assert_mktmpdir_traversal + Dir.mktmpdir do |target| + target = target.chomp('/') + '/' + traversal_path = target.sub(/\A\w:/, '') # for DOSISH + traversal_path = Array.new(target.count('/')-2, '..').join('/') + traversal_path + actual = yield traversal_path + assert_not_send([File.absolute_path(actual), :start_with?, target]) end end end diff --git a/test/uri/test_ldap.rb b/test/uri/test_ldap.rb index adad4454b5..64845e487a 100644 --- a/test/uri/test_ldap.rb +++ b/test/uri/test_ldap.rb @@ -95,6 +95,10 @@ class TestLDAP < Test::Unit::TestCase u.select(:scheme, :host, :not_exist, :port) end end + + def test_parse_invalid_uri + assert_raise(URI::InvalidURIError) {URI.parse("ldap:https://example.com")} + end end diff --git a/test/webrick/test_httpproxy.rb b/test/webrick/test_httpproxy.rb index a9f6f7d610..504eb1f915 100644 --- a/test/webrick/test_httpproxy.rb +++ b/test/webrick/test_httpproxy.rb @@ -213,7 +213,7 @@ class TestWEBrickHTTPProxy < Test::Unit::TestCase end end end - end + end if RUBY_VERSION >= '2.5' def make_certificate(key, cn) subject = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=#{cn}") diff --git a/test/webrick/test_httpserver.rb b/test/webrick/test_httpserver.rb index a6e70da7e8..2e5d44940c 100644 --- a/test/webrick/test_httpserver.rb +++ b/test/webrick/test_httpserver.rb @@ -253,7 +253,7 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase server.virtual_host(WEBrick::HTTPServer.new(vhost_config)) Thread.pass while server.status != :Running - sleep 1 if RubyVM::MJIT.enabled? # server.status behaves unexpectedly with --jit-wait + sleep 1 if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # server.status behaves unexpectedly with --jit-wait assert_equal(1, started, log.call) assert_equal(0, stopped, log.call) assert_equal(0, accepted, log.call) diff --git a/test/webrick/test_server.rb b/test/webrick/test_server.rb index 5f7f3a0b58..8162a186db 100644 --- a/test/webrick/test_server.rb +++ b/test/webrick/test_server.rb @@ -65,7 +65,7 @@ class TestWEBrickServer < Test::Unit::TestCase } TestWEBrick.start_server(Echo, config){|server, addr, port, log| true while server.status != :Running - sleep 1 if RubyVM::MJIT.enabled? # server.status behaves unexpectedly with --jit-wait + sleep 1 if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # server.status behaves unexpectedly with --jit-wait assert_equal(1, started, log.call) assert_equal(0, stopped, log.call) assert_equal(0, accepted, log.call) |