Skip to content

Commit

Permalink
test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Oct 5, 2024
1 parent e0c0238 commit b2c4e97
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ end

if isdefined(TranscodingStreams, :pledgeinsize)
function TranscodingStreams.pledgeinsize(codec::ZstdCompressor, insize::Int64, error::Error)::Symbol
if codec.cstream.ptr == C_NULL
Base.error("`startproc` must be called before `pledgeinsize`")

Check warning on line 116 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L114-L116

Added lines #L114 - L116 were not covered by tests
end
srcsize = if signbit(insize)
ZSTD_CONTENTSIZE_UNKNOWN

Check warning on line 119 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L118-L119

Added lines #L118 - L119 were not covered by tests
else
Expand All @@ -129,7 +132,7 @@ end

function TranscodingStreams.process(codec::ZstdCompressor, input::Memory, output::Memory, error::Error)
if codec.cstream.ptr == C_NULL
error("startproc must be called before process")
Base.error("`startproc` must be called before `process`")
end
cstream = codec.cstream
ibuffer_starting_pos = UInt(0)
Expand Down
54 changes: 47 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,47 @@ include("utils.jl")
if isdefined(TranscodingStreams, :pledgeinsize)
# when pledgeinsize is available transcode should save the
# decompressed size in a header
for n in [0:30; 1000; 1000000; 10000000;]
a = transcode(ZstdCompressor, rand(UInt8, n))
@test CodecZstd.find_decompressed_size(a) == n
a = transcode(ZstdCompressor, rand(0x00:0x01, n))
@test CodecZstd.find_decompressed_size(a) == n
a = transcode(ZstdCompressor, zeros(UInt8, n))
@test CodecZstd.find_decompressed_size(a) == n
for n in [0:30; 1000; 1000000;]
v = transcode(ZstdCompressor, rand(UInt8, n))
@test CodecZstd.find_decompressed_size(v) == n
end

# Test what happens if pledgeinsize promise is broken
d1 = zeros(UInt8, 10000)
d2 = zeros(UInt8, 10000)
GC.@preserve d1 d2 begin
@testset "too many bytes" begin
m1 = TranscodingStreams.Memory(pointer(d1), 1000)
m2 = TranscodingStreams.Memory(pointer(d2), 1000)
codec = ZstdCompressor()
e = TranscodingStreams.Error()
@test TranscodingStreams.startproc(codec, :read, e) === :ok
@test TranscodingStreams.pledgeinsize(codec, 10, e) === :ok
@test TranscodingStreams.process(codec, m1, m2, e) === (0, 0, :error)
@test e[] == ErrorException("zstd error: Src size is incorrect")
end
@testset "too few bytes" begin
m1 = TranscodingStreams.Memory(pointer(d1), 10)
m2 = TranscodingStreams.Memory(pointer(d2), 1000)
codec = ZstdCompressor()
e = TranscodingStreams.Error()
@test TranscodingStreams.startproc(codec, :read, e) === :ok
@test TranscodingStreams.pledgeinsize(codec, 10000, e) === :ok
@test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok
m1 = TranscodingStreams.Memory(pointer(d1), 0)
@test TranscodingStreams.process(codec, m1, m2, e)[3] === :error
@test e[] == ErrorException("zstd error: Src size is incorrect")
end
@testset "set pledgeinsize after process" begin
m1 = TranscodingStreams.Memory(pointer(d1), 1000)
m2 = TranscodingStreams.Memory(pointer(d2), 1000)
codec = ZstdCompressor()
e = TranscodingStreams.Error()
@test TranscodingStreams.startproc(codec, :read, e) === :ok
@test TranscodingStreams.process(codec, m1, m2, e)[3] === :ok
@test TranscodingStreams.pledgeinsize(codec, 10000, e) === :error
@test e[] == ErrorException("zstd error setting pledged source size")
end
end
end
end
Expand Down Expand Up @@ -218,6 +252,12 @@ include("utils.jl")
TranscodingStreams.finalize(codec)
data = [0x00,0x01]
GC.@preserve data let m = TranscodingStreams.Memory(pointer(data), length(data))
if isdefined(TranscodingStreams, :pledgeinsize)
try
TranscodingStreams.pledgeinsize(codec, 10, TranscodingStreams.Error())
catch
end
end
try
TranscodingStreams.expectedsize(codec, m)
catch
Expand Down

0 comments on commit b2c4e97

Please sign in to comment.