Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate CompressionLevel value is defined in BrotliStream ctor #57561

Merged
merged 5 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ internal static partial class BrotliUtils
public const int Quality_Max = 11;
public const int MaxInputSize = int.MaxValue - 515; // 515 is the max compressed extra bytes

internal static int GetQualityFromCompressionLevel(CompressionLevel level) =>
level switch
internal static int GetQualityFromCompressionLevel(CompressionLevel compressionLevel) =>
compressionLevel switch
{
CompressionLevel.Optimal => Quality_Default,
CompressionLevel.NoCompression => Quality_Min,
CompressionLevel.Fastest => 1,
CompressionLevel.SmallestSize => Quality_Max,
_ => (int)level,
_ => throw new ArgumentException(SR.ArgumentOutOfRange_Enum, nameof(compressionLevel))
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public class BrotliStreamUnitTests : CompressionStreamUnitTestBase
[OuterLoop("Test takes ~6 seconds to run")]
public override void FlushAsync_DuringWriteAsync() { base.FlushAsync_DuringWriteAsync(); }

[Theory]
[InlineData((CompressionLevel)(-1))]
[InlineData((CompressionLevel)4)]
public void Ctor_ArgumentValidation_InvalidCompressionLevel(CompressionLevel compressionLevel)
{
Assert.Throws<ArgumentException>(() => new BrotliStream(new MemoryStream(), compressionLevel));
}

[Fact]
public void InvalidQuality()
{
Expand Down