Пример #1
0
        [InlineData("c202")]               // tagged value
        public static void ReadBoolean_InvalidTypes_ShouldThrowInvalidOperationException(string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Throws <InvalidOperationException>(() => reader.ReadBoolean());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Пример #2
0
        public static void ReadBoolean_SingleValue_HappyPath(bool expectedResult, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Equal(CborReaderState.Boolean, reader.PeekState());
            bool actualResult = reader.ReadBoolean();

            Assert.Equal(expectedResult, actualResult);
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
Пример #3
0
        private protected override bool ReadData(CborReader reader)
        {
            if (reader.PeekState() != CborReaderState.Boolean)
            {
                return(false);
            }

            Data[0] = reader.ReadBoolean();

            return(true);
        }
Пример #4
0
        private static CborObject Read(CborReader reader)
        {
            CborReaderState s = reader.PeekState();

            return(s switch
            {
                CborReaderState.StartMap => ReadMap(reader),
                CborReaderState.StartArray => ReadArray(reader),
                CborReaderState.TextString => new CborTextString(reader.ReadTextString()),
                CborReaderState.Boolean => new CborBoolean(reader.ReadBoolean()),
                CborReaderState.ByteString => new CborByteString(reader.ReadByteString()),
                CborReaderState.UnsignedInteger => new CborInteger(reader.ReadInt64()),
                CborReaderState.NegativeInteger => new CborInteger(reader.ReadInt64()),
                CborReaderState.Null => ReadNull(reader),
                _ => throw new Exception($"Unhandled state. Was {s}")
            });
        public override CborValue Read(ref CborReader reader)
        {
            switch (reader.GetCurrentDataItemType())
            {
            case CborDataItemType.Boolean:
                return(reader.ReadBoolean());

            case CborDataItemType.Null:
                reader.ReadNull();
                return(CborValue.Null);

            case CborDataItemType.Signed:
                return(reader.ReadInt64());

            case CborDataItemType.Unsigned:
                return(reader.ReadUInt64());

            case CborDataItemType.Single:
                return(reader.ReadSingle());

            case CborDataItemType.Double:
                return(reader.ReadDouble());

            case CborDataItemType.Decimal:
                return(reader.ReadDecimal());

            case CborDataItemType.String:
                return(reader.ReadString());

            case CborDataItemType.Array:
                return(((ICborConverter <CborArray>) this).Read(ref reader));

            case CborDataItemType.Map:
                return(((ICborConverter <CborObject>) this).Read(ref reader));

            case CborDataItemType.ByteString:
                return(reader.ReadByteString());

            default:
                throw reader.BuildException("Unexpected data item type");
            }
        }
Пример #6
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()}");
                }
 public override bool Read(ref CborReader reader)
 {
     return(reader.ReadBoolean());
 }