* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / test / zlib / test_zlib.rb
bloba9de827ca8b794d28e5002b7064bbb3f2b9af433
1 # coding: us-ascii
2 # frozen_string_literal: true
3 require 'test/unit'
4 require 'stringio'
5 require 'tempfile'
6 require 'tmpdir'
7 require 'securerandom'
9 begin
10   require 'zlib'
11 rescue LoadError
12 end
14 if defined? Zlib
15   class TestZlibDeflate < Test::Unit::TestCase
16     def test_initialize
17       z = Zlib::Deflate.new
18       s = z.deflate("foo", Zlib::FINISH)
19       assert_equal("foo", Zlib::Inflate.inflate(s))
21       z = Zlib::Deflate.new
22       s = z.deflate("foo")
23       s << z.deflate(nil, Zlib::FINISH)
24       assert_equal("foo", Zlib::Inflate.inflate(s))
26       assert_raise(Zlib::StreamError) { Zlib::Deflate.new(10000) }
27     end
29     def test_dup
30       z1 = Zlib::Deflate.new
31       s = z1.deflate("foo")
32       z2 = z1.dup
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))
37     end
39     def test_deflate
40       s = Zlib::Deflate.deflate("foo")
41       assert_equal("foo", Zlib::Inflate.inflate(s))
43       assert_raise(Zlib::StreamError) { Zlib::Deflate.deflate("foo", 10000) }
44     end
46     def test_deflate_chunked
47       original = ''.dup
48       chunks = []
49       r = Random.new 0
51       z = Zlib::Deflate.new
53       2.times do
54         input = r.bytes(20000)
55         original << input
56         z.deflate(input) do |chunk|
57           chunks << chunk
58         end
59       end
61       assert_equal [16384, 16384],
62                    chunks.map { |chunk| chunk.length }
64       final = z.finish
66       assert_equal 7253, final.length
68       chunks << final
69       all = chunks.join
71       inflated = Zlib.inflate all
73       assert_equal original, inflated
74     end
76     def test_deflate_chunked_break
77       chunks = []
78       r = Random.new 0
80       z = Zlib::Deflate.new
82       input = r.bytes(20000)
83       z.deflate(input) do |chunk|
84         chunks << chunk
85         break
86       end
88       assert_equal [16384], chunks.map { |chunk| chunk.length }
90       final = z.finish
92       assert_equal 3632, final.length
94       all = chunks.join
95       all << final
97       original = Zlib.inflate all
99       assert_equal input, original
100     end
102     def test_addstr
103       z = Zlib::Deflate.new
104       z << "foo"
105       s = z.deflate(nil, Zlib::FINISH)
106       assert_equal("foo", Zlib::Inflate.inflate(s))
107     end
109     def test_flush
110       z = Zlib::Deflate.new
111       z << "foo"
112       s = z.flush
113       z << "bar"
114       s << z.flush_next_in
115       z << "baz"
116       s << z.flush_next_out
117       s << z.deflate("qux", Zlib::FINISH)
118       assert_equal("foobarbazqux", Zlib::Inflate.inflate(s))
119     end
121     def test_avail
122       z = Zlib::Deflate.new
123       assert_equal(0, z.avail_in)
124       assert_equal(0, z.avail_out)
125       z << "foo"
126       z.avail_out += 100
127       z << "bar"
128       s = z.finish
129       assert_equal("foobar", Zlib::Inflate.inflate(s))
130     end
132     def test_expand_buffer;
133       z = Zlib::Deflate.new
134       src = "baz" * 1000
135       z.avail_out = 1
136       GC.stress = true
137       s = z.deflate(src, Zlib::FINISH)
138       GC.stress = false
139       assert_equal(src, Zlib::Inflate.inflate(s))
140     end
142     def test_total
143       z = Zlib::Deflate.new
144       1000.times { z << "foo" }
145       s = z.finish
146       assert_equal(3000, z.total_in)
147       assert_operator(3000, :>, z.total_out)
148       assert_equal("foo" * 1000, Zlib::Inflate.inflate(s))
149     end
151     def test_data_type
152       z = Zlib::Deflate.new
153       assert([Zlib::ASCII, Zlib::BINARY, Zlib::UNKNOWN].include?(z.data_type))
154     end
156     def test_adler
157       z = Zlib::Deflate.new
158       z << "foo"
159       z.finish
160       assert_equal(0x02820145, z.adler)
161     end
163     def test_finished_p
164       z = Zlib::Deflate.new
165       assert_equal(false, z.finished?)
166       z << "foo"
167       assert_equal(false, z.finished?)
168       z.finish
169       assert_equal(true, z.finished?)
170       z.close
171       assert_raise(Zlib::Error) { z.finished? }
172     end
174     def test_closed_p
175       z = Zlib::Deflate.new
176       assert_equal(false, z.closed?)
177       z << "foo"
178       assert_equal(false, z.closed?)
179       z.finish
180       assert_equal(false, z.closed?)
181       z.close
182       assert_equal(true, z.closed?)
183     end
185     def test_params
186       z = Zlib::Deflate.new
187       z << "foo"
188       z.params(Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
189       z << "bar"
190       s = z.finish
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)
196       z << data[0, 10]
197       z.params(Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY)
198       z << data[10 .. -1]
199       assert_equal(data, Zlib::Inflate.inflate(z.finish))
201       z = Zlib::Deflate.new
202       s = z.deflate("foo", Zlib::FULL_FLUSH)
203       z.avail_out = 0
204       EnvUtil.suppress_warning {z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)}
205       s << z.deflate("bar", Zlib::FULL_FLUSH)
206       z.avail_out = 0
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.'
214     end
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
226       z << "foo"
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.'
230       end
231     end
233     def test_reset
234       z = Zlib::Deflate.new
235       z << "foo"
236       z.reset
237       z << "bar"
238       s = z.finish
239       assert_equal("bar", Zlib::Inflate.inflate(s))
240     end
242     def test_close
243       z = Zlib::Deflate.new
244       z.close
245       assert_raise(Zlib::Error) { z << "foo" }
246       assert_raise(Zlib::Error) { z.reset }
247     end
248   end
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*"))
254       end
255     end
257     def test_initialize
258       assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
260       s = Zlib::Deflate.deflate("foo")
261       z = Zlib::Inflate.new
262       z << s << nil
263       assert_equal("foo", z.finish)
264     end
266     def test_add_dictionary
267       dictionary = "foo"
269       deflate = Zlib::Deflate.new
270       deflate.set_dictionary dictionary
271       compressed = deflate.deflate "foofoofoo", Zlib::FINISH
272       deflate.close
274       out = nil
275       inflate = Zlib::Inflate.new
276       inflate.add_dictionary "foo"
278       out = inflate.inflate compressed
280       assert_equal "foofoofoo", out
281     end
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"
294       chunks = []
296       z = Zlib::Inflate.new
298       z.inflate(zeros) do |chunk|
299         chunks << chunk
300         break
301       end
303       z.finish do |chunk|
304         chunks << chunk
305       end
307       assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
308                    chunks.map { |chunk| chunk.size }
310       assert chunks.all? { |chunk|
311         chunk =~ /\A0+\z/
312       }
313     end
315     def test_inflate
316       s = Zlib::Deflate.deflate("foo")
317       z = Zlib::Inflate.new
318       s = z.inflate(s)
319       s << z.inflate(nil)
320       assert_equal("foo", s)
321       z.inflate("foo") # ???
322       z << "foo" # ???
323     end
325     def test_inflate_partial_input
326       deflated = Zlib::Deflate.deflate "\0"
328       z = Zlib::Inflate.new
330       inflated = "".dup
332       deflated.each_char do |byte|
333         inflated << z.inflate(byte)
334       end
336       inflated << z.finish
338       assert_equal "\0", inflated
339     end
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"
352       chunks = []
354       z = Zlib::Inflate.new
356       z.inflate(zeros) do |chunk|
357         chunks << chunk
358       end
360       assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
361                    chunks.map { |chunk| chunk.size }
363       assert chunks.all? { |chunk|
364         chunk =~ /\A0+\z/
365       }
366     end
368     def test_inflate_buffer
369       s = Zlib::Deflate.deflate("foo")
370       z = Zlib::Inflate.new
371       buf = String.new
372       s = z.inflate(s, buffer: buf)
373       assert_same(buf, s)
374       buf = String.new
375       s << z.inflate(nil, buffer: buf)
376       assert_equal("foo", s)
377       z.inflate("foo", buffer: buf) # ???
378       z << "foo" # ???
379     end
381     def test_inflate_buffer_partial_input
382       deflated = Zlib::Deflate.deflate "\0"
384       z = Zlib::Inflate.new
386       inflated = "".dup
388       buf = String.new
389       deflated.each_char do |byte|
390         inflated << z.inflate(byte, buffer: buf)
391       end
393       inflated << z.finish
395       assert_equal "\0", inflated
396     end
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"
409       chunks = []
411       z = Zlib::Inflate.new
413       buf = String.new
414       z.inflate(zeros, buffer: buf) do |chunk|
415         assert_same(buf, chunk)
416         chunks << chunk.dup
417       end
419       assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
420                    chunks.map { |chunk| chunk.size }
422       assert chunks.all? { |chunk|
423         chunk =~ /\A0+\z/
424       }
425     end
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"
438       chunks = []
440       z = Zlib::Inflate.new
442       z.inflate(zeros) do |chunk|
443         chunks << chunk
444         break
445       end
447       out = z.inflate nil
449       assert_equal 100_000 - chunks.first.length, out.length
450     end
452     def test_inflate_dictionary
453       dictionary = "foo"
455       deflate = Zlib::Deflate.new
456       deflate.set_dictionary dictionary
457       compressed = deflate.deflate "foofoofoo", Zlib::FINISH
458       deflate.close
460       out = nil
461       inflate = Zlib::Inflate.new
463       begin
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 ""
470       end
472       assert_equal "foofoofoo", out
473     end
475     def test_sync
476       z = Zlib::Deflate.new
477       s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
478       z.avail_out = 0
479       EnvUtil.suppress_warning {z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)}
480       s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
481       z.avail_out = 0
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))
500     end
502     def test_set_dictionary
503       z = Zlib::Inflate.new
504       assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
505       z.close
506     end
508     def test_multithread_deflate
509       zd = Zlib::Deflate.new
511       s = "x" * 10000
512       (0...10).map do |x|
513         Thread.new do
514           1000.times { zd.deflate(s) }
515         end
516       end.each do |th|
517         th.join
518       end
519     ensure
520       zd&.finish
521       zd&.close
522     end
524     def test_multithread_inflate
525       zi = Zlib::Inflate.new
527       s = Zlib.deflate("x" * 10000)
528       (0...10).map do |x|
529         Thread.new do
530           1000.times { zi.inflate(s) }
531         end
532       end.each do |th|
533         th.join
534       end
535     ensure
536       zi&.finish
537       zi&.close
538     end
540     def test_recursive_deflate
541       original_gc_stress = GC.stress
542       GC.stress = true
543       zd = Zlib::Deflate.new
545       s = SecureRandom.random_bytes(1024**2)
546       assert_raise(Zlib::InProgressError) do
547         zd.deflate(s) do
548           zd.deflate(s)
549         end
550       end
551     ensure
552       GC.stress = original_gc_stress
553       zd&.finish
554       zd&.close
555     end
557     def test_recursive_inflate
558       original_gc_stress = GC.stress
559       GC.stress = true
560       zi = Zlib::Inflate.new
562       s = Zlib.deflate(SecureRandom.random_bytes(1024**2))
564       assert_raise(Zlib::InProgressError) do
565         zi.inflate(s) do
566           zi.inflate(s)
567         end
568       end
569     ensure
570       GC.stress = original_gc_stress
571       zi&.close
572     end
573   end
575   class TestZlibGzipFile < Test::Unit::TestCase
576     def test_gzip_reader_zcat
577       Tempfile.create("test_zlib_gzip_file_to_io") {|t|
578         t.binmode
579         gz = Zlib::GzipWriter.new(t)
580         gz.print("foo")
581         gz.close
582         File.open(t.path, 'ab') do |f|
583           gz = Zlib::GzipWriter.new(f)
584           gz.print("bar")
585           gz.close
586         end
588         results = []
589         File.open(t.path, 'rb') do |f|
590           Zlib::GzipReader.zcat(f) do |str|
591             results << str
592           end
593         end
594         assert_equal(["foo", "bar"], results)
596         results = File.open(t.path, 'rb') do |f|
597           Zlib::GzipReader.zcat(f)
598         end
599         assert_equal("foobar", results)
600       }
601     end
603     def test_to_io
604       Tempfile.create("test_zlib_gzip_file_to_io") {|t|
605         t.close
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)
610         end
611       }
612     end
614     def test_crc
615       Tempfile.create("test_zlib_gzip_file_crc") {|t|
616         t.close
617         Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
619         Zlib::GzipReader.open(t.path) do |f|
620           f.read
621           assert_equal(0x8c736521, f.crc)
622         end
623       }
624     end
626     def test_mtime
627       tim = Time.now
629       Tempfile.create("test_zlib_gzip_file_mtime") {|t|
630         t.close
631         Zlib::GzipWriter.open(t.path) do |gz|
632           gz.mtime = -1
633           gz.mtime = tim
634           gz.print("foo")
635           gz.flush
636           assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
637         end
639         Zlib::GzipReader.open(t.path) do |f|
640           assert_equal(tim.to_i, f.mtime.to_i)
641         end
642       }
643     end
645     def test_zero_mtime
646       sio = StringIO.new
647       gz = Zlib::GzipWriter.new(sio)
648       gz.mtime = 0
649       gz.write("Hi")
650       gz.close
651       reading_io = StringIO.new(sio.string)
652       reader = Zlib::GzipReader.new(reading_io)
653       assert_equal(0, reader.mtime.to_i)
654     end
656     def test_level
657       Tempfile.create("test_zlib_gzip_file_level") {|t|
658         t.close
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)
663         end
664       }
665     end
667     def test_os_code
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)
673         end
674       }
675     end
677     def test_orig_name
678       Tempfile.create("test_zlib_gzip_file_orig_name") {|t|
679         t.close
680         Zlib::GzipWriter.open(t.path) do |gz|
681           gz.orig_name = "foobarbazqux\0quux"
682           gz.print("foo")
683           gz.flush
684           assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
685         end
687         Zlib::GzipReader.open(t.path) do |f|
688           assert_equal("foobarbazqux", f.orig_name)
689         end
690       }
691     end
693     def test_comment
694       Tempfile.create("test_zlib_gzip_file_comment") {|t|
695         t.close
696         Zlib::GzipWriter.open(t.path) do |gz|
697           gz.comment = "foobarbazqux\0quux"
698           gz.print("foo")
699           gz.flush
700           assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
701         end
703         Zlib::GzipReader.open(t.path) do |f|
704           assert_equal("foobarbazqux", f.comment)
705         end
706       }
707     end
709     def test_lineno
710       Tempfile.create("test_zlib_gzip_file_lineno") {|t|
711         t.close
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])
717           f.lineno = 1000
718           assert_equal([1000, "baz\n"], [f.lineno, f.gets])
719           assert_equal([1001, "qux\n"], [f.lineno, f.gets])
720         end
721       }
722     end
724     def test_closed_p
725       Tempfile.create("test_zlib_gzip_file_closed_p") {|t|
726         t.close
727         Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
729         Zlib::GzipReader.open(t.path) do |f|
730           assert_equal(false, f.closed?)
731           f.read
732           assert_equal(false, f.closed?)
733           f.close
734           assert_equal(true, f.closed?)
735         end
736       }
737     end
739     def test_sync
740       Tempfile.create("test_zlib_gzip_file_sync") {|t|
741         t.close
742         Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
744         Zlib::GzipReader.open(t.path) do |f|
745           f.sync = true
746           assert_equal(true, f.sync)
747           f.read
748           f.sync = false
749           assert_equal(false, f.sync)
750         end
751       }
752     end
754     def test_pos
755       Tempfile.create("test_zlib_gzip_file_pos") {|t|
756         t.close
757         Zlib::GzipWriter.open(t.path) do |gz|
758           gz.print("foo")
759           gz.flush
760           assert_equal(3, gz.tell)
761         end
762       }
763     end
765     def test_path
766       Tempfile.create("test_zlib_gzip_file_path") {|t|
767         t.close
769         gz = Zlib::GzipWriter.open(t.path)
770         gz.print("foo")
771         assert_equal(t.path, gz.path)
772         gz.close
773         assert_equal(t.path, gz.path)
775         Zlib::GzipReader.open(t.path) do |f|
776           assert_equal(t.path, f.path)
777           f.close
778           assert_equal(t.path, f.path)
779         end
781         s = "".dup
782         sio = StringIO.new(s)
783         gz = Zlib::GzipWriter.new(sio)
784         gz.print("foo")
785         assert_raise(NoMethodError) { gz.path }
786         gz.close
788         sio = StringIO.new(s)
789         gz = Zlib::GzipReader.new(sio)
790         assert_raise(NoMethodError) { gz.path }
791         gz.close
792       }
793     end
795     if defined? File::TMPFILE
796       def test_path_tmpfile
797         sio = StringIO.new("".dup, 'w')
798         gz = Zlib::GzipWriter.new(sio)
799         gz.write "hi"
800         gz.close
802         File.open(Dir.mktmpdir, File::RDWR | File::TMPFILE) do |io|
803           io.write sio.string
804           io.rewind
806           gz0 = Zlib::GzipWriter.new(io)
807           assert_raise(NoMethodError) { gz0.path }
809           gz1 = Zlib::GzipReader.new(io)
810           assert_raise(NoMethodError) { gz1.path }
811           gz0.close
812           gz1.close
813         end
814       rescue Errno::EINVAL
815         omit 'O_TMPFILE not supported (EINVAL)'
816       rescue Errno::EISDIR
817         omit 'O_TMPFILE not supported (EISDIR)'
818       rescue Errno::EOPNOTSUPP
819         omit 'O_TMPFILE not supported (EOPNOTSUPP)'
820       end
821     end
822   end
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"
826     def test_read0
827       assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
828     end
830     def test_ungetc
831       s = "".dup
832       w = Zlib::GzipWriter.new(StringIO.new(s))
833       w << (1...1000).to_a.inspect
834       w.close
835       r = Zlib::GzipReader.new(StringIO.new(s))
836       r.read(100)
837       r.ungetc ?a
838       assert_nothing_raised("[ruby-dev:24060]") {
839         r.read(100)
840         r.read
841         r.close
842       }
843     end
845     def test_ungetc_paragraph
846       s = "".dup
847       w = Zlib::GzipWriter.new(StringIO.new(s))
848       w << "abc"
849       w.close
850       r = Zlib::GzipReader.new(StringIO.new(s))
851       r.ungetc ?\n
852       assert_equal("abc", r.gets(""))
853       assert_nothing_raised("[ruby-dev:24065]") {
854         r.read
855         r.close
856       }
857     end
859     def test_ungetc_at_start_of_file
860       s = "".dup
861       w = Zlib::GzipWriter.new(StringIO.new(s))
862       w << "abc"
863       w.close
864       r = Zlib::GzipReader.new(StringIO.new(s))
866       r.ungetc ?!
868       assert_equal(-1, r.pos, "[ruby-core:81488][Bug #13616]")
869     end
871     def test_open
872       Tempfile.create("test_zlib_gzip_reader_open") {|t|
873         t.close
874         e = assert_raise(Zlib::GzipFile::Error) {
875           Zlib::GzipReader.open(t.path)
876         }
877         assert_equal("not in gzip format", e.message)
878         assert_nil(e.input)
879         open(t.path, "wb") {|f| f.write("foo")}
880         e = assert_raise(Zlib::GzipFile::Error) {
881           Zlib::GzipReader.open(t.path)
882         }
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)
888         }
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)
899         begin
900           assert_equal("foo", f.read)
901         ensure
902           f.close
903         end
904       }
905     end
907     def test_rewind
908       bug8467 = '[ruby-core:55220] [Bug #8467]'
909       Tempfile.create("test_zlib_gzip_reader_rewind") {|t|
910         t.close
911         Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
913         Zlib::GzipReader.open(t.path) do |f|
914           assert_equal("foo", f.read)
915           f.rewind
916           assert_equal("foo", f.read)
918           f.rewind
919           bytes = []
920           f.each_byte { |b| bytes << b }
921           assert_equal "foo".bytes.to_a, bytes, '[Bug #10101]'
922         end
923         open(t.path, "rb") do |f|
924           gz = Zlib::GzipReader.new(f)
925           gz.rewind
926           assert_equal(["foo"], gz.to_a, bug8467)
927         end
928       }
929     end
931     def test_unused
932       Tempfile.create("test_zlib_gzip_reader_unused") {|t|
933         t.close
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)
942         end
943       }
944     end
946     def test_unused2
947       zio = StringIO.new
949       io = Zlib::GzipWriter.new zio
950       io.write 'aaaa'
951       io.finish
953       io = Zlib::GzipWriter.new zio
954       io.write 'bbbb'
955       io.finish
957       zio.rewind
959       io = Zlib::GzipReader.new zio
960       assert_equal('aaaa', io.read)
961       unused = io.unused
962       assert_equal(24, unused.bytesize)
963       io.finish
965       zio.pos -= unused.length
967       io = Zlib::GzipReader.new zio
968       assert_equal('bbbb', io.read)
969       assert_equal(nil, io.unused)
970       io.finish
971     end
973     def test_read
974       Tempfile.create("test_zlib_gzip_reader_read") {|t|
975         t.close
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)
982         end
983       }
984     end
986     def test_readpartial
987       Tempfile.create("test_zlib_gzip_reader_readpartial") {|t|
988         t.close
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)))
993         end
995         Zlib::GzipReader.open(t.path) do |f|
996           s = "".dup
997           f.readpartial(3, s)
998           assert("foo".start_with?(s))
1000           assert_raise(ArgumentError) { f.readpartial(-1) }
1001         end
1002       }
1003     end
1005     def test_getc
1006       Tempfile.create("test_zlib_gzip_reader_getc") {|t|
1007         t.close
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) }
1012           assert_nil(f.getc)
1013         end
1014       }
1015     end
1017     def test_getbyte
1018       Tempfile.create("test_zlib_gzip_reader_getbyte") {|t|
1019         t.close
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)
1025         end
1026       }
1027     end
1029     def test_readchar
1030       Tempfile.create("test_zlib_gzip_reader_readchar") {|t|
1031         t.close
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 }
1037         end
1038       }
1039     end
1041     def test_each_byte
1042       Tempfile.create("test_zlib_gzip_reader_each_byte") {|t|
1043         t.close
1044         Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
1046         Zlib::GzipReader.open(t.path) do |f|
1047           a = []
1048           f.each_byte {|c| a << c }
1049           assert_equal("foobar".each_byte.to_a, a)
1050         end
1051       }
1052     end
1054     def test_gets
1055       Tempfile.create("test_zlib_gzip_reader_gets") {|t|
1056         t.close
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)
1063           assert_nil(f.gets)
1064         end
1066         Zlib::GzipReader.open(t.path) do |f|
1067           assert_equal("foo\nbar\nbaz\n", f.gets(nil))
1068         end
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))
1075           assert_nil(f.gets)
1076         end
1077       }
1078     end
1080     def test_gets2
1081       Tempfile.create("test_zlib_gzip_reader_gets2") {|t|
1082         t.close
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)
1090           assert_nil(f.gets)
1091         end
1093         Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
1094           assert_equal(ustrs.join(''), f.gets(nil))
1095         end
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))
1102           assert_nil(f.gets)
1103         end
1104       }
1105     end
1107     def test_readline
1108       Tempfile.create("test_zlib_gzip_reader_readline") {|t|
1109         t.close
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 }
1117         end
1118       }
1119     end
1121     def test_each
1122       Tempfile.create("test_zlib_gzip_reader_each") {|t|
1123         t.close
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) }
1129         end
1130       }
1131     end
1133     def test_readlines
1134       Tempfile.create("test_zlib_gzip_reader_readlines") {|t|
1135         t.close
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)
1140         end
1141       }
1142     end
1144     def test_reader_wrap
1145       Tempfile.create("test_zlib_gzip_reader_wrap") {|t|
1146         t.close
1147         Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
1148         f = open(t.path)
1149         f.binmode
1150         assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
1151         assert(f.closed?)
1152       }
1153     end
1155     def test_corrupted_header
1156       gz = Zlib::GzipWriter.new(StringIO.new(s = "".dup))
1157       gz.orig_name = "X"
1158       gz.comment = "Y"
1159       gz.print("foo")
1160       gz.finish
1161       # 14: magic(2) + method(1) + flag(1) + mtime(4) + exflag(1) + os(1) + orig_name(2) + comment(2)
1162       1.upto(14) do |idx|
1163         assert_raise(Zlib::GzipFile::Error, idx) do
1164           Zlib::GzipReader.new(StringIO.new(s[0, idx])).read
1165         end
1166       end
1167     end
1169     def test_encoding
1170       Tempfile.create("test_zlib_gzip_reader_encoding") {|t|
1171         t.binmode
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)
1177           gz.read
1178         end
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)
1185       }
1186     end
1188     def test_double_close
1189       Tempfile.create("test_zlib_gzip_reader_close") {|t|
1190         t.binmode
1191         content = "foo"
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 }
1197       }
1198     end
1200   end
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 }
1208     end
1210     def test_open
1211       assert_raise(ArgumentError) { Zlib::GzipWriter.open }
1213       Tempfile.create("test_zlib_gzip_writer_open") {|t|
1214         t.close
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)
1219         begin
1220           f.print("bar")
1221         ensure
1222           f.close
1223         end
1224         assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1226         assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
1227       }
1228     end
1230     def test_write
1231       Tempfile.create("test_zlib_gzip_writer_write") {|t|
1232         t.close
1233         Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
1234         assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1236         o = Object.new
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 })
1240       }
1241     end
1243     def test_putc
1244       Tempfile.create("test_zlib_gzip_writer_putc") {|t|
1245         t.close
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
1250       }
1251     end
1253     def test_puts
1254       Tempfile.create("test_zlib_gzip_writer_puts") {|t|
1255         t.close
1256         Zlib::GzipWriter.open(t.path) {|gz| gz.puts("foo") }
1257         assert_equal("foo\n", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1258       }
1259     end
1261     def test_writer_wrap
1262       Tempfile.create("test_zlib_gzip_writer_wrap") {|t|
1263         t.binmode
1264         Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
1265         assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
1266       }
1267     end
1269     def test_double_close
1270       Tempfile.create("test_zlib_gzip_reader_close") {|t|
1271         t.binmode
1272         w = Zlib::GzipWriter.wrap(t)
1273         assert_nothing_raised { w.close }
1274         assert_nothing_raised { w.close }
1275       }
1276     end
1278     def test_zlib_writer_buffered_write
1279       bug15356 = '[ruby-core:90346] [Bug #15356]'.freeze
1280       fixes = 'r61631 (commit a55abcc0ca6f628fc05304f81e5a044d65ab4a68)'.freeze
1281       ary = []
1282       def ary.write(*args)
1283         self.concat(args)
1284       end
1285       gz = Zlib::GzipWriter.new(ary)
1286       gz.write(bug15356)
1287       gz.write("\n")
1288       gz.write(fixes)
1289       gz.close
1290       assert_not_predicate ary, :empty?
1291       exp = [ bug15356, fixes ]
1292       assert_equal exp, Zlib.gunzip(ary.join('')).split("\n")
1293     end
1294   end
1296   class TestZlib < Test::Unit::TestCase
1297     def test_version
1298       assert_instance_of(String, Zlib.zlib_version)
1299     end
1301     def test_adler32
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")
1308         t.rewind
1309         assert_equal(0x02820145, Zlib.adler32(t))
1311         t.rewind
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)
1316         t.rewind
1317         assert_equal(0x8a62c964, Zlib.adler32(t))
1318       }
1319     end
1321     def test_adler32_combine
1322       one = Zlib.adler32("fo")
1323       two = Zlib.adler32("o")
1324       begin
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"
1331         end
1332         raise $!
1333       end
1334     end
1336     def test_crc32
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")
1343         t.rewind
1344         assert_equal(0x8c736521, Zlib.crc32(t))
1346         t.rewind
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)
1351         t.rewind
1352         assert_equal(0x07f0d68f, Zlib.crc32(t))
1353       }
1354     end
1356     def test_crc32_combine
1357       one = Zlib.crc32("fo")
1358       two = Zlib.crc32("o")
1359       begin
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"
1366         end
1367         raise $!
1368       end
1369     end
1371     def test_crc_table
1372       t = Zlib.crc_table
1373       assert_instance_of(Array, t)
1374       t.each {|x| assert_kind_of(Integer, x) }
1375     end
1377     def test_inflate
1378       s = Zlib::Deflate.deflate("foo")
1379       z = Zlib::Inflate.new
1380       s = z.inflate(s)
1381       s << z.inflate(nil)
1382       assert_equal("foo", s)
1383       z.inflate("foo") # ???
1384       z << "foo" # ???
1385     end
1387     def test_deflate
1388       s = Zlib::Deflate.deflate("foo")
1389       assert_equal("foo", Zlib::Inflate.inflate(s))
1391       assert_raise(Zlib::StreamError) { Zlib::Deflate.deflate("foo", 10000) }
1392     end
1394     def test_deflate_stream
1395       r = Random.new 0
1397       deflated = ''.dup
1399       Zlib.deflate(r.bytes(20000)) do |chunk|
1400         deflated << chunk
1401       end
1403       assert_equal 20016, deflated.length
1404     end
1406     def test_gzip
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
1430     end
1432     def test_gunzip
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) }
1450     end
1452     def test_gunzip_no_memory_leak
1453       assert_no_memory_leak(%[-rzlib], "#{<<~"{#"}", "#{<<~'};'}")
1454       d = Zlib.gzip("data")
1455       {#
1456         10_000.times {Zlib.gunzip(d)}
1457       };
1458     end
1459   end