Skip to content

Commit

Permalink
Workaround: Fix test failures on Ubuntu jammy s390x.
Browse files Browse the repository at this point in the history
This commit fixes the test failures on the zlib in Ubuntu jammy s390x.

According to the <https://packages.ubuntu.com/jammy-updates/zlib1g> -
`zlib_1.2.11.dfsg-2ubuntu9.2.debian.tar.xz`,
the zlib deb package is applying the patch 410.patch (madler/zlib#410), and
configured by `./configure --dfltcc` on Ubuntu jammy s390x. The `--dfltcc`
is to enable the deflate algorithm in hardware.

It produces a different (but still valid) compressed byte stream, and causes
the test failures in ruby/zlib. As a workaround, set the environment variable
`DFLTCC=0` disabling the implementation in zlib on s390x to the failing tests.

Note we need to test in a child Ruby process with `assert_separately` to test
on the `DFLTCC=0` set by the parent Ruby process.
  • Loading branch information
junaruga committed Sep 25, 2023
1 parent 4284bd0 commit f132c58
Showing 1 changed file with 97 additions and 70 deletions.
167 changes: 97 additions & 70 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@
end

if defined? Zlib
class TestZlibDeflate < Test::Unit::TestCase
class Zlib::TestCase < Test::Unit::TestCase
def child_env
return @child_env unless @child_env.nil?

child_env = {}
child_env['DFLTCC'] = '0' if RUBY_PLATFORM =~ /s390x/
@child_env = child_env
@child_env
end
end

class TestZlibDeflate < Zlib::TestCase
def test_initialize
z = Zlib::Deflate.new
s = z.deflate("foo", Zlib::FINISH)
Expand Down Expand Up @@ -44,59 +55,63 @@ def test_deflate
end

def test_deflate_chunked
original = ''.dup
chunks = []
r = Random.new 0

z = Zlib::Deflate.new

2.times do
input = r.bytes(20000)
original << input
z.deflate(input) do |chunk|
chunks << chunk
assert_separately([child_env, '-rzlib'], <<~'end;')
original = ''.dup
chunks = []
r = Random.new 0
z = Zlib::Deflate.new
2.times do
input = r.bytes(20000)
original << input
z.deflate(input) do |chunk|
chunks << chunk
end
end
end
assert_equal [16384, 16384],
chunks.map { |chunk| chunk.length }
assert_equal [16384, 16384],
chunks.map { |chunk| chunk.length }
final = z.finish
final = z.finish
assert_equal 7253, final.length
assert_equal 7253, final.length
chunks << final
all = chunks.join
chunks << final
all = chunks.join
inflated = Zlib.inflate all
inflated = Zlib.inflate all
assert_equal original, inflated
assert_equal original, inflated
end;
end

def test_deflate_chunked_break
chunks = []
r = Random.new 0
assert_separately([child_env, '-rzlib'], <<~'end;')
chunks = []
r = Random.new 0
z = Zlib::Deflate.new
z = Zlib::Deflate.new
input = r.bytes(20000)
z.deflate(input) do |chunk|
chunks << chunk
break
end
input = r.bytes(20000)
z.deflate(input) do |chunk|
chunks << chunk
break
end
assert_equal [16384], chunks.map { |chunk| chunk.length }
assert_equal [16384], chunks.map { |chunk| chunk.length }
final = z.finish
final = z.finish
assert_equal 3632, final.length
assert_equal 3632, final.length
all = chunks.join
all << final
all = chunks.join
all << final
original = Zlib.inflate all
original = Zlib.inflate all
assert_equal input, original
assert_equal input, original
end;
end

def test_addstr
Expand Down Expand Up @@ -247,7 +262,7 @@ def test_close
end
end

class TestZlibInflate < Test::Unit::TestCase
class TestZlibInflate < Zlib::TestCase
def test_class_inflate_dictionary
assert_raise(Zlib::NeedDict) do
Zlib::Inflate.inflate([0x08,0x3C,0x0,0x0,0x0,0x0].pack("c*"))
Expand Down Expand Up @@ -574,7 +589,7 @@ def test_recursive_inflate
end
end

class TestZlibGzipFile < Test::Unit::TestCase
class TestZlibGzipFile < Zlib::TestCase
def test_gzip_reader_zcat
Tempfile.create("test_zlib_gzip_file_to_io") {|t|
t.binmode
Expand Down Expand Up @@ -829,7 +844,7 @@ def test_path_tmpfile
end
end

class TestZlibGzipReader < Test::Unit::TestCase
class TestZlibGzipReader < Zlib::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))
Expand Down Expand Up @@ -952,30 +967,32 @@ def test_unused
end

def test_unused2
zio = StringIO.new
assert_separately([child_env, '-rzlib', '-rstringio'], <<~'end;')
zio = StringIO.new
io = Zlib::GzipWriter.new zio
io.write 'aaaa'
io.finish
io = Zlib::GzipWriter.new zio
io.write 'aaaa'
io.finish
io = Zlib::GzipWriter.new zio
io.write 'bbbb'
io.finish
io = Zlib::GzipWriter.new zio
io.write 'bbbb'
io.finish
zio.rewind
zio.rewind
io = Zlib::GzipReader.new zio
assert_equal('aaaa', io.read)
unused = io.unused
assert_equal(24, unused.bytesize)
io.finish
io = Zlib::GzipReader.new zio
assert_equal('aaaa', io.read)
unused = io.unused
assert_equal(24, unused.bytesize)
io.finish
zio.pos -= unused.length
zio.pos -= unused.length
io = Zlib::GzipReader.new zio
assert_equal('bbbb', io.read)
assert_equal(nil, io.unused)
io.finish
io = Zlib::GzipReader.new zio
assert_equal('bbbb', io.read)
assert_equal(nil, io.unused)
io.finish
end;
end

def test_read
Expand Down Expand Up @@ -1207,7 +1224,7 @@ def test_double_close

end

class TestZlibGzipWriter < Test::Unit::TestCase
class TestZlibGzipWriter < Zlib::TestCase
def test_invalid_new
assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
Expand Down Expand Up @@ -1301,7 +1318,7 @@ def ary.write(*args)
end
end

class TestZlib < Test::Unit::TestCase
class TestZlib < Zlib::TestCase
def test_version
assert_instance_of(String, Zlib.zlib_version)
end
Expand Down Expand Up @@ -1402,36 +1419,46 @@ def test_deflate
end

def test_deflate_stream
r = Random.new 0
assert_separately([child_env, '-rzlib'], <<~'end;')
r = Random.new 0
deflated = ''.dup
deflated = ''.dup
Zlib.deflate(r.bytes(20000)) do |chunk|
deflated << chunk
end
Zlib.deflate(r.bytes(20000)) do |chunk|
deflated << chunk
end
assert_equal 20016, deflated.length
assert_equal 20016, deflated.length
end;
end

def test_gzip
actual = Zlib.gzip("foo".freeze)
actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
actual[9] = "\xff" # replace OS
expected = %w[1f8b08000000000000ff4bcbcf07002165738c03000000].pack("H*")
assert_equal expected, actual
assert_separately([child_env, '-rzlib'], <<~'end;')
actual = Zlib.gzip("foo".freeze)
actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
actual[9] = "\xff" # replace OS
expected = %w[1f8b08000000000000ff4bcbcf07002165738c03000000].pack("H*")
assert_equal expected, actual
end;
end

def test_gzip_level_0
actual = Zlib.gzip("foo".freeze, level: 0)
actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
actual[9] = "\xff" # replace OS
expected = %w[1f8b08000000000000ff010300fcff666f6f2165738c03000000].pack("H*")
assert_equal expected, actual
end

def test_gzip_level_9
actual = Zlib.gzip("foo".freeze, level: 9)
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_gzip_level_9_filtered
actual = Zlib.gzip("foo".freeze, level: 9, strategy: Zlib::FILTERED)
actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
actual[9] = "\xff" # replace OS
Expand Down

0 comments on commit f132c58

Please sign in to comment.