summaryrefslogtreecommitdiff
path: root/test/zlib
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-09 22:45:39 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-09 22:45:39 +0000
commit583b6dc33c1d33237a368d6fe0ef3f779c3d72e4 (patch)
tree348cf0cbd418c4966e00c375bb7a60be419cf8d9 /test/zlib
parent1f63c0fcb834eb0606c80501d82c1e41e020aefd (diff)
Zlib.gzip and Zlib.gunzip [Feature #13020]
Encode and Decode gzip data without creating files. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57035 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/zlib')
-rw-r--r--test/zlib/test_zlib.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb
index 0092daf0d9..60fb84dd0d 100644
--- a/test/zlib/test_zlib.rb
+++ b/test/zlib/test_zlib.rb
@@ -1,3 +1,4 @@
+# coding: us-ascii
# frozen_string_literal: false
require 'test/unit'
require 'stringio'
@@ -1126,5 +1127,50 @@ if defined? Zlib
assert_equal 20016, deflated.length
end
+ def test_gzip
+ actual = Zlib.gzip("foo")
+ actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
+ actual[9] = "\xff" # replace OS
+ expected = %w[1f8b08000000000000ff4bcbcf07002165738c03000000].pack("H*")
+ assert_equal expected, actual
+
+ actual = Zlib.gzip("foo", 0)
+ actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
+ actual[9] = "\xff" # replace OS
+ expected = %w[1f8b08000000000000ff010300fcff666f6f2165738c03000000].pack("H*")
+ assert_equal expected, actual
+
+ actual = Zlib.gzip("foo", 9)
+ actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
+ actual[9] = "\xff" # replace OS
+ expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")
+ assert_equal expected, actual
+
+ actual = Zlib.gzip("foo", 9, Zlib::FILTERED)
+ actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
+ actual[9] = "\xff" # replace OS
+ expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")
+ assert_equal expected, actual
+ end
+
+ def test_gunzip
+ src = %w[1f8b08000000000000034bcbcf07002165738c03000000].pack("H*")
+ assert_equal 'foo', Zlib.gunzip(src)
+
+ src = %w[1f8b08000000000000034bcbcf07002165738c03000001].pack("H*")
+ assert_raise(Zlib::GzipFile::LengthError){ Zlib.gunzip(src) }
+
+ src = %w[1f8b08000000000000034bcbcf07002165738d03000000].pack("H*")
+ assert_raise(Zlib::GzipFile::CRCError){ Zlib.gunzip(src) }
+
+ src = %w[1f8b08000000000000034bcbcf07002165738d030000].pack("H*")
+ assert_raise(Zlib::GzipFile::Error){ Zlib.gunzip(src) }
+
+ src = %w[1f8b08000000000000034bcbcf0700].pack("H*")
+ assert_raise(Zlib::GzipFile::NoFooter){ Zlib.gunzip(src) }
+
+ src = %w[1f8b080000000000000].pack("H*")
+ assert_raise(Zlib::GzipFile::Error){ Zlib.gunzip(src) }
+ end
end
end