Ejemplo n.º 1
0
        public CborTag ReadTag()
        {
            CborTag tag = PeekTagCore(out int additionalBytes);

            AdvanceBuffer(1 + additionalBytes);
            _isTagContext = true;
            return(tag);
        }
Ejemplo n.º 2
0
        public CborTag ReadTag()
        {
            CborTag tag = PeekTagCore(out int bytesRead);

            AdvanceBuffer(bytesRead);
            _isTagContext = true;
            return(tag);
        }
Ejemplo n.º 3
0
        public void WriteTag(CborTag tag)
        {
            if (!CborConformanceLevelHelpers.AllowsTags(ConformanceLevel))
            {
                throw new InvalidOperationException("Tagged items are not permitted under the current conformance level.");
            }

            WriteUnsignedInteger(CborMajorType.Tag, (ulong)tag);
            _isTagContext = true;
        }
Ejemplo n.º 4
0
        // Implements major type 6 encoding per https://tools.ietf.org/html/rfc7049#section-2.1

        /// <summary>
        ///   Assign a semantic tag (major type 6) to the next data item.
        /// </summary>
        /// <param name="tag">The value to write.</param>
        /// <exception cref="InvalidOperationException">
        ///   Writing a new value exceeds the definite length of the parent data item. -or-
        ///   The major type of the encoded value is not permitted in the parent data item. -or-
        ///   The written data is not accepted under the current conformance mode.
        /// </exception>
        public void WriteTag(CborTag tag)
        {
            if (!CborConformanceModeHelpers.AllowsTags(ConformanceMode))
            {
                throw new InvalidOperationException(SR.Format(SR.Cbor_ConformanceMode_TagsNotSupported, ConformanceMode));
            }

            WriteUnsignedInteger(CborMajorType.Tag, (ulong)tag);
            _isTagContext = true;
        }
Ejemplo 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(encoding.Length, reader.BytesRemaining);
        }
Ejemplo n.º 6
0
        public void ReadTag(CborTag expectedTag)
        {
            CborTag tag = PeekTagCore(out int additionalBytes);

            if (expectedTag != tag)
            {
                throw new InvalidOperationException("CBOR tag mismatch.");
            }

            AdvanceBuffer(1 + additionalBytes);
            _isTagContext = true;
        }
Ejemplo n.º 7
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());
        }
Ejemplo n.º 8
0
        public CborTag ReadTag()
        {
            CborTag tag = PeekTagCore(out int additionalBytes);

            if (_isConformanceLevelCheckEnabled && !CborConformanceLevelHelpers.AllowsTags(ConformanceLevel))
            {
                throw new FormatException("Tagged items are not permitted under the current conformance level.");
            }

            AdvanceBuffer(1 + additionalBytes);
            _isTagContext = true;
            return(tag);
        }
Ejemplo n.º 9
0
        public void ReadTag(CborTag expectedTag)
        {
            CborTag tag = PeekTagCore(out int additionalBytes);

            if (_isConformanceLevelCheckEnabled && !CborConformanceLevelHelpers.AllowsTags(ConformanceLevel))
            {
                throw new FormatException("Tagged items are not permitted under the current conformance level.");
            }

            if (expectedTag != tag)
            {
                throw new InvalidOperationException("CBOR tag mismatch.");
            }

            AdvanceBuffer(1 + additionalBytes);
            _isTagContext = true;
        }
Ejemplo n.º 10
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()}");
                }
Ejemplo n.º 11
0
 public void WriteTag(CborTag tag)
 {
     WriteUnsignedInteger(CborMajorType.Tag, (ulong)tag);
     _isTagContext = true;
 }
Ejemplo n.º 12
0
 public void WriteTag(CborTag tag)
 {
     WriteUnsignedInteger(CborMajorType.Tag, (ulong)tag);
     // NB tag writes do not advance data item counters
     _isTagContext = true;
 }