|
1 | 1 | include("setup.jl") |
2 | 2 |
|
| 3 | +@testset "ChaosBufferStream" begin |
| 4 | + @testset "constant usage" begin |
| 5 | + io = BufferStream() |
| 6 | + cio = ChaosBufferStream(io; chunksizes=[17], sleepamnts=[0.001]) |
| 7 | + write(io, rand(UInt8, 30)) |
| 8 | + close(io) |
| 9 | + |
| 10 | + # Test that data comes out in 17-byte chunks (except for the last) |
| 11 | + buff = Array{UInt8}(undef, 30) |
| 12 | + t = @elapsed begin |
| 13 | + @test readbytes!(cio, buff, 30) == 17 |
| 14 | + @test readbytes!(cio, buff, 30) == 13 |
| 15 | + end |
| 16 | + @test t >= 0.001 |
| 17 | + end |
| 18 | + |
| 19 | + @testset "random usage" begin |
| 20 | + io = BufferStream() |
| 21 | + chunksizes = 5:10 |
| 22 | + cio = ChaosBufferStream(io; chunksizes=chunksizes, sleepamnts=[0.0]) |
| 23 | + write(io, rand(UInt8, 3000)) |
| 24 | + close(io) |
| 25 | + |
| 26 | + buff = Array{UInt8}(undef, 10) |
| 27 | + while !eof(cio) |
| 28 | + r = readbytes!(cio, buff, 10) |
| 29 | + # In normal operation, the chunk size must be one of |
| 30 | + # the given chunksizes, but at the end of the stream |
| 31 | + # it is allowed to be less. |
| 32 | + if !eof(cio) |
| 33 | + @test r ∈ chunksizes |
| 34 | + else |
| 35 | + @test r <= maximum(chunksizes) |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | +end |
| 40 | + |
3 | 41 | @testset "empty tarball" begin |
4 | 42 | dir = mktempdir() |
5 | 43 | tarball = Tar.create(dir) |
|
430 | 468 | end |
431 | 469 | end |
432 | 470 |
|
| 471 | + @testset "inconvenient stream buffering" begin |
| 472 | + # We will try feeding in an adversarial length that used to cause an assertion error |
| 473 | + open(tarball, read=true) do io |
| 474 | + # This will cause an assertion error because we know the padded space beyond the |
| 475 | + # end of the test file content will be larger than 17 bytes, causing the `for` |
| 476 | + # loop to exit early, failing the assertion. |
| 477 | + @test hash == Tar.tree_hash(ChaosBufferStream(io; chunksizes=[17]); skip_empty=true) |
| 478 | + end |
| 479 | + |
| 480 | + # This also affected read_data() |
| 481 | + mktempdir() do dir |
| 482 | + open(tarball, read=true) do io |
| 483 | + Tar.extract(ChaosBufferStream(io; chunksizes=[17]), dir) |
| 484 | + check_tree_hash(hash, dir) |
| 485 | + end |
| 486 | + end |
| 487 | + |
| 488 | + # We also perform a fuzzing test to convince ourselves there are no other errors |
| 489 | + # of this type within `Tar.tree_hash()`. |
| 490 | + for idx in 1:100 |
| 491 | + open(tarball, read=true) do io |
| 492 | + @test hash == Tar.tree_hash(ChaosBufferStream(io), skip_empty=true) |
| 493 | + end |
| 494 | + end |
| 495 | + end |
| 496 | + |
433 | 497 | @testset "with predicate" begin |
434 | 498 | # generate a version of dir with .skip entries |
435 | 499 | dir = make_test_dir(true) |
|
0 commit comments