2008-06-08 08:51:25 -04:00
|
|
|
require 'test/unit'
|
2004-08-06 12:01:02 -04:00
|
|
|
require 'stringio'
|
2008-06-08 08:51:25 -04:00
|
|
|
require 'tempfile'
|
2004-04-05 09:27:45 -04:00
|
|
|
|
2004-04-05 22:59:13 -04:00
|
|
|
begin
|
|
|
|
require 'zlib'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2004-04-05 09:27:45 -04:00
|
|
|
|
2004-04-05 22:59:13 -04:00
|
|
|
if defined? Zlib
|
2008-06-08 08:51:25 -04:00
|
|
|
class TestZlibDeflate < Test::Unit::TestCase
|
|
|
|
def test_initialize
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
s = z.deflate("foo", Zlib::FINISH)
|
|
|
|
assert_equal("foo", Zlib::Inflate.inflate(s))
|
|
|
|
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
s = z.deflate("foo")
|
|
|
|
s << z.deflate(nil, Zlib::FINISH)
|
|
|
|
assert_equal("foo", Zlib::Inflate.inflate(s))
|
|
|
|
|
|
|
|
assert_raise(Zlib::StreamError) { Zlib::Deflate.new(10000) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dup
|
|
|
|
z1 = Zlib::Deflate.new
|
|
|
|
s = z1.deflate("foo")
|
|
|
|
z2 = z1.dup
|
|
|
|
s1 = s + z1.deflate("bar", Zlib::FINISH)
|
|
|
|
s2 = s + z2.deflate("baz", Zlib::FINISH)
|
|
|
|
assert_equal("foobar", Zlib::Inflate.inflate(s1))
|
|
|
|
assert_equal("foobaz", Zlib::Inflate.inflate(s2))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deflate
|
|
|
|
s = Zlib::Deflate.deflate("foo")
|
|
|
|
assert_equal("foo", Zlib::Inflate.inflate(s))
|
|
|
|
|
|
|
|
assert_raise(Zlib::StreamError) { Zlib::Deflate.deflate("foo", 10000) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_addstr
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z << "foo"
|
|
|
|
s = z.deflate(nil, Zlib::FINISH)
|
|
|
|
assert_equal("foo", Zlib::Inflate.inflate(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_flush
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z << "foo"
|
|
|
|
s = z.flush
|
|
|
|
z << "bar"
|
|
|
|
s << z.flush_next_in
|
|
|
|
z << "baz"
|
|
|
|
s << z.flush_next_out
|
|
|
|
s << z.deflate("qux", Zlib::FINISH)
|
|
|
|
assert_equal("foobarbazqux", Zlib::Inflate.inflate(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_avail
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
assert_equal(0, z.avail_in)
|
|
|
|
assert_equal(0, z.avail_out)
|
|
|
|
z << "foo"
|
|
|
|
z.avail_out += 100
|
|
|
|
z << "bar"
|
|
|
|
s = z.finish
|
|
|
|
assert_equal("foobar", Zlib::Inflate.inflate(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_total
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
1000.times { z << "foo" }
|
|
|
|
s = z.finish
|
|
|
|
assert_equal(3000, z.total_in)
|
|
|
|
assert_operator(3000, :>, z.total_out)
|
|
|
|
assert_equal("foo" * 1000, Zlib::Inflate.inflate(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_data_type
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
assert([Zlib::ASCII, Zlib::BINARY, Zlib::UNKNOWN].include?(z.data_type))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_adler
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z << "foo"
|
2011-02-03 10:15:04 -05:00
|
|
|
z.finish
|
2008-06-08 08:51:25 -04:00
|
|
|
assert_equal(0x02820145, z.adler)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_finished_p
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
assert_equal(false, z.finished?)
|
|
|
|
z << "foo"
|
|
|
|
assert_equal(false, z.finished?)
|
2011-02-03 10:15:04 -05:00
|
|
|
z.finish
|
2008-06-08 08:51:25 -04:00
|
|
|
assert_equal(true, z.finished?)
|
|
|
|
z.close
|
|
|
|
assert_raise(Zlib::Error) { z.finished? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_closed_p
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
assert_equal(false, z.closed?)
|
|
|
|
z << "foo"
|
|
|
|
assert_equal(false, z.closed?)
|
|
|
|
s = z.finish
|
|
|
|
assert_equal(false, z.closed?)
|
|
|
|
z.close
|
|
|
|
assert_equal(true, z.closed?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_params
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z << "foo"
|
|
|
|
z.params(Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
|
|
|
|
z << "bar"
|
|
|
|
s = z.finish
|
|
|
|
assert_equal("foobar", Zlib::Inflate.inflate(s))
|
|
|
|
|
2008-07-11 10:06:19 -04:00
|
|
|
data = ('a'..'z').to_a.join
|
|
|
|
z = Zlib::Deflate.new(Zlib::NO_COMPRESSION, Zlib::MAX_WBITS,
|
|
|
|
Zlib::DEF_MEM_LEVEL, Zlib::DEFAULT_STRATEGY)
|
|
|
|
z << data[0, 10]
|
|
|
|
z.params(Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY)
|
|
|
|
z << data[10 .. -1]
|
|
|
|
assert_equal(data, Zlib::Inflate.inflate(z.finish))
|
|
|
|
|
2008-06-08 08:51:25 -04:00
|
|
|
z = Zlib::Deflate.new
|
|
|
|
s = z.deflate("foo", Zlib::FULL_FLUSH)
|
|
|
|
z.avail_out = 0
|
|
|
|
z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
|
|
|
|
s << z.deflate("bar", Zlib::FULL_FLUSH)
|
|
|
|
z.avail_out = 0
|
|
|
|
z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
|
|
|
|
s << z.deflate("baz", Zlib::FINISH)
|
|
|
|
assert_equal("foobarbaz", Zlib::Inflate.inflate(s))
|
|
|
|
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
assert_raise(Zlib::StreamError) { z.params(10000, 10000) }
|
2008-07-22 21:09:50 -04:00
|
|
|
z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_set_dictionary
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z.set_dictionary("foo")
|
|
|
|
s = z.deflate("foo" * 100, Zlib::FINISH)
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
assert_raise(Zlib::NeedDict) { z.inflate(s) }
|
|
|
|
z.set_dictionary("foo")
|
|
|
|
assert_equal("foo" * 100, z.inflate(s)) # ???
|
|
|
|
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z << "foo"
|
|
|
|
assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
|
|
|
|
z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reset
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z << "foo"
|
|
|
|
z.reset
|
|
|
|
z << "bar"
|
|
|
|
s = z.finish
|
|
|
|
assert_equal("bar", Zlib::Inflate.inflate(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_close
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
z.close
|
|
|
|
assert_raise(Zlib::Error) { z << "foo" }
|
|
|
|
assert_raise(Zlib::Error) { z.reset }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestZlibInflate < Test::Unit::TestCase
|
|
|
|
def test_initialize
|
|
|
|
assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
|
|
|
|
|
|
|
|
s = Zlib::Deflate.deflate("foo")
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
z << s << nil
|
|
|
|
assert_equal("foo", z.finish)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inflate
|
|
|
|
s = Zlib::Deflate.deflate("foo")
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
s = z.inflate(s)
|
|
|
|
s << z.inflate(nil)
|
|
|
|
assert_equal("foo", s)
|
|
|
|
z.inflate("foo") # ???
|
|
|
|
z << "foo" # ???
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sync
|
|
|
|
z = Zlib::Deflate.new
|
|
|
|
s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
|
|
|
|
z.avail_out = 0
|
|
|
|
z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
|
|
|
|
s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
|
|
|
|
z.avail_out = 0
|
|
|
|
z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
|
|
|
|
s << z.deflate("baz" * 1000, Zlib::FINISH)
|
|
|
|
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
assert_raise(Zlib::DataError) { z << "\0" * 100 }
|
|
|
|
assert_equal(false, z.sync(""))
|
|
|
|
assert_equal(false, z.sync_point?)
|
|
|
|
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
assert_raise(Zlib::DataError) { z << "\0" * 100 + s }
|
|
|
|
assert_equal(true, z.sync(""))
|
|
|
|
#assert_equal(true, z.sync_point?)
|
|
|
|
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
assert_equal(false, z.sync("\0" * 100))
|
|
|
|
assert_equal(false, z.sync_point?)
|
|
|
|
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
assert_equal(true, z.sync("\0" * 100 + s))
|
|
|
|
#assert_equal(true, z.sync_point?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set_dictionary
|
|
|
|
z = Zlib::Inflate.new
|
|
|
|
assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
|
|
|
|
z.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestZlibGzipFile < Test::Unit::TestCase
|
|
|
|
def test_to_io
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_to_io")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
2010-12-14 21:20:37 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_kind_of(IO, f.to_io)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_crc
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_crc")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
f.read
|
|
|
|
assert_equal(0x8c736521, f.crc)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mtime
|
|
|
|
tim = Time.now
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_mtime")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) do |gz|
|
|
|
|
gz.mtime = -1
|
|
|
|
gz.mtime = tim
|
|
|
|
gz.print("foo")
|
|
|
|
gz.flush
|
|
|
|
assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
|
|
|
|
end
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal(tim.to_i, f.mtime.to_i)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_level
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_level")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_os_code
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_os_code")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal(Zlib::OS_CODE, f.os_code)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_orig_name
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_orig_name")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) do |gz|
|
|
|
|
gz.orig_name = "foobarbazqux\0quux"
|
|
|
|
gz.print("foo")
|
|
|
|
gz.flush
|
|
|
|
assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
|
|
|
|
end
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal("foobarbazqux", f.orig_name)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_comment
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_comment")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) do |gz|
|
|
|
|
gz.comment = "foobarbazqux\0quux"
|
|
|
|
gz.print("foo")
|
|
|
|
gz.flush
|
|
|
|
assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
|
|
|
|
end
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal("foobarbazqux", f.comment)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_lineno
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_lineno")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal([0, "foo\n"], [f.lineno, f.gets])
|
|
|
|
assert_equal([1, "bar\n"], [f.lineno, f.gets])
|
|
|
|
f.lineno = 1000
|
|
|
|
assert_equal([1000, "baz\n"], [f.lineno, f.gets])
|
|
|
|
assert_equal([1001, "qux\n"], [f.lineno, f.gets])
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_closed_p
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_closed_p")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal(false, f.closed?)
|
|
|
|
f.read
|
|
|
|
assert_equal(false, f.closed?)
|
|
|
|
f.close
|
|
|
|
assert_equal(true, f.closed?)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sync
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_sync")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
f.sync = true
|
|
|
|
assert_equal(true, f.sync)
|
|
|
|
f.read
|
|
|
|
f.sync = false
|
|
|
|
assert_equal(false, f.sync)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pos
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_pos")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) do |gz|
|
|
|
|
gz.print("foo")
|
|
|
|
gz.flush
|
|
|
|
assert_equal(3, gz.tell)
|
|
|
|
end
|
|
|
|
end
|
2009-04-22 12:31:06 -04:00
|
|
|
|
|
|
|
def test_path
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_file_path")
|
2009-04-22 12:31:06 -04:00
|
|
|
t.close
|
|
|
|
|
|
|
|
gz = Zlib::GzipWriter.open(t.path)
|
|
|
|
gz.print("foo")
|
|
|
|
assert_equal(t.path, gz.path)
|
|
|
|
gz.close
|
|
|
|
assert_equal(t.path, gz.path)
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal(t.path, f.path)
|
|
|
|
f.close
|
|
|
|
assert_equal(t.path, f.path)
|
|
|
|
end
|
2009-04-22 12:31:06 -04:00
|
|
|
|
|
|
|
s = ""
|
|
|
|
sio = StringIO.new(s)
|
|
|
|
gz = Zlib::GzipWriter.new(sio)
|
|
|
|
gz.print("foo")
|
|
|
|
assert_raise(NoMethodError) { gz.path }
|
|
|
|
gz.close
|
|
|
|
|
|
|
|
sio = StringIO.new(s)
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.new(sio) do |f|
|
|
|
|
assert_raise(NoMethodError) { f.path }
|
|
|
|
end
|
2009-04-22 12:31:06 -04:00
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
2004-08-06 12:01:02 -04:00
|
|
|
class TestZlibGzipReader < Test::Unit::TestCase
|
|
|
|
D0 = "\037\213\010\000S`\017A\000\003\003\000\000\000\000\000\000\000\000\000"
|
|
|
|
def test_read0
|
|
|
|
assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
|
|
|
|
end
|
2004-09-07 01:32:26 -04:00
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_ungetc
|
2004-09-07 01:32:26 -04:00
|
|
|
s = ""
|
|
|
|
w = Zlib::GzipWriter.new(StringIO.new(s))
|
|
|
|
w << (1...1000).to_a.inspect
|
|
|
|
w.close
|
|
|
|
r = Zlib::GzipReader.new(StringIO.new(s))
|
|
|
|
r.read(100)
|
|
|
|
r.ungetc ?a
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_nothing_raised("[ruby-dev:24060]") {
|
2004-09-07 01:32:26 -04:00
|
|
|
r.read(100)
|
|
|
|
r.read
|
|
|
|
r.close
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_ungetc_paragraph
|
2004-09-07 01:32:26 -04:00
|
|
|
s = ""
|
|
|
|
w = Zlib::GzipWriter.new(StringIO.new(s))
|
|
|
|
w << "abc"
|
|
|
|
w.close
|
|
|
|
r = Zlib::GzipReader.new(StringIO.new(s))
|
2004-09-07 03:11:06 -04:00
|
|
|
r.ungetc ?\n
|
|
|
|
assert_equal("abc", r.gets(""))
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_nothing_raised("[ruby-dev:24065]") {
|
2004-09-07 03:11:06 -04:00
|
|
|
r.read
|
|
|
|
r.close
|
|
|
|
}
|
2004-09-07 01:32:26 -04:00
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
|
|
|
|
def test_open
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_open")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
2010-07-06 22:44:04 -04:00
|
|
|
e = assert_raise(Zlib::GzipFile::Error) {
|
|
|
|
Zlib::GzipReader.open(t.path)
|
|
|
|
}
|
|
|
|
assert_equal("not in gzip format", e.message)
|
|
|
|
assert_nil(e.input)
|
|
|
|
open(t.path, "wb") {|f| f.write("foo")}
|
|
|
|
e = assert_raise(Zlib::GzipFile::Error) {
|
|
|
|
Zlib::GzipReader.open(t.path)
|
|
|
|
}
|
|
|
|
assert_equal("not in gzip format", e.message)
|
|
|
|
assert_equal("foo", e.input)
|
|
|
|
open(t.path, "wb") {|f| f.write("foobarzothoge")}
|
|
|
|
e = assert_raise(Zlib::GzipFile::Error) {
|
|
|
|
Zlib::GzipReader.open(t.path)
|
|
|
|
}
|
|
|
|
assert_equal("not in gzip format", e.message)
|
|
|
|
assert_equal("foobarzothoge", e.input)
|
|
|
|
|
2008-06-08 08:51:25 -04:00
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) { Zlib::GzipReader.open }
|
|
|
|
|
|
|
|
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
|
|
|
|
|
|
|
|
f = Zlib::GzipReader.open(t.path)
|
2010-12-13 22:57:26 -05:00
|
|
|
begin
|
|
|
|
assert_equal("foo", f.read)
|
|
|
|
ensure
|
|
|
|
f.close
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rewind
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_rewind")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal("foo", f.read)
|
|
|
|
f.rewind
|
|
|
|
assert_equal("foo", f.read)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unused
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_unused")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
2011-02-03 04:56:52 -05:00
|
|
|
assert_equal(nil, f.unused)
|
2010-12-13 22:57:26 -05:00
|
|
|
assert_equal("foo", f.read(3))
|
2011-02-03 04:56:52 -05:00
|
|
|
assert_equal(nil, f.unused)
|
2010-12-13 22:57:26 -05:00
|
|
|
assert_equal("bar", f.read)
|
2011-02-03 04:56:52 -05:00
|
|
|
assert_equal(nil, f.unused)
|
2010-12-13 22:57:26 -05:00
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
2011-02-03 04:56:52 -05:00
|
|
|
def test_unused2
|
|
|
|
zio = StringIO.new
|
|
|
|
|
|
|
|
io = Zlib::GzipWriter.new zio
|
|
|
|
io.write 'aaaa'
|
|
|
|
io.finish
|
|
|
|
|
|
|
|
io = Zlib::GzipWriter.new zio
|
|
|
|
io.write 'bbbb'
|
|
|
|
io.finish
|
|
|
|
|
|
|
|
zio.rewind
|
|
|
|
|
|
|
|
io = Zlib::GzipReader.new zio
|
|
|
|
assert_equal('aaaa', io.read)
|
|
|
|
unused = io.unused
|
|
|
|
assert_equal(24, unused.bytesize)
|
|
|
|
io.finish
|
|
|
|
|
|
|
|
zio.pos -= unused.length
|
|
|
|
|
|
|
|
io = Zlib::GzipReader.new zio
|
|
|
|
assert_equal('bbbb', io.read)
|
|
|
|
assert_equal(nil, io.unused)
|
|
|
|
io.finish
|
|
|
|
end
|
|
|
|
|
2008-06-08 08:51:25 -04:00
|
|
|
def test_read
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_read")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
2009-08-29 12:17:26 -04:00
|
|
|
str = "\u3042\u3044\u3046"
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
|
2008-06-08 08:51:25 -04:00
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
|
|
|
|
assert_raise(ArgumentError) { f.read(-1) }
|
|
|
|
assert_equal(str, f.read)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_readpartial
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_readpartial")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert("foo".start_with?(f.readpartial(3)))
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
s = ""
|
|
|
|
f.readpartial(3, s)
|
|
|
|
assert("foo".start_with?(s))
|
2008-06-08 08:51:25 -04:00
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
assert_raise(ArgumentError) { f.readpartial(-1) }
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_getc
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_getc")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
"foobar".each_char {|c| assert_equal(c, f.getc) }
|
|
|
|
assert_nil(f.getc)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
2008-10-21 05:18:34 -04:00
|
|
|
def test_getbyte
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_getbyte")
|
2008-10-21 05:18:34 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
"foobar".each_byte {|c| assert_equal(c, f.getbyte) }
|
|
|
|
assert_nil(f.getbyte)
|
|
|
|
end
|
2008-10-21 05:18:34 -04:00
|
|
|
end
|
|
|
|
|
2008-06-08 08:51:25 -04:00
|
|
|
def test_readchar
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_readchar")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
"foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
|
|
|
|
assert_raise(EOFError) { f.readchar }
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_byte
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_each_byte")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
a = []
|
|
|
|
f.each_byte {|c| a << c }
|
|
|
|
assert_equal("foobar".each_byte.to_a, a)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
2010-12-24 20:54:28 -05:00
|
|
|
def test_gets
|
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_gets")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal("foo\n", f.gets)
|
|
|
|
assert_equal("bar\n", f.gets)
|
|
|
|
assert_equal("baz\n", f.gets)
|
|
|
|
assert_nil(f.gets)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal("foo\nbar\nbaz\n", f.gets(nil))
|
|
|
|
end
|
2010-12-24 21:02:55 -05:00
|
|
|
|
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal("foo\n", f.gets(10))
|
|
|
|
assert_equal("ba", f.gets(2))
|
|
|
|
assert_equal("r\nb", f.gets(nil, 3))
|
|
|
|
assert_equal("az\n", f.gets(nil, 10))
|
|
|
|
assert_nil(f.gets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gets2
|
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_gets2")
|
|
|
|
t.close
|
|
|
|
ustrs = %W"\u{3042 3044 3046}\n \u{304b 304d 304f}\n \u{3055 3057 3059}\n"
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print(*ustrs) }
|
|
|
|
|
|
|
|
Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
|
|
|
|
assert_equal(ustrs[0], f.gets)
|
|
|
|
assert_equal(ustrs[1], f.gets)
|
|
|
|
assert_equal(ustrs[2], f.gets)
|
|
|
|
assert_nil(f.gets)
|
|
|
|
end
|
|
|
|
|
|
|
|
Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
|
|
|
|
assert_equal(ustrs.join(''), f.gets(nil))
|
|
|
|
end
|
|
|
|
|
|
|
|
Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
|
|
|
|
assert_equal(ustrs[0], f.gets(20))
|
|
|
|
assert_equal(ustrs[1][0,2], f.gets(5))
|
|
|
|
assert_equal(ustrs[1][2..-1]+ustrs[2][0,1], f.gets(nil, 5))
|
|
|
|
assert_equal(ustrs[2][1..-1], f.gets(nil, 20))
|
|
|
|
assert_nil(f.gets)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
2010-12-24 20:54:28 -05:00
|
|
|
def test_readline
|
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_readline")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal("foo\n", f.readline)
|
|
|
|
assert_equal("bar\n", f.readline)
|
|
|
|
assert_equal("baz\n", f.readline)
|
|
|
|
assert_raise(EOFError) { f.readline }
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_each")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
a = ["foo\n", "bar\n", "baz\n"]
|
|
|
|
f.each {|l| assert_equal(a.shift, l) }
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_readlines
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_readlines")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
Zlib::GzipReader.open(t.path) do |f|
|
|
|
|
assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reader_wrap
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_reader_wrap")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
f = open(t.path)
|
|
|
|
assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
|
|
|
|
assert_raise(IOError) { f.close }
|
|
|
|
end
|
2004-08-06 12:01:02 -04:00
|
|
|
end
|
|
|
|
|
2004-04-05 22:59:13 -04:00
|
|
|
class TestZlibGzipWriter < Test::Unit::TestCase
|
2004-04-07 01:06:34 -04:00
|
|
|
def test_invalid_new
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
|
|
|
|
assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
|
|
|
|
assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close }
|
|
|
|
assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close }
|
2004-04-05 22:59:13 -04:00
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
|
|
|
|
def test_open
|
|
|
|
assert_raise(ArgumentError) { Zlib::GzipWriter.open }
|
|
|
|
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_writer_open")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
|
|
|
|
|
|
|
|
f = Zlib::GzipWriter.open(t.path)
|
2010-12-13 22:57:26 -05:00
|
|
|
begin
|
|
|
|
f.print("bar")
|
|
|
|
ensure
|
|
|
|
f.close
|
|
|
|
end
|
2008-06-08 08:51:25 -04:00
|
|
|
assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
|
|
|
|
|
|
|
|
assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_writer_write")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
|
|
|
|
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
|
|
|
|
|
|
|
|
o = Object.new
|
2008-10-21 10:14:13 -04:00
|
|
|
def o.to_s; "bar"; end
|
2008-06-08 08:51:25 -04:00
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) }
|
|
|
|
assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_putc
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_writer_putc")
|
2008-06-08 08:51:25 -04:00
|
|
|
t.close
|
|
|
|
Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
|
|
|
|
assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
|
|
|
|
|
|
|
|
# todo: multibyte char
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_writer_wrap
|
2010-12-13 22:57:26 -05:00
|
|
|
t = Tempfile.new("test_zlib_gzip_writer_wrap")
|
2008-06-08 08:51:25 -04:00
|
|
|
Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
|
|
|
|
t.close
|
|
|
|
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestZlib < Test::Unit::TestCase
|
|
|
|
def test_version
|
|
|
|
assert_instance_of(String, Zlib.zlib_version)
|
|
|
|
assert(Zlib.zlib_version.tainted?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_adler32
|
|
|
|
assert_equal(0x00000001, Zlib.adler32)
|
|
|
|
assert_equal(0x02820145, Zlib.adler32("foo"))
|
|
|
|
assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo")))
|
|
|
|
assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000))
|
|
|
|
end
|
|
|
|
|
2010-01-18 19:06:04 -05:00
|
|
|
def test_adler32_combine
|
|
|
|
one = Zlib.adler32("fo")
|
|
|
|
two = Zlib.adler32("o")
|
2010-05-18 22:42:57 -04:00
|
|
|
begin
|
|
|
|
assert_equal(0x02820145, Zlib.adler32_combine(one, two, 1))
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip "adler32_combine is not implemented"
|
|
|
|
end
|
2010-01-18 19:06:04 -05:00
|
|
|
end
|
|
|
|
|
2008-06-08 08:51:25 -04:00
|
|
|
def test_crc32
|
|
|
|
assert_equal(0x00000000, Zlib.crc32)
|
|
|
|
assert_equal(0x8c736521, Zlib.crc32("foo"))
|
|
|
|
assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo")))
|
|
|
|
assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000))
|
|
|
|
end
|
|
|
|
|
2010-01-18 19:06:04 -05:00
|
|
|
def test_crc32_combine
|
|
|
|
one = Zlib.crc32("fo")
|
|
|
|
two = Zlib.crc32("o")
|
2010-05-18 22:42:57 -04:00
|
|
|
begin
|
|
|
|
assert_equal(0x8c736521, Zlib.crc32_combine(one, two, 1))
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip "crc32_combine is not implemented"
|
|
|
|
end
|
2010-01-18 19:06:04 -05:00
|
|
|
end
|
|
|
|
|
2008-06-08 08:51:25 -04:00
|
|
|
def test_crc_table
|
|
|
|
t = Zlib.crc_table
|
|
|
|
assert_instance_of(Array, t)
|
|
|
|
t.each {|x| assert_kind_of(Integer, x) }
|
|
|
|
end
|
2004-04-05 09:27:45 -04:00
|
|
|
end
|
|
|
|
end
|