Skip to content

Commit

Permalink
Fixed 🐛 from fuzz-session (#183)
Browse files Browse the repository at this point in the history
* Moved FuzzVerification up

* Added tests based on findings

* Fix
  • Loading branch information
gfoidl committed Oct 13, 2022
1 parent fc17a95 commit d6d96b6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions source/gfoidl.Base64/Internal/Operations/CharOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static void Read4EncodedBytes(ref char src, out uint t0, out uint t1, out
t3 = Unsafe.Add(ref src, 3);

// Check for non-ASCII input
if (((t0 | t1) | (t2 | t3)) > 256)
if (((t0 | t1) | (t2 | t3)) >= 256)
{
t0 = t1 = t2 = t3 = 0;
}
Expand All @@ -139,7 +139,7 @@ public static void Read3EncodedBytes(ref char src, out uint t0, out uint t1, out
t2 = Unsafe.Add(ref src, 2);

// Check for non-ASCII input
if (((t0 | t1) | t2) > 256)
if (((t0 | t1) | t2) >= 256)
{
t0 = t1 = t2 = 0;
}
Expand All @@ -152,7 +152,7 @@ public static void Read2EncodedBytes(ref char src, out uint t0, out uint t1)
t1 = Unsafe.Add(ref src, 1);

// Check for non-ASCII input
if ((t0 | t1) > 256)
if ((t0 | t1) >= 256)
{
t0 = t1 = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using gfoidl.Base64.Internal;

namespace gfoidl.Base64.Tests.Internal;
namespace gfoidl.Base64.Tests;

[TestFixture(typeof(Base64Impl<Base64Encoding>))]
[TestFixture(typeof(Base64Impl<Base64UrlEncoding>))]
Expand All @@ -16,31 +16,29 @@ namespace gfoidl.Base64.Tests.Internal;
[Test, TestCaseSource(nameof(Invalid_data_TestCases))]
public void Invalid_data_byte(string fileName)
{
var sut = _encoder;
byte[] encoded = File.ReadAllBytes(fileName);

#if DEBUG
TestContext.WriteLine(Encoding.UTF8.GetString(encoded));
#endif

Span<byte> data = new byte[sut.GetMaxDecodedLength(encoded.Length)];
OperationStatus status = sut.Decode(encoded, data, out int _, out int _);
Span<byte> data = new byte[_encoder.GetMaxDecodedLength(encoded.Length)];
OperationStatus status = _encoder.Decode(encoded, data, out int _, out int _);

Assert.AreEqual(OperationStatus.InvalidData, status);
}
//-------------------------------------------------------------------------
[Test, TestCaseSource(nameof(Invalid_data_TestCases))]
public void Invalid_data_char(string fileName)
{
var sut = _encoder;
string encoded = File.ReadAllText(fileName);

#if DEBUG
TestContext.WriteLine(encoded);
#endif

Span<byte> data = new byte[sut.GetMaxDecodedLength(encoded.Length)];
OperationStatus status = sut.Decode(encoded, data, out int _, out int _);
Span<byte> data = new byte[_encoder.GetMaxDecodedLength(encoded.Length)];
OperationStatus status = _encoder.Decode(encoded, data, out int _, out int _);

Assert.AreEqual(OperationStatus.InvalidData, status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public void Malformed_input_to_byte_array___throws_FormatException(string input)
Assert.Throws<FormatException>(() => Base64.Default.Decode(input));
}
//-------------------------------------------------------------------------
private static IEnumerable<(string Input, int ExpectedConsumed, int ExpectedWritten)> Malformed_input_to_byte_array___RawTestCases()
private static IEnumerable<(string Input, int ExpectedConsumed, int ExpectedWritten)> Malformed_input___RawTestCases()
{
yield return ("ĀĀ==" , 0, 0);
yield return ("1234abc?" , 4, 3);
yield return ("1234ab?c" , 4, 3);
yield return ("1234ab=c" , 4, 3);
Expand All @@ -64,14 +65,14 @@ public void Malformed_input_to_byte_array___throws_FormatException(string input)
}
//-------------------------------------------------------------------------
private static IEnumerable<string> Malformed_input_to_byte_array___throws_FormatException_TestCases()
=> Malformed_input_to_byte_array___RawTestCases().Select(i => i.Input);
=> Malformed_input___RawTestCases().Select(i => i.Input);
//-------------------------------------------------------------------------
private static IEnumerable<object[]> Malformed_input___status_InvalidData_TestCasesWithIsFinalBlock()
{
foreach (var item in Malformed_input_to_byte_array___RawTestCases())
foreach (var item in Malformed_input___RawTestCases())
yield return new object[] { item.Input, true, item.ExpectedConsumed, item.ExpectedWritten };

foreach (var item in Malformed_input_to_byte_array___RawTestCases())
foreach (var item in Malformed_input___RawTestCases())
yield return new object[] { item.Input, false, item.ExpectedConsumed, item.ExpectedWritten };
}
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -115,7 +116,7 @@ public void Invalid_data_various_length___status_InvalidData()
decodedLength = Base64.Default.GetDecodedLength(tmp);

// Insert invalid data, just before eventual padding
tmp[tmp.Length - 3] = (byte)'~';
tmp[^3] = (byte)'~';

Span<byte> decoded = new byte[decodedLength];
status = Base64.Default.Decode(tmp, decoded, out consumed, out written);
Expand All @@ -126,7 +127,7 @@ public void Invalid_data_various_length___status_InvalidData()
decodedLength = Base64.Default.GetDecodedLength(tmp);

// Insert invalid data, just before eventual padding
tmp[tmp.Length - 3] = '~';
tmp[^3] = '~';

Span<byte> decoded = new byte[decodedLength];
status = Base64.Default.Decode(tmp, decoded, out consumed, out written);
Expand Down Expand Up @@ -351,15 +352,15 @@ public void Invalid_input_with_padding___InvalidData(
throw new NotSupportedException(); // just in case new types are introduced in the future
}

byte[] actualData = data.Slice(0, written).ToArray();
byte[] actualData = data[..written].ToArray();

Assert.Multiple(() =>
{
Assert.AreEqual(OperationStatus.InvalidData, status);
Assert.AreEqual(4, consumed);
Assert.AreEqual(3, written);
byte[] expectedData = Convert.FromBase64String(encodedString.Substring(0, consumed));
byte[] expectedData = Convert.FromBase64String(encodedString[..consumed]);
CollectionAssert.AreEqual(expectedData, actualData);
});
}
Expand Down Expand Up @@ -403,7 +404,7 @@ public void Padding_is_only_valid_for_isFinalBlock_true(
throw new NotSupportedException(); // just in case new types are introduced in the future
}

byte[] actualData = data.Slice(0, written).ToArray();
byte[] actualData = data[..written].ToArray();

Assert.Multiple(() =>
{
Expand All @@ -417,7 +418,7 @@ public void Padding_is_only_valid_for_isFinalBlock_true(
Assert.AreEqual(expectedConsumed, consumed);
Assert.AreEqual(expectedWritten , written);
byte[] expectedData = Convert.FromBase64String(encodedString.Substring(0, consumed));
byte[] expectedData = Convert.FromBase64String(encodedString[..consumed]);
CollectionAssert.AreEqual(expectedData, actualData);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public void Malformed_input_to_byte_array___throws_FormatException(string input)
Assert.Throws<FormatException>(() => Base64.Url.Decode(input));
}
//-------------------------------------------------------------------------
private static IEnumerable<(string Input, int ExpectedConsumed, int ExpectedWritten)> Malformed_input_to_byte_array___RawTestCases()
private static IEnumerable<(string Input, int ExpectedConsumed, int ExpectedWritten)> Malformed_input___RawTestCases()
{
yield return ("1234a=", 4, 3);
yield return ("ĀĀ" , 0, 0);
yield return ("1234a=" , 4, 3);
yield return ("1234abc?", 4, 3);
yield return ("1234ab?c", 4, 3);
yield return ("1234ab=c", 4, 3);
Expand All @@ -77,15 +78,15 @@ public void Malformed_input_to_byte_array___throws_FormatException(string input)
}
//-------------------------------------------------------------------------
private static IEnumerable<string> Malformed_input_to_byte_array___throws_FormatException_TestCases()
=> Malformed_input_to_byte_array___RawTestCases().Select(i => i.Input);
=> Malformed_input___RawTestCases().Select(i => i.Input);
//-------------------------------------------------------------------------
private static IEnumerable<object[]> Malformed_input___status_InvalidData_TestCasesWithIsFinalBlock()
{
foreach (var item in Malformed_input_to_byte_array___RawTestCases())
foreach (var item in Malformed_input___RawTestCases())
yield return new object[] { item.Input, true, item.ExpectedConsumed, item.ExpectedWritten };

// 1234a= should result in NeedMoreData, so skip it
foreach (var item in Malformed_input_to_byte_array___RawTestCases().Skip(1))
// ĀĀ, 1234a= should result in NeedMoreData, so skip it
foreach (var item in Malformed_input___RawTestCases().Skip(2))
yield return new object[] { item.Input, false, item.ExpectedConsumed, item.ExpectedWritten };
}
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -130,15 +131,15 @@ public void Invalid_data_various_length___status_InvalidData()
decodedLength = Base64.Url.GetDecodedLength(tmp);

// Insert invalid data, just before eventual padding
tmp[tmp.Length - 3] = (byte)'~';
tmp[^3] = (byte)'~';
}
else if (typeof(T) == typeof(char))
{
Span<char> tmp = MemoryMarshal.Cast<T, char>(encoded);
decodedLength = Base64.Url.GetDecodedLength(tmp);

// Insert invalid data, just before eventual padding
tmp[tmp.Length - 3] = '~';
tmp[^3] = '~';
}
else
{
Expand Down

0 comments on commit d6d96b6

Please sign in to comment.