Ejemplo n.º 1
0
        public void EncodedSpan_1_to_50_given___correct_decoded_len()
        {
            var sut = new Base64Encoder();

            Assert.Multiple(() =>
            {
                for (int i = 1; i < 50; ++i)
                {
                    var data      = new byte[i];
                    string base64 = Convert.ToBase64String(data);

                    int actual;

                    if (typeof(T) == typeof(byte))
                    {
                        byte[] base64Bytes = Encoding.ASCII.GetBytes(base64);
                        actual             = sut.GetDecodedLength(base64Bytes);
                    }
                    else if (typeof(T) == typeof(char))
                    {
                        actual = sut.GetDecodedLength(base64.AsSpan());
                    }
                    else
                    {
                        throw new NotSupportedException(); // just in case new types are introduced in the future
                    }

                    Assert.AreEqual(i, actual);
                }
            });
        }
Ejemplo n.º 2
0
        public void Invalid_data_various_length___status_InvalidData()
        {
            var sut = new Base64Encoder();
            var rnd = new Random(0);

            for (int i = 4; i < 200; ++i)
            {
                var data = new byte[i];
                rnd.NextBytes(data);

                int             encodedLength = sut.GetEncodedLength(data.Length);
                Span <T>        encoded       = new T[encodedLength];
                OperationStatus status        = sut.EncodeCore(data, encoded, out int consumed, out int written);
                Assume.That(status, Is.EqualTo(OperationStatus.Done));
                Assume.That(consumed, Is.EqualTo(data.Length));
                Assume.That(written, Is.EqualTo(encodedLength));

                int decodedLength;

                if (typeof(T) == typeof(byte))
                {
                    Span <byte> tmp = MemoryMarshal.AsBytes(encoded);
                    decodedLength = sut.GetDecodedLength(tmp);

                    // Insert invalid data, just before eventual padding
                    tmp[tmp.Length - 3] = (byte)'~';
                }
                else if (typeof(T) == typeof(char))
                {
                    Span <char> tmp = MemoryMarshal.Cast <T, char>(encoded);
                    decodedLength = sut.GetDecodedLength(tmp);

                    // Insert invalid data, just before eventual padding
                    tmp[tmp.Length - 3] = '~';
                }
                else
                {
                    throw new NotSupportedException(); // just in case new types are introduced in the future
                }

                Span <byte> decoded = new byte[decodedLength];

                status = sut.DecodeCore <T>(encoded, decoded, out consumed, out written);

                // Invalid data is in the last 4 bytes, so everyting up to the last multiple of 4 is read.
                int expectedConsumed = (encoded.Length - 3) / 4 * 4;
                int expectedWritten  = expectedConsumed / 4 * 3;

                Assert.Multiple(() =>
                {
                    Assert.AreEqual(OperationStatus.InvalidData, status);
                    Assert.AreEqual(expectedConsumed, consumed, "Fail at i = {0}", i);
                    Assert.AreEqual(expectedWritten, written, "Fail at i = {0}", i);
                });
            }
        }
Ejemplo n.º 3
0
        public void Invalid_data_various_length___status_InvalidData()
        {
            var sut = new Base64Encoder();
            var rnd = new Random(0);

            for (int i = 2; i < 200; ++i)
            {
                var data = new byte[i];
                rnd.NextBytes(data);

                int             encodedLength = sut.GetEncodedLength(data.Length);
                Span <T>        encoded       = new T[encodedLength];
                OperationStatus status        = sut.EncodeCore(data, encoded, out int consumed, out int written);
                Assume.That(status, Is.EqualTo(OperationStatus.Done));
                Assume.That(consumed, Is.EqualTo(data.Length));
                Assume.That(written, Is.EqualTo(encodedLength));

                int decodedLength;

                if (typeof(T) == typeof(byte))
                {
                    Span <byte> tmp = MemoryMarshal.AsBytes(encoded);
                    decodedLength = sut.GetDecodedLength(tmp);

                    // Insert invalid data
                    tmp[0] = (byte)'~';
                }
                else if (typeof(T) == typeof(char))
                {
                    Span <char> tmp = MemoryMarshal.Cast <T, char>(encoded);
                    decodedLength = sut.GetDecodedLength(tmp);

                    // Insert invalid data
                    tmp[0] = '~';
                }
                else
                {
                    throw new NotSupportedException(); // just in case new types are introduced in the future
                }

                Span <byte> decoded = new byte[decodedLength];

                status = sut.DecodeCore <T>(encoded, decoded, out int _, out int _);

                Assert.AreEqual(OperationStatus.InvalidData, status);
            }
        }
Ejemplo n.º 4
0
        public void EncodedLength_is_int_Max___OK()
        {
            var sut = new Base64Encoder();

            int actual = sut.GetDecodedLength(int.MaxValue);

            Assert.AreEqual(int.MaxValue / 4 * 3, actual);
        }
Ejemplo n.º 5
0
        public void EncodedLength_is_0___0()
        {
            var sut = new Base64Encoder();

            int actual = sut.GetDecodedLength(0);

            Assert.AreEqual(0, actual);
        }
Ejemplo n.º 6
0
        public void Buffer_chain_various_length_decode()
        {
            var sut = new Base64Encoder();
            var rnd = new Random(0);

            for (int i = 2; i < 200; ++i)
            {
                var data = new byte[i];
                rnd.NextBytes(data);

                int             encodedLength = sut.GetEncodedLength(data.Length);
                Span <T>        encoded       = new T[encodedLength];
                OperationStatus status        = sut.EncodeCore(data, encoded, out int consumed, out int written);
                Assume.That(status, Is.EqualTo(OperationStatus.Done), "fail at i = {0}", i);
                Assume.That(consumed, Is.EqualTo(data.Length), "fail at i = {0}", i);
                Assume.That(written, Is.EqualTo(encodedLength), "fail at i = {0}", i);

                int decodedLength;

                if (typeof(T) == typeof(byte))
                {
                    decodedLength = sut.GetDecodedLength(MemoryMarshal.AsBytes(encoded));
                }
                else if (typeof(T) == typeof(char))
                {
                    decodedLength = sut.GetDecodedLength(MemoryMarshal.Cast <T, char>(encoded));
                }
                else
                {
                    throw new NotSupportedException(); // just in case new types are introduced in the future
                }

                Span <byte> decoded = new byte[decodedLength];

                int partialLength = encoded.Length / 2;
                status = sut.DecodeCore <T>(encoded.Slice(0, partialLength), decoded, out consumed, out int written1, isFinalBlock: false);
                Assert.AreEqual(partialLength % 4 == 0 ? OperationStatus.Done : OperationStatus.NeedMoreData, status, "fail at i = {0}", i);

                status = sut.DecodeCore <T>(encoded.Slice(consumed), decoded.Slice(written1), out consumed, out int written2, isFinalBlock: true);
                Assert.AreEqual(OperationStatus.Done, status, "fail at i = {0}", i);
                Assert.AreEqual(decodedLength, written1 + written2, "fail at i = {0}", i);

                CollectionAssert.AreEqual(data, decoded.ToArray(), "fail at i = {0}", i);
            }
        }
Ejemplo n.º 7
0
        public void Large_data___avx2_event_fired()
        {
            Assume.That(Avx2.IsSupported);

            var sut  = new Base64Encoder();
            var data = new byte[50];
            var rnd  = new Random(0);

            rnd.NextBytes(data);

            int             encodedLength = sut.GetEncodedLength(data.Length);
            Span <T>        encoded       = new T[encodedLength];
            OperationStatus status        = sut.EncodeCore(data, encoded, out int consumed, out int written);

            Assume.That(status, Is.EqualTo(OperationStatus.Done));
            Assume.That(consumed, Is.EqualTo(data.Length));
            Assume.That(written, Is.EqualTo(encodedLength));

            int decodedLength;

            if (typeof(T) == typeof(byte))
            {
                decodedLength = sut.GetDecodedLength(MemoryMarshal.AsBytes(encoded));
            }
            else if (typeof(T) == typeof(char))
            {
                decodedLength = sut.GetDecodedLength(MemoryMarshal.Cast <T, char>(encoded));
            }
            else
            {
                throw new NotSupportedException(); // just in case new types are introduced in the future
            }

            Span <byte> decoded = new byte[decodedLength];

            bool avxExecuted = false;

            Base64Encoder.Avx2Decoded += (s, e) => avxExecuted = true;

            status = sut.DecodeCore <T>(encoded, decoded, out int _, out int _);
            Assume.That(status, Is.EqualTo(OperationStatus.Done));

            Assert.IsTrue(avxExecuted);
        }
Ejemplo n.º 8
0
        public void Empty_input(bool isFinalBlock)
        {
            var sut = new Base64Encoder();
            ReadOnlySpan <T> encoded = ReadOnlySpan <T> .Empty;

            Span <byte>     data   = new byte[sut.GetDecodedLength(encoded.Length)];
            OperationStatus status = sut.DecodeCore(encoded, data, out int consumed, out int written, isFinalBlock);

            Assert.AreEqual(OperationStatus.Done, status);
            Assert.AreEqual(0, consumed);
            Assert.AreEqual(0, written);
            Assert.IsTrue(data.IsEmpty);
        }
Ejemplo n.º 9
0
        public void Guid___ssse3_event_fired()
        {
            var sut  = new Base64Encoder();
            var data = Guid.NewGuid().ToByteArray();

            int             encodedLength = sut.GetEncodedLength(data.Length);
            Span <T>        encoded       = new T[encodedLength];
            OperationStatus status        = sut.EncodeCore(data, encoded, out int consumed, out int written);

            Assume.That(status, Is.EqualTo(OperationStatus.Done));
            Assume.That(consumed, Is.EqualTo(data.Length));
            Assume.That(written, Is.EqualTo(encodedLength));

            int decodedLength;

            if (typeof(T) == typeof(byte))
            {
                decodedLength = sut.GetDecodedLength(MemoryMarshal.AsBytes(encoded));
            }
            else if (typeof(T) == typeof(char))
            {
                decodedLength = sut.GetDecodedLength(MemoryMarshal.Cast <T, char>(encoded));
            }
            else
            {
                throw new NotSupportedException(); // just in case new types are introduced in the future
            }

            Span <byte> decoded = new byte[decodedLength];

            bool ssse3Executed = false;

            Base64Encoder.Ssse3Decoded += (s, e) => ssse3Executed = true;

            status = sut.DecodeCore <T>(encoded, decoded, out int _, out int _);
            Assume.That(status, Is.EqualTo(OperationStatus.Done));

            Assert.IsTrue(ssse3Executed);
        }
Ejemplo n.º 10
0
        public int EncodedLength_given___correct_decoded_length(int encodedLength)
        {
            var sut = new Base64Encoder();

            return(sut.GetDecodedLength(encodedLength));
        }
Ejemplo n.º 11
0
        public void Data_of_various_length___roundtrips_correclty()
        {
            var sut   = new Base64Encoder();
            var bytes = new byte[byte.MaxValue + 1];

            for (int i = 0; i < bytes.Length; ++i)
            {
                bytes[i] = (byte)(255 - i);
            }

            for (int i = 0; i < 256; ++i)
            {
                Span <byte>     source  = bytes.AsSpan(0, i + 1);
                Span <T>        encoded = new T[sut.GetEncodedLength(source.Length)];
                OperationStatus status  = sut.EncodeCore(source, encoded, out int consumed, out int written);

                Assert.AreEqual(OperationStatus.Done, status, nameof(status) + $" at i = {i}");
                Assert.AreEqual(source.Length, consumed, nameof(consumed) + $" at i = {i}");
                Assert.AreEqual(encoded.Length, written, nameof(written) + $" at i = {i}");

                string encodedText;
                int    decodedLength;

                if (typeof(T) == typeof(byte))
                {
                    Span <byte> encodedBytes = MemoryMarshal.AsBytes(encoded);
#if NETCOREAPP
                    encodedText = Encoding.ASCII.GetString(encodedBytes);
#else
                    encodedText = Encoding.ASCII.GetString(encodedBytes.ToArray());
#endif
                    decodedLength = sut.GetDecodedLength(encodedBytes);
                }
                else if (typeof(T) == typeof(char))
                {
#if NETCOREAPP
                    encodedText = new string(MemoryMarshal.Cast <T, char>(encoded));
#else
                    encodedText = new string(MemoryMarshal.Cast <T, char>(encoded).ToArray());
#endif
                    decodedLength = sut.GetDecodedLength(encodedText.AsSpan());
                }
                else
                {
                    throw new NotSupportedException(); // just in case new types are introduced in the future
                }
#if NETCOREAPP
                string expectedText = Convert.ToBase64String(source);
#else
                string expectedText = Convert.ToBase64String(source.ToArray());
#endif
                Assert.AreEqual(expectedText, encodedText);

                Span <byte> decoded = new byte[decodedLength];
                status = sut.DecodeCore <T>(encoded, decoded, out consumed, out written);

                Assert.AreEqual(OperationStatus.Done, status);
                Assert.AreEqual(encoded.Length, consumed);
                Assert.AreEqual(decodedLength, written);

                CollectionAssert.AreEqual(source.ToArray(), decoded.ToArray());
            }
        }