< Summary

Information
Class: DotnetFuzzing.Fuzzers.ZipArchiveFuzzer
Assembly: DotnetFuzzing
File(s): D:\runner\runtime\src\libraries\Fuzzing\DotnetFuzzing\Fuzzers\ZipArchiveFuzzer.cs
Line coverage
84%
Covered lines: 45
Uncovered lines: 8
Coverable lines: 53
Total lines: 87
Line coverage: 84.9%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
FuzzTarget(...)50%2271.42%
CopyToRentedArray(...)100%1160%
TestArchive()100%66100%

File(s)

D:\runner\runtime\src\libraries\Fuzzing\DotnetFuzzing\Fuzzers\ZipArchiveFuzzer.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers;
 5using System.IO.Compression;
 6using System.Threading.Tasks;
 7
 8namespace DotnetFuzzing.Fuzzers;
 9
 10internal sealed class ZipArchiveFuzzer : IFuzzer
 11{
 112    public string[] TargetAssemblies { get; } = ["System.IO.Compression"];
 013    public string[] TargetCoreLibPrefixes => [];
 014    public string Corpus => "ziparchive";
 15
 16    public void FuzzTarget(ReadOnlySpan<byte> bytes)
 70517    {
 70518        if (bytes.IsEmpty)
 019        {
 020            return;
 21        }
 22
 70523        TestArchive(CopyToRentedArray(bytes), bytes.Length, async: false).GetAwaiter().GetResult();
 70524        TestArchive(CopyToRentedArray(bytes), bytes.Length, async: true).GetAwaiter().GetResult();
 70525    }
 26
 27    private byte[] CopyToRentedArray(ReadOnlySpan<byte> bytes)
 141028    {
 141029        byte[] buffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
 30        try
 141031        {
 141032            bytes.CopyTo(buffer);
 141033            return buffer;
 34        }
 035        catch
 036        {
 037            ArrayPool<byte>.Shared.Return(buffer);
 038            throw;
 39        }
 141040    }
 41
 42    private async Task TestArchive(byte[] buffer, int length, bool async)
 141043    {
 44        try
 141045        {
 141046            using var stream = new MemoryStream(buffer, 0, length);
 47
 48            ZipArchive archive;
 49
 141050            if (async)
 70551            {
 70552                archive = await ZipArchive.CreateAsync(stream, ZipArchiveMode.Read, leaveOpen: false, entryNameEncoding:
 18253            }
 54            else
 70555            {
 70556                archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: false, entryNameEncoding: null);
 57857            }
 58
 353659            foreach (var entry in archive.Entries)
 82660            {
 61                // Access entry properties to simulate usage
 82662                _ = entry.FullName;
 82663                _ = entry.Length;
 82664                _ = entry.Comment;
 82665                _ = entry.LastWriteTime;
 82666                _ = entry.Crc32;
 82667            }
 68
 36469            if (async)
 18270            {
 18271                await archive.DisposeAsync();
 18272            }
 73            else
 18274            {
 18275                archive.Dispose();
 18276            }
 36477        }
 104678        catch (InvalidDataException)
 104679        {
 80            // ignore, this exception is expected to be thrown for invalid/corrupted archives.
 104681        }
 82        finally
 141083        {
 141084            ArrayPool<byte>.Shared.Return(buffer);
 141085        }
 141086    }
 87}