Esempio n. 1
0
        public static void TryReadByteString_SingleValue_HappyPath(string hexExpectedValue, string hexEncoding)
        {
            byte[] buffer        = new byte[32];
            byte[] encoding      = hexEncoding.HexToByteArray();
            byte[] expectedValue = hexExpectedValue.HexToByteArray();
            var    reader        = new CborReader(encoding);
            bool   result        = reader.TryReadByteString(buffer, out int bytesWritten);

            Assert.True(result);
            Assert.Equal(expectedValue.Length, bytesWritten);
            Assert.Equal(expectedValue, buffer[..bytesWritten]);
Esempio n. 2
0
        public static void ReadEncodedValue_NestedValue_HappyPath(string hexEncoding)
        {
            byte[] encoding = $"8301{hexEncoding}60".HexToByteArray();

            var reader = new CborReader(encoding);

            reader.ReadStartArray();
            reader.ReadInt64();
            byte[] encodedValue = reader.ReadEncodedValue().ToArray();

            Assert.Equal(hexEncoding, encodedValue.ByteArrayToHex().ToLower());
        }
Esempio n. 3
0
        public static void SkipValue_NestedValue_HappyPath(string hexEncoding)
        {
            byte[] encoding = $"8301{hexEncoding}03".HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartArray();
            reader.ReadInt64();
            reader.SkipValue();
            reader.ReadInt64();
            reader.ReadEndArray();
            Assert.Equal(CborReaderState.Finished, reader.Peek());
        }
Esempio n. 4
0
        public static void ReadEndMap_ImbalancedCall_ShouldThrowInvalidOperationException(string hexEncoding, int depth)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            for (int i = 0; i < depth; i++)
            {
                reader.ReadStartArray();
            }

            Assert.Throws <InvalidOperationException>(() => reader.ReadEndMap());
        }
Esempio n. 5
0
        public static void PeekTag_SingleValue_HappyPath(ulong expectedTag, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Equal(CborReaderState.Tag, reader.PeekState());
            CborTag tag = reader.PeekTag();

            Assert.Equal(expectedTag, (ulong)tag);
            Assert.Equal(CborReaderState.Tag, reader.PeekState());
            Assert.Equal(0, reader.BytesRead);
        }
Esempio n. 6
0
        public static void SkipValue_NotAtValue_ShouldThrowInvalidOperationException()
        {
            byte[] encoding = "80".HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartArray();

            int bytesRemaining = reader.BytesRemaining;

            Assert.Throws <InvalidOperationException>(() => reader.SkipValue());
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
        }
Esempio n. 7
0
        public static void ReadTag_CallingEndReadMapPrematurely_ShouldThrowInvalidOperationException(string hexEncoding)
        {
            // encoding is valid CBOR, so should not throw FormatException
            byte[] data   = hexEncoding.HexToByteArray();
            var    reader = new CborReader(data);

            reader.ReadStartMap();
            reader.ReadInt64();
            reader.ReadTag();
            Assert.Equal(CborReaderState.UnsignedInteger, reader.Peek());
            Assert.Throws <InvalidOperationException>(() => reader.ReadEndArray());
        }
Esempio n. 8
0
        public static void SkipValue_ValidationEnabled_InvalidUtf8_ShouldThrowFormatException(string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            FormatException exn = Assert.Throws <FormatException>(() => reader.SkipValue(validateConformance: true));

            Assert.NotNull(exn.InnerException);
            Assert.IsType <DecoderFallbackException>(exn.InnerException);

            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Esempio n. 9
0
        public static void Roundtrip_ByteString(string?hexInput)
        {
            byte[]? input = hexInput?.HexToByteArray();
#endif
            using var writer = new CborWriter();
            writer.WriteByteString(input);
            byte[] encoding = writer.GetEncoding();

            var    reader = new CborReader(encoding);
            byte[] result = reader.ReadByteString();
            AssertHelper.HexEqual(input ?? Array.Empty <byte>(), result);
        }
Esempio n. 10
0
        public static void ReadBigInteger_SingleValue_HappyPath(string expectedValueString, string hexEncoding)
        {
            BigInteger expectedValue = BigInteger.Parse(expectedValueString);

            byte[] data = hexEncoding.HexToByteArray();

            var reader = new CborReader(data);

            BigInteger result = reader.ReadBigInteger();

            Assert.Equal(CborReaderState.Finished, reader.PeekState());
            Assert.Equal(expectedValue, result);
        }
Esempio n. 11
0
        public static void ReadDecimal_SingleValue_HappyPath(string expectedStringValue, string hexEncoding)
        {
            decimal expectedValue = decimal.Parse(expectedStringValue, Globalization.CultureInfo.InvariantCulture);

            byte[] data = hexEncoding.HexToByteArray();

            var reader = new CborReader(data);

            decimal result = reader.ReadDecimal();

            Assert.Equal(CborReaderState.Finished, reader.PeekState());
            Assert.Equal(expectedValue, result);
        }
Esempio n. 12
0
        public static void ReadArray_IndefiniteLength_PrematureEndArrayCall_ShouldThrowInvalidOperationException(string hexEncoding, int length)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartArray();
            for (int i = 1; i < length; i++)
            {
                reader.ReadInt64();
            }

            Assert.Throws <InvalidOperationException>(() => reader.ReadEndArray());
        }
Esempio n. 13
0
        public static void ReadMap_IndefiniteLength_MissingBreakByte_ShouldReportEndOfData(string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartMap();
            while (reader.Peek() == CborReaderState.UnsignedInteger)
            {
                reader.ReadInt64();
            }

            Assert.Equal(CborReaderState.EndOfData, reader.Peek());
        }
Esempio n. 14
0
        public static void ReadTag_SingleValue_HappyPath(ulong expectedTag, object expectedValue, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Equal(CborReaderState.Tag, reader.PeekState());
            CborTag tag = reader.ReadTag();

            Assert.Equal(expectedTag, (ulong)tag);

            Helpers.VerifyValue(reader, expectedValue);
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
Esempio n. 15
0
        internal static void ReadMap_UnsortedKeys_ConformanceNotRequiringSortedKeys_ShouldSucceed(CborConformanceLevel level, object[] keySequence, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            reader.ReadStartMap();
            foreach (object key in keySequence)
            {
                Helpers.VerifyValue(reader, key); // verify key
                reader.ReadInt32();               // value is always an integer
            }

            reader.ReadEndMap();
        }
Esempio n. 16
0
        public static void CborReader_ReadingTwoPrimitiveValues_ShouldThrowInvalidOperationException()
        {
            ReadOnlyMemory <byte> buffer = new byte[] { 0, 0 };
            var reader = new CborReader(buffer);

            reader.ReadInt64();
            Assert.Equal(CborReaderState.Finished, reader.PeekState());

            int bytesRemaining = reader.BytesRemaining;

            Assert.Throws <InvalidOperationException>(() => reader.ReadInt64());
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
        }
Esempio n. 17
0
        public static void VerifyArray(CborReader reader, params object[] expectedValues)
        {
            Assert.Equal(CborReaderState.StartArray, reader.Peek());

            ulong?length = reader.ReadStartArray();

            Assert.NotNull(length);
            Assert.Equal(expectedValues.Length, (int)length !.Value);

            foreach (object value in expectedValues)
            {
                switch (value)
                {
                case int expected:
                    if (expected >= 0)
                    {
                        Assert.Equal(CborReaderState.UnsignedInteger, reader.Peek());
                    }
                    else
                    {
                        Assert.Equal(CborReaderState.NegativeInteger, reader.Peek());
                    }

                    long i = reader.ReadInt64();
                    Assert.Equal(expected, (int)i);
                    break;

                case string expected:
                    Assert.Equal(CborReaderState.TextString, reader.Peek());
                    string s = reader.ReadTextString();
                    Assert.Equal(expected, s);
                    break;

                case byte[] expected:
                    Assert.Equal(CborReaderState.ByteString, reader.Peek());
                    byte[] b = reader.ReadByteString();
                    Assert.Equal(expected, b);
                    break;

                case object[] nested:
                    VerifyArray(reader, nested);
                    break;

                default:
                    throw new ArgumentException($"Unrecognized argument type {value.GetType()}");
                }
            }

            Assert.Equal(CborReaderState.EndArray, reader.Peek());
            reader.ReadEndArray();
        }
Esempio n. 18
0
        public static void ReadTag_NestedTagWithMissingPayload_ShouldThrowFormatException()
        {
            byte[] encoding = "9fc2ff".HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartArray();
            reader.ReadTag();

            int bytesRemaining = reader.BytesRemaining;

            Assert.Equal(CborReaderState.FormatError, reader.PeekState());
            Assert.Throws <FormatException>(() => reader.ReadEndArray());
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
        }
Esempio n. 19
0
        public static void ReadUnixTimeSeconds_SingleValue_HappyPath(string expectedValueString, string hexEncoding)
        {
            DateTimeOffset expectedValue = DateTimeOffset.Parse(expectedValueString);

            byte[] data = hexEncoding.HexToByteArray();

            var reader = new CborReader(data);

            DateTimeOffset result = reader.ReadUnixTimeSeconds();

            Assert.Equal(CborReaderState.Finished, reader.PeekState());
            Assert.Equal(expectedValue, result);
            Assert.Equal(TimeSpan.Zero, result.Offset);
        }
Esempio n. 20
0
        public static void SkipValue_ExtremelyNestedValues_ShouldNotStackOverflow(int depth)
        {
            // Construct a valid CBOR encoding with extreme nesting:
            // defines a tower of `depth` nested singleton arrays,
            // with the innermost array containing zero.
            byte[] encoding = new byte[depth + 1];
            encoding.AsSpan(0, depth).Fill(0x81); // array of length 1
            encoding[depth] = 0;

            var reader = new CborReader(encoding);

            reader.SkipValue();
            Assert.Equal(CborReaderState.Finished, reader.Peek());
        }
Esempio n. 21
0
        public static void ReadMap_IndefiniteLength_OddKeyValuePairs_ShouldThrowFormatException(string hexEncoding, int length)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartMap();
            for (int i = 0; i < length; i++)
            {
                reader.ReadInt64();
            }

            Assert.Equal(CborReaderState.FormatError, reader.Peek()); // don't want this to fail
            Assert.Throws <FormatException>(() => reader.ReadEndMap());
        }
Esempio n. 22
0
        public static void ReadBigInteger_InvalidTagPayload_ShouldRollbackToInitialState(string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray());

            reader.ReadStartArray();
            int bytesRead      = reader.BytesRead;
            int bytesRemaining = reader.BytesRemaining;

            Assert.Throws <FormatException>(() => reader.ReadBigInteger());

            Assert.Equal(bytesRead, reader.BytesRead);
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
            Assert.Equal(CborReaderState.Tag, reader.PeekState());
        }
Esempio n. 23
0
        public static void ReadUnixTimeSeconds_InvalidFormat_ShouldRollbackToInitialState(string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray());

            reader.ReadStartArray();
            int bytesRead      = reader.BytesRead;
            int bytesRemaining = reader.BytesRemaining;

            Assert.Throws <FormatException>(() => reader.ReadUnixTimeSeconds());

            Assert.Equal(bytesRead, reader.BytesRead);
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
            Assert.Equal(CborReaderState.Tag, reader.PeekState());
            Assert.Equal(CborTag.UnixTimeSeconds, reader.ReadTag());
        }
Esempio n. 24
0
        public static void CborReader_MultipleRootValuesAllowed_ReadingBeyondEndOfBuffer_ShouldThrowInvalidOperationException()
        {
            string hexEncoding = "810102";
            var    reader      = new CborReader(hexEncoding.HexToByteArray(), allowMultipleRootLevelValues: true);

            Assert.Equal(CborReaderState.StartArray, reader.PeekState());
            reader.ReadStartArray();
            reader.ReadInt32();
            reader.ReadEndArray();

            Assert.Equal(CborReaderState.UnsignedInteger, reader.PeekState());
            reader.ReadInt32();

            Assert.Equal(CborReaderState.Finished, reader.PeekState());
            Assert.Throws <InvalidOperationException>(() => reader.ReadInt32());
        }
Esempio n. 25
0
        public static void ReadArray_IncorrectDefiniteLength_ShouldThrowFormatException(string hexEncoding, int expectedLength, int actualLength)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            ulong?length = reader.ReadStartArray();

            Assert.Equal(expectedLength, (int)length !.Value);

            for (int i = 0; i < actualLength; i++)
            {
                reader.ReadInt64();
            }

            Assert.Throws <FormatException>(() => reader.ReadInt64());
        }
Esempio n. 26
0
        public static void EndReadArray_DefiniteLengthNotMet_ShouldThrowInvalidOperationException(string hexEncoding, int expectedLength)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            ulong?length = reader.ReadStartArray();

            Assert.Equal(expectedLength, (int)length !.Value);

            for (int i = 1; i < expectedLength; i++)
            {
                reader.ReadInt64();
            }

            Assert.Throws <InvalidOperationException>(() => reader.ReadEndArray());
        }
Esempio n. 27
0
            public static void VerifyArray(CborReader reader, params object[] expectedValues)
            {
                Assert.Equal(CborReaderState.StartArray, reader.Peek());

                ulong?length = reader.ReadStartArray();

                Assert.NotNull(length);
                Assert.Equal(expectedValues.Length, (int)length !.Value);

                foreach (object value in expectedValues)
                {
                    VerifyValue(reader, value);
                }

                Assert.Equal(CborReaderState.EndArray, reader.Peek());
                reader.ReadEndArray();
            }
Esempio n. 28
0
            public static void VerifyValue(CborReader reader, object expectedValue)
            {
                switch (expectedValue)
                {
                case int expected:
                    VerifyPeekInteger(reader, isUnsignedInteger: expected >= 0);
                    long i = reader.ReadInt64();
                    Assert.Equal(expected, (int)i);
                    break;

                case long expected:
                    VerifyPeekInteger(reader, isUnsignedInteger: expected >= 0);
                    long l = reader.ReadInt64();
                    Assert.Equal(expected, l);
                    break;

                case ulong expected:
                    VerifyPeekInteger(reader, isUnsignedInteger: true);
                    ulong u = reader.ReadUInt64();
                    Assert.Equal(expected, u);
                    break;

                case string expected:
                    Assert.Equal(CborReaderState.TextString, reader.Peek());
                    string s = reader.ReadTextString();
                    Assert.Equal(expected, s);
                    break;

                case byte[] expected:
                    Assert.Equal(CborReaderState.ByteString, reader.Peek());
                    byte[] b = reader.ReadByteString();
                    Assert.Equal(expected, b);
                    break;

                case object[] nested when CborWriterTests.Helpers.IsCborMapRepresentation(nested):
                    VerifyMap(reader, nested);

                    break;

                case object[] nested:
                    VerifyArray(reader, nested);
                    break;

                default:
                    throw new ArgumentException($"Unrecognized argument type {expectedValue.GetType()}");
                }
Esempio n. 29
0
        public static void ReadMap_IndefiniteLength_PrematureEndArrayCall_ShouldThrowInvalidOperationException(string hexEncoding, int length)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartMap();
            for (int i = 1; i < length; i++)
            {
                reader.ReadInt64();
            }

            int bytesRemaining = reader.BytesRemaining;

            Assert.Equal(CborReaderState.UnsignedInteger, reader.PeekState());
            Assert.Throws <InvalidOperationException>(() => reader.ReadEndMap());

            Assert.Equal(bytesRemaining, reader.BytesRemaining);
        }
Esempio n. 30
0
        internal static void ReadMap_DuplicateKeys_StrictConformance_ShouldThrowFormatException(CborConformanceLevel level, object dupeKey, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

            reader.ReadStartMap();
            Helpers.VerifyValue(reader, dupeKey);
            reader.ReadInt32();

            int             bytesRead      = reader.BytesRead;
            int             bytesRemaining = reader.BytesRemaining;
            CborReaderState state          = reader.PeekState();

            Assert.Throws <FormatException>(() => Helpers.VerifyValue(reader, dupeKey));

            // ensure reader state is preserved
            Assert.Equal(bytesRead, reader.BytesRead);
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
            Assert.Equal(state, reader.PeekState());
        }