Esempio n. 1
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. 2
0
        public static void ReadInt32_OutOfRangeValues_ShouldThrowOverflowException(string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Throws <OverflowException>(() => reader.ReadInt32());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Esempio n. 3
0
        public static void ReadInt32_SingleValue_HappyPath(int expectedResult, string hexEncoding)
        {
            byte[] data         = hexEncoding.HexToByteArray();
            var    reader       = new CborReader(data);
            long   actualResult = reader.ReadInt32();

            Assert.Equal(expectedResult, actualResult);
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
Esempio n. 4
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. 5
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());
        }
Esempio n. 6
0
        internal static void ReadMap_UnsortedKeys_ConformanceRequiringSortedKeys_ShouldThrowFormatException(CborConformanceLevel level, object[] keySequence, string hexEncoding)
        {
            var reader = new CborReader(hexEncoding.HexToByteArray(), level);

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

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

            // the final element violates sorting invariant
            Assert.Throws <FormatException>(() => Helpers.VerifyValue(reader, keySequence.Last()));

            // ensure reader state is preserved
            Assert.Equal(bytesRead, reader.BytesRead);
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
            Assert.Equal(state, reader.PeekState());
        }
            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 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:
                    Assert.Equal(CborReaderState.StartTextString, reader.PeekState());
                    reader.ReadStartTextStringIndefiniteLength();
                    foreach (string expectedChunk in expectedChunks)
                    {
                        Assert.Equal(CborReaderState.TextString, reader.PeekState());
                        string chunk = reader.ReadTextString();
                        Assert.Equal(expectedChunk, chunk);
                    }
                    Assert.Equal(CborReaderState.EndTextString, reader.PeekState());
                    reader.ReadEndTextStringIndefiniteLength();
                    break;

                case byte[][] expectedChunks:
                    Assert.Equal(CborReaderState.StartByteString, reader.PeekState());
                    reader.ReadStartByteStringIndefiniteLength();
                    foreach (byte[] expectedChunk in expectedChunks)
                    {
                        Assert.Equal(CborReaderState.ByteString, reader.PeekState());
                        byte[] chunk = reader.ReadByteString();
                        Assert.Equal(expectedChunk.ByteArrayToHex(), chunk.ByteArrayToHex());
                    }
                    Assert.Equal(CborReaderState.EndByteString, reader.PeekState());
                    reader.ReadEndByteStringIndefiniteLength();
                    break;

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

                    break;

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

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

            InvalidOperationException exn = Assert.Throws <InvalidOperationException>(() => reader.ReadInt32());

            Assert.Equal("Data item major type mismatch.", exn.Message);

            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Esempio n. 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.StartTextString, reader.PeekState());
                    reader.ReadStartTextStringIndefiniteLength();
                    foreach (string expectedChunk in expectedChunks)
                    {
                        Assert.Equal(CborReaderState.TextString, reader.PeekState());
                        string chunk = reader.ReadTextString();
                        Assert.Equal(expectedChunk, chunk);
                    }
                    Assert.Equal(CborReaderState.EndTextString, reader.PeekState());
                    reader.ReadEndTextStringIndefiniteLength();
                    break;

                case byte[][] expectedChunks:
                    Assert.Equal(CborReaderState.StartByteString, reader.PeekState());
                    reader.ReadStartByteStringIndefiniteLength();
                    foreach (byte[] expectedChunk in expectedChunks)
                    {
                        Assert.Equal(CborReaderState.ByteString, reader.PeekState());
                        byte[] chunk = reader.ReadByteString();
                        Assert.Equal(expectedChunk.ByteArrayToHex(), chunk.ByteArrayToHex());
                    }
                    Assert.Equal(CborReaderState.EndByteString, reader.PeekState());
                    reader.ReadEndByteStringIndefiniteLength();
                    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()}");
                }