Example #1
0
        public void EncodingOutputTooSmallRetry()
        {
            Span <byte> source = new byte[750];

            Base64TestHelper.InitalizeBytes(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));
            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));
            expectedConsumed = encodedBytes.Length / 4 * 3;
            Assert.Equal(expectedConsumed, consumed);
            Assert.Equal(encodedBytes.Length, written);
            Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, encodedBytes.Length, source, encodedBytes));
        }
Example #2
0
        public void EncodeEmptySpan()
        {
            Span <byte> source       = Span <byte> .Empty;
            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 EncodingOutputTooSmall(bool isFinalBlock)
        {
            for (int numBytes = 4; numBytes < 20; numBytes++)
            {
                Span <byte> source = new byte[numBytes];
                Base64TestHelper.InitalizeBytes(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));
            }
        }
Example #4
0
        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.InitalizeBytes(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));
            }
        }
Example #5
0
        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.InitalizeBytes(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;

                Assert.Equal(OperationStatus.NeedMoreData, 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));
            }
        }