Skip to content

Commit

Permalink
add GC.@preserve to tests (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 authored Jun 23, 2024
1 parent 55c2c42 commit 4f14cf1
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 88 deletions.
48 changes: 26 additions & 22 deletions test/frame_compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,32 @@ erat ex bibendum ipsum, sed varius ipsum ipsum vitae dui.
end

@testset "Errors" begin
input = Memory(Vector{UInt8}(text))
output = Memory(Vector{UInt8}(undef, 1280))
not_initialized = LZ4FrameCompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, input, output, Error()) == (0, 0, :error)

compressed = transcode(LZ4FrameCompressor, Vector{UInt8}(text))
corrupted = copy(compressed)
corrupted[1] = 0x00
file = IOBuffer(corrupted)
stream = LZ4FrameDecompressorStream(file)
@test_throws CodecLz4.LZ4Exception read(stream)
@test_throws ArgumentError read(stream)

output = Memory(Vector{UInt8}(undef, 1))
compressor = LZ4FrameCompressor()
@test_nowarn TranscodingStreams.initialize(compressor)
@test TranscodingStreams.startproc(compressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(compressor, input, output, err) == (0, 0, :error)
@test err[].msg == "Output buffer too small for header."
@test_nowarn TranscodingStreams.finalize(compressor)
input_data = Vector{UInt8}(text)
output_data = Vector{UInt8}(undef, 1280)
GC.@preserve input_data output_data begin
input = Memory(pointer(input_data), length(input_data))
output = Memory(pointer(output_data), length(output_data))
not_initialized = LZ4FrameCompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, input, output, Error()) == (0, 0, :error)

compressed = transcode(LZ4FrameCompressor, Vector{UInt8}(text))
corrupted = copy(compressed)
corrupted[1] = 0x00
file = IOBuffer(corrupted)
stream = LZ4FrameDecompressorStream(file)
@test_throws CodecLz4.LZ4Exception read(stream)
@test_throws ArgumentError read(stream)

output = Memory(pointer(output_data), 1)
compressor = LZ4FrameCompressor()
@test_nowarn TranscodingStreams.initialize(compressor)
@test TranscodingStreams.startproc(compressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(compressor, input, output, err) == (0, 0, :error)
@test err[].msg == "Output buffer too small for header."
@test_nowarn TranscodingStreams.finalize(compressor)
end

codec = LZ4FrameDecompressor()
@test_throws CodecLz4.LZ4Exception transcode(codec, "not properly formatted")
Expand Down
40 changes: 22 additions & 18 deletions test/hc_compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,28 @@

@testset "Errors" begin
# Uninitialized
input = Memory(Vector{UInt8}(text))
output = Memory(Vector{UInt8}(undef, 1280))
not_initialized = LZ4HCCompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, input, output, Error()) == (0, 0, :error)

# Compression into too-small buffer
output = Memory(Vector{UInt8}(undef, 1))
compressor = LZ4HCCompressor()
try
@test_nowarn TranscodingStreams.initialize(compressor)
@test TranscodingStreams.startproc(compressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(compressor, input, output, err) == (0, 0, :error)

@test err[] isa BoundsError
finally
TranscodingStreams.finalize(compressor)
input_data = Vector{UInt8}(text)
output_data = Vector{UInt8}(undef, 1280)
GC.@preserve input_data output_data begin
input = Memory(pointer(input_data), length(input_data))
output = Memory(pointer(output_data), length(output_data))
not_initialized = LZ4HCCompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, input, output, Error()) == (0, 0, :error)

# Compression into too-small buffer
output = Memory(pointer(output_data), 1)
compressor = LZ4HCCompressor()
try
@test_nowarn TranscodingStreams.initialize(compressor)
@test TranscodingStreams.startproc(compressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(compressor, input, output, err) == (0, 0, :error)

@test err[] isa BoundsError
finally
TranscodingStreams.finalize(compressor)
end
end

# Block size too large
Expand Down
115 changes: 67 additions & 48 deletions test/lz4_compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,23 @@

@testset "Errors" begin
@testset "Uninitialized" begin
input = Memory(Vector{UInt8}(text))
output = Memory(Vector{UInt8}(undef, 1280))
not_initialized = LZ4FastCompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, input, output, Error()) == (0, 0, :error)

compressed = Memory(transcode(LZ4FastCompressor, text))
not_initialized = LZ4SafeDecompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, compressed, output, Error()) == (0, 0, :error)
input_data = Vector{UInt8}(text)
output_data = Vector{UInt8}(undef, 1280)
GC.@preserve input_data output_data begin
input = Memory(pointer(input_data), length(input_data))
output = Memory(pointer(output_data), length(output_data))
not_initialized = LZ4FastCompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, input, output, Error()) == (0, 0, :error)

compressed_data = transcode(LZ4FastCompressor, text)
GC.@preserve compressed_data begin
compressed = Memory(pointer(compressed_data), length(compressed_data))
not_initialized = LZ4SafeDecompressor()
@test TranscodingStreams.startproc(not_initialized, :read, Error()) == :error
@test TranscodingStreams.process(not_initialized, compressed, output, Error()) == (0, 0, :error)
end
end
end

@testset "Bad Input" begin
Expand All @@ -85,50 +92,62 @@

@testset "Bad Buffer Size" begin
# Decompression with too-small block_size
output = Memory(Vector{UInt8}(undef, 1024))
compressed = transcode(LZ4FastCompressor, text)
decompressor = LZ4SafeDecompressor(; block_size = 200)
try
@test_nowarn TranscodingStreams.initialize(decompressor)
@test TranscodingStreams.startproc(decompressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(decompressor, Memory(compressed), output, err) == (0, 0, :error)

@test err[] isa CodecLz4.LZ4Exception
@test err[].msg == "Decompression failed."
finally
TranscodingStreams.finalize(decompressor)
output_data = Vector{UInt8}(undef, 1024)
compressed_data = transcode(LZ4FastCompressor, text)
GC.@preserve output_data compressed begin
output = Memory(pointer(output_data), length(output_data))
compressed = Memory(pointer(compressed_data), length(compressed_data))
decompressor = LZ4SafeDecompressor(; block_size = 200)
try
@test_nowarn TranscodingStreams.initialize(decompressor)
@test TranscodingStreams.startproc(decompressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(decompressor, compressed, output, err) == (0, 0, :error)

@test err[] isa CodecLz4.LZ4Exception
@test err[].msg == "Decompression failed."
finally
TranscodingStreams.finalize(decompressor)
end
end

# Compression into too-small buffer
input = Memory(Vector{UInt8}(text))
output = Memory(Vector{UInt8}(undef, 1))
compressor = LZ4FastCompressor()

try
@test_nowarn TranscodingStreams.initialize(compressor)
@test TranscodingStreams.startproc(compressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(compressor, input, output, err) == (0, 0, :error)

@test err[] isa BoundsError
finally
TranscodingStreams.finalize(compressor)
input_data = Vector{UInt8}(text)
output_data = Vector{UInt8}(undef, 1)
GC.@preserve input_data output_data begin
input = Memory(pointer(input_data), length(input_data))
output = Memory(pointer(output_data), length(output_data))
compressor = LZ4FastCompressor()

try
@test_nowarn TranscodingStreams.initialize(compressor)
@test TranscodingStreams.startproc(compressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(compressor, input, output, err) == (0, 0, :error)

@test err[] isa BoundsError
finally
TranscodingStreams.finalize(compressor)
end
end

# Decompression into too-small buffer
output = Memory(Vector{UInt8}(undef, 1))
compressed = transcode(LZ4FastCompressor, text)
decompressor = LZ4SafeDecompressor()
try
@test_nowarn TranscodingStreams.initialize(decompressor)
@test TranscodingStreams.startproc(decompressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(decompressor, Memory(compressed), output, err) == (0, 0, :error)

@test err[] isa BoundsError
finally
TranscodingStreams.finalize(decompressor)
output_data = Vector{UInt8}(undef, 1)
compressed_data = transcode(LZ4FastCompressor, text)
GC.@preserve output_data compressed_data begin
output = Memory(pointer(output_data), length(output_data))
compressed = Memory(pointer(compressed_data), length(compressed_data))
decompressor = LZ4SafeDecompressor()
try
@test_nowarn TranscodingStreams.initialize(decompressor)
@test TranscodingStreams.startproc(decompressor, :read, Error()) == :ok
err = Error()
@test TranscodingStreams.process(decompressor, compressed, output, err) == (0, 0, :error)

@test err[] isa BoundsError
finally
TranscodingStreams.finalize(decompressor)
end
end
end
end
Expand Down

0 comments on commit 4f14cf1

Please sign in to comment.