Skip to content

Commit

Permalink
add tests against more test files
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Jul 21, 2024
1 parent 59d9c8b commit b497efc
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
/Manifest.toml
fixture.tar.gz
fixture
/test/testdata/
.CondaPkg
*.cov
13 changes: 13 additions & 0 deletions test/Artifacts.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[ziparchives-jl]
git-tree-sha1 = "df9dada7b01ead92def5830def01103ea6923f53"

[[ziparchives-jl.download]]
sha256 = "99a9bb1d9cba1fae77fd7be224b654da1f247815845ef4ac682a546e8dc70ceb"
url = "https://github.com/medyan-dev/ZipArchives.jl/releases/download/v2.1.6/fixture.tar.gz"

[deflate64-rs]
git-tree-sha1 = "3d6705eb6be762b7a849e06a7352f2a1872e222a"

[[deflate64-rs.download]]
sha256 = "c9d31151deac3eff8856e271c7fb8d2eb2c659d63830955a05c6a481d555d710"
url = "https://github.com/anatawa12/deflate64-rs/archive/refs/tags/v0.1.9.tar.gz"
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
CodecInflate64 = "6309b1aa-fc58-479c-8956-599a07234577"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
ZipArchives = "49080126-0e18-4c2a-b176-c102e4b3760c"
Expand Down
35 changes: 35 additions & 0 deletions test/gen_files.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using StableRNGs: StableRNG

# This script will create a "testdata" directory
# This directory can be compressed on windows to create
# a test for deflate64
# To compress, open the directory in File Explorer
# Then press ctrl A to select all the files.
# Next right click, and press "Compress to ZIP file" button.

rng = StableRNG(1234)

rm("testdata"; recursive=true, force=true)
dir = mkdir("testdata")

write(joinpath(dir,"abig.dat"), zeros(UInt8, 2^31))
for i in 0:500
write(joinpath(dir,"small$(i).dat"), zeros(UInt8, i))
end

thing = rand(rng, UInt8, 200)
d = UInt8[]
for dist in [0:258; 1000:1030; 2000:1000:33000; 34000:10000:100_000]
append!(d, thing)
append!(d, rand(rng, 0x00:0x0f, dist))
end
write(joinpath(dir, "dist-rand.dat"), d)

for n in 65536-300:65536-100
write(joinpath(dir, "dist-$(n).dat"), [thing; zeros(UInt8, n); thing])
end

write(joinpath(dir, "incomp.dat"), rand(rng, UInt8, 400_000))

# Finally write this file
write(joinpath(dir, "gen_files.jl"), read(@__FILE__))
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Test
using Random
using CodecInflate64
using Aqua: Aqua
using Pkg.Artifacts: @artifact_str, ensure_artifact_installed

Aqua.test_all(CodecInflate64)

Expand Down Expand Up @@ -35,5 +36,14 @@ include("tests_from_inflate.jl")
end
end

@testset "tests from ZipArchives.jl fixture" begin
ensure_artifact_installed("ziparchives-jl", joinpath(@__DIR__,"Artifacts.toml"))
fixture_dir = joinpath(artifact"ziparchives-jl", "fixture")
for file in readdir(fixture_dir)
checkcrc32_zipfile(joinpath(fixture_dir,file))
end
end

include("tests_from_deflate64-rs.jl")

include("test_ts.jl")
61 changes: 32 additions & 29 deletions test/tests_from_deflate64-rs.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
#=
These tests are ported from https://github.com/anatawa12/deflate64-rs
https://github.com/anatawa12/deflate64-rs is licensed under MIT License:
> The MIT License (MIT)
>
> Copyright (c) .NET Foundation and Contributors
> Copyright (c) anatawa12 2023
>
> All rights reserved.
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> SOFTWARE.
=#
using Pkg.Artifacts: @artifact_str, ensure_artifact_installed

# These tests are ported from https://github.com/anatawa12/deflate64-rs/releases/tag/v0.1.9

include("utils.jl")

#TODO Use custom errors for invalid deflate data.

@testset "tests from deflate64-rs" begin
ensure_artifact_installed("deflate64-rs", joinpath(@__DIR__,"Artifacts.toml"))
test_assets = joinpath(artifact"deflate64-rs", "deflate64-rs-0.1.9", "test-assets")
checkcrc32_zipfile(joinpath(test_assets,"deflate64.zip"))

u = read(joinpath(test_assets,"issue-13/logo.png"))
c = read(joinpath(test_assets,"issue-13/unitwf-1.5.0.minimized.zip"))[1183:1182+34919]
@test de64compress(c) == u

c = read(joinpath(test_assets,"issue-23/raw_deflate64_index_out_of_bounds"))
@test_throws ErrorException("incomplete code table") de64compress(c)

c = read(joinpath(test_assets,"issue-25/deflate64_not_enough_space.zip"))[31:end]
@test_throws ErrorException("incomplete code table") de64compress(c)

c = read(joinpath(test_assets,"issue-29/raw.zip"))[122:end]
@test_throws ErrorException("incomplete code table") de64compress(c)

c = read(joinpath(test_assets,"deflate64.zip"))[41:40+2669743]
stream = Deflate64DecompressorStream(IOBuffer(c))
u = UInt8[]
read!(stream, u)
@test u == UInt8[]
@test read(stream) == read(joinpath(test_assets,"folder/binary.wmv"))
end
3 changes: 2 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Test
using p7zip_jll: p7zip
using ZipArchives:
ZipReader,
Expand All @@ -9,7 +10,7 @@ using ZipArchives:
zip_compression_method, # experimental
zip_entry_data_offset # experimental
using CodecZlib: DeflateCompressor
using CodecInflate64: DeflateDecompressor, Deflate64Decompressor
using CodecInflate64: DeflateDecompressor, Deflate64Decompressor, Deflate64DecompressorStream, DeflateDecompressorStream

# p7zip doesn't seem to use the special 16 bit length code
function p7zip_64compress(data::Vector{UInt8})::Vector{UInt8}
Expand Down

0 comments on commit b497efc

Please sign in to comment.