diff --git a/test/frame_compression.jl b/test/frame_compression.jl index 2db79ee..e8d40f7 100644 --- a/test/frame_compression.jl +++ b/test/frame_compression.jl @@ -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") diff --git a/test/hc_compression.jl b/test/hc_compression.jl index f00036f..7cd0d51 100644 --- a/test/hc_compression.jl +++ b/test/hc_compression.jl @@ -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 diff --git a/test/lz4_compression.jl b/test/lz4_compression.jl index 0ba0420..0580fad 100644 --- a/test/lz4_compression.jl +++ b/test/lz4_compression.jl @@ -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 @@ -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