2 # frozen_string_literal: true
15 class TestZlibDeflate < Test::Unit::TestCase
18 s = z.deflate("foo", Zlib::FINISH)
19 assert_equal("foo", Zlib::Inflate.inflate(s))
23 s << z.deflate(nil, Zlib::FINISH)
24 assert_equal("foo", Zlib::Inflate.inflate(s))
26 assert_raise(Zlib::StreamError) { Zlib::Deflate.new(10000) }
30 z1 = Zlib::Deflate.new
33 s1 = s + z1.deflate("bar", Zlib::FINISH)
34 s2 = s + z2.deflate("baz", Zlib::FINISH)
35 assert_equal("foobar", Zlib::Inflate.inflate(s1))
36 assert_equal("foobaz", Zlib::Inflate.inflate(s2))
40 s = Zlib::Deflate.deflate("foo")
41 assert_equal("foo", Zlib::Inflate.inflate(s))
43 assert_raise(Zlib::StreamError) { Zlib::Deflate.deflate("foo", 10000) }
46 def test_deflate_chunked
54 input = r.bytes(20000)
56 z.deflate(input) do |chunk|
61 assert_equal [16384, 16384],
62 chunks.map { |chunk| chunk.length }
66 assert_equal 7253, final.length
71 inflated = Zlib.inflate all
73 assert_equal original, inflated
76 def test_deflate_chunked_break
82 input = r.bytes(20000)
83 z.deflate(input) do |chunk|
88 assert_equal [16384], chunks.map { |chunk| chunk.length }
92 assert_equal 3632, final.length
97 original = Zlib.inflate all
99 assert_equal input, original
103 z = Zlib::Deflate.new
105 s = z.deflate(nil, Zlib::FINISH)
106 assert_equal("foo", Zlib::Inflate.inflate(s))
110 z = Zlib::Deflate.new
116 s << z.flush_next_out
117 s << z.deflate("qux", Zlib::FINISH)
118 assert_equal("foobarbazqux", Zlib::Inflate.inflate(s))
122 z = Zlib::Deflate.new
123 assert_equal(0, z.avail_in)
124 assert_equal(0, z.avail_out)
129 assert_equal("foobar", Zlib::Inflate.inflate(s))
132 def test_expand_buffer;
133 z = Zlib::Deflate.new
137 s = z.deflate(src, Zlib::FINISH)
139 assert_equal(src, Zlib::Inflate.inflate(s))
143 z = Zlib::Deflate.new
144 1000.times { z << "foo" }
146 assert_equal(3000, z.total_in)
147 assert_operator(3000, :>, z.total_out)
148 assert_equal("foo" * 1000, Zlib::Inflate.inflate(s))
152 z = Zlib::Deflate.new
153 assert([Zlib::ASCII, Zlib::BINARY, Zlib::UNKNOWN].include?(z.data_type))
157 z = Zlib::Deflate.new
160 assert_equal(0x02820145, z.adler)
164 z = Zlib::Deflate.new
165 assert_equal(false, z.finished?)
167 assert_equal(false, z.finished?)
169 assert_equal(true, z.finished?)
171 assert_raise(Zlib::Error) { z.finished? }
175 z = Zlib::Deflate.new
176 assert_equal(false, z.closed?)
178 assert_equal(false, z.closed?)
180 assert_equal(false, z.closed?)
182 assert_equal(true, z.closed?)
186 z = Zlib::Deflate.new
188 z.params(Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
191 assert_equal("foobar", Zlib::Inflate.inflate(s))
193 data = ('a'..'z').to_a.join
194 z = Zlib::Deflate.new(Zlib::NO_COMPRESSION, Zlib::MAX_WBITS,
195 Zlib::DEF_MEM_LEVEL, Zlib::DEFAULT_STRATEGY)
197 z.params(Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY)
199 assert_equal(data, Zlib::Inflate.inflate(z.finish))
201 z = Zlib::Deflate.new
202 s = z.deflate("foo", Zlib::FULL_FLUSH)
204 EnvUtil.suppress_warning {z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)}
205 s << z.deflate("bar", Zlib::FULL_FLUSH)
207 EnvUtil.suppress_warning {z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)}
208 s << z.deflate("baz", Zlib::FINISH)
209 assert_equal("foobarbaz", Zlib::Inflate.inflate(s))
211 z = Zlib::Deflate.new
212 assert_raise(Zlib::StreamError) { z.params(10000, 10000) }
213 z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
216 def test_set_dictionary
217 z = Zlib::Deflate.new
218 z.set_dictionary("foo")
219 s = z.deflate("foo" * 100, Zlib::FINISH)
220 z = Zlib::Inflate.new
221 assert_raise(Zlib::NeedDict) { z.inflate(s) }
222 z.set_dictionary("foo")
223 assert_equal("foo" * 100, z.inflate(s)) # ???
225 z = Zlib::Deflate.new
227 assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
228 EnvUtil.suppress_warning do
229 z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
234 z = Zlib::Deflate.new
239 assert_equal("bar", Zlib::Inflate.inflate(s))
243 z = Zlib::Deflate.new
245 assert_raise(Zlib::Error) { z << "foo" }
246 assert_raise(Zlib::Error) { z.reset }
250 class TestZlibInflate < Test::Unit::TestCase
251 def test_class_inflate_dictionary
252 assert_raise(Zlib::NeedDict) do
253 Zlib::Inflate.inflate([0x08,0x3C,0x0,0x0,0x0,0x0].pack("c*"))
258 assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
260 s = Zlib::Deflate.deflate("foo")
261 z = Zlib::Inflate.new
263 assert_equal("foo", z.finish)
266 def test_add_dictionary
269 deflate = Zlib::Deflate.new
270 deflate.set_dictionary dictionary
271 compressed = deflate.deflate "foofoofoo", Zlib::FINISH
275 inflate = Zlib::Inflate.new
276 inflate.add_dictionary "foo"
278 out = inflate.inflate compressed
280 assert_equal "foofoofoo", out
283 def test_finish_chunked
284 # zeros = Zlib::Deflate.deflate("0" * 100_000)
285 zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
286 "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
287 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
288 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
289 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
290 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
291 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
292 "\000\000\000\000\000\000\000\257\006\351\247BH"
296 z = Zlib::Inflate.new
298 z.inflate(zeros) do |chunk|
307 assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
308 chunks.map { |chunk| chunk.size }
310 assert chunks.all? { |chunk|
316 s = Zlib::Deflate.deflate("foo")
317 z = Zlib::Inflate.new
320 assert_equal("foo", s)
321 z.inflate("foo") # ???
325 def test_inflate_partial_input
326 deflated = Zlib::Deflate.deflate "\0"
328 z = Zlib::Inflate.new
332 deflated.each_char do |byte|
333 inflated << z.inflate(byte)
338 assert_equal "\0", inflated
341 def test_inflate_chunked
342 # s = Zlib::Deflate.deflate("0" * 100_000)
343 zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
344 "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
345 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
346 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
347 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
348 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
349 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
350 "\000\000\000\000\000\000\000\257\006\351\247BH"
354 z = Zlib::Inflate.new
356 z.inflate(zeros) do |chunk|
360 assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
361 chunks.map { |chunk| chunk.size }
363 assert chunks.all? { |chunk|
368 def test_inflate_buffer
369 s = Zlib::Deflate.deflate("foo")
370 z = Zlib::Inflate.new
372 s = z.inflate(s, buffer: buf)
375 s << z.inflate(nil, buffer: buf)
376 assert_equal("foo", s)
377 z.inflate("foo", buffer: buf) # ???
381 def test_inflate_buffer_partial_input
382 deflated = Zlib::Deflate.deflate "\0"
384 z = Zlib::Inflate.new
389 deflated.each_char do |byte|
390 inflated << z.inflate(byte, buffer: buf)
395 assert_equal "\0", inflated
398 def test_inflate_buffer_chunked
399 # s = Zlib::Deflate.deflate("0" * 100_000)
400 zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
401 "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
402 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
403 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
404 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
405 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
406 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
407 "\000\000\000\000\000\000\000\257\006\351\247BH"
411 z = Zlib::Inflate.new
414 z.inflate(zeros, buffer: buf) do |chunk|
415 assert_same(buf, chunk)
419 assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
420 chunks.map { |chunk| chunk.size }
422 assert chunks.all? { |chunk|
427 def test_inflate_chunked_break
428 # zeros = Zlib::Deflate.deflate("0" * 100_000)
429 zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
430 "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
431 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
432 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
433 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
434 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
435 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
436 "\000\000\000\000\000\000\000\257\006\351\247BH"
440 z = Zlib::Inflate.new
442 z.inflate(zeros) do |chunk|
449 assert_equal 100_000 - chunks.first.length, out.length
452 def test_inflate_dictionary
455 deflate = Zlib::Deflate.new
456 deflate.set_dictionary dictionary
457 compressed = deflate.deflate "foofoofoo", Zlib::FINISH
461 inflate = Zlib::Inflate.new
464 out = inflate.inflate compressed
466 flunk "Zlib::NeedDict was not raised"
467 rescue Zlib::NeedDict
468 inflate.set_dictionary dictionary
469 out = inflate.inflate ""
472 assert_equal "foofoofoo", out
476 z = Zlib::Deflate.new
477 s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
479 EnvUtil.suppress_warning {z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)}
480 s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
482 EnvUtil.suppress_warning {z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)}
483 s << z.deflate("baz" * 1000, Zlib::FINISH)
485 z = Zlib::Inflate.new
486 assert_raise(Zlib::DataError) { z << "\0" * 100 }
487 assert_equal(false, z.sync(""))
488 assert_equal(false, z.sync_point?)
490 z = Zlib::Inflate.new
491 assert_raise(Zlib::DataError) { z << "\0" * 100 + s }
492 assert_equal(true, z.sync(""))
494 z = Zlib::Inflate.new
495 assert_equal(false, z.sync("\0" * 100))
496 assert_equal(false, z.sync_point?)
498 z = Zlib::Inflate.new
499 assert_equal(true, z.sync("\0" * 100 + s))
502 def test_set_dictionary
503 z = Zlib::Inflate.new
504 assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
508 def test_multithread_deflate
509 zd = Zlib::Deflate.new
514 1000.times { zd.deflate(s) }
524 def test_multithread_inflate
525 zi = Zlib::Inflate.new
527 s = Zlib.deflate("x" * 10000)
530 1000.times { zi.inflate(s) }
540 def test_recursive_deflate
541 original_gc_stress = GC.stress
543 zd = Zlib::Deflate.new
545 s = SecureRandom.random_bytes(1024**2)
546 assert_raise(Zlib::InProgressError) do
552 GC.stress = original_gc_stress
557 def test_recursive_inflate
558 original_gc_stress = GC.stress
560 zi = Zlib::Inflate.new
562 s = Zlib.deflate(SecureRandom.random_bytes(1024**2))
564 assert_raise(Zlib::InProgressError) do
570 GC.stress = original_gc_stress
575 class TestZlibGzipFile < Test::Unit::TestCase
576 def test_gzip_reader_zcat
577 Tempfile.create("test_zlib_gzip_file_to_io") {|t|
579 gz = Zlib::GzipWriter.new(t)
582 File.open(t.path, 'ab') do |f|
583 gz = Zlib::GzipWriter.new(f)
589 File.open(t.path, 'rb') do |f|
590 Zlib::GzipReader.zcat(f) do |str|
594 assert_equal(["foo", "bar"], results)
596 results = File.open(t.path, 'rb') do |f|
597 Zlib::GzipReader.zcat(f)
599 assert_equal("foobar", results)
604 Tempfile.create("test_zlib_gzip_file_to_io") {|t|
606 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
608 Zlib::GzipReader.open(t.path) do |f|
609 assert_kind_of(IO, f.to_io)
615 Tempfile.create("test_zlib_gzip_file_crc") {|t|
617 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
619 Zlib::GzipReader.open(t.path) do |f|
621 assert_equal(0x8c736521, f.crc)
629 Tempfile.create("test_zlib_gzip_file_mtime") {|t|
631 Zlib::GzipWriter.open(t.path) do |gz|
636 assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
639 Zlib::GzipReader.open(t.path) do |f|
640 assert_equal(tim.to_i, f.mtime.to_i)
647 gz = Zlib::GzipWriter.new(sio)
651 reading_io = StringIO.new(sio.string)
652 reader = Zlib::GzipReader.new(reading_io)
653 assert_equal(0, reader.mtime.to_i)
657 Tempfile.create("test_zlib_gzip_file_level") {|t|
659 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
661 Zlib::GzipReader.open(t.path) do |f|
662 assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
668 Tempfile.create("test_zlib_gzip_file_os_code") {|t|
669 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
671 Zlib::GzipReader.open(t.path) do |f|
672 assert_equal(Zlib::OS_CODE, f.os_code)
678 Tempfile.create("test_zlib_gzip_file_orig_name") {|t|
680 Zlib::GzipWriter.open(t.path) do |gz|
681 gz.orig_name = "foobarbazqux\0quux"
684 assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
687 Zlib::GzipReader.open(t.path) do |f|
688 assert_equal("foobarbazqux", f.orig_name)
694 Tempfile.create("test_zlib_gzip_file_comment") {|t|
696 Zlib::GzipWriter.open(t.path) do |gz|
697 gz.comment = "foobarbazqux\0quux"
700 assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
703 Zlib::GzipReader.open(t.path) do |f|
704 assert_equal("foobarbazqux", f.comment)
710 Tempfile.create("test_zlib_gzip_file_lineno") {|t|
712 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
714 Zlib::GzipReader.open(t.path) do |f|
715 assert_equal([0, "foo\n"], [f.lineno, f.gets])
716 assert_equal([1, "bar\n"], [f.lineno, f.gets])
718 assert_equal([1000, "baz\n"], [f.lineno, f.gets])
719 assert_equal([1001, "qux\n"], [f.lineno, f.gets])
725 Tempfile.create("test_zlib_gzip_file_closed_p") {|t|
727 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
729 Zlib::GzipReader.open(t.path) do |f|
730 assert_equal(false, f.closed?)
732 assert_equal(false, f.closed?)
734 assert_equal(true, f.closed?)
740 Tempfile.create("test_zlib_gzip_file_sync") {|t|
742 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
744 Zlib::GzipReader.open(t.path) do |f|
746 assert_equal(true, f.sync)
749 assert_equal(false, f.sync)
755 Tempfile.create("test_zlib_gzip_file_pos") {|t|
757 Zlib::GzipWriter.open(t.path) do |gz|
760 assert_equal(3, gz.tell)
766 Tempfile.create("test_zlib_gzip_file_path") {|t|
769 gz = Zlib::GzipWriter.open(t.path)
771 assert_equal(t.path, gz.path)
773 assert_equal(t.path, gz.path)
775 Zlib::GzipReader.open(t.path) do |f|
776 assert_equal(t.path, f.path)
778 assert_equal(t.path, f.path)
782 sio = StringIO.new(s)
783 gz = Zlib::GzipWriter.new(sio)
785 assert_raise(NoMethodError) { gz.path }
788 sio = StringIO.new(s)
789 gz = Zlib::GzipReader.new(sio)
790 assert_raise(NoMethodError) { gz.path }
795 if defined? File::TMPFILE
796 def test_path_tmpfile
797 sio = StringIO.new("".dup, 'w')
798 gz = Zlib::GzipWriter.new(sio)
802 File.open(Dir.mktmpdir, File::RDWR | File::TMPFILE) do |io|
806 gz0 = Zlib::GzipWriter.new(io)
807 assert_raise(NoMethodError) { gz0.path }
809 gz1 = Zlib::GzipReader.new(io)
810 assert_raise(NoMethodError) { gz1.path }
815 omit 'O_TMPFILE not supported (EINVAL)'
817 omit 'O_TMPFILE not supported (EISDIR)'
818 rescue Errno::EOPNOTSUPP
819 omit 'O_TMPFILE not supported (EOPNOTSUPP)'
824 class TestZlibGzipReader < Test::Unit::TestCase
825 D0 = "\037\213\010\000S`\017A\000\003\003\000\000\000\000\000\000\000\000\000"
827 assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
832 w = Zlib::GzipWriter.new(StringIO.new(s))
833 w << (1...1000).to_a.inspect
835 r = Zlib::GzipReader.new(StringIO.new(s))
838 assert_nothing_raised("[ruby-dev:24060]") {
845 def test_ungetc_paragraph
847 w = Zlib::GzipWriter.new(StringIO.new(s))
850 r = Zlib::GzipReader.new(StringIO.new(s))
852 assert_equal("abc", r.gets(""))
853 assert_nothing_raised("[ruby-dev:24065]") {
859 def test_ungetc_at_start_of_file
861 w = Zlib::GzipWriter.new(StringIO.new(s))
864 r = Zlib::GzipReader.new(StringIO.new(s))
868 assert_equal(-1, r.pos, "[ruby-core:81488][Bug #13616]")
872 Tempfile.create("test_zlib_gzip_reader_open") {|t|
874 e = assert_raise(Zlib::GzipFile::Error) {
875 Zlib::GzipReader.open(t.path)
877 assert_equal("not in gzip format", e.message)
879 open(t.path, "wb") {|f| f.write("foo")}
880 e = assert_raise(Zlib::GzipFile::Error) {
881 Zlib::GzipReader.open(t.path)
883 assert_equal("not in gzip format", e.message)
884 assert_equal("foo", e.input)
885 open(t.path, "wb") {|f| f.write("foobarzothoge")}
886 e = assert_raise(Zlib::GzipFile::Error) {
887 Zlib::GzipReader.open(t.path)
889 assert_equal("not in gzip format", e.message)
890 assert_equal("foobarzothoge", e.input)
892 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
894 assert_raise(ArgumentError) { Zlib::GzipReader.open }
896 assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
898 f = Zlib::GzipReader.open(t.path)
900 assert_equal("foo", f.read)
908 bug8467 = '[ruby-core:55220] [Bug #8467]'
909 Tempfile.create("test_zlib_gzip_reader_rewind") {|t|
911 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
913 Zlib::GzipReader.open(t.path) do |f|
914 assert_equal("foo", f.read)
916 assert_equal("foo", f.read)
920 f.each_byte { |b| bytes << b }
921 assert_equal "foo".bytes.to_a, bytes, '[Bug #10101]'
923 open(t.path, "rb") do |f|
924 gz = Zlib::GzipReader.new(f)
926 assert_equal(["foo"], gz.to_a, bug8467)
932 Tempfile.create("test_zlib_gzip_reader_unused") {|t|
934 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
936 Zlib::GzipReader.open(t.path) do |f|
937 assert_equal(nil, f.unused)
938 assert_equal("foo", f.read(3))
939 assert_equal(nil, f.unused)
940 assert_equal("bar", f.read)
941 assert_equal(nil, f.unused)
949 io = Zlib::GzipWriter.new zio
953 io = Zlib::GzipWriter.new zio
959 io = Zlib::GzipReader.new zio
960 assert_equal('aaaa', io.read)
962 assert_equal(24, unused.bytesize)
965 zio.pos -= unused.length
967 io = Zlib::GzipReader.new zio
968 assert_equal('bbbb', io.read)
969 assert_equal(nil, io.unused)
974 Tempfile.create("test_zlib_gzip_reader_read") {|t|
976 str = "\u3042\u3044\u3046"
977 Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
979 Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
980 assert_raise(ArgumentError) { f.read(-1) }
981 assert_equal(str, f.read)
987 Tempfile.create("test_zlib_gzip_reader_readpartial") {|t|
989 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
991 Zlib::GzipReader.open(t.path) do |f|
992 assert("foo".start_with?(f.readpartial(3)))
995 Zlib::GzipReader.open(t.path) do |f|
998 assert("foo".start_with?(s))
1000 assert_raise(ArgumentError) { f.readpartial(-1) }
1006 Tempfile.create("test_zlib_gzip_reader_getc") {|t|
1008 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
1010 Zlib::GzipReader.open(t.path) do |f|
1011 "foobar".each_char {|c| assert_equal(c, f.getc) }
1018 Tempfile.create("test_zlib_gzip_reader_getbyte") {|t|
1020 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
1022 Zlib::GzipReader.open(t.path) do |f|
1023 "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
1024 assert_nil(f.getbyte)
1030 Tempfile.create("test_zlib_gzip_reader_readchar") {|t|
1032 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
1034 Zlib::GzipReader.open(t.path) do |f|
1035 "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
1036 assert_raise(EOFError) { f.readchar }
1042 Tempfile.create("test_zlib_gzip_reader_each_byte") {|t|
1044 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
1046 Zlib::GzipReader.open(t.path) do |f|
1048 f.each_byte {|c| a << c }
1049 assert_equal("foobar".each_byte.to_a, a)
1055 Tempfile.create("test_zlib_gzip_reader_gets") {|t|
1057 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
1059 Zlib::GzipReader.open(t.path) do |f|
1060 assert_equal("foo\n", f.gets)
1061 assert_equal("bar\n", f.gets)
1062 assert_equal("baz\n", f.gets)
1066 Zlib::GzipReader.open(t.path) do |f|
1067 assert_equal("foo\nbar\nbaz\n", f.gets(nil))
1070 Zlib::GzipReader.open(t.path) do |f|
1071 assert_equal("foo\n", f.gets(10))
1072 assert_equal("ba", f.gets(2))
1073 assert_equal("r\nb", f.gets(nil, 3))
1074 assert_equal("az\n", f.gets(nil, 10))
1081 Tempfile.create("test_zlib_gzip_reader_gets2") {|t|
1083 ustrs = %W"\u{3042 3044 3046}\n \u{304b 304d 304f}\n \u{3055 3057 3059}\n"
1084 Zlib::GzipWriter.open(t.path) {|gz| gz.print(*ustrs) }
1086 Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
1087 assert_equal(ustrs[0], f.gets)
1088 assert_equal(ustrs[1], f.gets)
1089 assert_equal(ustrs[2], f.gets)
1093 Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
1094 assert_equal(ustrs.join(''), f.gets(nil))
1097 Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
1098 assert_equal(ustrs[0], f.gets(20))
1099 assert_equal(ustrs[1][0,2], f.gets(5))
1100 assert_equal(ustrs[1][2..-1]+ustrs[2][0,1], f.gets(nil, 5))
1101 assert_equal(ustrs[2][1..-1], f.gets(nil, 20))
1108 Tempfile.create("test_zlib_gzip_reader_readline") {|t|
1110 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
1112 Zlib::GzipReader.open(t.path) do |f|
1113 assert_equal("foo\n", f.readline)
1114 assert_equal("bar\n", f.readline)
1115 assert_equal("baz\n", f.readline)
1116 assert_raise(EOFError) { f.readline }
1122 Tempfile.create("test_zlib_gzip_reader_each") {|t|
1124 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
1126 Zlib::GzipReader.open(t.path) do |f|
1127 a = ["foo\n", "bar\n", "baz\n"]
1128 f.each {|l| assert_equal(a.shift, l) }
1134 Tempfile.create("test_zlib_gzip_reader_readlines") {|t|
1136 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
1138 Zlib::GzipReader.open(t.path) do |f|
1139 assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
1144 def test_reader_wrap
1145 Tempfile.create("test_zlib_gzip_reader_wrap") {|t|
1147 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
1150 assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
1155 def test_corrupted_header
1156 gz = Zlib::GzipWriter.new(StringIO.new(s = "".dup))
1161 # 14: magic(2) + method(1) + flag(1) + mtime(4) + exflag(1) + os(1) + orig_name(2) + comment(2)
1163 assert_raise(Zlib::GzipFile::Error, idx) do
1164 Zlib::GzipReader.new(StringIO.new(s[0, idx])).read
1170 Tempfile.create("test_zlib_gzip_reader_encoding") {|t|
1172 content = (0..255).to_a.pack('c*')
1173 Zlib::GzipWriter.wrap(t) {|gz| gz.print(content) }
1175 read_all = Zlib::GzipReader.open(t.path) do |gz|
1176 assert_equal(Encoding.default_external, gz.external_encoding)
1179 assert_equal(Encoding.default_external, read_all.encoding)
1181 # chunks are in BINARY regardless of encoding settings
1182 read_size = Zlib::GzipReader.open(t.path) {|gz| gz.read(1024) }
1183 assert_equal(Encoding::ASCII_8BIT, read_size.encoding)
1184 assert_equal(content, read_size)
1188 def test_double_close
1189 Tempfile.create("test_zlib_gzip_reader_close") {|t|
1192 Zlib::GzipWriter.wrap(t) {|gz| gz.print(content) }
1193 r = Zlib::GzipReader.open(t.path)
1194 assert_equal(content, r.read)
1195 assert_nothing_raised { r.close }
1196 assert_nothing_raised { r.close }
1202 class TestZlibGzipWriter < Test::Unit::TestCase
1203 def test_invalid_new
1204 assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
1205 assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
1206 assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close }
1207 assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close }
1211 assert_raise(ArgumentError) { Zlib::GzipWriter.open }
1213 Tempfile.create("test_zlib_gzip_writer_open") {|t|
1215 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
1216 assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1218 f = Zlib::GzipWriter.open(t.path)
1224 assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1226 assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
1231 Tempfile.create("test_zlib_gzip_writer_write") {|t|
1233 Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
1234 assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1237 def o.to_s; "bar"; end
1238 Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) }
1239 assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1244 Tempfile.create("test_zlib_gzip_writer_putc") {|t|
1246 Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
1247 assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1249 # todo: multibyte char
1254 Tempfile.create("test_zlib_gzip_writer_puts") {|t|
1256 Zlib::GzipWriter.open(t.path) {|gz| gz.puts("foo") }
1257 assert_equal("foo\n", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1261 def test_writer_wrap
1262 Tempfile.create("test_zlib_gzip_writer_wrap") {|t|
1264 Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
1265 assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1269 def test_double_close
1270 Tempfile.create("test_zlib_gzip_reader_close") {|t|
1272 w = Zlib::GzipWriter.wrap(t)
1273 assert_nothing_raised { w.close }
1274 assert_nothing_raised { w.close }
1278 def test_zlib_writer_buffered_write
1279 bug15356 = '[ruby-core:90346] [Bug #15356]'.freeze
1280 fixes = 'r61631 (commit a55abcc0ca6f628fc05304f81e5a044d65ab4a68)'.freeze
1282 def ary.write(*args)
1285 gz = Zlib::GzipWriter.new(ary)
1290 assert_not_predicate ary, :empty?
1291 exp = [ bug15356, fixes ]
1292 assert_equal exp, Zlib.gunzip(ary.join('')).split("\n")
1296 class TestZlib < Test::Unit::TestCase
1298 assert_instance_of(String, Zlib.zlib_version)
1302 assert_equal(0x00000001, Zlib.adler32)
1303 assert_equal(0x02820145, Zlib.adler32("foo"))
1304 assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo")))
1305 assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000))
1306 Tempfile.create("test_zlib_gzip_file_to_io") {|t|
1307 File.binwrite(t.path, "foo")
1309 assert_equal(0x02820145, Zlib.adler32(t))
1312 crc = Zlib.adler32(t.read(2))
1313 assert_equal(0x02820145, Zlib.adler32(t, crc))
1315 File.binwrite(t.path, "abc\x01\x02\x03" * 10000)
1317 assert_equal(0x8a62c964, Zlib.adler32(t))
1321 def test_adler32_combine
1322 one = Zlib.adler32("fo")
1323 two = Zlib.adler32("o")
1325 assert_equal(0x02820145, Zlib.adler32_combine(one, two, 1))
1326 rescue NotImplementedError
1327 omit "adler32_combine is not implemented"
1328 rescue Test::Unit::AssertionFailedError
1329 if /aix/ =~ RUBY_PLATFORM
1330 omit "zconf.h in zlib does not handle _LARGE_FILES in AIX. Skip until it is fixed"
1337 assert_equal(0x00000000, Zlib.crc32)
1338 assert_equal(0x8c736521, Zlib.crc32("foo"))
1339 assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo")))
1340 assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000))
1341 Tempfile.create("test_zlib_gzip_file_to_io") {|t|
1342 File.binwrite(t.path, "foo")
1344 assert_equal(0x8c736521, Zlib.crc32(t))
1347 crc = Zlib.crc32(t.read(2))
1348 assert_equal(0x8c736521, Zlib.crc32(t, crc))
1350 File.binwrite(t.path, "abc\x01\x02\x03" * 10000)
1352 assert_equal(0x07f0d68f, Zlib.crc32(t))
1356 def test_crc32_combine
1357 one = Zlib.crc32("fo")
1358 two = Zlib.crc32("o")
1360 assert_equal(0x8c736521, Zlib.crc32_combine(one, two, 1))
1361 rescue NotImplementedError
1362 omit "crc32_combine is not implemented"
1363 rescue Test::Unit::AssertionFailedError
1364 if /aix/ =~ RUBY_PLATFORM
1365 omit "zconf.h in zlib does not handle _LARGE_FILES in AIX. Skip until it is fixed"
1373 assert_instance_of(Array, t)
1374 t.each {|x| assert_kind_of(Integer, x) }
1378 s = Zlib::Deflate.deflate("foo")
1379 z = Zlib::Inflate.new
1382 assert_equal("foo", s)
1383 z.inflate("foo") # ???
1388 s = Zlib::Deflate.deflate("foo")
1389 assert_equal("foo", Zlib::Inflate.inflate(s))
1391 assert_raise(Zlib::StreamError) { Zlib::Deflate.deflate("foo", 10000) }
1394 def test_deflate_stream
1399 Zlib.deflate(r.bytes(20000)) do |chunk|
1403 assert_equal 20016, deflated.length
1407 actual = Zlib.gzip("foo".freeze)
1408 actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
1409 actual[9] = "\xff" # replace OS
1410 expected = %w[1f8b08000000000000ff4bcbcf07002165738c03000000].pack("H*")
1411 assert_equal expected, actual
1413 actual = Zlib.gzip("foo".freeze, level: 0)
1414 actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
1415 actual[9] = "\xff" # replace OS
1416 expected = %w[1f8b08000000000000ff010300fcff666f6f2165738c03000000].pack("H*")
1417 assert_equal expected, actual
1419 actual = Zlib.gzip("foo".freeze, level: 9)
1420 actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
1421 actual[9] = "\xff" # replace OS
1422 expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")
1423 assert_equal expected, actual
1425 actual = Zlib.gzip("foo".freeze, level: 9, strategy: Zlib::FILTERED)
1426 actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
1427 actual[9] = "\xff" # replace OS
1428 expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")
1429 assert_equal expected, actual
1433 src = %w[1f8b08000000000000034bcbcf07002165738c03000000].pack("H*")
1434 assert_equal 'foo', Zlib.gunzip(src.freeze)
1436 src = %w[1f8b08000000000000034bcbcf07002165738c03000001].pack("H*")
1437 assert_raise(Zlib::GzipFile::LengthError){ Zlib.gunzip(src) }
1439 src = %w[1f8b08000000000000034bcbcf07002165738d03000000].pack("H*")
1440 assert_raise(Zlib::GzipFile::CRCError){ Zlib.gunzip(src) }
1442 src = %w[1f8b08000000000000034bcbcf07002165738d030000].pack("H*")
1443 assert_raise(Zlib::GzipFile::Error){ Zlib.gunzip(src) }
1445 src = %w[1f8b08000000000000034bcbcf0700].pack("H*")
1446 assert_raise(Zlib::GzipFile::NoFooter){ Zlib.gunzip(src) }
1448 src = %w[1f8b080000000000000].pack("H*")
1449 assert_raise(Zlib::GzipFile::Error){ Zlib.gunzip(src) }
1452 def test_gunzip_no_memory_leak
1453 assert_no_memory_leak(%[-rzlib], "#{<<~"{#"}", "#{<<~'};'}")
1454 d = Zlib.gzip("data")
1456 10_000.times {Zlib.gunzip(d)}