public void EncodingOutputTooSmallRetry(bool isFinalBlock) { Span <byte> source = new byte[750]; Base64TestHelper.InitializeBytes(source); int outputSize = 320; int requiredSize = Base64.GetMaxEncodedToUtf8Length(source.Length); Span <byte> encodedBytes = new byte[outputSize]; Assert.Equal(OperationStatus.DestinationTooSmall, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int written, isFinalBlock)); int expectedConsumed = encodedBytes.Length / 4 * 3; Assert.Equal(expectedConsumed, consumed); Assert.Equal(encodedBytes.Length, written); Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, encodedBytes.Length, source, encodedBytes)); encodedBytes = new byte[requiredSize - outputSize]; source = source.Slice(consumed); Assert.Equal(OperationStatus.Done, Base64.EncodeToUtf8(source, encodedBytes, out consumed, out written, isFinalBlock)); expectedConsumed = encodedBytes.Length / 4 * 3; Assert.Equal(expectedConsumed, consumed); Assert.Equal(encodedBytes.Length, written); Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, encodedBytes.Length, source, encodedBytes)); }
public void EncodingOutputTooSmall(bool isFinalBlock) { for (int numBytes = 4; numBytes < 20; numBytes++) { Span <byte> source = new byte[numBytes]; Base64TestHelper.InitializeBytes(source, numBytes); Span <byte> encodedBytes = new byte[4]; Assert.Equal(OperationStatus.DestinationTooSmall, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int written, isFinalBlock)); int expectedConsumed = 3; Assert.Equal(expectedConsumed, consumed); Assert.Equal(encodedBytes.Length, written); Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, encodedBytes.Length, source, encodedBytes)); } }
public void BasicEncoding() { var rnd = new Random(42); for (int i = 0; i < 10; i++) { int numBytes = rnd.Next(100, 1000 * 1000); Span <byte> source = new byte[numBytes]; Base64TestHelper.InitializeBytes(source, numBytes); Span <byte> encodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(source.Length)]; Assert.Equal(OperationStatus.Done, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount)); Assert.Equal(source.Length, consumed); Assert.Equal(encodedBytes.Length, encodedBytesCount); Assert.True(Base64TestHelper.VerifyEncodingCorrectness(source.Length, encodedBytes.Length, source, encodedBytes)); } }
public void EncodeInPlace() { const int numberOfBytes = 15; Span <byte> testBytes = new byte[numberOfBytes / 3 * 4]; // slack since encoding inflates the data Base64TestHelper.InitializeBytes(testBytes); for (int numberOfBytesToTest = 0; numberOfBytesToTest <= numberOfBytes; numberOfBytesToTest++) { var expectedText = Convert.ToBase64String(testBytes.Slice(0, numberOfBytesToTest).ToArray()); Assert.Equal(OperationStatus.Done, Base64.EncodeToUtf8InPlace(testBytes, numberOfBytesToTest, out int bytesWritten)); Assert.Equal(Base64.GetMaxEncodedToUtf8Length(numberOfBytesToTest), bytesWritten); var encodedText = Encoding.ASCII.GetString(testBytes.Slice(0, bytesWritten).ToArray()); Assert.Equal(expectedText, encodedText); } }
public void BasicEncodingWithFinalBlockFalse() { var rnd = new Random(42); for (int i = 0; i < 10; i++) { int numBytes = rnd.Next(100, 1000 * 1000); Span <byte> source = new byte[numBytes]; Base64TestHelper.InitializeBytes(source, numBytes); Span <byte> encodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(source.Length)]; int expectedConsumed = source.Length / 3 * 3; // only consume closest multiple of three since isFinalBlock is false int expectedWritten = source.Length / 3 * 4; // The constant random seed guarantees that both states are tested. OperationStatus expectedStatus = numBytes % 3 == 0 ? OperationStatus.Done : OperationStatus.NeedMoreData; Assert.Equal(expectedStatus, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount, isFinalBlock: false)); Assert.Equal(expectedConsumed, consumed); Assert.Equal(expectedWritten, encodedBytesCount); Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, expectedWritten, source, encodedBytes)); } }