2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "active_support/core_ext/object/blank"
|
2008-06-02 18:42:10 -04:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class GzipTest < ActiveSupport::TestCase
|
2008-06-02 18:42:10 -04:00
|
|
|
def test_compress_should_decompress_to_the_same_value
|
|
|
|
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World"))
|
2012-09-22 05:07:50 -04:00
|
|
|
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", Zlib::NO_COMPRESSION))
|
|
|
|
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", Zlib::BEST_SPEED))
|
|
|
|
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", Zlib::BEST_COMPRESSION))
|
|
|
|
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", nil, Zlib::FILTERED))
|
|
|
|
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", nil, Zlib::HUFFMAN_ONLY))
|
|
|
|
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", nil, nil))
|
2008-06-02 18:42:10 -04:00
|
|
|
end
|
2011-02-09 04:23:45 -05:00
|
|
|
|
|
|
|
def test_compress_should_return_a_binary_string
|
2016-08-06 12:03:25 -04:00
|
|
|
compressed = ActiveSupport::Gzip.compress("")
|
2011-02-09 04:23:45 -05:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Encoding.find("binary"), compressed.encoding
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not compressed.blank?, "a compressed blank string should not be blank"
|
2011-02-09 04:23:45 -05:00
|
|
|
end
|
2012-09-22 05:07:50 -04:00
|
|
|
|
|
|
|
def test_compress_should_return_gzipped_string_by_compression_level
|
2016-10-28 23:05:58 -04:00
|
|
|
source_string = "Hello World" * 100
|
2012-09-22 05:07:50 -04:00
|
|
|
|
|
|
|
gzipped_by_speed = ActiveSupport::Gzip.compress(source_string, Zlib::BEST_SPEED)
|
|
|
|
assert_equal 1, Zlib::GzipReader.new(StringIO.new(gzipped_by_speed)).level
|
|
|
|
|
|
|
|
gzipped_by_best_compression = ActiveSupport::Gzip.compress(source_string, Zlib::BEST_COMPRESSION)
|
|
|
|
assert_equal 9, Zlib::GzipReader.new(StringIO.new(gzipped_by_best_compression)).level
|
|
|
|
|
|
|
|
assert_equal true, (gzipped_by_best_compression.bytesize < gzipped_by_speed.bytesize)
|
|
|
|
end
|
2017-02-24 17:26:54 -05:00
|
|
|
|
|
|
|
def test_decompress_checks_crc
|
|
|
|
compressed = ActiveSupport::Gzip.compress("Hello World")
|
|
|
|
first_crc_byte_index = compressed.bytesize - 8
|
|
|
|
compressed.setbyte(first_crc_byte_index, compressed.getbyte(first_crc_byte_index) ^ 0xff)
|
|
|
|
|
|
|
|
assert_raises(Zlib::GzipFile::CRCError) do
|
|
|
|
ActiveSupport::Gzip.decompress(compressed)
|
|
|
|
end
|
|
|
|
end
|
2011-02-09 05:36:44 -05:00
|
|
|
end
|