Пример #1
0
        public static void ReadByteString_IndefiniteLengthConcatenated_NestedValues_HappyPath()
        {
            string hexEncoding = "825f41ab40ff5f41ab40ff";

            byte[] data   = hexEncoding.HexToByteArray();
            var    reader = new CborReader(data);

            reader.ReadStartArray();
            Assert.Equal("AB", reader.ReadByteString().ByteArrayToHex());
            Assert.Equal("AB", reader.ReadByteString().ByteArrayToHex());
            reader.ReadEndArray();
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
Пример #2
0
        public static void ReadByteString_IndefiniteLength_AsSingleItem_SupportedConformanceMode_ShouldSucceed(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            reader.ReadByteString();
        }
Пример #3
0
        public static void ReadByteString_EmptyBuffer_ShouldThrowCborContentException()
        {
            byte[] encoding = Array.Empty <byte>();
            var    reader   = new CborReader(encoding);

            Assert.Throws <CborContentException>(() => reader.ReadByteString());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Пример #4
0
        public static void ReadByteString_StringLengthTooLarge_ShouldThrowCborContentException(string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Throws <CborContentException>(() => reader.ReadByteString());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Пример #5
0
        [InlineData("fb3ff199999999999a")] // 1.1
        public static void ReadByteString_InvalidType_ShouldThrowInvalidOperationException(string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Throws <InvalidOperationException>(() => reader.ReadByteString());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Пример #6
0
        public static void ReadByteString_IndefiniteLength_AsSingleItem_UnSupportedConformanceMode_ShouldThrowCborContentException(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            Assert.Throws <CborContentException>(() => reader.ReadByteString());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Пример #7
0
        public static void ReadByteString_NonCanonicalLengths_SupportedConformanceMode_ShouldSucceed(CborConformanceMode mode, string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding, mode);

            byte[] value = reader.ReadByteString();
            Assert.Equal(Array.Empty <byte>(), value);
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
Пример #8
0
        public static void ReadByteStringDefiniteLength_IndefiniteLengthInput_ShouldThrowInvalidOperationException(string hexEncoding)
        {
            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Throws <InvalidOperationException>(() => reader.ReadDefiniteLengthByteString());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
            reader.ReadByteString(); // regular byte string reader should still succeed
        }
Пример #9
0
        public static void ReadByteString_IndefiniteLengthConcatenated_ContainingInvalidMajorTypes_ShouldThrowCborContentException()
        {
            string hexEncoding = "5f4001ff";

            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            Assert.Throws <CborContentException>(() => reader.ReadByteString());
            Assert.Equal(encoding.Length, reader.BytesRemaining);
        }
Пример #10
0
        public static void ReadByteString_IndefiniteLengthConcatenated_SingleValue_HappyPath(string expectedHexValue, string hexEncoding)
        {
            byte[] data   = hexEncoding.HexToByteArray();
            var    reader = new CborReader(data);

            Assert.Equal(CborReaderState.StartIndefiniteLengthByteString, reader.PeekState());
            byte[] actualValue = reader.ReadByteString();
            Assert.Equal(expectedHexValue.ToUpper(), actualValue.ByteArrayToHex());
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
        public static void ReadByteString_SingleValue_HappyPath(string hexExpectedValue, string hexEncoding)
        {
            byte[] encoding      = hexEncoding.HexToByteArray();
            byte[] expectedValue = hexExpectedValue.HexToByteArray();
            var    reader        = new CborReader(encoding);

            byte[] output = reader.ReadByteString();
            Assert.Equal(expectedValue, output);
            Assert.Equal(CborReaderState.Finished, reader.PeekState());
        }
Пример #12
0
        public static void Roundtrip_ByteString(CborConformanceMode mode, byte[] input)
        {
            var writer = new CborWriter(mode);

            writer.WriteByteString(input);
            byte[] encoding = writer.Encode();

            var reader = new CborReader(encoding, mode);

            byte[] result = reader.ReadByteString();
            AssertHelper.HexEqual(input, result);
        }
        public static void Roundtrip_ByteString(string hexInput)
        {
            byte[] input = hexInput.HexToByteArray();
#endif
            var writer = new CborWriter();
            writer.WriteByteString(input);
            byte[] encoding = writer.Encode();

            var    reader = new CborReader(encoding);
            byte[] result = reader.ReadByteString();
            AssertHelper.HexEqual(input ?? Array.Empty <byte>(), result);
        }
Пример #14
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}")
            });
Пример #15
0
        public static void ReadByteString_IndefiniteLength_ContainingInvalidMajorTypes_ShouldThrowCborContentException()
        {
            string hexEncoding = "5f4001ff";

            byte[] encoding = hexEncoding.HexToByteArray();
            var    reader   = new CborReader(encoding);

            reader.ReadStartIndefiniteLengthByteString();
            reader.ReadByteString();

            int bytesRemaining = reader.BytesRemaining;

            Assert.Throws <CborContentException>(() => reader.PeekState());
            Assert.Throws <CborContentException>(() => reader.ReadInt64());
            Assert.Equal(bytesRemaining, reader.BytesRemaining);
        }
        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");
            }
        }
Пример #17
0
        public static void Roundtrip_IndefiniteByteString(CborConformanceMode mode, byte[][] chunks)
        {
            bool convertIndefiniteLengthEncodings = mode is CborConformanceMode.Canonical or CborConformanceMode.Ctap2Canonical;
            var  writer = new CborWriter(convertIndefiniteLengthEncodings: convertIndefiniteLengthEncodings);

            writer.WriteStartIndefiniteLengthByteString();
            foreach (byte[] chunk in chunks)
            {
                writer.WriteByteString(chunk);
            }
            writer.WriteEndIndefiniteLengthByteString();

            byte[] encoding = writer.Encode();

            var reader = new CborReader(encoding);

            byte[] expected = chunks.SelectMany(ch => ch).ToArray();
            byte[] result   = reader.ReadByteString();
            AssertHelper.HexEqual(expected, result);
        }
Пример #18
0
        public byte[] GetValueAsBytes()
        {
            var reader = new CborReader(EncodedValue);

            byte[] retVal;

            try
            {
                retVal = reader.ReadByteString();
            }
            catch (Exception ex) when(ex is CborContentException or InvalidOperationException)
            {
                throw new InvalidOperationException(SR.CoseHeaderValueErrorWhileDecoding, ex);
            }

            if (reader.BytesRemaining != 0)
            {
                throw new InvalidOperationException(SR.Format(SR.CoseHeaderMapCborEncodedValueNotValid));
            }

            return(retVal);
        }
Пример #19
0
 public override ReadOnlyMemory <byte> Read(ref CborReader reader)
 {
     return(new ReadOnlyMemory <byte>(reader.ReadByteString().ToArray()));
 }
Пример #20
0
 public override byte[] Read(ref CborReader reader)
 {
     return(reader.ReadByteString().ToArray());
 }
Пример #21
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 Guid Read(ref CborReader reader)
        {
            ReadOnlySpan <byte> bytes = reader.ReadByteString();

            return(new Guid(bytes));
        }