Repository: dotnet/runtime
File: src/libraries/System.Memory/tests/Base64/Base64DecoderUnitTests.cs (C#)

Code before:
            Assert.Equal(3, bytesWritten);
        }

        [Fact]
        public void DecodingPaddedQuantumWithIsFinalBlockFalse()
        {
            // Quantum with padding should not be decoded when isFinalBlock=false
            ReadOnlySpan<byte> base64Data = "AAA="u8;
            var output = new byte[10];

            OperationStatus status = Base64.DecodeFromUtf8(base64Data, output, out int bytesConsumed, out int bytesWritten, isFinalBlock: false);
            Assert.Equal(OperationStatus.InvalidData, status);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(0, bytesWritten);
        }

        [Theory]
        [InlineData("AQIDBAUG AQ\r\nQ=", 10, 6, "AQ\r\nQ=")]           // Two complete blocks, then whitespace-split final quantum
        [InlineData("AQID BAUG AQ\r\nQ=", 11, 6, "AQ\r\nQ=")]          // Two blocks with space, then whitespace-split final quantum
        [InlineData("AQIDBAUG\r\nAQID AQ\r\nQ=", 17, 9, "AQ\r\nQ=")]   // Multiple blocks with various whitespace patterns
        public void DecodingWithValidDataBeforeWhiteSpaceSplitFinalQuantum(string base64String, int expectedBytesConsumedFirstCall, int expectedBytesWrittenFirstCall, string expectedRemainingAfterFirstCall)
        {
            // When there's valid data before a whitespace-split final quantum and isFinalBlock=false,
            // verify the streaming scenario works correctly
            ReadOnlySpan<byte> base64Data = Encoding.ASCII.GetBytes(base64String);
            var output = new byte[100]; // Use larger buffer to avoid #123222

            // First call with isFinalBlock=false should decode the valid complete blocks and stop before the incomplete final quantum
            OperationStatus status = Base64.DecodeFromUtf8(base64Data, output, out int bytesConsumed, out int bytesWritten, isFinalBlock: false);
            
            Assert.Equal(OperationStatus.InvalidData, status);
            Assert.Equal(expectedBytesConsumedFirstCall, bytesConsumed);
            Assert.Equal(expectedBytesWrittenFirstCall, bytesWritten);
            
            // Verify that only the final block remains
            ReadOnlySpan<byte> remaining = base64Data.Slice(bytesConsumed);
            string remainingString = Encoding.ASCII.GetString(remaining);
            Assert.Equal(expectedRemainingAfterFirstCall, remainingString);
            
            // Verify we can complete decoding by retrying with the FULL input and isFinalBlock=true
            Array.Clear(output, 0, output.Length);
            status = Base64.DecodeFromUtf8(base64Data, output, out bytesConsumed, out bytesWritten, isFinalBlock: true);
            Assert.Equal(OperationStatus.Done, status);
            Assert.Equal(base64Data.Length, bytesConsumed);
            Assert.True(bytesWritten > 0, "Should have decoded data");
        }
    }
}


Review comment (suggestion):
```suggestion
            var output = new byte[100];
```

Code after:

            byte[] destination3 = new byte[3];
            OperationStatus status3 = Base64.DecodeFromUtf8(input, destination3, out int consumed3, out int written3);
            Assert.Equal(OperationStatus.Done, status3);
            Assert.Equal(input.Length, consumed3);
            Assert.Equal(3, written3);
        }

        [Fact]
        public void DecodingWithOnlyWhiteSpaceIntoSmallDestination()
        {
            // Input "        " (8 spaces) decodes to 0 bytes.
            // With destination of 1 byte, this should succeed, not report "Destination too short".
            byte[] allSpaces = Encoding.UTF8.GetBytes(new string(' ', 8));

            byte[] destination = new byte[1];
            OperationStatus status = Base64.DecodeFromUtf8(allSpaces, destination, out int consumed, out int written);
            Assert.Equal(OperationStatus.Done, status);
            Assert.Equal(allSpaces.Length, consumed);
            Assert.Equal(0, written);

            // Also test with empty destination buffer
            byte[] emptyDestination = Array.Empty<byte>();
            OperationStatus statusEmpty = Base64.DecodeFromUtf8(allSpaces, emptyDestination, out int consumedEmpty, out int writtenEmpty);
            Assert.Equal(OperationStatus.Done, statusEmpty);
            Assert.Equal(allSpaces.Length, consumedEmpty);
            Assert.Equal(0, writtenEmpty);
        }

        [Fact]
        public void DecodingWithWhiteSpaceIntoSmallDestination_ActualDestinationTooSmall()
        {
            // Input "  AQID" (leading whitespace only) decodes to 3 bytes.
            // With destination of 1 byte, this should correctly report "Destination too short".
            // Note: Base64 requires input length to be multiple of 4, so we use "AQIDBA==" which decodes to 4 bytes.
            byte[] input = Encoding.UTF8.GetBytes("  AQIDBA==");

            byte[] destination1 = new byte[1];
            OperationStatus status1 = Base64.DecodeFromUtf8(input, destination1, out _, out _);
            Assert.Equal(OperationStatus.DestinationTooSmall, status1);

            // With destination of 4 bytes, this should succeed.
            byte[] destination4 = new byte[4];
            OperationStatus status4 = Base64.DecodeFromUtf8(input, destination4, out int consumed4, out int written4);
            Assert.Equal(OperationStatus.Done, status4);
            Assert.Equal(input.Length, consumed4);
            Assert.Equal(4, written4);
            Assert.Equal(new byte[] { 1, 2, 3, 4 }, destination4);
        }

        [Fact]