-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Allow Base64Decoder to ignore space chars, add IsValid methods and tests #79334
Closed
Closed
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6d72060
Allow Base64Decoder to ignore space chars, add IsValid methods and tests
heathbm 35302b4
Address PR feedback regarding Base64.IsValid
heathbm 837b4bd
Address PR feedback: General optimizations
heathbm 9b3b581
Address PR feedback: Use vectorized decoding while enough src
heathbm 9983889
Address PR feedback: General optimization
heathbm f389289
Address PR feedback: Optimize for whitespace (\r\n) every 76 bytes
heathbm 8482bbd
Address PR feedback: Implement driver/worker pattern with original code
heathbm dca7ee6
Address PR feedback: Reuse existing decoding method with whitespace
heathbm fe32cd3
Merge branch 'main' into add-base64-is-valid
heathbm 261885c
Address PR feedback: Remove redundant empty buffer check
heathbm fb9e8de
Address PR Feedback: Add missing magic constant comment
heathbm b68d36c
Address PR Feedback: Avoid validation logic duplication
heathbm 689de25
Merge branch 'main' into add-base64-is-valid
heathbm 745fd41
Throw Base64FormatException when whitespace should not be ignored
heathbm b35ce12
Adress PR feedback: Improve naming of Base64Validator.cs internals
heathbm 5848058
Adress PR feedback: Add test to demonstrate extra whitespace is counted
heathbm 7c022b0
Address PR feedback: avoid bound-check
heathbm 04989b5
Address PR feedback: Base64.IsValid: Return when no more invalid chars
heathbm 5007df7
Address PR feedback: Refactor Bas64.IsValid method
heathbm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/libraries/System.Memory/tests/Base64/Base64TestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license.utf8Bytes, utf8Bytes.Length | ||
|
||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace System.Buffers.Text.Tests | ||
{ | ||
public class Base64TestBase | ||
{ | ||
public static IEnumerable<object[]> ValidBase64Strings_WithCharsThatMustBeIgnored() | ||
{ | ||
// Create a Base64 string | ||
string text = "a b c"; | ||
byte[] utf8Bytes = Encoding.UTF8.GetBytes(text); | ||
string base64Utf8String = Convert.ToBase64String(utf8Bytes); | ||
|
||
// Split the base64 string in half | ||
int stringLength = base64Utf8String.Length / 2; | ||
string firstSegment = base64Utf8String.Substring(0, stringLength); | ||
string secondSegment = base64Utf8String.Substring(stringLength, stringLength); | ||
|
||
// Insert ignored chars between the base 64 string | ||
// One will have 1 char, another will have 3 | ||
|
||
// Line feed | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(9), 3), utf8Bytes }; | ||
|
||
// Horizontal tab | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(10), 3), utf8Bytes }; | ||
|
||
// Carriage return | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(13), 3), utf8Bytes }; | ||
|
||
// Space | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedInTheMiddle(Convert.ToChar(32), 3), utf8Bytes }; | ||
|
||
string GetBase64StringWithPassedCharInsertedInTheMiddle(char charToInsert, int numberOfTimesToInsert) => $"{firstSegment}{new string(charToInsert, numberOfTimesToInsert)}{secondSegment}"; | ||
|
||
// Insert ignored chars at the start of the base 64 string | ||
// One will have 1 char, another will have 3 | ||
|
||
// Line feed | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(9), 3), utf8Bytes }; | ||
|
||
// Horizontal tab | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(10), 3), utf8Bytes }; | ||
|
||
// Carriage return | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(13), 3), utf8Bytes }; | ||
|
||
// Space | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheStart(Convert.ToChar(32), 3), utf8Bytes }; | ||
|
||
string GetBase64StringWithPassedCharInsertedAtTheStart(char charToInsert, int numberOfTimesToInsert) => $"{new string(charToInsert, numberOfTimesToInsert)}{firstSegment}{secondSegment}"; | ||
|
||
// Insert ignored chars at the end of the base 64 string | ||
// One will have 1 char, another will have 3 | ||
// Whitespace after end/padding is not included in consumed bytes | ||
|
||
// Line feed | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(9), 3), utf8Bytes }; | ||
|
||
// Horizontal tab | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(10), 3), utf8Bytes }; | ||
|
||
// Carriage return | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(13), 3), utf8Bytes }; | ||
|
||
// Space | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 1), utf8Bytes }; | ||
yield return new object[] { GetBase64StringWithPassedCharInsertedAtTheEnd(Convert.ToChar(32), 3), utf8Bytes }; | ||
|
||
string GetBase64StringWithPassedCharInsertedAtTheEnd(char charToInsert, int numberOfTimesToInsert) => $"{firstSegment}{secondSegment}{new string(charToInsert, numberOfTimesToInsert)}"; | ||
} | ||
|
||
public static IEnumerable<object[]> StringsOnlyWithCharsToBeIgnored() | ||
{ | ||
// One will have 1 char, another will have 3 | ||
|
||
// Line feed | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(9), 1) }; | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(9), 3) }; | ||
|
||
// Horizontal tab | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(10), 1) }; | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(10), 3) }; | ||
|
||
// Carriage return | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(13), 1) }; | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(13), 3) }; | ||
|
||
// Space | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(32), 1) }; | ||
yield return new object[] { GetRepeatedChar(Convert.ToChar(32), 3) }; | ||
|
||
string GetRepeatedChar(char charToInsert, int numberOfTimesToInsert) => new string(charToInsert, numberOfTimesToInsert); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you mix in some variants like
or something like that covered by the test
ValidBase64Strings_WithCharsThatMustBeIgnored
below?(Sorry, that I don't remember the generated testcases, it's too long ago 😉)
Also in https://gist.github.com/heathbm/f59662bd2334761d28288755a34e29ec you had some cool tests that generated inputs with whitespace at various places. In my lib that gist got into unit tests. Is everything covered here or should these tests be added too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I covered some of these cases here: https://github.com/dotnet/runtime/pull/79334/files#diff-6b4fec85572fdbcc9b03c81fe66cf8bf31cbb5620235abce7dab3c6dc034d862R11
It's a lighter version of the gist, as I was not sure if a test with that many loops/asserts would be acceptable in the pipeline.