public static void WriteEncodedValue_AtEndOfDefiniteLengthCollection_ShouldThrowInvalidOperationException()
        {
            var writer = new CborWriter();

            writer.WriteInt64(0);
            Assert.Throws <InvalidOperationException>(() => writer.WriteEncodedValue(new byte[] { 0x01 }));
        }
        [InlineData(CborConformanceMode.Ctap2Canonical, "a280800101")] // unsorted key encodings
        public static void WriteEncodedValue_InvalidConformance_ShouldThrowArgumentException(CborConformanceMode conformanceMode, string hexEncodedInput)
        {
            byte[] encodedInput = hexEncodedInput.HexToByteArray();
            var    writer       = new CborWriter(conformanceMode);

            Assert.Throws <ArgumentException>(() => writer.WriteEncodedValue(encodedInput));
        }
        public static void WriteEncodedValue_BadIndefiniteLengthStringValue_ShouldThrowInvalidOperationException()
        {
            var writer = new CborWriter();

            writer.WriteStartIndefiniteLengthTextString();
            Assert.Throws <InvalidOperationException>(() => writer.WriteEncodedValue(new byte[] { 0x01 }));
        }
Example #4
0
            public static void WriteValue(CborWriter writer, object value, bool useDefiniteLengthCollections = true)
            {
                switch (value)
                {
                case null: writer.WriteNull(); break;

                case bool b: writer.WriteBoolean(b); break;

                case int i: writer.WriteInt32(i); break;

                case long i: writer.WriteInt64(i); break;

                case ulong i: writer.WriteUInt64(i); break;

                case float f: writer.WriteSingle(f); break;

                case double d: writer.WriteDouble(d); break;

                case decimal d: writer.WriteDecimal(d); break;

                case string s: writer.WriteTextString(s); break;

                case BigInteger i: writer.WriteBigInteger(i); break;

                case DateTimeOffset d: writer.WriteDateTimeOffset(d); break;

                case byte[] b: writer.WriteByteString(b); break;

                case byte[][] chunks: WriteChunkedByteString(writer, chunks); break;

                case string[] chunks when IsIndefiniteLengthByteString(chunks):
                    byte[][]  byteChunks = chunks.Skip(1).Select(ch => ch.HexToByteArray()).ToArray();

                    WriteChunkedByteString(writer, byteChunks);
                    break;

                case string[] chunks: WriteChunkedTextString(writer, chunks); break;

                case object[] nested when IsCborMapRepresentation(nested): WriteMap(writer, nested, useDefiniteLengthCollections); break;

                case object[] nested when IsEncodedValueRepresentation(nested):
                    byte[]    encodedValue = ((string)nested[1]).HexToByteArray();

                    writer.WriteEncodedValue(encodedValue);
                    break;

                case object[] nested when IsTaggedValueRepresentation(nested):
                    writer.WriteTag((CborTag)nested[0]);

                    WriteValue(writer, nested[1]);
                    break;

                case object[] nested: WriteArray(writer, nested, useDefiniteLengthCollections); break;

                default: throw new ArgumentException($"Unrecognized argument type {value.GetType()}");
                }
                ;
            }
Example #5
0
        public static void WriteEncodedValue_RootValue_HappyPath(string hexEncodedValue)
        {
            byte[] encodedValue = hexEncodedValue.HexToByteArray();

            using var writer = new CborWriter();
            writer.WriteEncodedValue(encodedValue);

            string hexResult = writer.GetEncoding().ByteArrayToHex();

            Assert.Equal(hexEncodedValue, hexResult.ToLower());
        }
        public static void WriteEncodedValue_IndefiniteLengthByteString_HappyPath()
        {
            var writer = new CborWriter(convertIndefiniteLengthEncodings: false);

            writer.WriteStartIndefiniteLengthByteString();
            writer.WriteByteString(new byte[] { 1, 1, 1 });
            writer.WriteEncodedValue("43020202".HexToByteArray());
            writer.WriteEndIndefiniteLengthByteString();

            byte[] encoding = writer.Encode();
            Assert.Equal("5f4301010143020202ff", encoding.ByteArrayToHex().ToLower());
        }
        public static void WriteEncodedValue_IndefiniteLengthTextString_HappyPath()
        {
            var writer = new CborWriter(convertIndefiniteLengthEncodings: false);

            writer.WriteStartIndefiniteLengthTextString();
            writer.WriteTextString("foo");
            writer.WriteEncodedValue("63626172".HexToByteArray());
            writer.WriteEndIndefiniteLengthTextString();

            byte[] encoding = writer.Encode();
            Assert.Equal("7f63666f6f63626172ff", encoding.ByteArrayToHex().ToLower());
        }
Example #8
0
        public static void WriteEncodedValue_IndefiniteLengthByteString_HappyPath()
        {
            using var writer = new CborWriter(encodeIndefiniteLengths: true);

            writer.WriteStartByteString();
            writer.WriteByteString(new byte[] { 1, 1, 1 });
            writer.WriteEncodedValue("43020202".HexToByteArray());
            writer.WriteEndByteString();

            byte[] encoding = writer.GetEncoding();
            Assert.Equal("5f4301010143020202ff", encoding.ByteArrayToHex().ToLower());
        }
Example #9
0
        public static void WriteEncodedValue_IndefiniteLengthTextString_HappyPath()
        {
            using var writer = new CborWriter(encodeIndefiniteLengths: true);

            writer.WriteStartTextString();
            writer.WriteTextString("foo");
            writer.WriteEncodedValue("63626172".HexToByteArray());
            writer.WriteEndTextString();

            byte[] encoding = writer.GetEncoding();
            Assert.Equal("7f63666f6f63626172ff", encoding.ByteArrayToHex().ToLower());
        }
Example #10
0
        public static void WriteEncodedValue_NestedValue_HappyPath(string hexEncodedValue)
        {
            byte[] encodedValue = hexEncodedValue.HexToByteArray();

            using var writer = new CborWriter();
            writer.WriteStartArray(3);
            writer.WriteInt64(1);
            writer.WriteEncodedValue(encodedValue);
            writer.WriteTextString("");
            writer.WriteEndArray();

            string hexResult = writer.GetEncoding().ByteArrayToHex();

            Assert.Equal("8301" + hexEncodedValue + "60", hexResult.ToLower());
        }
        public static void WriteEncodedValue_ValidPayloadWithTrailingBytes_ShouldThrowArgumentException()
        {
            var writer = new CborWriter();

            Assert.Throws <ArgumentException>(() => writer.WriteEncodedValue(new byte[] { 0x01, 0x01 }));
        }
Example #12
0
 public static void WriteEncodedValue_InvalidCbor_ShouldThrowArgumentException(string hexEncodedInput)
 {
     byte[] encodedInput = hexEncodedInput.HexToByteArray();
     using var writer = new CborWriter();
     Assert.Throws <ArgumentException>(() => writer.WriteEncodedValue(encodedInput));
 }