Exemplo n.º 1
0
        public void Basic_decoding_with_invalid_input___InvalidData(string input, int expectedConsumed, int expectedWritten)
        {
            var sut = new Base64UrlEncoder();
            ReadOnlySpan <T> encoded;

            if (typeof(T) == typeof(byte))
            {
                ReadOnlySpan <byte> tmp = Encoding.ASCII.GetBytes(input);
                encoded = MemoryMarshal.Cast <byte, T>(tmp);
            }
            else if (typeof(T) == typeof(char))
            {
                encoded = MemoryMarshal.Cast <char, T>(input.AsSpan());  // .AsSpan() for net48
            }
            else
            {
                throw new NotSupportedException(); // just in case new types are introduced in the future
            }

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

            byte[] actualData = data.Slice(0, written).ToArray();

            Assert.Multiple(() =>
            {
                Assert.AreEqual(OperationStatus.InvalidData, status);
                Assert.AreEqual(expectedConsumed, consumed);
                Assert.AreEqual(expectedWritten, written);

                byte[] expectedData = Convert.FromBase64String(input.Substring(0, consumed).FromBase64Url());
                CollectionAssert.AreEqual(expectedData, actualData);
            });
        }
Exemplo n.º 2
0
        public void Malformed_input___status_InvalidData(string input, bool isFinalBlock, int expectedConsumed, int expectedWritten)
        {
            var sut = new Base64UrlEncoder();
            ReadOnlySpan <T> encoded;

            if (typeof(T) == typeof(byte))
            {
                ReadOnlySpan <byte> tmp = Encoding.ASCII.GetBytes(input);
                encoded = MemoryMarshal.Cast <byte, T>(tmp);
            }
            else if (typeof(T) == typeof(char))
            {
                encoded = MemoryMarshal.Cast <char, T>(input.AsSpan());  // AsSpan() for net48
            }
            else
            {
                throw new NotSupportedException(); // just in case new types are introduced in the future
            }

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

            Assert.Multiple(() =>
            {
                Assert.AreEqual(OperationStatus.InvalidData, status);
                Assert.AreEqual(expectedConsumed, consumed);
                Assert.AreEqual(expectedWritten, written);
            });
        }
Exemplo n.º 3
0
        public void EncodedLength_1_to_50_given___correct_max_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.GetMaxDecodedLength(base64Url.Length);

                    Assert.GreaterOrEqual(actual, i);
                }
            });
        }
Exemplo n.º 4
0
        public void Invalid_bytes___InvalidData(bool isFinalBlock)
        {
            var sut = new Base64UrlEncoder();

            byte[] invalidBytes = TestHelper.GetInvalidBytes(Base64UrlEncoder.DecodingMap).ToArray();

            Assume.That(invalidBytes.Length, Is.EqualTo(byte.MaxValue - 64 + 1));
            T[] invalid = new T[invalidBytes.Length - 1];

            if (typeof(T) == typeof(byte))
            {
                invalid = invalidBytes.Select(b => (T)(object)b).ToArray();
            }
            else if (typeof(T) == typeof(char))
            {
                invalid = invalidBytes.Select(b => (T)(object)(char)b).ToArray();
            }
            else
            {
                throw new NotSupportedException(); // just in case new types are introduced in the future
            }

            for (int j = 0; j < 8; ++j)
            {
#pragma warning disable CA2014 // Do not use stackalloc in loops
                Span <byte> tmp = stackalloc byte[8] {
                    50, 50, 50, 50, 80, 80, 80, 80
                };             // valid input - "2222PPPP"
                Span <T> encoded = stackalloc T[tmp.Length];
#pragma warning restore CA2014 // Do not use stackalloc in loops

                if (typeof(T) == typeof(byte))
                {
                    encoded = MemoryMarshal.Cast <byte, T>(tmp);
                }
                else if (typeof(T) == typeof(char))
                {
                    for (int i = 0; i < tmp.Length; ++i)
                    {
                        encoded[i] = (T)(object)(char)tmp[i];
                    }
                }
                else
                {
                    throw new NotSupportedException(); // just in case new types are introduced in the future
                }

                byte[] data = new byte[sut.GetMaxDecodedLength(encoded.Length)];

                for (int i = 0; i < invalid.Length; ++i)
                {
                    encoded[j] = invalid[i];

                    OperationStatus status = sut.DecodeCore <T>(encoded, data, out int consumed, out int written, isFinalBlock);

                    Assert.Multiple(() =>
                    {
                        Assert.AreEqual(OperationStatus.InvalidData, status, "j = {0}, i = {1}", j, i);

                        if (j < 4)
                        {
                            Assert.AreEqual(0, consumed, "j = {0}, i = {1}", j, i);
                            Assert.AreEqual(0, written, "j = {0}, i = {1}", j, i);
                        }
                        else
                        {
                            Assert.AreEqual(4, consumed, "j = {0}, i = {1}", j, i);
                            Assert.AreEqual(3, written, "j = {0}, i = {1}", j, i);
#if NETCOREAPP
                            string actual = Convert.ToBase64String(data.AsSpan(0, 3)).ToBase64Url();
#else
                            string actual = Convert.ToBase64String(data, 0, 3).ToBase64Url();
#endif
                            Assert.AreEqual("2222", actual, "j = {0}, i = {1}", j, i);
                        }
                    });
                }
            }
        }
    }
Exemplo n.º 5
0
        public void EncodedLength_is_negative___throws_ArgumentOutOfRange([Values(-1, int.MinValue)] int encodedLength)
        {
            var sut = new Base64UrlEncoder();

            Assert.Throws <ArgumentOutOfRangeException>(() => sut.GetMaxDecodedLength(encodedLength));
        }