Path: redb-route/tests/redb.Route.Tests/Serialization/Base64TextTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Serialization/Base64TextTests.cs
----
using redb.Route.Serialization;
namespace redb.Route.Tests.Serialization;
///
/// decodes a value with non-zero unused bits in its last character the same way on .NET 10
/// and .NET 11 (where refuses it), and is otherwise
/// .
///
public class Base64TextTests
{
[Theory]
[InlineData("YQ==", "61")]
[InlineData("YR==", "61")] // two pads: the low 4 bits of 'R' are unused
[InlineData("YT==", "61")]
[InlineData("YWI=", "6162")]
[InlineData("YWJ=", "6162")] // one pad: the low 2 bits of 'J' are unused
[InlineData("YWK=", "6162")]
[InlineData("YWJj", "616263")]
[InlineData("YR==\r\n", "61")] // trailing whitespace is skipped to find the last character
[InlineData("YR= =", "61")]
[InlineData("", "")]
public void Decode_ReadsTheUnusedBitsAsZero(string value, string hex)
{
Convert.ToHexString(Base64Text.Decode(value)).Should().Be(hex);
var buffer = new byte[8];
Base64Text.TryDecode(value, buffer, out var written).Should().BeTrue();
Convert.ToHexString(buffer, 0, written).Should().Be(hex);
}
[Fact]
public void Decode_GivesTheSameBytesAsTheCanonicalSpelling()
{
Base64Text.Decode("q1w2e3r4t5y6u7i8o9p0aZ==").Should().Equal(Convert.FromBase64String("q1w2e3r4t5y6u7i8o9p0aQ=="));
}
[Theory]
[InlineData("not base64!")]
[InlineData("YR")] // missing padding stays an error, as with Convert
[InlineData("Y===")]
[InlineData("Y*==")]
public void Decode_RefusesWhatConvertRefuses(string value)
{
var act = () => Base64Text.Decode(value);
act.Should().Throw();
Base64Text.TryDecode(value, new byte[8], out _).Should().BeFalse();
}
[Theory]
[InlineData("YR==")]
[InlineData("YWJ=")]
[InlineData("YR==\r\n")]
[InlineData("q1w2e3r4t5y6u7i8o9p0aZ==")]
public void TryDecodeCanonical_RefusesNonZeroUnusedBits_OnEveryRuntime(string value)
{
Base64Text.HasUnusedBitsSet(value).Should().BeTrue();
Base64Text.TryDecodeCanonical(value, new byte[32], out var written).Should().BeFalse();
written.Should().Be(0);
}
[Theory]
[InlineData("YQ==", "61")]
[InlineData("YWI=", "6162")]
[InlineData("YWJj", "616263")]
[InlineData("", "")]
public void TryDecodeCanonical_DecodesTheCanonicalSpelling(string value, string hex)
{
Base64Text.HasUnusedBitsSet(value).Should().BeFalse();
var buffer = new byte[8];
Base64Text.TryDecodeCanonical(value, buffer, out var written).Should().BeTrue();
Convert.ToHexString(buffer, 0, written).Should().Be(hex);
}
[Theory]
[InlineData("not base64!")]
[InlineData("YR")]
[InlineData("Y*==")]
public void HasUnusedBitsSet_IsFalse_ForWhatIsNotBase64_WhichConvertStillRefuses(string value)
{
Base64Text.HasUnusedBitsSet(value).Should().BeFalse();
Base64Text.TryDecodeCanonical(value, new byte[8], out _).Should().BeFalse();
}
[Fact]
public void Decode_OfNull_Throws()
{
var act = () => Base64Text.Decode(null!);
act.Should().Throw();
}
}