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

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

        [Theory]
        [InlineData("AAAAAAAA AA\r\nA=")]           // Two complete blocks, then whitespace-split final quantum
        [InlineData("AAAA AAAA AA\r\nA=")]          // Two blocks with space, then whitespace-split final quantum
        [InlineData("AAAAAAAA\r\nAAAA AA\r\nA=")]   // Multiple blocks with various whitespace patterns
        public void DecodingWithValidDataBeforeWhiteSpaceSplitFinalQuantum(string base64String)
        {
            // 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
            OperationStatus status = Base64.DecodeFromUtf8(base64Data, output, out int bytesConsumed, out int bytesWritten, isFinalBlock: false);
            
            // Should have processed some data (either Done with partial decode or InvalidData/NeedMoreData)
            Assert.True(status == OperationStatus.Done || status == OperationStatus.InvalidData || status == OperationStatus.NeedMoreData);
            
            // 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");
        }

        [Theory]
        [InlineData("AAAA AA\r\nA=")]               // Just base64 start, then whitespace-split final quantum
        [InlineData("AA AA AAAA AA\r\nA=")]         // Previous blocks with spaces, then whitespace-split final quantum
        public void DecodingWithSpacesBeforeWhiteSpaceSplitFinalQuantum(string base64String)
        {
            // Test cases where previous blocks already contain spaces
            ReadOnlySpan<byte> base64Data = Encoding.ASCII.GetBytes(base64String);
            var output = new byte[100]; // Use larger buffer to avoid #123222

            // First call with isFinalBlock=false
            OperationStatus status = Base64.DecodeFromUtf8(base64Data, output, out int bytesConsumed, out int bytesWritten, isFinalBlock: false);
            
            // 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):
The current asserts are too broad to be useful.
The expected `OperationStatus` in all cases should be `InvalidData`. Also assert that the expected number of bytes was consumed and written, and that the only remaining data in the input after the first decode is the final block.

Code after:
            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]
        public void DecodingWithEmbeddedWhiteSpaceIntoSmallDestination()
        {
            // Tests DecodeWithWhiteSpaceBlockwiseWrapper path - whitespace embedded in Base64 data.
            // Input "z A w w" has whitespace in the middle. "zAww" decodes to 3 bytes.
            byte[] input = Encoding.UTF8.GetBytes("z A w w");

            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);

            // Also test with larger embedded whitespace
            byte[] input2 = Encoding.UTF8.GetBytes("z  A  w  w");
            byte[] destination2 = new byte[3];
            OperationStatus status2 = Base64.DecodeFromUtf8(input2, destination2, out int consumed2, out int written2);
            Assert.Equal(OperationStatus.Done, status2);
            Assert.Equal(input2.Length, consumed2);
            Assert.Equal(3, written2);
        }

        [Fact]
        public void DecodingWithEmbeddedWhiteSpaceIntoSmallDestination_ActualDestinationTooSmall()
        {
            // Tests DecodeWithWhiteSpaceBlockwiseWrapper path with actual destination too small.
            // Input "A Q I D B A = =" (embedded whitespace) decodes to 4 bytes.
            byte[] input = Encoding.UTF8.GetBytes("A Q I D B A = =");

            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);