예제 #1
0
        public void EncodedSpan_1_to_50_given___correct_decoded_len()
        {
            var sut = new Base64UrlEncoder();

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

                    int actual;

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

                    Assert.AreEqual(i, actual);
                }
            });
        }
예제 #2
0
        public void Invalid_data_various_length___status_InvalidData()
        {
            var sut = new Base64UrlEncoder();
            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, 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);
                });
            }
        }
예제 #3
0
파일: Decode.cs 프로젝트: jinqi166/Base64
        public void Invalid_data_various_length___status_InvalidData()
        {
            var sut = new Base64UrlEncoder();
            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);
            }
        }
예제 #4
0
        public void Buffer_chain_various_length_decode()
        {
            var sut = new Base64UrlEncoder();
            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  = sut.GetDecodedLength(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];

                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);
            }
        }
예제 #5
0
        public void EncodedLength_is_0___0()
        {
            var sut = new Base64UrlEncoder();

            int actual = sut.GetDecodedLength(0);

            Assert.AreEqual(0, actual);
        }
예제 #6
0
파일: Decode.cs 프로젝트: jinqi166/Base64
        public void Large_data___avx2_event_fired()
        {
            Assume.That(Avx2.IsSupported);

            var sut  = new Base64UrlEncoder();
            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;

            Base64UrlEncoder.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);
        }
예제 #7
0
        public void EncodedSpan_is_empty___0()
        {
            var sut       = new Base64UrlEncoder();
            var emptySpan = ReadOnlySpan <T> .Empty;

            int actual;

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

            Assert.AreEqual(0, actual);
        }
예제 #8
0
파일: Decode.cs 프로젝트: jinqi166/Base64
        public void Guid___ssse3_event_fired()
        {
            var sut  = new Base64UrlEncoder();
            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;

            Base64UrlEncoder.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);
        }
예제 #9
0
파일: Decode.cs 프로젝트: jinqi166/Base64
        public void Empty_input()
        {
            var sut = new Base64UrlEncoder();
            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);

            Assert.AreEqual(OperationStatus.Done, status);
            Assert.AreEqual(0, consumed);
            Assert.AreEqual(0, written);
            Assert.IsTrue(data.IsEmpty);
        }
예제 #10
0
        public void EncodedSpan_1_to_50_char_given___correct_decoded_len()
        {
            var sut = new Base64UrlEncoder();

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

                    int actual = sut.GetDecodedLength(base64Url.AsSpan());

                    Assert.AreEqual(i, actual);
                }
            });
        }
예제 #11
0
        public void EncodedLength_is_negative___throws_ArgumentOutOfRange(int encodedLength)
        {
            var sut = new Base64UrlEncoder();

            Assert.Throws <ArgumentOutOfRangeException>(() => sut.GetDecodedLength(encodedLength));
        }
예제 #12
0
        public void EncodedLength_given_malformed___throws_MalformedInput(int encodedLength)
        {
            var sut = new Base64UrlEncoder();

            Assert.Throws <FormatException>(() => sut.GetDecodedLength(encodedLength));
        }
예제 #13
0
        public int EncodedLength_given___correct_decoded_length(int encodedLength)
        {
            var sut = new Base64UrlEncoder();

            return(sut.GetDecodedLength(encodedLength));
        }
예제 #14
0
        public void Malformed_input_length___throws_FormatException(int len)
        {
            var sut = new Base64UrlEncoder();

            Assert.Throws <FormatException>(() => sut.GetDecodedLength(len));
        }
예제 #15
0
        public void EncodedLength_is_int_Max___throws_ArgumentOutOfRange()
        {
            var sut = new Base64UrlEncoder();

            Assert.Throws <ArgumentOutOfRangeException>(() => sut.GetDecodedLength(int.MaxValue));
        }
예제 #16
0
        public void Data_of_various_length___roundtrips_correclty()
        {
            var sut   = new Base64UrlEncoder();
            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);
                Assert.AreEqual(source.Length, consumed);
                Assert.AreEqual(encoded.Length, written);

                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.ToBase64Url(), 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());
            }
        }