[InlineData("9f01c202ff")] // equivalent indefinite-length array
        public static void ReadTag_CallingEndReadArrayPrematurely_ShouldThrowInvalidOperationException(string hexEncoding)
        {
            // encoding is valid CBOR, so should not throw CborContentException
            byte[] data   = hexEncoding.HexToByteArray();
            var    reader = new CborReader(data);

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

            int bytesRemaining = reader.BytesRemaining;

            Assert.Equal(CborReaderState.UnsignedInteger, reader.PeekState());
            Assert.Throws <InvalidOperationException>(() => reader.ReadEndArray());
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
        }
Beispiel #2
0
        public static void ReadDateTimeOffset_StrictConformance_OnError_ShouldPreserveReaderState()
        {
            string hexEncoding = "a20101c06001"; // { 1 : 1 , 0("") : 1 } conforming CBOR with invalid date/time schema
            var    reader      = new CborReader(hexEncoding.HexToByteArray(), CborConformanceMode.Strict);

            reader.ReadStartMap();
            reader.ReadInt32();
            reader.ReadInt32();

            Assert.Throws <CborContentException>(() => reader.ReadDateTimeOffset()); // throws a format exception due to malformed date/time string
            // the following operation would original throw a false positive duplicate key error,
            // due to the checkpoint restore logic not properly resetting key uniqueness validation
            reader.SkipValue(disableConformanceModeChecks: false);

            reader.ReadInt32();
            reader.ReadEndMap();
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
        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();
            }

            int bytesRead = reader.BytesRead;

            Assert.Equal(CborReaderState.FormatError, reader.PeekState()); // don't want this to fail
            Assert.Throws <FormatException>(() => reader.ReadEndMap());

            Assert.Equal(bytesRead, reader.BytesRead);
        }
        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 bytesRead = reader.BytesRead;

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

            Assert.Equal(bytesRead, reader.BytesRead);
        }
Beispiel #5
0
        public static void ReadMap_IndefiniteLength_OddKeyValuePairs_ShouldThrowCborContentException(string hexEncoding, int length)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

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

            int bytesRemaining = reader.BytesRemaining;

            Assert.Throws <CborContentException>(() => reader.PeekState());
            Assert.Throws <CborContentException>(() => reader.ReadEndMap());

            Assert.Equal(bytesRemaining, reader.BytesRemaining);
        }
        public static void Depth_ShouldReturnExpectedValue(int depth)
        {
            byte[] encoding = Enumerable.Repeat <byte>(0x81, depth).Append <byte>(0x01).ToArray();
            var    reader   = new CborReader(encoding);

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

            Assert.Equal(depth, reader.CurrentDepth);
            reader.ReadInt32();
            Assert.Equal(depth, reader.CurrentDepth);

            for (int i = depth - 1; i >= 0; i--)
            {
                reader.ReadEndArray();
                Assert.Equal(i, reader.CurrentDepth);
            }

            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                throw new ArgumentException("No CBOR file specified.");
            }

            string file = args[0];

            byte[] content = File.ReadAllBytes(file);
            var    reader  = new CborReader(content, CborConformanceMode.Lax, allowMultipleRootLevelValues: true);

            try
            {
                while (reader.PeekState() != CborReaderState.Finished)
                {
                    reader.SkipValue();
                }
            }
            catch (CborContentException)
            {
            }
        }
Beispiel #8
0
        public static void Peek_EmptyBuffer_ShouldReturnEof()
        {
            var reader = new CborReader(ReadOnlyMemory <byte> .Empty);

            Assert.Equal(CborReaderState.EndOfData, reader.PeekState());
        }
Beispiel #9
0
            public static void VerifyValue(CborReader reader, object expectedValue, bool expectDefiniteLengthCollections = true)
            {
                switch (expectedValue)
                {
                case null:
                    Assert.Equal(CborReaderState.Null, reader.PeekState());
                    reader.ReadNull();
                    break;

                case bool expected:
                    Assert.Equal(CborReaderState.Boolean, reader.PeekState());
                    bool b = reader.ReadBoolean();
                    Assert.Equal(expected, b);
                    break;

                case int expected:
                    VerifyPeekInteger(reader, isUnsignedInteger: expected >= 0);
                    int i = reader.ReadInt32();
                    Assert.Equal(expected, 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 float expected:
                    Assert.Equal(CborReaderState.SinglePrecisionFloat, reader.PeekState());
                    float f = reader.ReadSingle();
                    Assert.Equal(expected, f);
                    break;

                case double expected:
                    Assert.Equal(CborReaderState.DoublePrecisionFloat, reader.PeekState());
                    double d = reader.ReadDouble();
                    Assert.Equal(expected, d);
                    break;

                case decimal expected:
                    Assert.Equal(CborReaderState.Tag, reader.PeekState());
                    decimal dec = reader.ReadDecimal();
                    Assert.Equal(expected, dec);
                    break;

                case BigInteger expected:
                    Assert.Equal(CborReaderState.Tag, reader.PeekState());
                    BigInteger bigint = reader.ReadBigInteger();
                    Assert.Equal(expected, bigint);
                    break;

                case DateTimeOffset expected:
                    Assert.Equal(CborReaderState.Tag, reader.PeekState());
                    DateTimeOffset dto = reader.ReadDateTimeOffset();
                    Assert.Equal(expected, dto);
                    break;

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

                case byte[] expected:
                    Assert.Equal(CborReaderState.ByteString, reader.PeekState());
                    byte[] bytes = reader.ReadByteString();
                    Assert.Equal(expected.ByteArrayToHex(), bytes.ByteArrayToHex());
                    break;

                case string[] expectedChunks when CborWriterTests.Helpers.IsIndefiniteLengthByteString(expectedChunks):
                    byte[][]  expectedByteChunks = expectedChunks.Skip(1).Select(ch => ch.HexToByteArray()).ToArray();

                    VerifyValue(reader, expectedByteChunks, expectDefiniteLengthCollections);
                    break;

                case string[] expectedChunks:
                    Assert.Equal(CborReaderState.StartIndefiniteLengthTextString, reader.PeekState());
                    reader.ReadStartIndefiniteLengthTextString();
                    foreach (string expectedChunk in expectedChunks)
                    {
                        Assert.Equal(CborReaderState.TextString, reader.PeekState());
                        string chunk = reader.ReadTextString();
                        Assert.Equal(expectedChunk, chunk);
                    }
                    Assert.Equal(CborReaderState.EndIndefiniteLengthTextString, reader.PeekState());
                    reader.ReadEndIndefiniteLengthTextString();
                    break;

                case byte[][] expectedChunks:
                    Assert.Equal(CborReaderState.StartIndefiniteLengthByteString, reader.PeekState());
                    reader.ReadStartIndefiniteLengthByteString();
                    foreach (byte[] expectedChunk in expectedChunks)
                    {
                        Assert.Equal(CborReaderState.ByteString, reader.PeekState());
                        byte[] chunk = reader.ReadByteString();
                        Assert.Equal(expectedChunk.ByteArrayToHex(), chunk.ByteArrayToHex());
                    }
                    Assert.Equal(CborReaderState.EndIndefiniteLengthByteString, reader.PeekState());
                    reader.ReadEndIndefiniteLengthByteString();
                    break;

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

                    break;

                case object[] nested when CborWriterTests.Helpers.IsEncodedValueRepresentation(nested):
                    string    expectedHexEncoding = (string)nested[1];

                    string actualHexEncoding = reader.ReadEncodedValue().ByteArrayToHex();
                    Assert.Equal(expectedHexEncoding, actualHexEncoding);
                    break;

                case object[] nested when CborWriterTests.Helpers.IsTaggedValueRepresentation(nested):
                    CborTag   expectedTag = (CborTag)nested[0];

                    object expectedNestedValue = nested[1];
                    Assert.Equal(CborReaderState.Tag, reader.PeekState());
                    Assert.Equal(expectedTag, reader.ReadTag());
                    VerifyValue(reader, expectedNestedValue, expectDefiniteLengthCollections);
                    break;

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

                default:
                    throw new ArgumentException($"Unrecognized argument type {expectedValue.GetType()}");
                }
Beispiel #10
0
                static void VerifyPeekInteger(CborReader reader, bool isUnsignedInteger)
                {
                    CborReaderState expectedState = isUnsignedInteger ? CborReaderState.UnsignedInteger : CborReaderState.NegativeInteger;

                    Assert.Equal(expectedState, reader.PeekState());
                }
        public static void Peek_EmptyBuffer_ShouldThrowCborContentException()
        {
            var reader = new CborReader(ReadOnlyMemory <byte> .Empty);

            Assert.Throws <CborContentException>(() => reader.PeekState());
        }