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.WriteInt64(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 string s: writer.WriteTextString(s); break;

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

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

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

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

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

                default: throw new ArgumentException($"Unrecognized argument type {value.GetType()}");
                }
                ;
            }
        public static void WriteArray_DefiniteLengthExceeded_ShouldThrowInvalidOperationException(int definiteLength)
        {
            using var writer = new CborWriter();
            writer.WriteStartArray(definiteLength);
            for (int i = 0; i < definiteLength; i++)
            {
                writer.WriteInt64(i);
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteInt64(0));
        }
Exemple #3
0
        public static void EndWriteMap_IndefiniteLength_EvenItems_ShouldThrowInvalidOperationException(int length)
        {
            using var writer = new CborWriter();
            writer.WriteStartMapIndefiniteLength();

            for (int i = 1; i < length; i++)
            {
                writer.WriteTextString($"key_{i}");
                writer.WriteInt64(i);
            }

            writer.WriteInt64(0);

            Assert.Throws <InvalidOperationException>(() => writer.WriteEndMap());
        }
Exemple #4
0
        public static void EndWriteMap_DefiniteLengthNotMet_WithNestedData_ShouldThrowInvalidOperationException(int definiteLength)
        {
            using var writer = new CborWriter();
            writer.WriteStartMap(definiteLength);
            for (int i = 1; i < definiteLength; i++)
            {
                writer.WriteTextString($"key_{i}");
                writer.WriteStartMap(definiteLength: 1);
                writer.WriteInt64(i);
                writer.WriteInt64(i);
                writer.WriteEndMap();
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteEndMap());
        }
 public static void IsWriteCompleted_OnWrittenPrimitive_ShouldBeTrue()
 {
     using var writer = new CborWriter();
     Assert.False(writer.IsWriteCompleted);
     writer.WriteInt64(42);
     Assert.True(writer.IsWriteCompleted);
 }
Exemple #6
0
            public static void ExecOperation(CborWriter writer, string op)
            {
                switch (op)
                {
                case nameof(writer.WriteInt64): writer.WriteInt64(42); break;

                case nameof(writer.WriteByteString): writer.WriteByteString(Array.Empty <byte>()); break;

                case nameof(writer.WriteTextString): writer.WriteTextString(""); break;

                case nameof(writer.WriteStartTextStringIndefiniteLength): writer.WriteStartTextStringIndefiniteLength(); break;

                case nameof(writer.WriteStartByteStringIndefiniteLength): writer.WriteStartByteStringIndefiniteLength(); break;

                case nameof(writer.WriteStartArray): writer.WriteStartArrayIndefiniteLength(); break;

                case nameof(writer.WriteStartMap): writer.WriteStartMapIndefiniteLength(); break;

                case nameof(writer.WriteEndByteStringIndefiniteLength): writer.WriteEndByteStringIndefiniteLength(); break;

                case nameof(writer.WriteEndTextStringIndefiniteLength): writer.WriteEndTextStringIndefiniteLength(); break;

                case nameof(writer.WriteEndArray): writer.WriteEndArray(); break;

                case nameof(writer.WriteEndMap): writer.WriteEndMap(); break;

                default: throw new Exception($"Unrecognized CborWriter operation name {op}");
                }
            }
Exemple #7
0
 public static void WriteInt64_SingleValue_HappyPath(long input, string hexExpectedEncoding)
 {
     byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
     using var writer = new CborWriter();
     writer.WriteInt64(input);
     AssertHelper.HexEqual(expectedEncoding, writer.ToArray());
 }
            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()}");
                }
                ;
            }
        public static void CborWriter_WritingTwoPrimitiveValues_ShouldThrowInvalidOperationException()
        {
            using var writer = new CborWriter();
            writer.WriteInt64(42);
            int bytesWritten = writer.BytesWritten;

            Assert.Throws <InvalidOperationException>(() => writer.WriteTextString("lorem ipsum"));
            Assert.Equal(bytesWritten, writer.BytesWritten);
        }
        public static void Roundtrip_Int64(long input)
        {
            using var writer = new CborWriter();
            writer.WriteInt64(input);
            byte[] encoding = writer.GetEncoding();

            var  reader = new CborReader(encoding);
            long result = reader.ReadInt64();

            Assert.Equal(input, result);
        }
        public static void WriteEndArray_DefiniteLengthNotMet_WithNestedData_ShouldThrowInvalidOperationException(int definiteLength)
        {
            using var writer = new CborWriter();
            writer.WriteStartArray(definiteLength);
            for (int i = 1; i < definiteLength; i++)
            {
                writer.WriteStartArray(definiteLength: 1);
                writer.WriteInt64(i);
                writer.WriteEndArray();
            }

            Assert.Throws <InvalidOperationException>(() => writer.WriteEndArray());
        }
Exemple #12
0
            public static void WriteValue(CborWriter writer, object value)
            {
                switch (value)
                {
                case int i: writer.WriteInt64(i); break;

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

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

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

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

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

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

                default: throw new ArgumentException($"Unrecognized argument type {value.GetType()}");
                }
                ;
            }
Exemple #13
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 WriteArray(CborWriter writer, params object[] values)
        {
            writer.WriteStartArray(values.Length);
            foreach (object value in values)
            {
                switch (value)
                {
                case int i: writer.WriteInt64(i); break;

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

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

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

                default: throw new ArgumentException($"Unrecognized argument type {value.GetType()}");
                }
                ;
            }
            writer.WriteEndArray();
        }
Exemple #15
0
 public static void WriteEncodedValue_AtEndOfDefiniteLengthCollection_ShouldThrowInvalidOperationException()
 {
     using var writer = new CborWriter();
     writer.WriteInt64(0);
     Assert.Throws <InvalidOperationException>(() => writer.WriteEncodedValue(new byte[] { 0x01 }));
 }